1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
4 * (C) 2003 by Argonne National Laboratory.
5 * See COPYRIGHT in top-level directory.
12 #ifdef HAVE_SYS_TIME_H
19 /* FIXME: What is this test supposed to accomplish? */
22 #define LARGE_BUF (256 * 1024)
24 /* FIXME: MAX_BUF is too large */
25 #define MAX_BUF (128 * 1024 * 1024)
28 __thread char * sbuf, * rbuf;
29 __thread int * recvcounts, * displs;
32 /* #define dprintf printf */
44 void comm_tests(MPI_Comm comm);
45 double run_test(long long msg_size, MPI_Comm comm, test_t test_type, double * max_time);
47 int main(int argc, char ** argv)
49 int comm_size, comm_rank;
52 MTest_Init(&argc, &argv);
53 MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
54 MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank);
56 if (LARGE_BUF * comm_size > MAX_BUF)
59 sbuf = (void *) calloc(MAX_BUF, 1);
60 rbuf = (void *) calloc(MAX_BUF, 1);
64 recvcounts = (void *) malloc(comm_size * sizeof(int));
65 displs = (void *) malloc(comm_size * sizeof(int));
66 if (!recvcounts || !displs || !sbuf || !rbuf) {
67 fprintf(stderr, "Unable to allocate memory:\n");
68 if (!sbuf) fprintf(stderr,"\tsbuf of %d bytes\n", MAX_BUF );
69 if (!rbuf) fprintf(stderr,"\trbuf of %d bytes\n", MAX_BUF );
70 if (!recvcounts) fprintf(stderr,"\trecvcounts of %zd bytes\n", comm_size * sizeof(int) );
71 if (!displs) fprintf(stderr,"\tdispls of %zd bytes\n", comm_size * sizeof(int) );
73 MPI_Abort(MPI_COMM_WORLD, -1);
77 dprintf("Message Range: (%d, %d); System size: %d\n", START_BUF, LARGE_BUF, comm_size);
82 /* COMM_WORLD tests */
84 dprintf("\n\n==========================================================\n");
85 dprintf(" MPI_COMM_WORLD\n");
86 dprintf("==========================================================\n");
88 comm_tests(MPI_COMM_WORLD);
90 /* non-COMM_WORLD tests */
92 dprintf("\n\n==========================================================\n");
93 dprintf(" non-COMM_WORLD\n");
94 dprintf("==========================================================\n");
96 MPI_Comm_split(MPI_COMM_WORLD, (comm_rank == comm_size - 1) ? 0 : 1, 0, &comm);
97 if (comm_rank < comm_size - 1)
101 /* Randomized communicator tests */
103 dprintf("\n\n==========================================================\n");
104 dprintf(" Randomized Communicator\n");
105 dprintf("==========================================================\n");
107 MPI_Comm_split(MPI_COMM_WORLD, 0, rand(), &comm);
109 MPI_Comm_free(&comm);
117 MTest_Finalize(errs);
123 void comm_tests(MPI_Comm comm)
125 int comm_size, comm_rank;
126 double rtime, max_time;
129 MPI_Comm_size(comm, &comm_size);
130 MPI_Comm_rank(comm, &comm_rank);
132 for (msg_size = START_BUF; msg_size <= LARGE_BUF; msg_size *= 2) {
134 dprintf("\n====> MSG_SIZE: %d\n", (int) msg_size);
138 rtime = run_test(msg_size, comm, REGULAR, &max_time);
140 dprintf("REGULAR:\tAVG: %.3f\tMAX: %.3f\n", rtime, max_time);
144 rtime = run_test(msg_size, comm, BCAST, &max_time);
146 dprintf("BCAST:\tAVG: %.3f\tMAX: %.3f\n", rtime, max_time);
150 rtime = run_test(msg_size, comm, SPIKE, &max_time);
152 dprintf("SPIKE:\tAVG: %.3f\tMAX: %.3f\n", rtime, max_time);
156 rtime = run_test(msg_size, comm, HALF_FULL, &max_time);
158 dprintf("HALF_FULL:\tAVG: %.3f\tMAX: %.3f\n", rtime, max_time);
162 rtime = run_test(msg_size, comm, LINEAR_DECREASE, &max_time);
164 dprintf("LINEAR_DECREASE:\tAVG: %.3f\tMAX: %.3f\n", rtime, max_time);
168 rtime = run_test(msg_size, comm, BELL_CURVE, &max_time);
170 dprintf("BELL_CURVE:\tAVG: %.3f\tMAX: %.3f\n", rtime, max_time);
176 double run_test(long long msg_size, MPI_Comm comm, test_t test_type,
180 int comm_size, comm_rank;
182 double total_time, avg_time;
185 MPI_Comm_size(comm, &comm_size);
186 MPI_Comm_rank(comm, &comm_rank);
189 for (i = 0; i < comm_size; i++) {
190 if (test_type == REGULAR)
191 recvcounts[i] = msg_size;
192 else if (test_type == BCAST)
193 recvcounts[i] = (!i) ? msg_size : 0;
194 else if (test_type == SPIKE)
195 recvcounts[i] = (!i) ? (msg_size / 2) : (msg_size / (2 * (comm_size - 1)));
196 else if (test_type == HALF_FULL)
197 recvcounts[i] = (i < (comm_size / 2)) ? (2 * msg_size) : 0;
198 else if (test_type == LINEAR_DECREASE) {
199 tmp = 2 * msg_size * (comm_size - 1 - i) / (comm_size - 1);
200 if (tmp != (int)tmp) {
201 fprintf( stderr, "Integer overflow in variable tmp\n" );
202 MPI_Abort( MPI_COMM_WORLD, 1 );
204 recvcounts[i] = (int) tmp;
206 /* If the maximum message size is too large, don't run */
207 if (tmp > MAX_BUF) return 0;
209 else if (test_type == BELL_CURVE) {
210 for (j = 0; j < i; j++) {
211 if (i - 1 + j >= comm_size) continue;
212 tmp = msg_size * comm_size / (log(comm_size) * i);
213 recvcounts[i - 1 + j] = (int) tmp;
214 displs[i - 1 + j] = 0;
216 /* If the maximum message size is too large, don't run */
217 if (tmp > MAX_BUF) return 0;
221 if (i < comm_size - 1)
222 displs[i+1] = displs[i] + recvcounts[i];
226 1: sbuf is large enough
227 2: rbuf is large enough
228 3: There were no failures (e.g., tmp nowhere > rbuf size
232 for (i = 0; i < LOOPS; i++) {
233 MPI_Allgatherv(sbuf, recvcounts[comm_rank], MPI_CHAR,
234 rbuf, recvcounts, displs, MPI_CHAR, comm);
239 /* Convert to microseconds (why?) */
240 total_time = 1.0e6 * (end - start);
241 MPI_Reduce(&total_time, &avg_time, 1, MPI_DOUBLE, MPI_SUM, 0, comm);
242 MPI_Reduce(&total_time, max_time, 1, MPI_DOUBLE, MPI_MAX, 0, comm);
244 return (avg_time / (LOOPS * comm_size));