Repository navigation
Expand file tree
/
Copy pathtest_no_locks.cpp
More file actions
389 lines (345 loc) · 11 KB
/
Copy pathtest_no_locks.cpp
File metadata and controls
389 lines (345 loc) · 11 KB
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
// test framework
#define NDEBUG
//#define cilk_for for
//#define usleep(a)
#define batch_size 10000
#include <set>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
// #include <pair>
#include "AdjacencyMatrix.cpp"
#include "Graph.hpp"
#include "OFM_no_locks.cpp"
#include <ctime>
#include <iostream>
#include <random>
#include <sys/time.h>
#define MAXVAL 100
#define batch_size_for_test 10
/*
static uint32_t x = 123456789;
static uint32_t y = 362436069;
static uint32_t z = 521288629;
static uint32_t w = 88675123;
uint32_t xor128(void) {
uint32_t t;
t = x ^ (x << 11);
x = y; y = z; z = w;
return w = w ^ (w >> 19) ^ (t ^ (t >> 8));
}
void srand() {
x = 123456789;
y = 362436069;
z = 521288629;
w = 88675123;
}
*/
uint32_t rand_in_range(uint32_t max) { return rand() % max; }
bool compare_matrices(Graph *g1, Graph *g2, int num_nodes) {
for (int i = 0; i < num_nodes; i++) {
for (int j = 0; j < num_nodes; j++) {
if (g1->find_value(i, j) != g2->find_value(i, j)) {
printf("i: %d, j: %d, g1_val: %d, g2_vl: %d\n", i, j,
g1->find_value(i, j), g2->find_value(i, j));
if (j + 1 < num_nodes) {
printf("i: %d, j: %d, g1_val: %d, g2_vl: %d\n", i, j + 1,
g1->find_value(i, j + 1), g2->find_value(i, j + 1));
}
if (j - 1 >= 0) {
printf("i: %d, j: %d, g1_val: %d, g2_vl: %d\n", i, j - 1,
g1->find_value(i, j - 1), g2->find_value(i, j - 1));
}
return false;
}
}
}
return true;
}
bool verify_structure(Graph *g, uint32_t num_nodes, uint32_t num_edges, int trials) {
// printf("########## starting trial %d out of %d, nodes = %u, edges = %u "
// "##########\n",
// j, trials, num_nodes, num_edges);
AdjacencyMatrix matrix = AdjacencyMatrix(num_nodes);
// AdjacencyHashMap adjhash = AdjacencyHashMap(num_nodes);
uint32_t srcs[batch_size_for_test] = {0};
uint32_t dests[batch_size_for_test] = {0};
uint32_t vals[batch_size_for_test] = {0};
for (uint32_t i = 0; i <= num_edges / batch_size_for_test; i++) {
for (uint32_t b = 0; b < batch_size_for_test; b++) {
uint32_t src = rand_in_range(num_nodes);
uint32_t dest = rand_in_range(num_nodes);
while (matrix.find_value(src, dest) != 0) {
src = rand_in_range(num_nodes);
dest = rand_in_range(num_nodes);
}
uint32_t val = rand_in_range(MAXVAL) + 1;
matrix.add_edge(src, dest, val);
srcs[b] = src;
dests[b] = dest;
vals[b] = val;
}
for (uint32_t b = 0; b < batch_size_for_test; b++) {
// printf("about to add edge %d, %d, %d by worker %lu\n",srcs[b],
// dests[b], vals[b], get_worker_num());
g->add_edge(srcs[b], dests[b], vals[b]);
}
}
if (!compare_matrices(&matrix, g, num_nodes)) {
printf("failed in add edges, nodes = %u, edges = %u\n", num_nodes,
num_edges);
if (num_nodes < 20) {
matrix.print_graph();
printf("\n");
g->print_graph();
}
return false;
}
for (uint32_t i = 0; i <= num_edges / batch_size_for_test; i++) {
for (uint32_t b = 0; b < batch_size_for_test; b++) {
uint32_t src = rand_in_range(num_nodes);
uint32_t dest = rand_in_range(num_nodes);
while (matrix.find_value(src, dest) != 0) {
src = rand_in_range(num_nodes);
dest = rand_in_range(num_nodes);
}
uint32_t val = rand_in_range(MAXVAL) + 1;
matrix.add_edge_update(src, dest, val);
srcs[b] = src;
dests[b] = dest;
vals[b] = val;
}
for (uint32_t b = 0; b < batch_size_for_test; b++) {
g->add_edge_update(srcs[b], dests[b], vals[b]);
}
}
if (!compare_matrices(&matrix, g, num_nodes)) {
printf("failed in add edges update, nodes = %u, edges = %u\n",
num_nodes, num_edges);
if (num_nodes < 20) {
matrix.print_graph();
printf("\n");
g->print_graph();
}
return false;
}
vector<uint32_t> test_vector(num_nodes, 0);
for (uint32_t i = 0; i < num_nodes; i++) {
test_vector[i] = rand_in_range(MAXVAL);
}
vector<uint32_t> r1 =
matrix.sparse_matrix_vector_multiplication(test_vector);
vector<uint32_t> r2 =
g->sparse_matrix_vector_multiplication(test_vector);
if (r1 != r2) {
printf("failed spvm, nodes = %u, edges = %u\n", num_nodes, num_edges);
for (auto const &c : r1) {
printf("%d ", c);
}
printf("\n");
for (auto const &c : r2) {
printf("%d ", c);
}
printf("\n");
return false;
}
int start_node = rand_in_range(num_nodes);
vector<int32_t> r3 = matrix.bfs(start_node);
vector<int32_t> r4 = g->bfs(start_node);
if (r3 != r4) {
printf("failed bfs, nodes = %u, edges = %u\n", num_nodes, num_edges);
matrix.print_graph();
printf("\n");
g->print_graph();
for (auto const &c : r3) {
printf("%d ", c);
}
printf("\n");
for (auto const &c : r4) {
printf("%d ", c);
}
printf("\n");
return false;
}
vector<double> test_vector2(num_nodes, 0);
for (uint32_t i = 0; i < num_nodes; i++) {
test_vector2[i] = ((double)rand_in_range(MAXVAL)) / MAXVAL;
}
vector<double> r5 = matrix.pagerank(test_vector2);
vector<double> r6 = g->pagerank(test_vector2);
if (r5 != r6) {
printf("failed pagerank, nodes = %u, edges = %u\n", num_nodes,
num_edges);
matrix.print_graph();
printf("\n");
g->print_graph();
for (auto const &c : test_vector2) {
printf("%f ", c);
}
printf("\n");
for (auto const &c : r5) {
printf("%f ", c);
}
printf("\n");
for (auto const &c : r6) {
printf("%f ", c);
}
printf("\n");
return false;
}
return true;
}
bool verify_pcsr() {
srand(0);
uint32_t node_counts[4] = {5, 10, 30, 100};
uint32_t edge_counts[4] = {5, 20, 100, 1000};
int trials = 1000;
for (int a = 0; a < 4; a++) {
for (int j = 1; j < trials; j++) {
uint32_t num_nodes = node_counts[a];
uint32_t num_edges = edge_counts[a];
OFM *ofm = new OFM(num_nodes);
if(!verify_structure(ofm, num_nodes, num_edges, trials)) {
return false;
}
}
}
return true;
}
static __inline__ uint64_t rdtsc(void)
{
unsigned hi, lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return ( (uint64_t)lo)|( ((uint64_t)hi)<<32 );
}
/******* timing functions ********/
bool time_structure(Graph* g, uint32_t num_nodes, uint32_t num_edges) {
srand(0);
struct timeval start, end;
uint32_t duration;
// add edge update
uint32_t *srcs = (uint32_t *) malloc(sizeof(uint32_t) * num_edges);
uint32_t *dests = (uint32_t *) malloc(sizeof(uint32_t) * num_edges);
uint32_t *vals = (uint32_t *) malloc(sizeof(uint32_t) * num_edges);
for (uint32_t k = 0; k < num_edges; k++) {
srcs[k] = rand_in_range(num_nodes);
dests[k] = rand_in_range(num_nodes);
vals[k] = rand_in_range(MAXVAL) + 1;
}
gettimeofday(&start, NULL);
std::vector<uint64_t> times(0);
for(int i = 0; i < num_edges; i+=1) {
uint64_t before = rdtsc();
g->add_edge(srcs[i], dests[i], vals[i]);
uint64_t after = rdtsc();
times.push_back(after-before);
}
gettimeofday(&end, NULL);
free(srcs);
free(dests);
free(vals);
std::sort(times.begin(), times.end());
duration =
(end.tv_sec - start.tv_sec) * 1000000u + (end.tv_usec - start.tv_usec);
if (num_edges > 0) {
printf("add_edge_update :%f: %lu: %lu: %lu: \n", ((double)duration) / (1000000), times[times.size()*.90], times[times.size()*.99], times[times.size()-1]);
} else {
printf("add_edge_update :N/A: ");
}
vector<uint32_t> test_vector(num_nodes, 0);
for (uint32_t i = 0; i < num_nodes; i++) {
test_vector[i] = rand_in_range(MAXVAL);
}
// spvm
/*
gettimeofday(&start, NULL);
// start = std::clock();
for (int spvm_iters = 0; spvm_iters < 100; spvm_iters++) {
test_vector = g->sparse_matrix_vector_multiplication(test_vector);
}
gettimeofday(&end, NULL);
duration = (end.tv_sec-start.tv_sec)*1000000u+(end.tv_usec-start.tv_usec);
printf("spvm: %f: ", ((double)duration)/1000000);
printf("spvm[0]: %d: ", test_vector[0]);
gettimeofday(&start, NULL);
*/
// bfs
/*
int start_node = rand_in_range(num_nodes);
gettimeofday(&start, NULL);
for (int bfs_iters = 0; bfs_iters < 100; bfs_iters++) {
vector<uint32_t> r4 = g->bfs(start_node);
start_node = r4[0] % num_nodes;
}
gettimeofday(&end, NULL);
duration = (end.tv_sec-start.tv_sec)*1000000u+(end.tv_usec-start.tv_usec);
printf("bfs in %f,", ((double)duration)/1000000);
printf("sn = %d,", start_node);
*/
/*
// pagerank
vector<uint32_t> test_vector2(num_nodes, 0);
for (uint32_t i = 0; i < num_nodes; i++) {
test_vector2[i] = ((uint32_t)rand_in_range(MAXVAL)) / MAXVAL;
}
gettimeofday(&start, NULL);
for (int pr_iters = 0; pr_iters < 100; pr_iters++) {
vector<uint32_t> test_vector2 = g->pagerank(test_vector2);
}
gettimeofday(&end, NULL);
duration = (end.tv_sec-start.tv_sec)*1000000u+(end.tv_usec-start.tv_usec);
printf("pagerank: %f:", ((double)duration)/1000000);
printf("pr[0]: %u\n", test_vector2[0]);
*/
return true;
}
bool time_pcsr(uint32_t num_nodes, uint32_t num_edges, int trials) {
srand(0);
struct timeval start, end;
uint32_t duration;
for (int j = 0; j < trials; j++) {
gettimeofday(&start, NULL);
OFM *ofm = new OFM(num_nodes);
gettimeofday(&end, NULL);
duration =
(end.tv_sec - start.tv_sec) * 1000000u + (end.tv_usec - start.tv_usec);
printf("nodes: %d: edges : %d: ", num_nodes, num_edges);
printf("created structure in :%f: ", ((double)duration) / 1000000);
gettimeofday(&start, NULL);
time_structure(ofm, num_nodes, num_edges);
delete ofm;
}
return true;
}
int main(int argc, char *argv[]) {
if (argc == 2) {
if (string(argv[1]) == "-v") {
if (!verify_pcsr()) {
return 0;
} else {
printf("pcsr verified\n");
}
}
}
uint32_t starting_size = 8;
assert(starting_size >= batch_size);
if (argc==3) {
if(string(argv[1]) == "-s") {
starting_size = atoi(argv[2]);
}
}
int trials = 1;
uint32_t num_nodes = 100000;
uint32_t starting_degree = starting_size;
uint32_t max_degree = 1024+1;
uint32_t num_edges = num_nodes * starting_degree;
int workers = __cilkrts_get_nworkers();
while(num_edges < max_degree * 100000) {
printf("PCSR: workers: %d: ", workers);
time_pcsr(num_nodes, num_edges, trials);
num_edges *= 2;
}
return 0;
}