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);
56 // TODO : add somewhere : if topo != NULL, free...
57 topo = smpi_topo_create(ndims);
60 for (i = 0 ; i < ndims ; i++) {
64 *comm_cart = MPI_COMM_NULL;
67 oldGroup = smpi_comm_group(comm_old);
68 newGroup = smpi_group_new(newSize);
69 for (i = 0 ; i < newSize ; i++) {
70 smpi_group_set_mapping(newGroup, smpi_group_index(oldGroup, i), i);
73 topo->nnodes = newSize;
75 memcpy(topo->dims, dims, ndims * sizeof(*topo->dims));
76 memcpy(topo->periodic, periods, ndims * sizeof(*topo->periodic));
78 // code duplication... See smpi_mpi_cart_coords
80 for (i=0; i<ndims; i++)
82 topo->dims[i] = dims[i];
83 topo->periodic[i] = periods[i];
84 nranks = nranks / dims[i];
85 /* FIXME: nranks could be zero (?) */
86 topo->position[i] = rank / nranks;
90 *comm_cart = smpi_comm_new(newGroup, topo);
94 *comm_cart = smpi_comm_new(smpi_comm_group(MPI_COMM_SELF), topo);
97 *comm_cart = MPI_COMM_NULL;
103 int smpi_mpi_cart_sub(MPI_Comm comm, const int remain_dims[], MPI_Comm *newcomm) {
104 MPI_Topology oldTopo = smpi_comm_topo(comm);
105 int oldNDims = oldTopo->ndims;
106 int i, j = 0, newNDims, *newDims = NULL, *newPeriodic = NULL;
108 if (remain_dims == NULL && oldNDims != 0) {
112 for (i = 0 ; i < oldNDims ; i++) {
113 if (remain_dims[i]) newNDims++;
117 newDims = malloc(newNDims * sizeof(*newDims));
118 newPeriodic = malloc(newNDims * sizeof(*newPeriodic));
120 // that should not segfault
121 for (i = 0 ; j < newNDims ; i++) {
123 newDims[j] = oldTopo->dims[i];
124 newPeriodic[j] = oldTopo->periodic[i];
129 return smpi_mpi_cart_create(comm, newNDims, newDims, newPeriodic, 0, newcomm);
135 int smpi_mpi_cart_coords(MPI_Comm comm, int rank, int maxdims,
139 MPI_Topology topo = smpi_comm_topo(comm);
141 nnodes = topo->nnodes;
142 for ( i=0; i < topo->ndims; i++ ) {
143 nnodes = nnodes / topo->dims[i];
144 coords[i] = rank / nnodes;
145 rank = rank % nnodes;
150 int smpi_mpi_cart_get(MPI_Comm comm, int maxdims, int* dims, int* periods, int* coords) {
151 MPI_Topology topo = smpi_comm_topo(comm);
153 for(i = 0 ; i < maxdims ; i++) {
154 dims[i] = topo->dims[i];
155 periods[i] = topo->periodic[i];
156 coords[i] = topo->position[i];
161 int smpi_mpi_cart_rank(MPI_Comm comm, int* coords, int* rank) {
162 MPI_Topology topo = smpi_comm_topo(comm);
163 int ndims = topo->ndims;
164 int multiplier, coord,i;
170 for ( i=ndims-1; i >=0; i-- ) {
173 /* Should we check first for args correction, then process,
174 * or check while we work (as it is currently done) ? */
175 if (coord >= topo->dims[i]) {
176 if ( topo->periodic[i] ) {
177 coord = coord % topo->dims[i];
180 // Should I do that ?
185 else if (coord < 0) {
186 if(topo->periodic[i]) {
187 coord = coord % topo->dims[i];
188 if (coord) coord = topo->dims[i] + coord;
196 *rank += multiplier * coord;
197 multiplier *= topo->dims[i];
202 int smpi_mpi_cart_shift(MPI_Comm comm, int direction, int disp,
203 int *rank_source, int *rank_dest) {
204 MPI_Topology topo = smpi_comm_topo(comm);
205 int position[topo->ndims];
208 if(topo->ndims == 0) {
211 if (topo->ndims < direction) {
215 smpi_mpi_cart_coords(comm, smpi_comm_rank(comm), topo->ndims, position);
216 position[direction] += disp;
218 if(position[direction] < 0 || position[direction] >= topo->dims[direction]) {
219 if(topo->periodic[direction]) {
220 position[direction] %= topo->dims[direction];
221 smpi_mpi_cart_rank(comm, position, rank_dest);
224 *rank_dest = MPI_PROC_NULL;
228 smpi_mpi_cart_rank(comm, position, rank_dest);
231 position[direction] = topo->position[direction] - disp;
232 if(position[direction] < 0 || position[direction] >= topo->dims[direction]) {
233 if(topo->periodic[direction]) {
234 position[direction] %= topo->dims[direction];
235 smpi_mpi_cart_rank(comm, position, rank_source);
238 *rank_source = MPI_PROC_NULL;
242 smpi_mpi_cart_rank(comm, position, rank_source);
248 int smpi_mpi_cartdim_get(MPI_Comm comm, int *ndims) {
249 MPI_Topology topo = smpi_comm_topo(comm);
251 *ndims = topo->ndims;
257 // Everything below has been taken from ompi, but could be easily rewritten.
260 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
261 * University Research and Technology
262 * Corporation. All rights reserved.
263 * Copyright (c) 2004-2005 The University of Tennessee and The University
264 * of Tennessee Research Foundation. All rights
266 * Copyright (c) 2004-2014 High Performance Computing Center Stuttgart,
267 * University of Stuttgart. All rights reserved.
268 * Copyright (c) 2004-2005 The Regents of the University of California.
269 * All rights reserved.
270 * Copyright (c) 2012 Los Alamos National Security, LLC. All rights
272 * Copyright (c) 2014 Intel, Inc. All rights reserved
275 * Additional copyrights may follow
281 /* static functions */
282 static int assignnodes(int ndim, int nfactor, int *pfacts,int **pdims);
283 static int getfactors(int num, int *nfators, int **factors);
286 * This is a utility function, no need to have anything in the lower
287 * layer for this at all
289 int smpi_mpi_dims_create(int nnodes, int ndims, int dims[])
300 /* Get # of free-to-be-assigned processes and # of free dimensions */
303 for (i = 0, p = dims; i < ndims; ++i,++p) {
306 } else if ((*p < 0) || ((nnodes % *p) != 0)) {
315 if (freeprocs == 1) {
321 if (freeprocs == 1) {
322 for (i = 0; i < ndims; ++i, ++dims) {
330 /* Factor the number of free processes */
331 if (MPI_SUCCESS != (err = getfactors(freeprocs, &nfactors, &factors))) {
335 /* Assign free processes to free dimensions */
336 if (MPI_SUCCESS != (err = assignnodes(freedims, nfactors, factors, &procs))) {
340 /* Return assignment results */
342 for (i = 0; i < ndims; ++i, ++dims) {
348 free((char *) factors);
349 free((char *) procs);
358 * Function: - assign processes to dimensions
359 * - get "best-balanced" grid
360 * - greedy bin-packing algorithm used
361 * - sort dimensions in decreasing order
362 * - dimensions array dynamically allocated
363 * Accepts: - # of dimensions
364 * - # of prime factors
365 * - array of prime factors
366 * - ptr to array of dimensions (returned value)
367 * Returns: - 0 or ERROR
370 assignnodes(int ndim, int nfactor, int *pfacts, int **pdims)
383 /* Allocate and initialize the bins */
384 bins = (int *) malloc((unsigned) ndim * sizeof(int));
386 return MPI_ERR_NO_MEM;
390 for (i = 0, p = bins; i < ndim; ++i, ++p) {
394 /* Loop assigning factors from the highest to the lowest */
395 for (j = nfactor - 1; j >= 0; --j) {
397 /* Assign a factor to the smallest bin */
399 for (i = 1, p = pmin + 1; i < ndim; ++i, ++p) {
407 /* Sort dimensions in decreasing order (O(n^2) for now) */
408 for (i = 0, pmin = bins; i < ndim - 1; ++i, ++pmin) {
409 for (j = i + 1, p = pmin + 1; j < ndim; ++j, ++p) {
424 * Function: - factorize a number
427 * - array of prime factors
428 * Returns: - MPI_SUCCESS or ERROR
431 getfactors(int num, int *nfactors, int **factors) {
442 /* Allocate the array of prime factors which cannot exceed log_2(num) entries */
443 sqrtnum = ceil(sqrt(num));
444 size = ceil(log(num) / log(2));
445 *factors = (int *) malloc((unsigned) size * sizeof(int));
448 /* determine all occurences of factor 2 */
449 while((num % 2) == 0) {
453 /* determine all occurences of uneven prime numbers up to sqrt(num) */
455 for(d = 3; (num > 1) && (d < sqrtnum); d += 2) {
456 while((num % d) == 0) {
461 /* as we looped only up to sqrt(num) one factor > sqrt(num) may be left over */
463 (*factors)[i++] = num;