Logo AND Algorithmique Numérique Distribuée

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