1 /* Copyright (c) 2014-2019. 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 /*******************************************************************************
23 * Cartesian topologies
24 ******************************************************************************/
26 Topo_Cart::Topo_Cart(int ndims) : ndims_(ndims), dims_(ndims), periodic_(ndims), position_(ndims)
30 /* reorder is ignored, don't know what would be the consequences of a dumb reordering but neither do I see the point of
32 Topo_Cart::Topo_Cart(MPI_Comm comm_old, int ndims, const int dims[], const int periods[], int /*reorder*/, MPI_Comm* comm_cart)
38 int rank = comm_old->rank();
42 for (int i = 0 ; i < ndims ; i++) {
46 *comm_cart = MPI_COMM_NULL;
49 oldGroup = comm_old->group();
50 newGroup = new Group(newSize);
51 for (int i = 0 ; i < newSize ; i++) {
52 newGroup->set_mapping(oldGroup->actor(i), i);
57 // FIXME : code duplication... See coords
59 for (int i=0; i<ndims; i++) {
61 periodic_[i] = periods[i];
62 nranks = nranks / dims[i];
63 /* FIXME: nranks could be zero (?) */
64 position_[i] = rank / nranks;
68 *comm_cart = new Comm(newGroup, this);
71 *comm_cart = new Comm(new Group(MPI_COMM_SELF->group()), this);
73 *comm_cart = MPI_COMM_NULL;
79 Topo_Cart* Topo_Cart::sub(const int remain_dims[], MPI_Comm *newcomm) {
80 int oldNDims = ndims_;
81 int *newDims = nullptr;
82 int *newPeriodic = nullptr;
84 if (remain_dims == nullptr && oldNDims != 0) {
88 for (int i = 0 ; i < oldNDims ; i++) {
94 newDims = new int[newNDims];
95 newPeriodic = new int[newNDims];
97 // that should not segfault
99 for (int i = 0 ; j < newNDims ; i++) {
101 newDims[j] =dims_[i];
102 newPeriodic[j] =periodic_[i];
107 Topo_Cart* res = new Topo_Cart(getComm(), newNDims, newDims, newPeriodic, 0, newcomm);
109 delete[] newPeriodic;
113 int Topo_Cart::coords(int rank, int /*maxdims*/, int coords[])
115 int nnodes = nnodes_;
116 for (int i = 0; i< ndims_; i++ ) {
117 nnodes = nnodes /dims_[i];
118 coords[i] = rank / nnodes;
119 rank = rank % nnodes;
124 int Topo_Cart::get(int maxdims, int* dims, int* periods, int* coords) {
125 int ndims=ndims_ < maxdims ?ndims_ : maxdims;
126 for(int i = 0 ; i < ndims ; i++) {
128 periods[i] =periodic_[i];
129 coords[i] =position_[i];
134 int Topo_Cart::rank(const int* coords, int* rank) {
139 for (int i=ndims-1; i >=0; i-- ) {
140 int coord = coords[i];
142 /* The user can give us whatever coordinates he wants. If one of them is out of range, either this dimension is
143 * periodic, and we consider the equivalent coordinate inside the bounds, or it's not and then it's an error
145 if (coord >=dims_[i]) {
147 coord = coord %dims_[i];
149 // Should I do that ?
153 } else if (coord < 0) {
155 coord = coord %dims_[i];
157 coord =dims_[i] + coord;
164 *rank += multiplier * coord;
165 multiplier *=dims_[i];
170 int Topo_Cart::shift(int direction, int disp, int *rank_source, int *rank_dest) {
172 int position[ndims_];
177 if (ndims_ < direction) {
181 this->coords(getComm()->rank(), ndims_, position);
182 position[direction] += disp;
184 if(position[direction] < 0 ||
185 position[direction] >=dims_[direction]) {
186 if(periodic_[direction]) {
187 position[direction] %=dims_[direction];
188 this->rank(position, rank_dest);
190 *rank_dest = MPI_PROC_NULL;
193 this->rank(position, rank_dest);
196 position[direction] = position_[direction] - disp;
197 if(position[direction] < 0 || position[direction] >=dims_[direction]) {
198 if(periodic_[direction]) {
199 position[direction] %=dims_[direction];
200 this->rank(position, rank_source);
202 *rank_source = MPI_PROC_NULL;
205 this->rank(position, rank_source);
211 int Topo_Cart::dim_get(int *ndims) {
216 // Everything below has been taken from ompi, but could be easily rewritten (and partially was to follow sonar rules).
219 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
220 * University Research and Technology
221 * Corporation. All rights reserved.
222 * Copyright (c) 2004-2005 The University of Tennessee and The University
223 * of Tennessee Research Foundation. All rights
225 * Copyright (c) 2004-2014 High Performance Computing Center Stuttgart,
226 * University of Stuttgart. All rights reserved.
227 * Copyright (c) 2004-2005 The Regents of the University of California.
228 * All rights reserved.
229 * Copyright (c) 2012 Los Alamos National Security, LLC. All rights
231 * Copyright (c) 2014 Intel, Inc. All rights reserved
234 * Additional copyrights may follow
240 * This is a utility function, no need to have anything in the lower layer for this at all
242 int Topo_Cart::Dims_create(int nnodes, int ndims, int dims[])
244 /* Get # of free-to-be-assigned processes and # of free dimensions */
245 int freeprocs = nnodes;
248 for (int i = 0; i < ndims; ++i) {
251 } else if ((*p < 0) || ((nnodes % *p) != 0)) {
261 if (freeprocs == 1) {
267 if (freeprocs == 1) {
268 for (int i = 0; i < ndims; ++i) {
277 /* Factor the number of free processes */
280 int err = getfactors(freeprocs, &nfactors, &factors);
281 if (MPI_SUCCESS != err)
284 /* Assign free processes to free dimensions */
286 err = assignnodes(freedims, nfactors, factors, &procs);
287 if (MPI_SUCCESS != err)
290 /* Return assignment results */
292 for (int i = 0; i < ndims; ++i) {
312 * Function: - assign processes to dimensions
313 * - get "best-balanced" grid
314 * - greedy bin-packing algorithm used
315 * - sort dimensions in decreasing order
316 * - dimensions array dynamically allocated
317 * Accepts: - # of dimensions
318 * - # of prime factors
319 * - array of prime factors
320 * - ptr to array of dimensions (returned value)
321 * Returns: - 0 or ERROR
323 static int assignnodes(int ndim, int nfactor, int *pfacts, int **pdims)
331 /* Allocate and initialize the bins */
332 int *bins = new int[ndim];
337 for (int i = 0 ; i < ndim; ++i) {
341 /* Loop assigning factors from the highest to the lowest */
342 for (int j = nfactor - 1; j >= 0; --j) {
344 /* Assign a factor to the smallest bin */
347 for (int i = 1; i < ndim; ++i) {
356 /* Sort dimensions in decreasing order (O(n^2) for now) */
358 for (int i = 0; i < ndim - 1; ++i) {
360 for (int j = i + 1; j < ndim; ++j) {
377 * Function: - factorize a number
380 * - array of prime factors
381 * Returns: - MPI_SUCCESS or ERROR
383 static int getfactors(int num, int *nfactors, int **factors) {
386 (*factors) = nullptr;
389 /* Allocate the array of prime factors which cannot exceed log_2(num) entries */
390 int sqrtnum = ceil(sqrt(double(num)));
391 int size = ceil(log(double(num)) / log(2.0));
392 *factors = new int[size];
395 /* determine all occurrences of factor 2 */
396 while((num % 2) == 0) {
400 /* determine all occurrences of uneven prime numbers up to sqrt(num) */
401 for(int d = 3; (num > 1) && (d < sqrtnum); d += 2) {
402 while((num % d) == 0) {
407 /* as we looped only up to sqrt(num) one factor > sqrt(num) may be left over */
409 (*factors)[i++] = num;