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