diff --git a/ompi/mca/topo/base/topo_base_cart_create.c b/ompi/mca/topo/base/topo_base_cart_create.c index 5df98950be..66cee67d68 100644 --- a/ompi/mca/topo/base/topo_base_cart_create.c +++ b/ompi/mca/topo/base/topo_base_cart_create.c @@ -1,3 +1,4 @@ +/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */ /* * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana * University Research and Technology @@ -10,6 +11,8 @@ * Copyright (c) 2004-2005 The Regents of the University of California. * All rights reserved. * Copyright (c) 2008 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2014 Los Alamos National Security, LLC. All right + * reserved. * $COPYRIGHT$ * * Additional copyrights may follow @@ -127,17 +130,27 @@ int mca_topo_base_cart_create(mca_topo_base_module_t *topo, } } - /* Copy the proc structure from the previous communicator over to - the new one. The topology module is then able to work on this - copy and rearrange it as it deems fit. */ - topo_procs = (ompi_proc_t**)malloc(num_procs * sizeof(ompi_proc_t *)); - if(OMPI_GROUP_IS_DENSE(old_comm->c_local_group)) { - memcpy(topo_procs, - old_comm->c_local_group->grp_proc_pointers, - num_procs * sizeof(ompi_proc_t *)); - } else { - for(i = 0 ; i < num_procs; i++) { - topo_procs[i] = ompi_group_peer_lookup(old_comm->c_local_group,i); + /* Don't do any of the other initialization if we're not supposed + to be part of the new communicator (because nnodes has been + reset to 0, making things like index[nnodes-1] be junk). + + JMS: This should really be refactored to use + comm_create_group(), because ompi_comm_allocate() still + complains about 0-byte mallocs in debug builds for 0-member + groups. */ + if (num_procs > 0) { + /* Copy the proc structure from the previous communicator over to + the new one. The topology module is then able to work on this + copy and rearrange it as it deems fit. */ + topo_procs = (ompi_proc_t**)malloc(num_procs * sizeof(ompi_proc_t *)); + if(OMPI_GROUP_IS_DENSE(old_comm->c_local_group)) { + memcpy(topo_procs, + old_comm->c_local_group->grp_proc_pointers, + num_procs * sizeof(ompi_proc_t *)); + } else { + for(i = 0 ; i < num_procs; i++) { + topo_procs[i] = ompi_group_peer_lookup(old_comm->c_local_group,i); + } } } diff --git a/ompi/mca/topo/base/topo_base_cart_sub.c b/ompi/mca/topo/base/topo_base_cart_sub.c index 3cef2e61ff..faa4eae632 100644 --- a/ompi/mca/topo/base/topo_base_cart_sub.c +++ b/ompi/mca/topo/base/topo_base_cart_sub.c @@ -1,3 +1,4 @@ +/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */ /* * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana * University Research and Technology @@ -10,6 +11,8 @@ * Copyright (c) 2004-2005 The Regents of the University of California. * All rights reserved. * Copyright (c) 2008 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2014 Los Alamos National Security, LLC. All rights + * reserved. * $COPYRIGHT$ * * Additional copyrights may follow @@ -120,28 +123,34 @@ int mca_topo_base_cart_sub (ompi_communicator_t* comm, cart->ndims = ndim; cart->dims = dorig; cart->periods = porig; - cart->coords = (int*)malloc(sizeof(int) * ndim); - if (NULL == cart->coords) { - free(cart->periods); - if(NULL != cart->dims) free(cart->dims); - free(cart); - return OMPI_ERR_OUT_OF_RESOURCE; - } - { /* setup the cartesian topology */ - int nprocs = temp_comm->c_local_group->grp_proc_count, - rank = temp_comm->c_local_group->grp_my_rank; - for (i = 0; i < ndim; ++i) { - nprocs /= cart->dims[i]; - cart->coords[i] = rank / nprocs; - rank %= nprocs; + /* NTH: protect against a 0-byte alloc in the ndims = 0 case */ + if (ndim > 0) { + cart->coords = (int*)malloc(sizeof(int) * ndim); + if (NULL == cart->coords) { + free(cart->periods); + if(NULL != cart->dims) free(cart->dims); + free(cart); + return OMPI_ERR_OUT_OF_RESOURCE; + } + { /* setup the cartesian topology */ + int nprocs = temp_comm->c_local_group->grp_proc_count, + rank = temp_comm->c_local_group->grp_my_rank; + + for (i = 0; i < ndim; ++i) { + nprocs /= cart->dims[i]; + cart->coords[i] = rank / nprocs; + rank %= nprocs; + } } } + temp_comm->c_topo = topo; temp_comm->c_topo->mtc.cart = cart; temp_comm->c_topo->reorder = false; temp_comm->c_flags |= OMPI_COMM_CART; } + *new_comm = temp_comm; return MPI_SUCCESS;