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. */
6 #include "xbt/sysdep.h"
12 /* static functions */
13 static int assignnodes(int ndim, int nfactor, int *pfacts,int **pdims);
14 static int getfactors(int num, int *nfators, int **factors);
21 Topo_Graph::~Topo_Graph()
27 Topo_Dist_Graph::~Topo_Dist_Graph()
32 delete[] out_weights_;
35 /*******************************************************************************
36 * Cartesian topologies
37 ******************************************************************************/
38 Topo_Cart::~Topo_Cart()
45 Topo_Cart::Topo_Cart(int ndims) : ndims_(ndims)
47 dims_ = new int[ndims];
48 periodic_ = new int[ndims];
49 position_ = new int[ndims];
52 /* reorder is ignored, don't know what would be the consequences of a dumb reordering but neither do I see the point of
54 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_;
103 int *newDims = nullptr;
104 int *newPeriodic = nullptr;
106 if (remain_dims == nullptr && oldNDims != 0) {
110 for (int i = 0 ; i < oldNDims ; i++) {
116 newDims = xbt_new(int, newNDims);
117 newPeriodic = xbt_new(int, newNDims);
119 // 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(comm_, 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) {
157 for (int i=ndims-1; i >=0; i-- ) {
160 /* The user can give us whatever coordinates he wants. If one of them is out of range, either this dimension is
161 * periodic, and we consider the equivalent coordinate inside the bounds, or it's not and then it's an error
163 if (coord >=dims_[i]) {
165 coord = coord %dims_[i];
167 // Should I do that ?
171 } else if (coord < 0) {
173 coord = coord %dims_[i];
175 coord =dims_[i] + coord;
182 *rank += multiplier * coord;
183 multiplier *=dims_[i];
188 int Topo_Cart::shift(int direction, int disp, int *rank_source, int *rank_dest) {
190 int position[ndims_];
195 if (ndims_ < direction) {
199 this->coords(comm_->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
259 * This is a utility function, no need to have anything in the lower layer for this at all
261 int Topo_Cart::Dims_create(int nnodes, int ndims, int dims[])
263 /* Get # of free-to-be-assigned processes and # of free dimensions */
264 int freeprocs = nnodes;
267 for (int i = 0; i < ndims; ++i) {
270 } else if ((*p < 0) || ((nnodes % *p) != 0)) {
280 if (freeprocs == 1) {
286 if (freeprocs == 1) {
287 for (int i = 0; i < ndims; ++i) {
296 /* Factor the number of free processes */
299 int err = getfactors(freeprocs, &nfactors, &factors);
300 if (MPI_SUCCESS != err)
303 /* Assign free processes to free dimensions */
305 err = assignnodes(freedims, nfactors, factors, &procs);
306 if (MPI_SUCCESS != err)
309 /* Return assignment results */
311 for (int i = 0; i < ndims; ++i) {
331 * Function: - assign processes to dimensions
332 * - get "best-balanced" grid
333 * - greedy bin-packing algorithm used
334 * - sort dimensions in decreasing order
335 * - dimensions array dynamically allocated
336 * Accepts: - # of dimensions
337 * - # of prime factors
338 * - array of prime factors
339 * - ptr to array of dimensions (returned value)
340 * Returns: - 0 or ERROR
342 static int assignnodes(int ndim, int nfactor, int *pfacts, int **pdims)
350 /* Allocate and initialize the bins */
351 int *bins = new int[ndim];
356 for (int i = 0 ; i < ndim; ++i) {
360 /* Loop assigning factors from the highest to the lowest */
361 for (int j = nfactor - 1; j >= 0; --j) {
363 /* Assign a factor to the smallest bin */
366 for (int i = 1; i < ndim; ++i) {
375 /* Sort dimensions in decreasing order (O(n^2) for now) */
377 for (int i = 0; i < ndim - 1; ++i) {
379 for (int j = i + 1; j < ndim; ++j) {
396 * Function: - factorize a number
399 * - array of prime factors
400 * Returns: - MPI_SUCCESS or ERROR
402 static int getfactors(int num, int *nfactors, int **factors) {
405 (*factors) = nullptr;
408 /* Allocate the array of prime factors which cannot exceed log_2(num) entries */
409 int sqrtnum = ceil(sqrt(num));
410 int size = ceil(log(num) / log(2));
411 *factors = new int[size];
414 /* determine all occurrences of factor 2 */
415 while((num % 2) == 0) {
419 /* determine all occurrences of uneven prime numbers up to sqrt(num) */
420 for(int d = 3; (num > 1) && (d < sqrtnum); d += 2) {
421 while((num % d) == 0) {
426 /* as we looped only up to sqrt(num) one factor > sqrt(num) may be left over */
428 (*factors)[i++] = num;