7 int main( int argc, char **argv )
12 int periods[NUM_DIMS];
14 int new_coords[NUM_DIMS];
16 MPI_Comm comm_temp, comm_cart, new_comm;
20 int remain_dims[NUM_DIMS];
23 MPI_Init( &argc, &argv );
25 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
26 MPI_Comm_size( MPI_COMM_WORLD, &size );
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 );
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 );
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" );
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",
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 );
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",
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] );
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++) {
77 MPI_Cart_shift(comm_cart, i, 1, &source, &dest);
79 printf ("[%d] Shifting %d in the %d dimension\n",rank,1,i);
80 printf ("[%d] source = %d dest = %d\n",rank,source,dest);
86 for (i=1; i<NUM_DIMS; i++) remain_dims[i] = 1;
87 MPI_Cart_sub ( comm_cart, remain_dims, &new_comm );
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" );
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",
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 );
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 );
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] );
126 /* We're at the end */
127 MPI_Comm_free( &new_comm );
128 MPI_Comm_free( &comm_temp );
129 MPI_Comm_free( &comm_cart );
131 if (errors) printf( "[%d] done with %d ERRORS!\n", rank,errors );