Logo AND Algorithmique Numérique Distribuée

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