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