forked from MohsenKoohi/LaganLighter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrans.c
2279 lines (1946 loc) · 69.2 KB
/
trans.c
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
#ifndef __TRANS_C
#define __TRANS_C
#include <unistd.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <assert.h>
#include <time.h>
#include "omp.c"
#include "partitioning.c"
#include "relabel.c"
void sort_neighbor_lists(struct par_env* pe, struct ll_400_graph* g)
{
assert(pe != NULL && g!= NULL);
// Allocating mem
unsigned int thread_partitions = 64;
unsigned int partitions_count = pe->threads_count * thread_partitions;
unsigned int* partitions = calloc(sizeof(unsigned int), partitions_count+1);
assert(partitions != NULL);
parallel_edge_partitioning(g, partitions, partitions_count);
unsigned long* ttimes = calloc(sizeof(unsigned long), pe->threads_count);
assert(ttimes != NULL);
struct dynamic_partitioning* dp = dynamic_partitioning_initialize(pe, partitions_count);
// Sorting
unsigned long mt = - get_nano_time();
#pragma omp parallel
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
for(unsigned int v = partitions[partition]; v < partitions[partition + 1]; v++)
{
unsigned int degree = g->offsets_list[v+1] - g->offsets_list[v];
if(degree < 2)
continue;
quick_sort_uint(&g->edges_list[g->offsets_list[v]], 0, degree - 1);
}
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
PTIP("Sorting");
// Releasing mem
free(partitions);
partitions = NULL;
free(ttimes);
ttimes = NULL;
dynamic_partitioning_release(dp);
dp = NULL;
return;
}
/*
Validates the transposition of `g` to `t`
returns `1` as true, and `0` as false
`flags`:
bit 0: no self-edges
*/
int validate_transposition(struct par_env* pe, struct ll_400_graph* g, struct ll_400_graph* t, unsigned int flags)
{
assert(pe != NULL && g != NULL && t != NULL);
// Allocating mem
unsigned int thread_partitions = 64;
unsigned int partitions_count = pe->threads_count * thread_partitions;
unsigned int* g_partitions = calloc(sizeof(unsigned int), partitions_count+1);
assert(g_partitions != NULL);
parallel_edge_partitioning(g, g_partitions, partitions_count);
unsigned int* t_partitions = calloc(sizeof(unsigned int), partitions_count+1);
assert(t_partitions != NULL);
parallel_edge_partitioning(t, t_partitions, partitions_count);
unsigned long* ttimes = calloc(sizeof(unsigned long), pe->threads_count);
assert(ttimes != NULL);
struct dynamic_partitioning* dp = dynamic_partitioning_initialize(pe, partitions_count);
int ret = 1;
// Validation
// Initial checks
assert(t->vertices_count == g->vertices_count);
assert(t->offsets_list[0] == 0);
assert(t->offsets_list[t->vertices_count] == t->edges_count);
assert(t->edges_count <= g->edges_count);
// Check if `g` is sorted
unsigned long mt = - get_nano_time();
#pragma omp parallel
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned long thread_se = 0;
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
for(unsigned int v = g_partitions[partition]; v < g_partitions[partition + 1]; v++)
for(unsigned long e = g->offsets_list[v]; e < g->offsets_list[v + 1]; e++)
if(e < (g->offsets_list[v + 1] - 1))
if(g->edges_list[e] >= g->edges_list[e + 1])
{
printf("v:%'u, deg:%'lu, eo:%'lu, neighbour: %'u, next-neighbour: %'u\n", v, g->offsets_list[v+1] - g->offsets_list[v], e, g->edges_list[e], g->edges_list[e+1]);
assert(g->edges_list[e] < g->edges_list[e + 1] && "The input graph does not have sorted neighbour-lists");
ret = 0;
}
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
PTIP("Validation 1, check input is sorted");
if(!ret)
goto validate_transposition_rel_mem;
// If an edge is in `t` it should be in `g`
mt = - get_nano_time();
#pragma omp parallel
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
for(unsigned int v = t_partitions[partition]; v < t_partitions[partition + 1]; v++)
{
assert(t->offsets_list[v+1] >= t->offsets_list[v]);
for(unsigned long e = t->offsets_list[v]; e < t->offsets_list[v + 1]; e++)
{
unsigned int dest = v;
unsigned int src = t->edges_list[e];
if((flags & 1U) && src == dest)
{
printf("Validation 1 error: src == dest %'u->%'u\n", src, dest);
assert(dest != src);
ret = 0;
}
unsigned long found = uint_binary_search(g->edges_list, g->offsets_list[src], g->offsets_list[src + 1], dest);
if(found == -1UL)
{
printf("Validation 1 error: cannot find %'u->%'u\n", src, dest);
assert(found != -1UL);
ret = 0;
}
}
}
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
PTIP("Validation 2, output includes all edges of the input");
if(!ret)
goto validate_transposition_rel_mem;
// If an edge is in `g` it should be in `t` for both endpoints
mt = - get_nano_time();
#pragma omp parallel
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
for(unsigned int v = g_partitions[partition]; v < g_partitions[partition + 1]; v++)
for(unsigned long e = g->offsets_list[v]; e < g->offsets_list[v + 1]; e++)
{
unsigned int src = v;
unsigned int dest = g->edges_list[e];
if((flags & 1U) && src == dest)
continue;
unsigned long found = uint_binary_search(t->edges_list, t->offsets_list[dest], t->offsets_list[dest + 1], src);
if(found == -1UL)
{
printf("Validation 2 error: cannot find %'u->%'u\n", src, dest);
assert(found != -1UL);
ret = 0;
}
}
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
PTIP("Validation 3, input includes all edges of the output");
// Releasing mem
validate_transposition_rel_mem:
free(t_partitions);
t_partitions = NULL;
free(g_partitions);
g_partitions = NULL;
free(ttimes);
ttimes = NULL;
dynamic_partitioning_release(dp);
dp = NULL;
return ret;
}
/*
The input graph should have sorted neighbour-lists as binary search is
used in order to check if a reverse edges exists.
Before increasing symmetric degree of each vertex in Step 2, we search to see
if that edge exists in the neighbour-list of the destination.
Total comlexity: O(|E|log(|E|/|V|)), assuming degree of each vertex is |E|/|V|.
We assign a bit for each edge to specify if this edge is symmetric or not.
Using this bit array we do not repeat the search in Step 5.
A faster version can rewrite all edges without searching for repeated edges
(i.e., we need dynamic memory for neighbour lists of each vertex),
but then after sorting we can remove repeated edges and rewrite the offsets list and filtered edges list.
This requires more memory, but is faster.
Total complexity of this version will be: O((|E|/|V|)*log(|E|/|V|)).
Another solution is to create the csc graph from csr (2|E| + |E|log(|E|/V|)),
and then creating symmetric graph from csc and csr. While the approximate complexity is the same as the first one,
this solution is faster as in the first solution we search out-neighbour list of each out-neighbour,
but in the third solution, we sort in-neighbours of each vertex. Moreover, when we want to sort the output,
sorting the symmetric graph is more prone to load imbalance than the csc graph.
flags:
bit 0 : validate results
bit 1 : sort neighbour-list of the output
bit 2 : remove self-edges
*/
struct ll_400_graph* symmetrize_graph(struct par_env* pe, struct ll_400_graph* in_graph, unsigned int flags)
{
// Initial checks
unsigned long tt = - get_nano_time();
assert(pe != NULL && in_graph != NULL);
printf("\n\033[3;35msymmetrize_graph\033[0;37m using \033[3;35m%d\033[0;37m threads.\n", pe->threads_count);
unsigned long free_mem = get_free_mem();
if(free_mem < in_graph->edges_count * sizeof(unsigned int) + in_graph->vertices_count * sizeof(unsigned long))
{
printf("Not enough memory.\n");
return NULL;
}
// Partitioning
unsigned int thread_partitions = 64;
unsigned int partitions_count = pe->threads_count * thread_partitions;
unsigned int* partitions = calloc(sizeof(unsigned int), partitions_count+1);
assert(partitions != NULL);
parallel_edge_partitioning(in_graph, partitions, partitions_count);
struct dynamic_partitioning* dp = dynamic_partitioning_initialize(pe, partitions_count);
// Allocating memory
struct ll_400_graph* out_graph =calloc(sizeof(struct ll_400_graph),1);
assert(out_graph != NULL);
out_graph->vertices_count = in_graph->vertices_count;
out_graph->offsets_list = numa_alloc_interleaved(sizeof(unsigned long) * ( 1 + in_graph->vertices_count));
assert(out_graph->offsets_list != NULL);
unsigned long* last_offsets = numa_alloc_interleaved(sizeof(unsigned long) * ( 1 + in_graph->vertices_count));
assert(last_offsets != NULL);
unsigned char* edge_is_symmetric = numa_alloc_interleaved(sizeof(unsigned char) * ( 1 + in_graph->edges_count / 8));
assert(edge_is_symmetric != NULL);
unsigned long* partitions_total_edges = calloc(sizeof(unsigned long), partitions_count);
assert(partitions_total_edges != NULL);
unsigned long* ttimes = calloc(sizeof(unsigned long), pe->threads_count);
assert(ttimes != NULL);
// (1) Checking if neighbour-lists are sorted and decrease degree for self-edges
unsigned long mt = - get_nano_time();
unsigned long self_edges = 0;
#pragma omp parallel reduction(+:self_edges)
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
for(unsigned int v = partitions[partition]; v < partitions[partition + 1]; v++)
{
long degree = in_graph->offsets_list[v+1] - in_graph->offsets_list[v];
for(unsigned long e = in_graph->offsets_list[v]; e < in_graph->offsets_list[v + 1]; e++)
{
if(flags & 4U) // remove self edges
if(in_graph->edges_list[e] == v)
{
self_edges++;
degree--;
}
if(e < (in_graph->offsets_list[v + 1] - 1))
if(in_graph->edges_list[e] >= in_graph->edges_list[e + 1])
{
printf("v:%u deg:%lu eo:%lu neighbour:%u neighbour+1:%u\n", v, in_graph->offsets_list[v+1] - in_graph->offsets_list[v], e, in_graph->edges_list[e], in_graph->edges_list[e+1]);
assert(in_graph->edges_list[e] < in_graph->edges_list[e + 1] && "The input graph does not have sorted neighbour-lists");
}
}
assert(degree >= 0);
out_graph->offsets_list[v] = degree;
}
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
PTIP("1: Check");
printf("%-20s \t\t\t %'10lu\n","Self edges:", self_edges);
// (2) Identifying degree of vertices in the symmetric graph
mt = - get_nano_time();
#pragma omp parallel
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
for(unsigned int v = partitions[partition]; v < partitions[partition + 1]; v++)
for(unsigned long e = in_graph->offsets_list[v]; e < in_graph->offsets_list[v + 1]; e++)
{
unsigned int dest = in_graph->edges_list[e];
// self-edges are already in in_graph (if they should exist), so we do not add them again
if(dest == v)
continue;
// Check if the edge exists in the neighbour-list of the dest
if(uint_binary_search(in_graph->edges_list, in_graph->offsets_list[dest], in_graph->offsets_list[dest + 1], v) != -1UL)
{
// setting the bit in the edge_is_symmetric to prevent being searched again in Step 5
unsigned char val = (((unsigned char)1)<<(e % 8));
unsigned char val2 = __atomic_fetch_or(&edge_is_symmetric[e / 8], val, __ATOMIC_RELAXED);
assert((val2 & val) == 0);
continue;
}
// Increment the degree of dest
__atomic_fetch_add(&out_graph->offsets_list[dest], 1UL, __ATOMIC_RELAXED);
}
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
PTIP("2: Degree");
// (3) Storing the total edges of each partition in partitions_total_edges
mt = - get_nano_time();
#pragma omp parallel
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
#pragma omp for nowait
for(unsigned int p = 0; p<partitions_count; p++)
{
unsigned long sum = 0;
for(unsigned int v = partitions[p]; v < partitions[p + 1]; v++)
sum += out_graph->offsets_list[v];
partitions_total_edges[p] = sum;
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
PTIP("3: Sum");
// Partial sum of partitions_total_edges
{
unsigned long sum = 0;
for(unsigned int p = 0; p < partitions_count; p++)
{
unsigned long temp = partitions_total_edges[p];
partitions_total_edges[p] = sum;
sum += temp;
}
out_graph->edges_count = sum;
printf("%-20s \t\t\t %'10lu\n","Symmetric edges:", out_graph->edges_count);
}
out_graph->offsets_list[out_graph->vertices_count] = out_graph->edges_count;
out_graph->edges_list = numa_alloc_interleaved(sizeof(unsigned int) * out_graph->edges_count);
assert(out_graph->edges_list != NULL);
// (4) Updating the last_offsets and out_graph->offsets_list and copying in_graph edges of each vertex
mt = - get_nano_time();
#pragma omp parallel
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
unsigned long current_offset = partitions_total_edges[partition];
for(unsigned int v = partitions[partition]; v < partitions[partition + 1]; v++)
{
unsigned long sym_degree = out_graph->offsets_list[v];
out_graph->offsets_list[v] = current_offset;
unsigned long last_offset = current_offset;
current_offset += sym_degree;
for(unsigned long e = in_graph->offsets_list[v]; e < in_graph->offsets_list[v+1]; e++)
{
unsigned int neighbour = in_graph->edges_list[e];
if( (flags & 4U) && neighbour == v)
continue;
out_graph->edges_list[last_offset++] = neighbour;
}
last_offsets[v] = last_offset;
assert(last_offset <= current_offset);
}
if(partition + 1 < partitions_count)
assert(current_offset == partitions_total_edges[partition + 1]);
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
PTIP("4: last_offsets");
// (5) Writing edges
mt = - get_nano_time();
#pragma omp parallel
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
for(unsigned int v = partitions[partition]; v < partitions[partition + 1]; v++)
for(unsigned long e = in_graph->offsets_list[v]; e < in_graph->offsets_list[v + 1]; e++)
{
unsigned int src = v;
unsigned int dest = in_graph->edges_list[e];
// do not duplicate self edges
if(src == dest)
continue;
if(edge_is_symmetric[e / 8] & ( ((unsigned char)1) << (e % 8) ) )
// if(uint_binary_search(in_graph->edges_list, in_graph->offsets_list[dest], in_graph->offsets_list[dest + 1], src) != -1UL)
continue;
unsigned long prev_offset = __atomic_fetch_add(&last_offsets[dest], 1UL, __ATOMIC_RELAXED);
assert(prev_offset < out_graph->offsets_list[dest+1]);
out_graph->edges_list[prev_offset] = src;
}
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
PTIP("5: Writing edges");
// Sorting
if((flags & 2U))
{
mt = - get_nano_time();
#pragma omp parallel
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int partition = -1U;
#pragma omp for nowait schedule(static, 16)
for(unsigned int v = 0; v < out_graph->vertices_count; v++)
{
unsigned int degree = out_graph->offsets_list[v+1] - out_graph->offsets_list[v];
if(degree < 2)
continue;
quick_sort_uint(&out_graph->edges_list[out_graph->offsets_list[v]], 0, degree - 1);
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
PTIP("Sorting");
}
// Validation
if((flags & 1U))
{
assert(out_graph->vertices_count == in_graph->vertices_count);
assert(out_graph->offsets_list[0] == 0);
assert(out_graph->offsets_list[out_graph->vertices_count] == out_graph->edges_count);
// If an edge is in out_graph it should be in in_graph
mt = - get_nano_time();
#pragma omp parallel
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
for(unsigned int v = partitions[partition]; v < partitions[partition + 1]; v++)
{
assert(out_graph->offsets_list[v+1] == last_offsets[v]);
assert(out_graph->offsets_list[v+1] >= out_graph->offsets_list[v]);
for(unsigned long e = out_graph->offsets_list[v]; e < out_graph->offsets_list[v + 1]; e++)
{
unsigned int dest = v;
unsigned int src = out_graph->edges_list[e];
if((flags & 4U) && src == dest)
{
printf("Validation error: src == dest %'u->%'u\n", src, dest);
assert(dest != src);
}
// it can be an edge in the neighbour-list of src
unsigned long found = uint_binary_search(in_graph->edges_list, in_graph->offsets_list[src], in_graph->offsets_list[src + 1], dest);
if(found != -1UL)
continue;
// it can be an edge in the neighbour-list of dest
found = uint_binary_search(in_graph->edges_list, in_graph->offsets_list[dest], in_graph->offsets_list[dest + 1], src);
if(found == -1UL)
{
printf("Validation error: cannot find %'u->%'u\n", src, dest);
assert(found != -1UL);
}
}
}
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
PTIP("Validation 1");
// If an edge is in in_graph it should be in out_graph for both endpoints
assert((flags & 2U) && "Neighbour-list should be sorted for the second evaluation.");
mt = - get_nano_time();
#pragma omp parallel
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
for(unsigned int v = partitions[partition]; v < partitions[partition + 1]; v++)
for(unsigned long e = in_graph->offsets_list[v]; e < in_graph->offsets_list[v + 1]; e++)
{
unsigned int src = v;
unsigned int dest = in_graph->edges_list[e];
if((flags & 4U) && src == dest)
continue;
unsigned long found = uint_binary_search(out_graph->edges_list, out_graph->offsets_list[src], out_graph->offsets_list[src + 1], dest);
assert(found != -1UL);
found = uint_binary_search(out_graph->edges_list, out_graph->offsets_list[dest], out_graph->offsets_list[dest + 1], src);
assert(found != -1UL);
}
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
PTIP("Validation 2");
}
// Releasing memory
free(partitions);
partitions = NULL;
dynamic_partitioning_release(dp);
dp = NULL;
numa_free(last_offsets, (1 + in_graph->vertices_count) * sizeof(unsigned long));
last_offsets = NULL;
free(partitions_total_edges);
partitions_total_edges = NULL;
numa_free(edge_is_symmetric, sizeof(unsigned char) * ( 1 + in_graph->edges_count / 8));
edge_is_symmetric = NULL;
free(ttimes);
ttimes = NULL;
// Finalizing
tt += get_nano_time();
printf("%-20s \t\t\t %'.3f (s)\n\n","Total time:", tt/1e9);
print_ll_400_graph(out_graph);
return out_graph;
}
/*
atomic_transpose() has two passes over edges to identify degree of vertex and then to write neighbour-lists.
Total complexity is 2|E| plus |E|log(|E|/|V|) if neighbour-lists should be sorted.
flags:
bit 0 : validate results (requires bit 1 to be set)
bit 1 : sort neighbour-list of the output
bit 2 : remove self-edges
bit 3 : only create offsets_list of the out_graph and do not write edges
*/
struct ll_400_graph* atomic_transpose(struct par_env* pe, struct ll_400_graph* in_graph, unsigned int flags)
{
// Initial checks
unsigned long tt = - get_nano_time();
assert(pe != NULL && in_graph != NULL);
printf("\n\033[3;35matomic_transpose\033[0;37m using \033[3;35m%d\033[0;37m threads.\n", pe->threads_count);
// Partitioning
unsigned int thread_partitions = 64;
unsigned int partitions_count = pe->threads_count * thread_partitions;
unsigned int* partitions = calloc(sizeof(unsigned int), partitions_count+1);
assert(partitions != NULL);
parallel_edge_partitioning(in_graph, partitions, partitions_count);
struct dynamic_partitioning* dp = dynamic_partitioning_initialize(pe, partitions_count);
// Allocating memory
struct ll_400_graph* out_graph =calloc(sizeof(struct ll_400_graph),1);
assert(out_graph != NULL);
out_graph->vertices_count = in_graph->vertices_count;
out_graph->offsets_list = numa_alloc_interleaved(sizeof(unsigned long) * ( 1 + in_graph->vertices_count));
assert(out_graph->offsets_list != NULL);
unsigned long* partitions_total_edges = calloc(sizeof(unsigned long), partitions_count);
assert(partitions_total_edges != NULL);
unsigned long* ttimes = calloc(sizeof(unsigned long), pe->threads_count);
assert(ttimes != NULL);
// (1) Identifying degree of vertices in the out_graph
unsigned long self_edges = 0;
unsigned long mt = - get_nano_time();
#pragma omp parallel reduction(+:self_edges)
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
for(unsigned int v = partitions[partition]; v < partitions[partition + 1]; v++)
for(unsigned long e = in_graph->offsets_list[v]; e < in_graph->offsets_list[v + 1]; e++)
{
unsigned int dest = in_graph->edges_list[e];
if(dest == v)
{
self_edges++;
if(flags & 4U) // remove self edges
continue;
}
// Increment the degree of dest
__atomic_fetch_add(&out_graph->offsets_list[dest], 1UL, __ATOMIC_RELAXED);
}
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
PTIP("(1) Identifying degrees");
printf("%-20s \t\t\t %'10lu\n","Self edges:", self_edges);
// (2) Calculating sum of edges of each partition in partitions_total_edges
mt = - get_nano_time();
#pragma omp parallel
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
#pragma omp for nowait
for(unsigned int p = 0; p<partitions_count; p++)
{
unsigned long sum = 0;
for(unsigned int v = partitions[p]; v < partitions[p + 1]; v++)
sum += out_graph->offsets_list[v];
partitions_total_edges[p] = sum;
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
PTIP("(2) Calculating sum");
// Partial sum of partitions_total_edges
{
unsigned long sum = 0;
for(unsigned int p = 0; p < partitions_count; p++)
{
unsigned long temp = partitions_total_edges[p];
partitions_total_edges[p] = sum;
sum += temp;
}
out_graph->edges_count = sum;
printf("%-20s \t\t\t %'10lu\n","out_graph edges:", out_graph->edges_count);
}
out_graph->offsets_list[out_graph->vertices_count] = out_graph->edges_count;
// (3) Updating the out_graph->offsets_list
mt = - get_nano_time();
#pragma omp parallel
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
unsigned long current_offset = partitions_total_edges[partition];
for(unsigned int v = partitions[partition]; v < partitions[partition + 1]; v++)
{
unsigned long t_degree = out_graph->offsets_list[v];
out_graph->offsets_list[v] = current_offset;
current_offset += t_degree;
}
if(partition + 1 < partitions_count)
assert(current_offset == partitions_total_edges[partition + 1]);
else
assert(current_offset == out_graph->edges_count);
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
PTIP("(3) Update offsets_list");
if(flags & 8U)
goto atomic_transpose_release;
out_graph->edges_list = numa_alloc_interleaved(sizeof(unsigned int) * out_graph->edges_count);
assert(out_graph->edges_list != NULL);
// (4) Writing edges
mt = - get_nano_time();
#pragma omp parallel
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
for(unsigned int v = partitions[partition]; v < partitions[partition + 1]; v++)
for(unsigned long e = in_graph->offsets_list[v]; e < in_graph->offsets_list[v + 1]; e++)
{
unsigned int src = v;
unsigned int dest = in_graph->edges_list[e];
if(src == dest)
if(flags & 4U) // remove self edges
continue;
unsigned long prev_offset = __atomic_fetch_add(&out_graph->offsets_list[dest], 1UL, __ATOMIC_RELAXED);
assert(prev_offset < out_graph->offsets_list[dest+1]);
out_graph->edges_list[prev_offset] = src;
}
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
PTIP("(4) Writing edges");
// (5) Updating the out_graph->offsets_list
mt = - get_nano_time();
#pragma omp parallel
{
unsigned int tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
unsigned long current_offset = partitions_total_edges[partition];
for(unsigned int v = partitions[partition]; v < partitions[partition + 1]; v++)
{
unsigned long next_vertex_offset = out_graph->offsets_list[v];
out_graph->offsets_list[v] = current_offset;
current_offset = next_vertex_offset;
}
if(partition + 1 < partitions_count)
assert(current_offset == partitions_total_edges[partition + 1]);
else
assert(current_offset == out_graph->edges_count);
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
PTIP("(5) Updating offsets_list");
// (6) Sorting
if(flags & (2U | 1U))
sort_neighbor_lists(pe, out_graph);
// Validation
if((flags & 1U))
{
assert(flags & 2U);
unsigned int tf = 0;
if(flags & 4U)
tf = 1U;
int ret = validate_transposition(pe, in_graph, out_graph, tf);
if(ret != 1)
{
printf(" Validation failed.\n");
assert(ret == 1);
}
}
// Releasing memory
atomic_transpose_release:
free(partitions);
partitions = NULL;
dynamic_partitioning_release(dp);
dp = NULL;
free(partitions_total_edges);
partitions_total_edges = NULL;
free(ttimes);
ttimes = NULL;
// Finalizing
tt += get_nano_time();
printf("%-20s \t\t\t %'.3f (s)\n\n","Total time:", tt/1e9);
print_ll_400_graph(out_graph);
return out_graph;
}
/*
PoTra
@misc{PoTra,
title={On Optimizing Locality of Graph Transposition on Modern Architectures},
author={Mohsen {Koohi Esfahani} and Hans Vandierendonck},
year={2025},
eprint={2501.06872},
archivePrefix={arXiv},
primaryClass={cs.DC},
url={https://arxiv.org/abs/2501.06872},
doi={10.48550/arXiv.2501.06872}
}
Arguments:
flags:
bit 0: validate results (requires bit 1 to be set)
bit 1: sort neighbour-list of the output
bit 2: remove self-edges
bit 3: only create offsets_list of the out_graph and do not write edges
bit 4: force HLH
bit 5: force Atomic
exec_info: an array of 40
[0] : total exec. time without sorting and validation
[1-9]: PAPI events
[10-17]: Steps exec time (in nanoseconds)
10: Step 1: Counting degrees
11: Step 2: Identifying offsets_file
12: Step 3: Writing edges
13: Sorting
14: Validation
15: Step 1.1: Sampling time
16: ---
17: Step 1.2: Creating hash table
[18-19]: Energy measurement
18: DRAM energy in Joules
19: pacakges energy in Joules
[20-40]: Other info
20: Sampled edges
21: Max degree of sampled grpah
22: Sum of degrees of k-top vertices in the sampled graph
23: @INSERT: Collissions: total number of collision
24: @INSERT: Longest collision
25: @INSERT: Collided insertions: inserts with at least 1 collision
26: MSP_warmup_partitions
27: MSP_test_partitions
28: Average diff
29: @EDGES: Collissions
30: @EDGES: Longest collision
31: @EDGES: Collided vertices
32: reverse of load factor
33: k
34: Load imbalance in 3.1
35: Edges of k-top vertices
36: cache_bytes_per_HDV
37: MSP speedup
38: MSP result > 0 HLH(Hash-based LDV/HDV), <0 => Atomic
39: ---
Questions/Problems/Future Improvements:
- For some graphs such as MS50, the main improvement of HLH is on Step 3 and in Step 1 atomic is better.
So our MSP (Method Selection Procedure) cannot detect it ...
- For MS50 AD/kV is very small (4-6) except for its CSC version which is around 240
- Load imbalance in 3.1
- The large speedup on Clueweb12,CSR is not very clear.
*/
struct ll_400_graph* potra(struct par_env* pe, struct ll_400_graph* in_graph, unsigned int flags, unsigned long* exec_info)
{
#ifdef _ENERGY_MEASUREMENT
struct energy_measurement* em = energy_measurement_init();
energy_measurement_start(em);
#endif
// Initialization
unsigned long tt = - get_nano_time();
assert(pe != NULL && in_graph != NULL);
printf("\n\033[3;35mpotra\033[0;37m using \033[3;35m%d\033[0;37m threads, flags: %x.\n", pe->threads_count, flags);
// Reset papi
#pragma omp parallel
{
unsigned tid = omp_get_thread_num();
papi_reset(pe->papi_args[tid]);
}
// Constants
const unsigned long edges_to_be_sampled = 0.05 * in_graph -> vertices_count;
const double hash_table_load_factor = 0.5;
const unsigned int cache_bytes_per_HDV = 4;
const unsigned long max_k =
(pe->L3_caches_total_size + pe->L2_caches_total_size) /
(8 / hash_table_load_factor + cache_bytes_per_HDV * pe->threads_count);
const unsigned long k = min(max_k, in_graph->vertices_count);