1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
4 * (C) 2001 by Argonne National Laboratory.
5 * See COPYRIGHT in top-level directory.
9 Test the group routines
10 (some tested elsewere)
14 MPI_Group_intersection
18 MPI_Group_translate_ranks
24 /* stdlib.h Needed for malloc declaration */
27 int main( int argc, char **argv )
31 MPI_Group g1, g2, g3, g4, g5, g6, g7, g8, g9, g10;
33 MPI_Comm comm, newcomm, splitcomm, dupcomm;
34 int i, grp_rank, rank, grp_size, size, result;
35 int nranks, *ranks, *ranks_out;
39 MPI_Init( &argc, &argv );
40 MPI_Comm_rank( MPI_COMM_WORLD, &worldrank );
42 comm = MPI_COMM_WORLD;
44 MPI_Comm_group( comm, &basegroup );
45 MPI_Comm_rank( comm, &rank );
46 MPI_Comm_size( comm, &size );
48 /* Get the basic information on this group */
49 MPI_Group_rank( basegroup, &grp_rank );
50 if (grp_rank != rank) {
52 fprintf( stdout, "group rank %d != comm rank %d\n", grp_rank, rank );
55 MPI_Group_size( basegroup, &grp_size );
56 if (grp_size != size) {
58 fprintf( stdout, "group size %d != comm size %d\n", grp_size, size );
62 /* Form a new communicator with inverted ranking */
63 MPI_Comm_split( comm, 0, size - rank, &newcomm );
64 MPI_Comm_group( newcomm, &g1 );
65 ranks = (int *)malloc( size * sizeof(int) );
66 ranks_out = (int *)malloc( size * sizeof(int) );
67 for (i=0; i<size; i++) ranks[i] = i;
69 MPI_Group_translate_ranks( g1, nranks, ranks, basegroup, ranks_out );
70 for (i=0; i<size; i++) {
71 if (ranks_out[i] != (size - 1) - i) {
73 fprintf( stdout, "Translate ranks got %d expected %d\n",
74 ranks_out[i], (size - 1) - i );
79 MPI_Group_compare( basegroup, g1, &result );
80 if (result != MPI_SIMILAR) {
82 fprintf( stdout, "Group compare should have been similar, was %d\n",
85 MPI_Comm_dup( comm, &dupcomm );
86 MPI_Comm_group( dupcomm, &g2 );
87 MPI_Group_compare( basegroup, g2, &result );
88 if (result != MPI_IDENT) {
90 fprintf( stdout, "Group compare should have been ident, was %d\n",
93 MPI_Comm_split( comm, rank < size/2, rank, &splitcomm );
94 MPI_Comm_group( splitcomm, &g3 );
95 MPI_Group_compare( basegroup, g3, &result );
96 if (result != MPI_UNEQUAL) {
98 fprintf( stdout, "Group compare should have been unequal, was %d\n",
102 /* Build two groups that have this process and one other, but do not
103 have the same processes */
105 ranks[1] = (rank + 1) % size;
106 MPI_Group_incl( basegroup, 2, ranks, &g3a );
107 ranks[1] = (rank + size - 1) % size;
108 MPI_Group_incl( basegroup, 2, ranks, &g3b );
109 MPI_Group_compare( g3a, g3b, &result );
110 if (result != MPI_UNEQUAL) {
112 fprintf( stdout, "Group compare of equal sized but different groups should have been unequal, was %d\n", result );
116 /* Build two new groups by excluding members; use Union to put them
120 for (i=0; i<size; i++) ranks[i] = i;
121 MPI_Group_excl( basegroup, 1, ranks, &g4 );
122 /* Exclude 1-(size-1) */
123 MPI_Group_excl( basegroup, size-1, ranks+1, &g5 );
124 MPI_Group_union( g5, g4, &g6 );
125 MPI_Group_compare( basegroup, g6, &result );
126 if (result != MPI_IDENT) {
129 /* See ordering requirements on union */
130 fprintf( stdout, "Group excl and union did not give ident groups\n" );
131 fprintf( stdout, "[%d] result of compare was %d\n", rank, result );
132 MPI_Group_size( g6, &usize );
133 fprintf( stdout, "Size of union is %d, should be %d\n", usize, size );
135 MPI_Group_union( basegroup, g4, &g7 );
136 MPI_Group_compare( basegroup, g7, &result );
137 if (result != MPI_IDENT) {
140 fprintf( stdout, "Group union of overlapping groups failed\n" );
141 fprintf( stdout, "[%d] result of compare was %d\n", rank, result );
142 MPI_Group_size( g7, &usize );
143 fprintf( stdout, "Size of union is %d, should be %d\n", usize, size );
146 /* Use range_excl instead of ranks */
147 /* printf ("range excl\n" ); fflush( stdout ); */
149 range[0][1] = size-1;
151 MPI_Group_range_excl( basegroup, 1, range, &g8 );
152 /* printf( "out of range excl\n" ); fflush( stdout ); */
153 MPI_Group_compare( g5, g8, &result );
154 /* printf( "out of compare\n" ); fflush( stdout ); */
155 if (result != MPI_IDENT) {
157 fprintf( stdout, "Group range excl did not give ident groups\n" );
160 /* printf( "intersection\n" ); fflush( stdout ); */
161 MPI_Group_intersection( basegroup, g4, &g9 );
162 MPI_Group_compare( g9, g4, &result );
163 if (result != MPI_IDENT) {
165 fprintf( stdout, "Group intersection did not give ident groups\n" );
168 /* Exclude EVERYTHING and check against MPI_GROUP_EMPTY */
169 /* printf( "range excl all\n" ); fflush( stdout ); */
171 range[0][1] = size-1;
173 MPI_Group_range_excl( basegroup, 1, range, &g10 );
175 /* printf( "done range excl all\n" ); fflush(stdout); */
176 MPI_Group_compare( g10, MPI_GROUP_EMPTY, &result );
177 /* printf( "done compare to MPI_GROUP_EMPTY\n" ); fflush(stdout); */
179 if (result != MPI_IDENT) {
182 "MPI_GROUP_EMPTY didn't compare against empty group\n");
185 /* printf( "freeing groups\n" ); fflush( stdout ); */
186 MPI_Group_free( &basegroup );
187 MPI_Group_free( &g1 );
188 MPI_Group_free( &g2 );
189 MPI_Group_free( &g3 );
190 MPI_Group_free( &g3a );
191 MPI_Group_free( &g3b );
192 MPI_Group_free( &g4 );
193 MPI_Group_free( &g5 );
194 MPI_Group_free( &g6 );
195 MPI_Group_free( &g7 );
196 MPI_Group_free( &g8 );
197 MPI_Group_free( &g9 );
198 MPI_Group_free( &g10 );
199 MPI_Comm_free( &dupcomm );
200 MPI_Comm_free( &splitcomm );
201 MPI_Comm_free( &newcomm );
203 MPI_Allreduce( &errs, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
204 if (worldrank == 0) {
206 printf( " No Errors\n" );
208 printf( "Found %d errors in MPI Group routines\n", toterr );