Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7623be60e1343f1fe2c45b796e6c9188d6b0ebf9
[simgrid.git] / teshsuite / smpi / mpich3-test / pt2pt / scancel.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2003 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "mpitest.h"
10
11 /*
12 static char MTEST_Descrip[] = "Test of various send cancel calls";
13 */
14
15 int main( int argc, char *argv[] )
16 {
17     int errs = 0;
18     int rank, size, /* source, */ dest;
19     MPI_Comm      comm;
20     MPI_Status    status;
21     MPI_Request   req;
22     static int bufsizes[4] = { 1, 100, 10000, 1000000 };
23     char *buf;
24 #ifdef TEST_IRSEND
25     int veryPicky = 0;   /* Set to 1 to test "quality of implementation" in
26                             a tricky part of cancel */
27 #endif
28     int  cs, flag, n;
29
30     MTest_Init( &argc, &argv );
31
32     comm = MPI_COMM_WORLD;
33     MPI_Comm_rank( comm, &rank );
34     MPI_Comm_size( comm, &size );
35
36     /* source = 0; */
37     dest   = size - 1;
38
39     MTestPrintfMsg( 1, "Starting scancel test\n" );
40     for (cs=0; cs<4; cs++) {
41         if (rank == 0) {
42             n = bufsizes[cs];
43             buf = (char *)malloc( n );
44             if (!buf) {
45                 fprintf( stderr, "Unable to allocate %d bytes\n", n );
46                 MPI_Abort( MPI_COMM_WORLD, 1 );
47                 exit(1);
48             }
49             MTestPrintfMsg( 1, "(%d) About to create isend and cancel\n",cs );
50             MPI_Isend( buf, n, MPI_CHAR, dest, cs+n+1, comm, &req );
51             MPI_Cancel( &req );
52             MPI_Wait( &req, &status );
53             MTestPrintfMsg( 1, "Completed wait on isend\n" );
54             MPI_Test_cancelled( &status, &flag );
55             if (!flag) {
56                 errs ++;
57                 printf( "Failed to cancel an Isend request\n" );
58                 fflush(stdout);
59             }
60             else
61             {
62                 n = 0;
63             }
64             /* Send the size, zero for successfully cancelled */
65             MPI_Send( &n, 1, MPI_INT, dest, 123, comm );
66             /* Send the tag so the message can be received */
67             n = cs+n+1;
68             MPI_Send( &n, 1, MPI_INT, dest, 123, comm );
69             free( buf );
70         }
71         else if (rank == dest)
72         {
73             int nn, tag;
74             char *btemp;
75             MPI_Recv( &nn, 1, MPI_INT, 0, 123, comm, &status );
76             MPI_Recv( &tag, 1, MPI_INT, 0, 123, comm, &status );
77             if (nn > 0)
78             {
79                 /* If the message was not cancelled, receive it here */
80                 btemp = (char*)malloc( nn );
81                 if (!btemp)
82                 {
83                     fprintf( stderr, "Unable to allocate %d bytes\n", nn );
84                     MPI_Abort( MPI_COMM_WORLD, 1 );
85                     exit(1);
86                 }
87                 MPI_Recv( btemp, nn, MPI_CHAR, 0, tag, comm, &status );
88                 free(btemp);
89             }
90         }
91         MPI_Barrier( comm );
92
93         if (rank == 0) {
94             char *bsendbuf;
95             int bsendbufsize;
96             int bf, bs;
97             n = bufsizes[cs];
98             buf = (char *)malloc( n );
99             if (!buf) {
100                 fprintf( stderr, "Unable to allocate %d bytes\n", n );
101                 MPI_Abort( MPI_COMM_WORLD, 1 );
102                 exit(1);
103             }
104             bsendbufsize = n + MPI_BSEND_OVERHEAD;
105             bsendbuf = (char *)malloc( bsendbufsize );
106             if (!bsendbuf) {
107                 fprintf( stderr, "Unable to allocate %d bytes for bsend\n", n );
108                 MPI_Abort( MPI_COMM_WORLD, 1 );
109                 exit(1);
110             }
111             MPI_Buffer_attach( bsendbuf, bsendbufsize );
112             MTestPrintfMsg( 1, "About to create and cancel ibsend\n" );
113             MPI_Ibsend( buf, n, MPI_CHAR, dest, cs+n+2, comm, &req );
114             MPI_Cancel( &req );
115             MPI_Wait( &req, &status );
116             MPI_Test_cancelled( &status, &flag );
117             if (!flag) {
118                 errs ++;
119                 printf( "Failed to cancel an Ibsend request\n" );
120                 fflush(stdout);
121             }
122             else
123             {
124                 n = 0;
125             }
126             /* Send the size, zero for successfully cancelled */
127             MPI_Send( &n, 1, MPI_INT, dest, 123, comm );
128             /* Send the tag so the message can be received */
129             n = cs+n+2;
130             MPI_Send( &n, 1, MPI_INT, dest, 123, comm );
131             free( buf );
132             MPI_Buffer_detach( &bf, &bs );
133             free( bsendbuf );
134         }
135         else if (rank == dest)
136         {
137             int nn, tag;
138             char *btemp;
139             MPI_Recv( &nn, 1, MPI_INT, 0, 123, comm, &status );
140             MPI_Recv( &tag, 1, MPI_INT, 0, 123, comm, &status );
141             if (nn > 0)
142             {
143                 /* If the message was not cancelled, receive it here */
144                 btemp = (char*)malloc( nn );
145                 if (!btemp)
146                 {
147                     fprintf( stderr, "Unable to allocate %d bytes\n", nn);
148                     MPI_Abort( MPI_COMM_WORLD, 1 );
149                     exit(1);
150                 }
151                 MPI_Recv( btemp, nn, MPI_CHAR, 0, tag, comm, &status );
152                 free(btemp);
153             }
154         }
155         MPI_Barrier( comm );
156
157         /* Because this test is erroneous, we do not perform it unless
158            TEST_IRSEND is defined.  */
159 #ifdef TEST_IRSEND
160         /* We avoid ready send to self because an implementation
161            is free to detect the error in delivering a message to
162            itself without a pending receive; we could also check
163            for an error return from the MPI_Irsend */
164         if (rank == 0 && dest != rank) {
165             n = bufsizes[cs];
166             buf = (char *)malloc( n );
167             if (!buf) {
168                 fprintf( stderr, "Unable to allocate %d bytes\n", n );
169                 MPI_Abort( MPI_COMM_WORLD, 1 );
170                 exit(1);
171             }
172             MTestPrintfMsg( 1, "About to create and cancel irsend\n" );
173             MPI_Irsend( buf, n, MPI_CHAR, dest, cs+n+3, comm, &req );
174             MPI_Cancel( &req );
175             MPI_Wait( &req, &status );
176             MPI_Test_cancelled( &status, &flag );
177             /* This can be pretty ugly.  The standard is clear (Section 3.8)
178                that either a sent message is received or the 
179                sent message is successfully cancelled.  Since this message
180                can never be received, the cancel must complete
181                successfully.  
182
183                However, since there is no matching receive, this
184                program is erroneous.  In this case, we can't really
185                flag this as an error */
186             if (!flag && veryPicky) {
187                 errs ++;
188                 printf( "Failed to cancel an Irsend request\n" );
189                 fflush(stdout);
190             }
191             if (flag)
192             {
193                 n = 0;
194             }
195             /* Send the size, zero for successfully cancelled */
196             MPI_Send( &n, 1, MPI_INT, dest, 123, comm );
197             /* Send the tag so the message can be received */
198             n = cs+n+3;
199             MPI_Send( &n, 1, MPI_INT, dest, 123, comm );
200             free( buf );
201         }
202         else if (rank == dest)
203         {
204             int n, tag;
205             char *btemp;
206             MPI_Recv( &n, 1, MPI_INT, 0, 123, comm, &status );
207             MPI_Recv( &tag, 1, MPI_INT, 0, 123, comm, &status );
208             if (n > 0)
209             {
210                 /* If the message was not cancelled, receive it here */
211                 btemp = (char*)malloc( n );
212                 if (!btemp)
213                 {
214                     fprintf( stderr, "Unable to allocate %d bytes\n", n);
215                     MPI_Abort( MPI_COMM_WORLD, 1 );
216                     exit(1);
217                 }
218                 MPI_Recv( btemp, n, MPI_CHAR, 0, tag, comm, &status );
219                 free(btemp);
220             }
221         }
222         MPI_Barrier( comm );
223 #endif
224
225         if (rank == 0) {
226             n = bufsizes[cs];
227             buf = (char *)malloc( n );
228             if (!buf) {
229                 fprintf( stderr, "Unable to allocate %d bytes\n", n );
230                 MPI_Abort( MPI_COMM_WORLD, 1 );
231                 exit(1);
232             }
233             MTestPrintfMsg( 1, "About to create and cancel issend\n" );
234             MPI_Issend( buf, n, MPI_CHAR, dest, cs+n+4, comm, &req );
235             MPI_Cancel( &req );
236             MPI_Wait( &req, &status );
237             MPI_Test_cancelled( &status, &flag );
238             if (!flag) {
239                 errs ++;
240                 printf( "Failed to cancel an Issend request\n" );
241                 fflush(stdout);
242             }
243             else
244             {
245                 n = 0;
246             }
247             /* Send the size, zero for successfully cancelled */
248             MPI_Send( &n, 1, MPI_INT, dest, 123, comm );
249             /* Send the tag so the message can be received */
250             n = cs+n+4;
251             MPI_Send( &n, 1, MPI_INT, dest, 123, comm );
252             free( buf );
253         }
254         else if (rank == dest)
255         {
256             int nn, tag;
257             char *btemp;
258             MPI_Recv( &nn, 1, MPI_INT, 0, 123, comm, &status );
259             MPI_Recv( &tag, 1, MPI_INT, 0, 123, comm, &status );
260             if (nn > 0)
261             {
262                 /* If the message was not cancelled, receive it here */
263                 btemp = (char*)malloc( nn );
264                 if (!btemp)
265                 {
266                     fprintf( stderr, "Unable to allocate %d bytes\n", nn);
267                     MPI_Abort( MPI_COMM_WORLD, 1 );
268                     exit(1);
269                 }
270                 MPI_Recv( btemp, nn, MPI_CHAR, 0, tag, comm, &status );
271                 free(btemp);
272             }
273         }
274         MPI_Barrier( comm );
275     }
276
277     MTest_Finalize( errs );
278     MPI_Finalize();
279     return 0;
280 }