Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'surf++'
[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         exit(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     }
84
85     if (rank == dest) {
86         MPI_Request r[5];
87         int i;
88
89         for (i=0; i<5; i++) {
90             MPI_Irecv( &rmsg[i], 1, MPI_INT, src, tag+i, comm, &r[i] );
91         }
92         if (rank != src) /* Just in case rank == src */
93             MPI_Barrier( MPI_COMM_WORLD );
94
95         for (i=0; i<4; i++) {
96             MPI_Wait( &r[i], MPI_STATUS_IGNORE );
97             if (rmsg[i] != 10+i) {
98                 errs++;
99                 fprintf( stderr, "message %d (%d) should be %d\n", i, rmsg[i], 10+i );
100             }
101         }
102         /* The MPI standard says that there is no way to use MPI_Request_free
103            safely with receive requests.  A strict MPI implementation may
104            choose to consider these erroreous (an IBM MPI implementation
105            does so)  */
106 #ifdef USE_STRICT_MPI
107         MPI_Wait( &r[4], MPI_STATUS_IGNORE );
108 #else
109         MTestPrintfMsg( 10, "About  free Irecv request\n" );
110         MPI_Request_free( &r[4] ); 
111 #endif
112     }
113
114     if (rank != dest && rank != src) {
115         MPI_Barrier( MPI_COMM_WORLD );
116     }
117
118
119     MTest_Finalize( errs );
120
121     MPI_Finalize();
122
123     return 0;
124 }