[553] | 1 | /*! |
---|
| 2 | \file server_distribution_description.hpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 04 Jan 2015 |
---|
[815] | 5 | \date 11 Jan 2016 |
---|
[553] | 6 | |
---|
| 7 | \brief Description of index distribution on server(s). |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include "server_distribution_description.hpp" |
---|
[657] | 11 | #include "exception.hpp" |
---|
[553] | 12 | |
---|
| 13 | namespace xios |
---|
| 14 | { |
---|
[815] | 15 | /*! |
---|
| 16 | \param [in] globalDimensionSize global dimension of grid |
---|
| 17 | \param [in] nServer number of server |
---|
| 18 | \param [in] serType type of server distribution. For now, we can distribute server by band or plan |
---|
| 19 | */ |
---|
| 20 | CServerDistributionDescription::CServerDistributionDescription(const std::vector<int>& globalDimensionSize, |
---|
| 21 | int nServer, |
---|
| 22 | ServerDistributionType serType) |
---|
| 23 | : nGlobal_(globalDimensionSize), indexBegin_(), dimensionSizes_(), globalIndex_(), |
---|
| 24 | vecGlobalIndex_(), serverType_(serType), nServer_(nServer), positionDimensionDistributed_(1) |
---|
[553] | 25 | { |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | CServerDistributionDescription::~CServerDistributionDescription() |
---|
[653] | 29 | { /* Nothing to do */ } |
---|
[553] | 30 | |
---|
| 31 | /*! |
---|
| 32 | Compute pre-defined global index distribution of server(s). |
---|
| 33 | \param [in] doComputeGlobalIndex flag to compute global index on each server. By default, false |
---|
[815] | 34 | |
---|
[553] | 35 | */ |
---|
[815] | 36 | void CServerDistributionDescription::computeServerDistribution(bool doComputeGlobalIndex, |
---|
| 37 | int positionDimensionDistributed) |
---|
[553] | 38 | { |
---|
[815] | 39 | switch (serverType_) { |
---|
[553] | 40 | case BAND_DISTRIBUTION: |
---|
[815] | 41 | computeBandDistribution(nServer_, positionDimensionDistributed); |
---|
[553] | 42 | break; |
---|
| 43 | default: |
---|
| 44 | break; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | if (doComputeGlobalIndex) |
---|
| 48 | { |
---|
[815] | 49 | vecGlobalIndex_.resize(nServer_); |
---|
[553] | 50 | int dim = nGlobal_.size(); |
---|
| 51 | std::vector<int> currentIndex(dim); |
---|
| 52 | |
---|
[815] | 53 | for (int idxServer = 0; idxServer < nServer_; ++idxServer) |
---|
[553] | 54 | { |
---|
| 55 | size_t ssize = 1, idx = 0; |
---|
| 56 | for (int j = 0; j < dim; ++j) ssize *= dimensionSizes_[idxServer][j]; |
---|
[653] | 57 | vecGlobalIndex_[idxServer].resize(ssize); |
---|
[553] | 58 | |
---|
| 59 | std::vector<int> idxLoop(dim,0); |
---|
| 60 | |
---|
| 61 | int innerLoopSize = dimensionSizes_[idxServer][0]; |
---|
| 62 | |
---|
| 63 | while (idx<ssize) |
---|
| 64 | { |
---|
| 65 | for (int idxDim = 0; idxDim < dim-1; ++idxDim) |
---|
| 66 | { |
---|
| 67 | if (idxLoop[idxDim] == dimensionSizes_[idxServer][idxDim]) |
---|
| 68 | { |
---|
| 69 | idxLoop[idxDim] = 0; |
---|
| 70 | ++idxLoop[idxDim+1]; |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | for (int idxDim = 1; idxDim < dim; ++idxDim) currentIndex[idxDim] = idxLoop[idxDim] + indexBegin_[idxServer][idxDim]; |
---|
| 75 | |
---|
| 76 | size_t mulDim, globalIndex; |
---|
| 77 | for (int j = 0; j < innerLoopSize; ++j) |
---|
| 78 | { |
---|
| 79 | mulDim = 1; |
---|
| 80 | globalIndex = j + indexBegin_[idxServer][0]; |
---|
| 81 | |
---|
| 82 | for (int k = 1; k < dim; ++k) |
---|
| 83 | { |
---|
| 84 | mulDim *= nGlobal_[k-1]; |
---|
[653] | 85 | globalIndex += currentIndex[k] * mulDim; |
---|
[553] | 86 | } |
---|
[653] | 87 | vecGlobalIndex_[idxServer](idx) = globalIndex; |
---|
[553] | 88 | ++idx; |
---|
| 89 | } |
---|
| 90 | idxLoop[0] += innerLoopSize; |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | |
---|
[569] | 96 | /*! |
---|
| 97 | Compute global index assigned to a server with a range.E.g: if a grid has 100 points and |
---|
| 98 | there are 2 servers, the first one takes index from 0 to 49, the second has index from 50 to 99 |
---|
[815] | 99 | |
---|
[569] | 100 | \param [in] indexBeginEnd begining and ending index of range |
---|
| 101 | */ |
---|
[815] | 102 | void CServerDistributionDescription::computeServerGlobalIndexInRange(const std::pair<size_t, size_t>& indexBeginEnd, |
---|
| 103 | int positionDimensionDistributed) |
---|
[568] | 104 | { |
---|
[815] | 105 | switch (serverType_) { |
---|
[568] | 106 | case BAND_DISTRIBUTION: |
---|
[815] | 107 | computeBandDistribution(nServer_, positionDimensionDistributed); |
---|
[568] | 108 | break; |
---|
| 109 | default: |
---|
| 110 | break; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | size_t indexBegin = indexBeginEnd.first; |
---|
| 114 | size_t indexEnd = indexBeginEnd.second; |
---|
| 115 | if (indexBegin > indexEnd) |
---|
| 116 | ERROR("CServerDistributionDescription::computeServerGlobalIndexInRange", |
---|
| 117 | << "Index begin is larger than index end"); |
---|
| 118 | |
---|
| 119 | int dim = nGlobal_.size(); |
---|
| 120 | std::vector<int> currentIndex(dim); |
---|
| 121 | |
---|
[815] | 122 | for (int idxServer = 0; idxServer < nServer_; ++idxServer) |
---|
[568] | 123 | { |
---|
| 124 | size_t ssize = 1, idx = 0; |
---|
| 125 | for (int j = 0; j < dim; ++j) ssize *= dimensionSizes_[idxServer][j]; |
---|
| 126 | |
---|
| 127 | std::vector<int> idxLoop(dim,0); |
---|
| 128 | int innerLoopSize = dimensionSizes_[idxServer][0]; |
---|
| 129 | |
---|
| 130 | while (idx<ssize) |
---|
| 131 | { |
---|
| 132 | for (int idxDim = 0; idxDim < dim-1; ++idxDim) |
---|
| 133 | { |
---|
| 134 | if (idxLoop[idxDim] == dimensionSizes_[idxServer][idxDim]) |
---|
| 135 | { |
---|
| 136 | idxLoop[idxDim] = 0; |
---|
| 137 | ++idxLoop[idxDim+1]; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | for (int idxDim = 1; idxDim < dim; ++idxDim) currentIndex[idxDim] = idxLoop[idxDim] + indexBegin_[idxServer][idxDim]; |
---|
| 142 | |
---|
| 143 | size_t mulDim, globalIndex; |
---|
| 144 | for (int j = 0; j < innerLoopSize; ++j) |
---|
| 145 | { |
---|
| 146 | mulDim = 1; |
---|
| 147 | globalIndex = j + indexBegin_[idxServer][0]; |
---|
| 148 | |
---|
| 149 | for (int k = 1; k < dim; ++k) |
---|
| 150 | { |
---|
| 151 | mulDim *= nGlobal_[k-1]; |
---|
| 152 | globalIndex += (currentIndex[k])*mulDim; |
---|
| 153 | } |
---|
| 154 | if ((indexBegin <= globalIndex) && (globalIndex <= indexEnd)) |
---|
| 155 | globalIndex_.insert(std::make_pair<size_t,int>(globalIndex, idxServer)); |
---|
| 156 | ++idx; |
---|
| 157 | } |
---|
| 158 | idxLoop[0] += innerLoopSize; |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | } |
---|
| 163 | |
---|
[553] | 164 | /*! |
---|
| 165 | Compute global index of servers with band distribution |
---|
| 166 | \param [in] nServer number of server |
---|
| 167 | */ |
---|
[657] | 168 | void CServerDistributionDescription::computeBandDistribution(int nServer, int positionDimensionDistributed) |
---|
[553] | 169 | { |
---|
| 170 | int dim = nGlobal_.size(); |
---|
[815] | 171 | positionDimensionDistributed_ = positionDimensionDistributed; |
---|
| 172 | if (1 == dim) positionDimensionDistributed_ = 0; |
---|
| 173 | if (positionDimensionDistributed_ > dim) |
---|
[657] | 174 | ERROR("CServerDistributionDescription::computeBandDistribution(int nServer, int positionDimensionDistributed)", |
---|
| 175 | << "Position of distributed dimension is invalid" << std::endl |
---|
[815] | 176 | << "Position of distributed dimension is " << positionDimensionDistributed_ |
---|
[657] | 177 | << "Dimension " << dim) |
---|
| 178 | |
---|
[557] | 179 | indexBegin_.resize(nServer); |
---|
| 180 | dimensionSizes_.resize(nServer); |
---|
[553] | 181 | |
---|
| 182 | for (int i = 0; i< nServer; ++i) |
---|
| 183 | { |
---|
[557] | 184 | indexBegin_[i].resize(dim); |
---|
| 185 | dimensionSizes_[i].resize(dim); |
---|
[553] | 186 | } |
---|
| 187 | |
---|
| 188 | int njRangeSize; |
---|
| 189 | int nGlobTemp = 0; |
---|
| 190 | std::vector<int> njRangeBegin(nServer,0); |
---|
| 191 | std::vector<int> njRangeEnd(nServer,0); |
---|
| 192 | |
---|
[815] | 193 | int positionDistributed = (1<dim) ? positionDimensionDistributed_ : 0; |
---|
[657] | 194 | nGlobTemp = nGlobal_[positionDistributed]; |
---|
[553] | 195 | |
---|
| 196 | for (int i = 0; i < nServer; ++i) |
---|
| 197 | { |
---|
| 198 | if (0 < i) njRangeBegin[i] = njRangeEnd[i-1]; |
---|
| 199 | njRangeSize = nGlobTemp / nServer; |
---|
| 200 | if (i < nGlobTemp%nServer) ++njRangeSize; |
---|
| 201 | njRangeEnd[i] = njRangeSize + njRangeBegin[i]; |
---|
| 202 | } |
---|
| 203 | njRangeEnd[nServer-1] = nGlobTemp; |
---|
| 204 | |
---|
| 205 | for (int i = 0; i < nServer; ++i) |
---|
| 206 | { |
---|
| 207 | for (int j = 0; j < dim; ++j) |
---|
| 208 | { |
---|
[657] | 209 | if (positionDistributed != j) |
---|
[553] | 210 | { |
---|
| 211 | if (1 == dim) |
---|
| 212 | { |
---|
| 213 | indexBegin_[i][j] = njRangeBegin[i]; |
---|
| 214 | dimensionSizes_[i][j] = njRangeEnd[i] - njRangeBegin[i]; |
---|
| 215 | } |
---|
| 216 | else |
---|
| 217 | { |
---|
| 218 | indexBegin_[i][j] = 0; |
---|
| 219 | dimensionSizes_[i][j] = nGlobal_[j]; |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | else |
---|
| 223 | { |
---|
| 224 | indexBegin_[i][j] = njRangeBegin[i]; |
---|
| 225 | dimensionSizes_[i][j] = njRangeEnd[i] - njRangeBegin[i]; |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | /*! |
---|
| 232 | Get size of each dimension on distributed server |
---|
| 233 | \return size of dimensions on server(s) |
---|
| 234 | */ |
---|
| 235 | std::vector<std::vector<int> > CServerDistributionDescription::getServerDimensionSizes() const |
---|
| 236 | { |
---|
| 237 | return dimensionSizes_; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | /*! |
---|
| 241 | Get index begin of each dimension on distributed server |
---|
| 242 | \return index begin of dimensions on server(s) |
---|
| 243 | */ |
---|
| 244 | std::vector<std::vector<int> > CServerDistributionDescription::getServerIndexBegin() const |
---|
| 245 | { |
---|
| 246 | return indexBegin_; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | /*! |
---|
| 250 | Get global index on distributed server |
---|
| 251 | \return global index on server(s) |
---|
| 252 | */ |
---|
[653] | 253 | const std::vector<CArray<size_t,1> >& CServerDistributionDescription::getGlobalIndex() const |
---|
[553] | 254 | { |
---|
| 255 | return vecGlobalIndex_; |
---|
| 256 | } |
---|
| 257 | |
---|
[569] | 258 | /*! |
---|
| 259 | Get global index calculated by computeServerGlobalIndexInRange |
---|
| 260 | */ |
---|
[568] | 261 | const boost::unordered_map<size_t,int>& CServerDistributionDescription::getGlobalIndexRange() const |
---|
| 262 | { |
---|
| 263 | return globalIndex_; |
---|
| 264 | } |
---|
[815] | 265 | |
---|
| 266 | int CServerDistributionDescription::getDimensionDistributed() |
---|
| 267 | { |
---|
| 268 | return ((1<nGlobal_.size()) ? positionDimensionDistributed_ : 0); |
---|
| 269 | } |
---|
| 270 | |
---|
[553] | 271 | } // namespace xios |
---|