Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge tag 'v3_9_90' into hypervisor
[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     }
28     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
29
30     dest = 0;
31     src  = 1;
32     comm = MPI_COMM_WORLD;
33
34     n0 = n1 = n2 = 65536;
35     buf0 = (int *)malloc( n0 * sizeof(int) );
36     buf1 = (int *)malloc( n1 * sizeof(int) );
37     buf2 = (int *)malloc( n2 * sizeof(int) );
38     if (!buf0 || !buf1 || !buf2) {
39         fprintf( stderr, "Unable to allocate buffers of size %d\n", 
40                  n0 * (int)sizeof(int) );
41         MPI_Abort( MPI_COMM_WORLD, 1 );
42     }
43     memset( buf0, -1, n0 * sizeof(int) );
44     memset( buf1, -1, n0 * sizeof(int) );
45     memset( buf2, -1, n0 * sizeof(int) );
46
47     if (rank == dest) {
48         MPI_Irecv( buf0, n0, MPI_INT, src, 0, comm, &r[0] );
49         MPI_Irecv( buf1, n1, MPI_INT, src, 1, comm, &r[1] );
50         MPI_Irecv( buf2, n2, MPI_INT, src, 2, comm, &r[2] );
51         
52         MPI_Barrier( comm );
53
54         MPI_Cancel( &r[1] );
55         MPI_Barrier( comm );
56         memset( s, -1, sizeof(s) );
57         MPI_Waitall( 3, r, s );
58         MPI_Test_cancelled( &s[0], &flag );
59         if (flag) {
60             errs++;
61             printf( "request 0 was cancelled!\n" );
62         }
63         MPI_Test_cancelled( &s[1], &flag );
64         if (!flag) {
65             errs++;
66             printf( "request 1 was not cancelled!\n" );
67         }
68         MPI_Test_cancelled( &s[2], &flag );
69         if (flag) {
70             errs++;
71             printf( "request 2 was cancelled!\n" );
72         }
73         MPI_Barrier( comm );
74     }
75     if (rank == src) {
76         int tflag;
77         MPI_Barrier( comm );
78         MPI_Barrier( comm );
79         MPI_Send( buf0, n0, MPI_INT, dest, 0, comm );
80         MPI_Isend( buf2, n2, MPI_INT, dest, 2, comm, &r[1] );
81         MPI_Isend( buf1, n1, MPI_INT, dest, 4, comm, &r[0] );
82         MPI_Cancel( &r[0] );
83         memset( s, -3, sizeof(s) );
84         s[0].MPI_ERROR = -3;
85         s[1].MPI_ERROR = -3;
86         MPI_Testall( 2, r, &tflag, s );
87         if (tflag) {
88             MPI_Test_cancelled( &s[0], &flag );
89             if (!flag) {
90                 errs++;
91                 printf( "send request 0 was not cancelled!\n" );
92             }
93             MPI_Test_cancelled( &s[1], &flag );
94             if (flag) {
95                 errs++;
96                 printf( "send request 1 was cancelled!\n" );
97             }
98         }
99         else {
100             /* If all requests are not complete, then neither r nor s 
101                may be changed */
102             if ( (s[0].MPI_ERROR) != -3) {
103                 errs++;
104                 printf( "Send request status 0 modified. s[0].MPI_ERROR = %x\n",
105                         s[0].MPI_ERROR );
106             }
107             if ( (s[1].MPI_ERROR) != -3) {
108                 errs++;
109                 printf( "Send request status 1 modified. s[1].MPI_ERROR = %x\n",
110                         s[1].MPI_ERROR );
111             }
112         }
113         MPI_Barrier( comm );
114         while (!tflag) {
115             MPI_Testall( 2, r, &tflag, s );
116         }
117         MPI_Test_cancelled( &s[0], &flag );
118         if (!flag) {
119             errs++;
120             printf( "send request 0 was not cancelled!\n" );
121         }
122         MPI_Test_cancelled( &s[1], &flag );
123         if (flag) {
124             errs++;
125             printf( "send request 1 was cancelled!\n" );
126         }
127     }
128     if (rank != src && rank != dest) {
129         MPI_Barrier( comm );
130         MPI_Barrier( comm );
131         MPI_Barrier( comm );
132     }
133
134     MTest_Finalize( errs );
135     MPI_Finalize();
136
137     return 0;
138 }