Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'v3_8_x'
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / cancel3.c
1 /*
2  * This file shows a typical use of MPI_Cancel to free Persistent Send's that
3  * are not wanted.  We check for both successful and unsuccessful 
4  * cancels
5  */
6
7 #include "mpi.h"
8 #include <stdio.h>
9
10 #if defined(NEEDS_STDLIB_PROTOTYPES)
11 #include "protofix.h"
12 #endif
13
14 int main( int argc, char **argv )
15 {
16     MPI_Request r1;
17     int         size, rank;
18     int         err = 0;
19     int         partner, buf[10], flag, idx, index;
20     MPI_Status  status;
21
22     MPI_Init( &argc, &argv );
23
24     MPI_Comm_size( MPI_COMM_WORLD, &size );
25     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
26     
27     if (size < 2) {
28         printf( "Cancel test requires at least 2 processes\n" );
29         MPI_Abort( MPI_COMM_WORLD, 1 );
30     }
31
32     /* 
33      * Here is the test.  First, we ensure an unsatisfied Irecv:
34      *       process 0             process size-1
35      *       Sendrecv              Sendrecv
36      *       Irecv                    ----
37      *       Cancel                   ----
38      *       Sendrecv              Sendrecv
39      * Next, we confirm receipt before canceling
40      *       Irecv                 Send
41      *       Sendrecv              Sendrecv
42      *       Cancel
43      */
44     if (rank == 0) {
45         partner = size - 1;
46         /* Cancel succeeds for wait/waitall */
47         MPI_Send_init( buf, 10, MPI_INT, partner, 0, MPI_COMM_WORLD, &r1 );
48         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
49                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
50                       MPI_COMM_WORLD, &status );
51         MPI_Start( &r1 );
52         MPI_Cancel( &r1 );
53         MPI_Wait( &r1, &status );
54         MPI_Test_cancelled( &status, &flag ); 
55         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
56                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
57                       MPI_COMM_WORLD, &status );
58         if (!flag) {
59             err++; 
60             printf( "Cancel of a send failed where it should succeed (Wait).\n" );
61         }
62         MPI_Request_free( &r1 ); 
63
64         /* Cancel fails for test/testall */
65         buf[0] = 3;
66         MPI_Send_init( buf, 3, MPI_INT, partner, 2, MPI_COMM_WORLD, &r1 );
67         MPI_Start( &r1 );
68         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
69                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
70                       MPI_COMM_WORLD, &status );
71         MPI_Cancel( &r1 );
72         MPI_Test( &r1, &flag, &status );
73         MPI_Test_cancelled( &status, &flag );
74         if (flag) {
75             err++;
76             printf( "Cancel of a send succeeded where it shouldn't (Test).\n" );
77         }
78         MPI_Request_free( &r1 );
79
80         /* Cancel succeeds for waitany */
81         MPI_Send_init( buf, 10, MPI_INT, partner, 0, MPI_COMM_WORLD, &r1 );
82         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
83                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
84                       MPI_COMM_WORLD, &status );
85         MPI_Start( &r1 );
86         MPI_Cancel( &r1 );
87         MPI_Waitany( 1, &r1, &idx, &status );
88         MPI_Test_cancelled( &status, &flag );
89         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
90                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
91                       MPI_COMM_WORLD, &status );
92         if (!flag) {
93             err++;
94             printf( "Cancel of a send failed where it should succeed (Waitany).\n" );
95         }
96         MPI_Request_free( &r1 );
97
98         /* Cancel fails for testany */
99         buf[0] = 3;
100         MPI_Send_init( buf, 3, MPI_INT, partner, 2, MPI_COMM_WORLD, &r1 );
101         MPI_Start( &r1 );
102         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
103                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
104                       MPI_COMM_WORLD, &status );
105         MPI_Cancel( &r1 );
106         MPI_Testany( 1, &r1, &idx, &flag, &status );
107         MPI_Test_cancelled( &status, &flag );
108         if (flag) {
109             err++;
110             printf( "Cancel of a send succeeded where it shouldn't (Testany).\n" );
111         }
112         MPI_Request_free( &r1 );
113
114         /* Cancel succeeds for waitsome */
115         MPI_Send_init( buf, 10, MPI_INT, partner, 0, MPI_COMM_WORLD, &r1 );
116         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
117                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
118                       MPI_COMM_WORLD, &status );
119         MPI_Start( &r1 );
120         MPI_Cancel( &r1 );
121         MPI_Waitsome( 1, &r1, &idx, &index, &status );
122         MPI_Test_cancelled( &status, &flag );
123         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
124                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
125                       MPI_COMM_WORLD, &status );
126         if (!flag) {
127             err++;
128             printf( "Cancel of a send failed where it should succeed (Waitsome).\n" );
129         }
130         MPI_Request_free( &r1 );
131
132         /* Cancel fails for testsome*/
133         buf[0] = 3;
134         MPI_Send_init( buf, 3, MPI_INT, partner, 2, MPI_COMM_WORLD, &r1 );
135         MPI_Start( &r1 );
136         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
137                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
138                       MPI_COMM_WORLD, &status );
139         MPI_Cancel( &r1 );
140         MPI_Testsome( 1, &r1, &idx, &index, &status );
141         MPI_Test_cancelled( &status, &flag );
142         if (flag) {
143             err++;
144             printf( "Cancel of a send succeeded where it shouldn't (Testsome).\n" );
145         }
146         MPI_Request_free( &r1 );
147
148         if (err) {
149             printf( "Test failed with %d errors.\n", err );
150         }
151         else {
152             printf( " No Errors\n" );
153         }
154     }
155     else if (rank == size - 1) {
156         partner = 0;
157         /* Cancel succeeds for wait/waitall */
158         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
159                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
160                       MPI_COMM_WORLD, &status );
161         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
162                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
163                       MPI_COMM_WORLD, &status );
164
165         /* Cancel fails for test/testall */
166         buf[0] = -1;
167         MPI_Recv( buf, 3, MPI_INT, partner, 2, MPI_COMM_WORLD, &status );
168         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
169                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
170                       MPI_COMM_WORLD, &status );
171
172         if (buf[0] == -1) {
173             printf( "Receive buffer did not change even though cancel should not have suceeded! (Test).\n" );
174             }
175
176         /* Cancel succeeds for waitany */
177         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
178                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
179                       MPI_COMM_WORLD, &status );
180         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
181                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
182                       MPI_COMM_WORLD, &status );
183         /* Cancel fails  for testany */
184         buf[0] = -1;
185         MPI_Recv( buf, 3, MPI_INT, partner, 2, MPI_COMM_WORLD, &status );
186         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
187                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
188                       MPI_COMM_WORLD, &status );
189         if (buf[0] == -1) {
190             printf( "Receive buffer did not change even though cancel should not have suceeded! (Testany).\n" );
191             }
192
193         /* Cancel succeeds for waitsome */
194         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
195                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
196                       MPI_COMM_WORLD, &status );
197         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
198                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
199                       MPI_COMM_WORLD, &status );
200         /* Cancel fails for testsome */
201         buf[0] = -1;
202         MPI_Recv( buf, 3, MPI_INT, partner, 2, MPI_COMM_WORLD, &status );
203         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
204                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
205                       MPI_COMM_WORLD, &status );
206
207         if (buf[0] == -1) {
208             printf( "Receive buffer did not change even though cancel should not have suceeded! (Test).\n" );
209             }
210
211     }
212
213     MPI_Finalize();
214     return 0;
215 }
216
217