Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add mpich3 test suite, to replace older one.
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / flood.c
1 #include "mpi.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "test.h"
5
6 #define MAX_REQ 16
7 #define DEF_MAX_MSG 2000000
8 /* 
9    This program tests a flood of data for both unexpected and expected messages   to test any internal message fragmentation or protocol shifts
10
11    An optional argument can change the maximum message size.  For example, use
12       flood 9000000
13    to stress the memory system (the size is the number of ints, not bytes)
14  */
15
16 void SetupData ( int *, int, int );
17 void SetupRdata ( int *, int );
18 int  CheckData ( int *, int, int, MPI_Status * );
19
20 #ifdef VERBOSE
21 static int verbose = 1;
22 #else
23 static int verbose = 0;
24 #endif
25
26 int main( int argc, char **argv )
27 {
28     MPI_Comm comm;
29     MPI_Request r[MAX_REQ];
30     MPI_Status  s[MAX_REQ];
31     int msgsize, maxmsg, root, i, size, rank, err = 0, toterr;
32     int max_msg_size = DEF_MAX_MSG;
33     int *sbuf, *rbuf;
34
35     MPI_Init( &argc, &argv );
36
37     comm = MPI_COMM_WORLD;
38
39     MPI_Comm_size( comm, &size );
40     MPI_Comm_rank( comm, &rank );
41
42     if (size < 2) {
43         printf( "This test requires at least 2 processors\n" );
44         MPI_Abort( comm, 1 );
45     }
46
47     /* Check for a max message argument */
48     if (rank == 0) {
49         if (argc > 1) {
50             max_msg_size = atoi( argv[1] );
51             /* Correct if unrecognized argument */
52             if (max_msg_size <= 0) max_msg_size = DEF_MAX_MSG;
53         }
54     }
55     MPI_Bcast( &max_msg_size, 1, MPI_INT, 0, MPI_COMM_WORLD );
56
57     /* First, try large blocking sends to root */
58     root = 0;
59     
60     msgsize = 128;
61     maxmsg  = max_msg_size;
62     if (rank == root && verbose) printf( "Blocking sends: " );
63     while (msgsize < maxmsg) {
64         if (rank == root) {
65             if (verbose) { printf( "%d ", msgsize ); fflush( stdout ); }
66             rbuf = (int *)malloc( msgsize * sizeof(int) );
67             if (!rbuf) {
68                 printf( "Could not allocate %d words\n", msgsize );
69                 MPI_Abort( comm, 1 );
70             }
71             for (i=0; i<size; i++) {
72                 if (i == rank) continue;
73                 SetupRdata( rbuf, msgsize );
74                 MPI_Recv( rbuf, msgsize, MPI_INT, i, 2*i, comm, s );
75                 err += CheckData( rbuf, msgsize, 2*i, s );
76             }
77             free( rbuf );
78         }
79         else {
80             sbuf = (int *)malloc( msgsize * sizeof(int) );
81             if (!sbuf) {
82                 printf( "Could not allocate %d words\n", msgsize );
83                 MPI_Abort( comm, 1 );
84             }
85             SetupData( sbuf, msgsize, 2*rank );
86             MPI_Ssend( sbuf, msgsize, MPI_INT, root, 2*rank, comm );
87             free( sbuf );
88         }
89         msgsize *= 4;
90     }
91     if (rank == 0 && verbose) { printf( "\n" ); fflush( stdout ); }
92
93     /* Next, try unexpected messages with Isends */
94     msgsize = 128;
95     maxmsg  = max_msg_size;
96     if (rank == root && verbose) printf( "Unexpected recvs: " );
97     while (msgsize < maxmsg) {
98         if (rank == root) {
99             if (verbose) { printf( "%d ", msgsize ); fflush( stdout ); }
100             rbuf = (int *)malloc( msgsize * sizeof(int) );
101             if (!rbuf) {
102                 printf( "Could not allocate %d words\n", msgsize );
103                 MPI_Abort( comm, 1 );
104             }
105             MPI_Barrier( comm );
106             for (i=0; i<size; i++) {
107                 if (i == rank) continue;
108                 SetupRdata( rbuf, msgsize );
109                 MPI_Recv( rbuf, msgsize, MPI_INT, i, 2*i, comm, s );
110                 err += CheckData( rbuf, msgsize, 2*i, s );
111             }
112             free( rbuf );
113         }
114         else {
115             sbuf = (int *)malloc( msgsize * sizeof(int) );
116             if (!sbuf) {
117                 printf( "Could not allocate %d words\n", msgsize );
118                 MPI_Abort( comm, 1 );
119             }
120             SetupData( sbuf, msgsize, 2*rank );
121             MPI_Isend( sbuf, msgsize, MPI_INT, root, 2*rank, comm, r );
122             MPI_Barrier( comm );
123             MPI_Wait( r, s );
124             free( sbuf );
125         }
126         msgsize *= 4;
127     }
128     if (rank == 0 && verbose) { printf( "\n" ); fflush( stdout ); }
129
130     /* Try large synchronous blocking sends to root */
131     root = 0;
132     
133     msgsize = 128;
134     maxmsg  = max_msg_size;
135     if (rank == root && verbose) printf( "Synchronous sends: " );
136     while (msgsize < maxmsg) {
137         if (rank == root) {
138             if (verbose) { printf( "%d ", msgsize ); fflush( stdout ); }
139             rbuf = (int *)malloc( msgsize * sizeof(int) );
140             if (!rbuf) {
141                 printf( "Could not allocate %d words\n", msgsize );
142                 MPI_Abort( comm, 1 );
143             }
144             for (i=0; i<size; i++) {
145                 if (i == rank) continue;
146                 SetupRdata( rbuf, msgsize );
147                 MPI_Recv( rbuf, msgsize, MPI_INT, i, 2*i, comm, s );
148                 err += CheckData( rbuf, msgsize, 2*i, s );
149             }
150             free( rbuf );
151         }
152         else {
153             sbuf = (int *)malloc( msgsize * sizeof(int) );
154             if (!sbuf) {
155                 printf( "Could not allocate %d words\n", msgsize );
156                 MPI_Abort( comm, 1 );
157             }
158             SetupData( sbuf, msgsize, 2*rank );
159             MPI_Send( sbuf, msgsize, MPI_INT, root, 2*rank, comm );
160             free( sbuf );
161         }
162         msgsize *= 4;
163     }
164     if (rank == 0 && verbose) { printf( "\n" ); fflush( stdout ); }
165
166     /* Next, try expected messages with Rsend */
167     msgsize = 128;
168     maxmsg  = max_msg_size;
169     if (rank == root && verbose) printf( "Expected recvs and Rsend: " );
170     while (msgsize < maxmsg) {
171         if (rank == root) {
172             if (verbose) { printf( "%d ", msgsize ); fflush( stdout ); }
173             rbuf = (int *)malloc( msgsize * sizeof(int) );
174             if (!rbuf) {
175                 printf( "Could not allocate %d words\n", msgsize );
176                 MPI_Abort( comm, 1 );
177             }
178             for (i=0; i<size; i++) {
179                 if (i == rank) continue;
180                 SetupRdata( rbuf, msgsize );
181                 MPI_Irecv( rbuf, msgsize, MPI_INT, i, 2*i, comm, r );
182                 MPI_Send( MPI_BOTTOM, 0, MPI_INT, i, 2*i+1, comm );
183                 MPI_Wait( r, s );
184                 err += CheckData( rbuf, msgsize, 2*i, s );
185             }
186             free( rbuf );
187         }
188         else {
189             sbuf = (int *)malloc( msgsize * sizeof(int) );
190             if (!sbuf) {
191                 printf( "Could not allocate %d words\n", msgsize );
192                 MPI_Abort( comm, 1 );
193             }
194             SetupData( sbuf, msgsize, 2*rank );
195             MPI_Recv( MPI_BOTTOM, 0, MPI_INT, root, 2*rank+1, comm, s );
196             MPI_Send( sbuf, msgsize, MPI_INT, root, 2*rank, comm );
197             free( sbuf );
198         }
199         msgsize *= 4;
200     }
201     if (rank == 0 && verbose) { printf( "\n" ); fflush( stdout ); }
202
203
204     MPI_Allreduce( &err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
205     
206     if (rank == 0) {
207         if (toterr == 0) printf( " No Errors\n" );
208         else printf( "!! found %d errors\n", toterr );
209     }
210     if (toterr) {
211         printf( "!! found %d errors on processor %d\n", err, rank );
212     }
213
214     MPI_Finalize( );
215     return 0;
216 }
217
218 void SetupData( sbuf, n, tag )
219 int *sbuf, n, tag;
220 {
221     int i;
222
223     for (i=0; i<n; i++) 
224         sbuf[i] = i;
225 }
226
227 int CheckData( rbuf, n, tag, s )
228 int *rbuf, n, tag;
229 MPI_Status *s;
230 {
231     int act_n, i;
232
233     MPI_Get_count( s, MPI_INT, &act_n );
234     if (act_n != n) {
235         printf( "Received %d instead of %d ints\n", act_n, n );
236         return 1;
237     }
238     for (i=0; i<n; i++) {
239         if (rbuf[i] != i) {
240             printf( "rbuf[%d] is %d, should be %d\n", i, rbuf[i], i );
241             printf( "rbuf[%d] is 0x%x, should be 0x%x\n", i, rbuf[i], i );
242             return 1;
243         }
244     }
245     return 0;
246 }
247
248 void SetupRdata( rbuf, n )
249 int *rbuf, n;
250 {
251     int i;
252     
253     for (i=0; i<n; i++) rbuf[i] = -(i+1);
254 }