[553] | 1 | /*! |
---|
| 2 | \file distribution_client.cpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 13 Jan 2015 |
---|
[569] | 5 | \date 09 Mars 2015 |
---|
[553] | 6 | |
---|
| 7 | \brief Index distribution on client side. |
---|
| 8 | */ |
---|
[551] | 9 | #include "distribution_client.hpp" |
---|
| 10 | |
---|
| 11 | namespace xios { |
---|
| 12 | |
---|
[1025] | 13 | CDistributionClient::CDistributionClient(int rank, CGrid* grid, const GlobalLocalDataMap& globalLocalIndex) |
---|
[983] | 14 | : CDistribution(rank, 0) |
---|
| 15 | , axisDomainOrder_() |
---|
| 16 | , nLocal_(), nGlob_(), nBeginLocal_(), nBeginGlobal_(),nZoomBegin_(), nZoomEnd_() |
---|
| 17 | , dataNIndex_(), dataDims_(), dataBegin_(), dataIndex_(), domainMasks_(), axisMasks_() |
---|
| 18 | , gridMask_(), indexMap_() |
---|
| 19 | , isDataDistributed_(true), axisNum_(0), domainNum_(0) |
---|
| 20 | , localDataIndex_(), localMaskIndex_() |
---|
| 21 | , globalLocalDataSendToServerMap_(globalLocalIndex) |
---|
| 22 | , infoIndex_(), isComputed_(false) |
---|
| 23 | , elementLocalIndex_(), elementGlobalIndex_(), elementIndexData_() |
---|
| 24 | , elementZoomMask_(), elementNLocal_(), elementNGlobal_() |
---|
| 25 | { |
---|
[992] | 26 | isComputed_ = true; |
---|
[983] | 27 | localDataIndex_.resize(globalLocalIndex.size()); |
---|
| 28 | localMaskIndex_.resize(globalLocalIndex.size()); |
---|
| 29 | GlobalLocalDataMap::const_iterator it = globalLocalIndex.begin(), ite = globalLocalIndex.end(); |
---|
| 30 | int idx = 0; |
---|
| 31 | for (; it!=ite; ++it) |
---|
| 32 | { |
---|
| 33 | localMaskIndex_[idx] = localDataIndex_[idx] = it->second; |
---|
| 34 | ++idx; |
---|
| 35 | } |
---|
[1025] | 36 | |
---|
| 37 | std::vector<CDomain*> domList = grid->getDomains(); |
---|
| 38 | std::vector<CAxis*> axisList = grid->getAxis(); |
---|
| 39 | std::vector<CScalar*> scalarList = grid->getScalars(); |
---|
| 40 | CArray<int,1> axisDomainOrder = grid->axis_domain_order; |
---|
| 41 | |
---|
| 42 | int numElement = axisDomainOrder.numElements(); |
---|
| 43 | // Because domain and axis can be in any order (axis1, domain1, axis2, axis3, ) |
---|
| 44 | // their position should be specified. In axisDomainOrder, domain == true, axis == false |
---|
| 45 | int dims = numElement; |
---|
| 46 | idx = 0; |
---|
| 47 | std::vector<int> indexMap(numElement); |
---|
| 48 | for (int i = 0; i < numElement; ++i) |
---|
| 49 | { |
---|
| 50 | indexMap[i] = idx; |
---|
| 51 | if (2 == axisDomainOrder(i)) |
---|
| 52 | { |
---|
| 53 | ++dims; |
---|
| 54 | idx += 2; |
---|
| 55 | } |
---|
| 56 | else ++idx; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | nGlob_.resize(dims); |
---|
| 60 | |
---|
| 61 | // Data_n_index of domain or axis (For now, axis uses its size as data_n_index |
---|
| 62 | dataNIndex_.resize(numElement); |
---|
| 63 | dataDims_.resize(numElement); |
---|
| 64 | isDataDistributed_ = false; |
---|
| 65 | int domIndex = 0, axisIndex = 0, scalarIndex = 0; |
---|
| 66 | for (idx = 0; idx < numElement; ++idx) |
---|
| 67 | { |
---|
| 68 | int eleDim = axisDomainOrder(idx); |
---|
| 69 | |
---|
| 70 | // If this is a domain |
---|
| 71 | if (2 == eleDim) |
---|
| 72 | { |
---|
| 73 | // On the j axis |
---|
| 74 | nGlob_.at(indexMap[idx]+1) = domList[domIndex]->nj_glo.getValue(); |
---|
| 75 | nGlob_.at(indexMap[idx]) = domList[domIndex]->ni_glo.getValue(); |
---|
| 76 | dataNIndex_.at(idx) = domList[domIndex]->data_i_index.numElements(); |
---|
| 77 | dataDims_.at(idx) = domList[domIndex]->data_dim.getValue(); |
---|
| 78 | |
---|
| 79 | isDataDistributed_ |= domList[domIndex]->isDistributed(); |
---|
| 80 | ++domIndex; |
---|
| 81 | } |
---|
| 82 | else if (1 == eleDim)// So it's an axis |
---|
| 83 | { |
---|
| 84 | nGlob_.at(indexMap[idx]) = axisList[axisIndex]->n_glo.getValue(); |
---|
| 85 | dataNIndex_.at(idx) = axisList[axisIndex]->data_index.numElements(); |
---|
| 86 | dataDims_.at(idx) = 1; |
---|
| 87 | |
---|
| 88 | isDataDistributed_ |= axisList[axisIndex]->isDistributed(); |
---|
| 89 | ++axisIndex; |
---|
| 90 | } |
---|
| 91 | else // scalar |
---|
| 92 | { |
---|
| 93 | nGlob_.at(indexMap[idx]) = 1; |
---|
| 94 | dataNIndex_.at(idx) = 1; |
---|
| 95 | dataDims_.at(idx) = 1; |
---|
| 96 | |
---|
| 97 | isDataDistributed_ |= false; |
---|
| 98 | ++scalarIndex; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
[983] | 102 | } |
---|
| 103 | |
---|
[653] | 104 | CDistributionClient::CDistributionClient(int rank, int dims, const CArray<size_t,1>& globalIndex) |
---|
| 105 | : CDistribution(rank, dims, globalIndex) |
---|
| 106 | , axisDomainOrder_() |
---|
| 107 | , nLocal_(), nGlob_(), nBeginLocal_(), nBeginGlobal_(),nZoomBegin_(), nZoomEnd_() |
---|
| 108 | , dataNIndex_(), dataDims_(), dataBegin_(), dataIndex_(), domainMasks_(), axisMasks_() |
---|
[861] | 109 | , gridMask_(), indexMap_() |
---|
[843] | 110 | , isDataDistributed_(true), axisNum_(0), domainNum_(0) |
---|
[833] | 111 | , localDataIndex_(), localMaskIndex_() |
---|
[831] | 112 | , globalLocalDataSendToServerMap_() |
---|
[834] | 113 | , infoIndex_(), isComputed_(false) |
---|
[861] | 114 | , elementLocalIndex_(), elementGlobalIndex_(), elementIndexData_() |
---|
| 115 | , elementZoomMask_(), elementNLocal_(), elementNGlobal_() |
---|
[551] | 116 | { |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | CDistributionClient::CDistributionClient(int rank, CGrid* grid) |
---|
[653] | 120 | : CDistribution(rank, 0) |
---|
| 121 | , axisDomainOrder_() |
---|
| 122 | , nLocal_(), nGlob_(), nBeginLocal_(), nBeginGlobal_(),nZoomBegin_(), nZoomEnd_() |
---|
| 123 | , dataNIndex_(), dataDims_(), dataBegin_(), dataIndex_(), domainMasks_(), axisMasks_() |
---|
[861] | 124 | , gridMask_(), indexMap_() |
---|
[843] | 125 | , isDataDistributed_(true), axisNum_(0), domainNum_(0) |
---|
[833] | 126 | , localDataIndex_(), localMaskIndex_() |
---|
[831] | 127 | , globalLocalDataSendToServerMap_() |
---|
[834] | 128 | , infoIndex_(), isComputed_(false) |
---|
[861] | 129 | , elementLocalIndex_(), elementGlobalIndex_(), elementIndexData_() |
---|
| 130 | , elementZoomMask_(), elementNLocal_(), elementNGlobal_() |
---|
[551] | 131 | { |
---|
| 132 | readDistributionInfo(grid); |
---|
| 133 | createGlobalIndex(); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | CDistributionClient::~CDistributionClient() |
---|
[653] | 137 | { /* Nothing to do */ } |
---|
[551] | 138 | |
---|
| 139 | /*! |
---|
| 140 | Read information of a grid to generate distribution. |
---|
| 141 | Every grid is composed of several axis or/and domain(s). Their information are processed |
---|
| 142 | stored and used to calculate index distribution between client and server |
---|
| 143 | \param [in] grid Grid to read |
---|
| 144 | */ |
---|
| 145 | void CDistributionClient::readDistributionInfo(CGrid* grid) |
---|
| 146 | { |
---|
| 147 | std::vector<CDomain*> domList = grid->getDomains(); |
---|
| 148 | std::vector<CAxis*> axisList = grid->getAxis(); |
---|
[887] | 149 | std::vector<CScalar*> scalarList = grid->getScalars(); |
---|
| 150 | CArray<int,1> axisDomainOrder = grid->axis_domain_order; |
---|
[551] | 151 | |
---|
[887] | 152 | readDistributionInfo(domList, axisList, scalarList, axisDomainOrder); |
---|
[551] | 153 | |
---|
| 154 | // Then check mask of grid |
---|
[653] | 155 | int gridDim = domList.size() * 2 + axisList.size(); |
---|
[551] | 156 | grid->checkMask(); |
---|
[567] | 157 | switch (gridDim) { |
---|
[888] | 158 | case 0: |
---|
| 159 | gridMask_.resize(1); |
---|
| 160 | gridMask_(0) = true; |
---|
| 161 | break; |
---|
[567] | 162 | case 1: |
---|
[817] | 163 | readGridMaskInfo(grid->mask_1d); |
---|
[567] | 164 | break; |
---|
| 165 | case 2: |
---|
[817] | 166 | readGridMaskInfo(grid->mask_2d); |
---|
[567] | 167 | break; |
---|
| 168 | case 3: |
---|
[817] | 169 | readGridMaskInfo(grid->mask_3d); |
---|
[567] | 170 | break; |
---|
[932] | 171 | case 4: |
---|
| 172 | readGridMaskInfo(grid->mask_4d); |
---|
| 173 | break; |
---|
| 174 | case 5: |
---|
| 175 | readGridMaskInfo(grid->mask_5d); |
---|
| 176 | break; |
---|
| 177 | case 6: |
---|
| 178 | readGridMaskInfo(grid->mask_6d); |
---|
| 179 | break; |
---|
| 180 | case 7: |
---|
| 181 | readGridMaskInfo(grid->mask_7d); |
---|
| 182 | break; |
---|
[567] | 183 | default: |
---|
| 184 | break; |
---|
| 185 | } |
---|
[551] | 186 | } |
---|
| 187 | |
---|
| 188 | /*! |
---|
| 189 | Read information from domain(s) and axis to generate distribution. |
---|
| 190 | All information related to domain, e.g ibegin, jbegin, ni, nj, ni_glo, nj_glo |
---|
| 191 | as well as related to axis, e.g dataNIndex, dataIndex will be stored to compute |
---|
| 192 | the distribution between clients and servers. Till now, every data structure of domain has been kept |
---|
| 193 | like before, e.g: data_n_index to make sure a compability, however, it should be changed? |
---|
| 194 | \param [in] domList List of domains of grid |
---|
| 195 | \param [in] axisList List of axis of grid |
---|
[887] | 196 | \param [in] scalarList List of scalar of grid |
---|
| 197 | \param [in] axisDomainOrder order of axis and domain inside a grid. 2 if domain, 1 if axis and zero if scalar |
---|
[567] | 198 | // \param [in] gridMask Mask of grid, for now, keep it 3 dimension, but it needs changing |
---|
[551] | 199 | */ |
---|
| 200 | void CDistributionClient::readDistributionInfo(const std::vector<CDomain*>& domList, |
---|
| 201 | const std::vector<CAxis*>& axisList, |
---|
[887] | 202 | const std::vector<CScalar*>& scalarList, |
---|
| 203 | const CArray<int,1>& axisDomainOrder) |
---|
[551] | 204 | { |
---|
[567] | 205 | domainNum_ = domList.size(); |
---|
| 206 | axisNum_ = axisList.size(); |
---|
[551] | 207 | numElement_ = axisDomainOrder.numElements(); // Number of element, e.x: Axis, Domain |
---|
| 208 | |
---|
| 209 | axisDomainOrder_.resize(numElement_); |
---|
| 210 | axisDomainOrder_ = axisDomainOrder; |
---|
| 211 | |
---|
| 212 | // Each domain or axis has its mask, of course |
---|
[567] | 213 | domainMasks_.resize(domainNum_); |
---|
| 214 | for (int i = 0; i < domainNum_;++i) |
---|
[551] | 215 | { |
---|
[664] | 216 | domainMasks_[i].resize(domList[i]->mask_1d.numElements()); |
---|
| 217 | domainMasks_[i] = domList[i]->mask_1d; |
---|
[551] | 218 | } |
---|
| 219 | |
---|
[567] | 220 | axisMasks_.resize(axisNum_); |
---|
| 221 | for (int i = 0; i < axisNum_; ++i) |
---|
[551] | 222 | { |
---|
| 223 | axisMasks_[i].resize(axisList[i]->mask.numElements()); |
---|
| 224 | axisMasks_[i] = axisList[i]->mask; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | // Because domain and axis can be in any order (axis1, domain1, axis2, axis3, ) |
---|
| 228 | // their position should be specified. In axisDomainOrder, domain == true, axis == false |
---|
| 229 | int idx = 0; |
---|
| 230 | indexMap_.resize(numElement_); |
---|
| 231 | this->dims_ = numElement_; |
---|
| 232 | for (int i = 0; i < numElement_; ++i) |
---|
| 233 | { |
---|
| 234 | indexMap_[i] = idx; |
---|
[887] | 235 | if (2 == axisDomainOrder(i)) |
---|
[551] | 236 | { |
---|
| 237 | ++(this->dims_); |
---|
| 238 | idx += 2; |
---|
| 239 | } |
---|
[567] | 240 | else ++idx; |
---|
[551] | 241 | } |
---|
| 242 | |
---|
| 243 | // Size of each dimension (local and global) |
---|
| 244 | nLocal_.resize(this->dims_); |
---|
| 245 | nGlob_.resize(this->dims_); |
---|
| 246 | nBeginLocal_.resize(this->dims_,0); |
---|
| 247 | nBeginGlobal_.resize(this->dims_,0); |
---|
| 248 | nZoomBegin_.resize(this->dims_); |
---|
| 249 | nZoomEnd_.resize(this->dims_); |
---|
| 250 | |
---|
| 251 | // Data_n_index of domain or axis (For now, axis uses its size as data_n_index |
---|
| 252 | dataNIndex_.resize(numElement_); |
---|
| 253 | dataDims_.resize(numElement_); |
---|
| 254 | dataBegin_.resize(this->dims_); |
---|
| 255 | |
---|
| 256 | // Data_*_index of each dimension |
---|
| 257 | dataIndex_.resize(this->dims_); |
---|
[818] | 258 | infoIndex_.resize(this->dims_); |
---|
[551] | 259 | |
---|
[861] | 260 | elementLocalIndex_.resize(numElement_); |
---|
| 261 | elementGlobalIndex_.resize(numElement_); |
---|
| 262 | elementIndexData_.resize(numElement_); |
---|
| 263 | elementZoomMask_.resize(numElement_); |
---|
| 264 | elementNLocal_.resize(numElement_); |
---|
| 265 | elementNGlobal_.resize(numElement_); |
---|
[1025] | 266 | |
---|
[861] | 267 | elementNLocal_[0] = 1; |
---|
| 268 | elementNGlobal_[0] = 1; |
---|
[1025] | 269 | isDataDistributed_ = false; |
---|
| 270 | |
---|
[861] | 271 | size_t localSize = 1, globalSize = 1; |
---|
| 272 | |
---|
[1025] | 273 | // A trick to determine position of each domain in domainList |
---|
| 274 | int domIndex = 0, axisIndex = 0, scalarIndex = 0; |
---|
| 275 | idx = 0; |
---|
| 276 | |
---|
| 277 | |
---|
[551] | 278 | // Update all the vectors above |
---|
[861] | 279 | for (idx = 0; idx < numElement_; ++idx) |
---|
[551] | 280 | { |
---|
[887] | 281 | int eleDim = axisDomainOrder(idx); |
---|
[861] | 282 | elementNLocal_[idx] = localSize; |
---|
| 283 | elementNGlobal_[idx] = globalSize; |
---|
[551] | 284 | |
---|
| 285 | // If this is a domain |
---|
[887] | 286 | if (2 == eleDim) |
---|
[551] | 287 | { |
---|
| 288 | // On the j axis |
---|
| 289 | nLocal_.at(indexMap_[idx]+1) = domList[domIndex]->nj.getValue(); |
---|
| 290 | nGlob_.at(indexMap_[idx]+1) = domList[domIndex]->nj_glo.getValue(); |
---|
| 291 | nBeginLocal_.at(indexMap_[idx]+1) = 0; |
---|
| 292 | nBeginGlobal_.at(indexMap_[idx]+1) = domList[domIndex]->jbegin; |
---|
[631] | 293 | nZoomBegin_.at((indexMap_[idx]+1)) = domList[domIndex]->global_zoom_jbegin; |
---|
| 294 | nZoomEnd_.at((indexMap_[idx]+1)) = domList[domIndex]->global_zoom_jbegin + domList[domIndex]->global_zoom_nj-1; |
---|
[551] | 295 | |
---|
[890] | 296 | dataBegin_.at(indexMap_[idx]+1) = domList[domIndex]->data_jbegin.getValue(); |
---|
[887] | 297 | dataIndex_.at(indexMap_[idx]+1).reference(domList[domIndex]->data_j_index); |
---|
| 298 | infoIndex_.at(indexMap_[idx]+1).reference(domList[domIndex]->j_index); |
---|
[551] | 299 | |
---|
| 300 | // On the i axis |
---|
| 301 | nLocal_.at(indexMap_[idx]) = domList[domIndex]->ni.getValue(); |
---|
| 302 | nGlob_.at(indexMap_[idx]) = domList[domIndex]->ni_glo.getValue(); |
---|
| 303 | nBeginLocal_.at(indexMap_[idx]) = 0; |
---|
| 304 | nBeginGlobal_.at(indexMap_[idx]) = domList[domIndex]->ibegin; |
---|
[631] | 305 | nZoomBegin_.at((indexMap_[idx])) = domList[domIndex]->global_zoom_ibegin; |
---|
| 306 | nZoomEnd_.at((indexMap_[idx])) = domList[domIndex]->global_zoom_ibegin + domList[domIndex]->global_zoom_ni-1; |
---|
[551] | 307 | |
---|
| 308 | dataBegin_.at(indexMap_[idx]) = domList[domIndex]->data_ibegin.getValue(); |
---|
[887] | 309 | dataIndex_.at(indexMap_[idx]).reference(domList[domIndex]->data_i_index); |
---|
| 310 | infoIndex_.at(indexMap_[idx]).reference(domList[domIndex]->i_index); |
---|
[551] | 311 | |
---|
[660] | 312 | dataNIndex_.at(idx) = domList[domIndex]->data_i_index.numElements(); |
---|
[551] | 313 | dataDims_.at(idx) = domList[domIndex]->data_dim.getValue(); |
---|
[594] | 314 | |
---|
| 315 | isDataDistributed_ |= domList[domIndex]->isDistributed(); |
---|
| 316 | |
---|
[861] | 317 | localSize *= nLocal_.at(indexMap_[idx]+1)* nLocal_.at(indexMap_[idx]); |
---|
| 318 | globalSize *= nGlob_.at(indexMap_[idx]+1)* nGlob_.at(indexMap_[idx]); |
---|
[551] | 319 | ++domIndex; |
---|
| 320 | } |
---|
[887] | 321 | else if (1 == eleDim)// So it's an axis |
---|
[551] | 322 | { |
---|
[666] | 323 | nLocal_.at(indexMap_[idx]) = axisList[axisIndex]->n.getValue(); |
---|
| 324 | nGlob_.at(indexMap_[idx]) = axisList[axisIndex]->n_glo.getValue(); |
---|
[553] | 325 | nBeginLocal_.at(indexMap_[idx]) = 0; |
---|
[666] | 326 | nBeginGlobal_.at(indexMap_[idx]) = axisList[axisIndex]->begin.getValue(); |
---|
[623] | 327 | nZoomBegin_.at((indexMap_[idx])) = axisList[axisIndex]->global_zoom_begin; |
---|
[821] | 328 | nZoomEnd_.at((indexMap_[idx])) = axisList[axisIndex]->global_zoom_begin + axisList[axisIndex]->global_zoom_n-1; |
---|
[551] | 329 | |
---|
| 330 | dataBegin_.at(indexMap_[idx]) = axisList[axisIndex]->data_begin.getValue(); |
---|
[887] | 331 | dataIndex_.at(indexMap_[idx]).reference(axisList[axisIndex]->data_index); |
---|
| 332 | infoIndex_.at(indexMap_[idx]).reference(axisList[axisIndex]->index); |
---|
[551] | 333 | dataNIndex_.at(idx) = axisList[axisIndex]->data_index.numElements(); |
---|
| 334 | dataDims_.at(idx) = 1; |
---|
[594] | 335 | |
---|
| 336 | isDataDistributed_ |= axisList[axisIndex]->isDistributed(); |
---|
| 337 | |
---|
[861] | 338 | localSize *= nLocal_.at(indexMap_[idx]); |
---|
| 339 | globalSize *= nGlob_.at(indexMap_[idx]); |
---|
| 340 | |
---|
[551] | 341 | ++axisIndex; |
---|
| 342 | } |
---|
[887] | 343 | else // scalar |
---|
| 344 | { |
---|
| 345 | nLocal_.at(indexMap_[idx]) = 1; |
---|
| 346 | nGlob_.at(indexMap_[idx]) = 1; |
---|
| 347 | nBeginLocal_.at(indexMap_[idx]) = 0; |
---|
| 348 | nBeginGlobal_.at(indexMap_[idx]) = 1; |
---|
| 349 | nZoomBegin_.at((indexMap_[idx])) = 0; |
---|
| 350 | nZoomEnd_.at((indexMap_[idx])) = 0; |
---|
| 351 | |
---|
| 352 | dataBegin_.at(indexMap_[idx]) = 0; |
---|
| 353 | dataIndex_.at(indexMap_[idx]).resize(1); dataIndex_.at(indexMap_[idx])(0) = 0; |
---|
| 354 | infoIndex_.at(indexMap_[idx]).resize(1); infoIndex_.at(indexMap_[idx])(0) = 0; |
---|
| 355 | dataNIndex_.at(idx) = 1; |
---|
| 356 | dataDims_.at(idx) = 1; |
---|
| 357 | |
---|
| 358 | isDataDistributed_ |= false; |
---|
| 359 | |
---|
| 360 | localSize *= nLocal_.at(indexMap_[idx]); |
---|
| 361 | globalSize *= nGlob_.at(indexMap_[idx]); |
---|
| 362 | |
---|
| 363 | ++scalarIndex; |
---|
| 364 | } |
---|
[551] | 365 | } |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | /*! |
---|
| 369 | Create local index of domain(s). |
---|
| 370 | A domain can have data index which even contains the "ghost" points. Very often, these |
---|
| 371 | data surround the true data. In order to send correct data to server, |
---|
| 372 | a client need to know index of the true data. |
---|
| 373 | */ |
---|
| 374 | void CDistributionClient::createLocalDomainDataIndex() |
---|
| 375 | { |
---|
| 376 | int idxDomain = 0; |
---|
| 377 | for (int i = 0; i < axisDomainOrder_.numElements(); ++i) |
---|
| 378 | { |
---|
[887] | 379 | if (2 == axisDomainOrder_(i)) |
---|
[551] | 380 | { |
---|
[861] | 381 | elementIndexData_[i].resize(dataNIndex_[i]); |
---|
| 382 | elementIndexData_[i] = false; |
---|
| 383 | int iIdx, jIdx = 0, count = 0, localIndex; |
---|
[551] | 384 | for (int j = 0; j < dataNIndex_[i]; ++j) |
---|
| 385 | { |
---|
[887] | 386 | iIdx = getDomainIndex((dataIndex_[indexMap_[i]])(j), (dataIndex_[indexMap_[i]+1])(j), |
---|
[551] | 387 | dataBegin_[indexMap_[i]], dataBegin_[indexMap_[i]+1], |
---|
| 388 | dataDims_[i], nLocal_[indexMap_[i]], jIdx); |
---|
| 389 | |
---|
| 390 | if ((iIdx >= nBeginLocal_[indexMap_[i]]) && (iIdx < nLocal_[indexMap_[i]]) && |
---|
| 391 | (jIdx >= nBeginLocal_[indexMap_[i]+1]) && (jIdx < nLocal_[indexMap_[i]+1]) && |
---|
[664] | 392 | (domainMasks_[idxDomain](iIdx + jIdx*nLocal_[indexMap_[i]]))) |
---|
[551] | 393 | { |
---|
[861] | 394 | ++count; |
---|
| 395 | elementIndexData_[i](j) = true; |
---|
[551] | 396 | } |
---|
| 397 | } |
---|
[861] | 398 | |
---|
| 399 | elementLocalIndex_[i].resize(count); |
---|
| 400 | elementGlobalIndex_[i].resize(count); |
---|
| 401 | elementZoomMask_[i].resize(count); |
---|
| 402 | elementZoomMask_[i] = false; |
---|
| 403 | count = 0; |
---|
| 404 | CArray<bool,1>& tmpIndexElementData = elementIndexData_[i]; |
---|
| 405 | CArray<bool,1>& tmpZoomMaskElement = elementZoomMask_[i]; |
---|
| 406 | CArray<int,1>& tmpLocalElementIndex = elementLocalIndex_[i]; |
---|
| 407 | CArray<size_t,1>& tmpGlobalElementIndex = elementGlobalIndex_[i]; |
---|
| 408 | for (int j = 0; j < dataNIndex_[i]; ++j) |
---|
| 409 | { |
---|
| 410 | if (tmpIndexElementData(j)) |
---|
| 411 | { |
---|
[887] | 412 | iIdx = getDomainIndex((dataIndex_[indexMap_[i]])(j), (dataIndex_[indexMap_[i]+1])(j), |
---|
[861] | 413 | dataBegin_[indexMap_[i]], dataBegin_[indexMap_[i]+1], |
---|
| 414 | dataDims_[i], nLocal_[indexMap_[i]], jIdx); |
---|
| 415 | localIndex = tmpLocalElementIndex(count) = iIdx + jIdx * nLocal_[indexMap_[i]]; |
---|
[887] | 416 | tmpGlobalElementIndex(count) = (infoIndex_[indexMap_[i]])(localIndex) + ((infoIndex_[indexMap_[i]+1])(localIndex))*nGlob_[indexMap_[i]]; |
---|
| 417 | if ((((infoIndex_[indexMap_[i]])(localIndex)) <= nZoomEnd_[indexMap_[i]]) |
---|
| 418 | && (nZoomBegin_[indexMap_[i]] <= ((infoIndex_[indexMap_[i]])(localIndex))) |
---|
| 419 | && (((infoIndex_[indexMap_[i]+1])(localIndex)) <= nZoomEnd_[indexMap_[i]+1]) |
---|
| 420 | && (nZoomBegin_[indexMap_[i]+1] <= ((infoIndex_[indexMap_[i]+1])(localIndex)))) |
---|
[861] | 421 | { |
---|
| 422 | tmpZoomMaskElement(count) = true; |
---|
| 423 | } |
---|
| 424 | ++count; |
---|
| 425 | } |
---|
| 426 | } |
---|
[551] | 427 | ++idxDomain; |
---|
| 428 | } |
---|
| 429 | } |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | /*! |
---|
| 433 | Create local index of axis. |
---|
| 434 | */ |
---|
| 435 | void CDistributionClient::createLocalAxisDataIndex() |
---|
| 436 | { |
---|
| 437 | int idxAxis = 0; |
---|
| 438 | for (int i = 0; i < axisDomainOrder_.numElements(); ++i) |
---|
| 439 | { |
---|
[887] | 440 | if (1 == axisDomainOrder_(i)) |
---|
[551] | 441 | { |
---|
[861] | 442 | elementIndexData_[i].resize(dataNIndex_[i]); |
---|
| 443 | elementIndexData_[i] = false; |
---|
| 444 | int iIdx = 0, count = 0, localIndex = 0; |
---|
[551] | 445 | for (int j = 0; j < dataNIndex_[i]; ++j) |
---|
| 446 | { |
---|
[887] | 447 | iIdx = getAxisIndex((dataIndex_[indexMap_[i]])(j), dataBegin_[indexMap_[i]], nLocal_[indexMap_[i]]); |
---|
[551] | 448 | if ((iIdx >= nBeginLocal_[indexMap_[i]]) && |
---|
| 449 | (iIdx < nLocal_[indexMap_[i]]) && (axisMasks_[idxAxis](iIdx))) |
---|
| 450 | { |
---|
[861] | 451 | ++count; |
---|
| 452 | elementIndexData_[i](j) = true; |
---|
[551] | 453 | } |
---|
| 454 | } |
---|
| 455 | |
---|
[861] | 456 | elementLocalIndex_[i].resize(count); |
---|
| 457 | elementGlobalIndex_[i].resize(count); |
---|
| 458 | elementZoomMask_[i].resize(count); |
---|
| 459 | elementZoomMask_[i] = false; |
---|
| 460 | count = 0; |
---|
| 461 | CArray<bool,1>& tmpIndexElementData = elementIndexData_[i]; |
---|
| 462 | CArray<bool,1>& tmpZoomMaskElement = elementZoomMask_[i]; |
---|
| 463 | CArray<int,1>& tmpLocalElementIndex = elementLocalIndex_[i]; |
---|
| 464 | CArray<size_t,1>& tmpGlobalElementIndex = elementGlobalIndex_[i]; |
---|
| 465 | for (int j = 0; j < dataNIndex_[i]; ++j) |
---|
[584] | 466 | { |
---|
[861] | 467 | if (tmpIndexElementData(j)) |
---|
[818] | 468 | { |
---|
[887] | 469 | iIdx = tmpLocalElementIndex(count) = getAxisIndex((dataIndex_[indexMap_[i]])(j), dataBegin_[indexMap_[i]], nLocal_[indexMap_[i]]); |
---|
| 470 | tmpGlobalElementIndex(count) = (infoIndex_[indexMap_[i]])(iIdx); |
---|
| 471 | if ((((infoIndex_[indexMap_[i]])(iIdx)) <= nZoomEnd_[indexMap_[i]]) |
---|
| 472 | && (nZoomBegin_[indexMap_[i]] <= ((infoIndex_[indexMap_[i]])(iIdx)))) |
---|
[818] | 473 | { |
---|
[861] | 474 | tmpZoomMaskElement(count) = true; |
---|
[818] | 475 | } |
---|
[861] | 476 | ++count; |
---|
[818] | 477 | } |
---|
[584] | 478 | } |
---|
[861] | 479 | ++idxAxis; |
---|
[584] | 480 | } |
---|
| 481 | } |
---|
| 482 | } |
---|
| 483 | |
---|
[551] | 484 | /*! |
---|
[887] | 485 | Create local index of scalar. |
---|
| 486 | */ |
---|
| 487 | void CDistributionClient::createLocalScalarDataIndex() |
---|
| 488 | { |
---|
| 489 | int idxAxis = 0; |
---|
| 490 | for (int i = 0; i < axisDomainOrder_.numElements(); ++i) |
---|
| 491 | { |
---|
| 492 | if (0 == axisDomainOrder_(i)) |
---|
| 493 | { |
---|
| 494 | elementIndexData_[i].resize(dataNIndex_[i]); |
---|
| 495 | elementIndexData_[i] = true; |
---|
| 496 | int count = 1; |
---|
| 497 | |
---|
| 498 | elementLocalIndex_[i].resize(count); |
---|
| 499 | elementLocalIndex_[i] = 0; |
---|
| 500 | elementGlobalIndex_[i].resize(count); |
---|
| 501 | elementGlobalIndex_[i] = 0; |
---|
| 502 | elementZoomMask_[i].resize(count); |
---|
| 503 | elementZoomMask_[i] = true; |
---|
| 504 | } |
---|
| 505 | } |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | /*! |
---|
[551] | 509 | Create global index on client |
---|
| 510 | In order to do the mapping between client-server, each client creates its own |
---|
| 511 | global index of sending data. This global index is then used to calculate to which server |
---|
| 512 | the client needs to send it data as well as which part of data belongs to the server. |
---|
| 513 | So as to make clients and server coherent in order of index, global index is calculated by |
---|
| 514 | take into account of C-convention, the rightmost dimension varies faster. |
---|
| 515 | */ |
---|
[584] | 516 | void CDistributionClient::createGlobalIndexSendToServer() |
---|
[551] | 517 | { |
---|
[834] | 518 | if (isComputed_) return; |
---|
| 519 | isComputed_ = true; |
---|
[551] | 520 | createLocalDomainDataIndex(); |
---|
| 521 | createLocalAxisDataIndex(); |
---|
[888] | 522 | createLocalScalarDataIndex(); |
---|
[551] | 523 | |
---|
| 524 | int idxDomain = 0, idxAxis = 0; |
---|
| 525 | std::vector<int> eachElementSize(numElement_); |
---|
| 526 | |
---|
| 527 | // Precompute size of the loop |
---|
| 528 | for (int i = 0; i < numElement_; ++i) |
---|
| 529 | { |
---|
[861] | 530 | eachElementSize[i] = elementLocalIndex_[i].numElements(); |
---|
[551] | 531 | } |
---|
| 532 | |
---|
| 533 | // Compute size of the global index on client |
---|
[623] | 534 | std::vector<StdSize> idxLoop(numElement_,0); |
---|
[861] | 535 | std::vector<StdSize> currentIndex(numElement_,0); |
---|
| 536 | std::vector<StdSize> currentGlobalIndex(numElement_,0); |
---|
[551] | 537 | int innerLoopSize = eachElementSize[0]; |
---|
| 538 | size_t idx = 0, indexLocalDataOnClientCount = 0, indexSend2ServerCount = 0; |
---|
| 539 | size_t ssize = 1; |
---|
| 540 | for (int i = 0; i < numElement_; ++i) ssize *= eachElementSize[i]; |
---|
| 541 | while (idx < ssize) |
---|
| 542 | { |
---|
| 543 | for (int i = 0; i < numElement_-1; ++i) |
---|
| 544 | { |
---|
| 545 | if (idxLoop[i] == eachElementSize[i]) |
---|
| 546 | { |
---|
| 547 | idxLoop[i] = 0; |
---|
| 548 | ++idxLoop[i+1]; |
---|
| 549 | } |
---|
| 550 | } |
---|
| 551 | |
---|
[553] | 552 | // Find out outer index |
---|
[567] | 553 | // Depending the inner-most element is axis or domain, |
---|
| 554 | // The outer loop index begins correspondingly at one (1) or zero (0) |
---|
[551] | 555 | for (int i = 1; i < numElement_; ++i) |
---|
| 556 | { |
---|
[861] | 557 | currentIndex[i] = elementLocalIndex_[i](idxLoop[i]); |
---|
[551] | 558 | } |
---|
| 559 | |
---|
| 560 | // Inner most index |
---|
| 561 | for (int i = 0; i < innerLoopSize; ++i) |
---|
| 562 | { |
---|
[861] | 563 | int gridMaskIndex = 0; |
---|
[864] | 564 | currentIndex[0] = elementLocalIndex_[0](i); |
---|
[861] | 565 | for (int k = 0; k < this->numElement_; ++k) |
---|
[551] | 566 | { |
---|
[861] | 567 | gridMaskIndex += (currentIndex[k])*elementNLocal_[k]; |
---|
[551] | 568 | } |
---|
| 569 | |
---|
[818] | 570 | if (gridMask_(gridMaskIndex)) |
---|
[567] | 571 | { |
---|
[551] | 572 | ++indexLocalDataOnClientCount; |
---|
| 573 | bool isIndexOnServer = true; |
---|
[818] | 574 | |
---|
| 575 | for (int idxElement = 0; idxElement < this->numElement_; ++idxElement) |
---|
| 576 | { |
---|
[861] | 577 | isIndexOnServer = isIndexOnServer && elementZoomMask_[idxElement](idxLoop[idxElement]); |
---|
[818] | 578 | } |
---|
[551] | 579 | if (isIndexOnServer) ++indexSend2ServerCount; |
---|
| 580 | } |
---|
| 581 | } |
---|
| 582 | idxLoop[0] += innerLoopSize; |
---|
| 583 | idx += innerLoopSize; |
---|
| 584 | } |
---|
| 585 | |
---|
[553] | 586 | // Now allocate these arrays |
---|
[653] | 587 | localDataIndex_.resize(indexLocalDataOnClientCount); |
---|
| 588 | localMaskIndex_.resize(indexSend2ServerCount); |
---|
[843] | 589 | globalLocalDataSendToServerMap_.rehash(std::ceil(indexSend2ServerCount/globalLocalDataSendToServerMap_.max_load_factor())); //globalLocalDataSendToServerMap_.reserve(indexSend2ServerCount); |
---|
[551] | 590 | |
---|
[553] | 591 | // We need to loop with data index |
---|
[551] | 592 | idxLoop.assign(numElement_,0); |
---|
| 593 | idx = indexLocalDataOnClientCount = indexSend2ServerCount = 0; |
---|
[567] | 594 | ssize = 1; for (int i = 0; i < numElement_; ++i) ssize *= dataNIndex_[i]; |
---|
| 595 | innerLoopSize = dataNIndex_[0]; |
---|
| 596 | int countLocalData = 0; |
---|
[874] | 597 | std::vector<int> correctIndexOfElement(numElement_,0); |
---|
[623] | 598 | bool isOuterIndexCorrect = true; |
---|
[551] | 599 | while (idx < ssize) |
---|
| 600 | { |
---|
| 601 | for (int i = 0; i < numElement_-1; ++i) |
---|
| 602 | { |
---|
[553] | 603 | if (idxLoop[i] == dataNIndex_[i]) |
---|
[551] | 604 | { |
---|
| 605 | idxLoop[i] = 0; |
---|
[874] | 606 | correctIndexOfElement[i] = 0; |
---|
[551] | 607 | ++idxLoop[i+1]; |
---|
[874] | 608 | if (isOuterIndexCorrect) ++correctIndexOfElement[i+1]; |
---|
[551] | 609 | } |
---|
| 610 | } |
---|
| 611 | |
---|
[567] | 612 | // Depending the inner-most element axis or domain, |
---|
| 613 | // The outer loop index begins correspondingly at one (1) or zero (0) |
---|
[861] | 614 | bool isIndexElementDataCorrect = true; |
---|
[551] | 615 | for (int i = 1; i < numElement_; ++i) |
---|
| 616 | { |
---|
[861] | 617 | if (elementIndexData_[i](idxLoop[i])) |
---|
[551] | 618 | { |
---|
[874] | 619 | currentIndex[i] = elementLocalIndex_[i](correctIndexOfElement[i]); |
---|
| 620 | currentGlobalIndex[i] = elementGlobalIndex_[i](correctIndexOfElement[i]); |
---|
[861] | 621 | isIndexElementDataCorrect &= true; |
---|
[551] | 622 | } |
---|
[861] | 623 | else isIndexElementDataCorrect = false; |
---|
[551] | 624 | } |
---|
| 625 | |
---|
[861] | 626 | isOuterIndexCorrect = isIndexElementDataCorrect; |
---|
[623] | 627 | |
---|
[874] | 628 | if (isOuterIndexCorrect) |
---|
[551] | 629 | { |
---|
[874] | 630 | // Inner most index |
---|
| 631 | int correctIndexInnerElement = 0; |
---|
| 632 | for (int i = 0; i < innerLoopSize; ++i) |
---|
[861] | 633 | { |
---|
[874] | 634 | bool isCurrentIndexDataCorrect = isOuterIndexCorrect; |
---|
| 635 | if (elementIndexData_[0](i)) |
---|
[551] | 636 | { |
---|
[874] | 637 | currentIndex[0] = elementLocalIndex_[0](correctIndexInnerElement); |
---|
| 638 | currentGlobalIndex[0] = elementGlobalIndex_[0](correctIndexInnerElement); |
---|
| 639 | isCurrentIndexDataCorrect &= true; |
---|
| 640 | ++correctIndexInnerElement; |
---|
[553] | 641 | } |
---|
[874] | 642 | else isCurrentIndexDataCorrect = false; |
---|
[861] | 643 | |
---|
[874] | 644 | if (isCurrentIndexDataCorrect) |
---|
[553] | 645 | { |
---|
[874] | 646 | int gridMaskIndex = 0; |
---|
| 647 | for (int k = 0; k < this->numElement_; ++k) |
---|
[861] | 648 | { |
---|
[874] | 649 | gridMaskIndex += (currentIndex[k])*elementNLocal_[k]; |
---|
[861] | 650 | } |
---|
| 651 | |
---|
[874] | 652 | if (gridMask_(gridMaskIndex)) |
---|
[861] | 653 | { |
---|
[874] | 654 | localDataIndex_[indexLocalDataOnClientCount] = countLocalData; |
---|
| 655 | bool isIndexOnServer = true; |
---|
| 656 | for (int idxElement = 0; idxElement < this->numElement_; ++idxElement) |
---|
[861] | 657 | { |
---|
[874] | 658 | isIndexOnServer = isIndexOnServer && elementZoomMask_[idxElement](correctIndexOfElement[idxElement]); |
---|
[861] | 659 | } |
---|
[874] | 660 | |
---|
| 661 | if (isIndexOnServer) |
---|
| 662 | { |
---|
| 663 | size_t globalIndex = 0; |
---|
| 664 | for (int k = 0; k < numElement_; ++k) |
---|
| 665 | { |
---|
| 666 | globalIndex += (currentGlobalIndex[k])*elementNGlobal_[k]; |
---|
| 667 | } |
---|
| 668 | globalLocalDataSendToServerMap_[globalIndex] = indexLocalDataOnClientCount; |
---|
| 669 | localMaskIndex_[indexSend2ServerCount] = gridMaskIndex; |
---|
| 670 | ++indexSend2ServerCount; |
---|
| 671 | } |
---|
| 672 | ++indexLocalDataOnClientCount; |
---|
[861] | 673 | } |
---|
[553] | 674 | } |
---|
[874] | 675 | ++countLocalData; |
---|
| 676 | correctIndexOfElement[0] = correctIndexInnerElement;; |
---|
[551] | 677 | } |
---|
[861] | 678 | } |
---|
| 679 | idxLoop[0] += innerLoopSize; |
---|
| 680 | idx += innerLoopSize; |
---|
| 681 | } |
---|
| 682 | } |
---|
[551] | 683 | |
---|
[861] | 684 | void CDistributionClient::createGlobalIndex() |
---|
| 685 | { |
---|
[551] | 686 | } |
---|
| 687 | |
---|
| 688 | /*! |
---|
| 689 | Retrieve index i and index j of a domain from its data index |
---|
| 690 | Data contains not only true data, which are sent to servers, but also ghost data, which |
---|
| 691 | very often play a role of border of each local data, so does data index. Because data of a domain |
---|
| 692 | can be one dimension, or two dimensions, there is a need to convert data index to domain index |
---|
| 693 | \param [in] dataIIndex index of i data |
---|
| 694 | \param [in] dataJIndex index of j data |
---|
| 695 | \param [in] dataIBegin index begin of i data |
---|
| 696 | \param [in] dataJBegin index begin of j data |
---|
| 697 | \param [in] dataDim dimension of data (1 or 2) |
---|
| 698 | \param [in] ni local size ni of domain |
---|
| 699 | \param [out] j j index of domain |
---|
| 700 | \return i index of domain |
---|
| 701 | */ |
---|
| 702 | int CDistributionClient::getDomainIndex(const int& dataIIndex, const int& dataJIndex, |
---|
| 703 | const int& dataIBegin, const int& dataJBegin, |
---|
| 704 | const int& dataDim, const int& ni, int& j) |
---|
| 705 | { |
---|
| 706 | int tempI = dataIIndex + dataIBegin, |
---|
[660] | 707 | tempJ = (dataJIndex + dataJBegin); |
---|
| 708 | int i = (dataDim == 1) ? (tempI) % ni |
---|
| 709 | : (tempI) ; |
---|
| 710 | j = (dataDim == 1) ? (tempI) / ni |
---|
| 711 | : (tempJ) ; |
---|
[551] | 712 | |
---|
| 713 | return i; |
---|
| 714 | } |
---|
| 715 | |
---|
| 716 | /*! |
---|
| 717 | Retrieve index of an axis from its data index |
---|
| 718 | \param [in] dataIndex index of data |
---|
| 719 | \param [in] dataBegin index begin of data |
---|
| 720 | \param [in] ni local size of axis |
---|
| 721 | \return index of domain |
---|
| 722 | */ |
---|
| 723 | int CDistributionClient::getAxisIndex(const int& dataIndex, const int& dataBegin, const int& ni) |
---|
| 724 | { |
---|
| 725 | int tempI = dataIndex + dataBegin; |
---|
[666] | 726 | return ((tempI)%ni); |
---|
[551] | 727 | } |
---|
| 728 | |
---|
[831] | 729 | /*! |
---|
| 730 | Return global local data mapping of client |
---|
| 731 | */ |
---|
[861] | 732 | CDistributionClient::GlobalLocalDataMap& CDistributionClient::getGlobalLocalDataSendToServer() |
---|
[584] | 733 | { |
---|
[834] | 734 | if (!isComputed_) createGlobalIndexSendToServer(); |
---|
[831] | 735 | return globalLocalDataSendToServerMap_; |
---|
[584] | 736 | } |
---|
| 737 | |
---|
[551] | 738 | /*! |
---|
| 739 | Return local data index of client |
---|
| 740 | */ |
---|
[834] | 741 | const std::vector<int>& CDistributionClient::getLocalDataIndexOnClient() |
---|
[551] | 742 | { |
---|
[834] | 743 | if (!isComputed_) createGlobalIndexSendToServer(); |
---|
[653] | 744 | return localDataIndex_; |
---|
[551] | 745 | } |
---|
| 746 | |
---|
[569] | 747 | /*! |
---|
[623] | 748 | Return local mask index of client |
---|
| 749 | */ |
---|
[834] | 750 | const std::vector<int>& CDistributionClient::getLocalMaskIndexOnClient() |
---|
[623] | 751 | { |
---|
[834] | 752 | if (!isComputed_) createGlobalIndexSendToServer(); |
---|
[653] | 753 | return localMaskIndex_; |
---|
[623] | 754 | } |
---|
| 755 | |
---|
[551] | 756 | } // namespace xios |
---|