1 /* Copyright (c) 2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "xbt/sysdep.h"
13 typedef struct s_smpi_mpi_cart_topology {
19 } s_smpi_mpi_cart_topology_t;
21 typedef struct s_smpi_mpi_graph_topology {
26 } s_smpi_mpi_graph_topology_t;
28 typedef struct s_smpi_dist_graph_topology {
36 } s_smpi_mpi_dist_graph_topology_t;
38 typedef struct s_smpi_mpi_topology {
41 MPIR_Graph_Topology graph;
42 MPIR_Cart_Topology cart;
43 MPIR_Dist_Graph_Topology dist_graph;
45 } s_smpi_mpi_topology_t;
47 void smpi_topo_destroy(MPI_Topology topo) {
54 // smpi_graph_topo_destroy(topo->topo.graph);
57 smpi_cart_topo_destroy(topo->topo.cart);
61 // smpi_dist_graph_topo_destroy(topo->topo.dist_graph);
69 MPI_Topology smpi_topo_create(MPIR_Topo_type kind) {
70 MPI_Topology newTopo = static_cast<MPI_Topology>(xbt_malloc(sizeof(*newTopo)));
72 // Allocate and initialize the right topo should be done by the caller
76 /*******************************************************************************
77 * Cartesian topologies
78 ******************************************************************************/
79 void smpi_cart_topo_destroy(MPIR_Cart_Topology cart) {
94 MPI_Topology smpi_cart_topo_create(int ndims) {
95 MPI_Topology newTopo = smpi_topo_create(MPI_CART);
96 MPIR_Cart_Topology newCart = static_cast<MPIR_Cart_Topology>(xbt_malloc(sizeof(*newCart)));
98 newCart->ndims = ndims;
99 newCart->dims = xbt_new(int, ndims);
100 newCart->periodic = xbt_new(int, ndims);
101 newCart->position = xbt_new(int, ndims);
102 newTopo->topo.cart = newCart;
106 /* reorder is ignored, don't know what would be the consequences of a dumb
107 * reordering but neither do I see the point of reordering*/
108 int smpi_mpi_cart_create(MPI_Comm comm_old, int ndims, int dims[], int periods[], int reorder, MPI_Comm *comm_cart) {
109 int retval = MPI_SUCCESS;
111 MPI_Topology newCart;
112 MPI_Group newGroup, oldGroup;
113 int rank, nranks, newSize;
115 rank = smpi_comm_rank(comm_old);
119 for (i = 0 ; i < ndims ; i++) {
122 if(rank >= newSize) {
123 *comm_cart = MPI_COMM_NULL;
126 newCart = smpi_cart_topo_create(ndims);
127 oldGroup = smpi_comm_group(comm_old);
128 newGroup = smpi_group_new(newSize);
129 for (i = 0 ; i < newSize ; i++) {
130 smpi_group_set_mapping(newGroup, smpi_group_index(oldGroup, i), i);
133 newCart->topo.cart->nnodes = newSize;
135 /* memcpy(newCart->topo.cart->dims, dims, ndims * sizeof(*newCart->topo.cart->dims)); */
136 /* memcpy(newCart->topo.cart->periodic, periods, ndims * sizeof(*newCart->topo.cart->periodic)); */
138 // FIXME : code duplication... See smpi_mpi_cart_coords
140 for (i=0; i<ndims; i++) {
141 newCart->topo.cart->dims[i] = dims[i];
142 newCart->topo.cart->periodic[i] = periods[i];
143 nranks = nranks / dims[i];
144 /* FIXME: nranks could be zero (?) */
145 newCart->topo.cart->position[i] = rank / nranks;
146 rank = rank % nranks;
149 *comm_cart = smpi_comm_new(newGroup, newCart);
152 newCart = smpi_cart_topo_create(ndims);
153 *comm_cart = smpi_comm_new(smpi_comm_group(MPI_COMM_SELF), newCart);
155 *comm_cart = MPI_COMM_NULL;
161 int smpi_mpi_cart_sub(MPI_Comm comm, const int remain_dims[], MPI_Comm *newcomm) {
162 MPI_Topology oldTopo = smpi_comm_topo(comm);
163 int oldNDims = oldTopo->topo.cart->ndims;
164 int i, j = 0, newNDims, *newDims = NULL, *newPeriodic = NULL;
166 if (remain_dims == NULL && oldNDims != 0) {
170 for (i = 0 ; i < oldNDims ; i++) {
171 if (remain_dims[i]) newNDims++;
175 newDims = xbt_new(int, newNDims);
176 newPeriodic = xbt_new(int, newNDims);
178 // that should not segfault
179 for (i = 0 ; j < newNDims ; i++) {
181 newDims[j] = oldTopo->topo.cart->dims[i];
182 newPeriodic[j] = oldTopo->topo.cart->periodic[i];
187 return smpi_mpi_cart_create(comm, newNDims, newDims, newPeriodic, 0, newcomm);
190 int smpi_mpi_cart_coords(MPI_Comm comm, int rank, int maxdims, int coords[]) {
193 MPI_Topology topo = smpi_comm_topo(comm);
195 nnodes = topo->topo.cart->nnodes;
196 for (int i=0; i < topo->topo.cart->ndims; i++ ) {
197 nnodes = nnodes / topo->topo.cart->dims[i];
198 coords[i] = rank / nnodes;
199 rank = rank % nnodes;
204 int smpi_mpi_cart_get(MPI_Comm comm, int maxdims, int* dims, int* periods, int* coords) {
205 MPI_Topology topo = smpi_comm_topo(comm);
207 for(int i = 0 ; i < maxdims ; i++) {
208 dims[i] = topo->topo.cart->dims[i];
209 periods[i] = topo->topo.cart->periodic[i];
210 coords[i] = topo->topo.cart->position[i];
215 int smpi_mpi_cart_rank(MPI_Comm comm, int* coords, int* rank) {
216 MPI_Topology topo = smpi_comm_topo(comm);
217 int ndims = topo->topo.cart->ndims;
218 int multiplier, coord,i;
222 for ( i=ndims-1; i >=0; i-- ) {
225 /* The user can give us whatever coordinates he wants. If one of them is out of range, either this dimension is
226 * periodic, and we consider the equivalent coordinate inside the bounds, or it's not and then it's an error
228 if (coord >= topo->topo.cart->dims[i]) {
229 if ( topo->topo.cart->periodic[i] ) {
230 coord = coord % topo->topo.cart->dims[i];
232 // Should I do that ?
236 } else if (coord < 0) {
237 if(topo->topo.cart->periodic[i]) {
238 coord = coord % topo->topo.cart->dims[i];
239 if (coord) coord = topo->topo.cart->dims[i] + coord;
246 *rank += multiplier * coord;
247 multiplier *= topo->topo.cart->dims[i];
252 int smpi_mpi_cart_shift(MPI_Comm comm, int direction, int disp, int *rank_source, int *rank_dest) {
253 MPI_Topology topo = smpi_comm_topo(comm);
254 int position[topo->topo.cart->ndims];
256 if(topo->topo.cart->ndims == 0) {
259 if (topo->topo.cart->ndims < direction) {
263 smpi_mpi_cart_coords(comm, smpi_comm_rank(comm), topo->topo.cart->ndims, position);
264 position[direction] += disp;
266 if(position[direction] < 0 ||
267 position[direction] >= topo->topo.cart->dims[direction]) {
268 if(topo->topo.cart->periodic[direction]) {
269 position[direction] %= topo->topo.cart->dims[direction];
270 smpi_mpi_cart_rank(comm, position, rank_dest);
272 *rank_dest = MPI_PROC_NULL;
275 smpi_mpi_cart_rank(comm, position, rank_dest);
278 position[direction] = topo->topo.cart->position[direction] - disp;
279 if(position[direction] < 0 || position[direction] >= topo->topo.cart->dims[direction]) {
280 if(topo->topo.cart->periodic[direction]) {
281 position[direction] %= topo->topo.cart->dims[direction];
282 smpi_mpi_cart_rank(comm, position, rank_source);
284 *rank_source = MPI_PROC_NULL;
287 smpi_mpi_cart_rank(comm, position, rank_source);
293 int smpi_mpi_cartdim_get(MPI_Comm comm, int *ndims) {
294 MPI_Topology topo = smpi_comm_topo(comm);
296 *ndims = topo->topo.cart->ndims;
300 // Everything below has been taken from ompi, but could be easily rewritten.
303 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
304 * University Research and Technology
305 * Corporation. All rights reserved.
306 * Copyright (c) 2004-2005 The University of Tennessee and The University
307 * of Tennessee Research Foundation. All rights
309 * Copyright (c) 2004-2014 High Performance Computing Center Stuttgart,
310 * University of Stuttgart. All rights reserved.
311 * Copyright (c) 2004-2005 The Regents of the University of California.
312 * All rights reserved.
313 * Copyright (c) 2012 Los Alamos National Security, LLC. All rights
315 * Copyright (c) 2014 Intel, Inc. All rights reserved
318 * Additional copyrights may follow
323 /* static functions */
324 static int assignnodes(int ndim, int nfactor, int *pfacts,int **pdims);
325 static int getfactors(int num, int *nfators, int **factors);
328 * This is a utility function, no need to have anything in the lower
329 * layer for this at all
331 int smpi_mpi_dims_create(int nnodes, int ndims, int dims[])
342 /* Get # of free-to-be-assigned processes and # of free dimensions */
345 for (i = 0, p = dims; i < ndims; ++i,++p) {
348 } else if ((*p < 0) || ((nnodes % *p) != 0)) {
357 if (freeprocs == 1) {
363 if (freeprocs == 1) {
364 for (i = 0; i < ndims; ++i, ++dims) {
372 /* Factor the number of free processes */
373 if (MPI_SUCCESS != (err = getfactors(freeprocs, &nfactors, &factors))) {
377 /* Assign free processes to free dimensions */
378 if (MPI_SUCCESS != (err = assignnodes(freedims, nfactors, factors, &procs))) {
382 /* Return assignment results */
384 for (i = 0; i < ndims; ++i, ++dims) {
390 free((char *) factors);
391 free((char *) procs);
400 * Function: - assign processes to dimensions
401 * - get "best-balanced" grid
402 * - greedy bin-packing algorithm used
403 * - sort dimensions in decreasing order
404 * - dimensions array dynamically allocated
405 * Accepts: - # of dimensions
406 * - # of prime factors
407 * - array of prime factors
408 * - ptr to array of dimensions (returned value)
409 * Returns: - 0 or ERROR
412 assignnodes(int ndim, int nfactor, int *pfacts, int **pdims)
425 /* Allocate and initialize the bins */
426 bins = (int *) malloc((unsigned) ndim * sizeof(int));
428 return MPI_ERR_NO_MEM;
432 for (i = 0, p = bins; i < ndim; ++i, ++p) {
436 /* Loop assigning factors from the highest to the lowest */
437 for (j = nfactor - 1; j >= 0; --j) {
439 /* Assign a factor to the smallest bin */
441 for (i = 1, p = pmin + 1; i < ndim; ++i, ++p) {
449 /* Sort dimensions in decreasing order (O(n^2) for now) */
450 for (i = 0, pmin = bins; i < ndim - 1; ++i, ++pmin) {
451 for (j = i + 1, p = pmin + 1; j < ndim; ++j, ++p) {
466 * Function: - factorize a number
469 * - array of prime factors
470 * Returns: - MPI_SUCCESS or ERROR
472 static int getfactors(int num, int *nfactors, int **factors) {
483 /* Allocate the array of prime factors which cannot exceed log_2(num) entries */
484 sqrtnum = ceil(sqrt(num));
485 size = ceil(log(num) / log(2));
486 *factors = (int *) malloc((unsigned) size * sizeof(int));
489 /* determine all occurences of factor 2 */
490 while((num % 2) == 0) {
494 /* determine all occurences of uneven prime numbers up to sqrt(num) */
495 for(d = 3; (num > 1) && (d < sqrtnum); d += 2) {
496 while((num % d) == 0) {
501 /* as we looped only up to sqrt(num) one factor > sqrt(num) may be left over */
503 (*factors)[i++] = num;