Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
handle nested datatypes in smpi (structs of vectors for example), which previously...
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / flood2.c
1 #include "mpi.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "test.h"
5
6 #define MAX_REQ 32
7 #define MAX_MSG_CNT 32000
8 #define MAX_MSG 2048
9 /* 
10    This program tests a flood of data of short messages to test handling
11    of both incoming messages and internal message queues
12  */
13
14 void SetupData ( int *, int, int );
15 void SetupRdata ( int *, int );
16 int  CheckData ( int *, int, int, MPI_Status * );
17
18 #ifdef VERBOSE
19 static int verbose = 1;
20 #else
21 static int verbose = 0;
22 #endif
23
24
25 int main( int argc, char **argv )
26 {
27     MPI_Comm comm;
28     MPI_Request r[MAX_REQ];
29     MPI_Status  s[MAX_REQ];
30     int msgsize, maxmsg, root, i, j, size, rank, err = 0, msgcnt, toterr;
31     int *sbuf, *rbuf;
32
33     MPI_Init( &argc, &argv );
34     
35     comm = MPI_COMM_WORLD;
36
37     MPI_Comm_size( comm, &size );
38     MPI_Comm_rank( comm, &rank );
39
40     if (size < 2) {
41         printf( "This test requires at least 2 processors\n" );
42         MPI_Abort( comm, 1 );
43     }
44
45     /* First, try large blocking sends to root */
46     root = 0;
47     
48     maxmsg =  MAX_MSG;
49     msgsize = 128;
50     msgcnt  = MAX_MSG_CNT;
51     if (rank == root && verbose) printf( "Blocking sends: " );
52     while (msgsize <= maxmsg) {
53         if (rank == root) {
54             if (verbose) { printf( "%d ", msgsize ); fflush( stdout ); }
55             rbuf = (int *)malloc( msgsize * sizeof(int) );
56             if (!rbuf) {
57                 printf( "Could not allocate %d words\n", msgsize );
58                 MPI_Abort( comm, 1 );
59             }
60             for (i=0; i<size; i++) {
61                 if (i == rank) continue;
62                 for (j=0; j<msgcnt; j++) {
63                     SetupRdata( rbuf, msgsize );
64                     MPI_Recv( rbuf, msgsize, MPI_INT, i, 2*i, comm, s );
65                     err += CheckData( rbuf, msgsize, 2*i, s );
66                 }
67             }
68             free( rbuf );
69         }
70         else {
71             sbuf = (int *)malloc( msgsize * sizeof(int) );
72             if (!sbuf) {
73                 printf( "Could not allocate %d words\n", msgsize );
74                 MPI_Abort( comm, 1 );
75             }
76             SetupData( sbuf, msgsize, 2*rank );
77             for (j=0; j<msgcnt; j++) 
78                 MPI_Send( sbuf, msgsize, MPI_INT, root, 2*rank, comm );
79             free( sbuf );
80         }
81         msgsize *= 4;
82     }
83     if (rank == 0 && verbose) { printf( "\n" ); fflush( stdout ); }
84
85     /* Next, try unexpected messages with Isends */
86     msgsize = 128;
87     maxmsg  = MAX_MSG;
88     msgcnt  = MAX_REQ;
89     if (rank == root && verbose) printf( "Unexpected recvs: " );
90     while (msgsize <= maxmsg) {
91         if (rank == root) {
92             if (verbose) { printf( "%d ", msgsize ); fflush( stdout ); }
93             rbuf = (int *)malloc( msgsize * sizeof(int) );
94             if (!rbuf) {
95                 printf( "Could not allocate %d words\n", msgsize );
96                 MPI_Abort( comm, 1 );
97             }
98             MPI_Barrier( comm );
99             for (i=0; i<size; i++) {
100                 if (i == rank) continue;
101                 for (j=0; j<msgcnt; j++) {
102                     SetupRdata( rbuf, msgsize );
103                     MPI_Recv( rbuf, msgsize, MPI_INT, i, 2*i, comm, s );
104                     err += CheckData( rbuf, msgsize, 2*i, s );
105                 }
106             }
107             free( rbuf );
108         }
109         else {
110             sbuf = (int *)malloc( msgsize * sizeof(int) );
111             if (!sbuf) {
112                 printf( "Could not allocate %d words\n", msgsize );
113                 MPI_Abort( comm, 1 );
114             }
115             SetupData( sbuf, msgsize, 2*rank );
116             for (j=0; j<msgcnt; j++) {
117                 MPI_Isend( sbuf, msgsize, MPI_INT, root, 2*rank, comm, &r[j] );
118             }
119             MPI_Barrier( comm );
120             MPI_Waitall( msgcnt, r, s );
121             free( sbuf );
122         }
123         msgsize *= 4;
124     }
125     if (rank == 0 && verbose) { printf( "\n" ); fflush( stdout ); }
126
127     /* Try large synchronous blocking sends to root */
128     root = 0;
129     
130     msgsize = 128;
131     maxmsg  = MAX_MSG;
132     if (rank == root && verbose) printf( "Synchronous sends: " );
133     while (msgsize <= maxmsg) {
134         if (rank == root) {
135             if (verbose) { printf( "%d ", msgsize ); fflush( stdout ); }
136             rbuf = (int *)malloc( msgsize * sizeof(int) );
137             if (!rbuf) {
138                 printf( "Could not allocate %d words\n", msgsize );
139                 MPI_Abort( comm, 1 );
140             }
141             for (i=0; i<size; i++) {
142                 if (i == rank) continue;
143                 for (j=0; j<msgcnt; j++) {
144                     SetupRdata( rbuf, msgsize );
145                     MPI_Recv( rbuf, msgsize, MPI_INT, i, 2*i, comm, s );
146                     err += CheckData( rbuf, msgsize, 2*i, s );
147                 }
148             }
149             free( rbuf );
150         }
151         else {
152             sbuf = (int *)malloc( msgsize * sizeof(int) );
153             if (!sbuf) {
154                 printf( "Could not allocate %d words\n", msgsize );
155                 MPI_Abort( comm, 1 );
156             }
157             SetupData( sbuf, msgsize, 2*rank );
158             for (j=0; j<msgcnt; j++) 
159                 MPI_Ssend( 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     MPI_Allreduce( &err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
167     
168     if (rank == 0) {
169         if (toterr == 0) printf( " No Errors\n" );
170         else printf( "!! found %d errors\n", toterr );
171     }
172     if (toterr) {
173         printf( "!! found %d errors on processor %d\n", err, rank );
174     }
175
176     MPI_Finalize( );
177     return 0;
178 }
179
180 void SetupData( sbuf, n, tag )
181 int *sbuf, n, tag;
182 {
183     int i;
184
185     for (i=0; i<n; i++) 
186         sbuf[i] = i;
187 }
188
189 int CheckData( rbuf, n, tag, s )
190 int *rbuf, n, tag;
191 MPI_Status *s;
192 {
193     int act_n, i;
194
195     MPI_Get_count( s, MPI_INT, &act_n );
196     if (act_n != n) {
197         printf( "Received %d instead of %d ints\n", act_n, n );
198         return 1;
199     }
200     for (i=0; i<n; i++) {
201         if (rbuf[i] != i) {
202             printf( "rbuf[%d] is %d, should be %d\n", i, rbuf[i], i );
203             return 1;
204         }
205     }
206     return 0;
207 }
208
209 void SetupRdata( rbuf, n )
210 int *rbuf, n;
211 {
212     int i;
213     
214     for (i=0; i<n; i++) rbuf[i] = -(i+1);
215 }