-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcentrality.c
227 lines (191 loc) · 5.09 KB
/
centrality.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
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "defs.h"
#include "globals.h"
#ifdef __MTA__
#include <sys/mta_task.h>
#include <machine/runtime.h>
#else
#include "compat/xmt-ops.h"
#endif
#include "stinger-atomics.h"
/* Set this variable to 1 to time each iteration */
#define TIMEBCPART 0
double centrality(graph *G, double *BC, int64_t Vs)
{
int64_t num_srcs = 0;
double timeBC, timep1, timep2, timer();
int64_t NE = G->numEdges;
int64_t NV = G->numVertices;
int64_t *eV = G->endVertex;
int64_t *start = G->edgeStart;
/* Allocate memory for data structures */
int64_t *Q = (int64_t *) xmalloc(NV * sizeof(int64_t));
int64_t *dist = (int64_t *) xmalloc(NV * sizeof(double));
int64_t *sigma = (int64_t *) xmalloc(NV * sizeof(int64_t));
int64_t *marks = (int64_t *) xmalloc((NV + 2) * sizeof(int64_t));
int64_t *QHead = (int64_t *) xmalloc(100 * SCALE * sizeof(int64_t));
int64_t *child = (int64_t *) xmalloc(NE * sizeof(int64_t));
int64_t *child_count = (int64_t *) xmalloc(NV * sizeof(int64_t));
int64_t *explored = (int64_t *) xmalloc(sizeof(int64_t)*NV);
int64_t j, k;
int64_t nQ, Qnext, Qstart, Qend;
/* Reuse the dist memory in the accumulation phase */
double *delta = (double *) dist;
OMP("omp parallel for")
for (j = 0; j < NV; j++) {
BC[j] = 0.0;
explored[j] = 0;
}
timeBC = timer();
int64_t x;
/* Use |Vs| nodes to compute centrality values */
for (x = 0; (x < NV) && (Vs > 0); x ++) {
int64_t d_phase;
int64_t s = rand() % NV;
while(explored[s])
{
s = rand() % NV;
}
explored[s] = 1;
if (start[s+1] == start[s]) {
continue;
} else {
Vs --;
}
num_srcs++;
#if TIMEBCPART
timep1 = timer();
timep2 = timer();
#endif
OMP("omp parallel for")
MTA("mta assert nodep")
for (j = 0; j < NV; j++)
{
dist[j] = -1;
sigma[j] = marks[j] = child_count[j] = 0;
}
#if TIMEBCPART
timep1 = timer() - timep1;
fprintf(stderr, "Src: %d, initialization time: %9.6lf sec.\n", s, timep1);
timep1 = timer();
#endif
/* Push node i onto Q and set bounds for first Q sublist */
Q[0] = s;
Qnext = 1;
nQ = 1;
QHead[0] = 0;
QHead[1] = 1;
dist[s] = 0;
marks[s] = 1;
sigma[s] = 1;
PushOnStack: /* Push nodes onto Q */
/* Execute the nested loop for each node v on the Q AND
for each neighbor w of v whose edge weight is not divisible by 8
*/
d_phase = nQ;
Qstart = QHead[nQ-1];
Qend = QHead[nQ];
MTA("mta assert no dependence")
MTA("mta block dynamic schedule")
for (j = Qstart; j < Qend; j++)
{
int64_t v = Q[j];
int64_t sigmav = sigma[v];
int64_t myStart = start[v];
int64_t myEnd = start[v+1];
int64_t ccount = 0;
for (k = myStart; k < myEnd; k++)
{
int64_t d, w, l;
w = eV[k];
d = dist[w];
/* If node has not been visited, set distance and push on Q (but only once) */
if (d < 0) {
if (stinger_int_fetch_add(&marks[w], 1) == 0)
{
dist[w] = d_phase;
Q[stinger_int_fetch_add(&Qnext, 1)] = w;
}
stinger_int_fetch_add(&sigma[w], sigmav);
l = myStart + ccount++;
child[l] = w;
}
else if (d == d_phase)
{
stinger_int_fetch_add(&sigma[w], sigmav);
l = myStart + ccount++;
child[l] = w;
}
}
child_count[v] = ccount;
}
/* If new nodes pushed onto Q */
if (Qnext != QHead[nQ])
{
nQ++;
QHead[nQ] = Qnext;
goto PushOnStack;
}
#if TIMEBCPART
timep1 = timer() - timep1;
fprintf(stderr, "Traversal time: %9.6lf sec, visited: %d\n", timep1, QHead[nQ]);
timep1 = timer();
#endif
#if 0
/* Code snippet to count the size of child multiset */
int64_t sum = 0;
for (j = 0; j < NV; j++) {
sum = sum + child_count[j];
}
avg_frac += (double)sum/NE;
fprintf(stderr, "child count: %d, fraction: %lf, visited: %d\n", sum,
(double) sum/NE, QHead[nQ]);
#endif
/* Dependence accumulation phase */
nQ--;
OMP("omp parallel for")
for (j=0; j < NV; j++) delta[j] = 0.0;
/* Pop nodes off of Q in the reverse order they were pushed on */
for ( ; nQ > 1; nQ --)
{
Qstart = QHead[nQ-1];
Qend = QHead[nQ];
/* For each v in the sublist AND for each w on v's list */
MTA("mta assert parallel")
MTA("mta block dynamic schedule")
MTA("mta assert no alias *sigma *Q *BC *delta *child *start *QHead")
for (j = Qstart; j < Qend; j++) {
int64_t v = Q[j];
int64_t myStart = start[v];
int64_t myEnd = myStart + child_count[v];
double sum = 0;
double sigma_v = (double) sigma[v];
for (k = myStart; k < myEnd; k++) {
int64_t w = child[k];
sum += sigma_v * (1.0 + delta[w]) / (double) sigma[w];
}
delta[v] = sum;
MTA("mta update")
BC[v] += sum;
}
}
#if TIMEBCPART
timep1 = timer() - timep1;
fprintf(stderr, "Accumulation time: %9.6lf sec.\n", timep1);
timep2 = timer() - timep2;
fprintf(stderr, "Src: %d, total time: %9.6lf sec.\n", s, timep2);
#endif
}
timeBC = timer() - timeBC;
free(Q);
free(dist);
free(sigma);
free(QHead);
free(marks);
free(child);
free(child_count);
return timeBC;
}