[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 | |
---|
[552] | 14 | CDistributionServer::CDistributionServer(int rank, const std::vector<int>& nZoomBegin, |
---|
[567] | 15 | const std::vector<int>& nZoomSize, |
---|
| 16 | const std::vector<int>& nZoomBeginGlobal, |
---|
| 17 | const std::vector<int>& nGlobal) |
---|
| 18 | : CDistribution(rank, nGlobal.size()), nGlobal_(nGlobal), nZoomBeginGlobal_(nZoomBeginGlobal), |
---|
[1144] | 19 | nZoomSize_(nZoomSize), nZoomBegin_(nZoomBegin), globalLocalIndexMap_() |
---|
[567] | 20 | { |
---|
| 21 | createGlobalIndex(); |
---|
| 22 | } |
---|
| 23 | |
---|
[551] | 24 | CDistributionServer::~CDistributionServer() |
---|
| 25 | { |
---|
| 26 | } |
---|
| 27 | |
---|
[552] | 28 | /*! |
---|
| 29 | Create global index on server side |
---|
| 30 | Like the similar function on client side, this function serves on creating global index |
---|
| 31 | for data written by the server. The global index is used to calculating local index of data |
---|
| 32 | written on each server |
---|
| 33 | */ |
---|
| 34 | void CDistributionServer::createGlobalIndex() |
---|
[551] | 35 | { |
---|
[552] | 36 | size_t idx = 0, ssize = 1; |
---|
| 37 | for (int i = 0; i < nZoomSize_.size(); ++i) ssize *= nZoomSize_[i]; |
---|
| 38 | |
---|
[653] | 39 | this->globalIndex_.resize(ssize); |
---|
[552] | 40 | std::vector<int> idxLoop(this->getDims(),0); |
---|
| 41 | std::vector<int> currentIndex(this->getDims()); |
---|
| 42 | int innerLoopSize = nZoomSize_[0]; |
---|
| 43 | |
---|
[930] | 44 | globalLocalIndexMap_.rehash(std::ceil(ssize/globalLocalIndexMap_.max_load_factor())); |
---|
[552] | 45 | while (idx<ssize) |
---|
[551] | 46 | { |
---|
[552] | 47 | for (int i = 0; i < this->dims_-1; ++i) |
---|
| 48 | { |
---|
| 49 | if (idxLoop[i] == nZoomSize_[i]) |
---|
| 50 | { |
---|
| 51 | idxLoop[i] = 0; |
---|
| 52 | ++idxLoop[i+1]; |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | for (int i = 1; i < this->dims_; ++i) currentIndex[i] = idxLoop[i] + nZoomBegin_[i]; |
---|
| 57 | |
---|
| 58 | size_t mulDim, globalIndex; |
---|
| 59 | for (int i = 0; i < innerLoopSize; ++i) |
---|
| 60 | { |
---|
| 61 | mulDim = 1; |
---|
| 62 | globalIndex = i + nZoomBegin_[0]; |
---|
| 63 | |
---|
| 64 | for (int k = 1; k < this->dims_; ++k) |
---|
| 65 | { |
---|
| 66 | mulDim *= nGlobal_[k-1]; |
---|
| 67 | globalIndex += (currentIndex[k])*mulDim; |
---|
| 68 | } |
---|
[930] | 69 | globalLocalIndexMap_[globalIndex] = idx; |
---|
[653] | 70 | this->globalIndex_(idx) = globalIndex; |
---|
[930] | 71 | |
---|
[552] | 72 | ++idx; |
---|
| 73 | } |
---|
| 74 | idxLoop[0] += innerLoopSize; |
---|
[551] | 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
[552] | 78 | /*! |
---|
| 79 | Compute local index for writing data on server |
---|
| 80 | \param [in] globalIndex Global index received from client |
---|
| 81 | */ |
---|
| 82 | void CDistributionServer::computeLocalIndex(CArray<size_t,1>& globalIndex) |
---|
| 83 | { |
---|
[1099] | 84 | size_t ssize = globalIndex.numElements(); |
---|
| 85 | size_t localIndexSize = std::min(globalIndex_.numElements(), ssize); |
---|
| 86 | CArray<size_t,1> localIndex(localIndexSize); |
---|
[930] | 87 | GlobalLocalMap::const_iterator ite = globalLocalIndexMap_.end(), it; |
---|
[1099] | 88 | int i = 0; |
---|
| 89 | for (size_t idx = 0; idx < ssize; ++idx) |
---|
[551] | 90 | { |
---|
[930] | 91 | it = globalLocalIndexMap_.find(globalIndex(idx)); |
---|
| 92 | if (ite != it) |
---|
[1099] | 93 | { |
---|
| 94 | localIndex(i) = it->second; |
---|
| 95 | ++i; |
---|
| 96 | } |
---|
[551] | 97 | } |
---|
| 98 | |
---|
[930] | 99 | globalIndex.reference(localIndex); |
---|
[551] | 100 | } |
---|
| 101 | |
---|
[676] | 102 | /*! |
---|
| 103 | Transforms local indexes owned by the server into global indexes |
---|
| 104 | \param [in/out] indexes on input, local indexes of the server, |
---|
| 105 | on output, the corresponding global indexes |
---|
| 106 | */ |
---|
| 107 | void CDistributionServer::computeGlobalIndex(CArray<int,1>& indexes) const |
---|
| 108 | { |
---|
| 109 | for (int i = 0; i < indexes.numElements(); ++i) |
---|
| 110 | { |
---|
| 111 | indexes(i) = globalIndex_(indexes(i)); |
---|
| 112 | } |
---|
| 113 | } |
---|
[567] | 114 | |
---|
[1099] | 115 | /*! |
---|
| 116 | Get the size of grid index in server (e.x: sizeGrid *= size of each dimensiion) |
---|
| 117 | */ |
---|
| 118 | int CDistributionServer::getGridSize() const |
---|
| 119 | { |
---|
| 120 | return globalLocalIndexMap_.size(); |
---|
| 121 | } |
---|
[676] | 122 | |
---|
| 123 | const std::vector<int>& CDistributionServer::getZoomBeginGlobal() const |
---|
[567] | 124 | { |
---|
| 125 | return nZoomBeginGlobal_; |
---|
| 126 | } |
---|
| 127 | |
---|
[676] | 128 | const std::vector<int>& CDistributionServer::getZoomBeginServer() const |
---|
[567] | 129 | { |
---|
| 130 | return nZoomBegin_; |
---|
| 131 | } |
---|
| 132 | |
---|
[676] | 133 | const std::vector<int>& CDistributionServer::getZoomSizeServer() const |
---|
[567] | 134 | { |
---|
| 135 | return nZoomSize_; |
---|
| 136 | } |
---|
[552] | 137 | } // namespace xios |
---|