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 void Topo::setComm(MPI_Comm comm)
24 xbt_assert(not comm_);
30 /*******************************************************************************
31 * Cartesian topologies
32 ******************************************************************************/
34 Topo_Cart::Topo_Cart(int ndims) : ndims_(ndims), dims_(ndims), periodic_(ndims), position_(ndims)
38 /* reorder is ignored, don't know what would be the consequences of a dumb reordering but neither do I see the point of
40 Topo_Cart::Topo_Cart(MPI_Comm comm_old, int ndims, const int dims[], const int periods[], int /*reorder*/, MPI_Comm* comm_cart)
46 int rank = comm_old->rank();
50 for (int i = 0 ; i < ndims ; i++) {
54 if(comm_cart != nullptr)
55 *comm_cart = MPI_COMM_NULL;
61 // FIXME : code duplication... See coords
63 for (int i=0; i<ndims; i++) {
65 periodic_[i] = periods[i];
66 nranks = nranks / dims[i];
67 /* FIXME: nranks could be zero (?) */
68 position_[i] = rank / nranks;
72 if(comm_cart != nullptr){
73 oldGroup = comm_old->group();
74 newGroup = new Group(newSize);
75 for (int i = 0 ; i < newSize ; i++) {
76 newGroup->set_mapping(oldGroup->actor(i), i);
78 *comm_cart = new Comm(newGroup, this);
81 if(comm_cart != nullptr){
83 *comm_cart = new Comm(new Group(MPI_COMM_SELF->group()), this);
85 *comm_cart = MPI_COMM_NULL;
89 if(comm_cart != nullptr)
93 Topo_Cart* Topo_Cart::sub(const int remain_dims[], MPI_Comm *newcomm) {
94 int oldNDims = ndims_;
95 int *newDims = nullptr;
96 int *newPeriodic = nullptr;
98 if (remain_dims == nullptr && oldNDims != 0) {
102 for (int i = 0 ; i < oldNDims ; i++) {
108 newDims = new int[newNDims];
109 newPeriodic = new int[newNDims];
111 // that should not segfault
113 for (int i = 0 ; j < newNDims ; i++) {
115 newDims[j] =dims_[i];
116 newPeriodic[j] =periodic_[i];
122 //split into several communicators
124 for (int i = 0; i < oldNDims; i++) {
125 if (not remain_dims[i]) {
126 color = (color * dims_[i] + position_[i]);
131 res = new Topo_Cart(getComm(), newNDims, newDims, newPeriodic, 0, newcomm);
133 *newcomm = getComm()->split(color, getComm()->rank());
134 res = new Topo_Cart(getComm(), newNDims, newDims, newPeriodic, 0, nullptr);
135 res->setComm(*newcomm);
138 delete[] newPeriodic;
142 int Topo_Cart::coords(int rank, int /*maxdims*/, int coords[])
144 int nnodes = nnodes_;
145 for (int i = 0; i< ndims_; i++ ) {
146 nnodes = nnodes /dims_[i];
147 coords[i] = rank / nnodes;
148 rank = rank % nnodes;
153 int Topo_Cart::get(int maxdims, int* dims, int* periods, int* coords) {
154 int ndims=ndims_ < maxdims ?ndims_ : maxdims;
155 for(int i = 0 ; i < ndims ; i++) {
157 periods[i] =periodic_[i];
158 coords[i] =position_[i];
163 int Topo_Cart::rank(const int* coords, int* rank) {
168 for (int i=ndims-1; i >=0; i-- ) {
169 int coord = coords[i];
171 /* The user can give us whatever coordinates he wants. If one of them is out of range, either this dimension is
172 * periodic, and we consider the equivalent coordinate inside the bounds, or it's not and then it's an error
174 if (coord >=dims_[i]) {
176 coord = coord %dims_[i];
178 // Should I do that ?
182 } else if (coord < 0) {
184 coord = coord %dims_[i];
186 coord =dims_[i] + coord;
193 *rank += multiplier * coord;
194 multiplier *=dims_[i];
199 int Topo_Cart::shift(int direction, int disp, int* rank_source, int* rank_dest)
204 if (ndims_ < direction) {
208 int* position = new int[ndims_];
209 this->coords(getComm()->rank(), ndims_, position);
210 position[direction] += disp;
212 if(position[direction] < 0 ||
213 position[direction] >=dims_[direction]) {
214 if(periodic_[direction]) {
215 position[direction] %=dims_[direction];
216 this->rank(position, rank_dest);
218 *rank_dest = MPI_PROC_NULL;
221 this->rank(position, rank_dest);
224 position[direction] = position_[direction] - disp;
225 if(position[direction] < 0 || position[direction] >=dims_[direction]) {
226 if(periodic_[direction]) {
227 position[direction] %=dims_[direction];
228 this->rank(position, rank_source);
230 *rank_source = MPI_PROC_NULL;
233 this->rank(position, rank_source);
239 int Topo_Cart::dim_get(int *ndims) {
244 // Everything below has been taken from ompi, but could be easily rewritten (and partially was to follow sonar rules).
247 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
248 * University Research and Technology
249 * Corporation. All rights reserved.
250 * Copyright (c) 2004-2005 The University of Tennessee and The University
251 * of Tennessee Research Foundation. All rights
253 * Copyright (c) 2004-2014 High Performance Computing Center Stuttgart,
254 * University of Stuttgart. All rights reserved.
255 * Copyright (c) 2004-2005 The Regents of the University of California.
256 * All rights reserved.
257 * Copyright (c) 2012 Los Alamos National Security, LLC. All rights
259 * Copyright (c) 2014 Intel, Inc. All rights reserved
262 * Additional copyrights may follow
268 * This is a utility function, no need to have anything in the lower layer for this at all
270 int Topo_Cart::Dims_create(int nnodes, int ndims, int dims[])
272 /* Get # of free-to-be-assigned processes and # of free dimensions */
273 int freeprocs = nnodes;
276 for (int i = 0; i < ndims; ++i) {
279 } else if ((*p < 0) || ((nnodes % *p) != 0)) {
289 if (freeprocs == 1) {
295 if (freeprocs == 1) {
296 for (int i = 0; i < ndims; ++i) {
305 /* Factor the number of free processes */
308 int err = getfactors(freeprocs, &nfactors, &factors);
309 if (MPI_SUCCESS != err)
312 /* Assign free processes to free dimensions */
314 err = assignnodes(freedims, nfactors, factors, &procs);
315 if (MPI_SUCCESS != err)
318 /* Return assignment results */
320 for (int i = 0; i < ndims; ++i) {
340 * Function: - assign processes to dimensions
341 * - get "best-balanced" grid
342 * - greedy bin-packing algorithm used
343 * - sort dimensions in decreasing order
344 * - dimensions array dynamically allocated
345 * Accepts: - # of dimensions
346 * - # of prime factors
347 * - array of prime factors
348 * - ptr to array of dimensions (returned value)
349 * Returns: - 0 or ERROR
351 static int assignnodes(int ndim, int nfactor, int *pfacts, int **pdims)
359 /* Allocate and initialize the bins */
360 int *bins = new int[ndim];
365 for (int i = 0 ; i < ndim; ++i) {
369 /* Loop assigning factors from the highest to the lowest */
370 for (int j = nfactor - 1; j >= 0; --j) {
372 /* Assign a factor to the smallest bin */
375 for (int i = 1; i < ndim; ++i) {
384 /* Sort dimensions in decreasing order (O(n^2) for now) */
386 for (int i = 0; i < ndim - 1; ++i) {
388 for (int j = i + 1; j < ndim; ++j) {
405 * Function: - factorize a number
408 * - array of prime factors
409 * Returns: - MPI_SUCCESS or ERROR
411 static int getfactors(int num, int *nfactors, int **factors) {
414 (*factors) = nullptr;
417 /* Allocate the array of prime factors which cannot exceed log_2(num) entries */
418 int sqrtnum = ceil(sqrt(double(num)));
419 int size = ceil(log(double(num)) / log(2.0));
420 *factors = new int[size];
423 /* determine all occurrences of factor 2 */
424 while((num % 2) == 0) {
428 /* determine all occurrences of uneven prime numbers up to sqrt(num) */
429 for(int d = 3; (num > 1) && (d < sqrtnum); d += 2) {
430 while((num % d) == 0) {
435 /* as we looped only up to sqrt(num) one factor > sqrt(num) may be left over */
437 (*factors)[i++] = num;