Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Upgrade coll mpich testlist to new mpich
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / opmaxloc.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 <string.h>
11 #include "mpitest.h"
12
13 /*
14 static char MTEST_Descrip[] = "Test MPI_MAXLOC operations on datatypes dupported by MPICH";
15 */
16
17 /*
18  * This test looks at the handling of char and types that  are not required
19  * integers (e.g., long long).  MPICH allows
20  * these as well.  A strict MPI test should not include this test.
21  *
22  * The rule on max loc is that if there is a tie in the value, the minimum
23  * rank is used (see 4.9.3 in the MPI-1 standard)
24  */
25 int main(int argc, char *argv[])
26 {
27     int errs = 0;
28     int rank, size;
29     MPI_Comm comm;
30
31     MTest_Init(&argc, &argv);
32
33     comm = MPI_COMM_WORLD;
34
35     MPI_Comm_rank(comm, &rank);
36     MPI_Comm_size(comm, &size);
37
38     /* 2 int */
39     {
40         struct twoint {
41             int val;
42             int loc;
43         } cinbuf[3], coutbuf[3];
44
45         cinbuf[0].val = 1;
46         cinbuf[0].loc = rank;
47         cinbuf[1].val = 0;
48         cinbuf[1].loc = rank;
49         cinbuf[2].val = rank;
50         cinbuf[2].loc = rank;
51
52         coutbuf[0].val = 0;
53         coutbuf[0].loc = -1;
54         coutbuf[1].val = 1;
55         coutbuf[1].loc = -1;
56         coutbuf[2].val = 1;
57         coutbuf[2].loc = -1;
58         MPI_Reduce(cinbuf, coutbuf, 3, MPI_2INT, MPI_MAXLOC, 0, comm);
59         if (rank == 0) {
60             if (coutbuf[0].val != 1 || coutbuf[0].loc != 0) {
61                 errs++;
62                 fprintf(stderr, "2int MAXLOC(1) test failed\n");
63             }
64             if (coutbuf[1].val != 0) {
65                 errs++;
66                 fprintf(stderr, "2int MAXLOC(0) test failed, value = %d, should be zero\n",
67                         coutbuf[1].val);
68             }
69             if (coutbuf[1].loc != 0) {
70                 errs++;
71                 fprintf(stderr,
72                         "2int MAXLOC(0) test failed, location of max = %d, should be zero\n",
73                         coutbuf[1].loc);
74             }
75             if (coutbuf[2].val != size - 1 || coutbuf[2].loc != size - 1) {
76                 errs++;
77                 fprintf(stderr, "2int MAXLOC(>) test failed\n");
78             }
79         }
80     }
81
82     /* float int */
83     {
84         struct floatint {
85             float val;
86             int loc;
87         } cinbuf[3], coutbuf[3];
88
89         cinbuf[0].val = 1;
90         cinbuf[0].loc = rank;
91         cinbuf[1].val = 0;
92         cinbuf[1].loc = rank;
93         cinbuf[2].val = (float) rank;
94         cinbuf[2].loc = rank;
95
96         coutbuf[0].val = 0;
97         coutbuf[0].loc = -1;
98         coutbuf[1].val = 1;
99         coutbuf[1].loc = -1;
100         coutbuf[2].val = 1;
101         coutbuf[2].loc = -1;
102         MPI_Reduce(cinbuf, coutbuf, 3, MPI_FLOAT_INT, MPI_MAXLOC, 0, comm);
103         if (rank == 0) {
104             if (coutbuf[0].val != 1 || coutbuf[0].loc != 0) {
105                 errs++;
106                 fprintf(stderr, "float-int MAXLOC(1) test failed\n");
107             }
108             if (coutbuf[1].val != 0) {
109                 errs++;
110                 fprintf(stderr, "float-int MAXLOC(0) test failed, value = %f, should be zero\n",
111                         coutbuf[1].val);
112             }
113             if (coutbuf[1].loc != 0) {
114                 errs++;
115                 fprintf(stderr,
116                         "float-int MAXLOC(0) test failed, location of max = %d, should be zero\n",
117                         coutbuf[1].loc);
118             }
119             if (coutbuf[2].val != size - 1 || coutbuf[2].loc != size - 1) {
120                 errs++;
121                 fprintf(stderr, "float-int MAXLOC(>) test failed\n");
122             }
123         }
124     }
125
126     /* long int */
127     {
128         struct longint {
129             long val;
130             int loc;
131         } cinbuf[3], coutbuf[3];
132
133         cinbuf[0].val = 1;
134         cinbuf[0].loc = rank;
135         cinbuf[1].val = 0;
136         cinbuf[1].loc = rank;
137         cinbuf[2].val = rank;
138         cinbuf[2].loc = rank;
139
140         coutbuf[0].val = 0;
141         coutbuf[0].loc = -1;
142         coutbuf[1].val = 1;
143         coutbuf[1].loc = -1;
144         coutbuf[2].val = 1;
145         coutbuf[2].loc = -1;
146         MPI_Reduce(cinbuf, coutbuf, 3, MPI_LONG_INT, MPI_MAXLOC, 0, comm);
147         if (rank == 0) {
148             if (coutbuf[0].val != 1 || coutbuf[0].loc != 0) {
149                 errs++;
150                 fprintf(stderr, "long-int MAXLOC(1) test failed\n");
151             }
152             if (coutbuf[1].val != 0) {
153                 errs++;
154                 fprintf(stderr, "long-int MAXLOC(0) test failed, value = %ld, should be zero\n",
155                         coutbuf[1].val);
156             }
157             if (coutbuf[1].loc != 0) {
158                 errs++;
159                 fprintf(stderr,
160                         "long-int MAXLOC(0) test failed, location of max = %d, should be zero\n",
161                         coutbuf[1].loc);
162             }
163             if (coutbuf[2].val != size - 1 || coutbuf[2].loc != size - 1) {
164                 errs++;
165                 fprintf(stderr, "long-int MAXLOC(>) test failed\n");
166             }
167         }
168     }
169
170     /* short int */
171     {
172         struct shortint {
173             short val;
174             int loc;
175         } cinbuf[3], coutbuf[3];
176
177         cinbuf[0].val = 1;
178         cinbuf[0].loc = rank;
179         cinbuf[1].val = 0;
180         cinbuf[1].loc = rank;
181         cinbuf[2].val = rank;
182         cinbuf[2].loc = rank;
183
184         coutbuf[0].val = 0;
185         coutbuf[0].loc = -1;
186         coutbuf[1].val = 1;
187         coutbuf[1].loc = -1;
188         coutbuf[2].val = 1;
189         coutbuf[2].loc = -1;
190         MPI_Reduce(cinbuf, coutbuf, 3, MPI_SHORT_INT, MPI_MAXLOC, 0, comm);
191         if (rank == 0) {
192             if (coutbuf[0].val != 1 || coutbuf[0].loc != 0) {
193                 errs++;
194                 fprintf(stderr, "short-int MAXLOC(1) test failed\n");
195             }
196             if (coutbuf[1].val != 0) {
197                 errs++;
198                 fprintf(stderr, "short-int MAXLOC(0) test failed, value = %d, should be zero\n",
199                         coutbuf[1].val);
200             }
201             if (coutbuf[1].loc != 0) {
202                 errs++;
203                 fprintf(stderr,
204                         "short-int MAXLOC(0) test failed, location of max = %d, should be zero\n",
205                         coutbuf[1].loc);
206             }
207             if (coutbuf[2].val != size - 1) {
208                 errs++;
209                 fprintf(stderr, "short-int MAXLOC(>) test failed, value = %d, should be %d\n",
210                         coutbuf[2].val, size - 1);
211             }
212             if (coutbuf[2].loc != size - 1) {
213                 errs++;
214                 fprintf(stderr,
215                         "short-int MAXLOC(>) test failed, location of max = %d, should be %d\n",
216                         coutbuf[2].loc, size - 1);
217             }
218         }
219     }
220
221     /* double int */
222     {
223         struct doubleint {
224             double val;
225             int loc;
226         } cinbuf[3], coutbuf[3];
227
228         cinbuf[0].val = 1;
229         cinbuf[0].loc = rank;
230         cinbuf[1].val = 0;
231         cinbuf[1].loc = rank;
232         cinbuf[2].val = rank;
233         cinbuf[2].loc = rank;
234
235         coutbuf[0].val = 0;
236         coutbuf[0].loc = -1;
237         coutbuf[1].val = 1;
238         coutbuf[1].loc = -1;
239         coutbuf[2].val = 1;
240         coutbuf[2].loc = -1;
241         MPI_Reduce(cinbuf, coutbuf, 3, MPI_DOUBLE_INT, MPI_MAXLOC, 0, comm);
242         if (rank == 0) {
243             if (coutbuf[0].val != 1 || coutbuf[0].loc != 0) {
244                 errs++;
245                 fprintf(stderr, "double-int MAXLOC(1) test failed\n");
246             }
247             if (coutbuf[1].val != 0) {
248                 errs++;
249                 fprintf(stderr, "double-int MAXLOC(0) test failed, value = %lf, should be zero\n",
250                         coutbuf[1].val);
251             }
252             if (coutbuf[1].loc != 0) {
253                 errs++;
254                 fprintf(stderr,
255                         "double-int MAXLOC(0) test failed, location of max = %d, should be zero\n",
256                         coutbuf[1].loc);
257             }
258             if (coutbuf[2].val != size - 1 || coutbuf[2].loc != size - 1) {
259                 errs++;
260                 fprintf(stderr, "double-int MAXLOC(>) test failed\n");
261             }
262         }
263     }
264
265 #ifdef HAVE_LONG_DOUBLE
266     /* long double int */
267     {
268         struct longdoubleint {
269             long double val;
270             int loc;
271         } cinbuf[3], coutbuf[3];
272
273         /* avoid valgrind warnings about padding bytes in the long double */
274         memset(&cinbuf[0], 0, sizeof(cinbuf));
275         memset(&coutbuf[0], 0, sizeof(coutbuf));
276
277         cinbuf[0].val = 1;
278         cinbuf[0].loc = rank;
279         cinbuf[1].val = 0;
280         cinbuf[1].loc = rank;
281         cinbuf[2].val = rank;
282         cinbuf[2].loc = rank;
283
284         coutbuf[0].val = 0;
285         coutbuf[0].loc = -1;
286         coutbuf[1].val = 1;
287         coutbuf[1].loc = -1;
288         coutbuf[2].val = 1;
289         coutbuf[2].loc = -1;
290         if (MPI_LONG_DOUBLE != MPI_DATATYPE_NULL) {
291             MPI_Reduce(cinbuf, coutbuf, 3, MPI_LONG_DOUBLE_INT, MPI_MAXLOC, 0, comm);
292             if (rank == 0) {
293                 if (coutbuf[0].val != 1 || coutbuf[0].loc != 0) {
294                     errs++;
295                     fprintf(stderr, "long double-int MAXLOC(1) test failed\n");
296                 }
297                 if (coutbuf[1].val != 0) {
298                     errs++;
299                     fprintf(stderr,
300                             "long double-int MAXLOC(0) test failed, value = %lf, should be zero\n",
301                             (double) coutbuf[1].val);
302                 }
303                 if (coutbuf[1].loc != 0) {
304                     errs++;
305                     fprintf(stderr,
306                             "long double-int MAXLOC(0) test failed, location of max = %d, should be zero\n",
307                             coutbuf[1].loc);
308                 }
309                 if (coutbuf[2].val != size - 1) {
310                     errs++;
311                     fprintf(stderr,
312                             "long double-int MAXLOC(>) test failed, value = %lf, should be %d\n",
313                             (double) coutbuf[2].val, size - 1);
314                 }
315                 if (coutbuf[2].loc != size - 1) {
316                     errs++;
317                     fprintf(stderr,
318                             "long double-int MAXLOC(>) test failed, location of max = %d, should be %d\n",
319                             coutbuf[2].loc, size - 1);
320                 }
321             }
322         }
323     }
324 #endif
325
326     MTest_Finalize(errs);
327     MPI_Finalize();
328     return 0;
329 }