Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove warning with mc
[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 { int val; int loc; } cinbuf[3], coutbuf[3];
41         
42         cinbuf[0].val = 1;
43         cinbuf[0].loc = rank;
44         cinbuf[1].val = 0;
45         cinbuf[1].loc = rank;
46         cinbuf[2].val = rank;
47         cinbuf[2].loc = rank;
48         
49         coutbuf[0].val = 0;
50         coutbuf[0].loc = -1;
51         coutbuf[1].val = 1;
52         coutbuf[1].loc = -1;
53         coutbuf[2].val = 1;
54         coutbuf[2].loc = -1;
55         MPI_Reduce( cinbuf, coutbuf, 3, MPI_2INT, MPI_MAXLOC, 0, comm );
56         if (rank == 0) {
57             if (coutbuf[0].val != 1 || coutbuf[0].loc != 0) {
58                 errs++;
59                 fprintf( stderr, "2int MAXLOC(1) test failed\n" );
60             }
61             if (coutbuf[1].val != 0) {
62                 errs++;
63                 fprintf( stderr, "2int MAXLOC(0) test failed, value = %d, should be zero\n", coutbuf[1].val );
64             }
65             if (coutbuf[1].loc != 0) {
66                 errs++;
67                 fprintf( stderr, "2int MAXLOC(0) test failed, location of max = %d, should be zero\n", coutbuf[1].loc );
68             }
69             if (coutbuf[2].val != size-1 || coutbuf[2].loc != size-1) {
70                 errs++;
71                 fprintf( stderr, "2int MAXLOC(>) test failed\n" );
72             }
73         }
74     }
75
76     /* float int */
77     {
78         struct floatint { float val; int loc; } cinbuf[3], coutbuf[3];
79         
80         cinbuf[0].val = 1;
81         cinbuf[0].loc = rank;
82         cinbuf[1].val = 0;
83         cinbuf[1].loc = rank;
84         cinbuf[2].val = (float)rank;
85         cinbuf[2].loc = rank;
86         
87         coutbuf[0].val = 0;
88         coutbuf[0].loc = -1;
89         coutbuf[1].val = 1;
90         coutbuf[1].loc = -1;
91         coutbuf[2].val = 1;
92         coutbuf[2].loc = -1;
93         MPI_Reduce( cinbuf, coutbuf, 3, MPI_FLOAT_INT, MPI_MAXLOC, 0, comm );
94         if (rank == 0) {
95             if (coutbuf[0].val != 1 || coutbuf[0].loc != 0) {
96                 errs++;
97                 fprintf( stderr, "float-int MAXLOC(1) test failed\n" );
98             }
99             if (coutbuf[1].val != 0) {
100                 errs++;
101                 fprintf( stderr, "float-int MAXLOC(0) test failed, value = %f, should be zero\n", coutbuf[1].val );
102             }
103             if (coutbuf[1].loc != 0) {
104                 errs++;
105                 fprintf( stderr, "float-int MAXLOC(0) test failed, location of max = %d, should be zero\n", coutbuf[1].loc );
106             }
107             if (coutbuf[2].val != size-1 || coutbuf[2].loc != size-1) {
108                 errs++;
109                 fprintf( stderr, "float-int MAXLOC(>) test failed\n" );
110             }
111         }
112     }
113     
114     /* long int */
115     {
116         struct longint { long val; int loc; } cinbuf[3], coutbuf[3];
117         
118         cinbuf[0].val = 1;
119         cinbuf[0].loc = rank;
120         cinbuf[1].val = 0;
121         cinbuf[1].loc = rank;
122         cinbuf[2].val = rank;
123         cinbuf[2].loc = rank;
124         
125         coutbuf[0].val = 0;
126         coutbuf[0].loc = -1;
127         coutbuf[1].val = 1;
128         coutbuf[1].loc = -1;
129         coutbuf[2].val = 1;
130         coutbuf[2].loc = -1;
131         MPI_Reduce( cinbuf, coutbuf, 3, MPI_LONG_INT, MPI_MAXLOC, 0, comm );
132         if (rank == 0) {
133             if (coutbuf[0].val != 1 || coutbuf[0].loc != 0) {
134                 errs++;
135                 fprintf( stderr, "long-int MAXLOC(1) test failed\n" );
136             }
137             if (coutbuf[1].val != 0) {
138                 errs++;
139                 fprintf( stderr, "long-int MAXLOC(0) test failed, value = %ld, should be zero\n", coutbuf[1].val );
140             }
141             if (coutbuf[1].loc != 0) {
142                 errs++;
143                 fprintf( stderr, "long-int MAXLOC(0) test failed, location of max = %d, should be zero\n", coutbuf[1].loc );
144             }
145             if (coutbuf[2].val != size-1 || coutbuf[2].loc != size-1) {
146                 errs++;
147                 fprintf( stderr, "long-int MAXLOC(>) test failed\n" );
148             }
149         }
150     }
151
152     /* short int */
153     {
154         struct shortint { short val; int loc; } cinbuf[3], coutbuf[3];
155         
156         cinbuf[0].val = 1;
157         cinbuf[0].loc = rank;
158         cinbuf[1].val = 0;
159         cinbuf[1].loc = rank;
160         cinbuf[2].val = rank;
161         cinbuf[2].loc = rank;
162         
163         coutbuf[0].val = 0;
164         coutbuf[0].loc = -1;
165         coutbuf[1].val = 1;
166         coutbuf[1].loc = -1;
167         coutbuf[2].val = 1;
168         coutbuf[2].loc = -1;
169         MPI_Reduce( cinbuf, coutbuf, 3, MPI_SHORT_INT, MPI_MAXLOC, 0, comm );
170         if (rank == 0) {
171             if (coutbuf[0].val != 1 || coutbuf[0].loc != 0) {
172                 errs++;
173                 fprintf( stderr, "short-int MAXLOC(1) test failed\n" );
174             }
175             if (coutbuf[1].val != 0) {
176                 errs++;
177                 fprintf( stderr, "short-int MAXLOC(0) test failed, value = %d, should be zero\n", coutbuf[1].val );
178             }
179             if (coutbuf[1].loc != 0) {
180                 errs++;
181                 fprintf( stderr, "short-int MAXLOC(0) test failed, location of max = %d, should be zero\n", coutbuf[1].loc );
182             }
183             if (coutbuf[2].val != size-1) {
184                 errs++;
185                 fprintf( stderr, "short-int MAXLOC(>) test failed, value = %d, should be %d\n", coutbuf[2].val, size-1 );
186             }
187             if (coutbuf[2].loc != size -1) {
188                 errs++;
189                 fprintf( stderr, "short-int MAXLOC(>) test failed, location of max = %d, should be %d\n", coutbuf[2].loc, size-1 );
190             }
191         }
192     }
193     
194     /* double int */
195     {
196         struct doubleint { double val; int loc; } cinbuf[3], coutbuf[3];
197         
198         cinbuf[0].val = 1;
199         cinbuf[0].loc = rank;
200         cinbuf[1].val = 0;
201         cinbuf[1].loc = rank;
202         cinbuf[2].val = rank;
203         cinbuf[2].loc = rank;
204         
205         coutbuf[0].val = 0;
206         coutbuf[0].loc = -1;
207         coutbuf[1].val = 1;
208         coutbuf[1].loc = -1;
209         coutbuf[2].val = 1;
210         coutbuf[2].loc = -1;
211         MPI_Reduce( cinbuf, coutbuf, 3, MPI_DOUBLE_INT, MPI_MAXLOC, 0, comm );
212         if (rank == 0) {
213             if (coutbuf[0].val != 1 || coutbuf[0].loc != 0) {
214                 errs++;
215                 fprintf( stderr, "double-int MAXLOC(1) test failed\n" );
216             }
217             if (coutbuf[1].val != 0) {
218                 errs++;
219                 fprintf( stderr, "double-int MAXLOC(0) test failed, value = %f, should be zero\n", coutbuf[1].val );
220             }
221             if (coutbuf[1].loc != 0) {
222                 errs++;
223                 fprintf( stderr, "double-int MAXLOC(0) test failed, location of max = %d, should be zero\n", coutbuf[1].loc );
224             }
225             if (coutbuf[2].val != size-1 || coutbuf[2].loc != size-1) {
226                 errs++;
227                 fprintf( stderr, "double-int MAXLOC(>) test failed\n" );
228             }
229         }
230     }
231     
232 #ifdef HAVE_LONG_DOUBLE
233     /* long double int */
234     {
235         struct longdoubleint { long double val; int loc; } cinbuf[3], coutbuf[3];
236
237         /* avoid valgrind warnings about padding bytes in the long double */
238         memset(&cinbuf[0], 0, sizeof(cinbuf));
239         memset(&coutbuf[0], 0, sizeof(coutbuf));
240
241         cinbuf[0].val = 1;
242         cinbuf[0].loc = rank;
243         cinbuf[1].val = 0;
244         cinbuf[1].loc = rank;
245         cinbuf[2].val = rank;
246         cinbuf[2].loc = rank;
247         
248         coutbuf[0].val = 0;
249         coutbuf[0].loc = -1;
250         coutbuf[1].val = 1;
251         coutbuf[1].loc = -1;
252         coutbuf[2].val = 1;
253         coutbuf[2].loc = -1;
254         if (MPI_LONG_DOUBLE != MPI_DATATYPE_NULL) {
255             MPI_Reduce( cinbuf, coutbuf, 3, MPI_LONG_DOUBLE_INT, MPI_MAXLOC, 
256                         0, comm );
257             if (rank == 0) {
258                 if (coutbuf[0].val != 1 || coutbuf[0].loc != 0) {
259                     errs++;
260                     fprintf( stderr, "long double-int MAXLOC(1) test failed\n" );
261                 }
262                 if (coutbuf[1].val != 0) {
263                     errs++;
264                     fprintf( stderr, "long double-int MAXLOC(0) test failed, value = %f, should be zero\n", (double)coutbuf[1].val );
265                 }
266                 if (coutbuf[1].loc != 0) {
267                     errs++;
268                     fprintf( stderr, "long double-int MAXLOC(0) test failed, location of max = %d, should be zero\n", coutbuf[1].loc );
269                 }
270                 if (coutbuf[2].val != size-1) {
271                     errs++;
272                     fprintf( stderr, "long double-int MAXLOC(>) test failed, value = %f, should be %d\n", (double)coutbuf[2].val, size-1 );
273                 }
274                 if (coutbuf[2].loc != size-1) {
275                     errs++;
276                     fprintf( stderr, "long double-int MAXLOC(>) test failed, location of max = %d, should be %d\n", coutbuf[2].loc, size-1 );
277                 }
278             }
279         }
280     }
281 #endif
282
283     MTest_Finalize( errs );
284     MPI_Finalize();
285     return 0;
286 }