cart_map.c 5.71 KB
Newer Older
1
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 *
 *  (C) 2001 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

#include "mpiimpl.h"
#include "topo.h"

/* -- Begin Profiling Symbol Block for routine MPI_Cart_map */
#if defined(HAVE_PRAGMA_WEAK)
#pragma weak MPI_Cart_map = PMPI_Cart_map
#elif defined(HAVE_PRAGMA_HP_SEC_DEF)
#pragma _HP_SECONDARY_DEF PMPI_Cart_map  MPI_Cart_map
#elif defined(HAVE_PRAGMA_CRI_DUP)
#pragma _CRI duplicate MPI_Cart_map as PMPI_Cart_map
18
19
#elif defined(HAVE_WEAK_ATTRIBUTE)
int MPI_Cart_map(MPI_Comm comm, int ndims, const int dims[], const int periods[], int *newrank) __attribute__((weak,alias("PMPI_Cart_map")));
20
21
22
23
24
25
26
27
28
#endif
/* -- End Profiling Symbol Block */

/* Define MPICH_MPI_FROM_PMPI if weak symbols are not supported to build
   the MPI routines */
#ifndef MPICH_MPI_FROM_PMPI
#undef MPI_Cart_map
#define MPI_Cart_map PMPI_Cart_map

29
30
31
32
#undef FUNCNAME
#define FUNCNAME MPIR_Cart_map
#undef FCNAME
#define FCNAME MPIU_QUOTE(FUNCNAME)
33
34
int MPIR_Cart_map( const MPID_Comm *comm_ptr, int ndims, const int dims[], 
		   const int periodic[], int *newrank )
35
36
{
    int rank, nranks, i, size, mpi_errno = MPI_SUCCESS;
37
38
39
40

    MPIU_UNREFERENCED_ARG(periodic);

    /* Determine number of processes needed for topology */
41
42
    if (ndims == 0) {
	nranks = 1;
43
    } else {
44
45
46
47
	nranks = dims[0];
	for ( i=1; i<ndims; i++ )
	    nranks *= dims[i];
    }
48
49
50
    size = comm_ptr->remote_size;
    
    /* Test that the communicator is large enough */
51
52
    MPIU_ERR_CHKANDJUMP2(size < nranks, mpi_errno, MPI_ERR_DIMS, "**topotoolarge",
                         "**topotoolarge %d %d", size, nranks);
53
54
55
56
57
58
59
60
61
62

    /* Am I in this range? */
    rank = comm_ptr->rank;
    if ( rank < nranks )
	/* This relies on the ranks *not* being reordered by the current
	   Cartesian routines */
	*newrank = rank;
    else
	*newrank = MPI_UNDEFINED;

63
64
65
66
 fn_exit:
    return mpi_errno;
 fn_fail:
    goto fn_exit;
67
}
68
69
70
71
72
73
74
75
76
77
78

#undef FUNCNAME
#define FUNCNAME MPIR_Cart_map_impl
#undef FCNAME
#define FCNAME MPIU_QUOTE(FUNCNAME)
int MPIR_Cart_map_impl(const MPID_Comm *comm_ptr, int ndims, const int dims[],
                       const int periods[], int *newrank)
{
    int mpi_errno = MPI_SUCCESS;
        
    if (comm_ptr->topo_fns != NULL && comm_ptr->topo_fns->cartMap != NULL) {
79
	/* --BEGIN USEREXTENSION-- */
80
81
82
83
84
	mpi_errno = comm_ptr->topo_fns->cartMap( comm_ptr, ndims,
						 (const int*) dims,
						 (const int*) periods,
						 newrank );
        if (mpi_errno) MPIU_ERR_POP(mpi_errno);
85
	/* --END USEREXTENSION-- */
86
87
88
89
90
91
92
93
94
95
96
97
98
    } else {
	mpi_errno = MPIR_Cart_map( comm_ptr, ndims,
				   (const int*) dims,
				   (const int*) periods, newrank );
        if (mpi_errno) MPIU_ERR_POP(mpi_errno);
    }

 fn_exit:
    return mpi_errno;
 fn_fail:
    goto fn_exit;
}

99
100
101
102
#endif

