Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #59 from fabienchaix/master
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / allred.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
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.
9  */
10
11 #include "mpi.h"
12 #include "mpitest.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #ifdef HAVE_STDINT_H
17 #include <stdint.h>
18 #endif
19
20 SMPI_VARINIT_GLOBAL(count, int);
21 SMPI_VARINIT_GLOBAL(size, int);
22 SMPI_VARINIT_GLOBAL(rank, int);
23 SMPI_VARINIT_GLOBAL(cerrcnt, int);
24
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; };
30
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" : \
44      "MPI_NO_OP")
45
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));
52
53 #define SET_INDEX_CONST(arr, val)               \
54     {                                           \
55         int i;                                  \
56         for (i = 0; i < SMPI_VARGET_GLOBAL(count); i++)             \
57             arr[i] = val;                       \
58     }
59
60 #define SET_INDEX_SUM(arr, val)                 \
61     {                                           \
62         int i;                                  \
63         for (i = 0; i < SMPI_VARGET_GLOBAL(count); i++)             \
64             arr[i] = i + val;                   \
65     }
66
67 #define SET_INDEX_FACTOR(arr, val)              \
68     {                                           \
69         int i;                                  \
70         for (i = 0; i < SMPI_VARGET_GLOBAL(count); i++)             \
71             arr[i] = i * (val);                 \
72     }
73
74 #define SET_INDEX_POWER(arr, val)               \
75     {                                           \
76         int i, j;                               \
77         for (i = 0; i < SMPI_VARGET_GLOBAL(count); i++) {           \
78             (arr)[i] = 1;                       \
79             for (j = 0; j < (val); j++)         \
80                 arr[i] *= i;                    \
81         }                                       \
82     }
83
84 #define ERROR_CHECK_AND_FREE(lerrcnt, mpi_type, mpi_op)                 \
85     do {                                                                \
86         char name[MPI_MAX_OBJECT_NAME] = {0};                           \
87         int len = 0;                                                    \
88         if (lerrcnt) {                                                  \
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));                    \
92         }                                                               \
93         free(in); free(out); free(sol);                                 \
94     } while(0)
95
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).
101 */
102 #define ALLREDUCE_AND_FREE(mpi_type, mpi_op, in, out, sol)              \
103     {                                                                   \
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 ); }        \
107         else {                                                          \
108           for (i = 0; i < SMPI_VARGET_GLOBAL(count); i++) {                                   \
109               if (out[i] != sol[i]) {                                     \
110                   SMPI_VARGET_GLOBAL(cerrcnt)++;                                              \
111                   lerrcnt++;                                              \
112               }                                                           \
113            }                                                              \
114         }                                                               \
115         ERROR_CHECK_AND_FREE(lerrcnt, mpi_type, mpi_op);                \
116     }
117
118 #define STRUCT_ALLREDUCE_AND_FREE(mpi_type, mpi_op, in, out, sol)       \
119     {                                                                   \
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 ); }        \
123         else {                                                            \
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)++;                                              \
127                   lerrcnt++;                                              \
128               }                                                           \
129             }                                                             \
130         }                                                               \
131         ERROR_CHECK_AND_FREE(lerrcnt, mpi_type, mpi_op);                \
132     }
133
134 #define SET_INDEX_STRUCT_CONST(arr, val, el)                    \
135     {                                                           \
136         int i;                                                  \
137         for (i = 0; i < SMPI_VARGET_GLOBAL(count); i++)                             \
138             arr[i].el = val;                                    \
139     }
140
141 #define SET_INDEX_STRUCT_SUM(arr, val, el)                      \
142     {                                                           \
143         int i;                                                  \
144         for (i = 0; i < SMPI_VARGET_GLOBAL(count); i++)                             \
145             arr[i].el = i + (val);                              \
146     }
147
148 #define sum_test1(type, mpi_type)                                       \
149     {                                                                   \
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);            \
155     }
156
157 #define prod_test1(type, mpi_type)                                      \
158     {                                                                   \
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);           \
164     }
165
166 #define max_test1(type, mpi_type)                                       \
167     {                                                                   \
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);            \
173     }
174
175 #define min_test1(type, mpi_type)                                       \
176     {                                                                   \
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);            \
182     }
183
184 #define const_test(type, mpi_type, mpi_op, val1, val2, val3)            \
185     {                                                                   \
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);             \
191     }
192
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)
215
216 #define band_test1(type, mpi_type)                                      \
217     {                                                                   \
218         DECL_MALLOC_IN_OUT_SOL(type);                                   \
219         if (SMPI_VARGET_GLOBAL(rank) == SMPI_VARGET_GLOBAL(size)-1) {                                           \
220             SET_INDEX_SUM(in, 0);                                       \
221         }                                                               \
222         else {                                                          \
223             SET_INDEX_CONST(in, ~0);                                    \
224         }                                                               \
225         SET_INDEX_SUM(sol, 0);                                          \
226         SET_INDEX_CONST(out, 0);                                        \
227         ALLREDUCE_AND_FREE(mpi_type, MPI_BAND, in, out, sol);           \
228     }
229
230 #define band_test2(type, mpi_type)                                      \
231     {                                                                   \
232         DECL_MALLOC_IN_OUT_SOL(type);                                   \
233         if (SMPI_VARGET_GLOBAL(rank) == SMPI_VARGET_GLOBAL(size)-1) {                                           \
234             SET_INDEX_SUM(in, 0);                                       \
235         }                                                               \
236         else {                                                          \
237             SET_INDEX_CONST(in, 0);                                     \
238         }                                                               \
239         SET_INDEX_CONST(sol, 0);                                        \
240         SET_INDEX_CONST(out, 0);                                        \
241         ALLREDUCE_AND_FREE(mpi_type, MPI_BAND, in, out, sol);           \
242     }
243
244 #define maxloc_test(type, mpi_type)                                     \
245     {                                                                   \
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);   \
254     }
255
256 #define minloc_test(type, mpi_type)                                     \
257     {                                                                   \
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);  \
266     }
267
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);                    \
280     } while (0)
281 #else
282 #define test_types_set_mpi_2_2_integer(op,post) do { } while (0)
283 #endif
284
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));                      \
288     } while (0)
289 #else
290 #define test_types_set_mpi_3_0_integer(op,post) do { } while (0)
291 #endif
292
293 #define test_types_set1(op, post)                                   \
294     {                                                               \
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);                    \
304     }
305
306 #define test_types_set2(op, post)               \
307     {                                           \
308         test_types_set1(op, post);              \
309         op##_test##post(float, MPI_FLOAT);      \
310         op##_test##post(double, MPI_DOUBLE);    \
311     }
312
313 #define test_types_set3(op, post)                                   \
314     {                                                               \
315         op##_test##post(unsigned char, MPI_BYTE);                   \
316     }
317
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)
321
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)                                             \
326     do {                                                                      \
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); \
331         }                                                                     \
332     } while (0)
333
334 #else
335 #define test_types_set4(op, post) do { } while (0)
336 #endif
337 #else
338
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)                                         \
342     do {                                                                  \
343         op##_test##post(float _Complex, MPI_C_FLOAT_COMPLEX);             \
344         op##_test##post(double _Complex, MPI_C_DOUBLE_COMPLEX);           \
345     } while (0)
346
347 #else
348 #define test_types_set4(op, post) do { } while (0)
349 #endif
350
351 #endif /* defined(USE_LONG_DOUBLE_COMPLEX) */
352
353 #if MTEST_HAVE_MIN_MPI_VERSION(2,2) && defined(HAVE__BOOL)
354 #define test_types_set5(op, post)           \
355     do {                                    \
356         op##_test##post(_Bool, MPI_C_BOOL); \
357     } while (0)
358
359 #else
360 #define test_types_set5(op, post) do { } while (0)
361 #endif
362
363 int main( int argc, char **argv )
364 {
365     MTest_Init( &argc, &argv );
366
367     MPI_Comm_size(MPI_COMM_WORLD, &SMPI_VARGET_GLOBAL(size));
368     MPI_Comm_rank(MPI_COMM_WORLD, &SMPI_VARGET_GLOBAL(rank));
369
370     if (SMPI_VARGET_GLOBAL(size) < 2) {
371         fprintf( stderr, "At least 2 processes required\n" );
372         MPI_Abort( MPI_COMM_WORLD, 1 );
373         exit(1);
374     }
375
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 );
379
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.
383      */
384     if (argc >= 2) {
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 );
389             exit(1);
390         }
391     }
392
393     test_types_set2(sum, 1);
394     test_types_set2(prod, 1);
395     test_types_set2(max, 1);
396     test_types_set2(min, 1);
397
398     test_types_set1(lor, 1);
399     test_types_set1(lor, 2);
400
401     test_types_set1(lxor, 1);
402     test_types_set1(lxor, 2);
403     test_types_set1(lxor, 3);
404
405     test_types_set1(land, 1);
406     test_types_set1(land, 2);
407
408     test_types_set1(bor, 1);
409     test_types_set1(band, 1);
410     test_types_set1(band, 2);
411
412     test_types_set1(bxor, 1);
413     test_types_set1(bxor, 2);
414     test_types_set1(bxor, 3);
415
416     test_types_set3(bor, 1);
417     test_types_set3(band, 1);
418     test_types_set3(band, 2);
419
420     test_types_set3(bxor, 1);
421     test_types_set3(bxor, 2);
422     test_types_set3(bxor, 3);
423
424     test_types_set4(sum, 1);
425     test_types_set4(prod, 1);
426
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);
434
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);
440
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);
446
447     MPI_Errhandler_set( MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL );
448     MTest_Finalize( SMPI_VARGET_GLOBAL(cerrcnt) );
449     MPI_Finalize();
450     return 0;
451 }