Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[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 elsewhere)
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++)
68         ranks[i] = i;
69     nranks = size;
70     MPI_Group_translate_ranks(g1, nranks, ranks, basegroup, ranks_out);
71     for (i = 0; i < size; i++) {
72         if (ranks_out[i] != (size - 1) - i) {
73             errs++;
74             fprintf(stdout, "Translate ranks got %d expected %d\n", 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", result);
83     }
84     MPI_Comm_dup(comm, &dupcomm);
85     MPI_Comm_group(dupcomm, &g2);
86     MPI_Group_compare(basegroup, g2, &result);
87     if (result != MPI_IDENT) {
88         errs++;
89         fprintf(stdout, "Group compare should have been ident, was %d\n", result);
90     }
91     MPI_Comm_split(comm, rank < size / 2, rank, &splitcomm);
92     MPI_Comm_group(splitcomm, &g3);
93     MPI_Group_compare(basegroup, g3, &result);
94     if (result != MPI_UNEQUAL) {
95         errs++;
96         fprintf(stdout, "Group compare should have been unequal, was %d\n", result);
97     }
98
99     /* Build two groups that have this process and one other, but do not
100      * have the same processes */
101     ranks[0] = rank;
102     ranks[1] = (rank + 1) % size;
103     MPI_Group_incl(basegroup, 2, ranks, &g3a);
104     ranks[1] = (rank + size - 1) % size;
105     MPI_Group_incl(basegroup, 2, ranks, &g3b);
106     MPI_Group_compare(g3a, g3b, &result);
107     if (result != MPI_UNEQUAL) {
108         errs++;
109         fprintf(stdout,
110                 "Group compare of equal sized but different groups should have been unequal, was %d\n",
111                 result);
112     }
113
114
115 /* Build two new groups by excluding members; use Union to put them
116    together again */
117
118 /* Exclude 0 */
119     for (i = 0; i < size; i++)
120         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, "MPI_GROUP_EMPTY didn't compare against empty group\n");
182     }
183
184     /* printf("freeing groups\n"); fflush(stdout); */
185     MPI_Group_free(&basegroup);
186     MPI_Group_free(&g1);
187     MPI_Group_free(&g2);
188     MPI_Group_free(&g3);
189     MPI_Group_free(&g3a);
190     MPI_Group_free(&g3b);
191     MPI_Group_free(&g4);
192     MPI_Group_free(&g5);
193     MPI_Group_free(&g6);
194     MPI_Group_free(&g7);
195     MPI_Group_free(&g8);
196     MPI_Group_free(&g9);
197     MPI_Group_free(&g10);
198     MPI_Comm_free(&dupcomm);
199     MPI_Comm_free(&splitcomm);
200     MPI_Comm_free(&newcomm);
201
202     MPI_Allreduce(&errs, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
203     if (worldrank == 0) {
204         if (toterr == 0)
205             printf(" No Errors\n");
206         else
207             printf("Found %d errors in MPI Group routines\n", toterr);
208     }
209
210     free(ranks);
211     free(ranks_out);
212     MPI_Finalize();
213     return toterr;
214 }