1 #include "xbt/sysdep.h"
7 typedef struct s_smpi_mpi_topology {
13 } s_smpi_mpi_topology_t;
17 void smpi_topo_destroy(MPI_Topology topo) {
32 MPI_Topology smpi_topo_create(int ndims) {
33 MPI_Topology topo = xbt_malloc(sizeof(*topo));
36 topo->dims = xbt_malloc(ndims * sizeof(*topo->dims));
37 topo->periodic = xbt_malloc(ndims * sizeof(*topo->periodic));
38 topo->position = xbt_malloc(ndims * sizeof(*topo->position));
42 /* reorder is ignored, don't know what would be the consequences of a dumb
43 * reordering but neither do I see the point of reordering*/
44 int smpi_mpi_cart_create(MPI_Comm comm_old, int ndims, int dims[],
45 int periods[], int reorder, MPI_Comm *comm_cart) {
46 int retval = MPI_SUCCESS;
49 MPI_Group newGroup, oldGroup;
50 int rank, nranks, newSize;
54 rank = smpi_comm_rank(comm_old);
60 topo = smpi_topo_create(ndims);
61 for (i = 0 ; i < ndims ; i++) {
65 *comm_cart = MPI_COMM_NULL;
68 oldGroup = smpi_comm_group(comm_old);
69 newGroup = smpi_group_new(newSize);
70 for (i = 0 ; i < newSize ; i++) {
71 smpi_group_set_mapping(newGroup, smpi_group_index(oldGroup, i), i);
74 topo->nnodes = newSize;
76 memcpy(topo->dims, dims, ndims * sizeof(*topo->dims));
77 memcpy(topo->periodic, periods, ndims * sizeof(*topo->periodic));
79 // code duplication... See smpi_mpi_cart_coords
81 for (i=0; i<ndims; i++)
83 topo->dims[i] = dims[i];
84 topo->periodic[i] = periods[i];
85 nranks = nranks / dims[i];
86 /* FIXME: nranks could be zero (?) */
87 topo->position[i] = rank / nranks;
91 *comm_cart = smpi_comm_new(newGroup, topo);
95 topo = smpi_topo_create(ndims);
96 *comm_cart = smpi_comm_new(smpi_comm_group(MPI_COMM_SELF), topo);
99 *comm_cart = MPI_COMM_NULL;
105 int smpi_mpi_cart_sub(MPI_Comm comm, const int remain_dims[], MPI_Comm *newcomm) {
106 MPI_Topology oldTopo = smpi_comm_topo(comm);
107 int oldNDims = oldTopo->ndims;
108 int i, j = 0, newNDims, *newDims = NULL, *newPeriodic = NULL;
110 if (remain_dims == NULL && oldNDims != 0) {
114 for (i = 0 ; i < oldNDims ; i++) {
115 if (remain_dims[i]) newNDims++;
119 newDims = malloc(newNDims * sizeof(*newDims));
120 newPeriodic = malloc(newNDims * sizeof(*newPeriodic));
122 // that should not segfault
123 for (i = 0 ; j < newNDims ; i++) {
125 newDims[j] = oldTopo->dims[i];
126 newPeriodic[j] = oldTopo->periodic[i];
131 return smpi_mpi_cart_create(comm, newNDims, newDims, newPeriodic, 0, newcomm);
137 int smpi_mpi_cart_coords(MPI_Comm comm, int rank, int maxdims,
141 MPI_Topology topo = smpi_comm_topo(comm);
143 nnodes = topo->nnodes;
144 for ( i=0; i < topo->ndims; i++ ) {
145 nnodes = nnodes / topo->dims[i];
146 coords[i] = rank / nnodes;
147 rank = rank % nnodes;
152 int smpi_mpi_cart_get(MPI_Comm comm, int maxdims, int* dims, int* periods, int* coords) {
153 MPI_Topology topo = smpi_comm_topo(comm);
155 for(i = 0 ; i < maxdims ; i++) {
156 dims[i] = topo->dims[i];
157 periods[i] = topo->periodic[i];
158 coords[i] = topo->position[i];
163 int smpi_mpi_cart_rank(MPI_Comm comm, int* coords, int* rank) {
164 MPI_Topology topo = smpi_comm_topo(comm);
165 int ndims = topo->ndims;
166 int multiplier, coord,i;
172 for ( i=ndims-1; i >=0; i-- ) {
175 /* Should we check first for args correction, then process,
176 * or check while we work (as it is currently done) ? */
177 if (coord >= topo->dims[i]) {
178 if ( topo->periodic[i] ) {
179 coord = coord % topo->dims[i];
182 // Should I do that ?
187 else if (coord < 0) {
188 if(topo->periodic[i]) {
189 coord = coord % topo->dims[i];
190 if (coord) coord = topo->dims[i] + coord;
198 *rank += multiplier * coord;
199 multiplier *= topo->dims[i];
204 int smpi_mpi_cart_shift(MPI_Comm comm, int direction, int disp,
205 int *rank_source, int *rank_dest) {
206 MPI_Topology topo = smpi_comm_topo(comm);
207 int position[topo->ndims];
210 if(topo->ndims == 0) {
213 if (topo->ndims < direction) {
217 smpi_mpi_cart_coords(comm, smpi_comm_rank(comm), topo->ndims, position);
218 position[direction] += disp;
220 if(position[direction] < 0 || position[direction] >= topo->dims[direction]) {
221 if(topo->periodic[direction]) {
222 position[direction] %= topo->dims[direction];
223 smpi_mpi_cart_rank(comm, position, rank_dest);
226 *rank_dest = MPI_PROC_NULL;
230 smpi_mpi_cart_rank(comm, position, rank_dest);
233 position[direction] = topo->position[direction] - disp;
234 if(position[direction] < 0 || position[direction] >= topo->dims[direction]) {
235 if(topo->periodic[direction]) {
236 position[direction] %= topo->dims[direction];
237 smpi_mpi_cart_rank(comm, position, rank_source);
240 *rank_source = MPI_PROC_NULL;
244 smpi_mpi_cart_rank(comm, position, rank_source);
250 int smpi_mpi_cartdim_get(MPI_Comm comm, int *ndims) {
251 MPI_Topology topo = smpi_comm_topo(comm);
253 *ndims = topo->ndims;
259 // Everything below has been taken from ompi, but could be easily rewritten.
262 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
263 * University Research and Technology
264 * Corporation. All rights reserved.
265 * Copyright (c) 2004-2005 The University of Tennessee and The University
266 * of Tennessee Research Foundation. All rights
268 * Copyright (c) 2004-2014 High Performance Computing Center Stuttgart,
269 * University of Stuttgart. All rights reserved.
270 * Copyright (c) 2004-2005 The Regents of the University of California.
271 * All rights reserved.
272 * Copyright (c) 2012 Los Alamos National Security, LLC. All rights
274 * Copyright (c) 2014 Intel, Inc. All rights reserved
277 * Additional copyrights may follow
283 /* static functions */
284 static int assignnodes(int ndim, int nfactor, int *pfacts,int **pdims);
285 static int getfactors(int num, int *nfators, int **factors);
288 * This is a utility function, no need to have anything in the lower
289 * layer for this at all
291 int smpi_mpi_dims_create(int nnodes, int ndims, int dims[])
302 /* Get # of free-to-be-assigned processes and # of free dimensions */
305 for (i = 0, p = dims; i < ndims; ++i,++p) {
308 } else if ((*p < 0) || ((nnodes % *p) != 0)) {
317 if (freeprocs == 1) {
323 if (freeprocs == 1) {
324 for (i = 0; i < ndims; ++i, ++dims) {
332 /* Factor the number of free processes */
333 if (MPI_SUCCESS != (err = getfactors(freeprocs, &nfactors, &factors))) {
337 /* Assign free processes to free dimensions */
338 if (MPI_SUCCESS != (err = assignnodes(freedims, nfactors, factors, &procs))) {
342 /* Return assignment results */
344 for (i = 0; i < ndims; ++i, ++dims) {
350 free((char *) factors);
351 free((char *) procs);
360 * Function: - assign processes to dimensions
361 * - get "best-balanced" grid
362 * - greedy bin-packing algorithm used
363 * - sort dimensions in decreasing order
364 * - dimensions array dynamically allocated
365 * Accepts: - # of dimensions
366 * - # of prime factors
367 * - array of prime factors
368 * - ptr to array of dimensions (returned value)
369 * Returns: - 0 or ERROR
372 assignnodes(int ndim, int nfactor, int *pfacts, int **pdims)
385 /* Allocate and initialize the bins */
386 bins = (int *) malloc((unsigned) ndim * sizeof(int));
388 return MPI_ERR_NO_MEM;
392 for (i = 0, p = bins; i < ndim; ++i, ++p) {
396 /* Loop assigning factors from the highest to the lowest */
397 for (j = nfactor - 1; j >= 0; --j) {
399 /* Assign a factor to the smallest bin */
401 for (i = 1, p = pmin + 1; i < ndim; ++i, ++p) {
409 /* Sort dimensions in decreasing order (O(n^2) for now) */
410 for (i = 0, pmin = bins; i < ndim - 1; ++i, ++pmin) {
411 for (j = i + 1, p = pmin + 1; j < ndim; ++j, ++p) {
426 * Function: - factorize a number
429 * - array of prime factors
430 * Returns: - MPI_SUCCESS or ERROR
433 getfactors(int num, int *nfactors, int **factors) {
444 /* Allocate the array of prime factors which cannot exceed log_2(num) entries */
445 sqrtnum = ceil(sqrt(num));
446 size = ceil(log(num) / log(2));
447 *factors = (int *) malloc((unsigned) size * sizeof(int));
450 /* determine all occurences of factor 2 */
451 while((num % 2) == 0) {
455 /* determine all occurences of uneven prime numbers up to sqrt(num) */
456 for(d = 3; (num > 1) && (d < sqrtnum); d += 2) {
457 while((num % d) == 0) {
462 /* as we looped only up to sqrt(num) one factor > sqrt(num) may be left over */
464 (*factors)[i++] = num;