-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwcc.c
297 lines (235 loc) · 6.67 KB
/
wcc.c
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
#include "random.h"
#include "barrier.h"
#define wcc_ROOT_NODE 0
static void print_stats(void);
static struct thread_stats {
uint64_t tasks, updates;
} thread_stats[64];
static inline void wcc_algo(int id, struct thread_buffer *b, struct node *n);
uint32_t * parents;
long* components;
int phase = 0;
static pthread_t threads[ALGO_NB_THREADS];
struct x_barrier* xsync;
/*
* Actual wcc algorithm
*/
inline int writeMin(long* curr, long newV) {
volatile long c; int r =0;
do c = *curr;
while (c > newV && !(r = __sync_bool_compare_and_swap(curr,c,newV)) );
return r;
}
static inline void wcc_algo(int _id, struct thread_buffer *b, struct node *n) {
struct node *dst;
uint32_t n_id = id(n);;
for(uint32_t idx = 0; idx< n->nb_out_edges; idx++) {
uint32_t dst_id = edge_array_out[n->outgoing_edges + idx].dst;
if(writeMin(&components[dst_id], components[n_id]) ){
if(__sync_bool_compare_and_swap(&in_frontier_next[dst_id], 0, 1))
thread_add_task(b, &nodes[dst_id]);
}
}
}
void *wcc_parallels(void *data) {
int id = (long)data;
__id = id + NB_CONCURRENCY;
if(id != 0) {
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(id, &mask);
sched_setaffinity(gettid(), sizeof(mask), &mask);
}
begin:;
if(!has_work_to_do())
usleep(10000);
__sync_fetch_and_add(&waiting, 1);
while(*(volatile int*)¶llels_done == 0) NOP10();
__sync_fetch_and_add(&waiting, -1);
uint32_t start,end;
start = NB_NODES / ALGO_NB_THREADS * id;
end = start + NB_NODES/ALGO_NB_THREADS;
if ( id == ALGO_NB_THREADS - 1) end = NB_NODES;
for(;start < end; start++)
in_frontier_next[start] = 0;
wait_b(xsync);
struct thread_buffer b;
struct node *n;
init_thread_buffer(&b);
do {
struct work w = get_work(id);
foreach_task(w, ids, n) {
wcc_algo(id, &b, n);
}
} while(sub_has_more_work());
thread_flush(&b);
__sync_fetch_and_add(¶llels_done, -1);
while(*(volatile int*)¶llels_done != 0) NOP10();
if(id != 0)
goto begin;
return NULL;
}
void wcc_construct(void) {
xsync = (struct x_barrier*) malloc(sizeof(struct x_barrier));
init_barrier(xsync, ALGO_NB_THREADS);
components = (long*) malloc( NB_NODES * sizeof(long));
if(load_mode == 2 || load_mode == 3 || load_mode == 4)
for(size_t i = 1; i < ALGO_NB_THREADS; i++)
pthread_create(&threads[i], NULL, wcc_parallels, (void*)i);
}
void wcc_destruct(void) {
free(components);
free(xsync);
if(load_mode == 2 || load_mode == 3 || load_mode == 4)
for(size_t i = 1; i < ALGO_NB_THREADS; i++)
pthread_cancel(threads[i]);
}
static used void iterator(struct node *nodes, algo_fun_t algo) {
int iterations = 0;
{
do { // iterations
start_iteration();
while(*(volatile int*)&waiting != ALGO_NB_THREADS - 1); // wait for all threads
parallels_done = ALGO_NB_THREADS; // go!
wcc_parallels((void*)0);
printf("Iter %d work %u\n", iterations, has_work_to_do());
stop_iteration();
iterations++;
} while(has_work_to_do());
}
}
void wcc_reset(struct node *nodes) {
print_stats();
memset(thread_stats, 0, sizeof(thread_stats));
for(size_t i = 0; i < NB_NODES; i++) {
components[i] = i;
in_frontier[i] = 0;
}
}
void validate()
{
uint32_t max_comp = 0;
uint32_t* diff_comp = (uint32_t*) malloc(NB_NODES * sizeof(uint32_t));
parallel_for(uint32_t i = 0; i < NB_NODES; i++ ) {
diff_comp[i] = 0;
}
if(load_mode == 2 || load_mode ==3) {
for(int i = 0; i < NB_NODES;i++) {
for(uint32_t idx = 0; idx < nodes[i].nb_out_edges;idx++) {
struct edge* e = &edge_array_out[nodes[i].outgoing_edges + idx];
if(components[i] != components[e->dst] ) {
printf ("ERROR %d in %d and %d in %d \n", i, components[i], e->dst, components[e->dst]);
exit(1);
}
}
}
}
else {
for(size_t i =0; i < nb_edges; i++) {
struct edge_t* e = &memblock[i];
if(components[e->src] != components[e->dst] ) {
printf ("ERROR %d in %d and %d in %d \n", e->src, components[e->src], e->dst, components[e->dst]);
exit(1);
}
}
}
uint32_t total_comp = 0;
for(uint32_t i =0 ;i < NB_NODES;i++) {
diff_comp[components[i]]++;
}
for(uint32_t i =0 ;i < NB_NODES;i++) {
if(diff_comp[i] > max_comp) max_comp = diff_comp[i];
if(diff_comp[i] !=0) total_comp++;
}
printf("Total comp %d , max comp %d \n", total_comp, max_comp);
free(diff_comp);
}
/*
* Default function that launches a wcc from all nodes
*/
void wcc(struct node *nodes) {
uint32_t changes = NB_NODES;
uint64_t algo_stop, algo_start;
int iterations = 0;
rdtscll(algo_start);
switch(load_mode){
case 0: //grid
for(int i = 0; i < NB_NODES; i++)
in_frontier_next[i] = 0;
while(changes) {
printf("Iter %d , active %d\n", iterations++, changes);
changes = 0;
for(uint32_t i = 0; i < P; i++) {
parallel_for(uint32_t j =0; j < P; j++) {
uint32_t start = row_offsets[i] + offsets[i][j];
uint32_t stop = j == P -1 ? (i == P -1 ? nb_edges:row_offsets[i+1]) : row_offsets[i] + offsets[i][j+1];
for(;start < stop; start++ ) {
struct edge_t* e = &memblock[start];
uint32_t src = e->src;
uint32_t dst = e->dst;
if(writeMin(&components[dst], components[src]) ) {
in_frontier_next[dst] = 1;
}
if(writeMin(&components[src], components[dst])) {
in_frontier_next[src] = 1;
}
}
}
}
for(size_t n= 0; n < NB_NODES; n++) {
if(in_frontier_next[n] == 1)
changes +=1 ;
in_frontier_next[n] = 0;
}
}
break;
case 8://edge array
while(changes > 0 ) {
printf("Iter %d , active %d\n", iterations, changes);
changes = 0;
parallel_for(size_t i =0 ; i < nb_edges; i++) {
struct edge_t* e = &memblock[i];
uint32_t src = e->src;
uint32_t dst = e->dst;
if(writeMin(&components[dst], components[src]) ) {
in_frontier_next[dst] = 1;
}
if(writeMin(&components[src], components[dst])) {
in_frontier_next[src] = 1;
}
}
for(size_t i = 0; i < NB_NODES; i++) {
if(in_frontier_next[i] == 1)
changes +=1 ;
in_frontier_next[i] = 0;
}
iterations++;
}
break;
case 2:
case 3:
case 4:
if(!get_task_list())
init_task_list(NB_NODES);
reset_task_lists();
for(size_t i = 0; i < NB_NODES; i++) {
add_task(&nodes[i]);
in_frontier_next[i] = 1;
}
stop_iteration();
iterator(nodes, wcc_algo);
break;
default:
printf("Don't know how to run with this layout\n");
exit(1);
}
rdtscll(algo_stop);
printf("Algo time %f \n", (float)(algo_stop - algo_start)/(float)get_cpu_freq());
validate();
}
static void print_stats(void) {
/* Stats */
}
struct algo_func current_algo = {
.reset = wcc_reset, .main = wcc, .construct = wcc_construct, .destruct = wcc_destruct,
};