Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'S4U'
[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         exit(1);
37     }
38     if (size < 4) {
39         fprintf( stderr, "This test requiers at least 4 processes\n" );
40         MPI_Abort( MPI_COMM_WORLD, 1 );
41         exit(1);
42     }
43
44     MPI_Comm_group( comm, &gworld );
45     for (i=0; i<size; i++) {
46         ranks[i] = i;
47         ranksout[i] = -1;
48     }
49     /* Try translating ranks from comm world compared against
50        comm self, so most will be UNDEFINED */
51     MPI_Group_translate_ranks( gworld, size, ranks, gself, ranksout );
52     
53     for (i=0; i<size; i++) {
54         if (i == rank) {
55             if (ranksout[i] != 0) {
56                 printf( "[%d] Rank %d is %d but should be 0\n", rank, 
57                         i, ranksout[i] );
58                 errs++;
59             }
60         }
61         else {
62             if (ranksout[i] != MPI_UNDEFINED) {
63                 printf( "[%d] Rank %d is %d but should be undefined\n", rank, 
64                         i, ranksout[i] );
65                 errs++;
66             }
67         }
68     }
69
70     /* MPI-2 Errata requires that MPI_PROC_NULL is mapped to MPI_PROC_NULL */
71     ranks[0] = MPI_PROC_NULL;
72     ranks[1] = 1;
73     ranks[2] = rank;
74     ranks[3] = MPI_PROC_NULL;
75     for (i=0; i<4; i++) ranksout[i] = -1;
76
77     MPI_Group_translate_ranks( gworld, 4, ranks, gself, ranksout );
78     if (ranksout[0] != MPI_PROC_NULL) {
79         printf( "[%d] Rank[0] should be MPI_PROC_NULL but is %d\n",
80                 rank, ranksout[0] );
81         errs++;
82     }
83     if (rank != 1 && ranksout[1] != MPI_UNDEFINED) {
84         printf( "[%d] Rank[1] should be MPI_UNDEFINED but is %d\n",
85                 rank, ranksout[1] );
86         errs++;
87     }
88     if (rank == 1 && ranksout[1] != 0) {
89         printf( "[%d] Rank[1] should be 0 but is %d\n",
90                 rank, ranksout[1] );
91         errs++;
92     }
93     if (ranksout[2] != 0) {
94         printf( "[%d] Rank[2] should be 0 but is %d\n",
95                 rank, ranksout[2] );
96         errs++;
97     }
98     if (ranksout[3] != MPI_PROC_NULL) {
99         printf( "[%d] Rank[3] should be MPI_PROC_NULL but is %d\n",
100                 rank, ranksout[3] );
101         errs++;
102     }
103
104     MPI_Group_free(&gself);
105
106     /* Now, try comparing small groups against larger groups, and use groups
107        with irregular members (to bypass optimizations in group_translate_ranks
108        for simple groups)
109      */
110     nelms = 0;
111     ranks[nelms++] = size - 2;
112     ranks[nelms++] = 0;
113     if (rank != 0 && rank != size - 2) {
114         ranks[nelms++] = rank; 
115     }
116
117     MPI_Group_incl( gworld, nelms, ranks, &ngroup );
118
119     for (i=0; i<nelms; i++) ranksout[i] = -1;
120     ranksin[0] = 1;
121     ranksin[1] = 0;
122     ranksin[2] = MPI_PROC_NULL;
123     ranksin[3] = 2;
124     MPI_Group_translate_ranks( ngroup, nelms+1, ranksin, gworld, ranksout );
125     for (i=0; i<nelms+1; i++) {
126         if (ranksin[i] == MPI_PROC_NULL) {
127             if (ranksout[i] != MPI_PROC_NULL) {
128                 fprintf( stderr, "Input rank for proc_null but output was %d\n",
129                          ranksout[i] );
130                 errs++;
131             }
132         }
133         else if (ranksout[i] != ranks[ranksin[i]]) {
134             fprintf( stderr, "Expected ranksout[%d] = %d but found %d\n",
135                      i, ranks[ranksin[i]], ranksout[i] );
136             errs++;
137         }
138     }
139     
140     range[0][0] = size -1 ;
141     range[0][1] = 0;
142     range[0][2] = -1;
143     MPI_Group_range_incl( gworld, 1, range, &galt);
144     for (i=0; i<nelms+1; i++) ranksout[i] = -1;
145     MPI_Group_translate_ranks( ngroup, nelms+1, ranksin, galt, ranksout );
146     for (i=0; i<nelms+1; i++) {
147         if (ranksin[i] == MPI_PROC_NULL) {
148             if (ranksout[i] != MPI_PROC_NULL) {
149                 fprintf( stderr, "Input rank for proc_null but output was %d\n",
150                          ranksout[i] );
151                 errs++;
152             }
153         }
154         else if (ranksout[i] != (size-1)-ranks[ranksin[i]]) {
155             fprintf( stderr, "Expected ranksout[%d] = %d but found %d\n",
156                      i, (size-1)-ranks[ranksin[i]], ranksout[i] );
157             errs++;
158         }
159     }
160     
161     
162     MPI_Group_free(&gworld);
163     MPI_Group_free(&galt);
164     MPI_Group_free(&ngroup);
165
166     MTest_Finalize( errs );
167     MPI_Finalize();
168
169     return 0;
170 }