1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
4 * (C) 2007 by Argonne National Laboratory.
5 * See COPYRIGHT in top-level directory.
13 * This program tests that MPI_Comm_create applies to intercommunicators;
14 * this is an extension added in MPI-2
17 int TestIntercomm( MPI_Comm );
19 int main( int argc, char *argv[] )
22 int size, isLeft, wrank;
23 MPI_Comm intercomm, newcomm;
24 MPI_Group oldgroup, newgroup;
26 MTest_Init( &argc, &argv );
28 MPI_Comm_size( MPI_COMM_WORLD, &size );
30 printf( "This test requires at least 4 processes\n" );
31 MPI_Abort( MPI_COMM_WORLD, 1 );
34 MPI_Comm_rank( MPI_COMM_WORLD, &wrank );
36 while (MTestGetIntercomm( &intercomm, &isLeft, 2 )) {
37 int ranks[10], nranks, result;
39 if (intercomm == MPI_COMM_NULL) continue;
41 MPI_Comm_group( intercomm, &oldgroup );
44 MTestPrintfMsg( 1, "Creating a new intercomm 0-0\n" );
45 MPI_Group_incl( oldgroup, nranks, ranks, &newgroup );
46 MPI_Comm_create( intercomm, newgroup, &newcomm );
48 /* Make sure that the new communicator has the appropriate pieces */
49 if (newcomm != MPI_COMM_NULL) {
50 int new_rsize, new_size, flag, commok = 1;
52 MPI_Comm_set_name( newcomm, (char*)"Single rank in each group" );
53 MPI_Comm_test_inter( intercomm, &flag );
56 printf( "[%d] Output communicator is not an intercomm\n",
61 MPI_Comm_remote_size( newcomm, &new_rsize );
62 MPI_Comm_size( newcomm, &new_size );
63 /* The new communicator has 1 process in each group */
66 printf( "[%d] Remote size is %d, should be one\n",
72 printf( "[%d] Local size is %d, should be one\n",
78 errs += TestIntercomm( newcomm );
81 MPI_Group_free( &newgroup );
82 if (newcomm != MPI_COMM_NULL) {
83 MPI_Comm_free( &newcomm );
86 /* Now, do a sort of dup, using the original group */
87 MTestPrintfMsg( 1, "Creating a new intercomm (manual dup)\n" );
88 MPI_Comm_create( intercomm, oldgroup, &newcomm );
89 MPI_Comm_set_name( newcomm, (char*)"Dup of original" );
90 MTestPrintfMsg( 1, "Creating a new intercomm (manual dup (done))\n" );
92 MPI_Comm_compare( intercomm, newcomm, &result );
93 MTestPrintfMsg( 1, "Result of comm/intercomm compare is %d\n", result );
94 if (result != MPI_CONGRUENT) {
98 case MPI_IDENT: rname = "IDENT"; break;
99 case MPI_CONGRUENT: rname = "CONGRUENT"; break;
100 case MPI_SIMILAR: rname = "SIMILAR"; break;
101 case MPI_UNEQUAL: rname = "UNEQUAL"; break;
102 printf( "[%d] Expected MPI_CONGRUENT but saw %d (%s)",
103 wrank, result, rname ); fflush(stdout);
107 /* Try to communication between each member of intercomm */
108 errs += TestIntercomm( newcomm );
111 if (newcomm != MPI_COMM_NULL) {
112 MPI_Comm_free(&newcomm);
114 /* test that an empty group in either side of the intercomm results in
115 * MPI_COMM_NULL for all members of the comm */
117 /* left side reuses oldgroup, our local group in intercomm */
118 MPI_Comm_create(intercomm, oldgroup, &newcomm);
121 /* right side passes MPI_GROUP_EMPTY */
122 MPI_Comm_create(intercomm, MPI_GROUP_EMPTY, &newcomm);
124 if (newcomm != MPI_COMM_NULL) {
125 printf("[%d] expected MPI_COMM_NULL, but got a different communicator\n", wrank); fflush(stdout);
129 if (newcomm != MPI_COMM_NULL) {
130 MPI_Comm_free(&newcomm);
132 MPI_Group_free( &oldgroup );
133 MPI_Comm_free( &intercomm );
136 MTest_Finalize(errs);
143 int TestIntercomm( MPI_Comm comm )
145 int local_size, remote_size, rank, **bufs, *bufmem, rbuf[2], j;
146 int errs = 0, wrank, nsize;
147 char commname[MPI_MAX_OBJECT_NAME+1];
150 MPI_Comm_rank( MPI_COMM_WORLD, &wrank );
151 MPI_Comm_size( comm, &local_size );
152 MPI_Comm_remote_size( comm, &remote_size );
153 MPI_Comm_rank( comm, &rank );
154 MPI_Comm_get_name( comm, commname, &nsize );
156 MTestPrintfMsg( 1, "Testing communication on intercomm '%s', remote_size=%d\n",
157 commname, remote_size );
159 reqs = (MPI_Request *)malloc( remote_size * sizeof(MPI_Request) );
161 printf( "[%d] Unable to allocated %d requests for testing intercomm %s\n",
162 wrank, remote_size, commname );
166 bufs = (int **) malloc( remote_size * sizeof(int *) );
168 printf( "[%d] Unable to allocated %d int pointers for testing intercomm %s\n",
169 wrank, remote_size, commname );
173 bufmem = (int *) malloc( remote_size * 2 * sizeof(int) );
175 printf( "[%d] Unable to allocated %d int data for testing intercomm %s\n",
176 wrank, 2*remote_size, commname );
181 /* Each process sends a message containing its own rank and the
182 rank of the destination with a nonblocking send. Because we're using
183 nonblocking sends, we need to use different buffers for each isend */
184 /* NOTE: the send buffer access restriction was relaxed in MPI-2.2, although
185 it doesn't really hurt to keep separate buffers for our purposes */
186 for (j=0; j<remote_size; j++) {
187 bufs[j] = &bufmem[2*j];
190 MPI_Isend( bufs[j], 2, MPI_INT, j, 0, comm, &reqs[j] );
192 MTestPrintfMsg( 2, "isends posted, about to recv\n" );
194 for (j=0; j<remote_size; j++) {
195 MPI_Recv( rbuf, 2, MPI_INT, j, 0, comm, MPI_STATUS_IGNORE );
197 printf( "[%d] Expected rank %d but saw %d in %s\n",
198 wrank, j, rbuf[0], commname );
201 if (rbuf[1] != rank) {
202 printf( "[%d] Expected target rank %d but saw %d from %d in %s\n",
203 wrank, rank, rbuf[1], j, commname );
209 MTestPrintfMsg( 2, "my recvs completed, about to waitall\n" );
210 MPI_Waitall( remote_size, reqs, MPI_STATUSES_IGNORE );