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, const std::vector<int>& nZoomBegin, |
---|
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), |
---|
19 | nZoomSize_(nZoomSize), nZoomBegin_(nZoomBegin), globalLocalIndexMap_() |
---|
20 | { |
---|
21 | createGlobalIndex(); |
---|
22 | } |
---|
23 | |
---|
24 | CDistributionServer::~CDistributionServer() |
---|
25 | { |
---|
26 | } |
---|
27 | |
---|
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() |
---|
35 | { |
---|
36 | size_t idx = 0, ssize = 1; |
---|
37 | for (int i = 0; i < nZoomSize_.size(); ++i) ssize *= nZoomSize_[i]; |
---|
38 | |
---|
39 | this->globalIndex_.resize(ssize); |
---|
40 | std::vector<int> idxLoop(this->getDims(),0); |
---|
41 | std::vector<int> currentIndex(this->getDims()); |
---|
42 | int innerLoopSize = nZoomSize_[0]; |
---|
43 | |
---|
44 | globalLocalIndexMap_.rehash(std::ceil(ssize/globalLocalIndexMap_.max_load_factor())); |
---|
45 | while (idx<ssize) |
---|
46 | { |
---|
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 | } |
---|
69 | globalLocalIndexMap_[globalIndex] = idx; |
---|
70 | this->globalIndex_(idx) = globalIndex; |
---|
71 | |
---|
72 | ++idx; |
---|
73 | } |
---|
74 | idxLoop[0] += innerLoopSize; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
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 | { |
---|
84 | size_t ssize = globalIndex.numElements(); |
---|
85 | size_t localIndexSize = std::min(globalIndex_.numElements(), ssize); |
---|
86 | CArray<size_t,1> localIndex(localIndexSize); |
---|
87 | GlobalLocalMap::const_iterator ite = globalLocalIndexMap_.end(), it; |
---|
88 | int i = 0; |
---|
89 | for (size_t idx = 0; idx < ssize; ++idx) |
---|
90 | { |
---|
91 | it = globalLocalIndexMap_.find(globalIndex(idx)); |
---|
92 | if (ite != it) |
---|
93 | { |
---|
94 | localIndex(i) = it->second; |
---|
95 | ++i; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | globalIndex.reference(localIndex); |
---|
100 | } |
---|
101 | |
---|
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 | } |
---|
114 | |
---|
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 | } |
---|
122 | |
---|
123 | const std::vector<int>& CDistributionServer::getZoomBeginGlobal() const |
---|
124 | { |
---|
125 | return nZoomBeginGlobal_; |
---|
126 | } |
---|
127 | |
---|
128 | const std::vector<int>& CDistributionServer::getZoomBeginServer() const |
---|
129 | { |
---|
130 | return nZoomBegin_; |
---|
131 | } |
---|
132 | |
---|
133 | const std::vector<int>& CDistributionServer::getZoomSizeServer() const |
---|
134 | { |
---|
135 | return nZoomSize_; |
---|
136 | } |
---|
137 | } // namespace xios |
---|