forked from the-data-lab/gstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgstore.cpp
3476 lines (2999 loc) · 102 KB
/
gstore.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
/*
* Copyright 2016 The George Washington University
* Written by Pradeep Kumar
* Directed by Prof. Howie Huang
*
* https://www.seas.gwu.edu/~howie/
* Contact: [email protected]
*
*
* Please cite the following paper:
*
* Pradeep Kumar and H. Howie Huang. 2016. G-Store: High-Performance Graph Store for Trillion-Edge Processing. In Proceedings of the International Conference for High Performance Computing, Networking, Storage and Analysis (SC '16).
*
* This file is part of G-Store.
*
* G-Store 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.
*
* G-Store 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 G-Store. If not, see <http://www.gnu.org/licenses/>.
*/
#include <omp.h>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <assert.h>
#include <algorithm>
#include <errno.h>
#include <cmath>
#include <fstream>
#include <libaio.h>
#include <math.h>
#include <asm/mman.h>
#include "wtime.h"
#include "gstore.h"
#include "pr.h"
#include "bfs.h"
#include "bfs2.h"
#include "kcore.h"
#include "wcc.h"
#include "traverse.h"
#define AIO_MAXIO 16384
#define AIO_BATCHIO 256
//These are IO threads
#define IO_THDS 1
index_t p = 0;
index_t p_s = 0;
size_t mp_count = 1;
int f_edge = 0;
size_t sz_copied = 0;
grid* g;
cache_driver* cache;
io_driver* io;
index_t* s_count_edge_in;
int arg = -1;
size_t total_size = (8192L << 20); // 2048 MB
char* main_buf = 0;
char* init_buf = 0;
size_t cache_size = 0;
//size_t memory = (1024L << 20);// 512 MB;
size_t memory = (256L << 20);// 512 MB;
size_t read_sz = (2L << 20);
uint8_t compressed = 0;
////////////////////////
void conv_to_text(string edgefile);
void text_to_bin(string textfile);
void remove_dup(string edgefile);
void end_ctx(part_meta_t* part_meta, int k, part_t b_i, part_t b_j, spart_t i, spart_t j);
bool end_cache_ctx(segment* seg, segment* tmp, size_t& start_addr,
matrix<spart_t, index_t>* start_edge, char* buf, char* edges);
void shallow_copy(segment* cached_pool1, segment* seg);
inline bool
is_end(last_read_t& last_read) {
return (last_read.b_i == p -1 && last_read.b_j == p - 1
&& last_read.s_i == p_p - 1 && last_read.s_j == p_p - 1);
}
inline bool
is_start(last_read_t last_read)
{
return (last_read.b_i == 0 &&
last_read.b_j == 0 &&
last_read.s_i == 0 &&
last_read.s_j);
}
grid::grid()
{
_edges = 0;
vert_degree = 0;
bvert_degree = 0;
bdegree_count = 0;
svert_degree = 0;
}
grid::~grid()
{
free(vert_degree);
vert_degree = 0;
free(bvert_degree);
bvert_degree = 0;
free(svert_degree);
svert_degree = 0;
}
void grid::init(int argc, char * argv[])
{
int o;
int job = 0;
uint32_t scale;
int c = 0;
string edgefile;
string part_file;
while ((o = getopt (argc, argv, "s:o:hi:j:c:m:v:a:")) != -1) {
switch(o) {
case 's': //scale
scale = atoi(optarg);
vert_count = (1L << scale);
scale -= bit_shift1;
p = (1 << scale);
p_s = (vert_count >> bit_shift2) + (0 != (vert_count & part_mask2_2));
read_part = new bitmap_t(p_s);
read_part_next = new bitmap_t(p_s);
read_part_next->reset();
break;
case 'v'://vert count
//vert_count = atoi(optarg);
sscanf(optarg, "%ld", &vert_count);
p = (vert_count >> bit_shift1) + (0 != (vert_count & part_mask1_2));
//p_s = (vert_count >> bit_shift2) + (0 != (vert_count & part_mask2_2));
p_s = p_p*p;
read_part = new bitmap_t(p_s);
read_part_next = new bitmap_t(p_s);
read_part_next->reset();
break;
case 'i':
edgefile = optarg;
break;
case 'o':
part_file = optarg;
break;
case 'j':
job = atoi(optarg);
break;
case 'h':
cout << "Coming soon" << endl;
return;
case 'c':
c = atoi(optarg);
break;
case 'm':
total_size = ((size_t)atoi(optarg) << 20);
break;
case 'a':
arg = atoi(optarg);
break;
default:
cout << "Unknown argument" << endl ;
}
}
cout << "Partition Count = " << p << endl;
cout << "Partition Count smaller = " << p_s << endl;
double start, end;
switch(c) {
case 1:
start = mywtime();
proc_grid(edgefile, part_file);
end = mywtime();
save_grid(part_file);
cout << "GStore Time = " << end - start << endl;
return ;
case 2:
proc_grid_big(edgefile, part_file);
save_grid_big(part_file);
return;
case 3:
text_to_bin(edgefile);
return;
case 4:
start = mywtime();
proc_csr(edgefile, part_file);
end = mywtime();
cout << "CSR Time = " << end - start << endl;
return;
case 5:
start = mywtime();
proc_grid(edgefile, part_file);
end = mywtime();
cout << "GStore Time = " << end - start << endl;
//break;
return ;
case 6:
remove_dup(edgefile);
return;
case 7:
conv_to_text(edgefile);
return;
}
switch(job) {
case 0:
start = mywtime();
read_aio_init(edgefile);
bfs();
end = mywtime();
cout << "Half BFS time = " << end-start << endl;
break;
case 10:
start = mywtime();
read_grid(edgefile);
bfs_mmap();
end = mywtime();
cout << "Half BFS time = " << end-start << endl;
break;
case 20:
read_grid_in_mem(edgefile);
start = mywtime();
bfs_mmap();
end = mywtime();
cout << "Half BFS time = " << end-start << endl;
break;
case 30:
start = mywtime();
read_aio_init(edgefile);
bfs2();
end = mywtime();
cout << "Half BFS time = " << end-start << endl;
break;
case 1:
start = mywtime();
read_aio_init(edgefile);
read_degree_in_mem(edgefile);
pagerank();
end = mywtime();
cout << "Page-rank time = " << end-start << endl;
break;
case 11:
start = mywtime();
read_grid(edgefile);
read_degree_in_mem(edgefile);
pagerank_mmap();
end = mywtime();
cout << "Page-rank_mmap time = " << end-start << endl;
break;
case 21:
read_grid_in_mem(edgefile);
read_degree_in_mem(edgefile);
start = mywtime();
pagerank_mmap();
end = mywtime();
cout << "Page-rank_mmap time = " << end-start << endl;
break;
case 2:
start = mywtime();
read_aio_init(edgefile);
read_degree_in_mem(edgefile);
kcore(arg);
end = mywtime();
cout << "Kcore time = " << end-start << endl;
break;
case 12:
start = mywtime();
read_grid(edgefile);
read_degree_in_mem(edgefile);
kcore_mmap(arg);
end = mywtime();
cout << "Kcore time = " << end-start << endl;
break;
case 22:
read_grid_in_mem(edgefile);
read_degree_in_mem(edgefile);
start = mywtime();
kcore_mmap(arg);
end = mywtime();
cout << "Kcore time = " << end-start << endl;
break;
case 3:
start = mywtime();
read_aio_init(edgefile);
wcc();
end = mywtime();
cout << "WCC time = " << end-start << endl;
break;
case 13:
start = mywtime();
read_grid(edgefile);
wcc_mmap();
end = mywtime();
cout << "WCC time = " << end-start << endl;
break;
case 23:
read_grid_in_mem(edgefile);
start = mywtime();
wcc_mmap();
end = mywtime();
cout << "WCC time = " << end-start << endl;
break;
case 4:
start = mywtime();
read_aio_init(edgefile);
read_degree_in_mem(edgefile);
traverse();
end = mywtime();
cout << "Single color propagation time = " << end-start << endl;
break;
case 100:
analyze_grid_size(edgefile);
return;
default:
cout << "Wrong value for -j argument" << endl;
}
}
void grid::pre_grid_big(string edgefile)
{
//Number of files in one row.
index_t p_v = (vert_count >> bit_shift0);
cout << "p_v = " << p_v << endl;
//Total number of files.
index_t vcount = calc_total_part(p_v);
string* tfile = new string [vcount];
FILE** tf = (FILE**) calloc(sizeof(FILE*), vcount);
cedge_t** buf = (cedge_t**)calloc(sizeof(cedge_t), vcount);
int64_t count = (1L<<30);
gedge_t* edges = (gedge_t*)malloc(sizeof(gedge_t)*count);
gedge_t* edges2 = (gedge_t*)malloc(sizeof(gedge_t)*count);
for( index_t ifile = 0; ifile < vcount; ++ifile) {
char tmp[64] = {0};
sprintf(tmp, ".%ld", ifile);
tfile[ifile] = edgefile + tmp;
tf[ifile] = fopen(tfile[ifile].c_str(), "wb+");
assert(tf[ifile] != NULL);
buf[ifile] = (cedge_t*)malloc(sizeof(cedge_t)*count);
cout << tfile[ifile] << endl;
}
//files
#ifdef HALF_GRID
matrix<spart_t, index_t> count_edge;
count_edge.init(p_v);
#else
matrix_f<spart_t, index_t> count_edge;
count_edge.init(p_v);
#endif
//Smaller partitions
index_t total_s_part = calc_total_part(p_s);
_s_start_edge = (index_t*) calloc(sizeof(index_t), total_s_part);
vert_degree = (degree_t*)calloc(sizeof(degree_t), vert_count);
//Read the file and convert it to first group.
struct stat st_edge;
FILE* fid_edge = fopen(edgefile.c_str(), "rb");
stat(edgefile.c_str(), &st_edge);
assert(st_edge.st_size != 0);
//index_t nedges = st_edge.st_size/sizeof(gedge_t);
size_t to_read = st_edge.st_size/sizeof(gedge_t);
size_t sz_read = 0;
index_t ecount = 0;
sz_read = fread(edges2, sizeof(gedge_t), count, fid_edge);
assert(sz_read != 0);
while(to_read != 0) {
to_read -= sz_read;
ecount = sz_read;
swap(edges, edges2);
#pragma omp parallel num_threads(NUM_THDS) shared(edges,edges2)
{
if (0 == omp_get_thread_num() && (to_read != 0)) {
sz_read = fread(edges2, sizeof(gedge_t), count, fid_edge);
if (sz_read == 0) {
int err = ferror(fid_edge);
cout << err << endl;
exit(-1);
}
assert(sz_read != 0);
}
#ifdef HALF_GRID
matrix<spart_t, index_t> start_edge_half;
start_edge_half.part_count = p_p;
#endif
matrix_f<spart_t, index_t> start_edge_full;
start_edge_full.part_count = p_p;
matrix<spart_t, index_t>* s_start_edge;
gedge_t edge;
part_t j, i;
part_t b_i, b_j;
spart_t s_i, s_j;
vertex_t v0, v1, v2, v3;
index_t offset;
#pragma omp for schedule (dynamic, 4096)
for (index_t k = 0; k < ecount; ++k) {
edge = edges[k];
if (edge.is_self_loop()) continue;
v2 = edge.get_v0();
v3 = edge.get_v1();
#ifdef HALF_GRID
v0 = min(v2, v3);
v1 = max(v3, v2);
#else
v0 = v2;
v1 = v3;
#endif
i = (v0 >> bit_shift0);
j = (v1 >> bit_shift0);
index_t n = count_edge.get_index(i,j);
index_t m = count_edge.atomic_incr(n);
buf[n][m].v0 = (v0 & part_mask0_2);
buf[n][m].v1 = (v1 & part_mask0_2);
b_i = (v0 >> bit_shift1);
b_j = (v1 >> bit_shift1);
s_i = ((v0 >> bit_shift2) & part_mask3_2);
s_j = ((v1 >> bit_shift2) & part_mask3_2);
#ifdef HALF_GRID
if (b_i == b_j) {
s_start_edge = &start_edge_half;
offset = beg_edge_offset1(b_i);
} else {
s_start_edge = &start_edge_full;
offset = beg_edge_offset2(b_i, b_j);
}
#else
s_start_edge = &start_edge_full;
offset = beg_edge_offset2(b_i, b_j);
#endif
s_start_edge->val = _s_start_edge + offset;
s_start_edge->atomic_incr(s_i, s_j);
__sync_fetch_and_add(vert_degree + edge.get_v0(), 1);
#ifdef HALF_GRID
__sync_fetch_and_add(vert_degree + edge.get_v1(), 1);
#endif
}
}
for (index_t ifile = 0; ifile < vcount; ++ifile) {
fwrite(buf[ifile], sizeof(cedge_t), count_edge.val[ifile], tf[ifile]);
count_edge.val[ifile] = 0;
}
}
//Calculate the edge start
index_t prefix_sum = 0;
index_t curr_value = 0;
for (index_t ipart = 0; ipart < total_s_part; ++ipart) {
curr_value = _s_start_edge[ipart];
_s_start_edge[ipart] = prefix_sum;
prefix_sum += curr_value;
}
_s_start_edge[total_s_part] = prefix_sum;
cout << "Total edges = " << prefix_sum << endl;
for (index_t ifile = 0; ifile < vcount; ++ifile) {
fclose(tf[ifile]);
}
fclose(fid_edge);
}
void grid::proc_grid_big(string edgefile, string part_file)
{
pre_grid_big(edgefile);
//Number of files in one row
index_t p_v = (vert_count >> bit_shift0);
//Total number of files.
index_t vcount = calc_total_part(p_v);
//The final edge file.
string file = part_file + ".grid";
/*
index_t total_s_part = calc_total_part(p_s);
int fd = open(file.c_str(), O_RDWR|O_CREAT, S_IRWXU);
ftruncate(fd, _s_start_edge[total_s_part]*sizeof(edge_t));
cout << "size of grid file = " << _s_start_edge[total_s_part]*sizeof(edge_t) << endl;
close(fd);
*/
FILE* f = fopen(file.c_str(), "wb+");
assert(f != 0);
//Number of big partitions in a file row and buffer for each one
index_t num_part = p/p_v;
cout << "num_part = " << num_part <<endl;
index_t total_parts = num_part*num_part;
edge_t** buf = (edge_t**)malloc(total_parts*sizeof(edge_t*));
//Counting number of edges in each smaller partitions.
matrix_f<part_t, index_t>* count_edge = new matrix_f<part_t, index_t>[total_parts];
//final edge count in each partition.
index_t* ecount = (index_t*)calloc(sizeof(index_t), total_parts);
//Read file[0]
char tmp[64] = {0};
string tfile = edgefile + string(".0");
FILE* tf = fopen(tfile.c_str(), "rb");
assert(tf != NULL);
struct stat st;
stat(tfile.c_str(), &st);
//Number of edges in this file.
index_t count2 = st.st_size/sizeof(cedge_t);
cedge_t* edges2 = (cedge_t*)malloc(st.st_size);
cout << "Reading " << tfile << endl;
fread(edges2, sizeof(cedge_t), count2, tf);
fclose(tf);
cedge_t* edges = 0;
index_t count = 0;
#pragma omp parallel num_threads(NUM_THDS) shared(edges2, edges, count2, count)
{
cedge_t edge;
vertex_t v0, v1;
part_t b_i, b_j;
spart_t s_i, s_j;
index_t offset, n , m;
#ifdef HALF_GRID
matrix<spart_t, index_t> start_edge_half;
start_edge_half.part_count = p_p;
#endif
matrix_f<spart_t, index_t> start_edge_full;
start_edge_full.part_count = p_p;
matrix<spart_t, index_t>* start_edge;
#pragma omp for
for (index_t ipart = 0; ipart < total_parts; ++ipart) {
count_edge[ipart].init(p_p);
buf[ipart] = (edge_t*)malloc(sizeof(edge_t)*(4L<<30));
}
for (index_t a = 0; a < p_v; ++a) {
index_t base_b_i = (a*num_part);
index_t buf_index = 0;
#ifdef HALF_GRID
for (index_t b = a; b < p_v; ++b)
#else
for (index_t b = 0; b < p_v; ++b)
#endif
{
#pragma omp barrier
if (0 == omp_get_thread_num()) {
swap(edges2, edges);
swap(count2, count);
free(edges2);
edges2 = 0;
}
#pragma omp barrier
index_t base_b_j = (b*num_part);
index_t ifile = calc_index(a, b, p_v) + 1;
if (0 == omp_get_thread_num() && (ifile < vcount)) {
//Read next file. i.e. file[ifile]
sprintf(tmp, ".%ld", ifile);
tfile = edgefile + tmp;
tf = fopen(tfile.c_str(), "rb");
assert(tf != NULL);
cout << "Reading " << tfile << endl;
stat(tfile.c_str(), &st);
count2 = st.st_size/sizeof(cedge_t);
edges2 = (cedge_t*)malloc(st.st_size);
fread(edges2, sizeof(cedge_t), count2, tf);
fclose(tf);
}
#pragma omp for schedule(dynamic, 4096)
for (index_t k = 0; k < count; ++k) {
edge = edges[k];
v0 = edge.v0 + (a << bit_shift0);
v1 = edge.v1 + (b << bit_shift0);
b_i = (v0 >> bit_shift1);
b_j = (v1 >> bit_shift1);
s_i = ((v0 >> bit_shift2) & part_mask3_2);
s_j = ((v1 >> bit_shift2) & part_mask3_2);
#ifdef HALF_GRID
if (b_i == b_j) {
start_edge = &start_edge_half;
offset = beg_edge_offset1(b_i);
} else {
start_edge = &start_edge_full;
offset = beg_edge_offset2(b_i,b_j);
}
#else
start_edge = &start_edge_full;
offset = beg_edge_offset2(b_i,b_j);
#endif
start_edge->val = _s_start_edge + offset;
buf_index = calc_index_f(b_i - base_b_i, b_j - base_b_j, num_part);
n = start_edge->get(s_i, s_j) - _s_start_edge[offset];
m = count_edge[buf_index].atomic_incr(s_i, s_j);
//This assignment takes last 2 bytes automatically
buf[buf_index][n + m].v0 = v0;
buf[buf_index][n + m].v1 = v1;
//edge count in each bigger partition
__sync_fetch_and_add(ecount + buf_index, 1);
}
//Write the big partitions now.
if ( 0 == omp_get_thread_num() ) {
for (index_t c = base_b_i; c < base_b_i + num_part; ++c) {
#ifdef HALF_GRID
if (base_b_i == base_b_j) {
offset = beg_edge_offset1(c);
for (index_t d = c; d < base_b_j + num_part; ++d) {
buf_index = calc_index_f(c - base_b_i,
d - base_b_j, num_part);
cout << "seeking at"
<< _s_start_edge[offset]*sizeof(edge_t) << endl;
if ( -1 == fseek(f, _s_start_edge[offset]*sizeof(edge_t),
SEEK_SET)) {
cout << "fseek failed" << endl;
}
fwrite(buf[buf_index], sizeof(edge_t),
ecount[buf_index], f);
memset(count_edge[buf_index].val, 0,
sizeof(index_t)*p_p*p_p);
ecount[buf_index] = 0;
offset = beg_edge_offset2(c, d + 1);
}
} else {
#endif
for (index_t d = base_b_j; d < base_b_j + num_part; ++d) {
buf_index = calc_index_f(c - base_b_i,
d - base_b_j, num_part);
offset = beg_edge_offset2(c,d);
cout << "seeking at"
<< _s_start_edge[offset]*sizeof(edge_t) << endl;
if ( -1 == fseek(f, _s_start_edge[offset]*sizeof(edge_t),
SEEK_SET)) {
cout << "fseek failed" << endl;
}
fwrite(buf[buf_index], sizeof(edge_t),
ecount[buf_index], f);
//reset all the counts.
memset(count_edge[buf_index].val, 0,
sizeof(index_t)*p_p*p_p);
ecount[buf_index] = 0;
}
#ifdef HALF_GRID
}
#endif
}
}
#pragma omp barrier
}
}
}
fclose(f);
}
void grid::pre_csr(string edgefile, gedge_t* edges, index_t nedges)
{
_s_start_edge = (index_t*) calloc(sizeof(index_t), vert_count + 1);
#ifndef HALF_GRID
s_count_edge_in = (index_t*) calloc(sizeof(index_t), vert_count + 1);
#endif
vert_degree = (degree_t*)calloc(sizeof(degree_t), vert_count);
#pragma omp parallel num_threads(NUM_THDS)
{
gedge_t edge;
vertex_t v0, v1;
#pragma omp for
for(index_t k = 0; k < nedges; ++k) {
edge = edges[k];
if (edge.is_self_loop()) continue;
v0 = edge.get_v0();
v1 = edge.get_v1();
__sync_fetch_and_add(_s_start_edge + v0, 1);
__sync_fetch_and_add(vert_degree + v0, 1);
#ifdef HALF_GRID
__sync_fetch_and_add(_s_start_edge + v1, 1);
__sync_fetch_and_add(vert_degree + v1, 1);
#else
__sync_fetch_and_add(s_count_edge_in + v1, 1);
#endif
}
}
//Calculate the CSR beg_pos
index_t prefix_sum = 0;
index_t curr_value = 0;
#ifndef HALF_GRID
index_t prefix_sum_in = 0;
index_t curr_value_in = 0;
#endif
for (index_t ipart = 0; ipart < vert_count; ++ipart) {
curr_value = _s_start_edge[ipart];
_s_start_edge[ipart] = prefix_sum;
prefix_sum += curr_value;
#ifndef HALF_GRID
curr_value_in = s_count_edge_in[ipart];
s_count_edge_in[ipart] = prefix_sum_in;
prefix_sum_in += curr_value_in;
#endif
}
_s_start_edge[vert_count] = prefix_sum;
#ifndef HALF_GRID
s_count_edge_in[vert_count] = prefix_sum_in;
cout << "Total edges = " << prefix_sum_in << endl;
#endif
cout << "Total edges = " << prefix_sum << endl;
}
void grid::proc_csr(string edgefile, string part_file)
{
//read the binary edge file
int fid_edge = open(edgefile.c_str(), O_RDONLY);
struct stat st_edge;
fstat(fid_edge, &st_edge);
assert(st_edge.st_size != 0);
index_t nedges = st_edge.st_size/sizeof(gedge_t);
gedge_t* edges;
/*
edges = (gedge_t*)mmap(0, st_edge.st_size, PROT_READ,
MAP_PRIVATE, fid_edge, 0);
madvise(edges, st_edge.st_size, MADV_SEQUENTIAL);
*/
edges = (gedge_t*) malloc(st_edge.st_size);
FILE* f = fopen(edgefile.c_str(), "rb");
fread(edges, sizeof(gedge_t), nedges, f);
double start = mywtime();
pre_csr(edgefile, edges, nedges);
index_t* count_edge = (index_t*) calloc(sizeof(index_t), vert_count);
uint32_t* adj = (uint32_t*)calloc(sizeof(uint32_t), _s_start_edge[vert_count]);
#ifndef HALF_GRID
uint32_t* adj_in = (uint32_t*)calloc(sizeof(uint32_t), s_count_edge_in[vert_count]);
index_t* count_edge_in = (index_t*) calloc(sizeof(index_t), vert_count);
#endif
//---classify the edges in the grid
#pragma omp parallel num_threads(NUM_THDS)
{
gedge_t edge;
vertex_t v0, v1;
index_t n, m;
#pragma omp for
for(index_t k = 0; k < nedges; ++k) {
edge = edges[k];
if (edge.is_self_loop()) continue;
v0 = edge.get_v0();
v1 = edge.get_v1();
n = _s_start_edge[v0];
m = __sync_fetch_and_add(count_edge + v0, 1);
adj[n + m] = v1;
#ifdef HALF_GRID
n = _s_start_edge[v1];
m = __sync_fetch_and_add(count_edge + v1, 1);
adj[n + m] = v0;
#else
n = s_count_edge_in[v1];
m = __sync_fetch_and_add(count_edge_in + v1, 1);
adj_in[n + m] = v0;
#endif
}
}
//munmap (edges, st_edge.st_size);
//free(adj);
//free(_s_start_edge);
//#ifndef HALF_GRID
//free(adj_in);
//#endif
close(fid_edge);
fclose(f);
double end = mywtime();
cout << "CSR conversion Time = " << end - start << endl;
cout << "classifcation done" << endl;
}
void grid::pre_grid(string edgefile, gedge_t* edges, index_t nedges)
{
index_t total_s_part = calc_total_part(p_s);
_s_start_edge = (index_t*) calloc(sizeof(index_t), total_s_part);
vert_degree = (degree_t*)calloc(sizeof(degree_t), vert_count);
//Do a dry run to know the size of each partition
#pragma omp parallel num_threads(NUM_THDS)
{
gedge_t edge;
part_t j, i;
spart_t s_i, s_j;
vertex_t v0, v1;
vertex_t v2, v3;
index_t offset = 0;
#ifdef HALF_GRID
matrix<spart_t, index_t> start_edge_half;
start_edge_half.part_count = p_p;
#endif
matrix_f<spart_t, index_t> start_edge_full;
start_edge_full.part_count = p_p;
matrix<spart_t, index_t>* start_edge;
#pragma omp for
for(index_t k = 0; k < nedges; ++k) {
edge = edges[k];
if (edge.is_self_loop()) continue;
v2 = edge.get_v0();
v3 = edge.get_v1();
#ifdef HALF_GRID
v0 = min(v2, v3);
v1 = max(v3, v2);
#else
v0 = v2;
v1 = v3;
#endif
i = (v0 >> bit_shift1);
j = (v1 >> bit_shift1);
s_i = ((v0 >> bit_shift2) & part_mask3_2);
s_j = ((v1 >> bit_shift2) & part_mask3_2);
#ifdef HALF_GRID
if (i == j) {
start_edge = &start_edge_half;
offset = beg_edge_offset1(i);
start_edge->val = _s_start_edge + offset;
} else {
start_edge = &start_edge_full;
offset = beg_edge_offset2(i, j);
start_edge->val = _s_start_edge + offset;
}
#else
start_edge = &start_edge_full;
offset = beg_edge_offset2(i, j);
start_edge->val = _s_start_edge + offset;
#endif
start_edge->atomic_incr(s_i, s_j);
__sync_fetch_and_add(vert_degree + edge.get_v0(), 1);
#ifdef HALF_GRID
__sync_fetch_and_add(vert_degree + edge.get_v1(), 1);
#endif
}
}
//Calculate the edge start
index_t prefix_sum = 0;
index_t curr_value = 0;
for (index_t ipart = 0; ipart < total_s_part; ++ipart) {
curr_value = _s_start_edge[ipart];
_s_start_edge[ipart] = prefix_sum;
prefix_sum += curr_value;
}
_s_start_edge[total_s_part] = prefix_sum;
cout << "Total edges = " << prefix_sum << endl;
}
void grid::proc_grid(string edgefile, string part_file)
{
//read the binary edge file
int fid_edge = open(edgefile.c_str(), O_RDONLY);
struct stat st_edge;
fstat(fid_edge, &st_edge);
assert(st_edge.st_size != 0);
index_t nedges = st_edge.st_size/sizeof(gedge_t);
gedge_t* edges;
//gedge_t* edges = (gedge_t*)mmap(0, st_edge.st_size, PROT_READ,
// MAP_PRIVATE, fid_edge, 0);
//madvise(edges, st_edge.st_size, MADV_SEQUENTIAL);
edges = (gedge_t*) malloc(st_edge.st_size);
FILE* f = fopen(edgefile.c_str(), "rb");
fread(edges, sizeof(gedge_t), nedges, f);
double start = mywtime();
pre_grid(edgefile, edges, nedges);
index_t total_s_part = calc_total_part(p_s);
index_t* s_count_edge = (index_t*) calloc(sizeof(index_t), total_s_part);
_edges = (edge_t*)calloc(sizeof(edge_t), _s_start_edge[total_s_part]);
//---classify the edges in the grid
//Do it mmap
//open and truncate
/*string file = part_file + ".grid";
int f = open(file.c_str(), O_RDWR|O_CREAT, S_IRWXU);
if (ENOENT == f ) {
cout << "failed to create binary grid file" << endl;
exit(-1);
}
ftruncate(f, prefix_sum*sizeof(edge_t));
_edges = (edge_t*)mmap(0, prefix_sum*sizeof(edge_t),
PROT_READ|PROT_WRITE,
MAP_SHARED,
f, 0);
if (MAP_FAILED == _edges) {
handle_error("pmap alloc");
}
*/
#pragma omp parallel num_threads(NUM_THDS)
{
gedge_t edge;
part_t j, i;
spart_t s_i, s_j;
vertex_t v0, v1;
vertex_t v2, v3;
index_t index;
#ifdef HALF_GRID
matrix<spart_t, index_t> start_edge_half;
start_edge_half.part_count = p_p;
matrix<spart_t, index_t> count_edge_half;
count_edge_half.part_count = p_p;
#endif
matrix_f<spart_t, index_t> start_edge_full;
start_edge_full.part_count = p_p;
matrix<spart_t, index_t>* start_edge;
matrix_f<spart_t, index_t> count_edge_full;
count_edge_full.part_count = p_p;
matrix<spart_t, index_t>* count_edge;
index_t offset, n, m;
#pragma omp for