Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add mpich3 test suite, to replace older one.
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / cancel.c
1 /*
2  * This file shows a typical use of MPI_Cancel to free IRecv'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;
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 */
47         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
48                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
49                       MPI_COMM_WORLD, &status );
50         MPI_Irecv( buf, 10, MPI_INT, partner, 0, MPI_COMM_WORLD, &r1 );
51         MPI_Cancel( &r1 );
52         MPI_Wait( &r1, &status );
53         MPI_Test_cancelled( &status, &flag );
54         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
55                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
56                       MPI_COMM_WORLD, &status );
57         if (!flag) {
58             err++;
59             printf( "Cancel of a receive failed where it should succeed.\n" );
60         }
61
62         /* Cancel fails */
63         MPI_Irecv( buf, 10, MPI_INT, partner, 2, MPI_COMM_WORLD, &r1 );
64         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
65                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
66                       MPI_COMM_WORLD, &status );
67         MPI_Cancel( &r1 );
68         MPI_Test( &r1, &flag, &status );
69         MPI_Test_cancelled( &status, &flag );
70         /* It is technically possible for the cancel to succeed, even though
71            the message was (at least partially) delivered.  I'm leaving
72            this test in since most of the MPICH devices provide this
73            behavior. */
74         if (flag) {
75             err++;
76             printf( "Cancel of a receive succeeded where it shouldn't.\n" );
77         }
78
79         if (err) {
80             printf( "Test failed with %d errors.\n", err );
81         }
82         else {
83             printf( " No Errors\n" );
84         }
85     }
86     else if (rank == size - 1) {
87         partner = 0;
88         /* Cancel succeeds */
89         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
90                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
91                       MPI_COMM_WORLD, &status );
92         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
93                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
94                       MPI_COMM_WORLD, &status );
95         /* Cancel fails */
96         MPI_Send( buf, 3, MPI_INT, partner, 2, MPI_COMM_WORLD );
97         MPI_Sendrecv( MPI_BOTTOM, 0, MPI_INT, partner, 1,
98                       MPI_BOTTOM, 0, MPI_INT, partner, 1,
99                       MPI_COMM_WORLD, &status );
100     }
101
102     /* 
103        Next test - check that a cancel for a request receive from
104        MPI_PROC_NULL succeeds (there is some suspicion that some
105        systems can't handle this - also, MPI_REQUEST_NULL 
106
107        Note that a null request is invalid (see the various NULL comments)
108     r1 = MPI_REQUEST_NULL;
109     MPI_Cancel( &r1 );
110     */
111     MPI_Irecv( buf, 10, MPI_INT, MPI_PROC_NULL, 0, MPI_COMM_WORLD, &r1 );
112     MPI_Cancel( &r1 );
113
114     MPI_Request_free( &r1 );
115     MPI_Finalize();
116     return 0;
117 }