1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2007 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
11 /* Check that Communicators can be created from various subsets of the
12 processes in the communicator.
15 void abortMsg( const char *, int );
16 int BuildComm( MPI_Comm, MPI_Group, const char [] );
18 void abortMsg( const char *str, int code )
20 char msg[MPI_MAX_ERROR_STRING];
23 MPI_Error_class( code, &class );
24 MPI_Error_string( code, msg, &resultLen );
25 fprintf( stderr, "%s: errcode = %d, class = %d, msg = %s\n",
26 str, code, class, msg );
27 MPI_Abort( MPI_COMM_WORLD, code );
31 int main( int argc, char *argv[] )
34 int wrank, wsize, gsize, err, errs = 0;
36 MPI_Group wGroup, godd, ghigh, geven;
38 MTest_Init( &argc, &argv );
40 MPI_Comm_size( MPI_COMM_WORLD, &wsize );
41 MPI_Comm_rank( MPI_COMM_WORLD, &wrank );
43 /* Create some groups */
44 MPI_Comm_group( MPI_COMM_WORLD, &wGroup );
46 MTestPrintfMsg( 2, "Creating groups\n" );
47 ranges[0][0] = 2*(wsize/2)-1;
50 err = MPI_Group_range_incl( wGroup, 1, ranges, &godd );
51 if (err) abortMsg( "Failed to create odd group: ", err );
52 err = MPI_Group_size( godd, &gsize );
53 if (err) abortMsg( "Failed to get size of odd group: ", err );
54 if (gsize != wsize/2) {
55 fprintf( stderr, "Group godd size is %d should be %d\n", gsize,
60 ranges[0][0] = wsize/2+1;
61 ranges[0][1] = wsize-1;
63 err = MPI_Group_range_incl( wGroup, 1, ranges, &ghigh );
64 if (err) abortMsg( "Failed to create high group\n", err );
66 ranges[0][1] = wsize-1;
68 err = MPI_Group_range_incl( wGroup, 1, ranges, &geven );
69 if (err) abortMsg( "Failed to create even group:", err );
71 MPI_Comm_dup( MPI_COMM_WORLD, &dupWorld );
72 MPI_Comm_set_name( dupWorld, (char*)"Dup of world" );
73 /* First, use the groups to create communicators from world and a dup
75 errs += BuildComm( MPI_COMM_WORLD, ghigh, "ghigh" );
76 errs += BuildComm( MPI_COMM_WORLD, godd, "godd" );
77 errs += BuildComm( MPI_COMM_WORLD, geven, "geven" );
78 errs += BuildComm( dupWorld, ghigh, "ghigh" );
79 errs += BuildComm( dupWorld, godd, "godd" );
80 errs += BuildComm( dupWorld, geven, "geven" );
82 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
83 /* check that we can create multiple communicators from a single collective
84 * call to MPI_Comm_create as long as the groups are all disjoint */
85 errs += BuildComm( MPI_COMM_WORLD, (wrank % 2 ? godd : geven), "godd+geven" );
86 errs += BuildComm( dupWorld, (wrank % 2 ? godd : geven), "godd+geven" );
87 errs += BuildComm( MPI_COMM_WORLD, MPI_GROUP_EMPTY, "MPI_GROUP_EMPTY" );
88 errs += BuildComm( dupWorld, MPI_GROUP_EMPTY, "MPI_GROUP_EMPTY" );
91 MPI_Comm_free( &dupWorld );
92 MPI_Group_free( &ghigh );
93 MPI_Group_free( &godd );
94 MPI_Group_free( &geven );
95 MPI_Group_free( &wGroup );
97 MTest_Finalize( errs );
103 int BuildComm( MPI_Comm oldcomm, MPI_Group group, const char gname[] )
106 int grank, gsize, rank, size, errs = 0;
107 char cname[MPI_MAX_OBJECT_NAME+1];
110 MPI_Group_rank( group, &grank );
111 MPI_Group_size( group, &gsize );
112 MPI_Comm_get_name( oldcomm, cname, &cnamelen );
113 MTestPrintfMsg( 2, "Testing comm %s from %s\n", cname, gname );
114 MPI_Comm_create( oldcomm, group, &newcomm );
115 if (newcomm == MPI_COMM_NULL && grank != MPI_UNDEFINED) {
117 fprintf( stderr, "newcomm is null but process is in group\n" );
119 if (newcomm != MPI_COMM_NULL && grank == MPI_UNDEFINED) {
121 fprintf( stderr, "newcomm is not null but process is not in group\n" );
123 if (newcomm != MPI_COMM_NULL && grank != MPI_UNDEFINED) {
124 MPI_Comm_rank( newcomm, &rank );
127 fprintf( stderr, "Rank is %d should be %d in comm from %s\n",
128 rank, grank, gname );
130 MPI_Comm_size( newcomm, &size );
133 fprintf( stderr, "Size is %d should be %d in comm from %s\n",
134 size, gsize, gname );
136 MPI_Comm_free( &newcomm );
137 MTestPrintfMsg( 2, "Done testing comm %s from %s\n", cname, gname );