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

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

Implementing zooming on a domain

+) Add algorithm to do zooming on a domain
+) Remove some redundant codes

Test
+) On Curie
+) test_complete and test_client are correct

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