Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge before commiting VM changes - Adrien
[simgrid.git] / teshsuite / smpi / mpich3-test / comm / comm_idup.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2012 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "mpi.h"
11 #include "mpitest.h"
12
13 /* This is a temporary #ifdef to control whether we test this functionality.  A
14  * configure-test or similar would be better.  Eventually the MPI-3 standard
15  * will be released and this can be gated on a MPI_VERSION check */
16 #if !defined(USE_STRICT_MPI) && defined(MPICH)
17 #define TEST_IDUP 1
18 #endif
19
20 /* assert-like macro that bumps the err count and emits a message */
21 #define check(x_)                                                                 \
22     do {                                                                          \
23         if (!(x_)) {                                                              \
24             ++errs;                                                               \
25             if (errs < 10) {                                                      \
26                 fprintf(stderr, "check failed: (%s), line %d\n", #x_, __LINE__); \
27             }                                                                     \
28         }                                                                         \
29     } while (0)
30
31 int main(int argc, char **argv)
32 {
33     int errs = 0;
34     int i;
35     int rank, size, lrank, lsize, rsize;
36     int buf[2];
37     MPI_Comm newcomm, ic, localcomm, stagger_comm;
38     MPI_Request rreq;
39
40     MPI_Init(&argc, &argv);
41
42     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
43     MPI_Comm_size(MPI_COMM_WORLD, &size);
44
45     if (size < 2) {
46         printf("this test requires at least 2 processes\n");
47         MPI_Abort(MPI_COMM_WORLD, 1);
48         exit(1);
49     }
50
51 #ifdef TEST_IDUP
52
53     /* test plan: make rank 0 wait in a blocking recv until all other processes
54      * have posted their MPI_Comm_idup ops, then post last.  Should ensure that
55      * idup doesn't block on the non-zero ranks, otherwise we'll get a deadlock.
56      */
57
58     if (rank == 0) {
59         for (i = 1; i < size; ++i) {
60             buf[0] = 0x01234567;
61             buf[1] = 0x89abcdef;
62             MPI_Recv(buf, 2, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
63         }
64         MPI_Comm_idup(MPI_COMM_WORLD, &newcomm, &rreq);
65         MPI_Wait(&rreq, MPI_STATUS_IGNORE);
66     }
67     else {
68         MPI_Comm_idup(MPI_COMM_WORLD, &newcomm, &rreq);
69         buf[0] = rank;
70         buf[1] = size + rank;
71         MPI_Ssend(buf, 2, MPI_INT, 0, 0, MPI_COMM_WORLD);
72         MPI_Wait(&rreq, MPI_STATUS_IGNORE);
73     }
74
75     /* do some communication to make sure that newcomm works */
76     buf[0] = rank;
77     buf[1] = 0xfeedface;
78     MPI_Allreduce(&buf[0], &buf[1], 1, MPI_INT, MPI_SUM, newcomm);
79     check(buf[1] == (size * (size-1) / 2));
80
81     MPI_Comm_free(&newcomm);
82
83     /* now construct an intercomm and make sure we can dup that too */
84     MPI_Comm_split(MPI_COMM_WORLD, rank % 2, rank, &localcomm);
85     MPI_Intercomm_create(localcomm, 0, MPI_COMM_WORLD, (rank == 0 ? 1 : 0), 1234, &ic);
86
87     /* Create a communicator on just the "right hand group" of the intercomm in
88      * order to make it more likely to catch bugs related to incorrectly
89      * swapping the context_id and recvcontext_id in the idup code. */
90     stagger_comm = MPI_COMM_NULL;
91     if (rank % 2) {
92         MPI_Comm_dup(localcomm, &stagger_comm);
93     }
94
95     MPI_Comm_rank(ic, &lrank);
96     MPI_Comm_size(ic, &lsize);
97     MPI_Comm_remote_size(ic, &rsize);
98
99     /* Similar to above pattern, but all non-local-rank-0 processes send to
100      * remote rank 0.  Both sides participate in this way. */
101     if (lrank == 0) {
102         for (i = 1; i < rsize; ++i) {
103             buf[0] = 0x01234567;
104             buf[1] = 0x89abcdef;
105             MPI_Recv(buf, 2, MPI_INT, i, 0, ic, MPI_STATUS_IGNORE);
106         }
107         MPI_Comm_idup(ic, &newcomm, &rreq);
108         MPI_Wait(&rreq, MPI_STATUS_IGNORE);
109     }
110     else {
111         MPI_Comm_idup(ic, &newcomm, &rreq);
112         buf[0] = lrank;
113         buf[1] = lsize + lrank;
114         MPI_Ssend(buf, 2, MPI_INT, 0, 0, ic);
115         MPI_Wait(&rreq, MPI_STATUS_IGNORE);
116     }
117
118     /* do some communication to make sure that newcomm works */
119     buf[0] = lrank;
120     buf[1] = 0xfeedface;
121     MPI_Allreduce(&buf[0], &buf[1], 1, MPI_INT, MPI_SUM, newcomm);
122     check(buf[1] == (rsize * (rsize-1) / 2));
123
124     /* free this down here, not before idup, otherwise it will undo our
125      * stagger_comm work */
126     MPI_Comm_free(&localcomm);
127
128     if (stagger_comm != MPI_COMM_NULL) {
129         MPI_Comm_free(&stagger_comm);
130     }
131     MPI_Comm_free(&newcomm);
132     MPI_Comm_free(&ic);
133
134 #endif /* TEST_IDUP */
135
136     MPI_Reduce((rank == 0 ? MPI_IN_PLACE : &errs), &errs, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
137     if (rank == 0) {
138         if (errs) {
139             printf("found %d errors\n", errs);
140         }
141         else {
142             printf(" No errors\n");
143         }
144     }
145
146     MPI_Finalize();
147
148     return 0;
149 }
150