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.
10 #include "smpi_cocci.h"
13 #ifdef HAVE_SYS_TIME_H
20 /* FIXME: What is this test supposed to accomplish? */
23 #define LARGE_BUF (256 * 1024)
25 /* FIXME: MAX_BUF is too large */
26 #define MAX_BUF (32 * 1024 * 1024)
30 int * recvcounts, * displs;
33 /* #define dprintf printf */
45 void comm_tests(MPI_Comm comm);
46 double run_test(long long msg_size, MPI_Comm comm, test_t test_type, double * max_time);
48 int main(int argc, char ** argv)
50 int comm_size, comm_rank;
53 MTest_Init(&argc, &argv);
54 MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
55 MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank);
57 if (LARGE_BUF * comm_size > MAX_BUF)
60 sbuf = (void *) calloc(MAX_BUF, 1);
61 rbuf = (void *) calloc(MAX_BUF, 1);
65 recvcounts = (void *) malloc(comm_size * sizeof(int));
66 displs = (void *) malloc(comm_size * sizeof(int));
67 if (!recvcounts || !displs || !sbuf || !rbuf) {
68 fprintf(stderr, "Unable to allocate memory:\n");
69 if (!sbuf) fprintf(stderr,"\tsbuf of %d bytes\n", MAX_BUF );
70 if (!rbuf) fprintf(stderr,"\trbuf of %d bytes\n", MAX_BUF );
71 if (!recvcounts) fprintf(stderr,"\trecvcounts of %zd bytes\n", comm_size * sizeof(int) );
72 if (!displs) fprintf(stderr,"\tdispls of %zd bytes\n", comm_size * sizeof(int) );
74 MPI_Abort(MPI_COMM_WORLD, -1);
79 dprintf("Message Range: (%d, %d); System size: %d\n", START_BUF, LARGE_BUF, comm_size);
84 /* COMM_WORLD tests */
86 dprintf("\n\n==========================================================\n");
87 dprintf(" MPI_COMM_WORLD\n");
88 dprintf("==========================================================\n");
90 comm_tests(MPI_COMM_WORLD);
92 /* non-COMM_WORLD tests */
94 dprintf("\n\n==========================================================\n");
95 dprintf(" non-COMM_WORLD\n");
96 dprintf("==========================================================\n");
98 MPI_Comm_split(MPI_COMM_WORLD, (comm_rank == comm_size - 1) ? 0 : 1, 0, &comm);
99 if (comm_rank < comm_size - 1)
101 MPI_Comm_free(&comm);
103 /* Randomized communicator tests */
105 dprintf("\n\n==========================================================\n");
106 dprintf(" Randomized Communicator\n");
107 dprintf("==========================================================\n");
109 MPI_Comm_split(MPI_COMM_WORLD, 0, rand(), &comm);
111 MPI_Comm_free(&comm);
119 MTest_Finalize(errs);
125 void comm_tests(MPI_Comm comm)
127 int comm_size, comm_rank;
128 double rtime = rtime; /* stop warning about unused variable */
132 MPI_Comm_size(comm, &comm_size);
133 MPI_Comm_rank(comm, &comm_rank);
135 for (msg_size = START_BUF; msg_size <= LARGE_BUF; msg_size *= 2) {
137 dprintf("\n====> MSG_SIZE: %d\n", (int) msg_size);
141 rtime = run_test(msg_size, comm, REGULAR, &max_time);
143 dprintf("REGULAR:\tAVG: %.3f\tMAX: %.3f\n", rtime, max_time);
147 rtime = run_test(msg_size, comm, BCAST, &max_time);
149 dprintf("BCAST:\tAVG: %.3f\tMAX: %.3f\n", rtime, max_time);
153 rtime = run_test(msg_size, comm, SPIKE, &max_time);
155 dprintf("SPIKE:\tAVG: %.3f\tMAX: %.3f\n", rtime, max_time);
159 rtime = run_test(msg_size, comm, HALF_FULL, &max_time);
161 dprintf("HALF_FULL:\tAVG: %.3f\tMAX: %.3f\n", rtime, max_time);
165 rtime = run_test(msg_size, comm, LINEAR_DECREASE, &max_time);
167 dprintf("LINEAR_DECREASE:\tAVG: %.3f\tMAX: %.3f\n", rtime, max_time);
171 rtime = run_test(msg_size, comm, BELL_CURVE, &max_time);
173 dprintf("BELL_CURVE:\tAVG: %.3f\tMAX: %.3f\n", rtime, max_time);
179 double run_test(long long msg_size, MPI_Comm comm, test_t test_type,
183 int comm_size, comm_rank;
185 double total_time, avg_time;
188 MPI_Comm_size(comm, &comm_size);
189 MPI_Comm_rank(comm, &comm_rank);
192 for (i = 0; i < comm_size; i++) {
193 if (test_type == REGULAR)
194 recvcounts[i] = msg_size;
195 else if (test_type == BCAST)
196 recvcounts[i] = (!i) ? msg_size : 0;
197 else if (test_type == SPIKE)
198 recvcounts[i] = (!i) ? (msg_size / 2) : (msg_size / (2 * (comm_size - 1)));
199 else if (test_type == HALF_FULL)
200 recvcounts[i] = (i < (comm_size / 2)) ? (2 * msg_size) : 0;
201 else if (test_type == LINEAR_DECREASE) {
202 tmp = 2 * msg_size * (comm_size - 1 - i) / (comm_size - 1);
203 if (tmp != (int)tmp) {
204 fprintf( stderr, "Integer overflow in variable tmp\n" );
205 MPI_Abort( MPI_COMM_WORLD, 1 );
208 recvcounts[i] = (int) tmp;
210 /* If the maximum message size is too large, don't run */
211 if (tmp > MAX_BUF) return 0;
213 else if (test_type == BELL_CURVE) {
214 for (j = 0; j < i; j++) {
215 if (i - 1 + j >= comm_size) continue;
216 tmp = msg_size * comm_size / (log(comm_size) * i);
217 recvcounts[i - 1 + j] = (int) tmp;
218 displs[i - 1 + j] = 0;
220 /* If the maximum message size is too large, don't run */
221 if (tmp > MAX_BUF) return 0;
225 if (i < comm_size - 1)
226 displs[i+1] = displs[i] + recvcounts[i];
230 1: sbuf is large enough
231 2: rbuf is large enough
232 3: There were no failures (e.g., tmp nowhere > rbuf size
236 for (i = 0; i < LOOPS; i++) {
237 MPI_Allgatherv(sbuf, recvcounts[comm_rank], MPI_CHAR,
238 rbuf, recvcounts, displs, MPI_CHAR, comm);
243 /* Convert to microseconds (why?) */
244 total_time = 1.0e6 * (end - start);
245 MPI_Reduce(&total_time, &avg_time, 1, MPI_DOUBLE, MPI_SUM, 0, comm);
246 MPI_Reduce(&total_time, max_time, 1, MPI_DOUBLE, MPI_MAX, 0, comm);
248 return (avg_time / (LOOPS * comm_size));