source: XIOS/trunk/src/node/axis.cpp @ 621

Last change on this file since 621 was 621, checked in by mhnguyen, 9 years ago

Implementing generic transformation algorithm (local commit)

+) Change a little bit to make sure everything work in order

Test
+) test_new_features passe with inverse

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
  • Property svn:executable set to *
File size: 12.9 KB
Line 
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
12namespace 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   void CAxis::checkAttributes(void)
66   {
67      if (this->size.isEmpty())
68         ERROR("CAxis::checkAttributes(void)",
69               << "Attribute <size> of the axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] must be specified");
70      StdSize size = this->size.getValue();
71
72      isDistributed_ = !this->ibegin.isEmpty() || !this->ni.isEmpty();
73
74      if (!this->ibegin.isEmpty())
75      {
76        StdSize ibegin = this->ibegin.getValue();
77        if ((ibegin < 0) || (ibegin > size-1))
78          ERROR("CAxis::checkAttributes(void)",
79                << "Attribute <ibegin> of the axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] must be non-negative and smaller than size-1");
80      }
81      else this->ibegin.setValue(0);
82
83      if (!this->ni.isEmpty())
84      {
85        StdSize ni = this->ni.getValue();
86        if ((ni < 0) || (ni > size))
87          ERROR("CAxis::checkAttributes(void)",
88                << "Attribute <ni> of the axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] must be non-negative and smaller than size");
89      }
90      else this->ni.setValue(size);
91      std::cout <<  "value " <<  value <<  std::endl;
92      StdSize true_size = value.numElements();
93      if (this->ni.getValue() != true_size)
94         ERROR("CAxis::checkAttributes(void)",
95               << "The array \'value\' of axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] has a different size that the one defined by the \'size\' attribute");
96
97      this->checkData();
98      this->checkMask();
99      this->checkZoom();
100
101      if (!bounds.isEmpty())
102      {
103        if (bounds.extent(0) != size || bounds.extent(1) != 2)
104            ERROR("CAxis::checkAttributes(void)",
105                  << "The bounds array of the axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] must be of dimension axis size x 2" << endl
106                  << "Axis size is " << size << endl
107                  << "Bounds size is "<< bounds.extent(0) << " x " << bounds.extent(1));
108      }
109   }
110
111   void CAxis::checkData()
112   {
113      if (data_begin.isEmpty()) data_begin.setValue(0);
114      if (!data_n.isEmpty() && data_n.getValue() <= 0)
115      {
116        ERROR("CAxis::checkData(void)",
117              << "Data dimension is negative (data_n).");
118      }
119      else if (data_n.isEmpty())
120        data_n.setValue(ni.getValue());
121
122      if (data_index.isEmpty())
123      {
124        int dn = data_n.getValue();
125        data_index.resize(dn);
126        for (int i = 0; i < dn; ++i) data_index(i) = (i+1);
127      }
128   }
129
130   void CAxis::checkZoom(void)
131   {
132      StdSize zoom_begin,zoom_end, zoom_size, axisSize;
133
134      zoom_begin = this->zoom_begin.isEmpty() ? 0 : this->zoom_begin.getValue();
135      zoom_size  = this->zoom_size.isEmpty() ? size.getValue() : this->zoom_size.getValue();
136      zoom_end   = this->zoom_end.isEmpty() ? (size.getValue() - 1) : this->zoom_end.getValue();
137
138      if (this->zoom_begin.isEmpty()) zoom_begin = zoom_end - zoom_size + 1;
139      if (this->zoom_end.isEmpty()) zoom_end = zoom_begin + zoom_size - 1;
140      if (this->zoom_size.isEmpty()) zoom_size = zoom_end - zoom_begin + 1;
141      axisSize = size.getValue();
142
143      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))
144        ERROR("CAxis::checkAttributes(void)",
145              << "One or more attributes among <zoom_begin>, <zoom_end>, <zoom_size> of axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] are not well specified");
146
147      this->zoom_begin.setValue(zoom_begin);
148      this->zoom_end.setValue(zoom_end);
149      this->zoom_size.setValue(zoom_size);
150   }
151
152   void CAxis::checkMask()
153   {
154      int begin_mask = 0,
155          end_mask = ni.getValue()-1;
156
157      if (!zoom_begin.isEmpty())
158      {
159         int zoom_end = zoom_begin.getValue() + zoom_size.getValue() - 1;
160
161         begin_mask = std::max(ibegin.getValue(), zoom_begin.getValue());
162         end_mask   = std::min(ibegin.getValue() + ni.getValue()-1, zoom_end);
163
164         begin_mask -= ibegin.getValue();
165         end_mask   -= ibegin.getValue();
166      }
167
168
169      if (!mask.isEmpty())
170      {
171         if (mask.extent(0) != ni)
172            ERROR("CAxis::checkMask(void)",
173                  << "the mask has not the same size than the local axis" << endl
174                  << "Local size is " << ni << "x" << endl
175                  << "Mask size is " << mask.extent(0) << "x");
176         for (int i = 0; i < ni; ++i)
177         {
178           if (i < begin_mask && i > end_mask)  mask(i) = false;
179         }
180      }
181      else // (!mask.hasValue())
182      { // Si aucun masque n'est défini,
183        // on en crée un nouveau qui valide l'intégralité du domaine.
184         mask.resize(ni);
185         for (int i = 0; i < ni.getValue(); ++i)
186         {
187               if (i >= begin_mask && i <= end_mask)
188                 mask(i) = true;
189               else  mask(i) = false;
190         }
191      }
192   }
193
194  bool CAxis::dispatchEvent(CEventServer& event)
195   {
196      if (SuperClass::dispatchEvent(event)) return true;
197      else
198      {
199        switch(event.type)
200        {
201           case EVENT_ID_SERVER_ATTRIBUT :
202             recvServerAttribut(event);
203             return true;
204             break;
205           default :
206             ERROR("bool CContext::dispatchEvent(CEventServer& event)",
207                    << "Unknown Event");
208           return false;
209         }
210      }
211   }
212
213   void CAxis::checkAttributesOnClient(const std::vector<int>& globalDim, int orderPositionInGrid,
214                                       CServerDistributionDescription::ServerDistributionType distType)
215   {
216     if (this->areClientAttributesChecked_) return;
217
218     this->checkAttributes();
219
220     this->areClientAttributesChecked_ = true;
221   }
222
223   // Send all checked attributes to server
224   void CAxis::sendCheckedAttributes(const std::vector<int>& globalDim, int orderPositionInGrid,
225                                     CServerDistributionDescription::ServerDistributionType distType)
226   {
227     if (!this->areClientAttributesChecked_) checkAttributesOnClient(globalDim,
228                                                                     orderPositionInGrid,
229                                                                     distType);
230     CContext* context = CContext::getCurrent();
231
232     if (this->isChecked) return;
233     if (context->hasClient)
234     {
235       sendServerAttribut(globalDim, orderPositionInGrid, distType);
236     }
237
238     this->isChecked = true;
239   }
240
241  void CAxis::sendServerAttribut(const std::vector<int>& globalDim, int orderPositionInGrid,
242                                 CServerDistributionDescription::ServerDistributionType distType)
243  {
244    CContext* context = CContext::getCurrent();
245    CContextClient* client = context->client;
246
247    CServerDistributionDescription serverDescription(globalDim);
248
249    int nbServer = client->serverSize;
250
251    serverDescription.computeServerDistribution(nbServer, false, distType);
252    std::vector<std::vector<int> > serverIndexBegin = serverDescription.getServerIndexBegin();
253    std::vector<std::vector<int> > serverDimensionSizes = serverDescription.getServerDimensionSizes();
254
255    CEventClient event(getType(),EVENT_ID_SERVER_ATTRIBUT);
256    if (client->isServerLeader())
257    {
258      std::list<CMessage> msgs;
259
260      const std::list<int>& ranks = client->getRanksServerLeader();
261      for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
262      {
263        // Use const int to ensure CMessage holds a copy of the value instead of just a reference
264        const int begin = serverIndexBegin[*itRank][orderPositionInGrid];
265        const int ni    = serverDimensionSizes[*itRank][orderPositionInGrid];
266        const int end   = begin + ni - 1;
267
268        msgs.push_back(CMessage());
269        CMessage& msg = msgs.back();
270        msg << this->getId();
271        msg << ni << begin << end;
272
273        event.push(*itRank,1,msg);
274      }
275      client->sendEvent(event);
276    }
277    else client->sendEvent(event);
278  }
279
280  void CAxis::recvServerAttribut(CEventServer& event)
281  {
282    CBufferIn* buffer = event.subEvents.begin()->buffer;
283    string axisId;
284    *buffer >> axisId;
285    get(axisId)->recvServerAttribut(*buffer);
286  }
287
288  void CAxis::recvServerAttribut(CBufferIn& buffer)
289  {
290    int zoom_end = zoom_begin.getValue()+zoom_size.getValue()-1;
291    int ni_srv, begin_srv, end_srv;
292
293    buffer>>ni_srv>>begin_srv>>end_srv;
294
295    zoom_begin_srv = zoom_begin.getValue() > begin_srv ? zoom_begin.getValue() : begin_srv;
296    zoom_end_srv   = zoom_end < end_srv ? zoom_end : end_srv;
297    zoom_size_srv  = zoom_end_srv - zoom_begin_srv + 1;
298
299    if (zoom_size_srv<=0)
300    {
301      zoom_begin_srv = 0; zoom_end_srv = 0; zoom_size_srv = 0;
302    }
303
304    if (size == ni)
305    {
306      zoom_begin_srv = zoom_begin.getValue();
307      zoom_end_srv   = zoom_end;
308      zoom_size_srv  = zoom_end_srv - zoom_begin_srv + 1;
309    }
310  }
311
312  bool CAxis::hasTransformation()
313  {
314    return (!transformationMap_.empty());
315  }
316
317  void CAxis::setTransformations(const TransMapTypes& axisTrans)
318  {
319    transformationMap_ = axisTrans;
320  }
321
322  CAxis::TransMapTypes CAxis::getAllTransformations(void)
323  {
324    return transformationMap_;
325  }
326
327  /*!
328    Check the validity of all transformations applied on axis
329  This functions is called AFTER all inherited attributes are solved
330  */
331  void CAxis::checkTransformations()
332  {
333    TransMapTypes::const_iterator itb = transformationMap_.begin(), it,
334                                  ite = transformationMap_.end();
335    for (it = itb; it != ite; ++it)
336    {
337      (it->second)->checkValid(this);
338    }
339  }
340
341  void CAxis::solveInheritanceTransformation()
342  {
343    if (this->hasTransformation()) return;
344
345    std::vector<CAxis*> refAxis;
346    CAxis* refer_sptr;
347    CAxis* refer_ptr = this;
348    while (refer_ptr->hasDirectAxisReference())
349    {
350      refAxis.push_back(refer_ptr);
351      refer_sptr = refer_ptr->getDirectAxisReference();
352      refer_ptr  = refer_sptr;
353      if (refer_ptr->hasTransformation()) break;
354    }
355
356    if (refer_ptr->hasTransformation())
357      for (int idx = 0; idx < refAxis.size(); ++idx)
358        refAxis[idx]->setTransformations(refer_ptr->getAllTransformations());
359  }
360
361  void CAxis::parse(xml::CXMLNode & node)
362  {
363    SuperClass::parse(node);
364
365    if (node.goToChildElement())
366    {
367      StdString inverseAxisDefRoot("inverse_axis_definition");
368      StdString inverse("inverse_axis");
369      StdString zoomAxisDefRoot("zoom_axis_definition");
370      StdString zoom("zoom_axis");
371      do
372      {
373        if (node.getElementName() == inverse) {
374          CInverseAxis* tmp = (CInverseAxisGroup::get(inverseAxisDefRoot))->createChild();
375          tmp->parse(node);
376          transformationMap_[TRANS_INVERSE_AXIS] = tmp;
377        } else if (node.getElementName() == zoom) {
378          CZoomAxis* tmp = (CZoomAxisGroup::get(zoomAxisDefRoot))->createChild();
379          tmp->parse(node);
380          transformationMap_[TRANS_ZOOM_AXIS] = tmp;
381        }
382      } while (node.goToNextElement()) ;
383      node.goToParentElement();
384    }
385  }
386
387  DEFINE_REF_FUNC(Axis,axis)
388
389   ///---------------------------------------------------------------
390
391} // namespace xios
Note: See TracBrowser for help on using the repository browser.