#undef FUNCNAME
#define FUNCNAME MPI_Cart_map
103
104
#undef FCNAME
#define FCNAME MPIU_QUOTE(FUNCNAME)
105
106
107
108
109
110
111
112
113
114
115
/*@
MPI_Cart_map - Maps process to Cartesian topology information 

Input Parameters:
+ comm - input communicator (handle) 
. ndims - number of dimensions of Cartesian structure (integer) 
. dims - integer array of size 'ndims' specifying the number of processes in 
  each coordinate direction 
- periods - logical array of size 'ndims' specifying the periodicity 
  specification in each coordinate direction 

116
Output Parameters:
117
118
119
120
121
122
123
124
125
126
127
128
129
. newrank - reordered rank of the calling process; 'MPI_UNDEFINED' if 
  calling process does not belong to grid (integer) 

.N SignalSafe

.N Fortran

.N Errors
.N MPI_SUCCESS
.N MPI_ERR_COMM
.N MPI_ERR_DIMS
.N MPI_ERR_ARG
@*/
130
int MPI_Cart_map(MPI_Comm comm, int ndims, const int dims[], const int periods[],
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
		 int *newrank)
{
    int mpi_errno = MPI_SUCCESS;
    MPID_Comm *comm_ptr = NULL;
    MPID_MPI_STATE_DECL(MPID_STATE_MPI_CART_MAP);

    MPIR_ERRTEST_INITIALIZED_ORDIE();
    
    MPID_MPI_FUNC_ENTER(MPID_STATE_MPI_CART_MAP);

    /* Validate parameters, especially handles needing to be converted */
#   ifdef HAVE_ERROR_CHECKING
    {
        MPID_BEGIN_ERROR_CHECKS;
        {
146
	    MPIR_ERRTEST_COMM(comm, mpi_errno);
147
148
149
150
151
152
        }
        MPID_END_ERROR_CHECKS;
    }
#   endif
    
    /* Convert MPI object handles to object pointers */
153
    MPID_Comm_get_ptr( comm, comm_ptr );
154
155
156
157
158
159
160
161

    /* Validate parameters and objects (post conversion) */
#   ifdef HAVE_ERROR_CHECKING
    {
        MPID_BEGIN_ERROR_CHECKS;
        {
            /* Validate comm_ptr */
            MPID_Comm_valid_ptr( comm_ptr, mpi_errno );
162
            if (mpi_errno) goto fn_fail;
163
164
165
	    /* If comm_ptr is not valid, it will be reset to null */
	    MPIR_ERRTEST_ARGNULL(newrank,"newrank",mpi_errno);
	    MPIR_ERRTEST_ARGNULL(dims,"dims",mpi_errno);
166
167
	    /* As of MPI 2.1, 0-dimensional cartesian topologies are valid */
	    if (ndims < 0) {
168
169
170
171
		mpi_errno = MPIR_Err_create_code( MPI_SUCCESS, 
				  MPIR_ERR_RECOVERABLE, 
				  FCNAME, __LINE__, MPI_ERR_DIMS,
				  "**dims", "**dims %d", ndims );
172
173
                goto fn_fail;
            }
174
175
176
177
178
179
        }
        MPID_END_ERROR_CHECKS;
    }
#   endif /* HAVE_ERROR_CHECKING */

    /* ... body of routine ...  */
180
181
182
183
    mpi_errno = MPIR_Cart_map_impl( comm_ptr, ndims, 
                                    (const int*) dims,
                                    (const int*) periods, newrank );
    if (mpi_errno) MPIU_ERR_POP(mpi_errno);
184
185
186
187
188
189
190
191
    /* ... end of body of routine ... */

  fn_exit:
    MPID_MPI_FUNC_EXIT(MPID_STATE_MPI_CART_MAP);
    return mpi_errno;

    /* --BEGIN ERROR HANDLING-- */
  fn_fail:
192
#   ifdef HAVE_ERROR_CHECKING
193
194
195
196
    {
	mpi_errno = MPIR_Err_create_code(
	    mpi_errno, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER, 
	    "**mpi_cart_map",
197
	    "**mpi_cart_map %C %d %p %p %p", comm, ndims, dims, periods,
198
199
	    newrank);
    }
200
#   endif
201
202
203
204
    mpi_errno = MPIR_Err_return_comm( comm_ptr, FCNAME, mpi_errno );
    goto fn_exit;
    /* --END ERROR HANDLING-- */
}