- int retval = MPI_SUCCESS;
- int i;
- MPI_Topology newCart;
- MPI_Group newGroup, oldGroup;
- int rank, nranks, newSize;
-
- rank = smpi_comm_rank(comm_old);
-
- newSize = 1;
- if(ndims != 0) {
- for (i = 0 ; i < ndims ; i++) {
- newSize *= dims[i];
- }
- if(rank >= newSize) {
- *comm_cart = MPI_COMM_NULL;
- return retval;
- }
- newCart = smpi_cart_topo_create(ndims);
- oldGroup = smpi_comm_group(comm_old);
- newGroup = smpi_group_new(newSize);
- for (i = 0 ; i < newSize ; i++) {
- smpi_group_set_mapping(newGroup, smpi_group_index(oldGroup, i), i);
- }
-
- newCart->topo.cart->nnodes = newSize;
-
- // FIXME : code duplication... See smpi_mpi_cart_coords
- nranks = newSize;
- for (i=0; i<ndims; i++) {
- newCart->topo.cart->dims[i] = dims[i];
- newCart->topo.cart->periodic[i] = periods[i];
- nranks = nranks / dims[i];
- /* FIXME: nranks could be zero (?) */
- newCart->topo.cart->position[i] = rank / nranks;
- rank = rank % nranks;
- }
-
- *comm_cart = smpi_comm_new(newGroup, newCart);
+ int retval = MPI_SUCCESS;
+ MPI_Topology newCart;
+ MPI_Group newGroup;
+ MPI_Group oldGroup;
+ int nranks;
+
+ int rank = smpi_comm_rank(comm_old);
+
+ int newSize = 1;
+ if(ndims != 0) {
+ for (int i = 0 ; i < ndims ; i++) {
+ newSize *= dims[i];
+ }
+ if(rank >= newSize) {
+ *comm_cart = MPI_COMM_NULL;
+ return retval;
+ }
+ newCart = smpi_cart_topo_create(ndims);
+ oldGroup = smpi_comm_group(comm_old);
+ newGroup = smpi_group_new(newSize);
+ for (int i = 0 ; i < newSize ; i++) {
+ smpi_group_set_mapping(newGroup, smpi_group_index(oldGroup, i), i);
+ }
+
+ newCart->topo.cart->nnodes = newSize;
+
+ // FIXME : code duplication... See smpi_mpi_cart_coords
+ nranks = newSize;
+ for (int i=0; i<ndims; i++) {
+ newCart->topo.cart->dims[i] = dims[i];
+ newCart->topo.cart->periodic[i] = periods[i];
+ nranks = nranks / dims[i];
+ /* FIXME: nranks could be zero (?) */
+ newCart->topo.cart->position[i] = rank / nranks;
+ rank = rank % nranks;
+ }
+
+ *comm_cart = smpi_comm_new(newGroup, newCart);
+ } else {
+ if (rank == 0) {
+ newCart = smpi_cart_topo_create(ndims);
+ *comm_cart = smpi_comm_new(smpi_group_copy(smpi_comm_group(MPI_COMM_SELF)), newCart);