forked from voutcn/megahit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cx1_functions.cpp
2639 lines (2413 loc) · 118 KB
/
cx1_functions.cpp
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* cx1_functions.cpp
* This file is a part of MEGAHIT
*
* Copyright (C) 2014 The University of Hong Kong
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <pthread.h>
#include <zlib.h>
#include <assert.h>
#include <algorithm>
#include <string>
#include <omp.h>
#include "timer.h"
#include "definitions.h"
#include "io-utility.h"
#include "helper_functions-inl.h"
#include "mem_file_checker-inl.h"
#include "sdbg_builder_util.h"
#include "sdbg_builder_writers.h"
#include "kmer_uint32.h"
#include "lv2_cpu_sort.h"
#include "MAC_pthread_barrier.h"
#include "kseq.h"
#ifndef DISABLE_GPU
#include "lv2_gpu_functions.h"
#endif
// *** func & def commonly used in both phases ***
#define PACKED_READS(i, globals) ((globals).packed_reads + (i) * (globals).words_per_read)
#define GPU_BYTES_PER_ITEM 16 // key & value, 4 bytes each. double for radix sort internal buffer
#define LV1_BYTES_PER_ITEM 4 // 32-bit differential offset
/**
* @brief extract the nth char in a packed read/edge
*/
inline int ExtractNthChar(edge_word_t *read_ptr, int n) {
int which_word = n / kCharsPerEdgeWord;
int index_in_word = n % kCharsPerEdgeWord;
return (read_ptr[which_word] >> (kBitsPerEdgeChar * (kCharsPerEdgeWord - 1 - index_in_word))) & kEdgeCharMask;
}
/**
* @brief map ACGT to 0123
*/
char dna_map[256];
void InitDNAMap() {
dna_map['A'] = dna_map['a'] = 0;
dna_map['C'] = dna_map['c'] = 1;
dna_map['G'] = dna_map['g'] = 2;
dna_map['T'] = dna_map['t'] = 3;
dna_map['N'] = dna_map['n'] = 2; // -> G
}
/**
* @brief auto adjust lv1 and lv2 number items according to available memory
*/
void GetNumItems(struct global_data_t &globals, int64_t mem_remained, int64_t lv2_bytes_per_item) {
int64_t min_lv1_items = globals.tot_bucket_size / (kMaxLv1ScanTime - 0.5);
int64_t min_lv2_items = std::max(globals.max_bucket_size, kMinLv2BatchSize);
// --- adjust max_lv2_items to fit memory ---
while (globals.max_lv2_items >= min_lv2_items) {
int64_t mem_lv2 = lv2_bytes_per_item * globals.max_lv2_items;
if (mem_remained <= mem_lv2) {
globals.max_lv2_items *= 0.9;
continue;
}
globals.max_lv1_items = (mem_remained - mem_lv2) / LV1_BYTES_PER_ITEM;
if (globals.max_lv1_items < min_lv1_items ||
globals.max_lv1_items < globals.max_lv2_items) {
globals.max_lv2_items *= 0.9;
} else {
break;
}
}
if (globals.max_lv2_items < min_lv2_items) {
err("[ERROR B::%s] No enough memory to process.\n", __func__);
exit(1);
}
// --- adjust max_lv2_items to fit more lv1 item ---
// TODO: 4 is arbitrary chosen, not fine tune
while (globals.max_lv2_items * 4 > globals.max_lv1_items) {
if (globals.max_lv2_items * 0.95 >= min_lv2_items) {
globals.max_lv2_items *= 0.95;
globals.max_lv1_items = (mem_remained - lv2_bytes_per_item * globals.max_lv2_items) / LV1_BYTES_PER_ITEM;
} else {
break;
}
}
}
/**
* @param end_limit the max allowed value for end_bucket
* @param num_items total num of items written to num_items by reference
* @return end_bucket such that bucket_sizes[start_bucket..end_bucket-1] sums up to at most item_limit
*/
int FindEndBucket(int64_t * bucket_sizes, int start_bucket, int end_limit, int64_t item_limit, int64_t &num_items) {
num_items = 0;
int end_bucket = start_bucket;
while (end_bucket < end_limit) { // simple linear scan
if (num_items + bucket_sizes[end_bucket] > item_limit) {
return end_bucket;
}
num_items += bucket_sizes[end_bucket];
end_bucket++;
}
return end_limit;
}
// single thread helper function
void Lv1ComputeBucketOffset(struct global_data_t &globals) {
// compute "global" (thread 0) offsets first
int64_t *offsets = globals.readpartitions[0].rp_bucket_offsets;
offsets[globals.lv1_start_bucket] = 0;
for (int b = globals.lv1_start_bucket + 1; b < globals.lv1_end_bucket; ++b) {
offsets[b] = offsets[b-1] + globals.bucket_sizes[b-1]; // accumulate
}
// then for each read partition
for (int t = 1; t < globals.num_cpu_threads; ++t) {
int64_t *this_offsets = globals.readpartitions[t].rp_bucket_offsets;
int64_t *prev_offsets = globals.readpartitions[t-1].rp_bucket_offsets;
int64_t *sizes = globals.readpartitions[t-1].rp_bucket_sizes;
for (int b = globals.lv1_start_bucket; b < globals.lv1_end_bucket; ++b) {
this_offsets[b] = prev_offsets[b] + sizes[b];
}
}
}
// single thread helper function
void Lv2DistributeBucketPartitions(struct global_data_t &globals, int num_output_threads) {
int64_t average = globals.lv2_num_items / (globals.num_cpu_threads - num_output_threads); // recall: we only have globals.num_cpu_threads-globals.phase1_num_output_threads bucketpartitions
int bucket = globals.lv2_start_bucket;
for (int t = 0; t < globals.num_cpu_threads-num_output_threads-1; ++t) {
int64_t num_items = 0;
globals.bucketpartitions[t].bp_start_bucket = bucket;
while (bucket < globals.lv2_end_bucket) {
num_items += globals.bucket_sizes[bucket++];
if (num_items >= average) {
break;
}
}
globals.bucketpartitions[t].bp_end_bucket = bucket;
}
// last
globals.bucketpartitions[globals.num_cpu_threads-num_output_threads-1].bp_start_bucket = bucket;
globals.bucketpartitions[globals.num_cpu_threads-num_output_threads-1].bp_end_bucket = globals.lv2_end_bucket;
}
namespace phase1 {
// debug
void DumpPackedReads(edge_word_t* p, int read_length) {
for (int i=0; i<read_length; ++i) {
int j = i;
log("%c", dna_chars[ (*(p+(j/kCharsPerEdgeWord)) >> ((kCharsPerEdgeWord-1-j%kCharsPerEdgeWord)*kBitsPerEdgeChar)) & kEdgeCharMask ]);
}
}
/*
* Packs an ASCII read into 2-bit per base form. The last base goes to the MSB of the first word.
* -Params-
* read: the read, in ASCII ACGT
* p: a pointer to the first edge_word_t of the packed sequence to be written to
* read_length: number of bases in the read
* last_word_shift: the number of empty bits in the last word. we need to shift the bits up by this amount. solely determined by read_length.
*/
inline void PackReadFromAscii(char* read, edge_word_t* p, int read_length, int words_per_read) {
// for de Bruijn graph construction, packing the reverse is more convenient
edge_word_t w = 0;
int i, j;
for (i = 0, j = 0; j < read_length; ++j) {
if (j % kCharsPerEdgeWord == 0 && j) { // TODO bitwise?
*(p++) = w;
w = 0;
++i;
}
while (read[j] == 'N') {
break;
}
w = (w << kBitsPerEdgeChar) | dna_map[ (int)read[ j ] ];
}
int last_word_shift = j % kCharsPerEdgeWord;
last_word_shift = last_word_shift ? (kCharsPerEdgeWord - last_word_shift) * kBitsPerEdgeChar : 0;
*p = w << last_word_shift;
while (++i < words_per_read) {
*(++p) = 0;
}
*p |= read_length;
}
/**
* @brief encode read_id and its offset in one int64_t
*/
inline int64_t EncodeOffset(int64_t read_id, int offset, int strand, int length_num_bits) {
return (read_id << (length_num_bits + 1)) | (offset << 1) | strand;
}
inline int GetReadLength(edge_word_t* read_p, int words_per_read, int mask) {
return *(read_p + words_per_read - 1) & mask;
}
inline int GetReadLengthByID(int64_t id, global_data_t &globals) {
return *(globals.packed_reads + (id + 1) * globals.words_per_read - 1) & globals.read_length_mask;
}
/**
* @brief read fastx queries and pack
*/
void ReadInputFile(struct global_data_t &globals) {
int64_t num_reads = 0;
int bits_read_length = 1; // bit needed to store read_length
while ((1 << bits_read_length) - 1 < globals.max_read_length) {
++bits_read_length;
}
globals.words_per_read = DivCeiling(globals.max_read_length * kBitsPerEdgeChar + bits_read_length, kBitsPerEdgeWord);
int64_t max_num_reads = globals.host_mem / (sizeof(edge_word_t) * globals.words_per_read) * 3 / 4; //TODO: more accurate
int read_length;
edge_word_t *packed_reads;
edge_word_t *packed_reads_p; // current pointer
globals.read_length_mask = (1 << bits_read_length) - 1;
globals.capacity = std::min(max_num_reads, int64_t(1048576)); // initial capacity 1M
gzFile fp = strcmp(globals.input_file, "-") ? gzopen(globals.input_file, "r") : gzdopen(fileno(stdin), "r");
kseq_t *seq = kseq_init(fp); // kseq to read files
packed_reads_p = packed_reads = (edge_word_t*) MallocAndCheck(globals.capacity * globals.words_per_read * sizeof(edge_word_t), __FILE__, __LINE__);
log("[B::%s] Max read length is %d; words per read: %d\n", __func__, globals.max_read_length, globals.words_per_read);
// --- main reading loop ---
while ((read_length = kseq_read(seq)) >= 0) {
std::reverse(seq->seq.s, seq->seq.s + read_length);
char *next_p = seq->seq.s;
while (read_length > globals.kmer_k) {
int scan_len = 0;
while (scan_len < read_length && next_p[scan_len] != 'N') {
++scan_len;
}
if (scan_len > globals.kmer_k && scan_len <= globals.max_read_length) {
if (num_reads >= globals.capacity) {
if (globals.capacity == max_num_reads) {
err("[B::%s WRANING] No enough memory to hold all the reads. Only the first %llu reads are kept.\n", __func__, num_reads);
break;
}
globals.capacity = std::min(globals.capacity * 2, max_num_reads);
edge_word_t *new_ptr = (edge_word_t*) realloc(packed_reads, globals.capacity * globals.words_per_read * sizeof(edge_word_t));
if (new_ptr != NULL) {
packed_reads = new_ptr;
packed_reads_p = packed_reads + globals.words_per_read * num_reads;
globals.capacity = globals.capacity;
} else {
err("[B::%s WRANING] No enough memory to hold all the reads. Only the first %llu reads are kept.\n", __func__, num_reads);
break;
}
}
// read length is ok! compress and store the packed read
PackReadFromAscii(next_p, packed_reads_p, scan_len, globals.words_per_read);
packed_reads_p += globals.words_per_read;
++num_reads;
} else if (scan_len > globals.max_read_length) { // this read length is wrong
err("[B::%s WARNING] Found a read of length %d > max read length = %d\n, it will be discarded.", __func__, scan_len, globals.max_read_length);
}
while (scan_len < read_length && next_p[scan_len] == 'N') {
++scan_len;
}
read_length -= scan_len;
next_p += scan_len;
}
}
globals.num_reads = num_reads;
globals.mem_packed_reads = globals.num_reads * globals.words_per_read * sizeof(edge_word_t);
globals.packed_reads = (edge_word_t*) ReAllocAndCheck(packed_reads, globals.mem_packed_reads, __FILE__, __LINE__);
if (!globals.packed_reads) {
err("[B::%s ERROR] Cannot reallocate memory for packed reads!\n", __func__);
exit(1);
}
kseq_destroy(seq);
gzclose(fp);
}
/**
* @brief init memory for bucket scan
*/
void PrepareBucketScan(struct global_data_t &globals) {
// init read partitions
for (int t = 0; t < globals.num_cpu_threads; ++t) {
struct readpartition_data_t &rp = globals.readpartitions[t];
rp.rp_id = t;
rp.globals = &globals;
rp.rp_bucket_sizes = (int64_t *) MallocAndCheck(phase1::kNumBuckets * sizeof(int64_t), __FILE__, __LINE__);
rp.rp_bucket_offsets = (int64_t *) MallocAndCheck(phase1::kNumBuckets * sizeof(int64_t), __FILE__, __LINE__);
// distribute reads to partitions
int64_t average = globals.num_reads / globals.num_cpu_threads;
rp.rp_start_id = t * average;
rp.rp_end_id = t < globals.num_cpu_threads - 1 ? (t + 1) * average : globals.num_reads;
rp.rp_lv1_differential_base = EncodeOffset(rp.rp_start_id, 0, 0, globals.offset_num_bits);
}
// init bucket partitions
for (int t = 0; t < globals.num_cpu_threads - globals.phase1_num_output_threads; ++t) {
struct bucketpartition_data_t &bp = globals.bucketpartitions[t];
bp.bp_id = t;
bp.globals = &globals;
}
globals.bucket_sizes = (int64_t *) MallocAndCheck(phase1::kNumBuckets * sizeof(int64_t), __FILE__, __LINE__);
}
void* PreprocessScanToFillBucketSizesThread(void *_data) {
struct readpartition_data_t &rp = *((struct readpartition_data_t*) _data);
struct global_data_t &globals = *(rp.globals);
int64_t *bucket_sizes = rp.rp_bucket_sizes;
memset(bucket_sizes, 0, phase1::kNumBuckets * sizeof(int64_t));
edge_word_t *read_p = PACKED_READS(rp.rp_start_id, globals);
KmerUint32 edge, rev_edge; // (k+1)-mer and its rc
for (int64_t read_id = rp.rp_start_id; read_id < rp.rp_end_id; ++read_id, read_p += globals.words_per_read) {
int read_length = GetReadLength(read_p, globals.words_per_read, globals.read_length_mask);
if (read_length < globals.kmer_k + 1) { continue; }
edge.init(read_p, globals.kmer_k + 1);
rev_edge.clean();
for (int i = 0; i <= globals.kmer_k; ++i) {
rev_edge.Append(3 - ExtractNthChar(read_p, globals.kmer_k - i));
}
int last_char_offset = globals.kmer_k;
while (true) {
if (rev_edge < edge) {
bucket_sizes[rev_edge.data_[0] >> (kCharsPerEdgeWord - phase1::kBucketPrefixLength) * kBitsPerEdgeChar]++;
} else {
bucket_sizes[edge.data_[0] >> (kCharsPerEdgeWord - phase1::kBucketPrefixLength) * kBitsPerEdgeChar]++;
}
if (++last_char_offset >= read_length) {
break;
} else {
int c = ExtractNthChar(read_p, last_char_offset);
edge.ShiftLeftAppend(c);
rev_edge.ShiftRightAppend(3 - c);
}
}
}
return NULL;
}
// multithread
void PreprocessScanToFillBucketSizes(struct global_data_t &globals) {
// create threads
for (int t = 1; t < globals.num_cpu_threads; ++t) {
pthread_create(&(globals.readpartitions[t].thread), NULL, PreprocessScanToFillBucketSizesThread, &globals.readpartitions[t]);
}
PreprocessScanToFillBucketSizesThread(&globals.readpartitions[0]);
for (int t = 1; t < globals.num_cpu_threads; ++t) {
pthread_join(globals.readpartitions[t].thread, NULL);
}
// sum up readpartitions bucketsizes to form global bucketsizes
int64_t *bucket_sizes = globals.bucket_sizes;
memset(bucket_sizes, 0, phase1::kNumBuckets * sizeof(int64_t));
// the array accesses in this loop are optimized by the compiler??
for (int t = 0; t < globals.num_cpu_threads; ++t) {
for (int b = 0; b < phase1::kNumBuckets; ++b) {
bucket_sizes[b] += globals.readpartitions[t].rp_bucket_sizes[b];
}
}
}
void InitGlobalData(struct global_data_t &globals) {
// --- initialize writer ---
for (int t = 0; t < globals.phase1_num_output_threads; ++t) {
char edges_file_name[10240];
sprintf(edges_file_name, "%s.edges.%d", globals.output_prefix, t);
globals.word_writer[t].init(edges_file_name);
}
// --- compute offset bits ---
{
globals.offset_num_bits = 0;
int len = 1;
while (len - 1 < globals.max_read_length) {
globals.offset_num_bits++;
len *= 2;
}
}
// --- initialize stat ---
globals.edge_counting = (int64_t *) MallocAndCheck((kMaxMulti_t + 1) * sizeof(int64_t), __FILE__, __LINE__);
globals.thread_edge_counting = (int64_t *) MallocAndCheck((kMaxMulti_t + 1) * globals.phase1_num_output_threads * sizeof(int64_t), __FILE__, __LINE__);
memset(globals.edge_counting, 0, (kMaxMulti_t + 1) * sizeof(int64_t));
// --- Fill bucket size ---
xtimer_t timer;
timer.reset();
timer.start();
log("[B::%s] Filling read partition buckets...\n", __func__);
pthread_mutex_init(&globals.lv1_items_scanning_lock, NULL); // init lock
PrepareBucketScan(globals);
PreprocessScanToFillBucketSizes(globals); // Multithread: fill the read partition buckets, then sum up into the global buckets
globals.max_bucket_size = *std::max_element(globals.bucket_sizes, globals.bucket_sizes + phase1::kNumBuckets);
globals.tot_bucket_size = 0;
for (int i = 0; i < phase1::kNumBuckets; ++i) { globals.tot_bucket_size += globals.bucket_sizes[i]; }
timer.stop();
log("[B::%s] Done. Time elapsed: %.4lfs\n", __func__, timer.elapsed());
// --- calculate lv2 memory ---
#ifdef DISABLE_GPU
globals.max_lv2_items = std::max(globals.max_bucket_size, kMinLv2BatchSize);
#else
int64_t lv2_mem = globals.gpu_mem - 1073741824; // should reserver ~1G for GPU sorting
globals.max_lv2_items = lv2_mem / GPU_BYTES_PER_ITEM;
globals.max_lv2_items = std::min(globals.max_lv2_items, globals.tot_bucket_size);
if (globals.max_bucket_size > globals.max_lv2_items) {
err("[ERROR B::%s] Bucket too large for GPU: contains %lld items. Please try CPU version.\n", __func__, globals.max_bucket_size);
// TODO: auto switch to CPU version
exit(1);
}
#endif
globals.words_per_substring = DivCeiling((globals.kmer_k + 1) * kBitsPerEdgeChar, kBitsPerEdgeWord);
globals.words_per_edge = DivCeiling((globals.kmer_k + 1) * kBitsPerEdgeChar + kBitsPerMulti_t, kBitsPerEdgeWord);
// lv2 bytes: substring, permutation, readinfo
int64_t lv2_bytes_per_item = (globals.words_per_substring) * sizeof(edge_word_t) + sizeof(uint32_t) + sizeof(int64_t);
lv2_bytes_per_item = lv2_bytes_per_item * 2; // double buffering
#ifdef DISABLE_GPU
lv2_bytes_per_item += sizeof(uint64_t) * 2; // CPU memory is used to simulate GPU
#endif
log("[B::%s] %d words per read, %d words per substring, %d words per edge\n", __func__, globals.words_per_read, globals.words_per_substring, globals.words_per_edge);
// --- memory stuff ---
int64_t mem_remained = globals.host_mem
- globals.mem_packed_reads
- globals.num_reads * sizeof(unsigned char) * 2 // first_in0 & last_out0
- phase1::kNumBuckets * sizeof(int64_t) * (globals.num_cpu_threads * 3 + 1)
- (kMaxMulti_t + 1) * (globals.phase1_num_output_threads + 1) * sizeof(int64_t);
if (globals.mem_flag == 1) {
// auto set memory
globals.max_lv1_items = std::max(globals.max_lv2_items, int64_t(globals.tot_bucket_size / (kDefaultLv1ScanTime - 0.5)));
int64_t mem_needed = globals.max_lv1_items * LV1_BYTES_PER_ITEM + globals.max_lv2_items * lv2_bytes_per_item;
if (mem_needed > mem_remained) {
GetNumItems(globals, mem_remained, lv2_bytes_per_item);
}
} else if (globals.mem_flag == 0) {
// min memory
globals.max_lv1_items = std::max(globals.max_lv2_items, int64_t(globals.tot_bucket_size / (kMaxLv1ScanTime - 0.5)));
int64_t mem_needed = globals.max_lv1_items * LV1_BYTES_PER_ITEM + globals.max_lv2_items * lv2_bytes_per_item;
if (mem_needed > mem_remained) {
GetNumItems(globals, mem_remained, lv2_bytes_per_item);
} else {
GetNumItems(globals, mem_needed, lv2_bytes_per_item);
}
} else {
// use all
GetNumItems(globals, mem_remained, lv2_bytes_per_item);
}
log("[B::%s] Memory for reads: %lld\n", __func__, globals.mem_packed_reads);
log("[B::%s] max # lv.1 items = %lld\n", __func__, globals.max_lv1_items);
log("[B::%s] max # lv.2 items = %lld\n", __func__, globals.max_lv2_items);
// --- alloc memory ---
globals.lv1_items = (int*) MallocAndCheck(globals.max_lv1_items * sizeof(int), __FILE__, __LINE__);
globals.lv2_substrings = (edge_word_t*) MallocAndCheck(globals.max_lv2_items * globals.words_per_substring * sizeof(edge_word_t), __FILE__, __LINE__);
globals.permutation = (uint32_t *) MallocAndCheck(globals.max_lv2_items * sizeof(uint32_t), __FILE__, __LINE__);
globals.lv2_substrings_to_output = (edge_word_t*) MallocAndCheck(globals.max_lv2_items * globals.words_per_substring * sizeof(edge_word_t), __FILE__, __LINE__);
globals.permutation_to_output = (uint32_t *) MallocAndCheck(globals.max_lv2_items * sizeof(uint32_t), __FILE__, __LINE__);
globals.lv2_read_info = (int64_t *) MallocAndCheck(globals.max_lv2_items * sizeof(int64_t), __FILE__, __LINE__);
globals.lv2_read_info_to_output = (int64_t *) MallocAndCheck(globals.max_lv2_items * sizeof(int64_t), __FILE__, __LINE__);
#ifdef LONG_READS
globals.first_0_out = (uint16_t*) MallocAndCheck(globals.num_reads * sizeof(uint16_t), __FILE__, __LINE__);
globals.last_0_in = (uint16_t*) MallocAndCheck(globals.num_reads * sizeof(uint16_t), __FILE__, __LINE__);
#else
globals.first_0_out = (unsigned char*) MallocAndCheck(globals.num_reads * sizeof(unsigned char), __FILE__, __LINE__);
globals.last_0_in = (unsigned char*) MallocAndCheck(globals.num_reads * sizeof(unsigned char), __FILE__, __LINE__);
#endif
#ifdef DISABLE_GPU
globals.cpu_sort_space = (uint64_t*) MallocAndCheck(sizeof(uint64_t) * globals.max_lv2_items, __FILE__, __LINE__);
#endif
memset(globals.first_0_out, 0xFF, globals.num_reads * sizeof(globals.first_0_out[0]));
memset(globals.last_0_in, 0xFF, globals.num_reads * sizeof(globals.last_0_in[0]));
// --- write the edge file header ---
globals.word_writer[0].output(globals.kmer_k);
globals.word_writer[0].output(globals.words_per_edge);
}
/**
* @brief worker thread for Lv1ScanToFillOffests
*/
void* Lv1ScanToFillOffsetsThread(void *_data) {
struct readpartition_data_t &rp = *((struct readpartition_data_t*) _data);
struct global_data_t &globals = *(rp.globals);
int64_t *prev_full_offsets = (int64_t *)MallocAndCheck(phase1::kNumBuckets * sizeof(int64_t), __FILE__, __LINE__); // temporary array for computing differentials
assert(prev_full_offsets != NULL);
for (int b = globals.lv1_start_bucket; b < globals.lv1_end_bucket; ++b)
prev_full_offsets[b] = rp.rp_lv1_differential_base;
// this loop is VERY similar to that in PreprocessScanToFillBucketSizesThread
edge_word_t *read_p = PACKED_READS(rp.rp_start_id, globals);
KmerUint32 edge, rev_edge; // (k+1)-mer and its rc
int key;
for (int64_t read_id = rp.rp_start_id; read_id < rp.rp_end_id; ++read_id, read_p += globals.words_per_read) {
int read_length = GetReadLength(read_p, globals.words_per_read, globals.read_length_mask);
if (read_length < globals.kmer_k + 1) { continue; }
edge.init(read_p, globals.kmer_k + 1);
rev_edge.clean();
for (int i = 0; i <= globals.kmer_k; ++i) {
rev_edge.Append(3 - ExtractNthChar(read_p, globals.kmer_k - i));
}
// ===== this is a macro to save some copy&paste ================
#define CHECK_AND_SAVE_OFFSET(offset, strand) \
do { \
assert(offset + globals.kmer_k < read_length); \
if (((key - globals.lv1_start_bucket) ^ (key - globals.lv1_end_bucket)) & kSignBitMask) { \
int64_t full_offset = EncodeOffset(read_id, offset, strand, globals.offset_num_bits); \
int64_t differential = full_offset - prev_full_offsets[key]; \
if (differential > kDifferentialLimit) { \
pthread_mutex_lock(&globals.lv1_items_scanning_lock); \
globals.lv1_items[ rp.rp_bucket_offsets[key]++ ] = -globals.lv1_items_special.size() - 1; \
globals.lv1_items_special.push_back(full_offset); \
pthread_mutex_unlock(&globals.lv1_items_scanning_lock); \
} else { \
assert((int) differential >= 0); \
globals.lv1_items[ rp.rp_bucket_offsets[key]++ ] = (int) differential; \
} \
prev_full_offsets[key] = full_offset; \
} \
} while (0)
// ^^^^^ why is the macro surrounded by a do-while? please ask Google
// =========== end macro ==========================
// shift the key char by char
int last_char_offset = globals.kmer_k;
while (true) {
if (rev_edge < edge) {
key = rev_edge.data_[0] >> (kCharsPerEdgeWord - phase1::kBucketPrefixLength) * kBitsPerEdgeChar;
CHECK_AND_SAVE_OFFSET(last_char_offset - globals.kmer_k, 1);
} else {
key = edge.data_[0] >> (kCharsPerEdgeWord - phase1::kBucketPrefixLength) * kBitsPerEdgeChar;
CHECK_AND_SAVE_OFFSET(last_char_offset - globals.kmer_k, 0);
}
if (++last_char_offset >= read_length) {
break;
} else {
int c = ExtractNthChar(read_p, last_char_offset);
edge.ShiftLeftAppend(c);
rev_edge.ShiftRightAppend(3 - c);
}
}
}
#undef CHECK_AND_SAVE_OFFSET
free(prev_full_offsets);
return NULL;
}
/**
* @brief file LV1 items into host mem
*/
void Lv1ScanToFillOffests(struct global_data_t &globals) {
globals.lv1_items_special.clear();
Lv1ComputeBucketOffset(globals);
// create threads
for (int t = 1; t < globals.num_cpu_threads; ++t) {
pthread_create(&(globals.readpartitions[t].thread), NULL, Lv1ScanToFillOffsetsThread, &globals.readpartitions[t]);
}
Lv1ScanToFillOffsetsThread(&globals.readpartitions[0]);
for (int t = 1; t < globals.num_cpu_threads; ++t) {
pthread_join(globals.readpartitions[t].thread, NULL);
}
// revert rp_bucket_offsets
Lv1ComputeBucketOffset(globals);
}
// single thread helper function
// 'spacing' is the strip length for read-word "coalescing"
void CopySubstring(edge_word_t* dest, edge_word_t* src_read, int offset, int num_chars_to_copy, global_data_t &globals) {
int64_t spacing = globals.lv2_num_items;
int words_per_read = globals.words_per_read;
int words_per_substring = globals.words_per_substring;
// copy words of the suffix to the suffix pool
int which_word = offset / kCharsPerEdgeWord;
int word_offset = offset % kCharsPerEdgeWord;
edge_word_t *src_p = src_read + which_word;
edge_word_t *dest_p = dest;
int num_words_copied = 0;
if (!word_offset) { // special case (word aligned), easy
while (which_word < words_per_read && num_words_copied < words_per_substring) {
*dest_p = *src_p; // write out
dest_p += spacing;
src_p++;
which_word++;
num_words_copied++;
}
} else { // not word-aligned
int bit_shift = offset * kBitsPerEdgeChar;
edge_word_t s = *src_p;
edge_word_t d = s << bit_shift;
which_word++;
while (which_word < words_per_read) {
s = *(++src_p);
d |= s >> (kBitsPerEdgeWord - bit_shift);
*dest_p = d; // write out
if (++num_words_copied >= words_per_substring) goto here;
dest_p += spacing;
d = s << bit_shift;
which_word++;
}
*dest_p = d; // write last word
here:
;
}
{
// now mask the extra bits (TODO can be optimized)
int num_bits_to_copy = num_chars_to_copy * 2;
int which_word = num_bits_to_copy / kBitsPerEdgeWord;
edge_word_t *p = dest + which_word * spacing;
int bits_to_clear = kBitsPerEdgeWord - num_bits_to_copy % kBitsPerEdgeWord;
if (bits_to_clear < kBitsPerEdgeWord) {
*p >>= bits_to_clear;
*p <<= bits_to_clear;
} else if (which_word < globals.words_per_substring) {
*p = 0;
}
which_word++;
while (which_word < globals.words_per_substring) { // fill zero
*(p+=spacing) = 0;
which_word++;
}
}
}
void CopySubstringRC(edge_word_t* dest, edge_word_t* src_read, int offset, int num_chars_to_copy, global_data_t &globals) {
assert(num_chars_to_copy == globals.kmer_k + 1);
int spacing = globals.lv2_num_items;
int which_word = (offset + num_chars_to_copy - 1) / kCharsPerEdgeWord;
int word_offset = (offset + num_chars_to_copy - 1) % kCharsPerEdgeWord;
edge_word_t *dest_p = dest;
if (word_offset == kCharsPerEdgeWord - 1) { // edge_word_t aligned
for (int i = 0; i < globals.words_per_substring && i <= which_word; ++i) {
*dest_p = ~ mirror(src_read[which_word - i]);
dest_p += spacing;
}
} else {
int bit_offset = (kCharsPerEdgeWord - 1 - word_offset) * kBitsPerEdgeChar;
int i;
edge_word_t w;
for (i = 0; i < globals.words_per_substring - 1 && i < which_word; ++i) {
w = (src_read[which_word - i] >> bit_offset) |
(src_read[which_word - i - 1] << (kBitsPerEdgeWord - bit_offset));
*dest_p = ~ mirror(w);
dest_p += spacing;
}
// last word
w = src_read[which_word - i] >> bit_offset;
if (which_word >= i + 1) {
w |= (src_read[which_word - i - 1] << (kBitsPerEdgeWord - bit_offset));
}
*dest_p = ~ mirror(w);
}
{
// now mask the extra bits (TODO can be optimized)
int num_bits_to_copy = num_chars_to_copy * 2;
int which_word = num_bits_to_copy / kBitsPerEdgeWord;
edge_word_t *p = dest + which_word * spacing;
int bits_to_clear = kBitsPerEdgeWord - num_bits_to_copy % kBitsPerEdgeWord;
if (bits_to_clear < kBitsPerEdgeWord) {
*p >>= bits_to_clear;
*p <<= bits_to_clear;
} else if (which_word < globals.words_per_substring) {
*p = 0;
}
which_word++;
while (which_word < globals.words_per_substring) { // fill zero
*(p+=spacing) = 0;
which_word++;
}
}
}
/**
* @brief worker thread for Lv2ExtractSubstrings
*/
void* Lv2ExtractSubstringsThread(void* _data) {
struct bucketpartition_data_t &bp = *((struct bucketpartition_data_t*) _data);
struct global_data_t &globals = *(bp.globals);
int *lv1_p = globals.lv1_items + globals.readpartitions[0].rp_bucket_offsets[ bp.bp_start_bucket ];
int64_t offset_mask = (1 << globals.offset_num_bits) - 1; // 0000....00011..11
edge_word_t *substrings_p = globals.lv2_substrings +
(globals.readpartitions[0].rp_bucket_offsets[ bp.bp_start_bucket ] - globals.readpartitions[0].rp_bucket_offsets[ globals.lv2_start_bucket ]);
int64_t *read_info_p = globals.lv2_read_info +
(globals.readpartitions[0].rp_bucket_offsets[ bp.bp_start_bucket ] - globals.readpartitions[0].rp_bucket_offsets[ globals.lv2_start_bucket ]);
for (int b = bp.bp_start_bucket; b < bp.bp_end_bucket; ++b) {
for (int t = 0; t < globals.num_cpu_threads; ++t) {
int64_t full_offset = globals.readpartitions[t].rp_lv1_differential_base;
int num = globals.readpartitions[t].rp_bucket_sizes[b];
for (int i = 0; i < num; ++i) {
if (*lv1_p >= 0) {
full_offset += *(lv1_p++);
} else {
full_offset = globals.lv1_items_special[-1 - *(lv1_p++)];
}
int64_t read_id = full_offset >> (globals.offset_num_bits + 1);
int strand = full_offset & 1;
int offset = (full_offset >> 1) & offset_mask;
int num_chars_to_copy = globals.kmer_k + 1;
unsigned char prev, next;
if (offset > 0) {
prev = ExtractNthChar(PACKED_READS(read_id, globals), offset - 1);
} else {
prev = kSentinelValue;
}
if (offset + globals.kmer_k + 1 < GetReadLengthByID(read_id, globals)) {
next = ExtractNthChar(PACKED_READS(read_id, globals), offset + globals.kmer_k + 1);
} else {
next = kSentinelValue;
}
if (strand == 0) {
CopySubstring(substrings_p, PACKED_READS(read_id, globals), offset, num_chars_to_copy, globals);
*read_info_p = (full_offset << 6) | (prev << 3) | next;
} else {
CopySubstringRC(substrings_p, PACKED_READS(read_id, globals), offset, num_chars_to_copy, globals);
*read_info_p = (full_offset << 6) | ((next == kSentinelValue ? kSentinelValue : (3 - next)) << 3)
| (prev == kSentinelValue ? kSentinelValue : (3 - prev));
}
#ifdef DBJ_DEBUG
if ((*substrings_p >> (32 - phase2::kBucketPrefixLength * 2)) != b) {
debug("WRONG substring wrong:%d right:%d read_id:%lld offset:%d strand: %d num_chars_to_copy:%d\n", *substrings_p >> (32 - phase2::kBucketPrefixLength * 2), b, read_id, offset, strand, num_chars_to_copy);
}
#endif
substrings_p++;
read_info_p++;
}
}
}
return NULL;
}
/**
* @brief extract true substring used for sorting
*/
void Lv2ExtractSubstrings(struct global_data_t &globals) {
Lv2DistributeBucketPartitions(globals, globals.phase1_num_output_threads);
// create threads
for (int t = 0; t < globals.num_cpu_threads-globals.phase1_num_output_threads; ++t) {
pthread_create(&(globals.bucketpartitions[t].thread), NULL, Lv2ExtractSubstringsThread, &globals.bucketpartitions[t]);
}
for (int t = 0; t < globals.num_cpu_threads-globals.phase1_num_output_threads; ++t) {
pthread_join(globals.bucketpartitions[t].thread, NULL);
}
#ifdef DBJ_DEBUG
for (int i = 0; i < globals.lv2_num_items; ++i) {
edge_word_t *substrings_p = globals.lv2_substrings + i;
int cur_bucket = *substrings_p >> (32 - phase1::kBucketPrefixLength * 2);
assert(cur_bucket < globals.lv2_end_bucket && cur_bucket >= globals.lv2_start_bucket);
}
#endif
}
// helper function for counting
inline bool IsDifferentEdges(edge_word_t *item1, edge_word_t* item2, int num_words, int spacing) {
for (int i = num_words - 1; i >= 0; --i) {
if (*(item1 + (int64_t)i * spacing) != *(item2 + (int64_t)i * spacing)) {
return true;
}
}
return false;
}
/**
* @brief pack an edge and its multiplicity to word-aligned spaces
*/
inline void PackEdge(edge_word_t *dest, edge_word_t *item, int counting, struct global_data_t &globals) {
for (int i = 0; i < globals.words_per_edge && i < globals.words_per_substring; ++i) {
dest[i] = *(item + (int64_t)i * globals.lv2_num_items_to_output);
}
int chars_in_last_word = (globals.kmer_k + 1) % kCharsPerEdgeWord;
int which_word = (globals.kmer_k + 1) / kCharsPerEdgeWord;
if (chars_in_last_word > 0) {
dest[which_word] >>= (kCharsPerEdgeWord - chars_in_last_word) * 2;
dest[which_word] <<= (kCharsPerEdgeWord - chars_in_last_word) * 2;
} else {
dest[which_word] = 0;
}
while (++which_word < globals.words_per_edge) {
dest[which_word] = 0;
}
dest[globals.words_per_edge - 1] |= std::min(kMaxMulti_t, counting);
}
void* Lv2CountingThread(void *_op) {
struct outputpartition_data_t *op = (struct outputpartition_data_t*) _op;
struct global_data_t &globals = *(op->globals);
int64_t op_start_index = op->op_start_index;
int64_t op_end_index = op->op_end_index;
int thread_id = op->op_id;
xtimer_t local_timer;
local_timer.start();
local_timer.reset();
int start_idx;
int end_idx;
edge_word_t packed_edge[32];
int count_prev[5], count_next[5];
int64_t offset_mask = (1 << globals.offset_num_bits) - 1;
int64_t *thread_edge_counting = globals.thread_edge_counting + thread_id * (kMaxMulti_t + 1);
for (int i = op_start_index; i < op_end_index; i = end_idx) {
start_idx = i;
end_idx = i + 1;
edge_word_t *first_item = globals.lv2_substrings_to_output + (globals.permutation_to_output[i]);
while (end_idx < op_end_index) {
if (IsDifferentEdges(first_item,
globals.lv2_substrings_to_output + globals.permutation_to_output[end_idx],
globals.words_per_substring, globals.lv2_num_items_to_output)) {
break;
}
++end_idx;
}
int count = end_idx - start_idx;
// update read's first and last
memset(count_prev, 0, sizeof(int) * 4);
memset(count_next, 0, sizeof(int) * 4);
bool has_in = false;
bool has_out = false;
for (int j = start_idx; j < end_idx; ++j) {
int prev_and_next = globals.lv2_read_info_to_output[globals.permutation_to_output[j]] & ((1 << 6) - 1);
count_prev[prev_and_next >> 3]++;
count_next[prev_and_next & 7]++;
}
for (int j = 0; j < 4; ++j) {
if (count_prev[j] >= globals.kmer_freq_threshold) { has_in = true; }
if (count_next[j] >= globals.kmer_freq_threshold) { has_out = true; }
}
if (!has_in && count >= globals.kmer_freq_threshold) {
for (int j = start_idx; j < end_idx; ++j) {
int64_t read_info = globals.lv2_read_info_to_output[globals.permutation_to_output[j]] >> 6;
int strand = read_info & 1;
int offset = (read_info >> 1) & offset_mask;
int64_t read_id = read_info >> (1 + globals.offset_num_bits);
if (strand == 0) {
// update last
while (true) {
auto old_value = globals.last_0_in[read_id];
if (old_value != kSentinelOffset && old_value >= offset) { break; }
if (__sync_bool_compare_and_swap(globals.last_0_in + read_id, old_value, offset)) {
break;
}
}
} else {
// update first
offset++;
while (true) {
auto old_value = globals.first_0_out[read_id];
if (old_value <= offset) { break; }
if (__sync_bool_compare_and_swap(globals.first_0_out + read_id, old_value, offset)) {
break;
}
}
}
}
}
if (!has_out && count >= globals.kmer_freq_threshold) {
for (int j = start_idx; j < end_idx; ++j) {
int64_t read_info = globals.lv2_read_info_to_output[globals.permutation_to_output[j]] >> 6;
int strand = read_info & 1;
int offset = (read_info >> 1) & offset_mask;
int64_t read_id = read_info >> (1 + globals.offset_num_bits);
if (strand == 0) {
// update first
offset++;
while (true) {
auto old_value = globals.first_0_out[read_id];
if (old_value <= offset) { break; }
if (__sync_bool_compare_and_swap(globals.first_0_out + read_id, old_value, offset)) {
break;
}
}
} else {
// update last
while (true) {
auto old_value = globals.last_0_in[read_id];
if (old_value != kSentinelOffset && old_value >= offset) { break; }
if (__sync_bool_compare_and_swap(globals.last_0_in + read_id, old_value, offset)) {
break;
}
}
}
}
}
++thread_edge_counting[std::min(count, kMaxMulti_t)];
if (count >= globals.kmer_freq_threshold) {
PackEdge(packed_edge, first_item, count, globals);
for (int x = 0; x < globals.words_per_edge; ++x) {
globals.word_writer[thread_id].output(packed_edge[x]);
}
}
}
local_timer.stop();
log("[B::%s] Counting time elapsed: %.4lfs\n", __func__, local_timer.elapsed());
return NULL;
}
/**
* @brief count and output solid (k+1)-mer
*/
void Lv2Counting(struct global_data_t &globals) {
int64_t last_end_index = 0;
int64_t items_per_thread = globals.lv2_num_items_to_output / globals.phase1_num_output_threads;
for (int thread_id = 0; thread_id < globals.phase1_num_output_threads - 1; ++thread_id) {
int64_t this_start_index = last_end_index;
int64_t this_end_index = this_start_index + items_per_thread;
if (this_end_index > globals.lv2_num_items_to_output) { this_end_index = globals.lv2_num_items_to_output; }
if (this_end_index > 0) {
while (this_end_index < globals.lv2_num_items_to_output) {
edge_word_t *prev_item = globals.lv2_substrings_to_output + (globals.permutation_to_output[this_end_index - 1]);
edge_word_t *item = globals.lv2_substrings_to_output + (globals.permutation_to_output[this_end_index]);
if (IsDifferentEdges(prev_item, item, globals.words_per_substring, globals.lv2_num_items_to_output)) {
break;
}
++this_end_index;
}
}
globals.outputpartitions[thread_id].op_start_index = this_start_index;
globals.outputpartitions[thread_id].op_end_index = this_end_index;
last_end_index = this_end_index;
}
// last partition
globals.outputpartitions[globals.phase1_num_output_threads - 1].op_start_index = last_end_index;
globals.outputpartitions[globals.phase1_num_output_threads - 1].op_end_index = globals.lv2_num_items_to_output;
memset(globals.thread_edge_counting, 0, sizeof(int64_t) * (kMaxMulti_t + 1) * globals.phase1_num_output_threads);
for (int thread_id = 0; thread_id < globals.phase1_num_output_threads; ++thread_id) {
globals.outputpartitions[thread_id].op_id = thread_id;
globals.outputpartitions[thread_id].globals = &globals;
pthread_create(&globals.output_threads[thread_id], NULL, Lv2CountingThread, &globals.outputpartitions[thread_id]);
}
}
void Lv2CountingJoin(struct global_data_t &globals) {
for (int thread_id = 0; thread_id < globals.phase1_num_output_threads; ++thread_id) {
pthread_join(globals.output_threads[thread_id], NULL);
for (int i = 1; i <= kMaxMulti_t; ++i) {
globals.edge_counting[i] += globals.thread_edge_counting[thread_id * (kMaxMulti_t + 1) + i];
}
}
}
void Phase1Clean(struct global_data_t &globals) {
pthread_mutex_destroy(&globals.lv1_items_scanning_lock);
free(globals.packed_reads);
free(globals.bucket_sizes);
free(globals.lv1_items);
free(globals.lv2_substrings);
free(globals.permutation);
free(globals.lv2_substrings_to_output);
free(globals.permutation_to_output);
free(globals.lv2_read_info);
free(globals.lv2_read_info_to_output);
free(globals.first_0_out);
free(globals.last_0_in);
free(globals.edge_counting);
free(globals.thread_edge_counting);