2 Test the group routines
11 MPI_Group_translate_ranks
19 /* stdlib.h Needed for malloc declaration */
23 int main( int argc, char **argv )
27 MPI_Group g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11, g12;
28 MPI_Comm comm, newcomm, splitcomm, dupcomm;
29 int i, grp_rank, rank, grp_size, size, result;
30 int nranks, *ranks, *ranks_out;
34 MPI_Init( &argc, &argv );
35 MPI_Comm_rank( MPI_COMM_WORLD, &worldrank );
37 comm = MPI_COMM_WORLD;
39 MPI_Comm_group( comm, &basegroup );
41 /* Get the basic information on this group */
42 MPI_Group_rank( basegroup, &grp_rank );
43 MPI_Comm_rank( comm, &rank );
44 if (grp_rank != rank) {
46 fprintf( stdout, "group rank %d != comm rank %d\n", grp_rank, rank );
49 MPI_Group_size( basegroup, &grp_size );
50 MPI_Comm_size( comm, &size );
51 if (grp_size != size) {
53 fprintf( stdout, "group size %d != comm size %d\n", grp_size, size );
57 /* Form a new communicator with inverted ranking */
58 MPI_Comm_split( comm, 0, size - rank, &newcomm );
59 MPI_Comm_group( newcomm, &g1 );
60 ranks = (int *)malloc( size * sizeof(int) );
61 ranks_out = (int *)malloc( size * sizeof(int) );
62 for (i=0; i<size; i++) ranks[i] = i;
64 MPI_Group_translate_ranks( g1, nranks, ranks, basegroup, ranks_out );
65 for (i=0; i<size; i++) {
66 if (ranks_out[i] != (size - 1) - i) {
68 fprintf( stdout, "Translate ranks got %d expected %d\n",
69 ranks_out[i], (size - 1) - i );
74 MPI_Group_compare( basegroup, g1, &result );
75 if (result != MPI_SIMILAR) {
77 fprintf( stdout, "Group compare should have been similar, was %d\n",
80 MPI_Comm_dup( comm, &dupcomm );
81 MPI_Comm_group( dupcomm, &g2 );
82 MPI_Group_compare( basegroup, g2, &result );
83 if (result != MPI_IDENT) {
85 fprintf( stdout, "Group compare should have been ident, was %d\n",
88 MPI_Comm_split( comm, rank < size/2, rank, &splitcomm );
89 MPI_Comm_group( splitcomm, &g3 );
90 MPI_Group_compare( basegroup, g3, &result );
91 if (result != MPI_UNEQUAL) {
93 fprintf( stdout, "Group compare should have been unequal, was %d\n",
96 MPI_Barrier(MPI_COMM_WORLD);
97 /* Build two new groups by excluding members; use Union to put them
101 MPI_Group_excl( basegroup, 1, ranks, &g4 );
102 /* Exclude 1-(size-1) */
103 MPI_Group_excl( basegroup, size-1, ranks+1, &g5 );
104 MPI_Group_union( g5, g4, &g6 );
105 MPI_Group_compare( basegroup, g6, &result );
106 if (result != MPI_IDENT) {
108 /* See ordering requirements on union */
109 fprintf( stdout, "Group excl and union did not give ident groups\n" );
111 MPI_Group_union( basegroup, g4, &g7 );
112 MPI_Group_compare( basegroup, g7, &result );
113 if (result != MPI_IDENT) {
115 fprintf( stdout, "Group union of overlapping groups failed\n" );
118 /* Use range_excl instead of ranks */
120 range[0][1] = size-1;
122 MPI_Group_range_excl( basegroup, 1, range, &g8 );
123 MPI_Group_compare( g5, g8, &result );
124 if (result != MPI_IDENT) {
126 fprintf( stdout, "Group range excl did not give ident groups\n" );
129 MPI_Group_intersection(g4, basegroup, &g9 );
130 MPI_Group_compare( g9, g4, &result );
131 if (result != MPI_IDENT) {
133 fprintf( stdout, "Group intersection did not give ident groups\n" );
136 MPI_Group_intersection(basegroup, g4 , &g9 );
137 MPI_Group_compare( g9, g4, &result );
138 if (result != MPI_IDENT) {
140 fprintf( stdout, "Group intersection 2 did not give ident groups\n" );
143 MPI_Group_intersection(basegroup, basegroup, &g9 );
144 MPI_Group_compare( g9, basegroup, &result );
145 if (result != MPI_IDENT) {
147 fprintf( stdout, "Group intersection 3 did not give ident groups\n" );
150 /* Exclude EVERYTHING and check against MPI_GROUP_EMPTY */
152 range[0][1] = size-1;
154 MPI_Group_range_excl( basegroup, 1, range, &g10 );
155 MPI_Group_compare( g10, MPI_GROUP_EMPTY, &result );
156 if (result != MPI_IDENT) {
159 "MPI_GROUP_EMPTY didn't compare against empty group\n");
162 /* Grouptest usually runs with 4 processes. Pick a range that specifies
163 1, size-1, but where "last" is size. This checks for an
164 error case that MPICH2 got wrong */
167 range[0][2] = size - 2;
168 MPI_Group_range_incl( basegroup, 1, range, &g11 );
171 MPI_Group_incl( basegroup, 2, ranks, &g12 );
172 MPI_Group_compare( g11, g12, &result );
173 if (result != MPI_IDENT) {
176 "MPI_Group_range_incl didn't compare against MPI_Group_incl\n" );
179 MPI_Group_free( &basegroup );
180 MPI_Group_free( &g1 );
181 MPI_Group_free( &g2 );
182 MPI_Group_free( &g3 );
183 MPI_Group_free( &g4 );
184 MPI_Group_free( &g5 );
185 MPI_Group_free( &g6 );
186 MPI_Group_free( &g7 );
187 MPI_Group_free( &g8 );
188 MPI_Group_free( &g9 );
189 MPI_Group_free( &g10 );
190 MPI_Group_free( &g11 );
191 MPI_Group_free( &g12 );
192 MPI_Comm_free( &dupcomm );
193 MPI_Comm_free( &splitcomm );
194 MPI_Comm_free( &newcomm );
196 MPI_Allreduce( &errs, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
197 if (worldrank == 0) {
199 printf( " No Errors\n" );
201 printf( "Found %d errors in MPI Group routines\n", toterr );