Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / teshsuite / smpi / mpich3-test / group / grouptest.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 #include "mpi.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 int main(int argc, char *argv[])
12 {
13     MPI_Group g1, g2, g4, g5, g45, selfgroup, g6;
14     int ranks[16], size, rank, myrank, range[1][3];
15     int errs = 0;
16     int i, rin[16], rout[16], result;
17
18     MPI_Init(&argc, &argv);
19
20     MPI_Comm_group(MPI_COMM_WORLD, &g1);
21     MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
22     MPI_Comm_size(MPI_COMM_WORLD, &size);
23     if (size < 8) {
24         fprintf(stderr, "Test requires 8 processes (16 preferred) only %d provided\n", size);
25         errs++;
26     }
27
28     /* 16 members, this process is rank 0, return in group 1 */
29     ranks[0] = myrank;
30     ranks[1] = 2;
31     ranks[2] = 7;
32     if (myrank == 2)
33         ranks[1] = 3;
34     if (myrank == 7)
35         ranks[2] = 6;
36     MPI_Group_incl(g1, 3, ranks, &g2);
37
38     /* Check the resulting group */
39     MPI_Group_size(g2, &size);
40     MPI_Group_rank(g2, &rank);
41
42     if (size != 3) {
43         fprintf(stderr, "Size should be %d, is %d\n", 3, size);
44         errs++;
45     }
46     if (rank != 0) {
47         fprintf(stderr, "Rank should be %d, is %d\n", 0, rank);
48         errs++;
49     }
50
51     rin[0] = 0;
52     rin[1] = 1;
53     rin[2] = 2;
54     MPI_Group_translate_ranks(g2, 3, rin, g1, rout);
55     for (i = 0; i < 3; i++) {
56         if (rout[i] != ranks[i]) {
57             fprintf(stderr, "translated rank[%d] %d should be %d\n", i, rout[i], ranks[i]);
58             errs++;
59         }
60     }
61
62     /* Translate the process of the self group against another group */
63     MPI_Comm_group(MPI_COMM_SELF, &selfgroup);
64     rin[0] = 0;
65     MPI_Group_translate_ranks(selfgroup, 1, rin, g1, rout);
66     if (rout[0] != myrank) {
67         fprintf(stderr, "translated of self is %d should be %d\n", rout[0], myrank);
68         errs++;
69     }
70
71     for (i = 0; i < size; i++)
72         rin[i] = i;
73     MPI_Group_translate_ranks(g1, size, rin, selfgroup, rout);
74     for (i = 0; i < size; i++) {
75         if (i == myrank && rout[i] != 0) {
76             fprintf(stderr, "translated world to self of %d is %d\n", i, rout[i]);
77             errs++;
78         }
79         else if (i != myrank && rout[i] != MPI_UNDEFINED) {
80             fprintf(stderr, "translated world to self of %d should be undefined, is %d\n",
81                     i, rout[i]);
82             errs++;
83         }
84     }
85     MPI_Group_free(&selfgroup);
86
87     /* Exclude everyone in our group */
88     {
89         int ii, *lranks, g1size;
90
91         MPI_Group_size(g1, &g1size);
92
93         lranks = (int *) malloc(g1size * sizeof(int));
94         for (ii = 0; ii < g1size; ii++)
95             lranks[ii] = ii;
96         MPI_Group_excl(g1, g1size, lranks, &g6);
97         if (g6 != MPI_GROUP_EMPTY) {
98             fprintf(stderr, "Group formed by excluding all ranks not empty\n");
99             errs++;
100             MPI_Group_free(&g6);
101         }
102         free(lranks);
103     }
104
105     /* Add tests for additional group operations */
106     /*
107      * g2 = incl 1,3,7
108      * g3 = excl 1,3,7
109      * intersect (w, g2) => g2
110      * intersect (w, g3) => g3
111      * intersect (g2, g3) => empty
112      *
113      * g4 = rincl 1:n-1:2
114      * g5 = rexcl 1:n-1:2
115      * union(g4, g5) => world
116      * g6 = rincl n-1:1:-1
117      * g7 = rexcl n-1:1:-1
118      * union(g6, g7) => concat of entries, similar to world
119      * diff(w, g2) => g3
120      */
121     MPI_Group_free(&g2);
122
123     range[0][0] = 1;
124     range[0][1] = size - 1;
125     range[0][2] = 2;
126     MPI_Group_range_excl(g1, 1, range, &g5);
127
128     range[0][0] = 1;
129     range[0][1] = size - 1;
130     range[0][2] = 2;
131     MPI_Group_range_incl(g1, 1, range, &g4);
132
133     MPI_Group_union(g4, g5, &g45);
134
135     MPI_Group_compare(MPI_GROUP_EMPTY, g4, &result);
136     if (result != MPI_UNEQUAL) {
137         errs++;
138         fprintf(stderr, "Comparison with empty group gave %d, not 3\n", result);
139     }
140     MPI_Group_free(&g4);
141     MPI_Group_free(&g5);
142     MPI_Group_free(&g45);
143
144     /* Now, duplicate the test, but using negative strides */
145     range[0][0] = size - 1;
146     range[0][1] = 1;
147     range[0][2] = -2;
148     MPI_Group_range_excl(g1, 1, range, &g5);
149
150     range[0][0] = size - 1;
151     range[0][1] = 1;
152     range[0][2] = -2;
153     MPI_Group_range_incl(g1, 1, range, &g4);
154
155     MPI_Group_union(g4, g5, &g45);
156
157     MPI_Group_compare(MPI_GROUP_EMPTY, g4, &result);
158     if (result != MPI_UNEQUAL) {
159         errs++;
160         fprintf(stderr,
161                 "Comparison with empty group (formed with negative strides) gave %d, not 3\n",
162                 result);
163     }
164     MPI_Group_free(&g4);
165     MPI_Group_free(&g5);
166     MPI_Group_free(&g45);
167     MPI_Group_free(&g1);
168
169     if (myrank == 0) {
170         if (errs == 0) {
171             printf(" No Errors\n");
172         }
173         else {
174             printf("Found %d errors\n", errs);
175         }
176     }
177
178     MPI_Finalize();
179     return 0;
180 }