Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / teshsuite / smpi / mpich3-test / pt2pt / bsendfrag.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 <stdio.h>
8 #include <stdlib.h>
9 #include "mpi.h"
10 #include "mpitest.h"
11
12 /*
13 static char MTEST_Descrip[] = "Test bsend message handling where \
14 different messages are received in different orders";
15 */
16
17 /*
18  * Notes on the test.
19  *
20  * To ensure that messages remain in the bsend buffer until received,
21  * messages are sent with size MSG_SIZE (ints).  
22  */
23
24 #define MSG_SIZE 17000
25
26 int main( int argc, char *argv[] )
27 {
28     int errs = 0;
29     int b1[MSG_SIZE], b2[MSG_SIZE], b3[MSG_SIZE], b4[MSG_SIZE];
30     int src, dest, size, rank, i;
31     MPI_Comm comm;
32     MPI_Status status;
33
34     MTest_Init( &argc, &argv );
35
36     MPI_Errhandler_set( MPI_COMM_WORLD, MPI_ERRORS_RETURN );
37
38     comm = MPI_COMM_WORLD;
39     MPI_Comm_rank( comm, &rank );
40     MPI_Comm_size( comm, &size );
41
42     if (size < 2) {
43         errs++;
44         fprintf( stderr, "At least 2 processes required\n" );
45         MPI_Abort( MPI_COMM_WORLD, 1 );
46     }
47
48     src  = 0;
49     dest = 1;
50
51     if (rank == src) {
52         int *buf, bufsize, bsize;
53
54         bufsize = 4 * (MSG_SIZE * sizeof(int) + MPI_BSEND_OVERHEAD);
55         buf = (int *)malloc( bufsize );
56         if (!buf) {
57             fprintf( stderr, "Could not allocate buffer of %d bytes\n", 
58                      bufsize );
59             MPI_Abort( MPI_COMM_WORLD, 1 );
60         }
61         MPI_Buffer_attach( buf, bufsize );
62
63         /* Initialize data */
64         for (i=0; i<MSG_SIZE; i++) {
65             b1[i] = i;
66             b2[i] = MSG_SIZE + i;
67             b3[i] = 2 * MSG_SIZE + i;
68             b4[i] = 3 * MSG_SIZE + i;
69         }
70         /* Send and reset buffers after bsend returns */
71         MPI_Bsend( b1, MSG_SIZE, MPI_INT, dest, 0, comm );
72         for (i=0; i<MSG_SIZE; i++) b1[i] = -b1[i];
73         MPI_Bsend( b2, MSG_SIZE, MPI_INT, dest, 1, comm );
74         for (i=0; i<MSG_SIZE; i++) b2[i] = -b2[i];
75         MPI_Bsend( b3, MSG_SIZE, MPI_INT, dest, 2, comm );
76         for (i=0; i<MSG_SIZE; i++) b3[i] = -b3[i];
77         MPI_Bsend( b4, MSG_SIZE, MPI_INT, dest, 3, comm );
78         for (i=0; i<MSG_SIZE; i++) b4[i] = -b4[i];
79
80         MPI_Barrier( comm );
81         /* Detach waits until all messages received */
82         MPI_Buffer_detach( &buf, &bsize );
83     }
84     else if (rank == dest) {
85         
86         MPI_Barrier( comm );
87         MPI_Recv( b2, MSG_SIZE, MPI_INT, src, 1, comm, &status );
88         MPI_Recv( b1, MSG_SIZE, MPI_INT, src, 0, comm, &status );
89         MPI_Recv( b4, MSG_SIZE, MPI_INT, src, 3, comm, &status );
90         MPI_Recv( b3, MSG_SIZE, MPI_INT, src, 2, comm, &status );
91
92         /* Check received data */
93         for (i=0; i<MSG_SIZE; i++) {
94             if (b1[i] != i) {
95                 errs++;
96                 if (errs < 16) printf( "b1[%d] is %d\n", i, b1[i] );
97             }
98             if (b2[i] != MSG_SIZE + i) {
99                 errs++;
100                 if (errs < 16) printf( "b2[%d] is %d\n", i, b2[i] );
101             }
102             if (b3[i] != 2 * MSG_SIZE + i) {
103                 errs++;
104                 if (errs < 16) printf( "b3[%d] is %d\n", i, b3[i] );
105             }
106             if (b4[i] != 3 * MSG_SIZE + i) {
107                 errs++;
108                 if (errs < 16) printf( "b4[%d] is %d\n", i, b4[i] );
109             }
110         }
111     }
112     else {
113         MPI_Barrier( comm );
114     }
115
116
117     MTest_Finalize( errs );
118     MPI_Finalize();
119     return 0;
120   
121 }