1 | #include "axis.hpp" |
---|
2 | |
---|
3 | #include "attribute_template.hpp" |
---|
4 | #include "object_template.hpp" |
---|
5 | #include "group_template.hpp" |
---|
6 | #include "message.hpp" |
---|
7 | #include "type.hpp" |
---|
8 | #include "context.hpp" |
---|
9 | #include "context_client.hpp" |
---|
10 | #include "xios_spl.hpp" |
---|
11 | |
---|
12 | namespace xios { |
---|
13 | |
---|
14 | /// ////////////////////// Définitions ////////////////////// /// |
---|
15 | |
---|
16 | CAxis::CAxis(void) |
---|
17 | : CObjectTemplate<CAxis>() |
---|
18 | , CAxisAttributes(), isChecked(false), relFiles(), baseRefObject(), areClientAttributesChecked_(false) |
---|
19 | , isDistributed_(false) |
---|
20 | , transformationMap_() |
---|
21 | { |
---|
22 | } |
---|
23 | |
---|
24 | CAxis::CAxis(const StdString & id) |
---|
25 | : CObjectTemplate<CAxis>(id) |
---|
26 | , CAxisAttributes(), isChecked(false), relFiles(), baseRefObject(), areClientAttributesChecked_(false) |
---|
27 | , isDistributed_(false) |
---|
28 | , transformationMap_() |
---|
29 | { |
---|
30 | } |
---|
31 | |
---|
32 | CAxis::~CAxis(void) |
---|
33 | { /* Ne rien faire de plus */ } |
---|
34 | |
---|
35 | ///--------------------------------------------------------------- |
---|
36 | |
---|
37 | const std::set<StdString> & CAxis::getRelFiles(void) const |
---|
38 | { |
---|
39 | return (this->relFiles); |
---|
40 | } |
---|
41 | |
---|
42 | bool CAxis::IsWritten(const StdString & filename) const |
---|
43 | { |
---|
44 | return (this->relFiles.find(filename) != this->relFiles.end()); |
---|
45 | } |
---|
46 | |
---|
47 | bool CAxis::isDistributed(void) const |
---|
48 | { |
---|
49 | return isDistributed_; |
---|
50 | } |
---|
51 | |
---|
52 | void CAxis::addRelFile(const StdString & filename) |
---|
53 | { |
---|
54 | this->relFiles.insert(filename); |
---|
55 | } |
---|
56 | |
---|
57 | //---------------------------------------------------------------- |
---|
58 | |
---|
59 | StdString CAxis::GetName(void) { return (StdString("axis")); } |
---|
60 | StdString CAxis::GetDefName(void){ return (CAxis::GetName()); } |
---|
61 | ENodeType CAxis::GetType(void) { return (eAxis); } |
---|
62 | |
---|
63 | //---------------------------------------------------------------- |
---|
64 | |
---|
65 | CAxis* CAxis::createAxis() |
---|
66 | { |
---|
67 | CAxis* axis = CAxisGroup::get("axis_definition")->createChild(); |
---|
68 | return axis; |
---|
69 | } |
---|
70 | |
---|
71 | void CAxis::duplicateAttributes(CAxis* axis) |
---|
72 | { |
---|
73 | axis->setAttributes(this); |
---|
74 | } |
---|
75 | |
---|
76 | void CAxis::checkAttributes(void) |
---|
77 | { |
---|
78 | if (this->size.isEmpty()) |
---|
79 | ERROR("CAxis::checkAttributes(void)", |
---|
80 | << "Attribute <size> of the axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] must be specified"); |
---|
81 | StdSize size = this->size.getValue(); |
---|
82 | |
---|
83 | isDistributed_ = !this->ibegin.isEmpty() || !this->ni.isEmpty(); |
---|
84 | |
---|
85 | if (!this->ibegin.isEmpty()) |
---|
86 | { |
---|
87 | StdSize ibegin = this->ibegin.getValue(); |
---|
88 | if ((ibegin < 0) || (ibegin > size-1)) |
---|
89 | ERROR("CAxis::checkAttributes(void)", |
---|
90 | << "Attribute <ibegin> of the axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] must be non-negative and smaller than size-1"); |
---|
91 | } |
---|
92 | else this->ibegin.setValue(0); |
---|
93 | |
---|
94 | if (!this->ni.isEmpty()) |
---|
95 | { |
---|
96 | StdSize ni = this->ni.getValue(); |
---|
97 | if ((ni < 0) || (ni > size)) |
---|
98 | ERROR("CAxis::checkAttributes(void)", |
---|
99 | << "Attribute <ni> of the axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] must be non-negative and smaller than size"); |
---|
100 | } |
---|
101 | else this->ni.setValue(size); |
---|
102 | std::cout << "value " << value << std::endl; |
---|
103 | StdSize true_size = value.numElements(); |
---|
104 | if (this->ni.getValue() != true_size) |
---|
105 | ERROR("CAxis::checkAttributes(void)", |
---|
106 | << "The array \'value\' of axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] has a different size that the one defined by the \'size\' attribute"); |
---|
107 | |
---|
108 | this->checkData(); |
---|
109 | this->checkMask(); |
---|
110 | this->checkZoom(); |
---|
111 | |
---|
112 | if (!bounds.isEmpty()) |
---|
113 | { |
---|
114 | if (bounds.extent(0) != size || bounds.extent(1) != 2) |
---|
115 | ERROR("CAxis::checkAttributes(void)", |
---|
116 | << "The bounds array of the axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] must be of dimension axis size x 2" << endl |
---|
117 | << "Axis size is " << size << endl |
---|
118 | << "Bounds size is "<< bounds.extent(0) << " x " << bounds.extent(1)); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | void CAxis::checkData() |
---|
123 | { |
---|
124 | if (data_begin.isEmpty()) data_begin.setValue(0); |
---|
125 | if (!data_n.isEmpty() && data_n.getValue() <= 0) |
---|
126 | { |
---|
127 | ERROR("CAxis::checkData(void)", |
---|
128 | << "Data dimension is negative (data_n)."); |
---|
129 | } |
---|
130 | else if (data_n.isEmpty()) |
---|
131 | data_n.setValue(ni.getValue()); |
---|
132 | |
---|
133 | if (data_index.isEmpty()) |
---|
134 | { |
---|
135 | int dn = data_n.getValue(); |
---|
136 | data_index.resize(dn); |
---|
137 | for (int i = 0; i < dn; ++i) data_index(i) = (i+1); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | void CAxis::checkZoom(void) |
---|
142 | { |
---|
143 | StdSize zoom_begin,zoom_end, zoom_size, axisSize; |
---|
144 | |
---|
145 | zoom_begin = this->zoom_begin.isEmpty() ? 0 : this->zoom_begin.getValue(); |
---|
146 | zoom_size = this->zoom_size.isEmpty() ? size.getValue() : this->zoom_size.getValue(); |
---|
147 | zoom_end = this->zoom_end.isEmpty() ? (size.getValue() - 1) : this->zoom_end.getValue(); |
---|
148 | |
---|
149 | if (this->zoom_begin.isEmpty()) zoom_begin = zoom_end - zoom_size + 1; |
---|
150 | if (this->zoom_end.isEmpty()) zoom_end = zoom_begin + zoom_size - 1; |
---|
151 | if (this->zoom_size.isEmpty()) zoom_size = zoom_end - zoom_begin + 1; |
---|
152 | axisSize = size.getValue(); |
---|
153 | |
---|
154 | if ( (zoom_begin < 0) || (zoom_begin > axisSize-1) || (zoom_end<0) || (zoom_end>axisSize-1) || (zoom_size<1) || (zoom_size>axisSize) || (zoom_begin>zoom_end)) |
---|
155 | ERROR("CAxis::checkAttributes(void)", |
---|
156 | << "One or more attributes among <zoom_begin>, <zoom_end>, <zoom_size> of axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] are not well specified"); |
---|
157 | |
---|
158 | this->zoom_begin.setValue(zoom_begin); |
---|
159 | this->zoom_end.setValue(zoom_end); |
---|
160 | this->zoom_size.setValue(zoom_size); |
---|
161 | } |
---|
162 | |
---|
163 | void CAxis::checkMask() |
---|
164 | { |
---|
165 | int begin_mask = 0, |
---|
166 | end_mask = ni.getValue()-1; |
---|
167 | |
---|
168 | if (!zoom_begin.isEmpty()) |
---|
169 | { |
---|
170 | int zoom_end = zoom_begin.getValue() + zoom_size.getValue() - 1; |
---|
171 | |
---|
172 | begin_mask = std::max(ibegin.getValue(), zoom_begin.getValue()); |
---|
173 | end_mask = std::min(ibegin.getValue() + ni.getValue()-1, zoom_end); |
---|
174 | |
---|
175 | begin_mask -= ibegin.getValue(); |
---|
176 | end_mask -= ibegin.getValue(); |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | if (!mask.isEmpty()) |
---|
181 | { |
---|
182 | if (mask.extent(0) != ni) |
---|
183 | ERROR("CAxis::checkMask(void)", |
---|
184 | << "the mask has not the same size than the local axis" << endl |
---|
185 | << "Local size is " << ni << "x" << endl |
---|
186 | << "Mask size is " << mask.extent(0) << "x"); |
---|
187 | for (int i = 0; i < ni; ++i) |
---|
188 | { |
---|
189 | if (i < begin_mask && i > end_mask) mask(i) = false; |
---|
190 | } |
---|
191 | } |
---|
192 | else // (!mask.hasValue()) |
---|
193 | { // Si aucun masque n'est défini, |
---|
194 | // on en crée un nouveau qui valide l'intégralité du domaine. |
---|
195 | mask.resize(ni); |
---|
196 | for (int i = 0; i < ni.getValue(); ++i) |
---|
197 | { |
---|
198 | if (i >= begin_mask && i <= end_mask) |
---|
199 | mask(i) = true; |
---|
200 | else mask(i) = false; |
---|
201 | } |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | bool CAxis::dispatchEvent(CEventServer& event) |
---|
206 | { |
---|
207 | if (SuperClass::dispatchEvent(event)) return true; |
---|
208 | else |
---|
209 | { |
---|
210 | switch(event.type) |
---|
211 | { |
---|
212 | case EVENT_ID_SERVER_ATTRIBUT : |
---|
213 | recvServerAttribut(event); |
---|
214 | return true; |
---|
215 | break; |
---|
216 | default : |
---|
217 | ERROR("bool CContext::dispatchEvent(CEventServer& event)", |
---|
218 | << "Unknown Event"); |
---|
219 | return false; |
---|
220 | } |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | void CAxis::checkAttributesOnClient(const std::vector<int>& globalDim, int orderPositionInGrid, |
---|
225 | CServerDistributionDescription::ServerDistributionType distType) |
---|
226 | { |
---|
227 | if (this->areClientAttributesChecked_) return; |
---|
228 | |
---|
229 | this->checkAttributes(); |
---|
230 | |
---|
231 | this->areClientAttributesChecked_ = true; |
---|
232 | } |
---|
233 | |
---|
234 | // Send all checked attributes to server |
---|
235 | void CAxis::sendCheckedAttributes(const std::vector<int>& globalDim, int orderPositionInGrid, |
---|
236 | CServerDistributionDescription::ServerDistributionType distType) |
---|
237 | { |
---|
238 | if (!this->areClientAttributesChecked_) checkAttributesOnClient(globalDim, |
---|
239 | orderPositionInGrid, |
---|
240 | distType); |
---|
241 | CContext* context = CContext::getCurrent(); |
---|
242 | |
---|
243 | if (this->isChecked) return; |
---|
244 | if (context->hasClient) |
---|
245 | { |
---|
246 | sendServerAttribut(globalDim, orderPositionInGrid, distType); |
---|
247 | } |
---|
248 | |
---|
249 | this->isChecked = true; |
---|
250 | } |
---|
251 | |
---|
252 | void CAxis::sendServerAttribut(const std::vector<int>& globalDim, int orderPositionInGrid, |
---|
253 | CServerDistributionDescription::ServerDistributionType distType) |
---|
254 | { |
---|
255 | CContext* context = CContext::getCurrent(); |
---|
256 | CContextClient* client = context->client; |
---|
257 | |
---|
258 | CServerDistributionDescription serverDescription(globalDim); |
---|
259 | |
---|
260 | int nbServer = client->serverSize; |
---|
261 | |
---|
262 | serverDescription.computeServerDistribution(nbServer, false, distType); |
---|
263 | std::vector<std::vector<int> > serverIndexBegin = serverDescription.getServerIndexBegin(); |
---|
264 | std::vector<std::vector<int> > serverDimensionSizes = serverDescription.getServerDimensionSizes(); |
---|
265 | |
---|
266 | CEventClient event(getType(),EVENT_ID_SERVER_ATTRIBUT); |
---|
267 | if (client->isServerLeader()) |
---|
268 | { |
---|
269 | std::list<CMessage> msgs; |
---|
270 | |
---|
271 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
272 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
273 | { |
---|
274 | // Use const int to ensure CMessage holds a copy of the value instead of just a reference |
---|
275 | const int begin = serverIndexBegin[*itRank][orderPositionInGrid]; |
---|
276 | const int ni = serverDimensionSizes[*itRank][orderPositionInGrid]; |
---|
277 | const int end = begin + ni - 1; |
---|
278 | |
---|
279 | msgs.push_back(CMessage()); |
---|
280 | CMessage& msg = msgs.back(); |
---|
281 | msg << this->getId(); |
---|
282 | msg << ni << begin << end; |
---|
283 | |
---|
284 | event.push(*itRank,1,msg); |
---|
285 | } |
---|
286 | client->sendEvent(event); |
---|
287 | } |
---|
288 | else client->sendEvent(event); |
---|
289 | } |
---|
290 | |
---|
291 | void CAxis::recvServerAttribut(CEventServer& event) |
---|
292 | { |
---|
293 | CBufferIn* buffer = event.subEvents.begin()->buffer; |
---|
294 | string axisId; |
---|
295 | *buffer >> axisId; |
---|
296 | get(axisId)->recvServerAttribut(*buffer); |
---|
297 | } |
---|
298 | |
---|
299 | void CAxis::recvServerAttribut(CBufferIn& buffer) |
---|
300 | { |
---|
301 | int zoom_end = zoom_begin.getValue()+zoom_size.getValue()-1; |
---|
302 | int ni_srv, begin_srv, end_srv; |
---|
303 | |
---|
304 | buffer>>ni_srv>>begin_srv>>end_srv; |
---|
305 | |
---|
306 | zoom_begin_srv = zoom_begin.getValue() > begin_srv ? zoom_begin.getValue() : begin_srv; |
---|
307 | zoom_end_srv = zoom_end < end_srv ? zoom_end : end_srv; |
---|
308 | zoom_size_srv = zoom_end_srv - zoom_begin_srv + 1; |
---|
309 | |
---|
310 | if (zoom_size_srv<=0) |
---|
311 | { |
---|
312 | zoom_begin_srv = 0; zoom_end_srv = 0; zoom_size_srv = 0; |
---|
313 | } |
---|
314 | |
---|
315 | if (size == ni) |
---|
316 | { |
---|
317 | zoom_begin_srv = zoom_begin.getValue(); |
---|
318 | zoom_end_srv = zoom_end; |
---|
319 | zoom_size_srv = zoom_end_srv - zoom_begin_srv + 1; |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | bool CAxis::hasTransformation() |
---|
324 | { |
---|
325 | return (!transformationMap_.empty()); |
---|
326 | } |
---|
327 | |
---|
328 | void CAxis::setTransformations(const TransMapTypes& axisTrans) |
---|
329 | { |
---|
330 | transformationMap_ = axisTrans; |
---|
331 | } |
---|
332 | |
---|
333 | CAxis::TransMapTypes CAxis::getAllTransformations(void) |
---|
334 | { |
---|
335 | return transformationMap_; |
---|
336 | } |
---|
337 | |
---|
338 | /*! |
---|
339 | Check the validity of all transformations applied on axis |
---|
340 | This functions is called AFTER all inherited attributes are solved |
---|
341 | */ |
---|
342 | void CAxis::checkTransformations() |
---|
343 | { |
---|
344 | TransMapTypes::const_iterator itb = transformationMap_.begin(), it, |
---|
345 | ite = transformationMap_.end(); |
---|
346 | for (it = itb; it != ite; ++it) |
---|
347 | { |
---|
348 | (it->second)->checkValid(this); |
---|
349 | } |
---|
350 | } |
---|
351 | |
---|
352 | void CAxis::solveInheritanceTransformation() |
---|
353 | { |
---|
354 | if (this->hasTransformation()) return; |
---|
355 | |
---|
356 | std::vector<CAxis*> refAxis; |
---|
357 | CAxis* refer_sptr; |
---|
358 | CAxis* refer_ptr = this; |
---|
359 | while (refer_ptr->hasDirectAxisReference()) |
---|
360 | { |
---|
361 | refAxis.push_back(refer_ptr); |
---|
362 | refer_sptr = refer_ptr->getDirectAxisReference(); |
---|
363 | refer_ptr = refer_sptr; |
---|
364 | if (refer_ptr->hasTransformation()) break; |
---|
365 | } |
---|
366 | |
---|
367 | if (refer_ptr->hasTransformation()) |
---|
368 | for (int idx = 0; idx < refAxis.size(); ++idx) |
---|
369 | refAxis[idx]->setTransformations(refer_ptr->getAllTransformations()); |
---|
370 | } |
---|
371 | |
---|
372 | void CAxis::parse(xml::CXMLNode & node) |
---|
373 | { |
---|
374 | SuperClass::parse(node); |
---|
375 | |
---|
376 | if (node.goToChildElement()) |
---|
377 | { |
---|
378 | StdString inverseAxisDefRoot("inverse_axis_definition"); |
---|
379 | StdString inverse("inverse_axis"); |
---|
380 | StdString zoomAxisDefRoot("zoom_axis_definition"); |
---|
381 | StdString zoom("zoom_axis"); |
---|
382 | do |
---|
383 | { |
---|
384 | if (node.getElementName() == inverse) { |
---|
385 | CInverseAxis* tmp = (CInverseAxisGroup::get(inverseAxisDefRoot))->createChild(); |
---|
386 | tmp->parse(node); |
---|
387 | transformationMap_.push_back(std::make_pair(TRANS_INVERSE_AXIS,tmp)); |
---|
388 | } else if (node.getElementName() == zoom) { |
---|
389 | CZoomAxis* tmp = (CZoomAxisGroup::get(zoomAxisDefRoot))->createChild(); |
---|
390 | tmp->parse(node); |
---|
391 | transformationMap_.push_back(std::make_pair(TRANS_ZOOM_AXIS,tmp)); |
---|
392 | } |
---|
393 | } while (node.goToNextElement()) ; |
---|
394 | node.goToParentElement(); |
---|
395 | } |
---|
396 | } |
---|
397 | |
---|
398 | DEFINE_REF_FUNC(Axis,axis) |
---|
399 | |
---|
400 | ///--------------------------------------------------------------- |
---|
401 | |
---|
402 | } // namespace xios |
---|