Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix dist
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / opprod.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include "mpi.h"
8 #include "mpitestconf.h"
9 #include <stdio.h>
10 #include "mpitest.h"
11
12 /*
13 static char MTEST_Descrip[] = "Test MPI_PROD operations on optional datatypes dupported by MPICH";
14 */
15
16 typedef struct {
17     double r, i;
18 } d_complex;
19 #ifdef HAVE_LONG_DOUBLE
20 typedef struct {
21     long double r, i;
22 } ld_complex;
23 #endif
24
25 /*
26  * This test looks at the handling of logical and for types that are not
27  * integers or are not required integers (e.g., long long).  MPICH allows
28  * these as well.  A strict MPI test should not include this test.
29  */
30 int main(int argc, char *argv[])
31 {
32     int errs = 0;
33     int rank, size, maxsize, result[6] = { 1, 1, 2, 6, 24, 120 };
34     MPI_Comm comm;
35     char cinbuf[3], coutbuf[3];
36     signed char scinbuf[3], scoutbuf[3];
37     unsigned char ucinbuf[3], ucoutbuf[3];
38     d_complex dinbuf[3], doutbuf[3];
39
40     MTest_Init(&argc, &argv);
41
42     comm = MPI_COMM_WORLD;
43
44     MPI_Comm_rank(comm, &rank);
45     MPI_Comm_size(comm, &size);
46     if (size > 5)
47         maxsize = 5;
48     else
49         maxsize = size;
50
51     /* General forumula: If we multiple the values from 1 to n, the
52      * product is n!.  This grows very fast, so we'll only use the first
53      * five (1! = 1, 2! = 2, 3! = 6, 4! = 24, 5! = 120), with n!
54      * stored in the array result[n] */
55
56 #ifndef USE_STRICT_MPI
57     /* char */
58     MTestPrintfMsg(10, "Reduce of MPI_CHAR\n");
59     cinbuf[0] = (rank < maxsize && rank > 0) ? rank : 1;
60     cinbuf[1] = 0;
61     cinbuf[2] = (rank > 1);
62
63     coutbuf[0] = 0;
64     coutbuf[1] = 1;
65     coutbuf[2] = 1;
66     MPI_Reduce(cinbuf, coutbuf, 3, MPI_CHAR, MPI_PROD, 0, comm);
67     if (rank == 0) {
68         if (coutbuf[0] != (char) result[maxsize - 1]) {
69             errs++;
70             fprintf(stderr, "char PROD(rank) test failed (%d!=%d)\n",
71                     (int) coutbuf[0], (int) result[maxsize]);
72         }
73         if (coutbuf[1]) {
74             errs++;
75             fprintf(stderr, "char PROD(0) test failed\n");
76         }
77         if (size > 1 && coutbuf[2]) {
78             errs++;
79             fprintf(stderr, "char PROD(>) test failed\n");
80         }
81     }
82 #endif /* USE_STRICT_MPI */
83
84     /* signed char */
85     MTestPrintfMsg(10, "Reduce of MPI_SIGNED_CHAR\n");
86     scinbuf[0] = (rank < maxsize && rank > 0) ? rank : 1;
87     scinbuf[1] = 0;
88     scinbuf[2] = (rank > 1);
89
90     scoutbuf[0] = 0;
91     scoutbuf[1] = 1;
92     scoutbuf[2] = 1;
93     MPI_Reduce(scinbuf, scoutbuf, 3, MPI_SIGNED_CHAR, MPI_PROD, 0, comm);
94     if (rank == 0) {
95         if (scoutbuf[0] != (signed char) result[maxsize - 1]) {
96             errs++;
97             fprintf(stderr, "signed char PROD(rank) test failed (%d!=%d)\n",
98                     (int) scoutbuf[0], (int) result[maxsize]);
99         }
100         if (scoutbuf[1]) {
101             errs++;
102             fprintf(stderr, "signed char PROD(0) test failed\n");
103         }
104         if (size > 1 && scoutbuf[2]) {
105             errs++;
106             fprintf(stderr, "signed char PROD(>) test failed\n");
107         }
108     }
109
110     /* unsigned char */
111     MTestPrintfMsg(10, "Reduce of MPI_UNSIGNED_CHAR\n");
112     ucinbuf[0] = (rank < maxsize && rank > 0) ? rank : 1;
113     ucinbuf[1] = 0;
114     ucinbuf[2] = (rank > 0);
115
116     ucoutbuf[0] = 0;
117     ucoutbuf[1] = 1;
118     ucoutbuf[2] = 1;
119     MPI_Reduce(ucinbuf, ucoutbuf, 3, MPI_UNSIGNED_CHAR, MPI_PROD, 0, comm);
120     if (rank == 0) {
121         if (ucoutbuf[0] != (unsigned char) result[maxsize - 1]) {
122             errs++;
123             fprintf(stderr, "unsigned char PROD(rank) test failed\n");
124         }
125         if (ucoutbuf[1]) {
126             errs++;
127             fprintf(stderr, "unsigned char PROD(0) test failed\n");
128         }
129         if (size > 1 && ucoutbuf[2]) {
130             errs++;
131             fprintf(stderr, "unsigned char PROD(>) test failed\n");
132         }
133     }
134
135 #ifndef USE_STRICT_MPI
136     /* For some reason, complex is not allowed for sum and prod */
137     if (MPI_DOUBLE_COMPLEX != MPI_DATATYPE_NULL) {
138         int dc;
139 #ifdef HAVE_LONG_DOUBLE
140         ld_complex ldinbuf[3], ldoutbuf[3];
141         MTEST_VG_MEM_INIT(ldinbuf, 3* sizeof(ldinbuf[0]));
142 #endif
143         /* Must determine which C type matches this Fortran type */
144         MPI_Type_size(MPI_DOUBLE_COMPLEX, &dc);
145         if (dc == sizeof(d_complex)) {
146             /* double complex; may be null if we do not have Fortran support */
147             dinbuf[0].r = (rank < maxsize && rank > 0) ? rank : 1;
148             dinbuf[1].r = 0;
149             dinbuf[2].r = (rank > 0);
150             dinbuf[0].i = 0;
151             dinbuf[1].i = 1;
152             dinbuf[2].i = -(rank > 0);
153
154             doutbuf[0].r = 0;
155             doutbuf[1].r = 1;
156             doutbuf[2].r = 1;
157             doutbuf[0].i = 0;
158             doutbuf[1].i = 1;
159             doutbuf[2].i = 1;
160             MPI_Reduce(dinbuf, doutbuf, 3, MPI_DOUBLE_COMPLEX, MPI_PROD, 0, comm);
161             if (rank == 0) {
162                 double imag, real;
163                 if (doutbuf[0].r != (double) result[maxsize - 1] || doutbuf[0].i != 0) {
164                     errs++;
165                     fprintf(stderr, "double complex PROD(rank) test failed\n");
166                 }
167                 /* Multiplying the imaginary part depends on size mod 4 */
168                 imag = 1.0;
169                 real = 0.0;     /* Make compiler happy */
170                 switch (size % 4) {
171                 case 1:
172                     imag = 1.0;
173                     real = 0.0;
174                     break;
175                 case 2:
176                     imag = 0.0;
177                     real = -1.0;
178                     break;
179                 case 3:
180                     imag = -1.0;
181                     real = 0.0;
182                     break;
183                 case 0:
184                     imag = 0.0;
185                     real = 1.0;
186                     break;
187                 }
188                 if (doutbuf[1].r != real || doutbuf[1].i != imag) {
189                     errs++;
190                     fprintf(stderr, "double complex PROD(i) test failed (%f,%f)!=(%f,%f)\n",
191                             doutbuf[1].r, doutbuf[1].i, real, imag);
192                 }
193                 if (doutbuf[2].r != 0 || doutbuf[2].i != 0) {
194                     errs++;
195                     fprintf(stderr, "double complex PROD(>) test failed\n");
196                 }
197             }
198         }
199 #ifdef HAVE_LONG_DOUBLE
200         else if (dc == sizeof(ld_complex)) {
201             /* double complex; may be null if we do not have Fortran support */
202             ldinbuf[0].r = (rank < maxsize && rank > 0) ? rank : 1;
203             ldinbuf[1].r = 0;
204             ldinbuf[2].r = (rank > 0);
205             ldinbuf[0].i = 0;
206             ldinbuf[1].i = 1;
207             ldinbuf[2].i = -(rank > 0);
208
209             ldoutbuf[0].r = 0;
210             ldoutbuf[1].r = 1;
211             ldoutbuf[2].r = 1;
212             ldoutbuf[0].i = 0;
213             ldoutbuf[1].i = 1;
214             ldoutbuf[2].i = 1;
215             MPI_Reduce(ldinbuf, ldoutbuf, 3, MPI_DOUBLE_COMPLEX, MPI_PROD, 0, comm);
216             if (rank == 0) {
217                 long double imag, real;
218                 if (ldoutbuf[0].r != (double) result[maxsize - 1] || ldoutbuf[0].i != 0) {
219                     errs++;
220                     fprintf(stderr, "double complex PROD(rank) test failed\n");
221                 }
222                 /* Multiplying the imaginary part depends on size mod 4 */
223                 imag = 1.0;
224                 real = 0.0;     /* Make compiler happy */
225                 switch (size % 4) {
226                 case 1:
227                     imag = 1.0;
228                     real = 0.0;
229                     break;
230                 case 2:
231                     imag = 0.0;
232                     real = -1.0;
233                     break;
234                 case 3:
235                     imag = -1.0;
236                     real = 0.0;
237                     break;
238                 case 0:
239                     imag = 0.0;
240                     real = 1.0;
241                     break;
242                 }
243                 if (ldoutbuf[1].r != real || ldoutbuf[1].i != imag) {
244                     errs++;
245                     fprintf(stderr, "double complex PROD(i) test failed (%Lf,%Lf)!=(%Lf,%Lf)\n",
246                             ldoutbuf[1].r, ldoutbuf[1].i, real, imag);
247                 }
248                 if (ldoutbuf[2].r != 0 || ldoutbuf[2].i != 0) {
249                     errs++;
250                     fprintf(stderr, "double complex PROD(>) test failed\n");
251                 }
252             }
253         }
254 #endif /* HAVE_LONG_DOUBLE */
255     }
256 #endif /* USE_STRICT_MPI */
257
258 #ifdef HAVE_LONG_DOUBLE
259     {
260         long double ldinbuf[3], ldoutbuf[3];
261         MTEST_VG_MEM_INIT(ldinbuf, 3 * sizeof(ldinbuf[0]));
262         /* long double */
263         ldinbuf[0] = (rank < maxsize && rank > 0) ? rank : 1;
264         ldinbuf[1] = 0;
265         ldinbuf[2] = (rank > 0);
266
267         ldoutbuf[0] = 0;
268         ldoutbuf[1] = 1;
269         ldoutbuf[2] = 1;
270         if (MPI_LONG_DOUBLE != MPI_DATATYPE_NULL) {
271             MPI_Reduce(ldinbuf, ldoutbuf, 3, MPI_LONG_DOUBLE, MPI_PROD, 0, comm);
272             if (rank == 0) {
273                 if (ldoutbuf[0] != (long double) result[maxsize - 1]) {
274                     errs++;
275                     fprintf(stderr, "long double PROD(rank) test failed\n");
276                 }
277                 if (ldoutbuf[1]) {
278                     errs++;
279                     fprintf(stderr, "long double PROD(0) test failed\n");
280                 }
281                 if (size > 1 && ldoutbuf[2] != 0) {
282                     errs++;
283                     fprintf(stderr, "long double PROD(>) test failed\n");
284                 }
285             }
286         }
287     }
288 #endif /* HAVE_LONG_DOUBLE */
289
290 #ifdef HAVE_LONG_LONG
291     {
292         long long llinbuf[3], lloutbuf[3];
293         /* long long */
294         llinbuf[0] = (rank < maxsize && rank > 0) ? rank : 1;
295         llinbuf[1] = 0;
296         llinbuf[2] = (rank > 0);
297
298         lloutbuf[0] = 0;
299         lloutbuf[1] = 1;
300         lloutbuf[2] = 1;
301         if (MPI_LONG_LONG != MPI_DATATYPE_NULL) {
302             MPI_Reduce(llinbuf, lloutbuf, 3, MPI_LONG_LONG, MPI_PROD, 0, comm);
303             if (rank == 0) {
304                 if (lloutbuf[0] != (long long) result[maxsize - 1]) {
305                     errs++;
306                     fprintf(stderr, "long long PROD(rank) test failed\n");
307                 }
308                 if (lloutbuf[1]) {
309                     errs++;
310                     fprintf(stderr, "long long PROD(0) test failed\n");
311                 }
312                 if (size > 1 && lloutbuf[2]) {
313                     errs++;
314                     fprintf(stderr, "long long PROD(>) test failed\n");
315                 }
316             }
317         }
318     }
319 #endif /* HAVE_LONG_LONG */
320
321     MTest_Finalize(errs);
322     MPI_Finalize();
323     return 0;
324 }