Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'v3_8_x'
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / cancelissend.c
1 /****************************************************************************
2
3   MESSAGE PASSING INTERFACE TEST CASE SUITE
4   
5   Copyright IBM Corp. 1995
6         
7   IBM Corp. hereby grants a non-exclusive license to use, copy, modify, and
8   distribute this software for any purpose and without fee provided that the
9   above copyright notice and the following paragraphs appear in all copies.
10           
11   IBM Corp. makes no representation that the test cases comprising this
12   suite are correct or are an accurate representation of any standard.
13                 
14   In no event shall IBM be liable to any party for direct, indirect, special
15   incidental, or consequential damage arising out of the use of this software
16   even if IBM Corp. has been advised of the possibility of such damage.
17                   
18   IBM CORP. SPECIFICALLY DISCLAIMS ANY WARRANTIES INCLUDING, BUT NOT LIMITED
19   TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20   PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS AND IBM
21   CORP. HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
22   ENHANCEMENTS, OR MODIFICATIONS.
23                         
24   ****************************************************************************
25                           
26   These test cases reflect an interpretation of the MPI Standard.  They are
27   are, in most cases, unit tests of specific MPI behaviors.  If a user of any
28   test case from this set believes that the MPI Standard requires behavior
29   different than that implied by the test case we would appreciate feedback.
30   
31   Comments may be sent to:
32   Richard Treumann
33   treumann@kgn.ibm.com
34                                   
35   ****************************************************************************
36 */
37 /* 
38  * WDG - July 6, 2004
39  *
40  * This is a modified version that:
41  *  Uses a shorter message (in case the implementation uses eager delivery
42  *  even with synchronous send)
43  *  Allows control of which process is executing the Issend and which the
44  *  receive (to simplify debugging)
45  */
46 #include <stdio.h>
47 #include "mpi.h"
48
49 int main(int argc, char *argv[])
50 {
51         int me, tasks, data, flag;
52         int err0 = 0;
53         int err1 = 0;
54         int errs, toterrs;
55         int master = 1, worker = 0;
56         MPI_Request request;
57         MPI_Status status;
58
59         MPI_Init(&argc,&argv);
60         MPI_Comm_rank(MPI_COMM_WORLD,&me);
61         MPI_Comm_size(MPI_COMM_WORLD,&tasks);
62
63         if (tasks < 2) {
64             printf( "Cancel test requires at least 2 processes\n" );
65             MPI_Abort( MPI_COMM_WORLD, 1 );
66         }
67
68         /* The original test sent 10000 elements with Issend.  This
69            one uses less data but keeps the array the same size */
70         { int data[100000]; if (me == master)  
71         {
72                 MPI_Irecv(data, 1, MPI_INT, worker, 1, MPI_COMM_WORLD,&request);
73                 MPI_Cancel(&request);
74                 MPI_Wait(&request,&status);
75                 MPI_Test_cancelled(&status,&flag);
76                 if (!flag) {
77                     err0++;
78                     printf("task %d ERROR: Receive request not cancelled!\n", me);
79                 }
80
81                 /* This is short enough to use eager but because it is
82                    a Synchronous send, it must still be possible to 
83                    cancel it, even when it is a short message */
84                 MPI_Issend(data, 100, MPI_INT, worker, 1, MPI_COMM_WORLD,&request);
85                 MPI_Cancel(&request);
86                 for (flag = 0;; )  
87                 {
88                         MPI_Test(&request,&flag,&status);
89                         if (flag) break;
90                 }
91                 
92                 MPI_Test_cancelled(&status,&flag);
93                 if (!flag) {
94                     err0++;
95                     printf("task %d ERROR: Send request not cancelled! (1)\n", me);
96                 }
97         }}
98         
99         if (me == master)  
100         {
101                 data = 5;
102                 MPI_Isend(&data, 1, MPI_INT, worker, 1, MPI_COMM_WORLD,&request);
103                 MPI_Cancel(&request);
104                 MPI_Wait(&request,&status);
105                 MPI_Test_cancelled(&status,&flag);
106                 if (!flag) {
107                     err0++;
108                     printf("task %d ERROR: Send request not cancelled! (2)\n", me);
109                 }
110                 MPI_Barrier(MPI_COMM_WORLD);
111                 status.MPI_TAG=MPI_SUCCESS;
112                 data = 6;
113                 MPI_Send(&data, 1, MPI_INT, worker, 5, MPI_COMM_WORLD);
114                 
115                 MPI_Isend(&data, 1, MPI_INT, worker, 1, MPI_COMM_WORLD,&request);
116                 MPI_Barrier(MPI_COMM_WORLD);
117                 MPI_Cancel(&request);
118                 MPI_Wait(&request,&status);
119                 MPI_Test_cancelled(&status,&flag);
120                 if (flag) {
121                     err0++;
122                     printf("task %d ERROR: Send request cancelled!\n", me);
123                 }
124         } 
125         else if (me == worker) 
126         {
127             MPI_Barrier(MPI_COMM_WORLD);
128             data = 0;
129             MPI_Recv(&data, 1, MPI_INT, master, 1, MPI_COMM_WORLD,&status);
130             if (data != 6) {
131                 err1++;
132                 printf("task %d ERROR: Send request not cancelled!\n", me);
133             }
134             
135             MPI_Recv(&data, 1, MPI_INT, master, 5, MPI_COMM_WORLD,&status);
136             if (data != 6) {
137                 err1++;
138                 printf("task %d ERROR: Send request not cancelled!\n", me);
139             }
140             MPI_Barrier(MPI_COMM_WORLD);
141         }
142         else {
143             /* These are needed when the size of MPI_COMM_WORLD > 2 */
144             MPI_Barrier( MPI_COMM_WORLD );
145             MPI_Barrier( MPI_COMM_WORLD );
146         }
147         
148         errs = err0 + err1;
149         MPI_Reduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD );
150             
151         if ( errs ) {
152             printf( "Test failed with %d errors.\n", errs );
153         }
154         if (me == 0 && toterrs == 0) {
155             printf( " No Errors\n" );
156         }
157               
158         MPI_Finalize();
159         return 0;
160 }