1 /* Copyright (c) 2014-2017. 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->index(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 = xbt_new(int, newNDims);
116 newPeriodic = xbt_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 return new Topo_Cart(getComm(), newNDims, newDims, newPeriodic, 0, newcomm);
131 int Topo_Cart::coords(int rank, int maxdims, int coords[]) {
132 int nnodes = nnodes_;
133 for (int i = 0; i< ndims_; i++ ) {
134 nnodes = nnodes /dims_[i];
135 coords[i] = rank / nnodes;
136 rank = rank % nnodes;
141 int Topo_Cart::get(int maxdims, int* dims, int* periods, int* coords) {
142 int ndims=ndims_ < maxdims ?ndims_ : maxdims;
143 for(int i = 0 ; i < ndims ; i++) {
145 periods[i] =periodic_[i];
146 coords[i] =position_[i];
151 int Topo_Cart::rank(int* coords, int* rank) {
156 for (int i=ndims-1; i >=0; i-- ) {
157 int coord = coords[i];
159 /* The user can give us whatever coordinates he wants. If one of them is out of range, either this dimension is
160 * periodic, and we consider the equivalent coordinate inside the bounds, or it's not and then it's an error
162 if (coord >=dims_[i]) {
164 coord = coord %dims_[i];
166 // Should I do that ?
170 } else if (coord < 0) {
172 coord = coord %dims_[i];
174 coord =dims_[i] + coord;
181 *rank += multiplier * coord;
182 multiplier *=dims_[i];
187 int Topo_Cart::shift(int direction, int disp, int *rank_source, int *rank_dest) {
189 int position[ndims_];
194 if (ndims_ < direction) {
198 this->coords(getComm()->rank(), ndims_, position);
199 position[direction] += disp;
201 if(position[direction] < 0 ||
202 position[direction] >=dims_[direction]) {
203 if(periodic_[direction]) {
204 position[direction] %=dims_[direction];
205 this->rank(position, rank_dest);
207 *rank_dest = MPI_PROC_NULL;
210 this->rank(position, rank_dest);
213 position[direction] = position_[direction] - disp;
214 if(position[direction] < 0 || position[direction] >=dims_[direction]) {
215 if(periodic_[direction]) {
216 position[direction] %=dims_[direction];
217 this->rank(position, rank_source);
219 *rank_source = MPI_PROC_NULL;
222 this->rank(position, rank_source);
228 int Topo_Cart::dim_get(int *ndims) {
233 // Everything below has been taken from ompi, but could be easily rewritten (and partially was to follow sonar rules).
236 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
237 * University Research and Technology
238 * Corporation. All rights reserved.
239 * Copyright (c) 2004-2005 The University of Tennessee and The University
240 * of Tennessee Research Foundation. All rights
242 * Copyright (c) 2004-2014 High Performance Computing Center Stuttgart,
243 * University of Stuttgart. All rights reserved.
244 * Copyright (c) 2004-2005 The Regents of the University of California.
245 * All rights reserved.
246 * Copyright (c) 2012 Los Alamos National Security, LLC. All rights
248 * Copyright (c) 2014 Intel, Inc. All rights reserved
251 * Additional copyrights may follow
257 * This is a utility function, no need to have anything in the lower layer for this at all
259 int Topo_Cart::Dims_create(int nnodes, int ndims, int dims[])
261 /* Get # of free-to-be-assigned processes and # of free dimensions */
262 int freeprocs = nnodes;
265 for (int i = 0; i < ndims; ++i) {
268 } else if ((*p < 0) || ((nnodes % *p) != 0)) {
278 if (freeprocs == 1) {
284 if (freeprocs == 1) {
285 for (int i = 0; i < ndims; ++i) {
294 /* Factor the number of free processes */
297 int err = getfactors(freeprocs, &nfactors, &factors);
298 if (MPI_SUCCESS != err)
301 /* Assign free processes to free dimensions */
303 err = assignnodes(freedims, nfactors, factors, &procs);
304 if (MPI_SUCCESS != err)
307 /* Return assignment results */
309 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 * - # of prime factors
336 * - array of prime factors
337 * - ptr to array of dimensions (returned value)
338 * Returns: - 0 or ERROR
340 static int assignnodes(int ndim, int nfactor, int *pfacts, int **pdims)
348 /* Allocate and initialize the bins */
349 int *bins = new int[ndim];
354 for (int i = 0 ; i < ndim; ++i) {
358 /* Loop assigning factors from the highest to the lowest */
359 for (int j = nfactor - 1; j >= 0; --j) {
361 /* Assign a factor to the smallest bin */
364 for (int i = 1; i < ndim; ++i) {
373 /* Sort dimensions in decreasing order (O(n^2) for now) */
375 for (int i = 0; i < ndim - 1; ++i) {
377 for (int j = i + 1; j < ndim; ++j) {
394 * Function: - factorize a number
397 * - array of prime factors
398 * Returns: - MPI_SUCCESS or ERROR
400 static int getfactors(int num, int *nfactors, int **factors) {
403 (*factors) = nullptr;
406 /* Allocate the array of prime factors which cannot exceed log_2(num) entries */
407 int sqrtnum = ceil(sqrt(num));
408 int size = ceil(log(num) / log(2));
409 *factors = new int[size];
412 /* determine all occurrences of factor 2 */
413 while((num % 2) == 0) {
417 /* determine all occurrences of uneven prime numbers up to sqrt(num) */
418 for(int d = 3; (num > 1) && (d < sqrtnum); d += 2) {
419 while((num % d) == 0) {
424 /* as we looped only up to sqrt(num) one factor > sqrt(num) may be left over */
426 (*factors)[i++] = num;