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