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 / testsome.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, index, outcount, indices[2];
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        sender:                               receiver:
52        irecv                                 irecv
53        isend
54        testsome (all fail)
55        testany  (all fail)
56        sendrecv                              sendrecv
57                                              isend
58        sendrecv                              sendrecv
59        testsome (both may)                   waitsome (both may)
60        waitall                               waitsome (must get other, if any)
61                                              waitsome (outcount = undefined)
62        This test DEPENDS on the handling of null requests, since the several
63        waits/tests may complete everything "early".
64      */
65     for (j=0; j<ntype; j++) {
66         if (world_rank == 0 && verbose) 
67             fprintf( stdout, "Testing type %s\n", names[j] );
68         /* This test does an irsend between both partners, with 
69            a sendrecv after the irecv used to guarentee that the
70            irsend has a matching receive
71          */
72         if (rank == 0) {
73             /* Sender */
74             partner = np - 1;
75 #if 0
76             MPIR_PrintDatatypePack( stdout, counts[j], types[j], 0, 0 );
77 #endif
78             obuf = outbufs[j];
79             for (k=0; k<bytesize[j]; k++) 
80                 obuf[k] = 0;
81             
82             MPI_Irecv(outbufs[j], counts[j], types[j], partner, tag, 
83                       comms[i], &requests[0] );
84
85             MPI_Isend( inbufs[j], counts[j], types[j], partner, tag, 
86                         comms[i], &requests[1] );
87
88             /* Note that the send may have completed */
89             MPI_Testsome( 1, &requests[0], &outcount, indices, statuses );
90             if (outcount != 0) {
91                 fprintf( stderr, "MPI_Testsome returned outcount = %d\n",
92                          outcount );
93                 err++;
94                 }
95             MPI_Testany( 1, &requests[0], &index, &flag, &status );
96             if (flag) {
97                 fprintf( stderr, "MPI_Testany returned flag = true\n" );
98                 err++;
99                 }
100             MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
101                           MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
102                           comms[i], &status );
103             MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
104                           MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
105                           comms[i], &status );
106             /* We EXPECT both to succeed, but they may not */
107             MPI_Testsome( 2, requests, &outcount, indices, statuses );
108             MPI_Waitall( 2, requests, statuses );
109             
110             /* Check the received data */
111             if (CheckDataAndPrint( inbufs[j], outbufs[j], bytesize[j],
112                                    names[j], j )) {
113                 err++;
114                 }
115             }
116         else if (rank == np - 1) {
117             /* receiver */
118             partner = 0;
119             obuf = outbufs[j];
120             for (k=0; k<bytesize[j]; k++) 
121                 obuf[k] = 0;
122             
123             MPI_Irecv(outbufs[j], counts[j], types[j], partner, tag, 
124                       comms[i], &requests[0] );
125
126             MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
127                           MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
128                           comms[i], &status );
129
130             MPI_Isend( inbufs[j], counts[j], types[j], partner, tag, 
131                         comms[i], &requests[1] );
132             
133             MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
134                           MPI_BOTTOM, 0, MPI_INT, partner, ncomm+i, 
135                           comms[i], &status );
136
137             MPI_Waitsome( 2, requests, &outcount, indices, statuses );
138             MPI_Waitsome( 2, requests, &outcount, indices, statuses );
139             MPI_Waitsome( 2, requests, &outcount, indices, statuses );
140             if (outcount != MPI_UNDEFINED) {
141                 err++;
142                 fprintf( stderr, 
143                 "MPI_Waitsome did not return outcount = MPI_UNDEFINED\n" );
144                 }
145
146             if (CheckDataAndPrint( inbufs[j], outbufs[j], bytesize[j],
147                                    names[j], j )) {
148                 err++;
149                 }
150
151             MPI_Waitall(1, &requests[1], &status );
152             }
153         }
154     }
155
156 if (err > 0) {
157     fprintf( stderr, "%d errors on %d\n", err, rank );
158     }
159  MPI_Allreduce( &err, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
160  if (world_rank == 0) {
161      if (toterr == 0) {
162          printf( " No Errors\n" );
163      }
164      else {
165          printf (" Found %d errors\n", toterr );
166      }
167  }
168 FreeDatatypes( types, inbufs, outbufs, counts, bytesize, names, ntype );
169 FreeComms( comms, ncomm );
170 MPI_Finalize();
171
172 return err;
173 }