Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MPI_Abort can theorically fail. Add a call to exit() to ensure that the program...
[simgrid.git] / teshsuite / smpi / mpich3-test / pt2pt / cancelrecv.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2006 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 #include <string.h>   /* For memset */
11
12 int main( int argc, char *argv[] )
13 {
14     MPI_Request r[3];
15     MPI_Status  s[3];
16     int *buf0, *buf1, *buf2;
17     int rank, size, src, dest, flag, errs = 0;
18     int n0, n1, n2;
19     MPI_Comm comm;
20
21     MTest_Init( &argc, &argv );
22
23     MPI_Comm_size( MPI_COMM_WORLD, &size );
24     if (size < 2) {
25         fprintf( stderr, "Must run with at least 2 processes\n" );
26         MPI_Abort( MPI_COMM_WORLD, 1 );
27         exit(1);
28     }
29     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
30
31     dest = 0;
32     src  = 1;
33     comm = MPI_COMM_WORLD;
34
35     n0 = n1 = n2 = 65536;
36     buf0 = (int *)malloc( n0 * sizeof(int) );
37     buf1 = (int *)malloc( n1 * sizeof(int) );
38     buf2 = (int *)malloc( n2 * sizeof(int) );
39     if (!buf0 || !buf1 || !buf2) {
40         fprintf( stderr, "Unable to allocate buffers of size %d\n", 
41                  n0 * (int)sizeof(int) );
42         MPI_Abort( MPI_COMM_WORLD, 1 );
43         exit(1);
44     }
45     memset( buf0, -1, n0 * sizeof(int) );
46     memset( buf1, -1, n0 * sizeof(int) );
47     memset( buf2, -1, n0 * sizeof(int) );
48
49     if (rank == dest) {
50         MPI_Irecv( buf0, n0, MPI_INT, src, 0, comm, &r[0] );
51         MPI_Irecv( buf1, n1, MPI_INT, src, 1, comm, &r[1] );
52         MPI_Irecv( buf2, n2, MPI_INT, src, 2, comm, &r[2] );
53         
54         MPI_Barrier( comm );
55
56         MPI_Cancel( &r[1] );
57         MPI_Barrier( comm );
58         memset( s, -1, sizeof(s) );
59         MPI_Waitall( 3, r, s );
60         MPI_Test_cancelled( &s[0], &flag );
61         if (flag) {
62             errs++;
63             printf( "request 0 was cancelled!\n" );
64         }
65         MPI_Test_cancelled( &s[1], &flag );
66         if (!flag) {
67             errs++;
68             printf( "request 1 was not cancelled!\n" );
69         }
70         MPI_Test_cancelled( &s[2], &flag );
71         if (flag) {
72             errs++;
73             printf( "request 2 was cancelled!\n" );
74         }
75         MPI_Barrier( comm );
76     }
77     if (rank == src) {
78         int tflag;
79         MPI_Barrier( comm );
80         MPI_Barrier( comm );
81         MPI_Send( buf0, n0, MPI_INT, dest, 0, comm );
82         MPI_Isend( buf2, n2, MPI_INT, dest, 2, comm, &r[1] );
83         MPI_Isend( buf1, n1, MPI_INT, dest, 4, comm, &r[0] );
84         MPI_Cancel( &r[0] );
85         memset( s, -3, sizeof(s) );
86         s[0].MPI_ERROR = -3;
87         s[1].MPI_ERROR = -3;
88         MPI_Testall( 2, r, &tflag, s );
89         if (tflag) {
90             MPI_Test_cancelled( &s[0], &flag );
91             if (!flag) {
92                 errs++;
93                 printf( "send request 0 was not cancelled!\n" );
94             }
95             MPI_Test_cancelled( &s[1], &flag );
96             if (flag) {
97                 errs++;
98                 printf( "send request 1 was cancelled!\n" );
99             }
100         }
101         else {
102             /* If all requests are not complete, then neither r nor s 
103                may be changed */
104             if ( (s[0].MPI_ERROR) != -3) {
105                 errs++;
106                 printf( "Send request status 0 modified. s[0].MPI_ERROR = %x\n",
107                         s[0].MPI_ERROR );
108             }
109             if ( (s[1].MPI_ERROR) != -3) {
110                 errs++;
111                 printf( "Send request status 1 modified. s[1].MPI_ERROR = %x\n",
112                         s[1].MPI_ERROR );
113             }
114         }
115         MPI_Barrier( comm );
116         while (!tflag) {
117             MPI_Testall( 2, r, &tflag, s );
118         }
119         MPI_Test_cancelled( &s[0], &flag );
120         if (!flag) {
121             errs++;
122             printf( "send request 0 was not cancelled!\n" );
123         }
124         MPI_Test_cancelled( &s[1], &flag );
125         if (flag) {
126             errs++;
127             printf( "send request 1 was cancelled!\n" );
128         }
129     }
130     if (rank != src && rank != dest) {
131         MPI_Barrier( comm );
132         MPI_Barrier( comm );
133         MPI_Barrier( comm );
134     }
135
136     MTest_Finalize( errs );
137     MPI_Finalize();
138
139     return 0;
140 }