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 elsewhere)
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++)
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) {
74 fprintf(stdout, "Translate ranks got %d expected %d\n", 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", result);
84 MPI_Comm_dup(comm, &dupcomm);
85 MPI_Comm_group(dupcomm, &g2);
86 MPI_Group_compare(basegroup, g2, &result);
87 if (result != MPI_IDENT) {
89 fprintf(stdout, "Group compare should have been ident, was %d\n", result);
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) {
96 fprintf(stdout, "Group compare should have been unequal, was %d\n", result);
99 /* Build two groups that have this process and one other, but do not
100 * have the same processes */
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) {
110 "Group compare of equal sized but different groups should have been unequal, was %d\n",
115 /* Build two new groups by excluding members; use Union to put them
119 for (i = 0; i < size; 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) {
181 fprintf(stdout, "MPI_GROUP_EMPTY didn't compare against empty group\n");
184 /* printf("freeing groups\n"); fflush(stdout); */
185 MPI_Group_free(&basegroup);
189 MPI_Group_free(&g3a);
190 MPI_Group_free(&g3b);
197 MPI_Group_free(&g10);
198 MPI_Comm_free(&dupcomm);
199 MPI_Comm_free(&splitcomm);
200 MPI_Comm_free(&newcomm);
202 MPI_Allreduce(&errs, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
203 if (worldrank == 0) {
205 printf(" No Errors\n");
207 printf("Found %d errors in MPI Group routines\n", toterr);