Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change include order for smpi tests/examples to avoid conflicts
[simgrid.git] / teshsuite / smpi / mpich3-test / pt2pt / pscancel.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     int  cs, flag, n;
25 #ifdef TEST_IRSEND
26     int veryPicky = 0;   /* Set to 1 to test "quality of implementation" in
27                             a tricky part of cancel */
28 #endif
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     for (cs=0; cs<4; cs++) {
40         if (rank == 0) {
41             n = bufsizes[cs];
42             buf = (char *)malloc( n );
43             if (!buf) {
44                 fprintf( stderr, "Unable to allocate %d bytes\n", n );
45                 MPI_Abort( MPI_COMM_WORLD, 1 );
46                 exit(1);
47             }
48             MPI_Send_init( buf, n, MPI_CHAR, dest, cs+n+1, comm, &req );
49             MPI_Start( &req );
50             MPI_Cancel( &req );
51             MPI_Wait( &req, &status );
52             MPI_Test_cancelled( &status, &flag );
53             if (!flag) {
54                 errs ++;
55                 printf( "Failed to cancel a persistent send request\n" );
56                 fflush(stdout);
57             }
58             else
59             {
60                 n = 0;
61             }
62             MPI_Request_free( &req );
63             /* Send the size, zero for successfully cancelled */
64             MPI_Send( &n, 1, MPI_INT, dest, 123, comm );
65             /* Send the tag so the message can be received */
66             n = cs+n+1;
67             MPI_Send( &n, 1, MPI_INT, dest, 123, comm );
68             free( buf );
69         }
70         else if (rank == dest)
71         {
72             int nn, tag;
73             char *btemp;
74             MPI_Recv( &nn, 1, MPI_INT, 0, 123, comm, &status );
75             MPI_Recv( &tag, 1, MPI_INT, 0, 123, comm, &status );
76             if (nn > 0)
77             {
78                 /* If the message was not cancelled, receive it here */
79                 btemp = (char*)malloc( nn );
80                 if (!btemp)
81                 {
82                     fprintf( stderr, "Unable to allocate %d bytes\n", nn);
83                     MPI_Abort( MPI_COMM_WORLD, 1 );
84                     exit(1);
85                 }
86                 MPI_Recv( btemp, nn, MPI_CHAR, 0, tag, comm, &status );
87                 free(btemp);
88             }
89         }
90         MPI_Barrier( comm );
91
92         if (rank == 0) {
93             char *bsendbuf;
94             int bsendbufsize;
95             int bf, bs;
96             n = bufsizes[cs];
97             buf = (char *)malloc( n );
98             if (!buf) {
99                 fprintf( stderr, "Unable to allocate %d bytes\n", n );
100                 MPI_Abort( MPI_COMM_WORLD, 1 );
101                 exit(1);
102             }
103             bsendbufsize = n + MPI_BSEND_OVERHEAD;
104             bsendbuf = (char *)malloc( bsendbufsize );
105             if (!bsendbuf) {
106                 fprintf( stderr, "Unable to allocate %d bytes for bsend\n", n );
107                 MPI_Abort( MPI_COMM_WORLD, 1 );
108                 exit(1);
109             }
110             MPI_Buffer_attach( bsendbuf, bsendbufsize );
111             MPI_Bsend_init( buf, n, MPI_CHAR, dest, cs+n+2, comm, &req );
112             MPI_Start( &req );
113             MPI_Cancel( &req );
114             MPI_Wait( &req, &status );
115             MPI_Test_cancelled( &status, &flag );
116             if (!flag) {
117                 errs ++;
118                 printf( "Failed to cancel a persistent bsend request\n" );
119                 fflush(stdout);
120             }
121             else
122             {
123                 n = 0;
124             }
125             MPI_Request_free( &req );
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             MPI_Rsend_init( buf, n, MPI_CHAR, dest, cs+n+3, comm, &req );
173             MPI_Start( &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 a persistent rsend request\n" );
189                 fflush(stdout);
190             }
191             if (flag)
192             {
193                 n = 0;
194             }
195             MPI_Request_free( &req );
196             /* Send the size, zero for successfully cancelled */
197             MPI_Send( &n, 1, MPI_INT, dest, 123, comm );
198             /* Send the tag so the message can be received */
199             n = cs+n+3;
200             MPI_Send( &n, 1, MPI_INT, dest, 123, comm );
201             free( buf );
202         }
203         else if (rank == dest)
204         {
205             int n, tag;
206             char *btemp;
207             MPI_Recv( &n, 1, MPI_INT, 0, 123, comm, &status );
208             MPI_Recv( &tag, 1, MPI_INT, 0, 123, comm, &status );
209             if (n > 0)
210             {
211                 /* If the message was not cancelled, receive it here */
212                 btemp = (char*)malloc( n );
213                 if (!btemp)
214                 {
215                     fprintf( stderr, "Unable to allocate %d bytes\n", n);
216                     MPI_Abort( MPI_COMM_WORLD, 1 );
217                     exit(1);
218                 }
219                 MPI_Recv( btemp, n, MPI_CHAR, 0, tag, comm, &status );
220                 free(btemp);
221             }
222         }
223         MPI_Barrier( comm );
224 #endif
225
226         if (rank == 0) {
227             n = bufsizes[cs];
228             buf = (char *)malloc( n );
229             if (!buf) {
230                 fprintf( stderr, "Unable to allocate %d bytes\n", n );
231                 MPI_Abort( MPI_COMM_WORLD, 1 );
232                 exit(1);
233             }
234             MPI_Ssend_init( buf, n, MPI_CHAR, dest, cs+n+4, comm, &req );
235             MPI_Start( &req );
236             MPI_Cancel( &req );
237             MPI_Wait( &req, &status );
238             MPI_Test_cancelled( &status, &flag );
239             if (!flag) {
240                 errs ++;
241                 printf( "Failed to cancel a persistent ssend request\n" );
242                 fflush(stdout);
243             }
244             else
245             {
246                 n = 0;
247             }
248             MPI_Request_free( &req );
249             /* Send the size, zero for successfully cancelled */
250             MPI_Send( &n, 1, MPI_INT, dest, 123, comm );
251             /* Send the tag so the message can be received */
252             n = cs+n+4;
253             MPI_Send( &n, 1, MPI_INT, dest, 123, comm );
254             free( buf );
255         }
256         else if (rank == dest)
257         {
258             int nn, tag;
259             char *btemp;
260             MPI_Recv( &nn, 1, MPI_INT, 0, 123, comm, &status );
261             MPI_Recv( &tag, 1, MPI_INT, 0, 123, comm, &status );
262             if (nn > 0)
263             {
264                 /* If the message was not cancelled, receive it here */
265                 btemp = (char*)malloc( nn );
266                 if (!btemp)
267                 {
268                     fprintf( stderr, "Unable to allocate %d bytes\n", nn);
269                     MPI_Abort( MPI_COMM_WORLD, 1 );
270                     exit(1);
271                 }
272                 MPI_Recv( btemp, nn, MPI_CHAR, 0, tag, comm, &status );
273                 free(btemp);
274             }
275         }
276         MPI_Barrier( comm );
277     }
278
279     MTest_Finalize( errs );
280     MPI_Finalize();
281     return 0;
282 }