| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Zoltan Toolkit for Load-balancing, Partitioning, Ordering and Coloring |
| 4 |
// |
| 5 |
// Copyright 2012 NTESS and the Zoltan contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
#include <mpi.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
int main(int argc, char *argv[]) |
| 15 |
{ |
| 16 |
int i, j, k, lval, gval, numClasses; |
| 17 |
int nextPart, rc, len, level, mine; |
| 18 |
int num_groups, my_group, sender, receiver; |
| 19 |
int me[NLEVELS], nprocs[NLEVELS]; |
| 20 |
int num_significant_levels=0; |
| 21 |
int level_part_range[NLEVELS][2]; |
| 22 |
int level_number[NLEVELS]; |
| 23 |
int *classes=NULL; |
| 24 |
int group_start, group_end; |
| 25 |
char *errorstr; |
| 26 |
comm_group level_info[NLEVELS]; |
| 27 |
|
| 28 |
MPI_Init(&argc, &argv); |
| 29 |
|
| 30 |
for (i=0; i < NLEVELS; i++){ |
| 31 |
rc = MPI_Comm_size(comm[i], nprocs + i); |
| 32 |
if (rc != MPI_SUCCESS){ |
| 33 |
MPI_Error_string(rc, errorstr, &len); |
| 34 |
fprintf(stderr,"(%d) MPI_Comm_size %s : %s\n",me[0],commName[i],errorstr); |
| 35 |
} |
| 36 |
|
| 37 |
rc = MPI_Comm_rank(comm[i], me + i); |
| 38 |
if (rc != MPI_SUCCESS){ |
| 39 |
MPI_Error_string(rc, errorstr, &len); |
| 40 |
fprintf(stderr,"(%d) MPI_Comm_size %s : %s\n",me[0],commName[i],errorstr); |
| 41 |
} |
| 42 |
#ifdef DEBUG_ME |
| 43 |
MPI_Barrier(zoltan_get_global_comm()); |
| 44 |
printf("(%d) %s communicator, size %d, my rank %d\n",me[0],commName[i],nprocs[i],me[i]); |
| 45 |
MPI_Barrier(zoltan_get_global_comm()); |
| 46 |
#endif |
| 47 |
} |
| 48 |
|
| 49 |
buf = (int *)malloc(sizeof(int) * nprocs[0]); |
| 50 |
classes = (int *)malloc(sizeof(int) * nprocs[0]); |
| 51 |
procToPart = (int *)malloc(sizeof(int) * nprocs[0]); |
| 52 |
partToProc = (int *)malloc(sizeof(int) * nprocs[0]); |
| 53 |
|
| 54 |
level_part_range[0][0] = 0; |
| 55 |
level_part_range[0][1] = nprocs[0]-1; |
| 56 |
|
| 57 |
for (i=0; i < NLEVELS - 1; i++){ |
| 58 |
/* |
| 59 |
* classes[k] contains the rank (in level i) of the rank 0 element of element k's subcommunicator |
| 60 |
*/ |
| 61 |
level_down(i, classes, me, nprocs); |
| 62 |
|
| 63 |
/* |
| 64 |
* my sub communicator will create which parts in the final partitioning? |
| 65 |
*/ |
| 66 |
new_part_range(classes, level_part_range[i], level_part_range[i+1], me[i], nprocs[i]); |
| 67 |
} |
| 68 |
|
| 69 |
#ifdef DEBUG_ME |
| 70 |
MPI_Barrier(zoltan_get_global_comm()); |
| 71 |
for (i=0; i < nprocs[0]; i++){ |
| 72 |
if (i == me[0]){ |
| 73 |
printf("(%d) ranges: %s (%d %d iam %d) %s (%d %d iam %d) %s (%d %d iam %d) %s (%d %d iam %d) \n", |
| 74 |
me[0], |
| 75 |
commName[0], level_part_range[0][0], level_part_range[0][1], level_part_range[0][0] + me[0], |
| 76 |
commName[1], level_part_range[1][0], level_part_range[1][1], level_part_range[1][0] + me[1], |
| 77 |
commName[2], level_part_range[2][0], level_part_range[2][1], level_part_range[2][0] + me[2], |
| 78 |
commName[3], level_part_range[3][0], level_part_range[3][1], level_part_range[3][0] + me[3]); |
| 79 |
} |
| 80 |
MPI_Barrier(zoltan_get_global_comm()); |
| 81 |
MPI_Barrier(zoltan_get_global_comm()); |
| 82 |
MPI_Barrier(zoltan_get_global_comm()); |
| 83 |
} |
| 84 |
#endif |
| 85 |
|
| 86 |
/* Figure out which levels are significant */ |
| 87 |
|
| 88 |
num_significant_levels = 1; |
| 89 |
level_number[0] = 0; |
| 90 |
gval = 0; |
| 91 |
|
| 92 |
for (i=0; i < NLEVELS - 1; i++){ |
| 93 |
if ((nprocs[i+1] == nprocs[i]) || (nprocs[i+1] == 1)){ |
| 94 |
lval=1; /* insignificant */ |
| 95 |
} |
| 96 |
else{ |
| 97 |
lval=0; /* meaningful level in hierarchy */ |
| 98 |
} |
| 99 |
|
| 100 |
MPI_Allreduce(&lval, &gval, 1, MPI_INT, MPI_SUM, zoltan_get_global_comm()); |
| 101 |
|
| 102 |
if (gval < nprocs[0]){ |
| 103 |
/* next level in hierarchy is significant for at least some processes */ |
| 104 |
level_number[num_significant_levels++] = i + 1; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
#ifdef DEBUG_ME |
| 109 |
MPI_Barrier(zoltan_get_global_comm()); |
| 110 |
printf("(%d) %d significant levels %d %d %d %d\n",me[0], num_significant_levels, |
| 111 |
level_number[0], level_number[1], level_number[2], level_number[3]); |
| 112 |
MPI_Barrier(zoltan_get_global_comm()); |
| 113 |
#endif |
| 114 |
|
| 115 |
/* Save global info about topology */ |
| 116 |
|
| 117 |
level = level_number[num_significant_levels-1]; |
| 118 |
|
| 119 |
my_part_number = level_part_range[level][0] + me[level]; |
| 120 |
|
| 121 |
MPI_Allgather(&my_part_number, 1, MPI_INT, procToPart, 1, MPI_INT, zoltan_get_global_comm()); |
| 122 |
|
| 123 |
for (i=0; i < nprocs[0]; i++){ |
| 124 |
partToProc[procToPart[i]] = i; |
| 125 |
} |
| 126 |
|
| 127 |
level_info[0].comm = zoltan_get_global_comm(); |
| 128 |
level_info[0].name = commName[0]; |
| 129 |
level_info[0].nGroups = 1; |
| 130 |
level_info[0].myGroup = 0; |
| 131 |
level_info[0].offsets = (int *)malloc(sizeof(int) * 2); |
| 132 |
level_info[0].offsets[0] = 0; |
| 133 |
level_info[0].offsets[1] = nprocs[0]; |
| 134 |
level_info[0].myLocalPart = my_part_number; |
| 135 |
|
| 136 |
for (i=1; i < num_significant_levels; i++){ |
| 137 |
|
| 138 |
level = level_number[i]; |
| 139 |
|
| 140 |
memset((void *)&level_info[i], 0, sizeof(comm_group)); |
| 141 |
mine = 0; |
| 142 |
|
| 143 |
MPI_Allgather(&(level_part_range[level][0]), 1, MPI_INT, buf, 1, MPI_INT, zoltan_get_global_comm()); |
| 144 |
memset(classes, 0, sizeof(int) * nprocs[0]); |
| 145 |
for (j=0; j < nprocs[0]; j++){ |
| 146 |
classes[buf[j]]++; |
| 147 |
} |
| 148 |
memset(buf, 0, sizeof(int) * nprocs[0]); |
| 149 |
for (j=0, numClasses=0; j < nprocs[0]; j++){ |
| 150 |
if (classes[j] > 0){ |
| 151 |
|
| 152 |
if (j < level_part_range[level][0]) mine++; |
| 153 |
|
| 154 |
buf[numClasses++] = classes[j]; /* number of parts/procs in sub communicator */ |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
level_info[i].comm = comm[level]; |
| 159 |
level_info[i].name = commName[level]; |
| 160 |
level_info[i].nGroups = numClasses; |
| 161 |
level_info[i].myGroup = mine; |
| 162 |
level_info[i].offsets = (int *)malloc(sizeof(int) * (numClasses + 1)); |
| 163 |
level_info[i].offsets[0] = 0; |
| 164 |
for (k=0; k < numClasses; k++){ |
| 165 |
level_info[i].offsets[k+1] = level_info[i].offsets[k] + buf[k]; |
| 166 |
} |
| 167 |
level_info[i].myLocalPart = me[level]; |
| 168 |
} |
| 169 |
|
| 170 |
free(buf); |
| 171 |
free(classes); |
| 172 |
|
| 173 |
/*#ifdef DEBUG_ME*/ |
| 174 |
if (my_part_number == 0){ |
| 175 |
for (i=0; i < num_significant_levels; i++){ |
| 176 |
printf("Level %d (%s) has %d groups: \n",i, level_info[i].name, level_info[i].nGroups); |
| 177 |
for (j=0; j < level_info[i].nGroups; j++){ |
| 178 |
printf(" Group %d has parts %d through %d\n",j, level_info[i].offsets[j], level_info[i].offsets[j+1]-1); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
/*#endif*/ |
| 183 |
|
| 184 |
/* do some ping pong tests to compare the levels in the topology */ |
| 185 |
|
| 186 |
for (i=1; i < num_significant_levels; i++){ |
| 187 |
|
| 188 |
if (level_info[i].nGroups <= 1) continue; |
| 189 |
|
| 190 |
/* ping pong within an entity */ |
| 191 |
|
| 192 |
sender = partToProc[0]; |
| 193 |
receiver = partToProc[level_info[i].offsets[1] - 1]; |
| 194 |
|
| 195 |
if (me[0] == sender){ |
| 196 |
printf("\nPing pong test within a %s communicator (between parts %d and %d):\n", |
| 197 |
level_info[i].name, 0, level_info[i].offsets[1]-1); |
| 198 |
} |
| 199 |
|
| 200 |
ping_pong_test(zoltan_get_global_comm(), me[0], sender, receiver, 100); |
| 201 |
|
| 202 |
/* ping pong across an entity */ |
| 203 |
|
| 204 |
if (me[0] == sender){ |
| 205 |
printf("\nPing pong test between two %s communicators (between parts %d and %d):\n", |
| 206 |
level_info[i].name, 0, level_info[i].offsets[1]); |
| 207 |
} |
| 208 |
|
| 209 |
sender = partToProc[0]; |
| 210 |
receiver = partToProc[level_info[i].offsets[1]]; |
| 211 |
|
| 212 |
ping_pong_test(zoltan_get_global_comm(), me[0], sender, receiver, 100); |
| 213 |
} |
| 214 |
|
| 215 |
MPI_Finalize(); |
| 216 |
} |
| 217 |
|
| 218 |
/* Following ping-pong test is converted from a program to a function by [email protected] */ |
| 219 |
|
| 220 |
/* pong.c Generic Benchmark code |
| 221 |
* Dave Turner - Ames Lab - July of 1994+++ |
| 222 |
* |
| 223 |
* Most Unix timers can't be trusted for very short times, so take this |
| 224 |
* into account when looking at the results. This code also only times |
| 225 |
* a single message passing event for each size, so the results may vary |
| 226 |
* between runs. For more accurate measurements, grab NetPIPE from |
| 227 |
* http://www.scl.ameslab.gov/ . |
| 228 |
*/ |
| 229 |
#include "mpi.h" |
| 230 |
#include <stdio.h> |
| 231 |
#include <stdlib.h> |
| 232 |
|
| 233 |
void ping_pong_test(MPI_Comm comm, int myproc, int senderRank, int receiverRank, int ntests) |
| 234 |
{ |
| 235 |
int size, other_proc, i, last, testno, j; |
| 236 |
double accuracy; |
| 237 |
double t0, t1, time, tsum; |
| 238 |
double *a, *b; |
| 239 |
double max_rate = 0.0, min_latency = 10e6; |
| 240 |
MPI_Request request, request_a, request_b; |
| 241 |
MPI_Status status; |
| 242 |
|
| 243 |
if ((myproc != senderRank) && (myproc != receiverRank)) return; |
| 244 |
|
| 245 |
#if defined (_CRAYT3E) |
| 246 |
a = (double *) shmalloc (132000 * sizeof (double)); |
| 247 |
b = (double *) shmalloc (132000 * sizeof (double)); |
| 248 |
#else |
| 249 |
a = (double *) malloc (132000 * sizeof (double)); |
| 250 |
b = (double *) malloc (132000 * sizeof (double)); |
| 251 |
#endif |
| 252 |
|
| 253 |
for (i = 0; i < 132000; i++) { |
| 254 |
a[i] = (double) i; |
| 255 |
b[i] = 0.0; |
| 256 |
} |
| 257 |
|
| 258 |
other_proc = ((myproc == senderRank) ? receiverRank : senderRank); |
| 259 |
|
| 260 |
/* Timer accuracy test */ |
| 261 |
|
| 262 |
if (myproc == senderRank){ |
| 263 |
t0 = MPI_Wtime(); |
| 264 |
t1 = MPI_Wtime(); |
| 265 |
|
| 266 |
while (t1 == t0) t1 = MPI_Wtime(); |
| 267 |
|
| 268 |
accuracy = (t1 - t0) * 1000000; |
| 269 |
|
| 270 |
printf("Timer accuracy of ~%f usecs\n\n", accuracy); |
| 271 |
fflush(stdout); |
| 272 |
} |
| 273 |
|
| 274 |
/* Communications between nodes |
| 275 |
* - Blocking sends and recvs |
| 276 |
* - No guarantee of prepost, so might pass through comm buffer |
| 277 |
*/ |
| 278 |
|
| 279 |
#if 0 |
| 280 |
for (size = 8; size <= 1048576; size *= 2) { |
| 281 |
for (i = 0; i < size / 8; i++) { |
| 282 |
a[i] = (double) i; |
| 283 |
b[i] = 0.0; |
| 284 |
} |
| 285 |
last = size / 8 - 1; |
| 286 |
|
| 287 |
MPI_Barrier(comm); |
| 288 |
|
| 289 |
if (myproc == senderRank) { |
| 290 |
|
| 291 |
t0 = MPI_Wtime(); |
| 292 |
MPI_Send(a, size/8, MPI_DOUBLE, other_proc, 0, comm); |
| 293 |
MPI_Recv(b, size/8, MPI_DOUBLE, other_proc, 0, comm, &status); |
| 294 |
t1 = MPI_Wtime(); |
| 295 |
time = 1.e6 * (t1 - t0); |
| 296 |
|
| 297 |
} else if (myproc == receiverRank){ |
| 298 |
|
| 299 |
MPI_Recv(b, size/8, MPI_DOUBLE, other_proc, 0, comm, &status); |
| 300 |
|
| 301 |
b[0] += 1.0; |
| 302 |
if (last != 0) |
| 303 |
b[last] += 1.0; |
| 304 |
|
| 305 |
MPI_Send(b, size/8, MPI_DOUBLE, other_proc, 0, comm); |
| 306 |
} |
| 307 |
|
| 308 |
MPI_Barrier(comm); |
| 309 |
|
| 310 |
if (myproc == senderRank){ |
| 311 |
if ((b[0] != 1.0 || b[last] != last + 1)) { |
| 312 |
printf("(%d) ERROR - b[0] = %f b[%d] = %f\n", my_part_number, b[0], last, b[last]); |
| 313 |
exit (1); |
| 314 |
} |
| 315 |
for (i = 1; i < last - 1; i++) |
| 316 |
if (b[i] != (double) i) |
| 317 |
printf("(%d) ERROR - b[%d] = %f\n", my_part_number, i, b[i]); |
| 318 |
if (time > accuracy) { |
| 319 |
printf(" %7d bytes took %9.0f usec (%8.3f MB/sec)\n", |
| 320 |
size, time, 2.0 * size / time); |
| 321 |
if (2 * size / time > max_rate) max_rate = 2 * size / time; |
| 322 |
if (time / 2 < min_latency) min_latency = time / 2; |
| 323 |
} else { |
| 324 |
printf(" %7d bytes took less than the timer accuracy\n", size); |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
#endif |
| 329 |
|
| 330 |
/* Async communications |
| 331 |
* - Prepost receives to guarantee bypassing the comm buffer |
| 332 |
*/ |
| 333 |
|
| 334 |
if (myproc == senderRank){ |
| 335 |
printf("\n Asynchronous ping-pong, averages over %d ping-pong tests\n\n",ntests); |
| 336 |
fflush(stdout); |
| 337 |
} |
| 338 |
|
| 339 |
for (size = 8; size <= 1048576; size *= 2) { |
| 340 |
|
| 341 |
tsum = 0; |
| 342 |
|
| 343 |
for (j=0; j < ntests; j++){ |
| 344 |
|
| 345 |
if (myproc == senderRank) { |
| 346 |
|
| 347 |
MPI_Recv(NULL, 0, MPI_CHAR, receiverRank, 99, zoltan_get_global_comm(), &status); /* ok to start */ |
| 348 |
MPI_Irecv(b, size/8, MPI_DOUBLE, other_proc, j, comm, &request); |
| 349 |
|
| 350 |
t0 = MPI_Wtime(); |
| 351 |
MPI_Send(a, size/8, MPI_DOUBLE, other_proc, j, comm); |
| 352 |
MPI_Wait(&request, &status); |
| 353 |
tsum += MPI_Wtime() - t0; |
| 354 |
|
| 355 |
} else if (myproc == receiverRank) { |
| 356 |
|
| 357 |
MPI_Irecv(b, size/8, MPI_DOUBLE, other_proc, j, comm, &request); |
| 358 |
|
| 359 |
MPI_Send(NULL, 0, MPI_CHAR, senderRank, 99, zoltan_get_global_comm()); /* ok to start */ |
| 360 |
|
| 361 |
MPI_Wait(&request, &status); |
| 362 |
|
| 363 |
MPI_Send(b, size/8, MPI_DOUBLE, other_proc, j, comm); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
if (myproc == senderRank) { |
| 368 |
time = (1.e6 * tsum) / ntests; |
| 369 |
if (time > accuracy) { |
| 370 |
printf(" %7d bytes took %9.0f usec (%8.3f MB/sec)\n", |
| 371 |
size, time, 2.0 * size / time); |
| 372 |
if (2 * size / time > max_rate) max_rate = 2 * size / time; |
| 373 |
if (time / 2 < min_latency) min_latency = time / 2; |
| 374 |
} else { |
| 375 |
printf(" %7d bytes took less than the timer accuracy\n", size); |
| 376 |
} |
| 377 |
fflush(stdout); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
#if 0 |
| 382 |
/* Bidirectional communications |
| 383 |
* - Prepost receives to guarantee bypassing the comm buffer |
| 384 |
*/ |
| 385 |
|
| 386 |
MPI_Barrier(comm); |
| 387 |
if (myproc == senderRank) printf("\n Bi-directional asynchronous ping-pong\n\n"); |
| 388 |
|
| 389 |
for (size = 8; size <= 1048576; size *= 2) { |
| 390 |
|
| 391 |
if ((myproc == senderRank) || (myproc == receiverRank)){ |
| 392 |
for (i = 0; i < size / 8; i++) { |
| 393 |
a[i] = (double) i; |
| 394 |
b[i] = 0.0; |
| 395 |
} |
| 396 |
last = size / 8 - 1; |
| 397 |
|
| 398 |
MPI_Irecv(b, size/8, MPI_DOUBLE, other_proc, 0, comm, &request_b); |
| 399 |
MPI_Irecv(a, size/8, MPI_DOUBLE, other_proc, 0, comm, &request_a); |
| 400 |
} |
| 401 |
|
| 402 |
MPI_Barrier(comm); |
| 403 |
|
| 404 |
if ((myproc == senderRank) || (myproc == receiverRank)){ |
| 405 |
t0 = MPI_Wtime(); |
| 406 |
MPI_Send(a, size/8, MPI_DOUBLE, other_proc, 0, comm); |
| 407 |
MPI_Wait(&request_b, &status); |
| 408 |
|
| 409 |
b[0] += 1.0; |
| 410 |
if (last != 0) |
| 411 |
b[last] += 1.0; |
| 412 |
|
| 413 |
MPI_Send(b, size/8, MPI_DOUBLE, other_proc, 0, comm); |
| 414 |
MPI_Wait(&request_a, &status); |
| 415 |
|
| 416 |
t1 = MPI_Wtime(); |
| 417 |
time = 1.e6 * (t1 - t0); |
| 418 |
} |
| 419 |
|
| 420 |
MPI_Barrier(comm); |
| 421 |
|
| 422 |
if ((myproc == senderRank) || (myproc == receiverRank)){ |
| 423 |
if ((a[0] != 1.0 || a[last] != last + 1)) |
| 424 |
printf("(%d) ERROR - a[0] = %f a[%d] = %f\n", my_part_number, a[0], last, a[last]); |
| 425 |
for (i = 1; i < last - 1; i++) |
| 426 |
if (a[i] != (double) i) |
| 427 |
printf("(%d) ERROR - a[%d] = %f\n", my_part_number, i, a[i]); |
| 428 |
if (myproc == senderRank && time > 0.000001) { |
| 429 |
printf(" %7d bytes took %9.0f usec (%8.3f MB/sec)\n", |
| 430 |
size, time, 2.0 * size / time); |
| 431 |
if (2 * size / time > max_rate) max_rate = 2 * size / time; |
| 432 |
if (time / 2 < min_latency) min_latency = time / 2; |
| 433 |
} else if (myproc == senderRank) { |
| 434 |
printf(" %7d bytes took less than the timer accuracy\n", size); |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
#endif |
| 439 |
|
| 440 |
if (myproc == senderRank) |
| 441 |
printf("\n Max rate = %f MB/sec Min latency = %f usec\n", |
| 442 |
max_rate, min_latency); |
| 443 |
} |
| 444 |
|
| 445 |
|