[631] | 1 | /*! |
---|
| 2 | \file domain_algorithm_transformation.hpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 02 Jul 2015 |
---|
| 5 | \date 02 Jul 2015 |
---|
| 6 | |
---|
| 7 | \brief Interface for all domain transformation algorithms. |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include "domain_algorithm_transformation.hpp" |
---|
[862] | 11 | #include "context.hpp" |
---|
| 12 | #include "context_client.hpp" |
---|
[866] | 13 | #include "domain.hpp" |
---|
[631] | 14 | |
---|
| 15 | namespace xios { |
---|
| 16 | |
---|
| 17 | CDomainAlgorithmTransformation::CDomainAlgorithmTransformation(CDomain* domainDestination, CDomain* domainSource) |
---|
| 18 | : CGenericAlgorithmTransformation(), domainDest_(domainDestination), domainSrc_(domainSource) |
---|
| 19 | { |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | CDomainAlgorithmTransformation::~CDomainAlgorithmTransformation() |
---|
| 23 | { |
---|
| 24 | } |
---|
| 25 | |
---|
[827] | 26 | void CDomainAlgorithmTransformation::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
[631] | 27 | { |
---|
| 28 | } |
---|
| 29 | |
---|
[862] | 30 | void CDomainAlgorithmTransformation::computeExchangeGlobalIndex(const CArray<size_t,1>& globalDomainIndex, |
---|
[866] | 31 | CClientClientDHTInt::Index2VectorInfoTypeMap& globalDomainIndexOnProc) |
---|
[862] | 32 | { |
---|
| 33 | CContext* context = CContext::getCurrent(); |
---|
| 34 | CContextClient* client=context->client; |
---|
| 35 | int clientRank = client->clientRank; |
---|
| 36 | int clientSize = client->clientSize; |
---|
| 37 | |
---|
| 38 | int niGlob = domainSrc_->ni_glo.getValue(); |
---|
| 39 | int njGlob = domainSrc_->nj_glo.getValue(); |
---|
| 40 | size_t globalIndex; |
---|
| 41 | int nIndexSize = domainSrc_->i_index.numElements(), i_ind, j_ind; |
---|
| 42 | CClientClientDHTInt::Index2VectorInfoTypeMap globalIndex2ProcRank; |
---|
| 43 | globalIndex2ProcRank.rehash(std::ceil(nIndexSize/globalIndex2ProcRank.max_load_factor())); |
---|
| 44 | for (int idx = 0; idx < nIndexSize; ++idx) |
---|
| 45 | { |
---|
| 46 | i_ind=domainSrc_->i_index(idx) ; |
---|
| 47 | j_ind=domainSrc_->j_index(idx) ; |
---|
| 48 | |
---|
| 49 | globalIndex = i_ind + j_ind * niGlob; |
---|
| 50 | globalIndex2ProcRank[globalIndex].push_back(clientRank); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | CClientClientDHTInt dhtIndexProcRank(globalIndex2ProcRank, client->intraComm); |
---|
| 54 | dhtIndexProcRank.computeIndexInfoMapping(globalDomainIndex); |
---|
[866] | 55 | globalDomainIndexOnProc = dhtIndexProcRank.getInfoIndexMap(); |
---|
[862] | 56 | |
---|
| 57 | |
---|
[866] | 58 | // std::vector<int> countIndex(clientSize,0); |
---|
| 59 | // const CClientClientDHTInt::Index2VectorInfoTypeMap& computedGlobalIndexOnProc = dhtIndexProcRank.getInfoIndexMap(); |
---|
| 60 | // CClientClientDHTInt::Index2VectorInfoTypeMap::const_iterator itb = computedGlobalIndexOnProc.begin(), it, |
---|
| 61 | // ite = computedGlobalIndexOnProc.end(); |
---|
| 62 | // for (it = itb; it != ite; ++it) |
---|
| 63 | // { |
---|
| 64 | // const std::vector<int>& procList = it->second; |
---|
| 65 | // for (int idx = 0; idx < procList.size(); ++idx) ++countIndex[procList[idx]]; |
---|
| 66 | // } |
---|
| 67 | // |
---|
| 68 | // globalDomainIndexOnProc.rehash(std::ceil(clientSize/globalDomainIndexOnProc.max_load_factor())); |
---|
| 69 | // for (int idx = 0; idx < clientSize; ++idx) |
---|
| 70 | // { |
---|
| 71 | // if (0 != countIndex[idx]) |
---|
| 72 | // { |
---|
| 73 | // globalDomainIndexOnProc[idx].resize(countIndex[idx]); |
---|
| 74 | // countIndex[idx] = 0; |
---|
| 75 | // } |
---|
| 76 | // } |
---|
| 77 | // |
---|
| 78 | // for (it = itb; it != ite; ++it) |
---|
| 79 | // { |
---|
| 80 | // const std::vector<int>& procList = it->second; |
---|
| 81 | // for (int idx = 0; idx < procList.size(); ++idx) |
---|
| 82 | // { |
---|
| 83 | // globalDomainIndexOnProc[procList[idx]][countIndex[procList[idx]]] = it->first; |
---|
| 84 | // ++countIndex[procList[idx]]; |
---|
| 85 | // } |
---|
| 86 | // } |
---|
[862] | 87 | } |
---|
| 88 | |
---|
[631] | 89 | /*! |
---|
| 90 | Compute an array of global index from a global index on a domain |
---|
| 91 | \param[in] domainDestGlobalIndex global index on an domain of destination grid |
---|
| 92 | \param[in] domainSrcGlobalIndex global index on an domain of source grid (which are needed by one index on domain destination) |
---|
| 93 | \param[in] domainPositionInGrid position of the domain in the grid |
---|
| 94 | \param[in] gridDestGlobalDim dimension size of destination grid (it should share the same size for all dimension, maybe except the domain on which transformation is performed) |
---|
| 95 | \param[in] gridSrcGlobalDim dimension size of source grid (it should share the same size for all dimension, maybe except the domain on which transformation is performed) |
---|
[668] | 96 | \param[in] globalIndexGridDestSendToServer global index of destination grid which are to be sent to server(s), this array is already acsending sorted |
---|
[631] | 97 | \param[in/out] globalIndexDestGrid array of global index (for 2d grid, this array is a line, for 3d, this array represents a plan). It should be preallocated |
---|
| 98 | \param[in/out] globalIndexSrcGrid array of global index of source grid (for 2d grid, this array is a line, for 3d, this array represents a plan). It should be preallocated |
---|
| 99 | */ |
---|
| 100 | void CDomainAlgorithmTransformation::computeGlobalGridIndexFromGlobalIndexElement(int domainDestGlobalIndex, |
---|
| 101 | const std::vector<int>& domainSrcGlobalIndex, |
---|
[827] | 102 | const std::vector<int>& destGlobalIndexPositionInGrid, |
---|
[631] | 103 | int domainPositionInGrid, |
---|
| 104 | const std::vector<int>& gridDestGlobalDim, |
---|
| 105 | const std::vector<int>& gridSrcGlobalDim, |
---|
[829] | 106 | const GlobalLocalMap& globalLocalIndexDestSendToServerMap, |
---|
| 107 | std::vector<std::pair<size_t,int> >& globalLocalIndexDestMap, |
---|
[631] | 108 | std::vector<std::vector<size_t> >& globalIndexSrcGrid) |
---|
| 109 | { |
---|
| 110 | int globalDim = gridDestGlobalDim.size(); |
---|
| 111 | int numElement = globalDim - 1; |
---|
| 112 | int domainElementPosition = 0; |
---|
| 113 | std::vector<int> currentIndex(globalDim); |
---|
| 114 | std::vector<int> gridDomainGlobalDim(numElement), indexMap(numElement); |
---|
| 115 | std::vector<int> idxLoop(numElement, 0), domainSrcGlobalDim(2), domainDestGlobalDim(2); |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | size_t ssize = 1, idx = 0, realGlobalIndexSize = 0; |
---|
| 119 | for (int i = 0; i< globalDim; ++i) |
---|
| 120 | { |
---|
| 121 | if (domainPositionInGrid == i) continue; |
---|
| 122 | if (domainPositionInGrid == (i+1)) |
---|
| 123 | { |
---|
| 124 | domainDestGlobalDim[0] = gridDestGlobalDim[i]; |
---|
| 125 | domainDestGlobalDim[1] = gridDestGlobalDim[i+1]; |
---|
| 126 | domainSrcGlobalDim[0] = gridSrcGlobalDim[i]; |
---|
| 127 | domainSrcGlobalDim[1] = gridSrcGlobalDim[i+1]; |
---|
| 128 | gridDomainGlobalDim[idx] = 1; |
---|
| 129 | domainElementPosition = idx; |
---|
| 130 | } |
---|
| 131 | else |
---|
| 132 | { |
---|
| 133 | gridDomainGlobalDim[idx] = gridDestGlobalDim[i]; |
---|
| 134 | } |
---|
| 135 | indexMap[idx] = i; |
---|
| 136 | ++idx; |
---|
| 137 | } |
---|
[663] | 138 | int iIndex = domainDestGlobalIndex % domainDestGlobalDim[0]; |
---|
[631] | 139 | int jIndex = domainDestGlobalIndex / domainDestGlobalDim[0]; |
---|
| 140 | |
---|
| 141 | for (int i = 0; i< numElement; ++i) ssize *= gridDomainGlobalDim[i]; |
---|
| 142 | |
---|
[829] | 143 | GlobalLocalMap::const_iterator iteArr = globalLocalIndexDestSendToServerMap.end(), it; |
---|
| 144 | |
---|
[631] | 145 | idx = 0; |
---|
| 146 | while (idx < ssize) |
---|
| 147 | { |
---|
| 148 | for (int i = 0; i < numElement; ++i) |
---|
| 149 | { |
---|
| 150 | if (idxLoop[i] == gridDomainGlobalDim[i]) |
---|
| 151 | { |
---|
| 152 | idxLoop[i] = 0; |
---|
| 153 | ++idxLoop[i+1]; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | for (int i = 0; i < numElement; ++i) |
---|
| 158 | { |
---|
| 159 | if (domainElementPosition == i) |
---|
| 160 | { |
---|
| 161 | currentIndex[indexMap[i]] = iIndex; |
---|
| 162 | currentIndex[indexMap[i]+1] = jIndex; |
---|
| 163 | } |
---|
| 164 | else |
---|
| 165 | currentIndex[indexMap[i]] = idxLoop[i]; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | size_t globIndex = currentIndex[0]; |
---|
| 169 | size_t mulDim = 1; |
---|
| 170 | for (int k = 1; k < globalDim; ++k) |
---|
| 171 | { |
---|
| 172 | mulDim *= gridDestGlobalDim[k-1]; |
---|
| 173 | globIndex += (currentIndex[k])*mulDim; |
---|
| 174 | } |
---|
| 175 | |
---|
[829] | 176 | if (iteArr != globalLocalIndexDestSendToServerMap.find(globIndex)) ++realGlobalIndexSize; |
---|
[631] | 177 | ++idxLoop[0]; |
---|
| 178 | ++idx; |
---|
| 179 | } |
---|
| 180 | |
---|
[829] | 181 | if (globalLocalIndexDestMap.size() != realGlobalIndexSize) |
---|
| 182 | globalLocalIndexDestMap.resize(realGlobalIndexSize); |
---|
[631] | 183 | |
---|
| 184 | if (realGlobalIndexSize != globalIndexSrcGrid.size()) globalIndexSrcGrid.resize(realGlobalIndexSize); |
---|
| 185 | for (int i = 0; i < globalIndexSrcGrid.size(); ++i) |
---|
| 186 | if (globalIndexSrcGrid[i].size() != domainSrcGlobalIndex.size()) |
---|
| 187 | globalIndexSrcGrid[i].resize(domainSrcGlobalIndex.size()); |
---|
| 188 | |
---|
| 189 | size_t realGlobalIndex = 0; |
---|
| 190 | idx = 0; |
---|
| 191 | idxLoop.assign(globalDim, 0); |
---|
| 192 | while (idx < ssize) |
---|
| 193 | { |
---|
| 194 | for (int i = 0; i < numElement; ++i) |
---|
| 195 | { |
---|
| 196 | if (idxLoop[i] == gridDomainGlobalDim[i]) |
---|
| 197 | { |
---|
| 198 | idxLoop[i] = 0; |
---|
| 199 | ++idxLoop[i+1]; |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | for (int i = 0; i < numElement; ++i) |
---|
| 204 | { |
---|
| 205 | if (domainElementPosition == i) |
---|
| 206 | { |
---|
| 207 | currentIndex[indexMap[i]] = iIndex; |
---|
| 208 | currentIndex[indexMap[i]+1] = jIndex; |
---|
| 209 | } |
---|
| 210 | else |
---|
| 211 | currentIndex[indexMap[i]] = idxLoop[i]; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | size_t globIndex = currentIndex[0]; |
---|
| 215 | size_t mulDim = 1; |
---|
| 216 | for (int k = 1; k < globalDim; ++k) |
---|
| 217 | { |
---|
| 218 | mulDim *= gridDestGlobalDim[k-1]; |
---|
| 219 | globIndex += (currentIndex[k])*mulDim; |
---|
| 220 | } |
---|
| 221 | |
---|
[829] | 222 | it = globalLocalIndexDestSendToServerMap.find(globIndex); |
---|
| 223 | if (iteArr != it) |
---|
[631] | 224 | { |
---|
[829] | 225 | globalLocalIndexDestMap[realGlobalIndex] = (std::make_pair(it->first,it->second)); |
---|
[631] | 226 | for (int i = 0; i < globalIndexSrcGrid[realGlobalIndex].size(); ++i) |
---|
| 227 | { |
---|
| 228 | domainGlobalIndex(domainSrcGlobalIndex[i], domainSrcGlobalDim[0], domainSrcGlobalDim[1], |
---|
| 229 | currentIndex[indexMap[domainElementPosition]], |
---|
| 230 | currentIndex[indexMap[domainElementPosition]+1]); |
---|
| 231 | |
---|
| 232 | globIndex = currentIndex[0]; |
---|
| 233 | mulDim = 1; |
---|
| 234 | for (int k = 1; k < globalDim; ++k) |
---|
| 235 | { |
---|
[657] | 236 | mulDim *= gridSrcGlobalDim[k-1]; |
---|
[631] | 237 | globIndex += (currentIndex[k])*mulDim; |
---|
| 238 | } |
---|
| 239 | (globalIndexSrcGrid[realGlobalIndex])[i] = globIndex; |
---|
| 240 | } |
---|
| 241 | ++realGlobalIndex; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | ++idxLoop[0]; |
---|
| 245 | ++idx; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | void CDomainAlgorithmTransformation::domainGlobalIndex(const int& index, const int& niGlob, const int& njGlob, |
---|
| 250 | int& iIndex, int& jIndex) |
---|
| 251 | { |
---|
[663] | 252 | iIndex = index % niGlob; |
---|
[631] | 253 | jIndex = index / niGlob; |
---|
| 254 | } |
---|
| 255 | } |
---|