Logo AND Algorithmique Numérique Distribuée

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