1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2012 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
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)
20 /* assert-like macro that bumps the err count and emits a message */
26 fprintf(stderr, "check failed: (%s), line %d\n", #x_, __LINE__); \
31 int main(int argc, char **argv)
35 int rank, size, lrank, lsize, rsize;
37 MPI_Comm newcomm, ic, localcomm, stagger_comm;
40 MPI_Init(&argc, &argv);
42 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
43 MPI_Comm_size(MPI_COMM_WORLD, &size);
46 printf("this test requires at least 2 processes\n");
47 MPI_Abort(MPI_COMM_WORLD, 1);
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.
59 for (i = 1; i < size; ++i) {
62 MPI_Recv(buf, 2, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
64 MPI_Comm_idup(MPI_COMM_WORLD, &newcomm, &rreq);
65 MPI_Wait(&rreq, MPI_STATUS_IGNORE);
68 MPI_Comm_idup(MPI_COMM_WORLD, &newcomm, &rreq);
71 MPI_Ssend(buf, 2, MPI_INT, 0, 0, MPI_COMM_WORLD);
72 MPI_Wait(&rreq, MPI_STATUS_IGNORE);
75 /* do some communication to make sure that newcomm works */
78 MPI_Allreduce(&buf[0], &buf[1], 1, MPI_INT, MPI_SUM, newcomm);
79 check(buf[1] == (size * (size-1) / 2));
81 MPI_Comm_free(&newcomm);
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);
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;
92 MPI_Comm_dup(localcomm, &stagger_comm);
95 MPI_Comm_rank(ic, &lrank);
96 MPI_Comm_size(ic, &lsize);
97 MPI_Comm_remote_size(ic, &rsize);
99 /* Similar to above pattern, but all non-local-rank-0 processes send to
100 * remote rank 0. Both sides participate in this way. */
102 for (i = 1; i < rsize; ++i) {
105 MPI_Recv(buf, 2, MPI_INT, i, 0, ic, MPI_STATUS_IGNORE);
107 MPI_Comm_idup(ic, &newcomm, &rreq);
108 MPI_Wait(&rreq, MPI_STATUS_IGNORE);
111 MPI_Comm_idup(ic, &newcomm, &rreq);
113 buf[1] = lsize + lrank;
114 MPI_Ssend(buf, 2, MPI_INT, 0, 0, ic);
115 MPI_Wait(&rreq, MPI_STATUS_IGNORE);
118 /* do some communication to make sure that newcomm works */
121 MPI_Allreduce(&buf[0], &buf[1], 1, MPI_INT, MPI_SUM, newcomm);
122 check(buf[1] == (rsize * (rsize-1) / 2));
124 /* free this down here, not before idup, otherwise it will undo our
125 * stagger_comm work */
126 MPI_Comm_free(&localcomm);
128 if (stagger_comm != MPI_COMM_NULL) {
129 MPI_Comm_free(&stagger_comm);
131 MPI_Comm_free(&newcomm);
134 #endif /* TEST_IDUP */
136 MPI_Reduce((rank == 0 ? MPI_IN_PLACE : &errs), &errs, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
139 printf("found %d errors\n", errs);
142 printf(" No errors\n");