[553] | 1 | /*! |
---|
| 2 | \file distribution_server.cpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 13 Jan 2015 |
---|
[930] | 5 | \date 10 Sep 2016 |
---|
[553] | 6 | |
---|
| 7 | \brief Index distribution on server side. |
---|
| 8 | */ |
---|
[551] | 9 | #include "distribution_server.hpp" |
---|
[668] | 10 | #include "utils.hpp" |
---|
[551] | 11 | |
---|
| 12 | namespace xios { |
---|
[553] | 13 | |
---|
[653] | 14 | CDistributionServer::CDistributionServer(int rank, int dims, const CArray<size_t,1>& globalIndex) |
---|
[1025] | 15 | : CDistribution(rank, dims, globalIndex), nGlobal_(), nZoomSize_(), nZoomBegin_(), globalLocalIndexMap_(), |
---|
| 16 | globalIndexEachDimension_() |
---|
[551] | 17 | { |
---|
| 18 | } |
---|
| 19 | |
---|
[552] | 20 | CDistributionServer::CDistributionServer(int rank, const std::vector<int>& nZoomBegin, |
---|
| 21 | const std::vector<int>& nZoomSize, const std::vector<int>& nGlobal) |
---|
[1025] | 22 | : CDistribution(rank, nGlobal.size()), nGlobal_(nGlobal), nZoomSize_(nZoomSize), nZoomBegin_(nZoomBegin), globalLocalIndexMap_(), |
---|
| 23 | globalIndexEachDimension_() |
---|
[551] | 24 | { |
---|
| 25 | createGlobalIndex(); |
---|
| 26 | } |
---|
| 27 | |
---|
[567] | 28 | CDistributionServer::CDistributionServer(int rank, const std::vector<int>& nZoomBegin, |
---|
| 29 | const std::vector<int>& nZoomSize, |
---|
| 30 | const std::vector<int>& nZoomBeginGlobal, |
---|
| 31 | const std::vector<int>& nGlobal) |
---|
| 32 | : CDistribution(rank, nGlobal.size()), nGlobal_(nGlobal), nZoomBeginGlobal_(nZoomBeginGlobal), |
---|
[1025] | 33 | nZoomSize_(nZoomSize), nZoomBegin_(nZoomBegin), globalLocalIndexMap_(), |
---|
| 34 | globalIndexEachDimension_() |
---|
[567] | 35 | { |
---|
| 36 | createGlobalIndex(); |
---|
| 37 | } |
---|
| 38 | |
---|
[1129] | 39 | // CDistributionServer::CDistributionServer(int rank, |
---|
| 40 | // const std::vector<CArray<int,1> >& globalIndexEachDimension, |
---|
| 41 | // const std::vector<int>& nGlobal) |
---|
| 42 | // : CDistribution(rank, nGlobal.size()), nGlobal_(nGlobal), nZoomBeginGlobal_(), |
---|
| 43 | // nZoomSize_(), nZoomBegin_(), globalLocalIndexMap_(), |
---|
| 44 | // globalIndexEachDimension_(globalIndexEachDimension) |
---|
| 45 | // { |
---|
| 46 | // createGlobalIndexFromIndex(globalIndexEachDimension, nGlobal); |
---|
| 47 | // } |
---|
[1025] | 48 | |
---|
[551] | 49 | CDistributionServer::~CDistributionServer() |
---|
| 50 | { |
---|
| 51 | } |
---|
| 52 | |
---|
[552] | 53 | /*! |
---|
| 54 | Create global index on server side |
---|
| 55 | Like the similar function on client side, this function serves on creating global index |
---|
| 56 | for data written by the server. The global index is used to calculating local index of data |
---|
| 57 | written on each server |
---|
| 58 | */ |
---|
| 59 | void CDistributionServer::createGlobalIndex() |
---|
[551] | 60 | { |
---|
[552] | 61 | size_t idx = 0, ssize = 1; |
---|
| 62 | for (int i = 0; i < nZoomSize_.size(); ++i) ssize *= nZoomSize_[i]; |
---|
| 63 | |
---|
[653] | 64 | this->globalIndex_.resize(ssize); |
---|
[552] | 65 | std::vector<int> idxLoop(this->getDims(),0); |
---|
| 66 | std::vector<int> currentIndex(this->getDims()); |
---|
| 67 | int innerLoopSize = nZoomSize_[0]; |
---|
| 68 | |
---|
[930] | 69 | globalLocalIndexMap_.rehash(std::ceil(ssize/globalLocalIndexMap_.max_load_factor())); |
---|
[552] | 70 | while (idx<ssize) |
---|
[551] | 71 | { |
---|
[552] | 72 | for (int i = 0; i < this->dims_-1; ++i) |
---|
| 73 | { |
---|
| 74 | if (idxLoop[i] == nZoomSize_[i]) |
---|
| 75 | { |
---|
| 76 | idxLoop[i] = 0; |
---|
| 77 | ++idxLoop[i+1]; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | for (int i = 1; i < this->dims_; ++i) currentIndex[i] = idxLoop[i] + nZoomBegin_[i]; |
---|
| 82 | |
---|
| 83 | size_t mulDim, globalIndex; |
---|
| 84 | for (int i = 0; i < innerLoopSize; ++i) |
---|
| 85 | { |
---|
| 86 | mulDim = 1; |
---|
| 87 | globalIndex = i + nZoomBegin_[0]; |
---|
| 88 | |
---|
| 89 | for (int k = 1; k < this->dims_; ++k) |
---|
| 90 | { |
---|
| 91 | mulDim *= nGlobal_[k-1]; |
---|
| 92 | globalIndex += (currentIndex[k])*mulDim; |
---|
| 93 | } |
---|
[930] | 94 | globalLocalIndexMap_[globalIndex] = idx; |
---|
[653] | 95 | this->globalIndex_(idx) = globalIndex; |
---|
[930] | 96 | |
---|
[552] | 97 | ++idx; |
---|
| 98 | } |
---|
| 99 | idxLoop[0] += innerLoopSize; |
---|
[551] | 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
[1129] | 103 | // void CDistributionServer::createGlobalIndexFromIndex(const std::vector<CArray<int,1> >& globalIndexOnEachDimension, |
---|
| 104 | // const std::vector<int>& nbGlobal) |
---|
| 105 | // { |
---|
| 106 | // size_t idx = 0, ssize = 1; |
---|
| 107 | // for (int i = 0; i < globalIndexOnEachDimension.size(); ++i) ssize *= globalIndexOnEachDimension[i].numElements(); |
---|
[1025] | 108 | |
---|
[1129] | 109 | // this->globalIndex_.resize(ssize); |
---|
| 110 | // size_t nbDim = nbGlobal.size(); |
---|
| 111 | // std::vector<int> idxLoop(nbDim,0); |
---|
| 112 | // std::vector<int> currentIndex(nbDim); |
---|
| 113 | // int innerLoopSize = globalIndexOnEachDimension[0].numElements(); |
---|
[1025] | 114 | |
---|
[1129] | 115 | // globalLocalIndexMap_.rehash(std::ceil(ssize/globalLocalIndexMap_.max_load_factor())); |
---|
| 116 | // while (idx<ssize) |
---|
| 117 | // { |
---|
| 118 | // for (int i = 0; i < nbDim-1; ++i) |
---|
| 119 | // { |
---|
| 120 | // if (idxLoop[i] == globalIndexOnEachDimension[i].numElements()) |
---|
| 121 | // { |
---|
| 122 | // idxLoop[i] = 0; |
---|
| 123 | // ++idxLoop[i+1]; |
---|
| 124 | // } |
---|
| 125 | // } |
---|
[1025] | 126 | |
---|
[1129] | 127 | // for (int i = 1; i < nbDim; ++i) |
---|
| 128 | // currentIndex[i] = globalIndexOnEachDimension[i](idxLoop[i]); |
---|
[1025] | 129 | |
---|
[1129] | 130 | // size_t mulDim, globalIndex; |
---|
| 131 | // for (int i = 0; i < innerLoopSize; ++i) |
---|
| 132 | // { |
---|
| 133 | // mulDim = 1; |
---|
| 134 | // globalIndex = globalIndexOnEachDimension[0](i); |
---|
[1025] | 135 | |
---|
[1129] | 136 | // for (int k = 0; k < nbDim; ++k) |
---|
| 137 | // { |
---|
| 138 | // mulDim *= nbGlobal[k]; |
---|
| 139 | // globalIndex += (currentIndex[k])*mulDim; |
---|
| 140 | // } |
---|
| 141 | // globalLocalIndexMap_[globalIndex] = idx; |
---|
| 142 | // this->globalIndex_(idx) = globalIndex; |
---|
[1025] | 143 | |
---|
[1129] | 144 | // ++idx; |
---|
| 145 | // } |
---|
| 146 | // idxLoop[0] += innerLoopSize; |
---|
| 147 | // } |
---|
[1025] | 148 | |
---|
[1129] | 149 | // } |
---|
[1025] | 150 | |
---|
[552] | 151 | /*! |
---|
| 152 | Compute local index for writing data on server |
---|
| 153 | \param [in] globalIndex global index received from client |
---|
| 154 | \return local index of written data |
---|
| 155 | */ |
---|
| 156 | CArray<size_t,1> CDistributionServer::computeLocalIndex(const CArray<size_t,1>& globalIndex) |
---|
[551] | 157 | { |
---|
[1129] | 158 | size_t ssize = globalIndex.numElements(); |
---|
| 159 | size_t localIndexSize = std::min(globalIndex_.numElements(), ssize); |
---|
| 160 | CArray<size_t,1> localIndex(localIndexSize); |
---|
[930] | 161 | GlobalLocalMap::const_iterator ite = globalLocalIndexMap_.end(), it; |
---|
[1129] | 162 | for (int idx = 0, i = 0; idx < ssize; ++idx) |
---|
[551] | 163 | { |
---|
[930] | 164 | it = globalLocalIndexMap_.find(globalIndex(idx)); |
---|
| 165 | if (ite != it) |
---|
[1129] | 166 | { |
---|
[930] | 167 | localIndex(idx) = it->second; |
---|
[1129] | 168 | ++i; |
---|
| 169 | } |
---|
[551] | 170 | } |
---|
[552] | 171 | |
---|
| 172 | return localIndex; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | /*! |
---|
| 176 | Compute local index for writing data on server |
---|
| 177 | \param [in] globalIndex Global index received from client |
---|
| 178 | */ |
---|
| 179 | void CDistributionServer::computeLocalIndex(CArray<size_t,1>& globalIndex) |
---|
| 180 | { |
---|
[1099] | 181 | size_t ssize = globalIndex.numElements(); |
---|
| 182 | size_t localIndexSize = std::min(globalIndex_.numElements(), ssize); |
---|
| 183 | CArray<size_t,1> localIndex(localIndexSize); |
---|
[930] | 184 | GlobalLocalMap::const_iterator ite = globalLocalIndexMap_.end(), it; |
---|
[1099] | 185 | int i = 0; |
---|
| 186 | for (size_t idx = 0; idx < ssize; ++idx) |
---|
[551] | 187 | { |
---|
[930] | 188 | it = globalLocalIndexMap_.find(globalIndex(idx)); |
---|
| 189 | if (ite != it) |
---|
[1099] | 190 | { |
---|
| 191 | localIndex(i) = it->second; |
---|
| 192 | ++i; |
---|
| 193 | } |
---|
[551] | 194 | } |
---|
| 195 | |
---|
[930] | 196 | globalIndex.reference(localIndex); |
---|
[551] | 197 | } |
---|
| 198 | |
---|
[676] | 199 | /*! |
---|
| 200 | Transforms local indexes owned by the server into global indexes |
---|
| 201 | \param [in/out] indexes on input, local indexes of the server, |
---|
| 202 | on output, the corresponding global indexes |
---|
| 203 | */ |
---|
| 204 | void CDistributionServer::computeGlobalIndex(CArray<int,1>& indexes) const |
---|
| 205 | { |
---|
| 206 | for (int i = 0; i < indexes.numElements(); ++i) |
---|
| 207 | { |
---|
| 208 | indexes(i) = globalIndex_(indexes(i)); |
---|
| 209 | } |
---|
| 210 | } |
---|
[567] | 211 | |
---|
[1099] | 212 | /*! |
---|
| 213 | Get the size of grid index in server (e.x: sizeGrid *= size of each dimensiion) |
---|
| 214 | */ |
---|
| 215 | int CDistributionServer::getGridSize() const |
---|
| 216 | { |
---|
| 217 | return globalLocalIndexMap_.size(); |
---|
| 218 | } |
---|
[676] | 219 | |
---|
| 220 | const std::vector<int>& CDistributionServer::getZoomBeginGlobal() const |
---|
[567] | 221 | { |
---|
| 222 | return nZoomBeginGlobal_; |
---|
| 223 | } |
---|
| 224 | |
---|
[676] | 225 | const std::vector<int>& CDistributionServer::getZoomBeginServer() const |
---|
[567] | 226 | { |
---|
| 227 | return nZoomBegin_; |
---|
| 228 | } |
---|
| 229 | |
---|
[676] | 230 | const std::vector<int>& CDistributionServer::getZoomSizeServer() const |
---|
[567] | 231 | { |
---|
| 232 | return nZoomSize_; |
---|
| 233 | } |
---|
[552] | 234 | } // namespace xios |
---|