Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Free memory.
[simgrid.git] / teshsuite / smpi / mpich3-test / pt2pt / probe-unexp.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 <stdio.h>
7 #include "mpi.h"
8 #include "mpitest.h"
9
10 #define MAX_BUF_SIZE_LG 22
11 #define NUM_MSGS_PER_BUF_SIZE 5
12 char buf[1 << MAX_BUF_SIZE_LG];
13
14 /*
15  * This program verifies that MPI_Probe() is operating properly in the face of
16  * unexpected messages arriving after MPI_Probe() has
17  * been called.  This program may hang if MPI_Probe() does not return when the
18  * message finally arrives (see req #375).
19  */
20 int main(int argc, char **argv)
21 {
22     int p_size;
23     int p_rank;
24     int msg_size_lg;
25     int errs = 0;
26     int mpi_errno;
27
28     MTest_Init(&argc, &argv);
29
30     MPI_Comm_size(MPI_COMM_WORLD, &p_size);
31     MPI_Comm_rank(MPI_COMM_WORLD, &p_rank);
32     /* To improve reporting of problems about operations, we
33      * change the error handler to errors return */
34     MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
35
36
37     for (msg_size_lg = 0; msg_size_lg <= MAX_BUF_SIZE_LG; msg_size_lg++) {
38         const int msg_size = 1 << msg_size_lg;
39         int msg_cnt;
40
41         MTestPrintfMsg(2, "testing messages of size %d\n", msg_size);
42         for (msg_cnt = 0; msg_cnt < NUM_MSGS_PER_BUF_SIZE; msg_cnt++) {
43             MPI_Status status;
44             const int tag = msg_size_lg * NUM_MSGS_PER_BUF_SIZE + msg_cnt;
45
46             MTestPrintfMsg(2, "Message count %d\n", msg_cnt);
47             if (p_rank == 0) {
48                 int p;
49
50                 for (p = 1; p < p_size; p++) {
51                     /* Wait for synchronization message */
52                     mpi_errno = MPI_Recv(NULL, 0, MPI_BYTE, MPI_ANY_SOURCE,
53                                          tag, MPI_COMM_WORLD, &status);
54                     if (mpi_errno != MPI_SUCCESS && errs++ < 10) {
55                         MTestPrintError(mpi_errno);
56                     }
57
58                     if (status.MPI_TAG != tag && errs++ < 10) {
59                         printf
60                             ("ERROR: unexpected message tag from MPI_Recv(): lp=0, rp=%d, expected=%d, actual=%d, count=%d\n",
61                              status.MPI_SOURCE, status.MPI_TAG, tag, msg_cnt);
62                     }
63
64 #                   if defined(VERBOSE)
65                     {
66                         printf("sending message: p=%d s=%d c=%d\n",
67                                status.MPI_SOURCE, msg_size, msg_cnt);
68                     }
69 #                   endif
70
71                     /* Send unexpected message which hopefully MPI_Probe() is
72                      * already waiting for at the remote process */
73                     mpi_errno = MPI_Send(buf, msg_size, MPI_BYTE,
74                                          status.MPI_SOURCE, status.MPI_TAG, MPI_COMM_WORLD);
75                     if (mpi_errno != MPI_SUCCESS && errs++ < 10) {
76                         MTestPrintError(mpi_errno);
77                     }
78                 }
79             }
80             else {
81                 int incoming_msg_size;
82
83                 /* Send synchronization message */
84                 mpi_errno = MPI_Send(NULL, 0, MPI_BYTE, 0, tag, MPI_COMM_WORLD);
85                 if (mpi_errno != MPI_SUCCESS && errs++ < 10) {
86                     MTestPrintError(mpi_errno);
87                 }
88
89                 /* Perform probe, hopefully before the master process can
90                  * send its reply */
91                 mpi_errno = MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
92                 if (mpi_errno != MPI_SUCCESS && errs++ < 10) {
93                     MTestPrintError(mpi_errno);
94                 }
95                 mpi_errno = MPI_Get_count(&status, MPI_BYTE, &incoming_msg_size);
96                 if (mpi_errno != MPI_SUCCESS && errs++ < 10) {
97                     MTestPrintError(mpi_errno);
98                 }
99                 if (status.MPI_SOURCE != 0 && errs++ < 10) {
100                     printf
101                         ("ERROR: unexpected message source from MPI_Probe(): p=%d, expected=0, actual=%d, count=%d\n",
102                          p_rank, status.MPI_SOURCE, msg_cnt);
103                 }
104                 if (status.MPI_TAG != tag && errs++ < 10) {
105                     printf
106                         ("ERROR: unexpected message tag from MPI_Probe(): p=%d, expected=%d, actual=%d, count=%d\n",
107                          p_rank, tag, status.MPI_TAG, msg_cnt);
108                 }
109                 if (incoming_msg_size != msg_size && errs++ < 10) {
110                     printf
111                         ("ERROR: unexpected message size from MPI_Probe(): p=%d, expected=%d, actual=%d, count=%d\n",
112                          p_rank, msg_size, incoming_msg_size, msg_cnt);
113                 }
114
115                 /* Receive the probed message from the master process */
116                 mpi_errno = MPI_Recv(buf, msg_size, MPI_BYTE, 0, tag, MPI_COMM_WORLD, &status);
117                 if (mpi_errno != MPI_SUCCESS && errs++ < 10) {
118                     MTestPrintError(mpi_errno);
119                 }
120                 mpi_errno = MPI_Get_count(&status, MPI_BYTE, &incoming_msg_size);
121                 if (mpi_errno != MPI_SUCCESS && errs++ < 10) {
122                     MTestPrintError(mpi_errno);
123                 }
124                 if (status.MPI_SOURCE != 0 && errs++ < 10) {
125                     printf
126                         ("ERROR: unexpected message source from MPI_Recv(): p=%d, expected=0, actual=%d, count=%d\n",
127                          p_rank, status.MPI_SOURCE, msg_cnt);
128                 }
129                 if (status.MPI_TAG != tag && errs++ < 10) {
130                     printf
131                         ("ERROR: unexpected message tag from MPI_Recv(): p=%d, expected=%d, actual=%d, count=%d\n",
132                          p_rank, tag, status.MPI_TAG, msg_cnt);
133                 }
134                 if (incoming_msg_size != msg_size && errs++ < 10) {
135                     printf
136                         ("ERROR: unexpected message size from MPI_Recv(): p=%d, expected=%d, actual=%d, count=%d\n",
137                          p_rank, msg_size, incoming_msg_size, msg_cnt);
138                 }
139             }
140         }
141     }
142
143     MTest_Finalize(errs);
144     MPI_Finalize();
145     return 0;
146 }