Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[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)
39             continue;
40         /* Determine the sender and receiver */
41         MPI_Comm_rank(comm, &rank);
42         MPI_Comm_size(comm, &size);
43         source = 0;
44         dest = size - 1;
45
46         /* Here is the test:  The sender */
47         if (rank == source) {
48             /* Get a bsend buffer.  Make it large enough that the Bsend
49              * internals will (probably) not use a eager send for the data.
50              * Have three such messages */
51             bufsize = 3 * (MPI_BSEND_OVERHEAD + msgsize);
52             buf = (unsigned char *) malloc(bufsize);
53             if (!buf) {
54                 fprintf(stderr, "Unable to allocate a buffer of %d bytes\n", 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_UNSIGNED_CHAR, dest, 0, comm);
69             MPI_Bsend(msg2, msgsize, MPI_UNSIGNED_CHAR, dest, 0, comm);
70             MPI_Bsend(msg3, msgsize, MPI_UNSIGNED_CHAR, dest, 0, comm);
71
72             /* Synchronize with our partner */
73             MPI_Sendrecv(NULL, 0, MPI_UNSIGNED_CHAR, dest, 10,
74                          NULL, 0, MPI_UNSIGNED_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             free(buf);
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(NULL, 0, MPI_UNSIGNED_CHAR, source, 10,
100                          NULL, 0, MPI_UNSIGNED_CHAR, source, 10, comm, MPI_STATUS_IGNORE);
101
102             /* Wait 2 seconds */
103 /*            tstart = MPI_Wtime();*/
104 /*            while (MPI_Wtime() - tstart < 2.0);*/
105             sleep(2);
106             /* Now receive the messages */
107             MPI_Recv(msg1, msgsize, MPI_UNSIGNED_CHAR, source, 0, comm, &status1);
108             MPI_Recv(msg2, msgsize, MPI_UNSIGNED_CHAR, source, 0, comm, &status2);
109             MPI_Recv(msg3, msgsize, MPI_UNSIGNED_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 }