1 /*
2 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
3 * University Research and Technology
4 * Corporation. All rights reserved.
5 * Copyright (c) 2004-2013 The University of Tennessee and The University
6 * of Tennessee Research Foundation. All rights
7 * reserved.
8 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
9 * University of Stuttgart. All rights reserved.
10 * Copyright (c) 2004-2005 The Regents of the University of California.
11 * All rights reserved.
12 * Copyright (c) 2015 Research Organization for Information Science
13 * and Technology (RIST). All rights reserved.
14 * $COPYRIGHT$
15 *
16 * Additional copyrights may follow
17 *
18 * $HEADER$
19 */
20
21 #include "ompi_config.h"
22 #include "ompi/mca/topo/base/base.h"
23 #include "ompi/communicator/communicator.h"
24
25 /*
26 * function - mca_topo_base_cart_map
27 *
28 * @param comm input communicator (handle)
29 * @param ndims number of dimensions of cartesian structure (integer)
30 * @param dims integer array of size 'ndims' specifying the number of
31 * processes in each coordinate direction
32 * @param periods logical array of size 'ndims' specifying the
33 * periodicity specification in each coordinate direction
34 * @param newrank reordered rank of the calling process; 'MPI_UNDEFINED'
35 * if calling process does not belong to grid (integer)
36 *
37 * @retval MPI_SUCCESS
38 * @retval MPI_ERR_DIMS
39 */
40
41 int mca_topo_base_cart_map(ompi_communicator_t* comm,
42 int ndims,
43 const int *dims, const int *periods, int *newrank)
44 {
45 int nprocs, rank, size, i;
46
47 /*
48 * Compute the # of processes in the grid.
49 */
50 nprocs = 1;
51 for (i = 0 ; i < ndims; ++i) {
52 if (dims[i] <= 0) {
53 return MPI_ERR_DIMS;
54 }
55 nprocs *= dims[i];
56 }
57 /*
58 * Check that number of processes <= size of communicator.
59 */
60 size = ompi_comm_size(comm);
61 if (nprocs > size) {
62 return MPI_ERR_DIMS;
63 }
64 /*
65 * Compute my new rank.
66 */
67 rank = ompi_comm_rank(comm);
68 *newrank = ((rank < 0) || (rank >= nprocs)) ? MPI_UNDEFINED : rank;
69
70 return MPI_SUCCESS;
71 }