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 if(comm_cart != nullptr)
47 *comm_cart = MPI_COMM_NULL;
50 oldGroup = comm_old->group();
51 newGroup = new Group(newSize);
52 for (int i = 0 ; i < newSize ; i++) {
53 newGroup->set_mapping(oldGroup->actor(i), i);
58 // FIXME : code duplication... See coords
60 for (int i=0; i<ndims; i++) {
62 periodic_[i] = periods[i];
63 nranks = nranks / dims[i];
64 /* FIXME: nranks could be zero (?) */
65 position_[i] = rank / nranks;
68 if(comm_cart != nullptr)
69 *comm_cart = new Comm(newGroup, this);
71 if(comm_cart != nullptr){
73 *comm_cart = new Comm(new Group(MPI_COMM_SELF->group()), this);
75 *comm_cart = MPI_COMM_NULL;
79 if(comm_cart != nullptr)
83 Topo_Cart* Topo_Cart::sub(const int remain_dims[], MPI_Comm *newcomm) {
84 int oldNDims = ndims_;
85 int *newDims = nullptr;
86 int *newPeriodic = nullptr;
88 if (remain_dims == nullptr && oldNDims != 0) {
92 for (int i = 0 ; i < oldNDims ; i++) {
98 newDims = new int[newNDims];
99 newPeriodic = new int[newNDims];
101 // that should not segfault
103 for (int i = 0 ; j < newNDims ; i++) {
105 newDims[j] =dims_[i];
106 newPeriodic[j] =periodic_[i];
112 //split into several communicators
114 for (int i = 0; i < oldNDims; i++) {
115 if (not remain_dims[i]) {
116 color = (color * dims_[i] + position_[i]);
121 res = new Topo_Cart(getComm(), newNDims, newDims, newPeriodic, 0, newcomm);
123 *newcomm = getComm()->split(color, getComm()->rank());
124 res = new Topo_Cart(getComm(), newNDims, newDims, newPeriodic, 0, nullptr);
125 res->setComm(*newcomm);
128 delete[] newPeriodic;
132 int Topo_Cart::coords(int rank, int /*maxdims*/, int coords[])
134 int nnodes = nnodes_;
135 for (int i = 0; i< ndims_; i++ ) {
136 nnodes = nnodes /dims_[i];
137 coords[i] = rank / nnodes;
138 rank = rank % nnodes;
143 int Topo_Cart::get(int maxdims, int* dims, int* periods, int* coords) {
144 int ndims=ndims_ < maxdims ?ndims_ : maxdims;
145 for(int i = 0 ; i < ndims ; i++) {
147 periods[i] =periodic_[i];
148 coords[i] =position_[i];
153 int Topo_Cart::rank(const int* coords, int* rank) {
158 for (int i=ndims-1; i >=0; i-- ) {
159 int coord = coords[i];
161 /* The user can give us whatever coordinates he wants. If one of them is out of range, either this dimension is
162 * periodic, and we consider the equivalent coordinate inside the bounds, or it's not and then it's an error
164 if (coord >=dims_[i]) {
166 coord = coord %dims_[i];
168 // Should I do that ?
172 } else if (coord < 0) {
174 coord = coord %dims_[i];
176 coord =dims_[i] + coord;
183 *rank += multiplier * coord;
184 multiplier *=dims_[i];
189 int Topo_Cart::shift(int direction, int disp, int* rank_source, int* rank_dest)
194 if (ndims_ < direction) {
198 int* position = new int[ndims_];
199 this->coords(getComm()->rank(), ndims_, position);
200 position[direction] += disp;
202 if(position[direction] < 0 ||
203 position[direction] >=dims_[direction]) {
204 if(periodic_[direction]) {
205 position[direction] %=dims_[direction];
206 this->rank(position, rank_dest);
208 *rank_dest = MPI_PROC_NULL;
211 this->rank(position, rank_dest);
214 position[direction] = position_[direction] - disp;
215 if(position[direction] < 0 || position[direction] >=dims_[direction]) {
216 if(periodic_[direction]) {
217 position[direction] %=dims_[direction];
218 this->rank(position, rank_source);
220 *rank_source = MPI_PROC_NULL;
223 this->rank(position, rank_source);
229 int Topo_Cart::dim_get(int *ndims) {
234 // Everything below has been taken from ompi, but could be easily rewritten (and partially was to follow sonar rules).
237 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
238 * University Research and Technology
239 * Corporation. All rights reserved.
240 * Copyright (c) 2004-2005 The University of Tennessee and The University
241 * of Tennessee Research Foundation. All rights
243 * Copyright (c) 2004-2014 High Performance Computing Center Stuttgart,
244 * University of Stuttgart. All rights reserved.
245 * Copyright (c) 2004-2005 The Regents of the University of California.
246 * All rights reserved.
247 * Copyright (c) 2012 Los Alamos National Security, LLC. All rights
249 * Copyright (c) 2014 Intel, Inc. All rights reserved
252 * Additional copyrights may follow
258 * This is a utility function, no need to have anything in the lower layer for this at all
260 int Topo_Cart::Dims_create(int nnodes, int ndims, int dims[])
262 /* Get # of free-to-be-assigned processes and # of free dimensions */
263 int freeprocs = nnodes;
266 for (int i = 0; i < ndims; ++i) {
269 } else if ((*p < 0) || ((nnodes % *p) != 0)) {
279 if (freeprocs == 1) {
285 if (freeprocs == 1) {
286 for (int i = 0; i < ndims; ++i) {
295 /* Factor the number of free processes */
298 int err = getfactors(freeprocs, &nfactors, &factors);
299 if (MPI_SUCCESS != err)
302 /* Assign free processes to free dimensions */
304 err = assignnodes(freedims, nfactors, factors, &procs);
305 if (MPI_SUCCESS != err)
308 /* Return assignment results */
310 for (int i = 0; i < ndims; ++i) {
330 * Function: - assign processes to dimensions
331 * - get "best-balanced" grid
332 * - greedy bin-packing algorithm used
333 * - sort dimensions in decreasing order
334 * - dimensions array dynamically allocated
335 * Accepts: - # of dimensions
336 * - # of prime factors
337 * - array of prime factors
338 * - ptr to array of dimensions (returned value)
339 * Returns: - 0 or ERROR
341 static int assignnodes(int ndim, int nfactor, int *pfacts, int **pdims)
349 /* Allocate and initialize the bins */
350 int *bins = new int[ndim];
355 for (int i = 0 ; i < ndim; ++i) {
359 /* Loop assigning factors from the highest to the lowest */
360 for (int j = nfactor - 1; j >= 0; --j) {
362 /* Assign a factor to the smallest bin */
365 for (int i = 1; i < ndim; ++i) {
374 /* Sort dimensions in decreasing order (O(n^2) for now) */
376 for (int i = 0; i < ndim - 1; ++i) {
378 for (int j = i + 1; j < ndim; ++j) {
395 * Function: - factorize a number
398 * - array of prime factors
399 * Returns: - MPI_SUCCESS or ERROR
401 static int getfactors(int num, int *nfactors, int **factors) {
404 (*factors) = nullptr;
407 /* Allocate the array of prime factors which cannot exceed log_2(num) entries */
408 int sqrtnum = ceil(sqrt(double(num)));
409 int size = ceil(log(double(num)) / log(2.0));
410 *factors = new int[size];
413 /* determine all occurrences of factor 2 */
414 while((num % 2) == 0) {
418 /* determine all occurrences of uneven prime numbers up to sqrt(num) */
419 for(int d = 3; (num > 1) && (d < sqrtnum); d += 2) {
420 while((num % d) == 0) {
425 /* as we looped only up to sqrt(num) one factor > sqrt(num) may be left over */
427 (*factors)[i++] = num;