1 | /*! |
---|
2 | \file distribution_server.cpp |
---|
3 | \author Ha NGUYEN |
---|
4 | \since 13 Jan 2015 |
---|
5 | \date 10 Sep 2016 |
---|
6 | |
---|
7 | \brief Index distribution on server side. |
---|
8 | */ |
---|
9 | #include "distribution_server.hpp" |
---|
10 | #include "utils.hpp" |
---|
11 | |
---|
12 | namespace xios { |
---|
13 | |
---|
14 | CDistributionServer::CDistributionServer(int rank, int dims, const CArray<size_t,1>& globalIndex) |
---|
15 | : CDistribution(rank, dims, globalIndex), nGlobal_(), nZoomSize_(), nZoomBegin_(), globalLocalIndexMap_(), |
---|
16 | globalIndexEachDimension_() |
---|
17 | { |
---|
18 | } |
---|
19 | |
---|
20 | CDistributionServer::CDistributionServer(int rank, const std::vector<int>& nZoomBegin, |
---|
21 | const std::vector<int>& nZoomSize, const std::vector<int>& nGlobal) |
---|
22 | : CDistribution(rank, nGlobal.size()), nGlobal_(nGlobal), nZoomSize_(nZoomSize), nZoomBegin_(nZoomBegin), globalLocalIndexMap_(), |
---|
23 | globalIndexEachDimension_() |
---|
24 | { |
---|
25 | createGlobalIndex(); |
---|
26 | } |
---|
27 | |
---|
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), |
---|
33 | nZoomSize_(nZoomSize), nZoomBegin_(nZoomBegin), globalLocalIndexMap_(), |
---|
34 | globalIndexEachDimension_() |
---|
35 | { |
---|
36 | createGlobalIndex(); |
---|
37 | } |
---|
38 | |
---|
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 | } |
---|
48 | |
---|
49 | CDistributionServer::~CDistributionServer() |
---|
50 | { |
---|
51 | } |
---|
52 | |
---|
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() |
---|
60 | { |
---|
61 | size_t idx = 0, ssize = 1; |
---|
62 | for (int i = 0; i < nZoomSize_.size(); ++i) ssize *= nZoomSize_[i]; |
---|
63 | |
---|
64 | this->globalIndex_.resize(ssize); |
---|
65 | std::vector<int> idxLoop(this->getDims(),0); |
---|
66 | std::vector<int> currentIndex(this->getDims()); |
---|
67 | int innerLoopSize = nZoomSize_[0]; |
---|
68 | |
---|
69 | globalLocalIndexMap_.rehash(std::ceil(ssize/globalLocalIndexMap_.max_load_factor())); |
---|
70 | while (idx<ssize) |
---|
71 | { |
---|
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 | } |
---|
94 | globalLocalIndexMap_[globalIndex] = idx; |
---|
95 | this->globalIndex_(idx) = globalIndex; |
---|
96 | |
---|
97 | ++idx; |
---|
98 | } |
---|
99 | idxLoop[0] += innerLoopSize; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
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(); |
---|
108 | |
---|
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(); |
---|
114 | |
---|
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 | } |
---|
126 | |
---|
127 | for (int i = 1; i < nbDim; ++i) |
---|
128 | currentIndex[i] = globalIndexOnEachDimension[i](idxLoop[i]); |
---|
129 | |
---|
130 | size_t mulDim, globalIndex; |
---|
131 | for (int i = 0; i < innerLoopSize; ++i) |
---|
132 | { |
---|
133 | mulDim = 1; |
---|
134 | globalIndex = globalIndexOnEachDimension[0](i); |
---|
135 | |
---|
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; |
---|
143 | |
---|
144 | ++idx; |
---|
145 | } |
---|
146 | idxLoop[0] += innerLoopSize; |
---|
147 | } |
---|
148 | |
---|
149 | } |
---|
150 | |
---|
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) |
---|
157 | { |
---|
158 | int ssize = globalIndex.numElements(); |
---|
159 | CArray<size_t,1> localIndex(ssize); |
---|
160 | GlobalLocalMap::const_iterator ite = globalLocalIndexMap_.end(), it; |
---|
161 | for (int idx = 0; idx < ssize; ++idx) |
---|
162 | { |
---|
163 | it = globalLocalIndexMap_.find(globalIndex(idx)); |
---|
164 | if (ite != it) |
---|
165 | localIndex(idx) = it->second; |
---|
166 | } |
---|
167 | |
---|
168 | return localIndex; |
---|
169 | } |
---|
170 | |
---|
171 | /*! |
---|
172 | Compute local index for writing data on server |
---|
173 | \param [in] globalIndex Global index received from client |
---|
174 | */ |
---|
175 | void CDistributionServer::computeLocalIndex(CArray<size_t,1>& globalIndex) |
---|
176 | { |
---|
177 | int ssize = globalIndex.numElements(); |
---|
178 | CArray<size_t,1> localIndex(ssize); |
---|
179 | GlobalLocalMap::const_iterator ite = globalLocalIndexMap_.end(), it; |
---|
180 | for (int idx = 0; idx < ssize; ++idx) |
---|
181 | { |
---|
182 | it = globalLocalIndexMap_.find(globalIndex(idx)); |
---|
183 | if (ite != it) |
---|
184 | localIndex(idx) = it->second; |
---|
185 | } |
---|
186 | |
---|
187 | globalIndex.reference(localIndex); |
---|
188 | } |
---|
189 | |
---|
190 | /*! |
---|
191 | Transforms local indexes owned by the server into global indexes |
---|
192 | \param [in/out] indexes on input, local indexes of the server, |
---|
193 | on output, the corresponding global indexes |
---|
194 | */ |
---|
195 | void CDistributionServer::computeGlobalIndex(CArray<int,1>& indexes) const |
---|
196 | { |
---|
197 | for (int i = 0; i < indexes.numElements(); ++i) |
---|
198 | { |
---|
199 | indexes(i) = globalIndex_(indexes(i)); |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | |
---|
204 | const std::vector<int>& CDistributionServer::getZoomBeginGlobal() const |
---|
205 | { |
---|
206 | return nZoomBeginGlobal_; |
---|
207 | } |
---|
208 | |
---|
209 | const std::vector<int>& CDistributionServer::getZoomBeginServer() const |
---|
210 | { |
---|
211 | return nZoomBegin_; |
---|
212 | } |
---|
213 | |
---|
214 | const std::vector<int>& CDistributionServer::getZoomSizeServer() const |
---|
215 | { |
---|
216 | return nZoomSize_; |
---|
217 | } |
---|
218 | } // namespace xios |
---|