-
Notifications
You must be signed in to change notification settings - Fork 3
/
graph.hpp
2971 lines (2547 loc) · 107 KB
/
graph.hpp
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
// ***********************************************************************
//
// NEVE
//
// ***********************************************************************
//
// Copyright (2019) Battelle Memorial Institute
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// ************************************************************************
#pragma once
#ifndef GRAPH_HPP
#define GRAPH_HPP
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <climits>
#include <array>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#if defined(USE_SHARED_MEMORY)
#include <omp.h>
#include <cstdlib>
#else
#include <mpi.h>
#endif
#include "algos.hpp"
unsigned seed;
#if defined(USE_SHARED_MEMORY)
class Graph
{
public:
Graph(): nv_(-1), ne_(-1),
edge_indices_(nullptr), edge_list_(nullptr),
vertex_degree_(nullptr)
{}
Graph(GraphElem nv):
nv_(nv), ne_(-1),
edge_list_(nullptr)
{
edge_indices_ = new GraphElem[nv_+1];
vertex_degree_ = new GraphWeight[nv_];
}
Graph(GraphElem nv, GraphElem ne):
nv_(nv), ne_(ne)
{
edge_indices_ = new GraphElem[nv_+1];
edge_list_ = new Edge[ne_];
vertex_degree_ = new GraphWeight[nv_];
}
~Graph()
{
delete []edge_indices_;
delete []edge_list_;
delete []vertex_degree_;
}
void set_edge_index(GraphElem const vertex, GraphElem const e0)
{
#if defined(DEBUG_BUILD)
assert((vertex >= 0) && (vertex <= nv_));
assert((e0 >= 0) && (e0 <= ne_));
edge_indices_.at(vertex) = e0;
#else
edge_indices_[vertex] = e0;
#endif
}
void edge_range(GraphElem const vertex, GraphElem& e0,
GraphElem& e1) const
{
e0 = edge_indices_[vertex];
e1 = edge_indices_[vertex+1];
}
void set_nedges(GraphElem ne)
{
ne_ = ne;
edge_list_ = new Edge[ne_];
}
GraphElem get_nv() const { return nv_; }
GraphElem get_ne() const { return ne_; }
// return edge and active info
// ----------------------------
Edge const& get_edge(GraphElem const index) const
{ return edge_list_[index]; }
Edge& set_edge(GraphElem const index)
{ return edge_list_[index]; }
// print edge list (with weights)
void print(bool print_weight = true) const
{
if (ne_ < MAX_PRINT_NEDGE)
{
for (GraphElem i = 0; i < nv_; i++)
{
GraphElem e0, e1;
edge_range(i, e0, e1);
if (print_weight) { // print weights (default)
for (GraphElem e = e0; e < e1; e++)
{
Edge const& edge = get_edge(e);
std::cout << i << " " << edge.tail_ << " " << edge.weight_ << std::endl;
}
}
else { // don't print weights
for (GraphElem e = e0; e < e1; e++)
{
Edge const& edge = get_edge(e);
std::cout << i << " " << edge.tail_ << std::endl;
}
}
}
}
else
{
std::cout << "Graph size is {" << nv_ << ", " << ne_ <<
"}, which will overwhelm STDOUT." << std::endl;
}
}
void flush()
{ std::memset(vertex_degree_, 0, nv_*sizeof(GraphWeight)); }
// Memory: 2*nv*(sizeof GraphElem) + 2*ne*(sizeof GraphWeight) + (2*ne*(sizeof GraphElem + GraphWeight))
#if defined(ZFILL_CACHE_LINES) && defined(__ARM_ARCH) && __ARM_ARCH >= 8
inline void nbrscan()
{
#pragma omp parallel
{
#ifdef LIKWID_MARKER_ENABLE
LIKWID_MARKER_START("nbrscan_zfill");
#endif
int const tid = omp_get_thread_num();
int const nthreads = omp_get_num_threads();
size_t chunk = nv_ / nthreads;
size_t rem = 0;
if (tid == nthreads - 1)
rem += nv_ % nthreads;
GraphWeight * const zfill_limit = vertex_degree_ + (tid+1)*chunk + rem - ZFILL_OFFSET;
#pragma omp for schedule(static)
for (GraphElem i=0; i < nv_; i+=ELEMS_PER_CACHE_LINE) {
GraphElem const * __restrict__ const edge_indices = edge_indices_ + i;
GraphWeight * __restrict__ const vertex_degree = vertex_degree_ + i;
if (vertex_degree + ZFILL_OFFSET < zfill_limit)
zfill(vertex_degree + ZFILL_OFFSET);
for(GraphElem j = 0; j < ELEMS_PER_CACHE_LINE; j++) {
if ((i + j) >= nv_)
break;
for (GraphElem e = edge_indices[j]; e < edge_indices[j+1]; e++) {
vertex_degree[j] = edge_list_[e].weight_;
}
}
}
#ifdef LIKWID_MARKER_ENABLE
LIKWID_MARKER_STOP("nbrscan_zfill");
#endif
} // parallel
}
#else
inline void nbrscan()
{
#ifdef LLNL_CALIPER_ENABLE
CALI_MARK_BEGIN("nbrscan");
CALI_MARK_BEGIN("parallel");
#endif
GraphElem e0, e1;
#ifdef ENABLE_PREFETCH
#ifdef __INTEL_COMPILER
#pragma noprefetch vertex_degree_
#pragma prefetch edge_indices_:3
#pragma prefetch edge_list_:3
#endif
#endif
#ifdef USE_OMP_DYNAMIC
#pragma omp parallel for schedule(dynamic)
#elif defined USE_OMP_TASKLOOP_MASTER
#pragma omp parallel
#pragma omp master
#pragma omp taskloop
#elif defined USE_OMP_TASKS_FOR
#pragma omp parallel
#pragma omp for
#elif defined LIKWID_MARKER_ENABLE
#pragma omp parallel
{
LIKWID_MARKER_START("nbrscan");
#pragma omp for schedule(static)
#else
#pragma omp parallel for schedule(static)
#endif
for (GraphElem i = 0; i < nv_; i++)
{
#ifdef USE_OMP_TASKS_FOR
#pragma omp task
{
#endif
for (GraphElem e = edge_indices_[i]; e < edge_indices_[i+1]; e++)
{
vertex_degree_[i] = edge_list_[e].weight_;
}
#ifdef USE_OMP_TASKS_FOR
}
#endif
}
#ifdef LLNL_CALIPER_ENABLE
CALI_MARK_END("parallel");
CALI_MARK_END("nbrscan");
#endif
#ifdef LIKWID_MARKER_ENABLE
LIKWID_MARKER_STOP("nbrscan");
}
#endif
}
#endif
// Memory: 2*nv*(sizeof GraphElem) + 3*ne*(sizeof GraphWeight) + (2*ne*(sizeof GraphElem + GraphWeight))
#if defined(ZFILL_CACHE_LINES) && defined(__ARM_ARCH) && __ARM_ARCH >= 8
inline void nbrsum()
{
#pragma omp parallel
{
#ifdef LIKWID_MARKER_ENABLE
LIKWID_MARKER_START("nbrsum_zfill");
#endif
int const tid = omp_get_thread_num();
int const nthreads = omp_get_num_threads();
size_t chunk = nv_ / nthreads;
size_t rem = 0;
if (tid == nthreads - 1)
rem += nv_ % nthreads;
GraphWeight * const zfill_limit = vertex_degree_ + (tid+1)*chunk + rem - ZFILL_OFFSET;
#pragma omp for schedule(static)
for (GraphElem i=0; i < nv_; i+=ELEMS_PER_CACHE_LINE) {
GraphElem const * __restrict__ const edge_indices = edge_indices_ + i;
GraphWeight * __restrict__ const vertex_degree = vertex_degree_ + i;
if (vertex_degree + ZFILL_OFFSET < zfill_limit)
zfill(vertex_degree + ZFILL_OFFSET);
for(GraphElem j = 0; j < ELEMS_PER_CACHE_LINE; j++) {
if ((i + j) >= nv_)
break;
GraphWeight sum = 0.0;
for (GraphElem e = edge_indices[j]; e < edge_indices[j+1]; e++) {
sum += edge_list_[e].weight_;
}
vertex_degree[j] = sum;
}
}
#ifdef LIKWID_MARKER_ENABLE
LIKWID_MARKER_STOP("nbrsum_zfill");
#endif
} // parallel
}
#else
inline void nbrsum()
{
#ifdef LLNL_CALIPER_ENABLE
CALI_MARK_BEGIN("nbrsum");
CALI_MARK_BEGIN("parallel");
#endif
GraphElem e0, e1;
#ifdef ENABLE_PREFETCH
#ifdef __INTEL_COMPILER
#pragma noprefetch vertex_degree_
#pragma prefetch edge_indices_:3
#pragma prefetch edge_list_:3
#endif
#endif
#ifdef USE_OMP_DYNAMIC
#pragma omp parallel for schedule(dynamic)
#elif defined USE_OMP_TASKLOOP_MASTER
#pragma omp parallel
#pragma omp master
#pragma omp taskloop
#elif defined USE_OMP_TASKS_FOR
#pragma omp parallel
#pragma omp for
#elif defined LIKWID_MARKER_ENABLE
#pragma omp parallel
{
LIKWID_MARKER_START("nbrsum");
#pragma omp for schedule(static)
#else
#pragma omp parallel for schedule(static)
#endif
for (GraphElem i = 0; i < nv_; i++)
{
#ifdef USE_OMP_TASKS_FOR
#pragma omp task
{
#endif
GraphWeight sum = 0.0;
for (GraphElem e = edge_indices_[i]; e < edge_indices_[i+1]; e++)
{
sum += edge_list_[e].weight_;
}
vertex_degree_[i] = sum;
#ifdef USE_OMP_TASKS_FOR
}
#endif
}
#ifdef LLNL_CALIPER_ENABLE
CALI_MARK_END("parallel");
CALI_MARK_END("nbrsum");
#endif
#ifdef LIKWID_MARKER_ENABLE
LIKWID_MARKER_STOP("nbrsum");
}
#endif
}
#endif
// Memory: 2*nv*(sizeof GraphElem) + 3*ne*(sizeof GraphWeight) + (2*ne*(sizeof GraphElem + GraphWeight))
#if defined(ZFILL_CACHE_LINES) && defined(__ARM_ARCH) && __ARM_ARCH >= 8
inline void nbrmax()
{
#pragma omp parallel
{
#ifdef LIKWID_MARKER_ENABLE
LIKWID_MARKER_START("nbrmax_zfill");
#endif
int const tid = omp_get_thread_num();
int const nthreads = omp_get_num_threads();
size_t chunk = nv_ / nthreads;
size_t rem = 0;
if (tid == nthreads - 1)
rem += nv_ % nthreads;
GraphWeight * const zfill_limit = vertex_degree_ + (tid+1)*chunk + rem - ZFILL_OFFSET;
#pragma omp for schedule(static)
for (GraphElem i=0; i < nv_; i+=ELEMS_PER_CACHE_LINE) {
GraphElem const * __restrict__ const edge_indices = edge_indices_ + i;
GraphWeight * __restrict__ const vertex_degree = vertex_degree_ + i;
if (vertex_degree + ZFILL_OFFSET < zfill_limit)
zfill(vertex_degree + ZFILL_OFFSET);
for(GraphElem j = 0; j < ELEMS_PER_CACHE_LINE; j++) {
if ((i + j) >= nv_)
break;
GraphWeight wmax = -1.0;
for (GraphElem e = edge_indices[j]; e < edge_indices[j+1]; e++) {
if (wmax < edge_list_[e].weight_)
wmax = edge_list_[e].weight_;
}
vertex_degree[j] = wmax;
}
}
#ifdef LIKWID_MARKER_ENABLE
LIKWID_MARKER_STOP("nbrmax_zfill");
#endif
} // parallel
}
#else
inline void nbrmax()
{
#ifdef LLNL_CALIPER_ENABLE
CALI_MARK_BEGIN("nbrmax");
CALI_MARK_BEGIN("parallel");
#endif
GraphElem e0, e1;
#ifdef ENABLE_PREFETCH
#ifdef __INTEL_COMPILER
#pragma noprefetch vertex_degree_
#pragma prefetch edge_indices_:3
#pragma prefetch edge_list_:3
#endif
#endif
#ifdef USE_OMP_DYNAMIC
#pragma omp parallel for schedule(dynamic)
#elif defined USE_OMP_TASKLOOP_MASTER
#pragma omp parallel
#pragma omp master
#pragma omp taskloop
#elif defined USE_OMP_TASKS_FOR
#pragma omp parallel
#pragma omp for
#elif defined LIKWID_MARKER_ENABLE
#pragma omp parallel
{
LIKWID_MARKER_START("nbrmax");
#pragma omp for schedule(static)
#else
#pragma omp parallel for schedule(static)
#endif
for (GraphElem i = 0; i < nv_; i++)
{
#ifdef USE_OMP_TASKS_FOR
#pragma omp task
{
#endif
GraphWeight wmax = -1.0;
for (GraphElem e = edge_indices_[i]; e < edge_indices_[i+1]; e++)
{
if (wmax < edge_list_[e].weight_)
wmax = edge_list_[e].weight_;
}
vertex_degree_[i] = wmax;
#ifdef USE_OMP_TASKS_FOR
}
#endif
}
#ifdef LLNL_CALIPER_ENABLE
CALI_MARK_END("parallel");
CALI_MARK_END("nbrmax");
#endif
#ifdef LIKWID_MARKER_ENABLE
LIKWID_MARKER_STOP("nbrmax");
}
#endif
}
#endif
// print statistics about edge distribution
void print_stats()
{
std::vector<GraphElem> pdeg(nv_, 0);
for (GraphElem v = 0; v < nv_; v++)
{
GraphElem e0, e1;
edge_range(v, e0, e1);
for (GraphElem e = e0; e < e1; e++)
pdeg[v] += 1;
}
std::sort(pdeg.begin(), pdeg.end());
GraphWeight loc = (GraphWeight)(nv_ + 1)/2.0;
GraphElem median;
if (fmod(loc, 1) != 0)
median = pdeg[(GraphElem)loc];
else
median = (pdeg[(GraphElem)floor(loc)] + pdeg[((GraphElem)floor(loc)+1)]) / 2;
GraphElem spdeg = std::accumulate(pdeg.begin(), pdeg.end(), 0);
GraphElem mpdeg = *(std::max_element(pdeg.begin(), pdeg.end()));
std::transform(pdeg.cbegin(), pdeg.cend(), pdeg.cbegin(),
pdeg.begin(), std::multiplies<GraphElem>{});
GraphElem psum_sq = std::accumulate(pdeg.begin(), pdeg.end(), 0);
GraphWeight paverage = (GraphWeight) spdeg / nv_;
GraphWeight pavg_sq = (GraphWeight) psum_sq / nv_;
GraphWeight pvar = std::abs(pavg_sq - (paverage*paverage));
GraphWeight pstddev = sqrt(pvar);
std::cout << std::endl;
std::cout << "--------------------------------------" << std::endl;
std::cout << "Graph characteristics" << std::endl;
std::cout << "--------------------------------------" << std::endl;
std::cout << "Number of vertices: " << nv_ << std::endl;
std::cout << "Number of edges: " << ne_ << std::endl;
std::cout << "Maximum number of edges: " << mpdeg << std::endl;
std::cout << "Median number of edges: " << median << std::endl;
std::cout << "Expected value of X^2: " << pavg_sq << std::endl;
std::cout << "Variance: " << pvar << std::endl;
std::cout << "Standard deviation: " << pstddev << std::endl;
std::cout << "--------------------------------------" << std::endl;
}
// public variables
GraphElem *edge_indices_;
Edge *edge_list_;
GraphWeight *vertex_degree_;
private:
GraphElem nv_, ne_;
};
// read in binary edge list files using POSIX I/O
class BinaryEdgeList
{
public:
BinaryEdgeList() :
M_(-1), N_(-1)
{}
// read a file and return a graph
Graph* read(std::string binfile)
{
std::ifstream file;
file.open(binfile.c_str(), std::ios::in | std::ios::binary);
if (!file.is_open())
{
std::cout << " Error opening file! " << std::endl;
std::abort();
}
// read the dimensions
file.read(reinterpret_cast<char*>(&M_), sizeof(GraphElem));
file.read(reinterpret_cast<char*>(&N_), sizeof(GraphElem));
#ifdef EDGE_AS_VERTEX_PAIR
GraphElem weighted;
file.read(reinterpret_cast<char*>(&weighted), sizeof(GraphElem));
N_ *= 2;
#endif
// create local graph
Graph *g = new Graph(M_, N_);
uint64_t tot_bytes=(M_+1)*sizeof(GraphElem);
ptrdiff_t offset = 2*sizeof(GraphElem);
if (tot_bytes < INT_MAX)
file.read(reinterpret_cast<char*>(&g->edge_indices_[0]), tot_bytes);
else
{
int chunk_bytes=INT_MAX;
uint8_t *curr_pointer = (uint8_t*) &g->edge_indices_[0];
uint64_t transf_bytes = 0;
while (transf_bytes < tot_bytes)
{
file.read(reinterpret_cast<char*>(&curr_pointer[offset]), chunk_bytes);
transf_bytes += chunk_bytes;
offset += chunk_bytes;
curr_pointer += chunk_bytes;
if ((tot_bytes - transf_bytes) < INT_MAX)
chunk_bytes = tot_bytes - transf_bytes;
}
}
N_ = g->edge_indices_[M_] - g->edge_indices_[0];
g->set_nedges(N_);
tot_bytes = N_*(sizeof(Edge));
offset = 2*sizeof(GraphElem) + (M_+1)*sizeof(GraphElem) + g->edge_indices_[0]*(sizeof(Edge));
#if defined(GRAPH_FT_LOAD)
ptrdiff_t currpos = file.tellg();
ptrdiff_t idx = 0;
GraphElem* vidx = (GraphElem*)malloc(M_ * sizeof(GraphElem));
const int num_sockets = (GRAPH_FT_LOAD == 0) ? 1 : GRAPH_FT_LOAD;
printf("Read file from %d sockets\n", num_sockets);
int n_blocks = num_sockets;
GraphElem NV_blk_sz = M_ / n_blocks;
GraphElem tid_blk_sz = omp_get_num_threads() / n_blocks;
#pragma omp parallel
{
for (int b=0; b<n_blocks; b++)
{
long NV_beg = b * NV_blk_sz;
long NV_end = std::min(M_, ((b+1) * NV_blk_sz) );
int tid_doit = b * tid_blk_sz;
if (omp_get_thread_num() == tid_doit)
{
// for each vertex within block
for (GraphElem i = NV_beg; i < NV_end ; i++)
{
// ensure first-touch allocation
// read and initialize using your code
vidx[i] = idx;
const GraphElem vcount = g->edge_indices_[i+1] - g->edge_indices_[i];
idx += vcount;
file.seekg(currpos + vidx[i] * sizeof(Edge), std::ios::beg);
file.read(reinterpret_cast<char*>(&g->edge_list_[vidx[i]]), sizeof(Edge) * (vcount));
}
}
}
}
free(vidx);
#else
if (tot_bytes < INT_MAX)
file.read(&g->edge_list_[0], tot_bytes);
else
{
int chunk_bytes=INT_MAX;
uint8_t *curr_pointer = (uint8_t*)&g->edge_list_[0];
uint64_t transf_bytes = 0;
while (transf_bytes < tot_bytes)
{
file.read(&curr_poointer[offset], tot_bytes);
transf_bytes += chunk_bytes;
offset += chunk_bytes;
curr_pointer += chunk_bytes;
if ((tot_bytes - transf_bytes) < INT_MAX)
chunk_bytes = (tot_bytes - transf_bytes);
}
}
#endif
file.close();
for(GraphElem i=1; i < M_+1; i++)
g->edge_indices_[i] -= g->edge_indices_[0];
g->edge_indices_[0] = 0;
return g;
}
private:
GraphElem M_, N_;
};
// RGG graph
class GenerateRGG
{
public:
GenerateRGG(GraphElem nv):
nv_(nv), rn_(0)
{
// calculate r(n)
GraphWeight rc = sqrt((GraphWeight)log(nv_)/(GraphWeight)(PI*nv_));
GraphWeight rt = sqrt((GraphWeight)2.0736/(GraphWeight)nv_);
rn_ = (rc + rt)/(GraphWeight)2.0;
assert(((GraphWeight)1.0) > rn_);
}
Graph* generate(bool isLCG, bool unitEdgeWeight = true, GraphWeight randomEdgePercent = 0.0)
{
std::vector<GraphWeight> X, Y;
X.resize(nv_);
Y.resize(nv_);
// create graph, edge list to be populated later
Graph *g = new Graph(nv_);
// measure the time to generate random numbers
double st = omp_get_wtime();
if (!isLCG) {
// set seed (declared an extern in utils)
seed = (unsigned)reseeder(1);
#if defined(PRINT_RANDOM_XY_COORD)
#pragma omp parallel for
for (GraphElem i = 0; i < nv_; i++) {
X[i] = genRandom<GraphWeight>(0.0, 1.0);
Y[i] = genRandom<GraphWeight>(0.0, 1.0);
std::cout << "X, Y: " << X[i] << ", " << Y[i] << std::endl;
}
#else
#pragma omp parallel for
for (GraphElem i = 0; i < nv_; i++) {
X[i] = genRandom<GraphWeight>(0.0, 1.0);
Y[i] = genRandom<GraphWeight>(0.0, 1.0);
}
#endif
}
else { // LCG
// X | Y
// e.g seeds: 1741, 3821
// create LCG object
// seed to generate x0
LCG xr(/*seed*/1, X.data(), nv_);
// generate random numbers between 0-1
xr.generate();
// rescale xr further between lo-hi
// and put the numbers in Y taking
// from X[n]
xr.rescale(Y.data(), nv_, 0);
#if defined(PRINT_RANDOM_XY_COORD)
for (GraphElem i = 0; i < nv_; i++) {
std::cout << "X, Y: " << X[i] << ", " << Y[i] << std::endl;
}
#endif
}
double et = omp_get_wtime();
double tt = et - st;
std::cout << "Average time to generate " << nv_
<< " random numbers using LCG (in s): "
<< tt << std::endl;
// edges
std::vector<EdgeTuple> edgeList;
#if defined(CHECK_NUM_EDGES)
GraphElem numEdges = 0;
#endif
for (GraphElem i = 0; i < nv_; i++) {
for (GraphElem j = i + 1; j < nv_; j++) {
// euclidean distance:
// 2D: sqrt((px-qx)^2 + (py-qy)^2)
GraphWeight dx = X[i] - X[j];
GraphWeight dy = Y[i] - Y[j];
GraphWeight ed = sqrt(dx*dx + dy*dy);
// are the two vertices within the range?
if (ed <= rn_) {
if (!unitEdgeWeight) {
edgeList.emplace_back(i, j, ed);
edgeList.emplace_back(j, i, ed);
}
else {
edgeList.emplace_back(i, j);
edgeList.emplace_back(j, i);
}
#if defined(CHECK_NUM_EDGES)
numEdges += 2;
#endif
g->edge_indices_[i+1]++;
g->edge_indices_[j+1]++;
}
}
}
// add random edges based on
// randomEdgePercent
if (randomEdgePercent > 0.0) {
const GraphElem pnedges = (edgeList.size()/2);
// extra #edges
const GraphElem nrande = ((GraphElem)(randomEdgePercent * (GraphWeight)pnedges)/100);
#if defined(PRINT_EXTRA_NEDGES)
int extraEdges = 0;
#endif
unsigned rande_seed = (unsigned)(time(0)^getpid());
GraphWeight weight = 1.0;
std::hash<GraphElem> reh;
// cannot use genRandom if it's already been seeded
std::default_random_engine re(rande_seed);
std::uniform_int_distribution<GraphElem> IR, JR;
std::uniform_real_distribution<GraphWeight> IJW;
for (GraphElem k = 0; k < nrande; k++) {
// randomly pick start/end vertex and target from my list
const GraphElem i = (GraphElem)IR(re, std::uniform_int_distribution<GraphElem>::param_type{0, (nv_- 1)});
const GraphElem j = (GraphElem)JR(re, std::uniform_int_distribution<GraphElem>::param_type{0, (nv_- 1)});
if (i == j)
continue;
// check for duplicates prior to edgeList insertion
auto found = std::find_if(edgeList.begin(), edgeList.end(),
[&](EdgeTuple const& et)
{ return ((et.ij_[0] == i) && (et.ij_[1] == j)); });
// OK to insert, not in list
if (found == std::end(edgeList)) {
// calculate weight
if (!unitEdgeWeight) {
GraphWeight dx = X[i] - X[j];
GraphWeight dy = Y[i] - Y[j];
weight = sqrt(dx*dx + dy*dy);
}
#if defined(PRINT_EXTRA_NEDGES)
extraEdges += 2;
#endif
#if defined(CHECK_NUM_EDGES)
numEdges += 2;
#endif
edgeList.emplace_back(i, j, weight);
edgeList.emplace_back(j, i, weight);
g->edge_indices_[i+1]++;
g->edge_indices_[j+1]++;
}
}
#if defined(PRINT_EXTRA_NEDGES)
std::cout << "Adding extra " << (extraEdges/2) << " edges while trying to incorporate "
<< randomEdgePercent << "%" << " extra edges globally." << std::endl;
#endif
} // end of (conditional) random edges addition
// set graph edge indices
std::partial_sum(g->edge_indices_, g->edge_indices_ + (nv_+1), g->edge_indices_);
for(GraphElem i = 1; i < nv_+1; i++)
g->edge_indices_[i] -= g->edge_indices_[0];
g->edge_indices_[0] = 0;
g->set_edge_index(0, 0);
for (GraphElem i = 0; i < nv_; i++)
g->set_edge_index(i+1, g->edge_indices_[i+1]);
const GraphElem nedges = g->edge_indices_[nv_] - g->edge_indices_[0];
g->set_nedges(nedges);
// set graph edge list
// sort edge list
auto ecmp = [] (EdgeTuple const& e0, EdgeTuple const& e1)
{ return ((e0.ij_[0] < e1.ij_[0]) || ((e0.ij_[0] == e1.ij_[0]) && (e0.ij_[1] < e1.ij_[1]))); };
if (!std::is_sorted(edgeList.begin(), edgeList.end(), ecmp)) {
#if defined(DEBUG_PRINTF)
std::cout << "Edge list is not sorted." << std::endl;
#endif
std::sort(edgeList.begin(), edgeList.end(), ecmp);
}
#if defined(DEBUG_PRINTF)
else
std::cout << "Edge list is sorted!" << std::endl;
#endif
GraphElem ePos = 0;
for (GraphElem i = 0; i < nv_; i++) {
GraphElem e0, e1;
g->edge_range(i, e0, e1);
#if defined(DEBUG_PRINTF)
if ((i % 100000) == 0)
std::cout << "Processing edges for vertex: " << i << ", range(" << e0 << ", " << e1 <<
")" << std::endl;
#endif
for (GraphElem j = e0; j < e1; j++) {
Edge &edge = g->set_edge(j);
assert(ePos == j);
assert(i == edgeList[ePos].ij_[0]);
edge.tail_ = edgeList[ePos].ij_[1];
edge.weight_ = edgeList[ePos].w_;
ePos++;
}
}
#if defined(CHECK_NUM_EDGES)
const GraphElem ne = g->get_ne();
assert(ne == numEdges);
#endif
edgeList.clear();
X.clear();
Y.clear();
return g;
}
GraphWeight get_d() const { return rn_; }
GraphElem get_nv() const { return nv_; }
private:
GraphElem nv_;
GraphWeight rn_;
};
#else // MPI per process graph instance
#if defined(ENABLE_HWLOC)
#include "hwloc.h"
#endif
class Graph
{
public:
Graph():
lnv_(-1), lne_(-1), nv_(-1),
ne_(-1), comm_(MPI_COMM_WORLD)
{
MPI_Comm_size(comm_, &size_);
MPI_Comm_rank(comm_, &rank_);
}
Graph(GraphElem lnv, GraphElem lne,
GraphElem nv, GraphElem ne,
MPI_Comm comm=MPI_COMM_WORLD):
lnv_(lnv), lne_(lne),
nv_(nv), ne_(ne),
comm_(comm)
{
MPI_Comm_size(comm_, &size_);
MPI_Comm_rank(comm_, &rank_);
degree_ = new GraphWeight[lnv_];
std::memset(degree_, 0, lnv_*sizeof(GraphWeight));
edge_indices_.resize(lnv_+1, 0);
edge_list_.resize(lne_); // this is usually populated later
parts_.resize(size_+1);
parts_[0] = 0;
for (GraphElem i = 1; i < size_+1; i++)
parts_[i] = ((nv_ * i) / size_);
}
~Graph()
{
delete []degree_;
edge_list_.clear();
edge_indices_.clear();
parts_.clear();
targets_.clear();
}
// update vertex partition information
void repart(std::vector<GraphElem> const& parts)
{ memcpy(parts_.data(), parts.data(), sizeof(GraphElem)*(size_+1)); }
// TODO FIXME put asserts like the following
// everywhere function member of Graph class
void set_edge_index(GraphElem const vertex, GraphElem const e0)
{
#if defined(DEBUG_BUILD)
assert((vertex >= 0) && (vertex <= lnv_));
assert((e0 >= 0) && (e0 <= lne_));
edge_indices_.at(vertex) = e0;
#else
edge_indices_[vertex] = e0;
#endif
}
void edge_range(GraphElem const vertex, GraphElem& e0,
GraphElem& e1) const
{
e0 = edge_indices_[vertex];
e1 = edge_indices_[vertex+1];
}
// collective
void set_nedges(GraphElem lne)
{
lne_ = lne;
edge_list_.resize(lne_);
// compute total number of edges
ne_ = 0;
MPI_Allreduce(&lne_, &ne_, 1, MPI_GRAPH_TYPE, MPI_SUM, comm_);
}
GraphElem get_base(const int rank) const
{ return parts_[rank]; }
GraphElem get_bound(const int rank) const
{ return parts_[rank+1]; }
GraphElem get_range(const int rank) const
{ return (parts_[rank+1] - parts_[rank] + 1); }
int get_owner(const GraphElem vertex) const
{
const std::vector<GraphElem>::const_iterator iter =
std::upper_bound(parts_.begin(), parts_.end(), vertex);
return (iter - parts_.begin() - 1);
}
GraphElem get_lnv() const { return lnv_; }
GraphElem get_lne() const { return lne_; }
GraphElem get_nv() const { return nv_; }