1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2001 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
6 /* Warning - this test will fail for MPI_PROD & maybe MPI_SUM
7 * if more than 10 MPI processes are used. Loss of precision
8 * will occur as the number of processors is increased.
20 int count, size, rank;
44 #define mpi_op2str(op) \
45 ((op == MPI_SUM) ? "MPI_SUM" : \
46 (op == MPI_PROD) ? "MPI_PROD" : \
47 (op == MPI_MAX) ? "MPI_MAX" : \
48 (op == MPI_MIN) ? "MPI_MIN" : \
49 (op == MPI_LOR) ? "MPI_LOR" : \
50 (op == MPI_LXOR) ? "MPI_LXOR" : \
51 (op == MPI_LAND) ? "MPI_LAND" : \
52 (op == MPI_BOR) ? "MPI_BOR" : \
53 (op == MPI_BAND) ? "MPI_BAND" : \
54 (op == MPI_BXOR) ? "MPI_BXOR" : \
55 (op == MPI_MAXLOC) ? "MPI_MAXLOC" : \
56 (op == MPI_MINLOC) ? "MPI_MINLOC" : \
59 /* calloc to avoid spurious valgrind warnings when "type" has padding bytes */
60 #define DECL_MALLOC_IN_OUT_SOL(type) \
61 type *in, *out, *sol; \
62 in = (type *) calloc(count, sizeof(type)); \
63 out = (type *) calloc(count, sizeof(type)); \
64 sol = (type *) calloc(count, sizeof(type));
66 #define SET_INDEX_CONST(arr, val) \
69 for (i = 0; i < count; i++) \
73 #define SET_INDEX_SUM(arr, val) \
76 for (i = 0; i < count; i++) \
80 #define SET_INDEX_FACTOR(arr, val) \
83 for (i = 0; i < count; i++) \
87 #define SET_INDEX_POWER(arr, val) \
90 for (i = 0; i < count; i++) { \
92 for (j = 0; j < (val); j++) \
97 #define ERROR_CHECK_AND_FREE(lerrcnt, mpi_type, mpi_op) \
99 char name[MPI_MAX_OBJECT_NAME] = {0}; \
102 MPI_Type_get_name(mpi_type, name, &len); \
103 fprintf(stderr, "(%d) Error for type %s and op %s\n", \
104 rank, name, mpi_op2str(mpi_op)); \
106 free(in); free(out); free(sol); \
109 /* The logic on the error check on MPI_Allreduce assumes that all
110 MPI_Allreduce routines return a failure if any do - this is sufficient
111 for MPI implementations that reject some of the valid op/datatype pairs
112 (and motivated this addition, as some versions of the IBM MPI
113 failed in just this way).
115 #define ALLREDUCE_AND_FREE(mpi_type, mpi_op, in, out, sol) \
117 int i, rc, lerrcnt = 0; \
118 rc = MPI_Allreduce(in, out, count, mpi_type, mpi_op, MPI_COMM_WORLD); \
119 if (rc) { lerrcnt++; cerrcnt++; MTestPrintError(rc); } \
121 for (i = 0; i < count; i++) { \
122 if (out[i] != sol[i]) { \
128 ERROR_CHECK_AND_FREE(lerrcnt, mpi_type, mpi_op); \
131 #define STRUCT_ALLREDUCE_AND_FREE(mpi_type, mpi_op, in, out, sol) \
133 int i, rc, lerrcnt = 0; \
134 rc = MPI_Allreduce(in, out, count, mpi_type, mpi_op, MPI_COMM_WORLD); \
135 if (rc) { lerrcnt++; cerrcnt++; MTestPrintError(rc); } \
137 for (i = 0; i < count; i++) { \
138 if ((out[i].a != sol[i].a) || (out[i].b != sol[i].b)) { \
144 ERROR_CHECK_AND_FREE(lerrcnt, mpi_type, mpi_op); \
147 #define SET_INDEX_STRUCT_CONST(arr, val, el) \
150 for (i = 0; i < count; i++) \
154 #define SET_INDEX_STRUCT_SUM(arr, val, el) \
157 for (i = 0; i < count; i++) \
158 arr[i].el = i + (val); \
161 #define sum_test1(type, mpi_type) \
163 DECL_MALLOC_IN_OUT_SOL(type); \
164 SET_INDEX_SUM(in, 0); \
165 SET_INDEX_FACTOR(sol, size); \
166 SET_INDEX_CONST(out, 0); \
167 ALLREDUCE_AND_FREE(mpi_type, MPI_SUM, in, out, sol); \
170 #define prod_test1(type, mpi_type) \
172 DECL_MALLOC_IN_OUT_SOL(type); \
173 SET_INDEX_SUM(in, 0); \
174 SET_INDEX_POWER(sol, size); \
175 SET_INDEX_CONST(out, 0); \
176 ALLREDUCE_AND_FREE(mpi_type, MPI_PROD, in, out, sol); \
179 #define max_test1(type, mpi_type) \
181 DECL_MALLOC_IN_OUT_SOL(type); \
182 SET_INDEX_SUM(in, rank); \
183 SET_INDEX_SUM(sol, size - 1); \
184 SET_INDEX_CONST(out, 0); \
185 ALLREDUCE_AND_FREE(mpi_type, MPI_MAX, in, out, sol); \
188 #define min_test1(type, mpi_type) \
190 DECL_MALLOC_IN_OUT_SOL(type); \
191 SET_INDEX_SUM(in, rank); \
192 SET_INDEX_SUM(sol, 0); \
193 SET_INDEX_CONST(out, 0); \
194 ALLREDUCE_AND_FREE(mpi_type, MPI_MIN, in, out, sol); \
197 #define const_test(type, mpi_type, mpi_op, val1, val2, val3) \
199 DECL_MALLOC_IN_OUT_SOL(type); \
200 SET_INDEX_CONST(in, (val1)); \
201 SET_INDEX_CONST(sol, (val2)); \
202 SET_INDEX_CONST(out, (val3)); \
203 ALLREDUCE_AND_FREE(mpi_type, mpi_op, in, out, sol); \
206 #define lor_test1(type, mpi_type) \
207 const_test(type, mpi_type, MPI_LOR, (rank & 0x1), (size > 1), 0)
208 #define lor_test2(type, mpi_type) \
209 const_test(type, mpi_type, MPI_LOR, 0, 0, 0)
210 #define lxor_test1(type, mpi_type) \
211 const_test(type, mpi_type, MPI_LXOR, (rank == 1), (size > 1), 0)
212 #define lxor_test2(type, mpi_type) \
213 const_test(type, mpi_type, MPI_LXOR, 0, 0, 0)
214 #define lxor_test3(type, mpi_type) \
215 const_test(type, mpi_type, MPI_LXOR, 1, (size & 0x1), 0)
216 #define land_test1(type, mpi_type) \
217 const_test(type, mpi_type, MPI_LAND, (rank & 0x1), 0, 0)
218 #define land_test2(type, mpi_type) \
219 const_test(type, mpi_type, MPI_LAND, 1, 1, 0)
220 #define bor_test1(type, mpi_type) \
221 const_test(type, mpi_type, MPI_BOR, (rank & 0x3), ((size < 3) ? size - 1 : 0x3), 0)
222 #define bxor_test1(type, mpi_type) \
223 const_test(type, mpi_type, MPI_BXOR, (rank == 1) * 0xf0, (size > 1) * 0xf0, 0)
224 #define bxor_test2(type, mpi_type) \
225 const_test(type, mpi_type, MPI_BXOR, 0, 0, 0)
226 #define bxor_test3(type, mpi_type) \
227 const_test(type, mpi_type, MPI_BXOR, ~0, (size &0x1) ? ~0 : 0, 0)
229 #define band_test1(type, mpi_type) \
231 DECL_MALLOC_IN_OUT_SOL(type); \
232 if (rank == size-1) { \
233 SET_INDEX_SUM(in, 0); \
236 SET_INDEX_CONST(in, ~0); \
238 SET_INDEX_SUM(sol, 0); \
239 SET_INDEX_CONST(out, 0); \
240 ALLREDUCE_AND_FREE(mpi_type, MPI_BAND, in, out, sol); \
243 #define band_test2(type, mpi_type) \
245 DECL_MALLOC_IN_OUT_SOL(type); \
246 if (rank == size-1) { \
247 SET_INDEX_SUM(in, 0); \
250 SET_INDEX_CONST(in, 0); \
252 SET_INDEX_CONST(sol, 0); \
253 SET_INDEX_CONST(out, 0); \
254 ALLREDUCE_AND_FREE(mpi_type, MPI_BAND, in, out, sol); \
257 #define maxloc_test(type, mpi_type) \
259 DECL_MALLOC_IN_OUT_SOL(type); \
260 SET_INDEX_STRUCT_SUM(in, rank, a); \
261 SET_INDEX_STRUCT_CONST(in, rank, b); \
262 SET_INDEX_STRUCT_SUM(sol, size - 1, a); \
263 SET_INDEX_STRUCT_CONST(sol, size - 1, b); \
264 SET_INDEX_STRUCT_CONST(out, 0, a); \
265 SET_INDEX_STRUCT_CONST(out, -1, b); \
266 STRUCT_ALLREDUCE_AND_FREE(mpi_type, MPI_MAXLOC, in, out, sol); \
269 #define minloc_test(type, mpi_type) \
271 DECL_MALLOC_IN_OUT_SOL(type); \
272 SET_INDEX_STRUCT_SUM(in, rank, a); \
273 SET_INDEX_STRUCT_CONST(in, rank, b); \
274 SET_INDEX_STRUCT_SUM(sol, 0, a); \
275 SET_INDEX_STRUCT_CONST(sol, 0, b); \
276 SET_INDEX_STRUCT_CONST(out, 0, a); \
277 SET_INDEX_STRUCT_CONST(out, -1, b); \
278 STRUCT_ALLREDUCE_AND_FREE(mpi_type, MPI_MINLOC, in, out, sol); \
281 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
282 #define test_types_set_mpi_2_2_integer(op,post) do { \
283 op##_test##post(int8_t, MPI_INT8_T); \
284 op##_test##post(int16_t, MPI_INT16_T); \
285 op##_test##post(int32_t, MPI_INT32_T); \
286 op##_test##post(int64_t, MPI_INT64_T); \
287 op##_test##post(uint8_t, MPI_UINT8_T); \
288 op##_test##post(uint16_t, MPI_UINT16_T); \
289 op##_test##post(uint32_t, MPI_UINT32_T); \
290 op##_test##post(uint64_t, MPI_UINT64_T); \
291 op##_test##post(MPI_Aint, MPI_AINT); \
292 op##_test##post(MPI_Offset, MPI_OFFSET); \
295 #define test_types_set_mpi_2_2_integer(op,post) do { } while (0)
298 #if MTEST_HAVE_MIN_MPI_VERSION(3,0)
299 #define test_types_set_mpi_3_0_integer(op,post) do { \
300 op##_test##post(MPI_Count, MPI_COUNT); \
303 #define test_types_set_mpi_3_0_integer(op,post) do { } while (0)
306 #define test_types_set1(op, post) \
308 op##_test##post(int, MPI_INT); \
309 op##_test##post(long, MPI_LONG); \
310 op##_test##post(short, MPI_SHORT); \
311 op##_test##post(unsigned short, MPI_UNSIGNED_SHORT); \
312 op##_test##post(unsigned, MPI_UNSIGNED); \
313 op##_test##post(unsigned long, MPI_UNSIGNED_LONG); \
314 op##_test##post(unsigned char, MPI_UNSIGNED_CHAR); \
315 test_types_set_mpi_2_2_integer(op,post); \
316 test_types_set_mpi_3_0_integer(op,post); \
319 #define test_types_set2(op, post) \
321 test_types_set1(op, post); \
322 op##_test##post(float, MPI_FLOAT); \
323 op##_test##post(double, MPI_DOUBLE); \
326 #define test_types_set3(op, post) \
328 op##_test##post(unsigned char, MPI_BYTE); \
331 /* Make sure that we test complex and double complex, even if long
332 double complex is not available */
333 #if defined(USE_LONG_DOUBLE_COMPLEX)
335 #if MTEST_HAVE_MIN_MPI_VERSION(2,2) && defined(HAVE_FLOAT__COMPLEX) \
336 && defined(HAVE_DOUBLE__COMPLEX) \
337 && defined(HAVE_LONG_DOUBLE__COMPLEX)
338 #define test_types_set4(op, post) \
340 op##_test##post(float _Complex, MPI_C_FLOAT_COMPLEX); \
341 op##_test##post(double _Complex, MPI_C_DOUBLE_COMPLEX); \
342 if (MPI_C_LONG_DOUBLE_COMPLEX != MPI_DATATYPE_NULL) { \
343 op##_test##post(long double _Complex, MPI_C_LONG_DOUBLE_COMPLEX); \
348 #define test_types_set4(op, post) do { } while (0)
352 #if MTEST_HAVE_MIN_MPI_VERSION(2,2) && defined(HAVE_FLOAT__COMPLEX) \
353 && defined(HAVE_DOUBLE__COMPLEX)
354 #define test_types_set4(op, post) \
356 op##_test##post(float _Complex, MPI_C_FLOAT_COMPLEX); \
357 op##_test##post(double _Complex, MPI_C_DOUBLE_COMPLEX); \
361 #define test_types_set4(op, post) do { } while (0)
364 #endif /* defined(USE_LONG_DOUBLE_COMPLEX) */
366 #if MTEST_HAVE_MIN_MPI_VERSION(2,2) && defined(HAVE__BOOL)
367 #define test_types_set5(op, post) \
369 op##_test##post(_Bool, MPI_C_BOOL); \
373 #define test_types_set5(op, post) do { } while (0)
376 int main(int argc, char **argv)
378 MTest_Init(&argc, &argv);
380 MPI_Comm_size(MPI_COMM_WORLD, &size);
381 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
384 fprintf(stderr, "At least 2 processes required\n");
385 MPI_Abort(MPI_COMM_WORLD, 1);
388 /* Set errors return so that we can provide better information
389 * should a routine reject one of the operand/datatype pairs */
390 MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
393 /* Allow an argument to override the count.
394 * Note that the product tests may fail if the count is very large.
397 count = atoi(argv[1]);
399 fprintf(stderr, "Invalid count argument %s\n", argv[1]);
400 MPI_Abort(MPI_COMM_WORLD, 1);
404 test_types_set2(sum, 1);
405 test_types_set2(prod, 1);
406 test_types_set2(max, 1);
407 test_types_set2(min, 1);
409 test_types_set1(lor, 1);
410 test_types_set1(lor, 2);
412 test_types_set1(lxor, 1);
413 test_types_set1(lxor, 2);
414 test_types_set1(lxor, 3);
416 test_types_set1(land, 1);
417 test_types_set1(land, 2);
419 test_types_set1(bor, 1);
420 test_types_set1(band, 1);
421 test_types_set1(band, 2);
423 test_types_set1(bxor, 1);
424 test_types_set1(bxor, 2);
425 test_types_set1(bxor, 3);
427 test_types_set3(bor, 1);
428 test_types_set3(band, 1);
429 test_types_set3(band, 2);
431 test_types_set3(bxor, 1);
432 test_types_set3(bxor, 2);
433 test_types_set3(bxor, 3);
435 test_types_set4(sum, 1);
436 test_types_set4(prod, 1);
438 test_types_set5(lor, 1);
439 test_types_set5(lor, 2);
440 test_types_set5(lxor, 1);
441 test_types_set5(lxor, 2);
442 test_types_set5(lxor, 3);
443 test_types_set5(land, 1);
444 test_types_set5(land, 2);
446 maxloc_test(struct int_test, MPI_2INT);
447 maxloc_test(struct long_test, MPI_LONG_INT);
448 maxloc_test(struct short_test, MPI_SHORT_INT);
449 maxloc_test(struct float_test, MPI_FLOAT_INT);
450 maxloc_test(struct double_test, MPI_DOUBLE_INT);
452 minloc_test(struct int_test, MPI_2INT);
453 minloc_test(struct long_test, MPI_LONG_INT);
454 minloc_test(struct short_test, MPI_SHORT_INT);
455 minloc_test(struct float_test, MPI_FLOAT_INT);
456 minloc_test(struct double_test, MPI_DOUBLE_INT);
458 MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL);
459 MTest_Finalize(cerrcnt);