1 | /*! |
---|
2 | \file distribution_server.cpp |
---|
3 | \author Ha NGUYEN |
---|
4 | \since 13 Jan 2015 |
---|
5 | \date 04 Feb 2015 |
---|
6 | |
---|
7 | \brief Index distribution on server side. |
---|
8 | */ |
---|
9 | #include "distribution_server.hpp" |
---|
10 | |
---|
11 | namespace xios { |
---|
12 | |
---|
13 | CDistributionServer::CDistributionServer(int rank, int dims, const CArray<size_t,1>& globalIndex) |
---|
14 | : CDistribution(rank, dims, globalIndex), nGlobal_(), nZoomSize_(), nZoomBegin_() |
---|
15 | { |
---|
16 | } |
---|
17 | |
---|
18 | CDistributionServer::CDistributionServer(int rank, const std::vector<int>& nZoomBegin, |
---|
19 | const std::vector<int>& nZoomSize, const std::vector<int>& nGlobal) |
---|
20 | : CDistribution(rank, nGlobal.size()), nGlobal_(nGlobal), nZoomSize_(nZoomSize), nZoomBegin_(nZoomBegin) |
---|
21 | { |
---|
22 | createGlobalIndex(); |
---|
23 | } |
---|
24 | |
---|
25 | CDistributionServer::CDistributionServer(int rank, const std::vector<int>& nZoomBegin, |
---|
26 | const std::vector<int>& nZoomSize, |
---|
27 | const std::vector<int>& nZoomBeginGlobal, |
---|
28 | const std::vector<int>& nGlobal) |
---|
29 | : CDistribution(rank, nGlobal.size()), nGlobal_(nGlobal), nZoomBeginGlobal_(nZoomBeginGlobal), |
---|
30 | nZoomSize_(nZoomSize), nZoomBegin_(nZoomBegin) |
---|
31 | { |
---|
32 | createGlobalIndex(); |
---|
33 | } |
---|
34 | |
---|
35 | CDistributionServer::~CDistributionServer() |
---|
36 | { |
---|
37 | } |
---|
38 | |
---|
39 | /*! |
---|
40 | Create global index on server side |
---|
41 | Like the similar function on client side, this function serves on creating global index |
---|
42 | for data written by the server. The global index is used to calculating local index of data |
---|
43 | written on each server |
---|
44 | */ |
---|
45 | void CDistributionServer::createGlobalIndex() |
---|
46 | { |
---|
47 | size_t idx = 0, ssize = 1; |
---|
48 | for (int i = 0; i < nZoomSize_.size(); ++i) ssize *= nZoomSize_[i]; |
---|
49 | |
---|
50 | this->globalIndex_.resize(ssize); |
---|
51 | std::vector<int> idxLoop(this->getDims(),0); |
---|
52 | std::vector<int> currentIndex(this->getDims()); |
---|
53 | int innerLoopSize = nZoomSize_[0]; |
---|
54 | |
---|
55 | while (idx<ssize) |
---|
56 | { |
---|
57 | for (int i = 0; i < this->dims_-1; ++i) |
---|
58 | { |
---|
59 | if (idxLoop[i] == nZoomSize_[i]) |
---|
60 | { |
---|
61 | idxLoop[i] = 0; |
---|
62 | ++idxLoop[i+1]; |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | for (int i = 1; i < this->dims_; ++i) currentIndex[i] = idxLoop[i] + nZoomBegin_[i]; |
---|
67 | |
---|
68 | size_t mulDim, globalIndex; |
---|
69 | for (int i = 0; i < innerLoopSize; ++i) |
---|
70 | { |
---|
71 | mulDim = 1; |
---|
72 | globalIndex = i + nZoomBegin_[0]; |
---|
73 | |
---|
74 | for (int k = 1; k < this->dims_; ++k) |
---|
75 | { |
---|
76 | mulDim *= nGlobal_[k-1]; |
---|
77 | globalIndex += (currentIndex[k])*mulDim; |
---|
78 | } |
---|
79 | this->globalIndex_(idx) = globalIndex; |
---|
80 | ++idx; |
---|
81 | } |
---|
82 | idxLoop[0] += innerLoopSize; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | /*! |
---|
87 | Compute local index for writing data on server |
---|
88 | \param [in] globalIndex global index received from client |
---|
89 | \return local index of written data |
---|
90 | */ |
---|
91 | CArray<size_t,1> CDistributionServer::computeLocalIndex(const CArray<size_t,1>& globalIndex) |
---|
92 | { |
---|
93 | CArray<size_t,1>::const_iterator itBegin = this->globalIndex_.begin(), |
---|
94 | itEnd = this->globalIndex_.end(), it; |
---|
95 | |
---|
96 | int ssize = globalIndex.numElements(), idx = 0; |
---|
97 | CArray<size_t,1> localIndex(ssize); |
---|
98 | it = itBegin; |
---|
99 | for (int i = 0; i < ssize; ++i) |
---|
100 | { |
---|
101 | it = std::find(itBegin, itEnd, globalIndex(i)); |
---|
102 | // it = std::lower_bound(it, itEnd, globalIndex(i)); |
---|
103 | if (itEnd != it) |
---|
104 | { |
---|
105 | localIndex(idx) = std::distance(itBegin, it); |
---|
106 | ++idx; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | return localIndex; |
---|
111 | } |
---|
112 | |
---|
113 | /*! |
---|
114 | Compute local index for writing data on server |
---|
115 | \param [in] globalIndex Global index received from client |
---|
116 | */ |
---|
117 | void CDistributionServer::computeLocalIndex(CArray<size_t,1>& globalIndex) |
---|
118 | { |
---|
119 | CArray<size_t,1>::const_iterator itBegin = this->globalIndex_.begin(), |
---|
120 | itEnd = this->globalIndex_.end(), it; |
---|
121 | |
---|
122 | int ssize = globalIndex.numElements(), idx = 0; |
---|
123 | CArray<size_t,1> localIndex(ssize); |
---|
124 | it = itBegin; |
---|
125 | for (int i = 0; i < ssize; ++i) |
---|
126 | { |
---|
127 | it = std::find(itBegin, itEnd, globalIndex(i)); |
---|
128 | // it = std::lower_bound(it, itEnd, globalIndex(i)); |
---|
129 | if (itEnd != it) |
---|
130 | { |
---|
131 | localIndex(idx) = std::distance(itBegin, it); |
---|
132 | ++idx; |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | globalIndex = localIndex; |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | std::vector<int> CDistributionServer::getZoomBeginGlobal() const |
---|
141 | { |
---|
142 | return nZoomBeginGlobal_; |
---|
143 | } |
---|
144 | |
---|
145 | std::vector<int> CDistributionServer::getZoomBeginServer() const |
---|
146 | { |
---|
147 | return nZoomBegin_; |
---|
148 | } |
---|
149 | |
---|
150 | std::vector<int> CDistributionServer::getZoomSizeServer() const |
---|
151 | { |
---|
152 | return nZoomSize_; |
---|
153 | } |
---|
154 | } // namespace xios |
---|