-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.hpp
executable file
·273 lines (227 loc) · 8.49 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
#ifndef __UTILS_H
#define __UTILS_H
#include "graph.hpp"
#include <cstring>
#include <random>
#include <utility>
#ifndef RANDOM_MAX_WEIGHT
#define RANDOM_MAX_WEIGHT (1.0)
#endif
#ifndef RANDOM_MIN_WEIGHT
#define RANDOM_MIN_WEIGHT (0.01)
#endif
#ifndef TERMINATION_PHASE_COUNT
#define TERMINATION_PHASE_COUNT (200)
#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)
#define SR_LCG_TAG 108
struct GraphElemTuple {
GraphElem i_, j_;
GraphWeight w_;
GraphElemTuple(GraphElem i, GraphElem j, GraphWeight w):
i_(i), j_(j), w_(w)
{}
GraphElemTuple(GraphElem i, GraphElem j):
i_(i), j_(j), w_(1.0)
{}
GraphElemTuple():
i_(-1), j_(-1), w_(0.0)
{}
// compare
bool operator <(GraphElemTuple const& tp) const
{ return (i_ < tp.i_) || ((!(tp.i_ < i_)) && (j_ < tp.j_)); }
};
typedef enum
{
RND_WEIGHT, // random real weight, between 0-1
ONE_WEIGHT, // weight = 1
ORG_WEIGHT, // use original weights of graph
ABS_WEIGHT // use absolute original weights of graph
} Weight_t;
extern unsigned seed;
double mytimer(void);
// uses a static random engine (seed)
GraphWeight genRandom(GraphWeight low, GraphWeight high);
// sort edge list
void processGraphData(Graph &g, std::vector<GraphElem> &edgeCount,
std::vector<GraphElemTuple> &edgeList,
const GraphElem nv, const GraphElem ne);
// forward declare LCG class for
// distributed random number
// generation
// Parallel Linear Congruential Generator
// x[i] = (a*x[i-1] + b)%M
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(); }
// 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];
}
// 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 // __UTILS_H