Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce the size of partial shared malloc tests.
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / opsum.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_SUM 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;
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
47 #ifndef USE_STRICT_MPI
48     /* char */
49     MTestPrintfMsg(10, "Reduce of MPI_CHAR\n");
50     cinbuf[0] = 1;
51     cinbuf[1] = 0;
52     cinbuf[2] = (rank > 0);
53
54     coutbuf[0] = 0;
55     coutbuf[1] = 1;
56     coutbuf[2] = 1;
57     MPI_Reduce(cinbuf, coutbuf, 3, MPI_CHAR, MPI_SUM, 0, comm);
58     if (rank == 0) {
59         if (size < 128 && coutbuf[0] != size) {
60             errs++;
61             fprintf(stderr, "char SUM(1) test failed\n");
62         }
63         if (size < 128 && coutbuf[1] != 0) {
64             errs++;
65             fprintf(stderr, "char SUM(0) test failed\n");
66         }
67         if (size < 128 && coutbuf[2] != size - 1) {
68             errs++;
69             fprintf(stderr, "char SUM(>) test failed\n");
70         }
71     }
72 #endif /* USE_MPI_STRICT */
73
74     /* signed char */
75     MTestPrintfMsg(10, "Reduce of MPI_SIGNED_CHAR\n");
76     scinbuf[0] = 1;
77     scinbuf[1] = 0;
78     scinbuf[2] = (rank > 0);
79
80     scoutbuf[0] = 0;
81     scoutbuf[1] = 1;
82     scoutbuf[2] = 1;
83     MPI_Reduce(scinbuf, scoutbuf, 3, MPI_SIGNED_CHAR, MPI_SUM, 0, comm);
84     if (rank == 0) {
85         if (size < 128 && scoutbuf[0] != size) {
86             errs++;
87             fprintf(stderr, "signed char SUM(1) test failed\n");
88         }
89         if (size < 128 && scoutbuf[1] != 0) {
90             errs++;
91             fprintf(stderr, "signed char SUM(0) test failed\n");
92         }
93         if (size < 128 && scoutbuf[2] != size - 1) {
94             errs++;
95             fprintf(stderr, "signed char SUM(>) test failed\n");
96         }
97     }
98
99     /* unsigned char */
100     MTestPrintfMsg(10, "Reduce of MPI_UNSIGNED_CHAR\n");
101     ucinbuf[0] = 1;
102     ucinbuf[1] = 0;
103     ucinbuf[2] = (rank > 0);
104
105     ucoutbuf[0] = 0;
106     ucoutbuf[1] = 1;
107     ucoutbuf[2] = 1;
108     MPI_Reduce(ucinbuf, ucoutbuf, 3, MPI_UNSIGNED_CHAR, MPI_SUM, 0, comm);
109     if (rank == 0) {
110         if (size < 128 && ucoutbuf[0] != size) {
111             errs++;
112             fprintf(stderr, "unsigned char SUM(1) test failed\n");
113         }
114         if (size < 128 && ucoutbuf[1]) {
115             errs++;
116             fprintf(stderr, "unsigned char SUM(0) test failed\n");
117         }
118         if (size < 128 && ucoutbuf[2] != size - 1) {
119             errs++;
120             fprintf(stderr, "unsigned char SUM(>) test failed\n");
121         }
122     }
123
124 #ifndef USE_STRICT_MPI
125     /* For some reason, complex is not allowed for sum and prod */
126     if (MPI_DOUBLE_COMPLEX != MPI_DATATYPE_NULL) {
127         int dc;
128 #ifdef HAVE_LONG_DOUBLE
129         ld_complex ldinbuf[3], ldoutbuf[3];
130         MTEST_VG_MEM_INIT(ldinbuf, 3 * sizeof(ldinbuf[0]));
131 #endif
132         /* Must determine which C type matches this Fortran type */
133         MPI_Type_size(MPI_DOUBLE_COMPLEX, &dc);
134         if (dc == sizeof(d_complex)) {
135             MTestPrintfMsg(10, "Reduce of MPI_DOUBLE_COMPLEX\n");
136             /* double complex; may be null if we do not have Fortran support */
137             dinbuf[0].r = 1;
138             dinbuf[1].r = 0;
139             dinbuf[2].r = (rank > 0);
140             dinbuf[0].i = -1;
141             dinbuf[1].i = 0;
142             dinbuf[2].i = -(rank > 0);
143
144             doutbuf[0].r = 0;
145             doutbuf[1].r = 1;
146             doutbuf[2].r = 1;
147             doutbuf[0].i = 0;
148             doutbuf[1].i = 1;
149             doutbuf[2].i = 1;
150             MPI_Reduce(dinbuf, doutbuf, 3, MPI_DOUBLE_COMPLEX, MPI_SUM, 0, comm);
151             if (rank == 0) {
152                 if (doutbuf[0].r != size || doutbuf[0].i != -size) {
153                     errs++;
154                     fprintf(stderr, "double complex SUM(1) test failed\n");
155                 }
156                 if (doutbuf[1].r != 0 || doutbuf[1].i != 0) {
157                     errs++;
158                     fprintf(stderr, "double complex SUM(0) test failed\n");
159                 }
160                 if (doutbuf[2].r != size - 1 || doutbuf[2].i != 1 - size) {
161                     errs++;
162                     fprintf(stderr, "double complex SUM(>) test failed\n");
163                 }
164             }
165         }
166 #ifdef HAVE_LONG_DOUBLE
167         else if (dc == sizeof(ld_complex)) {
168             MTestPrintfMsg(10, "Reduce of MPI_DOUBLE_COMPLEX\n");
169             /* double complex; may be null if we do not have Fortran support */
170             ldinbuf[0].r = 1;
171             ldinbuf[1].r = 0;
172             ldinbuf[2].r = (rank > 0);
173             ldinbuf[0].i = -1;
174             ldinbuf[1].i = 0;
175             ldinbuf[2].i = -(rank > 0);
176
177             ldoutbuf[0].r = 0;
178             ldoutbuf[1].r = 1;
179             ldoutbuf[2].r = 1;
180             ldoutbuf[0].i = 0;
181             ldoutbuf[1].i = 1;
182             ldoutbuf[2].i = 1;
183             MPI_Reduce(ldinbuf, ldoutbuf, 3, MPI_DOUBLE_COMPLEX, MPI_SUM, 0, comm);
184             if (rank == 0) {
185                 if (ldoutbuf[0].r != size || ldoutbuf[0].i != -size) {
186                     errs++;
187                     fprintf(stderr, "double complex SUM(1) test failed\n");
188                 }
189                 if (ldoutbuf[1].r != 0 || ldoutbuf[1].i != 0) {
190                     errs++;
191                     fprintf(stderr, "double complex SUM(0) test failed\n");
192                 }
193                 if (ldoutbuf[2].r != size - 1 || ldoutbuf[2].i != 1 - size) {
194                     errs++;
195                     fprintf(stderr, "double complex SUM(>) test failed\n");
196                 }
197             }
198         }
199 #endif
200         /* Implicitly ignore if there is no matching C type */
201     }
202 #endif /* USE_STRICT_MPI */
203
204 #ifdef HAVE_LONG_DOUBLE
205     {
206         long double ldinbuf[3], ldoutbuf[3];
207         MTEST_VG_MEM_INIT(ldinbuf, 3 * sizeof(ldinbuf[0]));
208         /* long double */
209         ldinbuf[0] = 1;
210         ldinbuf[1] = 0;
211         ldinbuf[2] = (rank > 0);
212
213         ldoutbuf[0] = 0;
214         ldoutbuf[1] = 1;
215         ldoutbuf[2] = 1;
216         if (MPI_LONG_DOUBLE != MPI_DATATYPE_NULL) {
217             MTestPrintfMsg(10, "Reduce of MPI_LONG_DOUBLE\n");
218             MPI_Reduce(ldinbuf, ldoutbuf, 3, MPI_LONG_DOUBLE, MPI_SUM, 0, comm);
219             if (rank == 0) {
220                 if (ldoutbuf[0] != size) {
221                     errs++;
222                     fprintf(stderr, "long double SUM(1) test failed\n");
223                 }
224                 if (ldoutbuf[1] != 0.0) {
225                     errs++;
226                     fprintf(stderr, "long double SUM(0) test failed\n");
227                 }
228                 if (ldoutbuf[2] != size - 1) {
229                     errs++;
230                     fprintf(stderr, "long double SUM(>) test failed\n");
231                 }
232             }
233         }
234     }
235 #endif
236
237 #ifdef HAVE_LONG_LONG
238     {
239         long long llinbuf[3], lloutbuf[3];
240         /* long long */
241         llinbuf[0] = 1;
242         llinbuf[1] = 0;
243         llinbuf[2] = (rank > 0);
244
245         lloutbuf[0] = 0;
246         lloutbuf[1] = 1;
247         lloutbuf[2] = 1;
248         if (MPI_LONG_LONG != MPI_DATATYPE_NULL) {
249             MTestPrintfMsg(10, "Reduce of MPI_LONG_LONG\n");
250             MPI_Reduce(llinbuf, lloutbuf, 3, MPI_LONG_LONG, MPI_SUM, 0, comm);
251             if (rank == 0) {
252                 if (lloutbuf[0] != size) {
253                     errs++;
254                     fprintf(stderr, "long long SUM(1) test failed\n");
255                 }
256                 if (lloutbuf[1] != 0) {
257                     errs++;
258                     fprintf(stderr, "long long SUM(0) test failed\n");
259                 }
260                 if (lloutbuf[2] != size - 1) {
261                     errs++;
262                     fprintf(stderr, "long long SUM(>) test failed\n");
263                 }
264             }
265         }
266     }
267 #endif
268
269     MTest_Finalize(errs);
270     MPI_Finalize();
271     return 0;
272 }