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 / testall.c
1 #include "mpi.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "dtypes.h"
5 #include "gcomm.h"
6
7 #if defined(NEEDS_STDLIB_PROTOTYPES)
8 #include "protofix.h"
9 #endif
10
11 int verbose = 0;
12 /* 
13    Multiple completions
14    
15    This is similar to a test in allpair.f, but with an expanded range of
16    datatypes and communicators.
17  */
18
19 int main( int argc, char **argv )
20 {
21 MPI_Datatype *types;
22 void         **inbufs, **outbufs;
23 char         **names;
24 int          *counts, *bytesize, ntype;
25 MPI_Comm     comms[20];
26 int          ncomm = 20, rank, np, partner, tag;
27 int          i, j, k, err, toterr, world_rank;
28 MPI_Status   status, statuses[2];
29 int          flag;
30 char         *obuf;
31 MPI_Request  requests[2];
32
33
34 MPI_Init( &argc, &argv );
35
36 AllocateForData( &types, &inbufs, &outbufs, &counts, &bytesize, 
37                  &names, &ntype );
38 GenerateData( types, inbufs, outbufs, counts, bytesize, names, &ntype );
39
40 MPI_Comm_rank( MPI_COMM_WORLD, &world_rank );
41 MakeComms( comms, 20, &ncomm, 0 );
42
43 /* Test over a wide range of datatypes and communicators */
44 err = 0;
45 for (i=0; i<ncomm; i++) {
46     MPI_Comm_rank( comms[i], &rank );
47     MPI_Comm_size( comms[i], &np );
48     if (np < 2) continue;
49     tag = i;
50     /* This is the test.  
51        master:                               worker:
52        irecv                                 send
53        isend
54        testall  (fail)
55        sendrecv                              sendrecv
56                                              irecv
57        sendrecv                              sendrecv
58                                              wait
59        sendrecv                              sendrecv
60        testall  (should succeed)                  
61      */
62     for (j=0; j<ntype; j++) {
63         if (world_rank == 0 && verbose) 
64             fprintf( stdout, "Testing type %s\n", names[j] );
65         if (rank == 0) {
66             /* Master */
67             partner = np - 1;
68 #if 0
69             MPIR_PrintDatatypePack( stdout, counts[j], types[j], 0, 0 );
70 #endif
71             obuf = outbufs[j];
72             for (k=0; k<bytesize[j]; k++) 
73                 obuf[k] = 0;
74             
75             MPI_Irecv(outbufs[j], counts[j], types[j], partner, tag, 
76                       comms[i], &requests[0] );
77             
78             /* Use issend to ensure that the test cannot complete */
79             MPI_Isend( inbufs[j], counts[j], types[j], partner, tag, 
80                         comms[i], &requests[1] );
81
82             /* Note that the send may have completed */
83             MPI_Testall( 2, &requests[0], &flag, statuses );
84             if (flag) {
85                 err++;
86                 fprintf( stderr, "MPI_Testall returned flag == true!\n" );
87                 }
88             if (requests[1] == MPI_REQUEST_NULL) {
89                 err++;
90                 fprintf( stderr, "MPI_Testall freed a request\n" );
91                 }
92             MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
93                           MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
94                           comms[i], &status );
95             MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
96                           MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
97                           comms[i], &status );
98             MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
99                           MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
100                           comms[i], &status );
101             /* This should succeed, but may fail if the wait below is 
102                still waiting */
103             MPI_Testall( 2, requests, &flag, statuses );
104             if (!flag) {
105                 err++;
106                 fprintf( stderr, "MPI_Testall returned flag == false!\n" );
107                 }
108             if (requests[0] != MPI_REQUEST_NULL || 
109                 requests[1] != MPI_REQUEST_NULL) {
110                 err++;
111                 fprintf( stderr, "MPI_Testall failed to free requests (test %d)\n", j );
112                 if (requests[0] != MPI_REQUEST_NULL) {
113                     fprintf( stderr, "Failed to free Irecv request\n" );
114                 }
115                 if (requests[1] != MPI_REQUEST_NULL) {
116                     fprintf( stderr, "Failed to free Isend request\n" );
117                 }
118             }
119             /* Check the received data */
120             if (CheckDataAndPrint( inbufs[j], outbufs[j], bytesize[j],
121                                    names[j], j )) {
122                 err++;
123                 }
124             }
125         else if (rank == np - 1) {
126             /* receiver */
127             partner = 0;
128             obuf = outbufs[j];
129             for (k=0; k<bytesize[j]; k++) 
130                 obuf[k] = 0;
131             
132             MPI_Send( inbufs[j], counts[j], types[j], partner, tag, 
133                         comms[i] );
134             
135             MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
136                           MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
137                           comms[i], &status );
138
139             MPI_Irecv(outbufs[j], counts[j], types[j], partner, tag, 
140                       comms[i], &requests[0] );
141
142             MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
143                           MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
144                           comms[i], &status );
145
146             MPI_Wait( requests, statuses );
147             if (CheckDataAndPrint( inbufs[j], outbufs[j], bytesize[j],
148                                    names[j], j )) {
149                 err++;
150                 }
151             MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
152                           MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
153                           comms[i], &status );
154             }
155         }
156     }
157
158 if (err > 0) {
159     fprintf( stderr, "%d errors on %d\n", err, rank );
160     }
161 MPI_Allreduce( &err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
162  if (world_rank == 0) {
163      if (toterr == 0) {
164          printf( " No Errors\n" );
165      }
166      else {
167          printf (" Found %d errors\n", toterr );
168      }
169  }
170 FreeDatatypes( types, inbufs, outbufs, counts, bytesize, names, ntype );
171 FreeComms( comms, ncomm );
172 MPI_Finalize();
173
174 return err;
175 }