Logo AND Algorithmique Numérique Distribuée

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