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) 2008 Cisco Systems, Inc. All rights reserved.
13 * Copyright (c) 2012-2013 Inria. 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 - Returns the shifted source and destination ranks, given a
27 * shift direction and amount
28 *
29 * @param comm communicator with cartesian structure (handle)
30 * @param direction coordinate directionension of shift (integer)
31 * @param disp displacement (> 0: upwards shift, < 0: downwards shift) (integer)
32 * @param rank_source rank of source process (integer)
33 * @param rank_dest rank of destination process (integer)
34 *
35 * The 'direction' argument is in the range '[0,n-1]' for an n-directionensional
36 * Cartesian mesh.
37 *
38 * @retval MPI_SUCCESS
39 */
40 int mca_topo_base_cart_shift(ompi_communicator_t* comm,
41 int direction,
42 int disp,
43 int *rank_source,
44 int *rank_dest)
45 {
46 int factor, thisdirection = 0, thisperiod = 0, ord;
47 int srcord, destord, i, *d, *q;
48
49 /*
50 * Handle the trivial case.
51 */
52 ord = ompi_comm_rank(comm);
53
54 if (disp == 0) {
55 *rank_dest = *rank_source = ord;
56 return MPI_SUCCESS;
57 }
58 /*
59 * Compute the rank factor and ordinate.
60 */
61 factor = ompi_comm_size(comm);
62 d = comm->c_topo->mtc.cart->dims;
63 q = comm->c_topo->mtc.cart->periods;
64 for (i = 0; (i < comm->c_topo->mtc.cart->ndims) && (i <= direction); ++i, ++d, ++q) {
65 thisdirection = *d;
66 thisperiod = *q;
67
68 ord %= factor;
69 factor /= thisdirection;
70 }
71
72 ord /= factor;
73 /*
74 * Check the displacement value and compute the new ranks.
75 */
76 *rank_source = *rank_dest = MPI_UNDEFINED;
77
78 srcord = ord - disp;
79 destord = ord + disp;
80 if ( ((destord < 0) || (destord >= thisdirection)) && (!thisperiod) ) {
81 *rank_dest = MPI_PROC_NULL;
82 } else {
83 destord %= thisdirection;
84 if (destord < 0) destord += thisdirection;
85 *rank_dest = ompi_comm_rank(comm);
86 *rank_dest += ((destord - ord) * factor);
87 }
88 if ( ((srcord < 0) || (srcord >= thisdirection)) && (!thisperiod) ) {
89 *rank_source = MPI_PROC_NULL;
90 } else {
91 srcord %= thisdirection;
92 if (srcord < 0) srcord += thisdirection;
93 *rank_source= ompi_comm_rank(comm);
94 *rank_source += ((srcord - ord) * factor);
95 }
96
97 return MPI_SUCCESS;
98 }