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 SMPI_VARINIT_GLOBAL(count, int);
21 SMPI_VARINIT_GLOBAL(size, int);
22 SMPI_VARINIT_GLOBAL(rank, int);
23 SMPI_VARINIT_GLOBAL(cerrcnt, int);
25 struct int_test { int a; int b; };
26 struct long_test { long a; int b; };
27 struct short_test { short a; int b; };
28 struct float_test { float a; int b; };
29 struct double_test { double a; int b; };
31 #define mpi_op2str(op) \
32 ((op == MPI_SUM) ? "MPI_SUM" : \
33 (op == MPI_PROD) ? "MPI_PROD" : \
34 (op == MPI_MAX) ? "MPI_MAX" : \
35 (op == MPI_MIN) ? "MPI_MIN" : \
36 (op == MPI_LOR) ? "MPI_LOR" : \
37 (op == MPI_LXOR) ? "MPI_LXOR" : \
38 (op == MPI_LAND) ? "MPI_LAND" : \
39 (op == MPI_BOR) ? "MPI_BOR" : \
40 (op == MPI_BAND) ? "MPI_BAND" : \
41 (op == MPI_BXOR) ? "MPI_BXOR" : \
42 (op == MPI_MAXLOC) ? "MPI_MAXLOC" : \
43 (op == MPI_MINLOC) ? "MPI_MINLOC" : \
46 /* calloc to avoid spurious valgrind warnings when "type" has padding bytes */
47 #define DECL_MALLOC_IN_OUT_SOL(type) \
48 type *in, *out, *sol; \
49 in = (type *) calloc(SMPI_VARGET_GLOBAL(count), sizeof(type)); \
50 out = (type *) calloc(SMPI_VARGET_GLOBAL(count), sizeof(type)); \
51 sol = (type *) calloc(SMPI_VARGET_GLOBAL(count), sizeof(type));
53 #define SET_INDEX_CONST(arr, val) \
56 for (i = 0; i < SMPI_VARGET_GLOBAL(count); i++) \
60 #define SET_INDEX_SUM(arr, val) \
63 for (i = 0; i < SMPI_VARGET_GLOBAL(count); i++) \
67 #define SET_INDEX_FACTOR(arr, val) \
70 for (i = 0; i < SMPI_VARGET_GLOBAL(count); i++) \
74 #define SET_INDEX_POWER(arr, val) \
77 for (i = 0; i < SMPI_VARGET_GLOBAL(count); i++) { \
79 for (j = 0; j < (val); j++) \
84 #define ERROR_CHECK_AND_FREE(lerrcnt, mpi_type, mpi_op) \
86 char name[MPI_MAX_OBJECT_NAME] = {0}; \
89 MPI_Type_get_name(mpi_type, name, &len); \
90 fprintf(stderr, "(%d) Error for type %s and op %s\n", \
91 SMPI_VARGET_GLOBAL(rank), name, mpi_op2str(mpi_op)); \
93 free(in); free(out); free(sol); \
96 /* The logic on the error check on MPI_Allreduce assumes that all
97 MPI_Allreduce routines return a failure if any do - this is sufficient
98 for MPI implementations that reject some of the valid op/datatype pairs
99 (and motivated this addition, as some versions of the IBM MPI
100 failed in just this way).
102 #define ALLREDUCE_AND_FREE(mpi_type, mpi_op, in, out, sol) \
104 int i, rc, lerrcnt = 0; \
105 rc = MPI_Allreduce(in, out, SMPI_VARGET_GLOBAL(count), mpi_type, mpi_op, MPI_COMM_WORLD); \
106 if (rc) { lerrcnt++; SMPI_VARGET_GLOBAL(cerrcnt)++; MTestPrintError( rc ); } \
108 for (i = 0; i < SMPI_VARGET_GLOBAL(count); i++) { \
109 if (out[i] != sol[i]) { \
110 SMPI_VARGET_GLOBAL(cerrcnt)++; \
115 ERROR_CHECK_AND_FREE(lerrcnt, mpi_type, mpi_op); \
118 #define STRUCT_ALLREDUCE_AND_FREE(mpi_type, mpi_op, in, out, sol) \
120 int i, rc, lerrcnt = 0; \
121 rc = MPI_Allreduce(in, out, SMPI_VARGET_GLOBAL(count), mpi_type, mpi_op, MPI_COMM_WORLD); \
122 if (rc) { lerrcnt++; SMPI_VARGET_GLOBAL(cerrcnt)++; MTestPrintError( rc ); } \
124 for (i = 0; i < SMPI_VARGET_GLOBAL(count); i++) { \
125 if ((out[i].a != sol[i].a) || (out[i].b != sol[i].b)) { \
126 SMPI_VARGET_GLOBAL(cerrcnt)++; \
131 ERROR_CHECK_AND_FREE(lerrcnt, mpi_type, mpi_op); \
134 #define SET_INDEX_STRUCT_CONST(arr, val, el) \
137 for (i = 0; i < SMPI_VARGET_GLOBAL(count); i++) \
141 #define SET_INDEX_STRUCT_SUM(arr, val, el) \
144 for (i = 0; i < SMPI_VARGET_GLOBAL(count); i++) \
145 arr[i].el = i + (val); \
148 #define sum_test1(type, mpi_type) \
150 DECL_MALLOC_IN_OUT_SOL(type); \
151 SET_INDEX_SUM(in, 0); \
152 SET_INDEX_FACTOR(sol, SMPI_VARGET_GLOBAL(size)); \
153 SET_INDEX_CONST(out, 0); \
154 ALLREDUCE_AND_FREE(mpi_type, MPI_SUM, in, out, sol); \
157 #define prod_test1(type, mpi_type) \
159 DECL_MALLOC_IN_OUT_SOL(type); \
160 SET_INDEX_SUM(in, 0); \
161 SET_INDEX_POWER(sol, SMPI_VARGET_GLOBAL(size)); \
162 SET_INDEX_CONST(out, 0); \
163 ALLREDUCE_AND_FREE(mpi_type, MPI_PROD, in, out, sol); \
166 #define max_test1(type, mpi_type) \
168 DECL_MALLOC_IN_OUT_SOL(type); \
169 SET_INDEX_SUM(in, SMPI_VARGET_GLOBAL(rank)); \
170 SET_INDEX_SUM(sol, SMPI_VARGET_GLOBAL(size) - 1); \
171 SET_INDEX_CONST(out, 0); \
172 ALLREDUCE_AND_FREE(mpi_type, MPI_MAX, in, out, sol); \
175 #define min_test1(type, mpi_type) \
177 DECL_MALLOC_IN_OUT_SOL(type); \
178 SET_INDEX_SUM(in, SMPI_VARGET_GLOBAL(rank)); \
179 SET_INDEX_SUM(sol, 0); \
180 SET_INDEX_CONST(out, 0); \
181 ALLREDUCE_AND_FREE(mpi_type, MPI_MIN, in, out, sol); \
184 #define const_test(type, mpi_type, mpi_op, val1, val2, val3) \
186 DECL_MALLOC_IN_OUT_SOL(type); \
187 SET_INDEX_CONST(in, (val1)); \
188 SET_INDEX_CONST(sol, (val2)); \
189 SET_INDEX_CONST(out, (val3)); \
190 ALLREDUCE_AND_FREE(mpi_type, mpi_op, in, out, sol); \
193 #define lor_test1(type, mpi_type) \
194 const_test(type, mpi_type, MPI_LOR, (SMPI_VARGET_GLOBAL(rank) & 0x1), (SMPI_VARGET_GLOBAL(size) > 1), 0)
195 #define lor_test2(type, mpi_type) \
196 const_test(type, mpi_type, MPI_LOR, 0, 0, 0)
197 #define lxor_test1(type, mpi_type) \
198 const_test(type, mpi_type, MPI_LXOR, (SMPI_VARGET_GLOBAL(rank) == 1), (SMPI_VARGET_GLOBAL(size) > 1), 0)
199 #define lxor_test2(type, mpi_type) \
200 const_test(type, mpi_type, MPI_LXOR, 0, 0, 0)
201 #define lxor_test3(type, mpi_type) \
202 const_test(type, mpi_type, MPI_LXOR, 1, (SMPI_VARGET_GLOBAL(size) & 0x1), 0)
203 #define land_test1(type, mpi_type) \
204 const_test(type, mpi_type, MPI_LAND, (SMPI_VARGET_GLOBAL(rank) & 0x1), 0, 0)
205 #define land_test2(type, mpi_type) \
206 const_test(type, mpi_type, MPI_LAND, 1, 1, 0)
207 #define bor_test1(type, mpi_type) \
208 const_test(type, mpi_type, MPI_BOR, (SMPI_VARGET_GLOBAL(rank) & 0x3), ((SMPI_VARGET_GLOBAL(size) < 3) ? SMPI_VARGET_GLOBAL(size) - 1 : 0x3), 0)
209 #define bxor_test1(type, mpi_type) \
210 const_test(type, mpi_type, MPI_BXOR, (SMPI_VARGET_GLOBAL(rank) == 1) * 0xf0, (SMPI_VARGET_GLOBAL(size) > 1) * 0xf0, 0)
211 #define bxor_test2(type, mpi_type) \
212 const_test(type, mpi_type, MPI_BXOR, 0, 0, 0)
213 #define bxor_test3(type, mpi_type) \
214 const_test(type, mpi_type, MPI_BXOR, ~0, (SMPI_VARGET_GLOBAL(size) &0x1) ? ~0 : 0, 0)
216 #define band_test1(type, mpi_type) \
218 DECL_MALLOC_IN_OUT_SOL(type); \
219 if (SMPI_VARGET_GLOBAL(rank) == SMPI_VARGET_GLOBAL(size)-1) { \
220 SET_INDEX_SUM(in, 0); \
223 SET_INDEX_CONST(in, ~0); \
225 SET_INDEX_SUM(sol, 0); \
226 SET_INDEX_CONST(out, 0); \
227 ALLREDUCE_AND_FREE(mpi_type, MPI_BAND, in, out, sol); \
230 #define band_test2(type, mpi_type) \
232 DECL_MALLOC_IN_OUT_SOL(type); \
233 if (SMPI_VARGET_GLOBAL(rank) == SMPI_VARGET_GLOBAL(size)-1) { \
234 SET_INDEX_SUM(in, 0); \
237 SET_INDEX_CONST(in, 0); \
239 SET_INDEX_CONST(sol, 0); \
240 SET_INDEX_CONST(out, 0); \
241 ALLREDUCE_AND_FREE(mpi_type, MPI_BAND, in, out, sol); \
244 #define maxloc_test(type, mpi_type) \
246 DECL_MALLOC_IN_OUT_SOL(type); \
247 SET_INDEX_STRUCT_SUM(in, SMPI_VARGET_GLOBAL(rank), a); \
248 SET_INDEX_STRUCT_CONST(in, SMPI_VARGET_GLOBAL(rank), b); \
249 SET_INDEX_STRUCT_SUM(sol, SMPI_VARGET_GLOBAL(size) - 1, a); \
250 SET_INDEX_STRUCT_CONST(sol, SMPI_VARGET_GLOBAL(size) - 1, b); \
251 SET_INDEX_STRUCT_CONST(out, 0, a); \
252 SET_INDEX_STRUCT_CONST(out, -1, b); \
253 STRUCT_ALLREDUCE_AND_FREE(mpi_type, MPI_MAXLOC, in, out, sol); \
256 #define minloc_test(type, mpi_type) \
258 DECL_MALLOC_IN_OUT_SOL(type); \
259 SET_INDEX_STRUCT_SUM(in, SMPI_VARGET_GLOBAL(rank), a); \
260 SET_INDEX_STRUCT_CONST(in, SMPI_VARGET_GLOBAL(rank), b); \
261 SET_INDEX_STRUCT_SUM(sol, 0, a); \
262 SET_INDEX_STRUCT_CONST(sol, 0, b); \
263 SET_INDEX_STRUCT_CONST(out, 0, a); \
264 SET_INDEX_STRUCT_CONST(out, -1, b); \
265 STRUCT_ALLREDUCE_AND_FREE(mpi_type, MPI_MINLOC, in, out, sol); \
268 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
269 #define test_types_set_mpi_2_2_integer(op,post) do { \
270 op##_test##post(int8_t, MPI_INT8_T); \
271 op##_test##post(int16_t, MPI_INT16_T); \
272 op##_test##post(int32_t, MPI_INT32_T); \
273 op##_test##post(int64_t, MPI_INT64_T); \
274 op##_test##post(uint8_t, MPI_UINT8_T); \
275 op##_test##post(uint16_t, MPI_UINT16_T); \
276 op##_test##post(uint32_t, MPI_UINT32_T); \
277 op##_test##post(uint64_t, MPI_UINT64_T); \
278 op##_test##post(MPI_Aint, MPI_AINT); \
279 op##_test##post(MPI_Offset, MPI_OFFSET); \
282 #define test_types_set_mpi_2_2_integer(op,post) do { } while (0)
285 #if MTEST_HAVE_MIN_MPI_VERSION(3,0)
286 #define test_types_set_mpi_3_0_integer(op,post) do { \
287 op##_test##post(MPI_SMPI_VARGET_GLOBAL(count), MPI_SMPI_VARGET_GLOBAL(count)); \
290 #define test_types_set_mpi_3_0_integer(op,post) do { } while (0)
293 #define test_types_set1(op, post) \
295 op##_test##post(int, MPI_INT); \
296 op##_test##post(long, MPI_LONG); \
297 op##_test##post(short, MPI_SHORT); \
298 op##_test##post(unsigned short, MPI_UNSIGNED_SHORT); \
299 op##_test##post(unsigned, MPI_UNSIGNED); \
300 op##_test##post(unsigned long, MPI_UNSIGNED_LONG); \
301 op##_test##post(unsigned char, MPI_UNSIGNED_CHAR); \
302 test_types_set_mpi_2_2_integer(op,post); \
303 test_types_set_mpi_3_0_integer(op,post); \
306 #define test_types_set2(op, post) \
308 test_types_set1(op, post); \
309 op##_test##post(float, MPI_FLOAT); \
310 op##_test##post(double, MPI_DOUBLE); \
313 #define test_types_set3(op, post) \
315 op##_test##post(unsigned char, MPI_BYTE); \
318 /* Make sure that we test complex and double complex, even if long
319 double complex is not available */
320 #if defined(USE_LONG_DOUBLE_COMPLEX)
322 #if MTEST_HAVE_MIN_MPI_VERSION(2,2) && defined(HAVE_FLOAT__COMPLEX) \
323 && defined(HAVE_DOUBLE__COMPLEX) \
324 && defined(HAVE_LONG_DOUBLE__COMPLEX)
325 #define test_types_set4(op, post) \
327 op##_test##post(float _Complex, MPI_C_FLOAT_COMPLEX); \
328 op##_test##post(double _Complex, MPI_C_DOUBLE_COMPLEX); \
329 if (MPI_C_LONG_DOUBLE_COMPLEX != MPI_DATATYPE_NULL) { \
330 op##_test##post(long double _Complex, MPI_C_LONG_DOUBLE_COMPLEX); \
335 #define test_types_set4(op, post) do { } while (0)
339 #if MTEST_HAVE_MIN_MPI_VERSION(2,2) && defined(HAVE_FLOAT__COMPLEX) \
340 && defined(HAVE_DOUBLE__COMPLEX)
341 #define test_types_set4(op, post) \
343 op##_test##post(float _Complex, MPI_C_FLOAT_COMPLEX); \
344 op##_test##post(double _Complex, MPI_C_DOUBLE_COMPLEX); \
348 #define test_types_set4(op, post) do { } while (0)
351 #endif /* defined(USE_LONG_DOUBLE_COMPLEX) */
353 #if MTEST_HAVE_MIN_MPI_VERSION(2,2) && defined(HAVE__BOOL)
354 #define test_types_set5(op, post) \
356 op##_test##post(_Bool, MPI_C_BOOL); \
360 #define test_types_set5(op, post) do { } while (0)
363 int main( int argc, char **argv )
365 MTest_Init( &argc, &argv );
367 MPI_Comm_size(MPI_COMM_WORLD, &SMPI_VARGET_GLOBAL(size));
368 MPI_Comm_rank(MPI_COMM_WORLD, &SMPI_VARGET_GLOBAL(rank));
370 if (SMPI_VARGET_GLOBAL(size) < 2) {
371 fprintf( stderr, "At least 2 processes required\n" );
372 MPI_Abort( MPI_COMM_WORLD, 1 );
376 /* Set errors return so that we can provide better information
377 should a routine reject one of the operand/datatype pairs */
378 MPI_Errhandler_set( MPI_COMM_WORLD, MPI_ERRORS_RETURN );
380 SMPI_VARGET_GLOBAL(count) = 10;
381 /* Allow an argument to override the count.
382 Note that the product tests may fail if the count is very large.
385 SMPI_VARGET_GLOBAL(count) = atoi( argv[1] );
386 if (SMPI_VARGET_GLOBAL(count) <= 0) {
387 fprintf( stderr, "Invalid count argument %s\n", argv[1] );
388 MPI_Abort( MPI_COMM_WORLD, 1 );
393 test_types_set2(sum, 1);
394 test_types_set2(prod, 1);
395 test_types_set2(max, 1);
396 test_types_set2(min, 1);
398 test_types_set1(lor, 1);
399 test_types_set1(lor, 2);
401 test_types_set1(lxor, 1);
402 test_types_set1(lxor, 2);
403 test_types_set1(lxor, 3);
405 test_types_set1(land, 1);
406 test_types_set1(land, 2);
408 test_types_set1(bor, 1);
409 test_types_set1(band, 1);
410 test_types_set1(band, 2);
412 test_types_set1(bxor, 1);
413 test_types_set1(bxor, 2);
414 test_types_set1(bxor, 3);
416 test_types_set3(bor, 1);
417 test_types_set3(band, 1);
418 test_types_set3(band, 2);
420 test_types_set3(bxor, 1);
421 test_types_set3(bxor, 2);
422 test_types_set3(bxor, 3);
424 test_types_set4(sum, 1);
425 test_types_set4(prod, 1);
427 test_types_set5(lor, 1);
428 test_types_set5(lor, 2);
429 test_types_set5(lxor, 1);
430 test_types_set5(lxor, 2);
431 test_types_set5(lxor, 3);
432 test_types_set5(land, 1);
433 test_types_set5(land, 2);
435 maxloc_test(struct int_test, MPI_2INT);
436 maxloc_test(struct long_test, MPI_LONG_INT);
437 maxloc_test(struct short_test, MPI_SHORT_INT);
438 maxloc_test(struct float_test, MPI_FLOAT_INT);
439 maxloc_test(struct double_test, MPI_DOUBLE_INT);
441 minloc_test(struct int_test, MPI_2INT);
442 minloc_test(struct long_test, MPI_LONG_INT);
443 minloc_test(struct short_test, MPI_SHORT_INT);
444 minloc_test(struct float_test, MPI_FLOAT_INT);
445 minloc_test(struct double_test, MPI_DOUBLE_INT);
447 MPI_Errhandler_set( MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL );
448 MTest_Finalize( SMPI_VARGET_GLOBAL(cerrcnt) );