-
Notifications
You must be signed in to change notification settings - Fork 2
/
CSR.cpp
473 lines (435 loc) · 12.4 KB
/
CSR.cpp
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
/*
* adjacency matrix
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <vector>
#include <cstring>
#include "helpers.h"
#include "Graph.hpp"
class CSR : public Graph {
public:
// data members
uint32_t *nodes;
uint32_t *edges;
uint32_t *values;
SpinLock *locks;
uint32_t N;
uint32_t M;
// function headings
CSR(uint32_t init_n);
~CSR();
uint64_t get_size();
uint64_t get_size_ideal();
uint64_t get_n();
uint32_t find_value(uint32_t src, uint32_t dest);
uint32_t find_value_index(uint32_t src, uint32_t dest);
void print_graph();
void print_arrays();
void add_node();
void add_edge(uint32_t src, uint32_t dest, uint32_t value);
void add_edge_update(uint32_t src, uint32_t dest, uint32_t value);
void convert(Graph* g);
void add_edge_batch_update(uint32_t *srcs, uint32_t *dests, uint32_t *vals, uint32_t edge_count);
void add_file3(string filename);
vector<tuple<uint32_t, uint32_t, uint32_t> > get_edges() {
printf("get_edges not implemented for csr\n");
exit(1);
}
uint32_t num_neighbors(uint32_t node) {
uint32_t start = nodes[node];
uint32_t end;
if (node < N - 1) { end = nodes[node+1]; }
else { end = M; }
return end - start;
}
class iterator {
public:
uint32_t index;
uint32_t *edges;
uint32_t *values;
iterator(CSR *G, uint32_t node, bool start) {
if (!start) {
uint32_t end;
if (node < G->N - 1) { end = G->nodes[node+1]; }
else { end = G->M; }
index = end;
return;
}
index = G->nodes[node];
edges = G->edges;
values = G->values;
return;
}
bool operator==(const iterator& other) const {
return index == other.index;
}
bool operator!=(const iterator& other) const {
return index != other.index;
}
iterator& operator++() {
index+=1;
return *this;
}
edge_t operator*() const {
return {values[index], edges[index]};
}
};
iterator begin(uint32_t node) {
return iterator(this, node, true);
}
iterator end(uint32_t node) {
return iterator(this, node, false);
}
BFS
PAGERANK
SPMV
TRIANGLE_COUNT_SORTED
PARALLEL_BFS
};
// for soc-
// starting at 1
void CSR::add_file3(string filename) {
vector<tuple<uint32_t, uint32_t, uint32_t>> edges_to_add;
ifstream myfile(filename.c_str());
string line;
if (myfile.is_open()) {
while ( getline (myfile,line) ) {
vector<string> elems = split(line, '\t');
int src = atoi(elems[0].c_str())-1;
int dest = atoi(elems[1].c_str())-1;
edges_to_add.push_back(make_tuple( src, dest, 1 ));
// if (line_num++ > 400000000) {
// break;
// }
}
myfile.close();
// return 0;
} else {
printf("file was not opened\n");
}
// populate edges
sort(edges_to_add.begin(), edges_to_add.end());
uint32_t current_node = 0;
for(uint32_t i = 0; i < edges_to_add.size(); i++) {
uint32_t src = get<0>(edges_to_add[i]);
uint32_t dest = get<1>(edges_to_add[i]);
uint32_t val = get<2>(edges_to_add[i]);
while (src >= current_node) {
add_node();
current_node++;
}
edges = (uint32_t *) realloc(edges, (M+1)*sizeof(uint32_t));
values = (uint32_t *) realloc(values, (M+1)*sizeof(uint32_t));
edges[M] = dest;
values[M] = val;
M += 1;
}
}
void CSR::convert(Graph* g) {
free(nodes);
free(edges);
free(values);
nodes = nullptr;
edges = nullptr;
values = nullptr;
vector<tuple<uint32_t, uint32_t, uint32_t> > edges_to_add = g->get_edges();
N = 0;
M = 0;
// populate edges
sort(edges_to_add.begin(), edges_to_add.end());
uint32_t current_node = 0;
for(uint32_t i = 0; i < edges_to_add.size(); i++) {
uint32_t src = get<0>(edges_to_add[i]);
uint32_t dest = get<1>(edges_to_add[i]);
uint32_t val = get<2>(edges_to_add[i]);
while (src >= current_node) {
add_node();
current_node++;
}
edges = (uint32_t *) realloc(edges, (M+1)*sizeof(uint32_t));
values = (uint32_t *) realloc(values, (M+1)*sizeof(uint32_t));
edges[M] = dest;
values[M] = val;
M += 1;
}
}
uint64_t CSR::get_n() {
return N;
}
uint64_t CSR::get_size() {
return (N + M + M * sizeof(uint32_t));
}
void CSR::print_arrays() {
printf("NODES\n");
for(uint32_t i = 0; i < N; i++) {
printf("%d ", nodes[i]);
}
printf("\nEDGES\n");
for(uint32_t i = 0; i < M; i++) {
printf("%d ", edges[i]);
}
printf("\nVALUES\n");
for(uint32_t i = 0; i < M; i++) {
printf("%d ", values[i]);
}
printf("\n");
}
// find value of (src, dest)
uint32_t CSR::find_value(uint32_t src, uint32_t dest) {
uint32_t end = 0;
if (src == N - 1) { end = M; }
else { end = nodes[src + 1]; }
uint32_t start = nodes[src];
if(nodes[src] == end) {
return 0;
}
if(edges[start] == dest) {
return values[start];
}
while (start + 1 < end) {
uint32_t mid = (start + end) / 2;
if (edges[mid] == dest) { return values[mid]; }
else if (edges[mid] > dest) {
end = mid;
}
else {
start = mid;
}
}
// not found
return 0;
}
uint32_t CSR::find_value_index(uint32_t src, uint32_t dest) {
uint32_t end = 0;
if (src == N - 1) { end = M; }
else { end = nodes[src + 1]; }
uint32_t start = nodes[src];
if(nodes[src] == end) {
return UINT32_MAX;
}
if(edges[start] == dest) {
return start;
}
while (start + 1 < end) {
uint32_t mid = (start + end) / 2;
if (edges[mid] == dest) {
return mid;
}
else if (edges[mid] > dest) {
end = mid;
}
else {
start = mid;
}
}
// not found
return UINT32_MAX;
}
void CSR::add_node() {
SpinLock lock;
locks = (SpinLock *) realloc(locks, (N+1)*sizeof(SpinLock));
nodes = (uint32_t *) realloc(nodes, (N+1)*sizeof(uint32_t));
locks[N] = lock;
nodes[N] = M;
N += 1;
}
// src, dest < N
void CSR::add_edge(uint32_t src, uint32_t dest, uint32_t value) {
uint32_t end = 0;
if (src == N - 1) { end = M; }
else { end = nodes[src + 1]; }
uint32_t start = nodes[src];
//printf("adding edge (%u, %u, %u): start = %d, end = %d\n", src, dest, value, start, end);
if(M == 0) {
edges = (uint32_t *) realloc(edges, (M+1)*sizeof(uint32_t));
values = (uint32_t *) realloc(values, (M+1)*sizeof(uint32_t));
edges[M] = dest;
values[M] = value;
M += 1;
for(uint32_t i = src + 1; i < N; i++) {
nodes[i]++;
}
return;
}
if (dest < edges[start] || start == end) {
edges = (uint32_t *) realloc(edges, (M+1)*sizeof(uint32_t));
values = (uint32_t *) realloc(values, (M+1)*sizeof(uint32_t));
memmove(edges + start + 1, edges + start, (M - start)*sizeof(uint32_t));
edges[start] = dest;
memmove(values + start + 1, values + start, (M - start)*sizeof(uint32_t));
values[start] = value;
M += 1;
for(uint32_t i = src + 1; i < N; i++) {
nodes[i]++;
}
return;
} else if (end == 0 || dest > edges[end - 1]) {
edges = (uint32_t *) realloc(edges, (M+1)*sizeof(uint32_t));
values = (uint32_t *) realloc(values, (M+1)*sizeof(uint32_t));
memmove(edges + end + 1, edges + end, (M - end)*sizeof(uint32_t));
edges[end] = dest;
memmove(values + end + 1, values + end, (M - end)*sizeof(uint32_t));
values[end] = value;
M += 1;
for(uint32_t i = src + 1; i < N; i++) {
nodes[i]++;
}
return;
}
// could be optimized by making it a binary search
// but it doesn't change the complexity
else {
for (uint32_t i = start; i < end; i++) {
if (edges[i] > dest) {
edges = (uint32_t *) realloc(edges, (M+1)*sizeof(uint32_t));
values = (uint32_t *) realloc(values, (M+1)*sizeof(uint32_t));
memmove(edges + i + 1, edges + i, (M - i)*sizeof(uint32_t));
edges[i] = dest;
memmove(values + i + 1, values + i, (M - i)*sizeof(uint32_t));
values[i] = value;
M += 1;
break;
} else if (edges[i] == dest) {
values[i] = value;
return;
}
}
for(uint32_t i = src + 1; i < N; i++) {
nodes[i]++;
}
}
}
void CSR::add_edge_update(uint32_t src, uint32_t dest, uint32_t value) {
uint32_t index = find_value_index(src, dest);
if (index < UINT32_MAX) {
values[index] = value;
return;
}
uint32_t end = 0;
if (src == N - 1) { end = M; }
else { end = nodes[src + 1]; }
uint32_t start = nodes[src];
//printf("start = %d, end = %d\n", start, end);
if(M == 0) {
edges = (uint32_t *) realloc(edges, (M+1)*sizeof(uint32_t));
values = (uint32_t *) realloc(values, (M+1)*sizeof(uint32_t));
edges[M] = dest;
values[M] = value;
M += 1;
for(uint32_t i = src + 1; i < N; i++) {
nodes[i]++;
}
return;
}
if (dest < edges[start] || start == end) {
edges = (uint32_t *) realloc(edges, (M+1)*sizeof(uint32_t));
values = (uint32_t *) realloc(values, (M+1)*sizeof(uint32_t));
memmove(edges + start + 1, edges + start, (M - start)*sizeof(uint32_t));
edges[start] = dest;
memmove(values + start + 1, values + start, (M - start)*sizeof(uint32_t));
values[start] = value;
M += 1;
for(uint32_t i = src + 1; i < N; i++) {
nodes[i]++;
}
return;
} else if (end == 0 || dest > edges[end - 1]) {
edges = (uint32_t *) realloc(edges, (M+1)*sizeof(uint32_t));
values = (uint32_t *) realloc(values, (M+1)*sizeof(uint32_t));
memmove(edges + end + 1, edges + end, (M - end)*sizeof(uint32_t));
edges[end] = dest;
memmove(values + end + 1, values + end, (M - end)*sizeof(uint32_t));
values[end] = value;
M += 1;
for(uint32_t i = src + 1; i < N; i++) {
nodes[i]++;
}
return;
}
// could be optimized by making it a binary search
// but it doesn't change the complexity
else {
for (uint32_t i = start; i < end; i++) {
if (edges[i] > dest) {
edges = (uint32_t *) realloc(edges, (M+1)*sizeof(uint32_t));
values = (uint32_t *) realloc(values, (M+1)*sizeof(uint32_t));
memmove(edges + i + 1, edges + i, (M - i)*sizeof(uint32_t));
edges[i] = dest;
memmove(values + i + 1, values + i, (M - i)*sizeof(uint32_t));
values[i] = value;
M += 1;
break;
} else if (edges[i] == dest) {
values[i] = value;
return;
}
}
for(uint32_t i = src + 1; i < N; i++) {
nodes[i]++;
}
}
}
void CSR::add_edge_batch_update(uint32_t *srcs, uint32_t *dests, uint32_t *vals, uint32_t edge_count) {
for(int i = 0; i < edge_count; i++) {
add_edge_update(srcs[i], dests[i], vals[i]);
}
}
void CSR::print_graph() {
for(uint32_t i = 0; i < N; i++) {
vector<uint32_t> edgelist(N, 0);
uint32_t end = 0;
if (i == N - 1) { end = M; }
else { end = nodes[i + 1]; }
uint32_t start = nodes[i];
for(uint32_t j = start; j < end; j++) {
edgelist[edges[j]] = values[j];
}
for(uint32_t j = 0; j < N; j++) {
printf("%03d ", edgelist[j]);
}
printf("\n");
}
}
// constructor
CSR::CSR(uint32_t init_n) {
edges = nullptr;
values = nullptr;
nodes = nullptr;
locks = nullptr;
N = 0;
M = 0;
for(uint32_t i = 0; i < init_n; i++) {
add_node();
}
}
CSR::~CSR() {
free(edges);
free(values);
free(nodes);
free(locks);
}
/*
int main() {
// graph_t g;
// setup(&g);
CSR g = CSR(5);
while (1) {
uint32_t src, dest, value;
scanf("%d %d %d", &src, &dest, &value);
// printf("src:%d, dst: %d, val:%d\n", src, dest, value);
g.add_edge(src, dest, value);
g.print_graph();
std::vector<uint32_t> temp;
temp.push_back(10);
temp.push_back(1);
temp.push_back(0);
temp.push_back(0);
temp.push_back(10);
std::vector<uint32_t> res = g.sparse_matrix_vector_multiplication(temp);
for(int i = 0; i < 5; i++) { printf("%d ", res[i]); }
printf("\n");
}
}
*/