Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Free memory.
[simgrid.git] / teshsuite / smpi / mpich3-test / pt2pt / rqfreeb.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 #define USE_STRICT_MPI 1 
11 /* Test Ibsend and Request_free */
12 int main(int argc, char *argv[])
13 {
14     MPI_Comm comm = MPI_COMM_WORLD;
15     int dest = 1, src = 0, tag = 1;
16     int s1;
17     char *buf, *bbuf;
18     int smsg[5], rmsg[5];
19     int errs = 0, rank, size;
20     int bufsize, bsize;
21
22     MTest_Init(&argc, &argv);
23     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
24     MPI_Comm_size(MPI_COMM_WORLD, &size);
25     if (src >= size || dest >= size) {
26         int r = src;
27         if (dest > r)
28             r = dest;
29         fprintf(stderr, "This program requires %d processes\n", r - 1);
30         MPI_Abort(MPI_COMM_WORLD, 1);
31     }
32
33     if (rank == src) {
34         MPI_Request r;
35
36         MPI_Barrier(MPI_COMM_WORLD);
37
38         /* According to the standard, we must use the PACK_SIZE length of each
39          * message in the computation of the message buffer size */
40         MPI_Pack_size(5, MPI_INT, comm, &s1);
41         bufsize = MPI_BSEND_OVERHEAD + s1 + 2000;
42         buf = (char *) malloc(bufsize);
43         MPI_Buffer_attach(buf, bufsize);
44
45         MTestPrintfMsg(10, "About create and free Isend request\n");
46         smsg[0] = 10;
47         MPI_Isend(&smsg[0], 1, MPI_INT, dest, tag, comm, &r);
48         MPI_Request_free(&r);
49         if (r != MPI_REQUEST_NULL) {
50             errs++;
51             fprintf(stderr, "Request not set to NULL after request free\n");
52         }
53         MTestPrintfMsg(10, "About create and free Ibsend request\n");
54         smsg[1] = 11;
55         MPI_Ibsend(&smsg[1], 1, MPI_INT, dest, tag + 1, comm, &r);
56         MPI_Request_free(&r);
57         if (r != MPI_REQUEST_NULL) {
58             errs++;
59             fprintf(stderr, "Request not set to NULL after request free\n");
60         }
61         MTestPrintfMsg(10, "About create and free Issend request\n");
62         smsg[2] = 12;
63         MPI_Issend(&smsg[2], 1, MPI_INT, dest, tag + 2, comm, &r);
64         MPI_Request_free(&r);
65         if (r != MPI_REQUEST_NULL) {
66             errs++;
67             fprintf(stderr, "Request not set to NULL after request free\n");
68         }
69         MTestPrintfMsg(10, "About create and free Irsend request\n");
70         smsg[3] = 13;
71         MPI_Irsend(&smsg[3], 1, MPI_INT, dest, tag + 3, comm, &r);
72         MPI_Request_free(&r);
73         if (r != MPI_REQUEST_NULL) {
74             errs++;
75             fprintf(stderr, "Request not set to NULL after request free\n");
76         }
77         smsg[4] = 14;
78         MPI_Isend(&smsg[4], 1, MPI_INT, dest, tag + 4, comm, &r);
79         MPI_Wait(&r, MPI_STATUS_IGNORE);
80
81         /* We can't guarantee that messages arrive until the detach */
82         MPI_Buffer_detach(&bbuf, &bsize);
83         free(buf);
84     }
85
86     if (rank == dest) {
87         MPI_Request r[5];
88         int i;
89
90         for (i = 0; i < 5; i++) {
91             MPI_Irecv(&rmsg[i], 1, MPI_INT, src, tag + i, comm, &r[i]);
92         }
93         if (rank != src)        /* Just in case rank == src */
94             MPI_Barrier(MPI_COMM_WORLD);
95
96         for (i = 0; i < 4; i++) {
97             MPI_Wait(&r[i], MPI_STATUS_IGNORE);
98             if (rmsg[i] != 10 + i) {
99                 errs++;
100                 fprintf(stderr, "message %d (%d) should be %d\n", i, rmsg[i], 10 + i);
101             }
102         }
103         /* The MPI standard says that there is no way to use MPI_Request_free
104          * safely with receive requests.  A strict MPI implementation may
105          * choose to consider these erroreous (an IBM MPI implementation
106          * does so)  */
107 #ifdef USE_STRICT_MPI
108         MPI_Wait(&r[4], MPI_STATUS_IGNORE);
109 #else
110         MTestPrintfMsg(10, "About  free Irecv request\n");
111         MPI_Request_free(&r[4]);
112 #endif
113     }
114
115     if (rank != dest && rank != src) {
116         MPI_Barrier(MPI_COMM_WORLD);
117     }
118
119
120     MTest_Finalize(errs);
121
122     MPI_Finalize();
123
124     return 0;
125 }