Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
70aa336360e881cc0cb0b8c08a1cc5413277bada
[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                 exit(1);
57             }
58             
59             MPI_Buffer_attach( buf, bufsize );
60
61             /* Initialize the buffers */
62             for (i=0; i<msgsize; i++) {
63                 msg1[i] = 0xff ^ (i & 0xff);
64                 msg2[i] = 0xff ^ (3*i & 0xff);
65                 msg3[i] = 0xff ^ (5*i & 0xff);
66             }
67
68             /* Initiate the bsends */
69             MPI_Bsend( msg1, msgsize, MPI_CHAR, dest, 0, comm );
70             MPI_Bsend( msg2, msgsize, MPI_CHAR, dest, 0, comm );
71             MPI_Bsend( msg3, msgsize, MPI_CHAR, dest, 0, comm );
72
73             /* Synchronize with our partner */
74             MPI_Sendrecv( 0, 0, MPI_CHAR, dest, 10, 
75                           0, 0, MPI_CHAR, dest, 10, comm, MPI_STATUS_IGNORE );
76
77             /* Detach the buffers.  There should be pending operations */
78             MPI_Buffer_detach ( &bufp, &outsize );
79             if (bufp != buf) {
80                 fprintf( stderr, "Wrong buffer returned\n" );
81                 errs++;
82             }
83             if (outsize != bufsize) {
84                 fprintf( stderr, "Wrong buffer size returned\n" );
85                 errs++;
86             }
87         }
88         else if (rank == dest) {
89             double tstart;
90
91             /* Clear the message buffers */
92             for (i=0; i<msgsize; i++) {
93                 msg1[i] = 0;
94                 msg2[i] = 0;
95                 msg3[i] = 0;
96             }
97
98             /* Wait for the synchronize */
99             MPI_Sendrecv( 0, 0, MPI_CHAR, source, 10, 
100                           0, 0, MPI_CHAR, source, 10, comm, MPI_STATUS_IGNORE );
101
102             /* Wait 2 seconds */
103             tstart = MPI_Wtime();
104             while (MPI_Wtime() - tstart < 2.0) ;
105
106             /* Now receive the messages */
107             MPI_Recv( msg1, msgsize, MPI_CHAR, source, 0, comm, &status1 );
108             MPI_Recv( msg2, msgsize, MPI_CHAR, source, 0, comm, &status2 );
109             MPI_Recv( msg3, msgsize, MPI_CHAR, source, 0, comm, &status3 );
110
111             /* Check that we have the correct data */
112             for (i=0; i<msgsize; i++) {
113                 if (msg1[i] != (0xff ^ (i & 0xff))) { 
114                     if (errs < 10) {
115                         fprintf( stderr, "msg1[%d] = %d\n", i, msg1[i] );
116                     }
117                     errs++;
118                 }
119                 if (msg2[i] != (0xff ^ (3*i & 0xff))) {
120                     if (errs < 10) {
121                         fprintf( stderr, "msg2[%d] = %d\n", i, msg2[i] );
122                     }
123                     errs++;
124                 }
125                 if (msg3[i] != (0xff ^ (5*i & 0xff))) {
126                     if (errs < 10) {
127                         fprintf( stderr, "msg2[%d] = %d\n", i, msg2[i] );
128                     }
129                     errs++;
130                 }
131             }
132             
133         }
134                 
135         
136         MTestFreeComm( &comm );
137     }
138     free( msg1 );
139
140     MTest_Finalize( errs );
141     MPI_Finalize();
142     return 0;
143 }