Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge remote-tracking branch 'origin/libdw2'
[simgrid.git] / teshsuite / smpi / mpich3-test / group / grouptest2.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2001 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7
8 /*
9    Test the group routines
10    (some tested elsewere)
11
12 MPI_Group_compare
13 MPI_Group_excl
14 MPI_Group_intersection
15 MPI_Group_range_excl
16 MPI_Group_rank
17 MPI_Group_size
18 MPI_Group_translate_ranks
19 MPI_Group_union
20
21  */
22 #include "mpi.h"
23 #include <stdio.h>
24 /* stdlib.h Needed for malloc declaration */
25 #include <stdlib.h>
26
27 int main( int argc, char **argv )
28 {
29     int errs=0, toterr;
30     MPI_Group basegroup;
31     MPI_Group g1, g2, g3, g4, g5, g6, g7, g8, g9, g10;
32     MPI_Group g3a, g3b;
33     MPI_Comm  comm, newcomm, splitcomm, dupcomm;
34     int       i, grp_rank, rank, grp_size, size, result;
35     int       nranks, *ranks, *ranks_out;
36     int       range[1][3];
37     int       worldrank;
38
39     MPI_Init( &argc, &argv );
40     MPI_Comm_rank( MPI_COMM_WORLD, &worldrank );
41
42     comm = MPI_COMM_WORLD;
43
44     MPI_Comm_group( comm, &basegroup );
45     MPI_Comm_rank( comm, &rank );
46     MPI_Comm_size( comm, &size );
47
48 /* Get the basic information on this group */
49     MPI_Group_rank( basegroup, &grp_rank );
50     if (grp_rank != rank) {
51         errs++;
52         fprintf( stdout, "group rank %d != comm rank %d\n", grp_rank, rank );
53     }
54
55     MPI_Group_size( basegroup, &grp_size );
56     if (grp_size != size) {
57         errs++;
58         fprintf( stdout, "group size %d != comm size %d\n", grp_size, size );
59     }
60
61
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;
68     nranks = size;
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) {
72             errs++;
73             fprintf( stdout, "Translate ranks got %d expected %d\n", 
74                      ranks_out[i], (size - 1) - i );
75         }
76     }
77
78 /* Check Compare */
79     MPI_Group_compare( basegroup, g1, &result );
80     if (result != MPI_SIMILAR) {
81         errs++;
82         fprintf( stdout, "Group compare should have been similar, was %d\n",
83                  result );
84     }
85     MPI_Comm_dup( comm, &dupcomm );
86     MPI_Comm_group( dupcomm, &g2 );
87     MPI_Group_compare( basegroup, g2, &result );
88     if (result != MPI_IDENT) {
89         errs++;
90         fprintf( stdout, "Group compare should have been ident, was %d\n",
91                  result );
92     }
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) {
97         errs++;
98         fprintf( stdout, "Group compare should have been unequal, was %d\n",
99                  result );
100     }
101
102     /* Build two groups that have this process and one other, but do not
103        have the same processes */
104     ranks[0] = rank;
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) {
111         errs++;
112         fprintf( stdout, "Group compare of equal sized but different groups should have been unequal, was %d\n", result );
113     }
114     
115
116 /* Build two new groups by excluding members; use Union to put them
117    together again */
118
119 /* Exclude 0 */
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) {
127         int usize;
128         errs++;
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 );
134     }
135     MPI_Group_union( basegroup, g4, &g7 );
136     MPI_Group_compare( basegroup, g7, &result );
137     if (result != MPI_IDENT) {
138         int usize;
139         errs++;
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 );
144     }
145
146 /* Use range_excl instead of ranks */
147     /* printf ("range excl\n" ); fflush( stdout ); */
148     range[0][0] = 1;
149     range[0][1] = size-1;
150     range[0][2] = 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) {
156         errs++;
157         fprintf( stdout, "Group range excl did not give ident groups\n" );
158     }
159
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) {
164         errs++;
165         fprintf( stdout, "Group intersection did not give ident groups\n" );
166     }
167
168 /* Exclude EVERYTHING and check against MPI_GROUP_EMPTY */
169     /* printf( "range excl all\n" ); fflush( stdout ); */
170     range[0][0] = 0;
171     range[0][1] = size-1;
172     range[0][2] = 1;
173     MPI_Group_range_excl( basegroup, 1, range, &g10 );
174
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); */
178
179     if (result != MPI_IDENT) {
180         errs++;
181         fprintf( stdout, 
182                  "MPI_GROUP_EMPTY didn't compare against empty group\n");
183     }
184
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 );
202
203     MPI_Allreduce( &errs, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
204     if (worldrank == 0) {
205         if (toterr == 0) 
206             printf( " No Errors\n" );
207         else
208             printf( "Found %d errors in MPI Group routines\n", toterr );
209     }
210
211     MPI_Finalize();
212     return toterr;
213 }