Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add mpich3 test suite, to replace older one.
[simgrid.git] / teshsuite / smpi / mpich-test / topol / cartorder.c
1 #include "mpi.h"
2 #include <stdio.h>
3 #include "test.h"
4
5 /* This test makes sure that the ordering if reorder is FALSE is
6    as specified in 6.2, virtual topologies 
7
8    At the same time, it duplicates the tests in cart.c, but
9    with reorder = 0.
10 */
11
12 #define NUM_DIMS 2
13
14 int main( int argc, char **argv )
15 {
16     int              rank, size, i;
17     int              errors=0;
18     int              dims[NUM_DIMS];
19     int              periods[NUM_DIMS];
20     int              coords[NUM_DIMS];
21     int              new_coords[NUM_DIMS];
22     int              reorder = 0;
23     MPI_Comm         comm_temp, comm_cart, new_comm;
24     int              topo_status;
25     int              ndims;
26     int              new_rank;
27     int              remain_dims[NUM_DIMS];
28     int              newnewrank;
29
30     MPI_Init( &argc, &argv );
31
32     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
33     MPI_Comm_size( MPI_COMM_WORLD, &size );
34
35     /* Clear dims array and get dims for topology */
36     for(i=0;i<NUM_DIMS;i++) { dims[i] = 0; periods[i] = 0; }
37     MPI_Dims_create ( size, NUM_DIMS, dims );
38
39     /* Make a new communicator with a topology */
40     MPI_Cart_create ( MPI_COMM_WORLD, 2, dims, periods, reorder, &comm_temp );
41     MPI_Comm_dup ( comm_temp, &comm_cart );
42
43     /* Determine the status of the new communicator */
44     MPI_Topo_test ( comm_cart, &topo_status );
45     if (topo_status != MPI_CART) {
46         printf( "topo_status of duped comm is not MPI_CART\n" );
47         errors++;
48     }
49
50     /* How many dims do we have? */
51     MPI_Cartdim_get( comm_cart, &ndims );
52     if ( ndims != NUM_DIMS ) {
53         printf( "Number of dims of duped comm (%d) should be %d\n", 
54                 ndims, NUM_DIMS );
55         errors++;
56     }
57
58     /* Get the topology, does it agree with what we put in? */
59     for(i=0;i<NUM_DIMS;i++) { dims[i] = 0; periods[i] = 0; }
60     MPI_Cart_get ( comm_cart, NUM_DIMS, dims, periods, coords );
61
62     /* Check that the coordinates are correct */
63 #if NUM_DIMS == 2
64     if (rank != coords[1] + coords[0] * dims[1]) {
65         errors++;
66         fprintf( stderr, 
67 "Did not get expected coordinate (row major required by MPI standard 6.2)\n" );
68     }
69 #endif
70     /* Does the mapping from coords to rank work? */
71     MPI_Cart_rank ( comm_cart, coords, &new_rank );
72     if ( new_rank != rank ) {
73         printf( "New rank of duped comm (%d) != old rank (%d)\n", 
74                 new_rank, rank );
75         errors++;
76     }
77
78     /* Does the mapping from rank to coords work */
79     MPI_Cart_coords ( comm_cart, rank, NUM_DIMS, new_coords );
80     for (i=0;i<NUM_DIMS;i++) 
81         if ( coords[i] != new_coords[i] ) {
82             printf( "Old coords[%d] of duped comm (%d) != new_coords (%d)\n", 
83                     i, coords[i], new_coords[i] );
84             errors++;
85         }
86
87     /* Let's shift in each dimension and see how it works!   */
88     /* Because it's late and I'm tired, I'm not making this  */
89     /* automatically test itself.                            */
90     for (i=0;i<NUM_DIMS;i++) {
91       int source, dest;
92       MPI_Cart_shift(comm_cart, i, 1, &source, &dest);
93 #ifdef VERBOSE      
94       printf ("[%d] Shifting %d in the %d dimension\n",rank,1,i);
95       printf ("[%d]    source = %d  dest = %d\n",rank,source,dest); 
96 #endif
97     }
98
99     /* Subdivide */
100     remain_dims[0] = 0; 
101     for (i=1; i<NUM_DIMS; i++) remain_dims[i] = 1;
102     MPI_Cart_sub ( comm_cart, remain_dims, &new_comm );
103
104     /* Determine the status of the new communicator */
105     MPI_Topo_test ( new_comm, &topo_status );
106     if (topo_status != MPI_CART) {
107         printf( "topo_status of cartsub comm is not MPI_CART\n" );
108         errors++;
109     }
110
111     /* How many dims do we have? */
112     MPI_Cartdim_get( new_comm, &ndims );
113     if ( ndims != NUM_DIMS-1 ) {
114         printf( "Number of dims of cartsub comm (%d) should be %d\n", 
115                 ndims, NUM_DIMS-1 );
116         errors++;
117     }
118
119     /* Get the topology, does it agree with what we put in? */
120     for(i=0;i<NUM_DIMS-1;i++) { dims[i] = 0; periods[i] = 0; }
121     MPI_Cart_get ( new_comm, ndims, dims, periods, coords );
122     
123     /* Does the mapping from coords to rank work? */
124     MPI_Comm_rank ( new_comm, &newnewrank );
125     MPI_Cart_rank ( new_comm, coords, &new_rank );
126     if ( new_rank != newnewrank ) {
127         printf( "New rank of cartsub comm (%d) != old rank (%d)\n", 
128                 new_rank, newnewrank );
129         errors++;
130     }
131     /* Does the mapping from rank to coords work */
132     MPI_Cart_coords ( new_comm, new_rank, NUM_DIMS -1, new_coords );
133     for (i=0;i<NUM_DIMS-1;i++) 
134         if ( coords[i] != new_coords[i] ) {
135             printf( "Old coords[%d] of cartsub comm (%d) != new_coords (%d)\n", 
136                     i, coords[i], new_coords[i] );
137             errors++;
138         }
139
140     /* We're at the end */
141     MPI_Comm_free( &new_comm );
142     MPI_Comm_free( &comm_temp );
143     MPI_Comm_free( &comm_cart );
144     Test_Waitforall( );
145     if (errors) printf( "[%d] done with %d ERRORS!\n", rank,errors );
146     MPI_Finalize();
147     return 0;
148 }