-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.hpp
653 lines (564 loc) · 19.2 KB
/
utils.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
// ***********************************************************************
//
// 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 UTILS_HPP
#define UTILS_HPP
#define PI (3.14159)
#ifndef MAX_PRINT_NEDGE
#define MAX_PRINT_NEDGE (100000)
#endif
// Read https://en.wikipedia.org/wiki/Linear_congruential_generator#Period_length
// about choice of LCG parameters
// From numerical recipes
// TODO FIXME investigate larger periods
#define MLCG (2147483647) // 2^31 - 1
#define ALCG (16807) // 7^5
#define BLCG (0)
#ifdef USE_SHARED_MEMORY
#ifdef NTIMES
#if NTIMES<=1
# define NTIMES 10
#endif
#endif
#ifndef NTIMES
# define NTIMES 10
#endif
#ifdef ALIGNMENT
#if ALIGNMENT<=8
# define ALIGNMENT 16
#endif
#endif
#ifndef ALIGNMENT
# define ALIGNMENT 16
#endif
#ifndef DEFAULT_NV
#define DEFAULT_NV 524288
#endif
#else
#define SR_UP_TAG 100
#define SR_DOWN_TAG 101
#define SR_SIZES_UP_TAG 102
#define SR_SIZES_DOWN_TAG 103
#define SR_X_UP_TAG 104
#define SR_X_DOWN_TAG 105
#define SR_Y_UP_TAG 106
#define SR_Y_DOWN_TAG 107
#define SR_LCG_TAG 108
#endif
#include <random>
#include <utility>
#include <cstring>
#ifdef USE_32_BIT_GRAPH
using GraphElem = int32_t;
using GraphWeight = float;
#ifdef USE_SHARED_MEMORY
typedef std::aligned_storage<sizeof(GraphElem),alignof(GraphElem)>::type __GraphElem__;
typedef std::aligned_storage<sizeof(GraphWeight),alignof(GraphWeight)>::type __GraphWeight__;
#else
const MPI_Datatype MPI_GRAPH_TYPE = MPI_INT32_T;
const MPI_Datatype MPI_WEIGHT_TYPE = MPI_FLOAT;
#endif
#else
using GraphElem = int64_t;
using GraphWeight = double;
#ifdef USE_SHARED_MEMORY
typedef std::aligned_storage<sizeof(GraphElem),alignof(GraphElem)>::type __GraphElem__;
typedef std::aligned_storage<sizeof(GraphWeight),alignof(GraphWeight)>::type __GraphWeight__;
#else
const MPI_Datatype MPI_GRAPH_TYPE = MPI_INT64_T;
const MPI_Datatype MPI_WEIGHT_TYPE = MPI_DOUBLE;
#endif
#endif
extern unsigned seed;
#ifdef EDGE_AS_VERTEX_PAIR
struct Edge
{
GraphElem head_, tail_;
GraphWeight weight_;
Edge(): head_(-1), tail_(-1), weight_(0.0) {}
};
#else
struct Edge
{
GraphElem tail_;
GraphWeight weight_;
Edge(): tail_(-1), weight_(0.0) {}
};
#endif
#if defined(USE_SHARED_MEMORY) && defined(ZFILL_CACHE_LINES) && defined(__ARM_ARCH) && __ARM_ARCH >= 8
#ifndef CACHE_LINE_SIZE_BYTES
#define CACHE_LINE_SIZE_BYTES 256
#endif
/* The zfill distance must be large enough to be ahead of the L2 prefetcher */
static const int ZFILL_DISTANCE = 100;
/* x-byte cache lines */
static const int ELEMS_PER_CACHE_LINE = CACHE_LINE_SIZE_BYTES / sizeof(GraphWeight);
/* Offset from a[j] to zfill */
static const int ZFILL_OFFSET = ZFILL_DISTANCE * ELEMS_PER_CACHE_LINE;
static inline void zfill(GraphWeight * a)
{ asm volatile("dc zva, %0": : "r"(a)); }
#endif
struct EdgeTuple
{
GraphElem ij_[2];
GraphWeight w_;
EdgeTuple(GraphElem i, GraphElem j, GraphWeight w):
ij_{i, j}, w_(w)
{}
EdgeTuple(GraphElem i, GraphElem j):
ij_{i, j}, w_(1.0)
{}
EdgeTuple():
ij_{-1, -1}, w_(0.0)
{}
};
#if !defined(USE_SHARED_MEMORY)
#include <mpi.h>
struct EdgeTuple2
{
GraphElem i_, j_;
GraphWeight w_;
EdgeTuple2(GraphElem i, GraphElem j, GraphWeight w):
i_(i), j_(j), w_(w)
{}
EdgeTuple2(GraphElem i, GraphElem j):
i_(i), j_(j), w_(1.0)
{}
EdgeTuple2():
i_(-1), j_(-1), w_(0.0)
{}
};
void createEdgeTupleType(MPI_Datatype* edgeType)
{
EdgeTuple2 einfo;
MPI_Aint begin, s, t, w;
MPI_Get_address(&einfo, &begin);
MPI_Get_address(&einfo.i_, &s);
MPI_Get_address(&einfo.j_, &t);
MPI_Get_address(&einfo.w_, &w);
int blens[] = { 1, 1, 1 };
MPI_Aint displ[] = { s - begin, t - begin, w - begin };
MPI_Datatype types[] = { MPI_GRAPH_TYPE, MPI_GRAPH_TYPE, MPI_WEIGHT_TYPE };
MPI_Type_create_struct(3, blens, displ, types, edgeType);
MPI_Type_commit(edgeType);
}
void freeEdgeTupleType(MPI_Datatype* edgeType)
{ MPI_Type_free(edgeType); }
#endif
enum DegreeOrder { none, ascending, descending, normal };
enum ProcessGraphOutput { no, adjacency, chaco_unweighted, chaco_weighted };
// Is nprocs a power-of-2?
int is_pwr2(int pes)
{ return ((pes != 0) && !(pes & (pes - 1))); }
// return unint32_t seed
GraphElem reseeder(unsigned initseed)
{
std::seed_seq seq({initseed});
std::vector<std::uint32_t> seeds(1);
seq.generate(seeds.begin(), seeds.end());
return (GraphElem)seeds[0];
}
// Local random number generator
template<typename T, typename G = std::default_random_engine>
T genRandom(T lo, T hi)
{
thread_local static G gen(seed);
using Dist = typename std::conditional
<
std::is_integral<T>::value
, std::uniform_int_distribution<T>
, std::uniform_real_distribution<T>
>::type;
thread_local static Dist utd {};
return utd(gen, typename Dist::param_type{lo, hi});
}
// https://stackoverflow.com/questions/3909272/sorting-two-corresponding-arrays
template <typename T>
class sort_indices
{
public:
sort_indices(T* ptr) : ptr_(ptr) {}
bool operator()(T i, T j) const
{ return ptr_[i] < ptr_[j]; }
private:
T* ptr_;
};
template <typename T>
class sort_indices_greater
{
public:
sort_indices_greater(T* ptr) : ptr_(ptr) {}
bool operator()(T i, T j) const
{ return ptr_[i] > ptr_[j]; }
private:
T* ptr_;
};
// CSR for shared-memory/serial implementations
class CSR
{
public:
CSR(GraphElem nv, GraphElem ne):
nv_(nv), ne_(ne)
{
edge_indices_ = new GraphElem[nv_+1];
edge_list_ = new Edge[ne_];
}
~CSR()
{
delete []edge_indices_;
delete []edge_list_;
}
void edge_range(GraphElem const vertex, GraphElem& e0,
GraphElem& e1) const
{
e0 = edge_indices_[vertex];
e1 = edge_indices_[vertex+1];
}
GraphElem get_nv() const { return nv_; }
GraphElem get_ne() const { return ne_; }
Edge const& get_edge(GraphElem const index) const
{ return edge_list_[index]; }
Edge& get_edge(GraphElem const index)
{ return edge_list_[index]; }
GraphElem *edge_indices_;
Edge *edge_list_;
private:
GraphElem nv_, ne_;
};
// Parallel Linear Congruential Generator
// x[i] = (a*x[i-1] + b)%M
#ifdef USE_SHARED_MEMORY
class LCG
{
public:
LCG(unsigned seed, GraphWeight* drand, GraphElem n) :
seed_(seed), drand_(drand), n_(n)
{
// allocate long random numbers
rnums_.resize(n_);
// init x0
x0_ = reseeder(seed_);
// prefix to generate first random value per process
prefix_op();
}
~LCG() { rnums_.clear(); }
// matrix-matrix multiplication for 2x2 matrices
void matmat_2x2(GraphElem c[], GraphElem a[], GraphElem b[])
{
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
GraphElem sum = 0;
for (int k = 0; k < 2; k++) {
sum += a[i*2+k]*b[k*2+j];
}
c[i*2+j] = sum;
}
}
}
// x *= y
void matop_2x2(GraphElem x[], GraphElem y[])
{
GraphElem tmp[4];
matmat_2x2(tmp, x, y);
memcpy(x, tmp, sizeof(GraphElem[4]));
}
// find kth power of a 2x2 matrix
void mat_power(GraphElem mat[], GraphElem k)
{
GraphElem tmp[4];
memcpy(tmp, mat, sizeof(GraphElem[4]));
// mat-mat multiply k times
for (GraphElem p = 0; p < k-1; p++)
matop_2x2(mat, tmp);
}
// prefix for matrix-matrix operation
// `x0 is the very first random number in the series
// `ab is a 2-length array which stores a and b
// `n_ is #vertices == nv or n_
// `rnums is n_ length array which stores the random nums for a process
void prefix_op()
{
GraphElem global_op[4];
global_op[0] = ALCG;
global_op[1] = 0;
global_op[2] = BLCG;
global_op[3] = 1;
mat_power(global_op, n_); // M^(n/p)
GraphElem prefix_op[4] = {1,0,0,1}; // I in row-major
// populate the first random number entry - (x0*a + b)%P
rnums_[0] = x0_;
}
// generate random number based on the first
// random number on a process
// TODO check the 'quick'n dirty generators to
// see if we can avoid the mod
void generate()
{
#if defined(PRINT_LCG_LONG_RANDOM_NUMBERS)
std::cout << rnums_[0] << std::endl;
for (GraphElem i = 1; i < n_; i++) {
rnums_[i] = (rnums_[i-1]*ALCG + BLCG)%MLCG;
std::cout << rnums_[i] << std::endl;
}
#else
for (GraphElem i = 1; i < n_; i++) {
rnums_[i] = (rnums_[i-1]*ALCG + BLCG)%MLCG;
}
#endif
GraphWeight mult = 1.0 / (GraphWeight)(1.0 + (GraphWeight)(MLCG-1));
#if defined(PRINT_LCG_DOUBLE_RANDOM_NUMBERS)
for (GraphElem i = 0; i < n_; i++) {
drand_[i] = (GraphWeight)((GraphWeight)fabs(rnums_[i]) * mult ); // 0-1
std::cout << drand_[i] << std::endl;
}
#else
for (GraphElem i = 0; i < n_; i++)
drand_[i] = (GraphWeight)((GraphWeight)fabs(rnums_[i]) * mult); // 0-1
#endif
}
// copy from drand_[idx_start] to new_drand,
// rescale the random numbers between lo and hi
void rescale(GraphWeight* new_drand, GraphElem idx_start, GraphWeight const& lo)
{
GraphWeight range = 1.0;
#if defined(PRINT_LCG_DOUBLE_LOHI_RANDOM_NUMBERS)
for (GraphElem i = idx_start, j = 0; i < n_; i++, j++) {
new_drand[j] = lo + (GraphWeight)(range * drand_[i]);
std::cout << new_drand[j] << std::endl;
}
#else
for (GraphElem i = idx_start, j = 0; i < n_; i++, j++)
new_drand[j] = lo + (GraphWeight)(range * drand_[i]); // lo-hi
#endif
}
private:
unsigned seed_;
GraphElem n_, x0_;
GraphWeight* drand_;
std::vector<GraphElem> rnums_;
};
#else // MPI-based version
class LCG
{
public:
LCG(unsigned seed, GraphWeight* drand,
GraphElem n, MPI_Comm comm = MPI_COMM_WORLD):
seed_(seed), drand_(drand), n_(n)
{
comm_ = comm;
MPI_Comm_size(comm_, &nprocs_);
MPI_Comm_rank(comm_, &rank_);
// allocate long random numbers
rnums_.resize(n_);
// init x0
if (rank_ == 0)
x0_ = reseeder(seed_);
// step #1: bcast x0 from root
MPI_Bcast(&x0_, 1, MPI_GRAPH_TYPE, 0, comm_);
// step #2: parallel prefix to generate first random value per process
parallel_prefix_op();
}
~LCG() { rnums_.clear(); }
// matrix-matrix multiplication for 2x2 matrices
void matmat_2x2(GraphElem c[], GraphElem a[], GraphElem b[])
{
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
GraphElem sum = 0;
for (int k = 0; k < 2; k++) {
sum += a[i*2+k]*b[k*2+j];
}
c[i*2+j] = sum;
}
}
}
// x *= y
void matop_2x2(GraphElem x[], GraphElem y[])
{
GraphElem tmp[4];
matmat_2x2(tmp, x, y);
memcpy(x, tmp, sizeof(GraphElem[4]));
}
// find kth power of a 2x2 matrix
void mat_power(GraphElem mat[], GraphElem k)
{
GraphElem tmp[4];
memcpy(tmp, mat, sizeof(GraphElem[4]));
// mat-mat multiply k times
for (GraphElem p = 0; p < k-1; p++)
matop_2x2(mat, tmp);
}
// parallel prefix for matrix-matrix operation
// `x0 is the very first random number in the series
// `ab is a 2-length array which stores a and b
// `n_ is (n/p)
// `rnums is n_ length array which stores the random nums for a process
void parallel_prefix_op()
{
GraphElem global_op[4];
global_op[0] = ALCG;
global_op[1] = 0;
global_op[2] = BLCG;
global_op[3] = 1;
mat_power(global_op, n_); // M^(n/p)
GraphElem prefix_op[4] = {1,0,0,1}; // I in row-major
GraphElem global_op_recv[4];
int steps = (int)(log2((double)nprocs_));
for (int s = 0; s < steps; s++) {
int mate = rank_^(1 << s); // toggle the sth LSB to find my neighbor
// send/recv global to/from mate
MPI_Sendrecv(global_op, 4, MPI_GRAPH_TYPE, mate, SR_LCG_TAG,
global_op_recv, 4, MPI_GRAPH_TYPE, mate, SR_LCG_TAG,
comm_, MPI_STATUS_IGNORE);
matop_2x2(global_op, global_op_recv);
if (mate < rank_)
matop_2x2(prefix_op, global_op_recv);
MPI_Barrier(comm_);
}
// populate the first random number entry for each process
// (x0*a + b)%P
if (rank_ == 0)
rnums_[0] = x0_;
else
rnums_[0] = (x0_*prefix_op[0] + prefix_op[2])%MLCG;
}
// generate random number based on the first
// random number on a process
// TODO check the 'quick'n dirty generators to
// see if we can avoid the mod
void generate()
{
#if defined(PRINT_LCG_LONG_RANDOM_NUMBERS)
for (int k = 0; k < nprocs_; k++) {
if (k == rank_) {
std::cout << "------------" << std::endl;
std::cout << "Process#" << rank_ << " :" << std::endl;
std::cout << "------------" << std::endl;
std::cout << rnums_[0] << std::endl;
for (GraphElem i = 1; i < n_; i++) {
rnums_[i] = (rnums_[i-1]*ALCG + BLCG)%MLCG;
std::cout << rnums_[i] << std::endl;
}
}
MPI_Barrier(comm_);
}
#else
for (GraphElem i = 1; i < n_; i++) {
rnums_[i] = (rnums_[i-1]*ALCG + BLCG)%MLCG;
}
#endif
GraphWeight mult = 1.0 / (GraphWeight)(1.0 + (GraphWeight)(MLCG-1));
#if defined(PRINT_LCG_DOUBLE_RANDOM_NUMBERS)
for (int k = 0; k < nprocs_; k++) {
if (k == rank_) {
std::cout << "------------" << std::endl;
std::cout << "Process#" << rank_ << " :" << std::endl;
std::cout << "------------" << std::endl;
for (GraphElem i = 0; i < n_; i++) {
drand_[i] = (GraphWeight)((GraphWeight)fabs(rnums_[i]) * mult ); // 0-1
std::cout << drand_[i] << std::endl;
}
}
MPI_Barrier(comm_);
}
#else
for (GraphElem i = 0; i < n_; i++)
drand_[i] = (GraphWeight)((GraphWeight)fabs(rnums_[i]) * mult); // 0-1
#endif
}
// copy from drand_[idx_start] to new_drand,
// rescale the random numbers between lo and hi
void rescale(GraphWeight* new_drand, GraphElem idx_start, GraphWeight const& lo)
{
GraphWeight range = (1.0 / (GraphWeight)nprocs_);
#if defined(PRINT_LCG_DOUBLE_LOHI_RANDOM_NUMBERS)
for (int k = 0; k < nprocs_; k++) {
if (k == rank_) {
std::cout << "------------" << std::endl;
std::cout << "Process#" << rank_ << " :" << std::endl;
std::cout << "------------" << std::endl;
for (GraphElem i = idx_start, j = 0; i < n_; i++, j++) {
new_drand[j] = lo + (GraphWeight)(range * drand_[i]);
std::cout << new_drand[j] << std::endl;
}
}
MPI_Barrier(comm_);
}
#else
for (GraphElem i = idx_start, j = 0; i < n_; i++, j++)
new_drand[j] = lo + (GraphWeight)(range * drand_[i]); // lo-hi
#endif
}
private:
MPI_Comm comm_;
int nprocs_, rank_;
unsigned seed_;
GraphElem n_, x0_;
GraphWeight* drand_;
std::vector<GraphElem> rnums_;
};
#endif
// locks
#ifndef SSTMAC
#ifdef USE_OPENMP_LOCK
#else
#ifdef USE_SPINLOCK
#include <atomic>
std::atomic_flag lkd_ = ATOMIC_FLAG_INIT;
#else
#include <mutex>
std::mutex mtx_;
#endif
void lock() {
#ifdef USE_SPINLOCK
while (lkd_.test_and_set(std::memory_order_acquire)) { ; }
#else
mtx_.lock();
#endif
}
void unlock() {
#ifdef USE_SPINLOCK
lkd_.clear(std::memory_order_release);
#else
mtx_.unlock();
#endif
}
#endif // end of SSTMAC
#endif
#endif // UTILS