Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add mpich3 test suite, to replace older one.
[simgrid.git] / teshsuite / smpi / mpich3-test / group / gtranks.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include <stdio.h>
8 #include "mpitest.h"
9
10 #define MAX_WORLD_SIZE 1024
11
12 int main( int argc, char *argv[] )
13 {
14     int errs = 0;
15     int ranks[MAX_WORLD_SIZE], ranksout[MAX_WORLD_SIZE], 
16         ranksin[MAX_WORLD_SIZE];
17     int range[1][3];
18     MPI_Group gworld, gself, ngroup, galt;
19     MPI_Comm  comm;
20     int rank, size, i, nelms;
21
22     MTest_Init( &argc, &argv );
23
24     MPI_Comm_group( MPI_COMM_SELF, &gself );
25
26     comm = MPI_COMM_WORLD;
27
28     MPI_Comm_size( comm, &size );
29     MPI_Comm_rank( comm, &rank );
30
31     if (size > MAX_WORLD_SIZE) {
32         fprintf( stderr, 
33          "This test requires a comm world with no more than %d processes\n", 
34                  MAX_WORLD_SIZE );
35         MPI_Abort( MPI_COMM_WORLD, 1 );
36     }
37     if (size < 4) {
38         fprintf( stderr, "This test requiers at least 4 processes\n" );
39         MPI_Abort( MPI_COMM_WORLD, 1 );
40     }
41
42     MPI_Comm_group( comm, &gworld );
43     for (i=0; i<size; i++) {
44         ranks[i] = i;
45         ranksout[i] = -1;
46     }
47     /* Try translating ranks from comm world compared against
48        comm self, so most will be UNDEFINED */
49     MPI_Group_translate_ranks( gworld, size, ranks, gself, ranksout );
50     
51     for (i=0; i<size; i++) {
52         if (i == rank) {
53             if (ranksout[i] != 0) {
54                 printf( "[%d] Rank %d is %d but should be 0\n", rank, 
55                         i, ranksout[i] );
56                 errs++;
57             }
58         }
59         else {
60             if (ranksout[i] != MPI_UNDEFINED) {
61                 printf( "[%d] Rank %d is %d but should be undefined\n", rank, 
62                         i, ranksout[i] );
63                 errs++;
64             }
65         }
66     }
67
68     /* MPI-2 Errata requires that MPI_PROC_NULL is mapped to MPI_PROC_NULL */
69     ranks[0] = MPI_PROC_NULL;
70     ranks[1] = 1;
71     ranks[2] = rank;
72     ranks[3] = MPI_PROC_NULL;
73     for (i=0; i<4; i++) ranksout[i] = -1;
74
75     MPI_Group_translate_ranks( gworld, 4, ranks, gself, ranksout );
76     if (ranksout[0] != MPI_PROC_NULL) {
77         printf( "[%d] Rank[0] should be MPI_PROC_NULL but is %d\n",
78                 rank, ranksout[0] );
79         errs++;
80     }
81     if (rank != 1 && ranksout[1] != MPI_UNDEFINED) {
82         printf( "[%d] Rank[1] should be MPI_UNDEFINED but is %d\n",
83                 rank, ranksout[1] );
84         errs++;
85     }
86     if (rank == 1 && ranksout[1] != 0) {
87         printf( "[%d] Rank[1] should be 0 but is %d\n",
88                 rank, ranksout[1] );
89         errs++;
90     }
91     if (ranksout[2] != 0) {
92         printf( "[%d] Rank[2] should be 0 but is %d\n",
93                 rank, ranksout[2] );
94         errs++;
95     }
96     if (ranksout[3] != MPI_PROC_NULL) {
97         printf( "[%d] Rank[3] should be MPI_PROC_NULL but is %d\n",
98                 rank, ranksout[3] );
99         errs++;
100     }
101
102     MPI_Group_free(&gself);
103
104     /* Now, try comparing small groups against larger groups, and use groups
105        with irregular members (to bypass optimizations in group_translate_ranks
106        for simple groups)
107      */
108     nelms = 0;
109     ranks[nelms++] = size - 2;
110     ranks[nelms++] = 0;
111     if (rank != 0 && rank != size - 2) {
112         ranks[nelms++] = rank; 
113     }
114
115     MPI_Group_incl( gworld, nelms, ranks, &ngroup );
116
117     for (i=0; i<nelms; i++) ranksout[i] = -1;
118     ranksin[0] = 1;
119     ranksin[1] = 0;
120     ranksin[2] = MPI_PROC_NULL;
121     ranksin[3] = 2;
122     MPI_Group_translate_ranks( ngroup, nelms+1, ranksin, gworld, ranksout );
123     for (i=0; i<nelms+1; i++) {
124         if (ranksin[i] == MPI_PROC_NULL) {
125             if (ranksout[i] != MPI_PROC_NULL) {
126                 fprintf( stderr, "Input rank for proc_null but output was %d\n",
127                          ranksout[i] );
128                 errs++;
129             }
130         }
131         else if (ranksout[i] != ranks[ranksin[i]]) {
132             fprintf( stderr, "Expected ranksout[%d] = %d but found %d\n",
133                      i, ranks[ranksin[i]], ranksout[i] );
134             errs++;
135         }
136     }
137     
138     range[0][0] = size -1 ;
139     range[0][1] = 0;
140     range[0][2] = -1;
141     MPI_Group_range_incl( gworld, 1, range, &galt);
142     for (i=0; i<nelms+1; i++) ranksout[i] = -1;
143     MPI_Group_translate_ranks( ngroup, nelms+1, ranksin, galt, ranksout );
144     for (i=0; i<nelms+1; i++) {
145         if (ranksin[i] == MPI_PROC_NULL) {
146             if (ranksout[i] != MPI_PROC_NULL) {
147                 fprintf( stderr, "Input rank for proc_null but output was %d\n",
148                          ranksout[i] );
149                 errs++;
150             }
151         }
152         else if (ranksout[i] != (size-1)-ranks[ranksin[i]]) {
153             fprintf( stderr, "Expected ranksout[%d] = %d but found %d\n",
154                      i, (size-1)-ranks[ranksin[i]], ranksout[i] );
155             errs++;
156         }
157     }
158     
159     
160     MPI_Group_free(&gworld);
161     MPI_Group_free(&galt);
162     MPI_Group_free(&ngroup);
163
164     MTest_Finalize( errs );
165     MPI_Finalize();
166
167     return 0;
168 }