1 /* Copyright (c) 2014-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "smpi_comm.hpp"
9 #include "smpi_topo.hpp"
10 #include "xbt/sysdep.h"
14 /* static functions */
15 static int assignnodes(int ndim, int nfactor, int *pfacts,int **pdims);
16 static int getfactors(int num, int *nfators, int **factors);
22 Topo_Graph::~Topo_Graph()
28 Topo_Dist_Graph::~Topo_Dist_Graph()
33 delete[] out_weights_;
36 /*******************************************************************************
37 * Cartesian topologies
38 ******************************************************************************/
39 Topo_Cart::~Topo_Cart()
46 Topo_Cart::Topo_Cart(int ndims) : ndims_(ndims)
48 dims_ = new int[ndims];
49 periodic_ = new int[ndims];
50 position_ = new int[ndims];
53 /* reorder is ignored, don't know what would be the consequences of a dumb reordering but neither do I see the point of
55 Topo_Cart::Topo_Cart(MPI_Comm comm_old, int ndims, int dims[], int periods[], int reorder, MPI_Comm *comm_cart) : Topo_Cart(ndims) {
59 int rank = comm_old->rank();
63 for (int i = 0 ; i < ndims ; i++) {
67 *comm_cart = MPI_COMM_NULL;
70 oldGroup = comm_old->group();
71 newGroup = new Group(newSize);
72 for (int i = 0 ; i < newSize ; i++) {
73 newGroup->set_mapping(oldGroup->actor(i), i);
78 // FIXME : code duplication... See coords
80 for (int i=0; i<ndims; i++) {
82 periodic_[i] = periods[i];
83 nranks = nranks / dims[i];
84 /* FIXME: nranks could be zero (?) */
85 position_[i] = rank / nranks;
89 *comm_cart = new Comm(newGroup, this);
92 *comm_cart = new Comm(new Group(MPI_COMM_SELF->group()), this);
94 *comm_cart = MPI_COMM_NULL;
100 Topo_Cart* Topo_Cart::sub(const int remain_dims[], MPI_Comm *newcomm) {
101 int oldNDims = ndims_;
102 int *newDims = nullptr;
103 int *newPeriodic = nullptr;
105 if (remain_dims == nullptr && oldNDims != 0) {
109 for (int i = 0 ; i < oldNDims ; i++) {
115 newDims = new int[newNDims];
116 newPeriodic = new int[newNDims];
118 // that should not segfault
120 for (int i = 0 ; j < newNDims ; i++) {
122 newDims[j] =dims_[i];
123 newPeriodic[j] =periodic_[i];
128 Topo_Cart* res = new Topo_Cart(getComm(), newNDims, newDims, newPeriodic, 0, newcomm);
130 delete[] newPeriodic;
134 int Topo_Cart::coords(int rank, int maxdims, int coords[]) {
135 int nnodes = nnodes_;
136 for (int i = 0; i< ndims_; i++ ) {
137 nnodes = nnodes /dims_[i];
138 coords[i] = rank / nnodes;
139 rank = rank % nnodes;
144 int Topo_Cart::get(int maxdims, int* dims, int* periods, int* coords) {
145 int ndims=ndims_ < maxdims ?ndims_ : maxdims;
146 for(int i = 0 ; i < ndims ; i++) {
148 periods[i] =periodic_[i];
149 coords[i] =position_[i];
154 int Topo_Cart::rank(int* coords, int* rank) {
159 for (int i=ndims-1; i >=0; i-- ) {
160 int coord = coords[i];
162 /* The user can give us whatever coordinates he wants. If one of them is out of range, either this dimension is
163 * periodic, and we consider the equivalent coordinate inside the bounds, or it's not and then it's an error
165 if (coord >=dims_[i]) {
167 coord = coord %dims_[i];
169 // Should I do that ?
173 } else if (coord < 0) {
175 coord = coord %dims_[i];
177 coord =dims_[i] + coord;
184 *rank += multiplier * coord;
185 multiplier *=dims_[i];
190 int Topo_Cart::shift(int direction, int disp, int *rank_source, int *rank_dest) {
192 int position[ndims_];
197 if (ndims_ < direction) {
201 this->coords(getComm()->rank(), ndims_, position);
202 position[direction] += disp;
204 if(position[direction] < 0 ||
205 position[direction] >=dims_[direction]) {
206 if(periodic_[direction]) {
207 position[direction] %=dims_[direction];
208 this->rank(position, rank_dest);
210 *rank_dest = MPI_PROC_NULL;
213 this->rank(position, rank_dest);
216 position[direction] = position_[direction] - disp;
217 if(position[direction] < 0 || position[direction] >=dims_[direction]) {
218 if(periodic_[direction]) {
219 position[direction] %=dims_[direction];
220 this->rank(position, rank_source);
222 *rank_source = MPI_PROC_NULL;
225 this->rank(position, rank_source);
231 int Topo_Cart::dim_get(int *ndims) {
236 // Everything below has been taken from ompi, but could be easily rewritten (and partially was to follow sonar rules).
239 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
240 * University Research and Technology
241 * Corporation. All rights reserved.
242 * Copyright (c) 2004-2005 The University of Tennessee and The University
243 * of Tennessee Research Foundation. All rights
245 * Copyright (c) 2004-2014 High Performance Computing Center Stuttgart,
246 * University of Stuttgart. All rights reserved.
247 * Copyright (c) 2004-2005 The Regents of the University of California.
248 * All rights reserved.
249 * Copyright (c) 2012 Los Alamos National Security, LLC. All rights
251 * Copyright (c) 2014 Intel, Inc. All rights reserved
254 * Additional copyrights may follow
260 * This is a utility function, no need to have anything in the lower layer for this at all
262 int Topo_Cart::Dims_create(int nnodes, int ndims, int dims[])
264 /* Get # of free-to-be-assigned processes and # of free dimensions */
265 int freeprocs = nnodes;
268 for (int i = 0; i < ndims; ++i) {
271 } else if ((*p < 0) || ((nnodes % *p) != 0)) {
281 if (freeprocs == 1) {
287 if (freeprocs == 1) {
288 for (int i = 0; i < ndims; ++i) {
297 /* Factor the number of free processes */
300 int err = getfactors(freeprocs, &nfactors, &factors);
301 if (MPI_SUCCESS != err)
304 /* Assign free processes to free dimensions */
306 err = assignnodes(freedims, nfactors, factors, &procs);
307 if (MPI_SUCCESS != err)
310 /* Return assignment results */
312 for (int i = 0; i < ndims; ++i) {
332 * Function: - assign processes to dimensions
333 * - get "best-balanced" grid
334 * - greedy bin-packing algorithm used
335 * - sort dimensions in decreasing order
336 * - dimensions array dynamically allocated
337 * Accepts: - # of dimensions
338 * - # of prime factors
339 * - array of prime factors
340 * - ptr to array of dimensions (returned value)
341 * Returns: - 0 or ERROR
343 static int assignnodes(int ndim, int nfactor, int *pfacts, int **pdims)
351 /* Allocate and initialize the bins */
352 int *bins = new int[ndim];
357 for (int i = 0 ; i < ndim; ++i) {
361 /* Loop assigning factors from the highest to the lowest */
362 for (int j = nfactor - 1; j >= 0; --j) {
364 /* Assign a factor to the smallest bin */
367 for (int i = 1; i < ndim; ++i) {
376 /* Sort dimensions in decreasing order (O(n^2) for now) */
378 for (int i = 0; i < ndim - 1; ++i) {
380 for (int j = i + 1; j < ndim; ++j) {
397 * Function: - factorize a number
400 * - array of prime factors
401 * Returns: - MPI_SUCCESS or ERROR
403 static int getfactors(int num, int *nfactors, int **factors) {
406 (*factors) = nullptr;
409 /* Allocate the array of prime factors which cannot exceed log_2(num) entries */
410 int sqrtnum = ceil(sqrt(num));
411 int size = ceil(log(num) / log(2));
412 *factors = new int[size];
415 /* determine all occurrences of factor 2 */
416 while((num % 2) == 0) {
420 /* determine all occurrences of uneven prime numbers up to sqrt(num) */
421 for(int d = 3; (num > 1) && (d < sqrtnum); d += 2) {
422 while((num % d) == 0) {
427 /* as we looped only up to sqrt(num) one factor > sqrt(num) may be left over */
429 (*factors)[i++] = num;