summaryrefslogtreecommitdiff
path: root/src/communication/blockLoadBalancer.hh
blob: 0c61af1be693af8698c9ff77eab233fcc34eeffe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*  This file is part of the OpenLB library
 *
 *  Copyright (C) 2007 Mathias Krause
 *  E-mail contact: info@openlb.net
 *  The most recent release of OpenLB can be downloaded at
 *  <http://www.openlb.net/>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this program; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA  02110-1301, USA.
*/

#ifndef BLOCK_LOAD_BALANCER_HH
#define BLOCK_LOAD_BALANCER_HH

#include <vector>
#include <map>
#include "communication/mpiManager.h"
#include "communication/blockLoadBalancer.h"
#include "geometry/cuboidGeometry2D.h"
#include "geometry/cuboidGeometry3D.h"
#include "core/olbDebug.h"

namespace olb {

template<typename T>
BlockLoadBalancer<T>::BlockLoadBalancer(int rank, int size, int globChunkSize, int offset)
{
  init_chunkD(rank, size, globChunkSize, offset);
}

template<typename T>
BlockLoadBalancer<T>::BlockLoadBalancer(CuboidGeometry3D<T>& cGeometry)
{
  init_chunkD(singleton::mpi().getRank(), singleton::mpi().getSize(), cGeometry.getNc(), 0);
}

template<typename T>
BlockLoadBalancer<T>::BlockLoadBalancer(CuboidGeometry2D<T>& cGeometry)
{
  init_chunkD(singleton::mpi().getRank(), singleton::mpi().getSize(), cGeometry.getNc(), 0);
}

template<typename T>
void BlockLoadBalancer<T>::init_chunkD(int rank, int size, int globChunkSize, int offset)
{

  OLB_PRECONDITION(rank>=0 && size>=1 && offset>=0)
  OLB_PRECONDITION(size<=globChunkSize &&  rank<size);

  // nice way to calculate # of chunks per processor
  this->_locChunkSize = (globChunkSize+size-rank-1)/size;
  this->_size = _locChunkSize;
  if (rank+1 <= globChunkSize-(globChunkSize/size)*size) {
    this->_firstGlobNum = globChunkSize/size * rank + rank + offset;
    this->_lastGlobNum  = this->_firstGlobNum + this->_locChunkSize - 1;
  } else {
    this->_firstGlobNum = globChunkSize/size * rank + globChunkSize - (globChunkSize/size)*size + offset;
    this->_lastGlobNum  = this->_firstGlobNum + this->_locChunkSize - 1;
  }
  for (int i=0; i<this->_locChunkSize; i++) {
    this->_loc[this->_firstGlobNum + i] = i;
    this->_glob.push_back(this->_firstGlobNum + i);
  }
  int temp = offset;
  for (int iRank=0; iRank<size; iRank++) {
    int iLocChunkSize = (globChunkSize+size-iRank-1)/size;
    for (int i=0; i<iLocChunkSize; i++) {
      this->_rank[temp] = iRank;
      temp++;
    }
  }
}

template<typename T>
int BlockLoadBalancer<T>::locChunkSize() const
{
  return _locChunkSize;
}

template<typename T>
int BlockLoadBalancer<T>::firstGlobNum() const
{
  return _firstGlobNum;
}

template<typename T>
int BlockLoadBalancer<T>::lastGlobNum() const
{
  return _lastGlobNum;
}

}  // namespace olb
#endif