Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add mpich3 test suite, to replace older one.
[simgrid.git] / teshsuite / smpi / mpich-test / context / grouptest.c
1 /*
2    Test the group routines
3    (some tested elsewere)
4
5 MPI_Group_compare
6 MPI_Group_excl
7 MPI_Group_intersection
8 MPI_Group_range_excl
9 MPI_Group_rank
10 MPI_Group_size
11 MPI_Group_translate_ranks
12 MPI_Group_union
13 MPI_Group_range_incl
14 MPI_Group_incl
15
16  */
17 #include "mpi.h"
18 #include <stdio.h>
19 /* stdlib.h Needed for malloc declaration */
20 #include <stdlib.h>
21 #include "test.h"
22
23 int main( int argc, char **argv )
24 {
25     int errs=0, toterr;
26     MPI_Group basegroup;
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;
31     int       range[2][3];
32     int       worldrank;
33
34     MPI_Init( &argc, &argv );
35     MPI_Comm_rank( MPI_COMM_WORLD, &worldrank );
36
37     comm = MPI_COMM_WORLD;
38
39     MPI_Comm_group( comm, &basegroup );
40
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) {
45         errs++;
46         fprintf( stdout, "group rank %d != comm rank %d\n", grp_rank, rank );
47     }
48
49     MPI_Group_size( basegroup, &grp_size );
50     MPI_Comm_size( comm, &size );
51     if (grp_size != size) {
52         errs++;
53         fprintf( stdout, "group size %d != comm size %d\n", grp_size, size );
54     }
55
56
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;
63     nranks = size;
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) {
67             errs++;
68             fprintf( stdout, "Translate ranks got %d expected %d\n", 
69                      ranks_out[i], (size - 1) - i );
70         }
71     }
72
73 /* Check Compare */
74     MPI_Group_compare( basegroup, g1, &result );
75     if (result != MPI_SIMILAR) {
76         errs++;
77         fprintf( stdout, "Group compare should have been similar, was %d\n",
78                  result );
79     }
80     MPI_Comm_dup( comm, &dupcomm );
81     MPI_Comm_group( dupcomm, &g2 );
82     MPI_Group_compare( basegroup, g2, &result );
83     if (result != MPI_IDENT) {
84         errs++;
85         fprintf( stdout, "Group compare should have been ident, was %d\n",
86                  result );
87     }
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) {
92         errs++;
93         fprintf( stdout, "Group compare should have been unequal, was %d\n",
94                  result );
95     }
96 MPI_Barrier(MPI_COMM_WORLD);
97 /* Build two new groups by excluding members; use Union to put them
98    together again */
99
100 /* Exclude 0 */
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) {
107         errs++;
108         /* See ordering requirements on union */
109         fprintf( stdout, "Group excl and union did not give ident groups\n" );
110     }
111     MPI_Group_union( basegroup, g4, &g7 );
112     MPI_Group_compare( basegroup, g7, &result );
113     if (result != MPI_IDENT) {
114         errs++;
115         fprintf( stdout, "Group union of overlapping groups failed\n" );
116     }
117
118 /* Use range_excl instead of ranks */
119     range[0][0] = 1;
120     range[0][1] = size-1;
121     range[0][2] = 1;
122     MPI_Group_range_excl( basegroup, 1, range, &g8 );
123     MPI_Group_compare( g5, g8, &result );
124     if (result != MPI_IDENT) {
125         errs++;
126         fprintf( stdout, "Group range excl did not give ident groups\n" );
127     }
128
129     MPI_Group_intersection(g4, basegroup, &g9 );
130     MPI_Group_compare( g9, g4, &result );
131     if (result != MPI_IDENT) {
132         errs++;
133         fprintf( stdout, "Group intersection did not give ident groups\n" );
134     }
135     
136     MPI_Group_intersection(basegroup, g4 , &g9 );
137     MPI_Group_compare( g9, g4, &result );
138     if (result != MPI_IDENT) {
139         errs++;
140         fprintf( stdout, "Group intersection 2 did not give ident groups\n" );
141     }
142     
143         MPI_Group_intersection(basegroup, basegroup, &g9 );
144     MPI_Group_compare( g9, basegroup, &result );
145     if (result != MPI_IDENT) {
146         errs++;
147         fprintf( stdout, "Group intersection 3 did not give ident groups\n" );
148     }
149
150 /* Exclude EVERYTHING and check against MPI_GROUP_EMPTY */
151     range[0][0] = 0;
152     range[0][1] = size-1;
153     range[0][2] = 1;
154     MPI_Group_range_excl( basegroup, 1, range, &g10 );
155     MPI_Group_compare( g10, MPI_GROUP_EMPTY, &result );
156     if (result != MPI_IDENT) {
157         errs++;
158         fprintf( stdout, 
159                  "MPI_GROUP_EMPTY didn't compare against empty group\n");
160     }
161
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 */
165     range[0][0] = 1;
166     range[0][1] = size ;
167     range[0][2] = size - 2;
168     MPI_Group_range_incl( basegroup, 1, range, &g11 );
169     ranks[0] = 1;
170     ranks[1] = size-1;
171     MPI_Group_incl( basegroup, 2, ranks, &g12 );
172     MPI_Group_compare( g11, g12, &result );
173     if (result != MPI_IDENT) {
174         errs++;
175         fprintf( stderr, 
176                  "MPI_Group_range_incl didn't compare against MPI_Group_incl\n" );
177     }
178
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 );
195
196     MPI_Allreduce( &errs, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
197     if (worldrank == 0) {
198         if (toterr == 0) 
199             printf( " No Errors\n" );
200         else
201             printf( "Found %d errors in MPI Group routines\n", toterr );
202     }
203
204     MPI_Finalize();
205     return toterr;
206 }