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