-
Notifications
You must be signed in to change notification settings - Fork 2
/
comm_parent.c
413 lines (381 loc) · 15.5 KB
/
comm_parent.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
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
// ************************************************************************
//
// miniAMR: stencil computations with boundary exchange and AMR.
//
// Copyright (2014) Sandia Corporation. Under the terms of Contract
// DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
// retains certain rights in this software.
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// Questions? Contact Courtenay T. Vaughan ([email protected])
// Richard F. Barrett ([email protected])
//
// ************************************************************************
#include <stdlib.h>
#include <mpi.h>
#include "block.h"
#include "comm.h"
#include "proto.h"
// These routines are concerned with communicating information between
// parents and their children. This includes information about refinement
// level and also includes a routine to keep track to where children are
// being moved to. For on node children, the parent has the index of the
// child block and for off node children has the block number.
void comm_parent(void)
{
int i, j, b, which, type, offset;
int *send_int = (int *) send_buff;
int *recv_int = (int *) recv_buff;
parent *pp;
MPI_Status status;
type = 20;
for (i = 0; i < par_p.num_comm_part; i++)
MPI_Irecv(&recv_int[par_p.index[i]], par_p.comm_num[i], MPI_INTEGER,
par_p.comm_part[i], type, MPI_COMM_WORLD, &request[i]);
for (i = 0; i < par_b.num_comm_part; i++) {
if (nonblocking)
offset = par_b.index[i];
else
offset = 0;
for (j = 0; j < par_b.comm_num[i]; j++)
if (par_b.comm_b[par_b.index[i]+j] < 0)
// parent, so send 0 (its parent can not refine)
send_int[offset+j] = 0;
else
send_int[offset+j] = blocks[par_b.comm_b[par_b.index[i]+j]].refine;
if (nonblocking)
MPI_Isend(&send_int[par_b.index[i]], par_b.comm_num[i], MPI_INTEGER,
par_b.comm_part[i], type, MPI_COMM_WORLD, &s_req[i]);
else
MPI_Send(&send_int[0], par_b.comm_num[i], MPI_INTEGER,
par_b.comm_part[i], type, MPI_COMM_WORLD);
}
for (i = 0; i < par_p.num_comm_part; i++) {
MPI_Waitany(par_p.num_comm_part, request, &which, &status);
for (j = 0; j < par_p.comm_num[which]; j++)
if (recv_int[par_p.index[which]+j] > -1) {
pp = &parents[par_p.comm_p[par_p.index[which]+j]];
pp->refine = 0;
for (b = 0; b < 8; b++)
if (pp->child_node[b] == my_pe && pp->child[b] >= 0 &&
blocks[pp->child[b]].refine == -1)
blocks[pp->child[b]].refine = 0;
}
}
if (nonblocking)
for (i = 0; i < par_b.num_comm_part; i++)
MPI_Waitany(par_b.num_comm_part, s_req, &which, &status);
}
void comm_parent_reverse(void)
{
int i, j, which, type, offset;
int *send_int = (int *) send_buff;
int *recv_int = (int *) recv_buff;
MPI_Status status;
type = 21;
for (i = 0; i < par_b.num_comm_part; i++)
MPI_Irecv(&recv_int[par_b.index[i]], par_b.comm_num[i], MPI_INTEGER,
par_b.comm_part[i], type, MPI_COMM_WORLD, &request[i]);
for (i = 0; i < par_p.num_comm_part; i++) {
if (nonblocking)
offset = par_p.index[i];
else
offset = 0;
for (j = 0; j < par_p.comm_num[i]; j++)
send_int[offset+j] = parents[par_p.comm_p[par_p.index[i]+j]].refine;
if (nonblocking)
MPI_Isend(&send_int[par_p.index[i]], par_p.comm_num[i], MPI_INTEGER,
par_p.comm_part[i], type, MPI_COMM_WORLD, &s_req[i]);
else
MPI_Send(&send_int[0], par_p.comm_num[i], MPI_INTEGER,
par_p.comm_part[i], type, MPI_COMM_WORLD);
}
for (i = 0; i < par_b.num_comm_part; i++) {
MPI_Waitany(par_b.num_comm_part, request, &which, &status);
for (j = 0; j < par_b.comm_num[which]; j++)
if (recv_int[par_b.index[which]+j] > -1 &&
par_b.comm_b[par_b.index[which]+j] >= 0)
if (blocks[par_b.comm_b[par_b.index[which]+j]].refine == -1)
blocks[par_b.comm_b[par_b.index[which]+j]].refine = 0;
}
if (nonblocking)
for (i = 0; i < par_p.num_comm_part; i++)
MPI_Waitany(par_p.num_comm_part, s_req, &which, &status);
}
void comm_parent_unrefine(void)
{
int i, j, which, type, offset;
int *send_int = (int *) send_buff;
int *recv_int = (int *) recv_buff;
MPI_Status status;
type = 22;
for (i = 0; i < par_b.num_comm_part; i++)
MPI_Irecv(&recv_int[par_b.index[i]], par_b.comm_num[i], MPI_INTEGER,
par_b.comm_part[i], type, MPI_COMM_WORLD, &request[i]);
for (i = 0; i < par_p.num_comm_part; i++) {
if (nonblocking)
offset = par_p.index[i];
else
offset = 0;
for (j = 0; j < par_p.comm_num[i]; j++)
send_int[offset+j] = parents[par_p.comm_p[par_p.index[i]+j]].refine;
if (nonblocking)
MPI_Isend(&send_int[par_p.index[i]], par_p.comm_num[i], MPI_INTEGER,
par_p.comm_part[i], type, MPI_COMM_WORLD, &s_req[i]);
else
MPI_Send(&send_int[0], par_p.comm_num[i], MPI_INTEGER,
par_p.comm_part[i], type, MPI_COMM_WORLD);
}
for (i = 0; i < par_b.num_comm_part; i++) {
MPI_Waitany(par_b.num_comm_part, request, &which, &status);
for (j = 0; j < par_b.comm_num[which]; j++)
if (par_b.comm_b[par_b.index[which]+j] >= 0)
blocks[par_b.comm_b[par_b.index[which]+j]].refine =
recv_int[par_b.index[which]+j];
}
if (nonblocking)
for (i = 0; i < par_p.num_comm_part; i++)
MPI_Waitany(par_p.num_comm_part, s_req, &which, &status);
}
// Communicate new proc to parents - coordinate properly
// As new proc numbers come in, del the current and add the new
void comm_parent_proc(void)
{
int i, j, which, type, offset;
int *send_int = (int *) send_buff;
int *recv_int = (int *) recv_buff;
parent *pp;
MPI_Status status;
// duplicate par_p to par_p1
if (par_p.num_comm_part > par_p1.max_part) {
free(par_p1.comm_part);
free(par_p1.comm_num);
free(par_p1.index);
par_p1.max_part = par_p.max_part;
par_p1.comm_part = (int *) ma_malloc(par_p.max_part*sizeof(int),
__FILE__, __LINE__);
par_p1.comm_num = (int *) ma_malloc(par_p.max_part*sizeof(int),
__FILE__, __LINE__);
par_p1.index = (int *) ma_malloc(par_p.max_part*sizeof(int),
__FILE__, __LINE__);
}
if (par_p.num_cases > par_p1.max_cases) {
free(par_p1.comm_b);
free(par_p1.comm_p);
free(par_p1.comm_c);
par_p1.max_cases = par_p.max_cases;
par_p1.comm_b = (int *) ma_malloc(par_p.max_cases*sizeof(int),
__FILE__, __LINE__);
par_p1.comm_p = (int *) ma_malloc(par_p.max_cases*sizeof(int),
__FILE__, __LINE__);
par_p1.comm_c = (int *) ma_malloc(par_p.max_cases*sizeof(int),
__FILE__, __LINE__);
}
par_p1.num_comm_part = par_p.num_comm_part;
for (i = 0; i < par_p.num_comm_part; i++) {
par_p1.comm_part[i] = par_p.comm_part[i];
par_p1.comm_num[i] = par_p.comm_num[i];
par_p1.index[i] = par_p.index[i];
}
par_p1.num_cases = par_p.num_cases;
for (i = 0; i < par_p.num_cases; i++) {
par_p1.comm_b[i] = par_p.comm_b[i];
par_p1.comm_p[i] = par_p.comm_p[i];
par_p1.comm_c[i] = par_p.comm_c[i];
}
type = 23;
for (i = 0; i < par_p.num_comm_part; i++)
MPI_Irecv(&recv_int[par_p.index[i]], par_p.comm_num[i], MPI_INTEGER,
par_p.comm_part[i], type, MPI_COMM_WORLD, &request[i]);
for (i = 0; i < par_b.num_comm_part; i++) {
if (nonblocking)
offset = par_b.index[i];
else
offset = 0;
for (j = 0; j < par_b.comm_num[i]; j++)
if (par_b.comm_b[par_b.index[i]+j] < 0)
// parent and will not move, so send current processor
send_int[offset+j] = my_pe;
else
send_int[offset+j] =
blocks[par_b.comm_b[par_b.index[i]+j]].new_proc;
if (nonblocking)
MPI_Isend(&send_int[par_b.index[i]], par_b.comm_num[i], MPI_INTEGER,
par_b.comm_part[i], type, MPI_COMM_WORLD, &s_req[i]);
else
MPI_Send(&send_int[0], par_b.comm_num[i], MPI_INTEGER,
par_b.comm_part[i], type, MPI_COMM_WORLD);
}
for (i = 0; i < par_p1.num_comm_part; i++) {
MPI_Waitany(par_p1.num_comm_part, request, &which, &status);
for (j = 0; j < par_p1.comm_num[which]; j++)
if (recv_int[par_p1.index[which]+j] > -1) {
pp = &parents[par_p1.comm_p[par_p1.index[which]+j]];
if (pp->child_node[par_p1.comm_c[par_p1.index[which]+j]] !=
recv_int[par_p1.index[which]+j]) {
del_par_list(&par_p, par_p1.comm_p[par_p1.index[which]+j],
par_p1.comm_b[par_p1.index[which]+j],
par_p1.comm_c[par_p1.index[which]+j],
par_p1.comm_part[which]);
if (recv_int[par_p1.index[which]+j] != my_pe) {
add_par_list(&par_p, par_p1.comm_p[par_p1.index[which]+j],
par_p1.comm_b[par_p1.index[which]+j],
par_p1.comm_c[par_p1.index[which]+j],
recv_int[par_p1.index[which]+j], 1);
pp->child_node[par_p1.comm_c[par_p1.index[which]+j]] =
recv_int[par_p1.index[which]+j];
} else
pp->child_node[par_p1.comm_c[par_p1.index[which]+j]] = my_pe;
}
}
}
if (nonblocking)
for (i = 0; i < par_b.num_comm_part; i++)
MPI_Waitany(par_b.num_comm_part, s_req, &which, &status);
}
// Below are routines for adding and deleting from arrays used above
void add_par_list(par_comm *pc, int parent, int block, int child, int pe,
int sort)
{
int i, j, *tmp;
// first add information into comm_part, comm_num, and index
// i is being used as an index to where the info goes in the arrays
for (i = 0; i < pc->num_comm_part; i++)
if (pc->comm_part[i] >= pe)
break;
if (i < pc->num_comm_part && pc->comm_part[i] == pe) {
for (j = pc->num_comm_part-1; j > i; j--)
pc->index[j]++;
pc->comm_num[i]++;
} else {
// adding new pe, make sure arrays are large enough
if (pc->num_comm_part == pc->max_part) {
pc->max_part = (int)(2.0*((double) (pc->num_comm_part + 1)));
tmp = (int *) ma_malloc(pc->max_part*sizeof(int), __FILE__, __LINE__);
for (j = 0; j < i; j++)
tmp[j] = pc->comm_part[j];
for (j = i; j < pc->num_comm_part; j++)
tmp[j+1] = pc->comm_part[j];
free(pc->comm_part);
pc->comm_part = tmp;
tmp = (int *) ma_malloc(pc->max_part*sizeof(int), __FILE__, __LINE__);
for (j = 0; j < i; j++)
tmp[j] = pc->comm_num[j];
for (j = i; j < pc->num_comm_part; j++)
tmp[j+1] = pc->comm_num[j];
free(pc->comm_num);
pc->comm_num = tmp;
tmp = (int *) ma_malloc(pc->max_part*sizeof(int), __FILE__, __LINE__);
for (j = 0; j <= i; j++)
tmp[j] = pc->index[j];
for (j = i; j < pc->num_comm_part; j++)
tmp[j+1] = pc->index[j] + 1;
free(pc->index);
pc->index = tmp;
} else {
for (j = pc->num_comm_part; j > i; j--) {
pc->comm_part[j] = pc->comm_part[j-1];
pc->comm_num[j] = pc->comm_num[j-1];
pc->index[j] = pc->index[j-1] + 1;
}
}
if (i == pc->num_comm_part)
pc->index[i] = pc->num_cases;
pc->num_comm_part++;
pc->comm_part[i] = pe;
pc->comm_num[i] = 1;
}
// now add into to comm_p and comm_b according to index
// first check if there is room in the arrays
if (pc->num_cases == pc->max_cases) {
pc->max_cases = (int)(2.0*((double) (pc->num_cases+1)));
tmp = (int *) ma_malloc(pc->max_cases*sizeof(int), __FILE__, __LINE__);
for (j = 0; j < pc->num_cases; j++)
tmp[j] = pc->comm_p[j];
free(pc->comm_p);
pc->comm_p = tmp;
tmp = (int *) ma_malloc(pc->max_cases*sizeof(int), __FILE__, __LINE__);
for (j = 0; j < pc->num_cases; j++)
tmp[j] = pc->comm_b[j];
free(pc->comm_b);
pc->comm_b = tmp;
tmp = (int *) ma_malloc(pc->max_cases*sizeof(int), __FILE__, __LINE__);
for (j = 0; j < pc->num_cases; j++)
tmp[j] = pc->comm_c[j];
free(pc->comm_c);
pc->comm_c = tmp;
}
if (pc->index[i] == pc->num_cases) {
// at end of arrays
pc->comm_p[pc->num_cases] = parent;
pc->comm_b[pc->num_cases] = block;
pc->comm_c[pc->num_cases] = child;
} else {
for (j = pc->num_cases; j >= pc->index[i]+pc->comm_num[i]; j--) {
pc->comm_p[j] = pc->comm_p[j-1];
pc->comm_b[j] = pc->comm_b[j-1];
pc->comm_c[j] = pc->comm_c[j-1];
}
for (j = pc->index[i]+pc->comm_num[i]-1; j >= pc->index[i]; j--) {
if (j == pc->index[i] ||
(sort && (parents[pc->comm_p[j-1]].number < parents[parent].number
|| (pc->comm_p[j-1] == parent && pc->comm_c[j-1] < child))) ||
(!sort && (pc->comm_p[j-1] < parent
|| (pc->comm_p[j-1] == parent && pc->comm_c[j-1] < child)))) {
pc->comm_p[j] = parent;
pc->comm_b[j] = block;
pc->comm_c[j] = child;
break;
} else {
pc->comm_p[j] = pc->comm_p[j-1];
pc->comm_b[j] = pc->comm_b[j-1];
pc->comm_c[j] = pc->comm_c[j-1];
}
}
}
pc->num_cases++;
}
void del_par_list(par_comm *pc, int parent, int block, int child, int pe)
{
int i, j, k;
// find core number in index list and use i below
for (i = 0; i < pc->num_comm_part; i++)
if (pc->comm_part[i] == pe)
break;
// find and delete case in comm_p, comm_b, and comm_c
pc->num_cases--;
for (j = pc->index[i]; j < pc->index[i]+pc->comm_num[i]; j++)
if (pc->comm_p[j] == parent && pc->comm_c[j] == child) {
for (k = j; k < pc->num_cases; k++) {
pc->comm_p[k] = pc->comm_p[k+1];
pc->comm_b[k] = pc->comm_b[k+1];
pc->comm_c[k] = pc->comm_c[k+1];
}
break;
}
// fix index and adjust comm_part and comm_num
pc->comm_num[i]--;
if (pc->comm_num[i])
for (j = i+1; j < pc->num_comm_part; j++)
pc->index[j]--;
else {
pc->num_comm_part--;
for (j = i; j < pc->num_comm_part; j++) {
pc->comm_part[j] = pc->comm_part[j+1];
pc->comm_num[j] = pc->comm_num[j+1];
pc->index[j] = pc->index[j+1] - 1;
}
}
}