Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8ef0062028c44c957c8450ec7ac5bdd6225773bd
[simgrid.git] / teshsuite / smpi / mpich3-test / pt2pt / bsend1.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2001 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 #include "mpitestconf.h"
11 #ifdef HAVE_STRING_H
12 #include <string.h>
13 #endif
14
15 /* 
16  * This is a simple program that tests bsend.  It may be run as a single
17  * process to simplify debugging; in addition, bsend allows send-to-self
18  * programs.
19  */
20 int main( int argc, char *argv[] )
21 {
22     MPI_Comm comm = MPI_COMM_WORLD;
23     int dest = 0, src = 0, tag = 1;
24     int s1, s2, s3;
25     char *buf, *bbuf;
26     char msg1[7], msg3[17];
27     double msg2[2];
28     char rmsg1[64], rmsg3[64];
29     double rmsg2[64];
30     int errs = 0, rank;
31     int bufsize, bsize;
32
33     MTest_Init( &argc, &argv );
34     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
35
36     /* According to the standard, we must use the PACK_SIZE length of each
37        message in the computation of the message buffer size */
38     MPI_Pack_size( 7, MPI_CHAR, comm, &s1 );
39     MPI_Pack_size( 2, MPI_DOUBLE, comm, &s2 );
40     MPI_Pack_size( 17, MPI_CHAR, comm, &s3 );
41     bufsize = 3 * MPI_BSEND_OVERHEAD + s1 + s2 + s3;
42     buf = (char *)malloc( bufsize );
43     MPI_Buffer_attach( buf, bufsize );
44
45     strncpy( msg1, "012345", 7 );
46     strncpy( msg3, "0123401234012341", 17 );
47     msg2[0] = 1.23; msg2[1] = 3.21;
48
49     if (rank == src) {
50         /* These message sizes are chosen to expose any alignment problems */
51         MPI_Bsend( msg1, 7, MPI_CHAR, dest, tag, comm );
52         MPI_Bsend( msg2, 2, MPI_DOUBLE, dest, tag, comm );
53         MPI_Bsend( msg3, 17, MPI_CHAR, dest, tag, comm );
54     }
55
56     if (rank == dest) {
57         MPI_Recv( rmsg1, 7, MPI_CHAR, src, tag, comm, MPI_STATUS_IGNORE );
58         MPI_Recv( rmsg2, 10, MPI_DOUBLE, src, tag, comm, MPI_STATUS_IGNORE );
59         MPI_Recv( rmsg3, 17, MPI_CHAR, src, tag, comm, MPI_STATUS_IGNORE );
60
61         if (strcmp( rmsg1, msg1 ) != 0) {
62             errs++;
63             fprintf( stderr, "message 1 (%s) should be %s\n", rmsg1, msg1 );
64         }
65         if (rmsg2[0] != msg2[0] || rmsg2[1] != msg2[1]) {
66             errs++;
67             fprintf( stderr, 
68           "message 2 incorrect, values are (%f,%f) but should be (%f,%f)\n",
69                      rmsg2[0], rmsg2[1], msg2[0], msg2[1] );
70         }
71         if (strcmp( rmsg3, msg3 ) != 0) {
72             errs++;
73             fprintf( stderr, "message 3 (%s) should be %s\n", rmsg3, msg3 );
74         }
75     }
76
77     /* We can't guarantee that messages arrive until the detach */
78     MPI_Buffer_detach( &bbuf, &bsize );
79
80     MTest_Finalize( errs );
81     
82     MPI_Finalize();
83     return 0;
84 }