forked from voutcn/megahit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdbg_multi_io.h
414 lines (342 loc) · 12 KB
/
sdbg_multi_io.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#ifndef SDBG_MULTI_IO__
#define SDBG_MULTI_IO__
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string>
#include <vector>
#include <algorithm>
#include "definitions.h"
#include "utils.h"
#include "mem_file_checker-inl.h"
struct SdbgPartitionRecord {
int thread_id;
long long starting_offset;
long long num_items;
long long num_tips;
long long num_large_mul;
long long num_last1;
long long num_w[9];
SdbgPartitionRecord(): thread_id(-1), starting_offset(0), num_items(0),
num_tips(0), num_large_mul(0), num_last1(0) {
memset(num_w, 0, sizeof(num_w));
}
};
class SdbgWriter {
private:
std::string file_prefix_;
int num_threads_;
int num_buckets_;
std::vector<FILE *> files_;
std::vector<int> cur_bucket_;
std::vector<int64_t> cur_thread_offset_; // offset in BYTE
std::vector<SdbgPartitionRecord> p_rec_;
bool is_opened_;
int kmer_size_;
int words_per_tip_label_;
public:
SdbgWriter() {}
~SdbgWriter() {
destroy();
}
void set_num_threads(int num_threads) {
num_threads_ = num_threads;
}
void set_file_prefix(const std::string &file_prefix) {
file_prefix_ = file_prefix;
}
void set_kmer_size(int k) {
kmer_size_ = k;
words_per_tip_label_ = DivCeiling(k * 2, 32);
}
void set_num_buckets(int num_buckets) {
num_buckets_ = num_buckets;
}
void init_files() {
files_.resize(num_threads_);
cur_bucket_.resize(num_threads_, -1);
cur_thread_offset_.resize(num_threads_, 0);
p_rec_.resize(num_buckets_);
for (int i = 0; i < num_threads_; ++i) {
files_[i] = OpenFileAndCheck(FormatString("%s.sdbg.%d", file_prefix_.c_str(), i), "wb");
}
is_opened_ = true;
}
void write(int tid, int32_t bucket, int w, int last, int tip, multi_t multiplicity, uint32_t *packed_tip_label) {
assert(tid < num_threads_);
if (bucket != cur_bucket_[tid]) {
cur_bucket_[tid] = bucket;
assert(p_rec_[bucket].thread_id == -1);
p_rec_[bucket].thread_id = tid;
p_rec_[bucket].starting_offset = cur_thread_offset_[tid];
}
uint16_t packed_sdbg_item = w | (last << 4) | (tip << 5) | (std::min(multiplicity, (multi_t)kMulti2Sp) << 8);
fwrite(&packed_sdbg_item, sizeof(uint16_t), 1, files_[tid]);
++p_rec_[bucket].num_items;
++p_rec_[bucket].num_w[w];
p_rec_[bucket].num_last1 += last;
cur_thread_offset_[tid] += sizeof(uint16_t);
if (multiplicity > kMaxMulti2_t) {
fwrite(&multiplicity, sizeof(multi_t), 1, files_[tid]);
multiplicity = kMulti2Sp;
++p_rec_[bucket].num_large_mul;
cur_thread_offset_[tid] += sizeof(multi_t);
}
if (tip) {
fwrite(packed_tip_label, sizeof(uint32_t), words_per_tip_label_, files_[tid]);
++p_rec_[bucket].num_tips;
cur_thread_offset_[tid] += sizeof(uint32_t) * words_per_tip_label_;
}
}
int64_t num_edges() {
int64_t total_edges = 0;
for (int i = 0; i < num_buckets_; ++i) {
total_edges += p_rec_[i].num_items;
}
return total_edges;
}
int64_t num_w(int w) {
int64_t ret = 0;
for (int i = 0; i < num_buckets_; ++i) {
ret += p_rec_[i].num_w[w];
}
return ret;
}
int64_t num_last1() {
int64_t ret = 0;
for (int i = 0; i < num_buckets_; ++i) {
ret += p_rec_[i].num_last1;
}
return ret;
}
int64_t num_tips() {
int64_t ret = 0;
for (int i = 0; i < num_buckets_; ++i) {
ret += p_rec_[i].num_tips;
}
return ret;
}
void destroy() {
if (is_opened_) {
for (int i = 0; i < num_threads_; ++i) {
fclose(files_[i]);
}
FILE *sdbg_info = OpenFileAndCheck((file_prefix_ + ".sdbg_info").c_str(), "w");
fprintf(sdbg_info, "k %d\n", kmer_size_);
fprintf(sdbg_info, "words_per_tip_label %d\n", words_per_tip_label_);
fprintf(sdbg_info, "num_buckets %d\n", num_buckets_);
fprintf(sdbg_info, "num_threads %d\n", num_threads_);
int64_t total_edges = 0;
int64_t total_tips = 0;
int64_t total_large_mul = 0;
for (int i = 0; i < num_buckets_; ++i) {
total_edges += p_rec_[i].num_items;
total_tips += p_rec_[i].num_tips;
total_large_mul += p_rec_[i].num_large_mul;
}
fprintf(sdbg_info, "total_size %lld\n", (long long)total_edges);
fprintf(sdbg_info, "num_tips %lld\n", (long long)total_tips);
fprintf(sdbg_info, "large_multi %lld\n", (long long)total_large_mul);
for (int i = 0; i < num_buckets_; ++i) {
fprintf(sdbg_info, "%d %d %lld %lld %lld %lld\n", i,
p_rec_[i].thread_id,
(long long)p_rec_[i].starting_offset,
(long long)p_rec_[i].num_items,
(long long)p_rec_[i].num_tips,
(long long)p_rec_[i].num_large_mul);
}
fclose(sdbg_info);
files_.clear();
cur_bucket_.clear();
cur_thread_offset_.clear(); // offset in BYTE
p_rec_.clear();
is_opened_ = false;
}
}
};
class SdbgReader {
private:
std::string file_prefix_;
int kmer_size_;
int words_per_tip_label_;
long long num_items_;
long long num_tips_;
long long num_large_mul_;
long long f_[6];
int num_files_;
int num_buckets_;
int pre_lkt_len_;
int pre_lkt_size_;
int cur_bucket_;
long long cur_bucket_cnt_;
long long cur_vol_;
void *cur_bucket_ptr_;
std::vector<SdbgPartitionRecord> p_rec_;
std::vector<long long> pre_lkt_;
std::vector<long long> file_sizes_;
std::vector<int> fds_;
void *mmap_;
long page_size_;
int64_t mmap_size_;
bool is_opened_;
public:
SdbgReader(): is_opened_(false) {}
~SdbgReader() {
destroy();
}
void set_file_prefix(const std::string &prefix) {
file_prefix_ = prefix;
}
void read_info() {
FILE *sdbg_info = OpenFileAndCheck((file_prefix_ + ".sdbg_info").c_str(), "r");
assert(fscanf(sdbg_info, "k %d\n", &kmer_size_) == 1);
assert(fscanf(sdbg_info, "words_per_tip_label %d\n", &words_per_tip_label_) == 1);
assert(fscanf(sdbg_info, "num_buckets %d\n", &num_buckets_) == 1);
assert(fscanf(sdbg_info, "num_threads %d\n", &num_files_) == 1);
assert(fscanf(sdbg_info, "total_size %lld\n", &num_items_) == 1);
assert(fscanf(sdbg_info, "num_tips %lld\n", &num_tips_) == 1);
assert(fscanf(sdbg_info, "large_multi %lld\n", &num_large_mul_) == 1);
p_rec_.resize(num_buckets_);
file_sizes_.resize(num_files_, 0);
long long acc = 0;
f_[0] = -1;
f_[1] = 0;
for (int i = 0; i < num_buckets_; ++i) {
int dummy;
assert(fscanf(sdbg_info, "%d %d %lld %lld %lld %lld\n", &dummy,
&p_rec_[i].thread_id,
&p_rec_[i].starting_offset,
&p_rec_[i].num_items,
&p_rec_[i].num_tips,
&p_rec_[i].num_large_mul) == 6);
file_sizes_[p_rec_[i].thread_id] += p_rec_[i].num_items * sizeof(uint16_t) + p_rec_[i].num_tips * sizeof(uint32_t) * words_per_tip_label_ + p_rec_[i].num_large_mul * sizeof(multi_t);
acc += p_rec_[i].num_items;
f_[i / (num_buckets_ / 4) + 2] = acc;
}
if (fscanf(sdbg_info, "%d", &pre_lkt_len_) == EOF) {
pre_lkt_len_ = 0;
pre_lkt_size_ = num_buckets_;
while ((1 << (pre_lkt_len_ * 2)) < num_buckets_) {
++pre_lkt_len_;
}
pre_lkt_.resize(pre_lkt_size_ * 2);
for (long long acc = 0, i = 0; i < pre_lkt_size_; ++i) {
pre_lkt_[i * 2] = acc;
acc += p_rec_[i].num_items;
pre_lkt_[i * 2 + 1] = acc - 1;
}
} else {
pre_lkt_size_ = 1 << (pre_lkt_len_ * 2);
pre_lkt_.resize(pre_lkt_size_ * 2);
for (int i = 0; i < pre_lkt_size_; ++i) {
assert(fscanf(sdbg_info, "%lld%lld", &pre_lkt_[i*2], &pre_lkt_[i*2+1]) == 2);
}
}
fclose(sdbg_info);
}
int kmer_size() const {
return kmer_size_;
}
int words_per_tip_label() const {
return words_per_tip_label_;
}
int prefix_lkt_len() const {
return pre_lkt_len_;
}
int prefix_lkt_size() const {
return pre_lkt_size_;
}
long long num_items() const {
return num_items_;
}
long long num_tips() const {
return num_tips_;
}
long long num_large_mul() const {
return num_large_mul_;
}
const long long *f() const {
return f_;
}
long long bucket_size(int i) const {
return p_rec_[i].num_items;
}
long long prefix_lkt(int i) const {
return pre_lkt_[i];
}
void init_files() {
assert(!is_opened_);
fds_.resize(num_files_);
page_size_ = sysconf(_SC_PAGESIZE);
for (int i = 0; i < num_files_; ++i) {
fds_[i] = open(FormatString("%s.sdbg.%d", file_prefix_.c_str(), i), O_RDONLY);
assert(fds_[i] != -1);
}
cur_bucket_ = -1;
cur_bucket_cnt_ = 0;
cur_vol_ = 0;
mmap_ = NULL;
mmap_size_ = 0;
is_opened_ = true;
}
bool NextItem(uint16_t &item) {
if (cur_bucket_ >= num_buckets_) {
return false;
}
while (cur_bucket_cnt_ >= cur_vol_) {
cur_bucket_cnt_ = 0;
++cur_bucket_;
while (cur_bucket_ < num_buckets_ && p_rec_[cur_bucket_].thread_id == -1) {
++cur_bucket_;
}
if (cur_bucket_ >= num_buckets_) {
return false;
}
if (mmap_) {
munmap(mmap_, mmap_size_);
mmap_ = NULL;
}
int64_t offset = p_rec_[cur_bucket_].starting_offset / page_size_ * page_size_;
mmap_size_ = p_rec_[cur_bucket_].num_items * sizeof(uint16_t) +
p_rec_[cur_bucket_].num_tips * sizeof(uint32_t) * words_per_tip_label_ +
p_rec_[cur_bucket_].num_large_mul * sizeof(multi_t);
mmap_size_ += p_rec_[cur_bucket_].starting_offset - offset;
mmap_ = mmap(NULL, mmap_size_, PROT_READ, MAP_PRIVATE, fds_[p_rec_[cur_bucket_].thread_id], offset);
assert(mmap_ != NULL);
madvise(mmap_, mmap_size_, MADV_SEQUENTIAL);
cur_bucket_ptr_ = (char*)mmap_ + p_rec_[cur_bucket_].starting_offset - offset;
cur_vol_ = p_rec_[cur_bucket_].num_items;
}
++cur_bucket_cnt_;
item = *((uint16_t *)cur_bucket_ptr_);
cur_bucket_ptr_ = (void *)((char *)cur_bucket_ptr_ + sizeof(uint16_t));
return true;
}
multi_t NextLargeMul() {
multi_t large_mul = *((multi_t *)cur_bucket_ptr_);
cur_bucket_ptr_ = (void *)((char *)cur_bucket_ptr_ + sizeof(multi_t));
return large_mul;
}
void NextTipLabel(uint32_t *tip_label) {
memcpy(tip_label, cur_bucket_ptr_, sizeof(uint32_t) * words_per_tip_label_);
cur_bucket_ptr_ = (void *)((char *)cur_bucket_ptr_ + sizeof(uint32_t) * words_per_tip_label_);
}
void destroy() {
if (is_opened_) {
if (mmap_) {
munmap(mmap_, mmap_size_);
mmap_ = NULL;
}
for (int i = 0; i < num_files_; ++i) {
close(fds_[i]);
}
file_sizes_.clear();
fds_.clear();
is_opened_ = false;
}
}
};
#endif