1 /* Copyright (c) 2014-2022. 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"
15 /* static functions */
16 static int assignnodes(int ndim, const std::vector<int>& factors, std::vector<int>& dims);
17 static int getfactors(int num, std::vector<int>& factors);
22 void Topo::setComm(MPI_Comm comm)
24 xbt_assert(not comm_);
28 /*******************************************************************************
29 * Cartesian topologies
30 ******************************************************************************/
32 Topo_Cart::Topo_Cart(int ndims) : ndims_(ndims), dims_(ndims), periodic_(ndims), position_(ndims)
36 /* reorder is ignored, don't know what would be the consequences of a dumb reordering but neither do I see the point of
38 Topo_Cart::Topo_Cart(MPI_Comm comm_old, int ndims, const int dims[], const int periods[], int /*reorder*/, MPI_Comm* comm_cart)
41 int rank = comm_old->rank();
45 for (int i = 0 ; i < ndims ; i++) {
49 if(comm_cart != nullptr)
50 *comm_cart = MPI_COMM_NULL;
56 // FIXME : code duplication... See coords
58 for (int i=0; i<ndims; i++) {
60 periodic_[i] = periods[i];
61 nranks = nranks / dims[i];
62 /* FIXME: nranks could be zero (?) */
63 position_[i] = rank / nranks;
67 if(comm_cart != nullptr){
68 const Group* oldGroup = comm_old->group();
69 auto* newGroup = new Group(newSize);
70 for (int i = 0 ; i < newSize ; i++) {
71 newGroup->set_mapping(oldGroup->actor(i), i);
73 *comm_cart = new Comm(newGroup, std::shared_ptr<Topo>(this));
76 if(comm_cart != nullptr){
78 auto* group = new Group(MPI_COMM_SELF->group());
79 *comm_cart = new Comm(group, std::shared_ptr<Topo>(this));
81 *comm_cart = MPI_COMM_NULL;
85 if(comm_cart != nullptr){
90 Topo_Cart* Topo_Cart::sub(const int remain_dims[], MPI_Comm *newcomm) {
91 int oldNDims = ndims_;
92 std::vector<int> newDims;
93 std::vector<int> newPeriodic;
95 if (remain_dims == nullptr && oldNDims != 0) {
99 for (int i = 0 ; i < oldNDims ; i++) {
105 newDims.resize(newNDims);
106 newPeriodic.resize(newNDims);
108 // that should not segfault
110 for (int i = 0; i < oldNDims; i++) {
112 newDims[j] =dims_[i];
113 newPeriodic[j] =periodic_[i];
119 //split into several communicators
121 for (int i = 0; i < oldNDims; i++) {
122 if (not remain_dims[i]) {
123 color = (color * dims_[i] + position_[i]);
128 res = new Topo_Cart(getComm(), newNDims, newDims.data(), newPeriodic.data(), 0, newcomm);
130 *newcomm = getComm()->split(color, getComm()->rank());
131 auto topo = std::make_shared<Topo_Cart>(getComm(), newNDims, newDims.data(), newPeriodic.data(), 0, nullptr);
133 res->setComm(*newcomm);
134 (*newcomm)->set_topo(topo);
139 int Topo_Cart::coords(int rank, int /*maxdims*/, int coords[])
141 int nnodes = nnodes_;
142 for (int i = 0; i< ndims_; i++ ) {
143 nnodes = nnodes /dims_[i];
144 coords[i] = rank / nnodes;
145 rank = rank % nnodes;
150 int Topo_Cart::get(int maxdims, int* dims, int* periods, int* coords) {
151 int ndims=ndims_ < maxdims ?ndims_ : maxdims;
152 for(int i = 0 ; i < ndims ; i++) {
154 periods[i] =periodic_[i];
155 coords[i] =position_[i];
160 int Topo_Cart::rank(const int* coords, int* rank) {
165 for (int i=ndims-1; i >=0; i-- ) {
166 int coord = coords[i];
168 /* The user can give us whatever coordinates he wants. If one of them is out of range, either this dimension is
169 * periodic, and we consider the equivalent coordinate inside the bounds, or it's not and then it's an error
171 if (coord >=dims_[i]) {
173 coord = coord %dims_[i];
175 // Should I do that ?
179 } else if (coord < 0) {
181 coord = coord %dims_[i];
183 coord =dims_[i] + coord;
190 *rank += multiplier * coord;
191 multiplier *=dims_[i];
196 int Topo_Cart::shift(int direction, int disp, int* rank_source, int* rank_dest)
201 if (ndims_ < direction) {
205 std::vector<int> position(ndims_);
206 this->coords(getComm()->rank(), ndims_, position.data());
207 position[direction] += disp;
209 if(position[direction] < 0 ||
210 position[direction] >=dims_[direction]) {
211 if(periodic_[direction]) {
212 position[direction] %=dims_[direction];
213 this->rank(position.data(), rank_dest);
215 *rank_dest = MPI_PROC_NULL;
218 this->rank(position.data(), rank_dest);
221 position[direction] = position_[direction] - disp;
222 if(position[direction] < 0 || position[direction] >=dims_[direction]) {
223 if(periodic_[direction]) {
224 position[direction] %=dims_[direction];
225 this->rank(position.data(), rank_source);
227 *rank_source = MPI_PROC_NULL;
230 this->rank(position.data(), rank_source);
235 int Topo_Cart::dim_get(int* ndims) const
241 // Everything below has been taken from ompi, but could be easily rewritten (and partially was to follow sonar rules).
244 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
245 * University Research and Technology
246 * Corporation. All rights reserved.
247 * Copyright (c) 2004-2005 The University of Tennessee and The University
248 * of Tennessee Research Foundation. All rights
250 * Copyright (c) 2004-2014 High Performance Computing Center Stuttgart,
251 * University of Stuttgart. All rights reserved.
252 * Copyright (c) 2004-2005 The Regents of the University of California.
253 * All rights reserved.
254 * Copyright (c) 2012 Los Alamos National Security, LLC. All rights
256 * Copyright (c) 2014 Intel, Inc. All rights reserved
259 * Additional copyrights may follow
265 * This is a utility function, no need to have anything in the lower layer for this at all
267 int Topo_Cart::Dims_create(int nnodes, int ndims, int dims[])
269 /* Get # of free-to-be-assigned processes and # of free dimensions */
270 int freeprocs = nnodes;
272 for (int i = 0; i < ndims; ++i) {
275 } else if ((dims[i] < 0) || ((nnodes % dims[i]) != 0)) {
279 freeprocs /= dims[i];
284 if (freeprocs == 1) {
290 if (freeprocs == 1) {
291 for (int i = 0; i < ndims; ++i) {
299 /* Factor the number of free processes */
300 std::vector<int> factors;
301 int err = getfactors(freeprocs, factors);
302 if (MPI_SUCCESS != err)
305 /* Assign free processes to free dimensions */
306 std::vector<int> procs;
307 err = assignnodes(freedims, factors, procs);
308 if (MPI_SUCCESS != err)
311 /* Return assignment results */
312 auto p = procs.begin();
313 for (int i = 0; i < ndims; ++i) {
329 * Function: - assign processes to dimensions
330 * - get "best-balanced" grid
331 * - greedy bin-packing algorithm used
332 * - sort dimensions in decreasing order
333 * - dimensions array dynamically allocated
334 * Accepts: - # of dimensions
335 * - std::vector of prime factors
336 * - reference to std::vector of dimensions (returned value)
337 * Returns: - 0 or ERROR
339 static int assignnodes(int ndim, const std::vector<int>& factors, std::vector<int>& dims)
345 /* Allocate and initialize the bins */
347 dims.resize(ndim, 1);
349 /* Loop assigning factors from the highest to the lowest */
350 for (auto pfact = factors.crbegin(); pfact != factors.crend(); ++pfact) {
351 /* Assign a factor to the smallest bin */
352 auto pmin = std::min_element(dims.begin(), dims.end());
356 /* Sort dimensions in decreasing order */
357 std::sort(dims.begin(), dims.end(), std::greater<>());
365 * Function: - factorize a number
367 * - reference to std::vector of prime factors
368 * Returns: - MPI_SUCCESS or ERROR
370 static int getfactors(int num, std::vector<int>& factors)
377 /* determine all occurrences of factor 2 */
378 while((num % 2) == 0) {
380 factors.push_back(2);
382 /* determine all occurrences of uneven prime numbers up to sqrt(num) */
384 while ((num > 1) && (d * d < num)) {
385 while((num % d) == 0) {
387 factors.push_back(d);
391 /* as we looped only up to sqrt(num) one factor > sqrt(num) may be left over */
393 factors.push_back(num);