Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'v3_8_x'
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / hvectest2.c
1 /*
2     hvectest2 - test program that sends an array of floats from the first 
3                 process of a group to the last, using send and recv and the
4                 struct datatype for variable length vectors
5 */
6
7 #include "mpi.h"
8 #include <stdio.h>
9 #include <string.h>
10 #include "test.h"
11 /* #define SHOWMSG */
12
13 /* Prototypes for picky compilers */
14 void ClearArray ( double *, int, double );
15 void SetArray ( double *, int );
16
17 #ifdef VERBOSE
18 static int verbose = 1;
19 #else
20 static int verbose = 0;
21 #endif
22
23 void ClearArray( a, n, v )
24 double *a, v;
25 int    n;
26 {
27     int i;
28     for (i=0; i<n; i++) a[i] = v;
29 }
30
31 void SetArray( a, n )
32 double *a;
33 int    n;
34 {
35     int i;
36     for (i=0; i<n; i++) a[i] = (double)i;
37 }
38
39 /* 
40    This test requires that the MPI implementation support predefined 
41    MPI_Datatypes in static initializers (i.e., they must be compile time
42    constants).  This was voted as a clarification on 4/26/95.
43  */
44 int main( int argc, char **argv )
45 {
46     int rank, size, to, from, tag, count, i;
47     int src, dest;
48     int st_source, st_tag, st_count;
49     int errcnt = 0;
50     MPI_Request handle;
51     MPI_Status status;
52     double data[100];
53     MPI_Datatype rowtype;
54     static int blens[2] = { 1, 1 };
55     MPI_Datatype types[2] = { MPI_DOUBLE, MPI_UB };
56     MPI_Aint displs[2];
57
58     MPI_Init( &argc, &argv );
59     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
60     MPI_Comm_size( MPI_COMM_WORLD, &size );
61
62     /* dest writes out the received stats; for the output to be
63        consistant (with the final check), it should be procees 0 */
64     if (argc > 1 && argv[1] && strcmp( "-alt", argv[1] ) == 0) {
65         dest = size - 1;
66         src  = 0;
67         }
68     else {
69         src  = size - 1;
70         dest = 0;
71         }
72
73     displs[0] = 0;
74     displs[1] = 10*sizeof(double);
75 /*
76     blens[0]  = 1;
77     blens[1]  = 1;
78     types[0]  = MPI_DOUBLE;
79     types[1]  = MPI_UB;
80  */
81     MPI_Type_struct( 2, blens, displs, types, &rowtype );
82     MPI_Type_commit( &rowtype );
83     /* First test: send a row */
84     if (rank == src)
85     {
86         to     = dest;
87         count  = 10;
88         tag    = 2001;
89         SetArray( data, 100 );
90         /* Send a row */
91         MPI_Send( data, count, rowtype, to, tag, MPI_COMM_WORLD );
92 #ifdef SHOWMSG
93         printf("%d sent", rank );
94         for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
95 #endif
96     }
97
98     if (rank == dest)
99     {
100         tag   = MPI_ANY_TAG;
101         count = 10;             
102         from  = MPI_ANY_SOURCE;
103         
104         ClearArray( data, 100, -1.0 );
105         MPI_Recv(data, count, MPI_DOUBLE, from, tag, MPI_COMM_WORLD,
106                  &status ); 
107
108         st_source = status.MPI_SOURCE;
109         st_tag    = status.MPI_TAG;
110         MPI_Get_count( &status, MPI_DOUBLE, &st_count );
111
112         if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
113             printf( "Status info: source = %d, tag = %d, count = %d\n",
114                     st_source, st_tag, st_count );
115         }
116 #ifdef SHOWMSG
117         printf( "%d received", rank);
118         for (i = 0; i < 10; i++) printf(" %f",data[i]); printf("\n");
119 #endif
120         for (i = 0; i < 10; i++) if (data[i] != 10*i) { 
121             errcnt++;
122             fprintf( stderr, 
123                     "[%d](rcv double) %d'th element = %f, should be %f\n",
124                      rank, i, data[i], 10.0*i );
125             }
126     }
127
128     /* Second test: receive a column into row */
129     if (rank == src)
130     {
131         to     = dest;
132         count  = 10;
133         tag    = 2001;
134         SetArray( data, 100 );
135         /* Send a row */
136         /* MPE_Print_datatype_pack_action( stdout, count, 
137                                            MPI_DOUBLE, 0, 0 ); */
138         MPI_Send( data, count, MPI_DOUBLE, to, tag, MPI_COMM_WORLD );
139 #ifdef SHOWMSG
140         printf("%d sent", rank );
141         for (i = 0; i < 10; i++) printf(" %f",data[i]);printf("\n");
142 #endif
143     }
144     if (rank == dest)
145     {
146         tag   = MPI_ANY_TAG;
147         count = 10;             
148         from  = MPI_ANY_SOURCE;
149         ClearArray( data, 100, -1.0 );
150         MPI_Recv(data, count, rowtype, from, tag, MPI_COMM_WORLD,
151                  &status ); 
152         /* MPE_Print_datatype_unpack_action( stdout, count, rowtype, 0, 0 ); */
153
154         st_source = status.MPI_SOURCE;
155         st_tag    = status.MPI_TAG;
156         MPI_Get_count( &status, MPI_DOUBLE, &st_count );
157
158         if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
159             printf( "Status info: source = %d, tag = %d, count = %d\n",
160                 st_source, st_tag, st_count );
161         }
162 #ifdef SHOWMSG
163         printf( "%d received", rank);
164         for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
165 #endif
166         for (i = 0; i < 10; i++) if (data[i*10] != i) {
167             errcnt++;
168             fprintf( stderr, 
169                     "[%d](rcv row) %d'th element = %f, should be %f\n",
170                      rank, i, data[i*10], 1.0*i );
171             }
172     }
173
174     /* Third test: send AND receive a row */
175     if (rank == src)
176     {
177         to     = dest;
178         count  = 10;
179         tag    = 2001;
180         SetArray( data, 100 );
181         /* Send a row */
182         MPI_Send( data, count, rowtype, to, tag, MPI_COMM_WORLD );
183 #ifdef SHOWMSG
184         printf("%d sent", rank );
185         for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
186 #endif
187     }
188     if (rank == dest)
189     {
190         tag   = MPI_ANY_TAG;
191         count = 10;             
192         from  = MPI_ANY_SOURCE;
193         ClearArray( data, 100, -1.0 );
194         MPI_Recv(data, count, rowtype, from, tag, MPI_COMM_WORLD,
195                  &status ); 
196
197         st_source = status.MPI_SOURCE;
198         st_tag    = status.MPI_TAG;
199         MPI_Get_count( &status, MPI_DOUBLE, &st_count );
200
201         if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
202             printf( "Status info: source = %d, tag = %d, count = %d\n",
203                     st_source, st_tag, st_count );
204         }
205 #ifdef SHOWMSG
206         printf( "%d received", rank);
207         for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
208 #endif
209         for (i = 0; i < 10; i++) if (data[i*10] != i*10) {
210             errcnt++;
211             fprintf( stderr, 
212                     "[%d](rcv row-row) %d'th element = %f, should be %f\n",
213                      rank, i, data[i*10], 10.0*i );
214             }
215     }
216
217     /* Second Set of Tests: Use Isend and Irecv instead of Send and Recv */
218     /* First test: send a row */
219     if (rank == src)
220     {
221         to     = dest;
222         count  = 10;
223         tag    = 2001;
224         SetArray( data, 100 );
225         /* Send a row */
226         MPI_Isend( data, count, rowtype, to, tag, MPI_COMM_WORLD, &handle );
227         MPI_Wait( &handle, &status );
228 #ifdef SHOWMSG
229         printf("%d sent", rank );
230         for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
231 #endif
232     }
233
234     if (rank == dest)
235     {
236         tag   = MPI_ANY_TAG;
237         count = 10;             
238         from  = MPI_ANY_SOURCE;
239         ClearArray( data, 100, -1.0 );
240         MPI_Irecv(data, count, MPI_DOUBLE, from, tag, MPI_COMM_WORLD,
241                  &handle ); 
242         MPI_Wait( &handle, &status );
243
244         st_source = status.MPI_SOURCE;
245         st_tag    = status.MPI_TAG;
246         MPI_Get_count( &status, MPI_DOUBLE, &st_count );
247
248         if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
249             printf( "Status info: source = %d, tag = %d, count = %d\n",
250                     st_source, st_tag, st_count );
251         }
252 #ifdef SHOWMSG
253         printf( "%d received", rank);
254         for (i = 0; i < 10; i++) printf(" %f",data[i]); printf("\n");
255 #endif
256         for (i = 0; i < 10; i++) if (data[i] != 10*i) {
257             errcnt++;
258             fprintf( stderr, 
259                     "[%d](ircv double) %d'th element = %f, should be %f\n",
260                      rank, i, data[i], 10.0*i );
261             }
262     }
263
264     /* Second test: receive a column into row */
265     if (rank == src)
266     {
267         to     = dest;
268         count  = 10;
269         tag    = 2001;
270         SetArray( data, 100 );
271         /* Send a row */
272         MPI_Isend( data, count, MPI_DOUBLE, to, tag, MPI_COMM_WORLD, 
273                    &handle );
274         MPI_Wait( &handle, &status );
275 #ifdef SHOWMSG
276         printf("%d sent", rank );
277         for (i = 0; i < 10; i++) printf(" %f",data[i]);printf("\n");
278 #endif
279     }
280     if (rank == dest)
281     {
282         tag   = MPI_ANY_TAG;
283         count = 10;             
284         from  = MPI_ANY_SOURCE;
285         ClearArray( data, 100, -1.0 );
286         MPI_Irecv(data, count, rowtype, from, tag, MPI_COMM_WORLD,
287                  &handle ); 
288         MPI_Wait( &handle, &status );
289
290         st_source = status.MPI_SOURCE;
291         st_tag    = status.MPI_TAG;
292         MPI_Get_count( &status, MPI_DOUBLE, &st_count );
293
294         if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
295             printf( "Status info: source = %d, tag = %d, count = %d\n",
296                     st_source, st_tag, st_count );
297         }
298 #ifdef SHOWMSG
299         printf( "%d received", rank);
300         for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
301 #endif
302         for (i = 0; i < 10; i++) if (data[i*10] != i) {
303             errcnt++;
304             fprintf( stderr, 
305                     "[%d](ircv row) %d'th element = %f, should be %f\n",
306                      rank, i, data[i*10], 1.0*i );
307             }
308     }
309
310     /* Third test: send AND receive a row */
311     if (rank == src)
312     {
313         to     = dest;
314         count  = 10;
315         tag    = 2001;
316         SetArray( data, 100 );
317         /* Send a row */
318         MPI_Isend( data, count, rowtype, to, tag, MPI_COMM_WORLD, &handle );
319         MPI_Wait( &handle, &status );
320 #ifdef SHOWMSG
321         printf("%d sent", rank );
322         for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
323 #endif
324     }
325     if (rank == dest)
326     {
327         tag   = MPI_ANY_TAG;
328         count = 10;             
329         from  = MPI_ANY_SOURCE;
330         ClearArray( data, 100, -1.0 );
331         MPI_Irecv(data, count, rowtype, from, tag, MPI_COMM_WORLD,
332                  &handle ); 
333         MPI_Wait( &handle, &status );
334
335         st_source = status.MPI_SOURCE;
336         st_tag    = status.MPI_TAG;
337         MPI_Get_count( &status, MPI_DOUBLE, &st_count );
338
339         if (st_source != src || st_tag != 2001 || st_count != 10 || verbose) {
340             printf( "Status info: source = %d, tag = %d, count = %d\n",
341                     st_source, st_tag, st_count );
342         }
343 #ifdef SHOWMSG
344         printf( "%d received", rank);
345         for (i = 0; i < 10; i++) printf(" %f",data[i*10]);printf("\n");
346 #endif
347         for (i = 0; i < 10; i++) if (data[i*10] != i*10) {
348             errcnt++;
349             fprintf( stderr, 
350                     "[%d](ircv row-row) %d'th element = %f, should be %f\n",
351                      rank, i, data[i*10], 10.0*i );
352             }
353     }
354
355     i = errcnt;
356     MPI_Allreduce( &i, &errcnt, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
357     if (errcnt > 0) {
358         printf( "Found %d errors in the run \n", errcnt );
359         }
360     MPI_Type_free( &rowtype );
361     Test_Waitforall( );
362     MPI_Finalize();
363     return 0;
364 }
365
366