Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add mpich3 test suite, to replace older one.
[simgrid.git] / teshsuite / smpi / mpich3-test / pt2pt / bsendpending.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include "mpi.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "mpitest.h"
11
12 /*
13 static char MTEST_Descrip[] = "Test the handling of BSend operations when a detach occurs before the bsend data has been sent.";
14 */
15
16 int main( int argc, char *argv[] )
17 {
18     int errs = 0;
19     int rank, size, source, dest;
20     unsigned char *buf, *bufp;
21     int minsize = 2; 
22     int i, msgsize, bufsize, outsize;
23     unsigned char *msg1, *msg2, *msg3;
24     MPI_Comm      comm;
25     MPI_Status    status1, status2, status3;
26
27     MTest_Init( &argc, &argv );
28
29     /* The following illustrates the use of the routines to 
30        run through a selection of communicators and datatypes.
31        Use subsets of these for tests that do not involve combinations 
32        of communicators, datatypes, and counts of datatypes */
33     msgsize = 128 * 1024;
34     msg1 = (unsigned char *)malloc( 3 * msgsize );
35     msg2 = msg1 + msgsize;
36     msg3 = msg2 + msgsize;
37     while (MTestGetIntracommGeneral( &comm, minsize, 1 )) {
38         if (comm == MPI_COMM_NULL) continue;
39         /* Determine the sender and receiver */
40         MPI_Comm_rank( comm, &rank );
41         MPI_Comm_size( comm, &size );
42         source = 0;
43         dest   = size - 1;
44
45         /* Here is the test:  The sender */
46         if (rank == source) {
47             /* Get a bsend buffer.  Make it large enough that the Bsend
48                internals will (probably) not use a eager send for the data.
49                Have three such messages */
50             bufsize = 3 * (MPI_BSEND_OVERHEAD + msgsize);
51             buf     = (unsigned char *)malloc( bufsize );
52             if (!buf) {
53                 fprintf( stderr, "Unable to allocate a buffer of %d bytes\n",
54                          bufsize );
55                 MPI_Abort( MPI_COMM_WORLD, 1 );
56             }
57             
58             MPI_Buffer_attach( buf, bufsize );
59
60             /* Initialize the buffers */
61             for (i=0; i<msgsize; i++) {
62                 msg1[i] = 0xff ^ (i & 0xff);
63                 msg2[i] = 0xff ^ (3*i & 0xff);
64                 msg3[i] = 0xff ^ (5*i & 0xff);
65             }
66
67             /* Initiate the bsends */
68             MPI_Bsend( msg1, msgsize, MPI_CHAR, dest, 0, comm );
69             MPI_Bsend( msg2, msgsize, MPI_CHAR, dest, 0, comm );
70             MPI_Bsend( msg3, msgsize, MPI_CHAR, dest, 0, comm );
71
72             /* Synchronize with our partner */
73             MPI_Sendrecv( 0, 0, MPI_CHAR, dest, 10, 
74                           0, 0, MPI_CHAR, dest, 10, comm, MPI_STATUS_IGNORE );
75
76             /* Detach the buffers.  There should be pending operations */
77             MPI_Buffer_detach ( &bufp, &outsize );
78             if (bufp != buf) {
79                 fprintf( stderr, "Wrong buffer returned\n" );
80                 errs++;
81             }
82             if (outsize != bufsize) {
83                 fprintf( stderr, "Wrong buffer size returned\n" );
84                 errs++;
85             }
86         }
87         else if (rank == dest) {
88             double tstart;
89
90             /* Clear the message buffers */
91             for (i=0; i<msgsize; i++) {
92                 msg1[i] = 0;
93                 msg2[i] = 0;
94                 msg3[i] = 0;
95             }
96
97             /* Wait for the synchronize */
98             MPI_Sendrecv( 0, 0, MPI_CHAR, source, 10, 
99                           0, 0, MPI_CHAR, source, 10, comm, MPI_STATUS_IGNORE );
100
101             /* Wait 2 seconds */
102             tstart = MPI_Wtime();
103             while (MPI_Wtime() - tstart < 2.0) ;
104
105             /* Now receive the messages */
106             MPI_Recv( msg1, msgsize, MPI_CHAR, source, 0, comm, &status1 );
107             MPI_Recv( msg2, msgsize, MPI_CHAR, source, 0, comm, &status2 );
108             MPI_Recv( msg3, msgsize, MPI_CHAR, source, 0, comm, &status3 );
109
110             /* Check that we have the correct data */
111             for (i=0; i<msgsize; i++) {
112                 if (msg1[i] != (0xff ^ (i & 0xff))) { 
113                     if (errs < 10) {
114                         fprintf( stderr, "msg1[%d] = %d\n", i, msg1[i] );
115                     }
116                     errs++;
117                 }
118                 if (msg2[i] != (0xff ^ (3*i & 0xff))) {
119                     if (errs < 10) {
120                         fprintf( stderr, "msg2[%d] = %d\n", i, msg2[i] );
121                     }
122                     errs++;
123                 }
124                 if (msg3[i] != (0xff ^ (5*i & 0xff))) {
125                     if (errs < 10) {
126                         fprintf( stderr, "msg2[%d] = %d\n", i, msg2[i] );
127                     }
128                     errs++;
129                 }
130             }
131             
132         }
133                 
134         
135         MTestFreeComm( &comm );
136     }
137     free( msg1 );
138
139     MTest_Finalize( errs );
140     MPI_Finalize();
141     return 0;
142 }