Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
43e2ed85a7584baa87a0a7fc264f6ede923393e9
[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     {
39         const int msg_size = 1 << msg_size_lg;
40         int msg_cnt;
41
42         MTestPrintfMsg( 2, "testing messages of size %d\n", msg_size );
43         for (msg_cnt = 0; msg_cnt < NUM_MSGS_PER_BUF_SIZE; msg_cnt++)
44         {
45             MPI_Status status;
46             const int tag = msg_size_lg * NUM_MSGS_PER_BUF_SIZE + msg_cnt;
47             
48             MTestPrintfMsg( 2, "Message count %d\n", msg_cnt );
49             if (p_rank == 0)
50             {
51                 int p;
52                 
53                 for (p = 1; p < p_size; p ++)
54                 {
55                     /* Wait for synchronization message */
56                     mpi_errno = MPI_Recv(NULL, 0, MPI_BYTE, MPI_ANY_SOURCE, 
57                                          tag, MPI_COMM_WORLD, &status);
58                     if (mpi_errno != MPI_SUCCESS && errs++ < 10)
59                     {
60                         MTestPrintError(mpi_errno);
61                     }
62                     
63                     if (status.MPI_TAG != tag && errs++ < 10)
64                     {
65                         printf("ERROR: unexpected message tag from MPI_Recv(): lp=0, rp=%d, expected=%d, actual=%d, count=%d\n",
66                                status.MPI_SOURCE, status.MPI_TAG, tag, msg_cnt);
67                     }
68
69 #                   if defined(VERBOSE)
70                     {
71                         printf("sending message: p=%d s=%d c=%d\n", 
72                                status.MPI_SOURCE, msg_size, msg_cnt);
73                     }
74 #                   endif
75                     
76                     /* Send unexpected message which hopefully MPI_Probe() is 
77                        already waiting for at the remote process */
78                     mpi_errno = MPI_Send (buf, msg_size, MPI_BYTE, 
79                             status.MPI_SOURCE, status.MPI_TAG, MPI_COMM_WORLD);
80                     if (mpi_errno != MPI_SUCCESS && errs++ < 10)
81                     {
82                         MTestPrintError(mpi_errno);
83                     }
84                 }
85             }
86             else
87             {
88                 int incoming_msg_size;
89
90                 /* Send synchronization message */
91                 mpi_errno = MPI_Send(NULL, 0, MPI_BYTE, 0, tag, MPI_COMM_WORLD);
92                 if (mpi_errno != MPI_SUCCESS && errs++ < 10)
93                 {
94                     MTestPrintError(mpi_errno);
95                 }
96
97                 /* Perform probe, hopefully before the master process can 
98                    send its reply */
99                 mpi_errno = MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, 
100                                       MPI_COMM_WORLD, &status);
101                 if (mpi_errno != MPI_SUCCESS && errs++ < 10)
102                 {
103                     MTestPrintError(mpi_errno);
104                 }
105                 mpi_errno = MPI_Get_count(&status, MPI_BYTE, &incoming_msg_size);
106                 if (mpi_errno != MPI_SUCCESS && errs++ < 10)
107                 {
108                     MTestPrintError(mpi_errno);
109                 }
110                 if (status.MPI_SOURCE != 0 && errs++ < 10)
111                 {
112                     printf("ERROR: unexpected message source from MPI_Probe(): p=%d, expected=0, actual=%d, count=%d\n",
113                            p_rank, status.MPI_SOURCE, msg_cnt);
114                 }
115                 if (status.MPI_TAG != tag && errs++ < 10)
116                 {
117                     printf("ERROR: unexpected message tag from MPI_Probe(): p=%d, expected=%d, actual=%d, count=%d\n",
118                            p_rank, tag, status.MPI_TAG, msg_cnt);
119                 }
120                 if (incoming_msg_size != msg_size && errs++ < 10)
121                 {
122                     printf("ERROR: unexpected message size from MPI_Probe(): p=%d, expected=%d, actual=%d, count=%d\n",
123                            p_rank, msg_size, incoming_msg_size, msg_cnt);
124                 }
125
126                 /* Receive the probed message from the master process */
127                 mpi_errno = MPI_Recv(buf, msg_size, MPI_BYTE, 0, tag, 
128                                      MPI_COMM_WORLD, &status);
129                 if (mpi_errno != MPI_SUCCESS && errs++ < 10)
130                 {
131                     MTestPrintError(mpi_errno);
132                 }
133                 mpi_errno = MPI_Get_count(&status, MPI_BYTE, &incoming_msg_size);
134                 if (mpi_errno != MPI_SUCCESS && errs++ < 10)
135                 {
136                     MTestPrintError(mpi_errno);
137                 }
138                 if (status.MPI_SOURCE != 0 && errs++ < 10)
139                 {
140                     printf("ERROR: unexpected message source from MPI_Recv(): p=%d, expected=0, actual=%d, count=%d\n",
141                            p_rank, status.MPI_SOURCE, msg_cnt);
142                 }
143                 if (status.MPI_TAG != tag && errs++ < 10)
144                 {
145                     printf("ERROR: unexpected message tag from MPI_Recv(): p=%d, expected=%d, actual=%d, count=%d\n",
146                            p_rank, tag, status.MPI_TAG, msg_cnt);
147                 }
148                 if (incoming_msg_size != msg_size && errs++ < 10)
149                 {
150                     printf("ERROR: unexpected message size from MPI_Recv(): p=%d, expected=%d, actual=%d, count=%d\n",
151                            p_rank, msg_size, incoming_msg_size, msg_cnt);
152                 }
153             }
154         }
155     }
156
157     MTest_Finalize( errs );
158     MPI_Finalize();
159     return 0;
160 }