-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrepart.cpp
272 lines (242 loc) · 9.27 KB
/
repart.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
int repart(dist_graph_t* g, int32_t* local_parts) {
MPI_Barrier(MPI_COMM_WORLD);
double elt = timer();
int32_t* sendcounts = (int32_t*)malloc(nprocs * sizeof(int32_t));
assert(sendcounts != NULL);
int32_t* recvcounts = (int32_t*)malloc(nprocs * sizeof(int32_t));
assert(recvcounts != NULL);
for (uint32_t i = 0; i < nprocs; ++i) {
sendcounts[i] = 0;
recvcounts[i] = 0;
}
for (uint32_t i = 0; i < g->n_local; ++i) {
int32_t rank = local_parts[i];
++sendcounts[rank];
}
MPI_Alltoall(sendcounts, 1, MPI_INT32_T, recvcounts, 1, MPI_INT32_T,
MPI_COMM_WORLD);
int32_t* sdispls = (int32_t*)malloc(nprocs * sizeof(int32_t));
assert(sdispls != NULL);
int32_t* sdispls_cpy = (int32_t*)malloc(nprocs * sizeof(int32_t));
assert(sdispls_cpy != NULL);
int32_t* rdispls = (int32_t*)malloc(nprocs * sizeof(int32_t));
assert(rdispls != NULL);
sdispls[0] = 0;
sdispls_cpy[0] = 0;
rdispls[0] = 0;
for (uint32_t i = 0; i < nprocs - 1; ++i) {
sdispls[i + 1] = sdispls[i] + sendcounts[i];
sdispls_cpy[i + 1] = sdispls[i + 1];
rdispls[i + 1] = rdispls[i] + recvcounts[i];
}
int32_t total_send_deg = sdispls[nprocs - 1] + sendcounts[nprocs - 1];
int32_t total_recv_deg = rdispls[nprocs - 1] + recvcounts[nprocs - 1];
printf("%d totals %d %d %li\n", procid, total_send_deg, total_recv_deg,
g->n_local);
assert(total_send_deg == g->n_local);
uint32_t* sendbuf_vids;
uint32_t* sendbuf_deg_out;
uint32_t* sendbuf_deg_in;
uint32_t* recvbuf_vids;
uint32_t* recvbuf_deg_out;
uint32_t* recvbuf_deg_in;
sendbuf_vids = (uint32_t*)malloc(total_send_deg * sizeof(uint32_t));
assert(sendbuf_vids != NULL);
sendbuf_deg_out = (uint32_t*)malloc(total_send_deg * sizeof(uint32_t));
assert(sendbuf_deg_out != NULL);
sendbuf_deg_in = (uint32_t*)malloc(total_send_deg * sizeof(uint32_t));
assert(sendbuf_deg_in != NULL);
recvbuf_vids = (uint32_t*)malloc(total_recv_deg * sizeof(uint32_t));
assert(recvbuf_vids != NULL);
recvbuf_deg_out = (uint32_t*)malloc(total_recv_deg * sizeof(uint32_t));
assert(recvbuf_deg_out != NULL);
recvbuf_deg_in = (uint32_t*)malloc(total_recv_deg * sizeof(uint32_t));
assert(recvbuf_deg_in != NULL);
for (uint32_t i = 0; i < g->n_local; ++i) {
uint32_t rank = local_parts[i];
uint32_t snd_index = sdispls_cpy[rank]++;
sendbuf_vids[snd_index] = g->local_unmap[i];
sendbuf_deg_out[snd_index] = (uint32_t)out_degree(g, i);
sendbuf_deg_in[snd_index] = (uint32_t)in_degree(g, i);
}
MPI_Alltoallv(sendbuf_vids, sendcounts, sdispls, MPI_UINT32_T, recvbuf_vids,
recvcounts, rdispls, MPI_UINT32_T, MPI_COMM_WORLD);
MPI_Alltoallv(sendbuf_deg_out, sendcounts, sdispls, MPI_UINT32_T,
recvbuf_deg_out, recvcounts, rdispls, MPI_UINT32_T,
MPI_COMM_WORLD);
MPI_Alltoallv(sendbuf_deg_in, sendcounts, sdispls, MPI_UINT32_T,
recvbuf_deg_in, recvcounts, rdispls, MPI_UINT32_T,
MPI_COMM_WORLD);
free(sendbuf_vids);
free(sendbuf_deg_out);
free(sendbuf_deg_in);
for (uint32_t i = 0; i < nprocs; ++i) {
sendcounts[i] = 0;
recvcounts[i] = 0;
}
for (uint32_t i = 0; i < g->n_local; ++i) {
int32_t rank = local_parts[i];
sendcounts[rank] += (int32_t)out_degree(g, i);
}
MPI_Alltoall(sendcounts, 1, MPI_INT32_T, recvcounts, 1, MPI_INT32_T,
MPI_COMM_WORLD);
sdispls[0] = 0;
sdispls_cpy[0] = 0;
rdispls[0] = 0;
for (uint32_t i = 0; i < nprocs - 1; ++i) {
sdispls[i + 1] = sdispls[i] + sendcounts[i];
sdispls_cpy[i + 1] = sdispls[i + 1];
rdispls[i + 1] = rdispls[i] + recvcounts[i];
}
uint32_t total_send_out = sdispls[nprocs - 1] + sendcounts[nprocs - 1];
uint32_t total_recv_out = rdispls[nprocs - 1] + recvcounts[nprocs - 1];
assert(total_send_out == g->m_local_out);
uint32_t* sendbuf_e_out;
uint32_t* recvbuf_e_out;
sendbuf_e_out = (uint32_t*)malloc(total_send_out * sizeof(uint32_t));
assert(sendbuf_e_out != NULL);
recvbuf_e_out = (uint32_t*)malloc(total_recv_out * sizeof(uint32_t));
assert(recvbuf_e_out != NULL);
uint32_t counter = 0;
for (uint32_t i = 0; i < g->n_local; ++i) {
uint32_t out_degree = out_degree(g, i);
uint32_t* outs = out_vertices(g, i);
uint32_t rank = local_parts[i];
uint32_t snd_index = sdispls_cpy[rank];
sdispls_cpy[rank] += out_degree;
for (uint32_t j = 0; j < out_degree; ++j) {
assert(outs[j] < g->n_total);
uint32_t out;
if (outs[j] < g->n_local)
out = g->local_unmap[outs[j]];
else
out = g->ghost_unmap[outs[j] - g->n_local];
sendbuf_e_out[snd_index++] = out;
counter++;
assert(out < g->n);
}
}
assert(counter == g->m_local_out);
MPI_Alltoallv(sendbuf_e_out, sendcounts, sdispls, MPI_UINT32_T, recvbuf_e_out,
recvcounts, rdispls, MPI_UINT32_T, MPI_COMM_WORLD);
free(sendbuf_e_out);
free(g->out_edges);
free(g->out_degree_list);
g->out_edges = recvbuf_e_out;
g->m_local_out = (int64_t)total_recv_out;
g->out_degree_list =
(uint32_t*)malloc((total_recv_deg + 1) * sizeof(uint32_t));
g->out_degree_list[0] = 0;
for (uint32_t i = 0; i < total_recv_deg; ++i)
g->out_degree_list[i + 1] = g->out_degree_list[i] + recvbuf_deg_out[i];
printf("BLAH %d %u %u\n", procid, g->out_degree_list[total_recv_deg],
g->m_local_out);
assert(g->out_degree_list[total_recv_deg] == g->m_local_out);
free(recvbuf_deg_out);
for (uint32_t i = 0; i < nprocs; ++i) sendcounts[i] = 0;
for (uint32_t i = 0; i < g->n_local; ++i) {
int32_t rank = (uint32_t)local_parts[i];
sendcounts[rank] += (int32_t)in_degree(g, i);
}
MPI_Alltoall(sendcounts, 1, MPI_INT32_T, recvcounts, 1, MPI_INT32_T,
MPI_COMM_WORLD);
sdispls[0] = 0;
sdispls_cpy[0] = 0;
rdispls[0] = 0;
for (uint32_t i = 0; i < nprocs - 1; ++i) {
sdispls[i + 1] = sdispls[i] + sendcounts[i];
sdispls_cpy[i + 1] = sdispls[i + 1];
rdispls[i + 1] = rdispls[i] + recvcounts[i];
}
uint32_t total_send_in = sdispls[nprocs - 1] + sendcounts[nprocs - 1];
uint32_t total_recv_in = rdispls[nprocs - 1] + recvcounts[nprocs - 1];
assert(total_send_in == g->m_local_in);
uint32_t* sendbuf_e_in;
uint32_t* recvbuf_e_in;
sendbuf_e_in = (uint32_t*)malloc(total_send_in * sizeof(uint32_t));
assert(sendbuf_e_in != NULL);
recvbuf_e_in = (uint32_t*)malloc(total_recv_in * sizeof(uint32_t));
assert(recvbuf_e_in != NULL);
counter = 0;
for (uint32_t i = 0; i < g->n_local; ++i) {
uint32_t in_degree = in_degree(g, i);
uint32_t* ins = in_vertices(g, i);
uint32_t rank = local_parts[i];
uint32_t snd_index = sdispls_cpy[rank];
sdispls_cpy[rank] += in_degree;
for (uint32_t j = 0; j < in_degree; ++j) {
assert(ins[j] < g->n_total);
uint32_t in;
if (ins[j] < g->n_local)
in = g->local_unmap[ins[j]];
else
in = g->ghost_unmap[ins[j] - g->n_local];
sendbuf_e_in[snd_index++] = in;
counter++;
assert(in < g->n);
}
}
assert(counter == g->m_local_in);
MPI_Alltoallv(sendbuf_e_in, sendcounts, sdispls, MPI_UINT32_T, recvbuf_e_in,
recvcounts, rdispls, MPI_UINT32_T, MPI_COMM_WORLD);
free(sendbuf_e_in);
free(g->in_edges);
free(g->in_degree_list);
g->in_edges = recvbuf_e_in;
g->m_local_in = (int64_t)total_recv_in;
g->in_degree_list =
(uint32_t*)malloc((total_recv_deg + 1) * sizeof(uint32_t));
g->in_degree_list[0] = 0;
for (uint32_t i = 0; i < total_recv_deg; ++i)
g->in_degree_list[i + 1] = g->in_degree_list[i] + recvbuf_deg_in[i];
assert(g->in_degree_list[total_recv_deg] == g->m_local_in);
free(recvbuf_deg_in);
free(g->local_unmap);
g->local_unmap = (uint32_t*)malloc(total_recv_deg * sizeof(uint32_t));
for (uint32_t i = 0; i < total_recv_deg; ++i)
g->local_unmap[i] = recvbuf_vids[i];
free(recvbuf_vids);
g->n_local = total_recv_deg;
elt = timer() - elt;
printf("%d done repart %li %li %li, %9.6lf (s)\n", procid, g->n_local,
g->m_local_in, g->m_local_out, elt);
}
int get_ghost_tasks(dist_graph_t* g) {
double elt = timer();
int64_t n_local_max = g->n_local;
int32_t cur_size;
MPI_Allreduce(MPI_IN_PLACE, &n_local_max, 1, MPI_INT64_T, MPI_MAX,
MPI_COMM_WORLD);
uint32_t* buf = (uint32_t*)malloc(n_local_max * sizeof(uint32_t));
uint32_t* tmp_buf;
for (uint32_t p = 0; p < nprocs; ++p) {
if (p == procid) {
printf("%d my time to shine\n", procid);
tmp_buf = g->local_unmap;
cur_size = (int32_t)g->n_local;
} else
tmp_buf = buf;
MPI_Bcast(&cur_size, 1, MPI_INT32_T, p, MPI_COMM_WORLD);
// MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(tmp_buf, cur_size, MPI_UINT32_T, p, MPI_COMM_WORLD);
// MPI_Barrier(MPI_COMM_WORLD);
if (p != procid) {
// printf("%d putting this shit to biz\n", procid);
#pragma omp parallel for
for (uint32_t i = 0; i < (uint32_t)cur_size; ++i) {
#if NO_HASH
uint32_t val = g->mapper[buf[i]];
#else
uint32_t val = get_value(&g->map, buf[i]);
#endif
if (val != NULL_KEY) g->ghost_tasks[val - g->n_local] = p;
}
}
}
for (uint32_t i = 0; i < (uint32_t)g->n_ghost; ++i)
if (g->ghost_tasks[i] >= nprocs)
printf("EROR %d -- %u, %u %u\n", procid, i, g->ghost_unmap[i],
g->ghost_tasks[i]);
elt = timer() - elt;
printf("%d done getting ghost tasks %9.6lf (s)\n", procid, elt);
}