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

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

Correcting a minor bug during merge

Test: NO

  • 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: 9.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 "xmlioserver_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   { /* Ne rien faire de plus */ }
20
21   CAxis::CAxis(const StdString & id)
22      : CObjectTemplate<CAxis>(id)
23      , CAxisAttributes(), isChecked(false), relFiles(), baseRefObject(), areClientAttributesChecked_(false)
24   { /* Ne rien faire de plus */ }
25
26   CAxis::~CAxis(void)
27   { /* Ne rien faire de plus */ }
28
29   ///---------------------------------------------------------------
30
31   const std::set<StdString> & CAxis::getRelFiles(void) const
32   {
33      return (this->relFiles);
34   }
35
36   bool CAxis::IsWritten(const StdString & filename) const
37   {
38      return (this->relFiles.find(filename) != this->relFiles.end());
39   }
40
41   void CAxis::addRelFile(const StdString & filename)
42   {
43      this->relFiles.insert(filename);
44   }
45
46   //----------------------------------------------------------------
47
48   StdString CAxis::GetName(void)   { return (StdString("axis")); }
49   StdString CAxis::GetDefName(void){ return (CAxis::GetName()); }
50   ENodeType CAxis::GetType(void)   { return (eAxis); }
51
52   //----------------------------------------------------------------
53
54   void CAxis::checkAttributes(void)
55   {
56      if (this->size.isEmpty())
57         ERROR("CAxis::checkAttributes(void)",
58               << "Attribute <size> of the axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] must be specified");
59      StdSize size = this->size.getValue();
60
61      if (!this->ibegin.isEmpty())
62      {
63        StdSize ibegin = this->ibegin.getValue();
64        if ((ibegin < 0) || (ibegin > size-1))
65          ERROR("CAxis::checkAttributes(void)",
66                << "Attribute <ibegin> of the axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] must be non-negative and smaller than size-1");
67      }
68      else this->ibegin.setValue(0);
69
70      if (!this->ni.isEmpty())
71      {
72        StdSize ni = this->ni.getValue();
73        if ((ni < 0) || (ni > size))
74          ERROR("CAxis::checkAttributes(void)",
75                << "Attribute <ni> of the axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] must be non-negative and smaller than size");
76      }
77      else this->ni.setValue(size);
78
79      StdSize true_size = value.numElements();
80      if (size != true_size)
81         ERROR("CAxis::checkAttributes(void)",
82               << "The array \'value\' of axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] has a different size that the one defined by the \'size\' attribute");
83
84      this->checkData();
85      this->checkMask();
86      this->checkZoom();
87   }
88
89   void CAxis::checkData()
90   {
91      if (data_begin.isEmpty()) data_begin.setValue(0);
92      if (!data_n.isEmpty() && data_n.getValue() <= 0)
93      {
94        ERROR("CAxis::checkData(void)",
95              << "Data dimension is negative (data_n).");
96      }
97      else if (data_n.isEmpty())
98        data_n.setValue(ni.getValue());
99
100      if (data_index.isEmpty())
101      {
102        int dn = data_n.getValue();
103        data_index.resize(dn);
104        for (int i = 0; i < dn; ++i) data_index(i) = (i+1);
105      }
106   }
107
108   void CAxis::checkZoom(void)
109   {
110      StdSize zoom_begin,zoom_end, zoom_size, axisSize;
111
112      zoom_begin = (this->zoom_begin.isEmpty()) ?  0 : this->zoom_begin.getValue() ;
113      zoom_size  = (this->zoom_size.isEmpty()) ?  size.getValue() : this->zoom_size.getValue() ;
114      zoom_end   = (this->zoom_end.isEmpty()) ?  (size.getValue() - 1) : this->zoom_end.getValue() ;
115
116      if (this->zoom_begin.isEmpty()) zoom_begin=zoom_end-zoom_size+1 ;
117      if (this->zoom_end.isEmpty()) zoom_end=zoom_begin+zoom_size-1 ;
118      if (this->zoom_size.isEmpty()) zoom_size=zoom_end-zoom_begin+1 ;
119      axisSize = size.getValue();
120
121      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))
122        ERROR("CAxis::checkAttributes(void)",
123              << "One or more attributes among <zoom_begin>, <zoom_end>, <zoom_size> of axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] are not well specified");
124
125      this->zoom_begin.setValue(zoom_begin) ;
126      this->zoom_end.setValue(zoom_end) ;
127      this->zoom_size.setValue(zoom_size) ;
128   }
129
130   void CAxis::checkMask()
131   {
132      int begin_mask = 0,
133          end_mask = ni.getValue()-1;
134
135      if (!zoom_begin.isEmpty())
136      {
137         int zoom_end = zoom_begin.getValue() + zoom_size.getValue() - 1;
138
139         begin_mask = std::max(ibegin.getValue(), zoom_begin.getValue());
140         end_mask   = std::min(ibegin.getValue() + ni.getValue()-1, zoom_end);
141
142         begin_mask -= ibegin.getValue();
143         end_mask   -= ibegin.getValue();
144      }
145
146
147      if (!mask.isEmpty())
148      {
149         if (mask.extent(0) != ni)
150            ERROR("CAxis::checkMask(void)",
151                  <<"the mask has not the same size than the local axis"<<endl
152                  <<"Local size is "<<ni<<"x"<<endl
153                  <<"Mask size is "<<mask.extent(0)<<"x");
154         for (int i = 0; i < ni; ++i)
155         {
156           if (i < begin_mask && i > end_mask)  mask(i) = false;
157         }
158      }
159      else // (!mask.hasValue())
160      { // Si aucun masque n'est défini,
161        // on en crée un nouveau qui valide l'intégralité du domaine.
162         mask.resize(ni);
163         for (int i = 0; i < ni.getValue(); ++i)
164         {
165               if (i >= begin_mask && i <= end_mask)
166                 mask(i) = true;
167               else  mask(i) = false;
168         }
169      }
170   }
171
172  bool CAxis::dispatchEvent(CEventServer& event)
173   {
174      if (SuperClass::dispatchEvent(event)) return true ;
175      else
176      {
177        switch(event.type)
178        {
179           case EVENT_ID_SERVER_ATTRIBUT :
180             recvServerAttribut(event) ;
181             return true ;
182             break ;
183           default :
184             ERROR("bool CContext::dispatchEvent(CEventServer& event)",
185                    <<"Unknown Event") ;
186           return false ;
187         }
188      }
189   }
190
191   void CAxis::checkAttributesOnClient(const std::vector<int>& globalDim, int orderPositionInGrid,
192                                       CServerDistributionDescription::ServerDistributionType distType)
193   {
194     if (this->areClientAttributesChecked_) return;
195     this->checkAttributes();
196
197     CContext* context=CContext::getCurrent() ;
198     if (context->hasClient)
199     {
200       computeServerIndex(globalDim, orderPositionInGrid, distType);
201     }
202
203     this->areClientAttributesChecked_ = true;
204   }
205
206   void CAxis::computeServerIndex(const std::vector<int>& globalDim, int orderPositionInGrid,
207                                  CServerDistributionDescription::ServerDistributionType distType)
208   {
209     CServerDistributionDescription serverDescription(globalDim);
210
211     CContext* context=CContext::getCurrent() ;
212     CContextClient* client=context->client ;
213     int nbServer=client->serverSize ;
214     int serverRank=client->getServerLeader() ;
215
216     serverDescription.computeServerDistribution(nbServer, false, distType);
217     std::vector<std::vector<int> > serverIndexBegin = serverDescription.getServerIndexBegin();
218     std::vector<std::vector<int> > serverDimensionSizes = serverDescription.getServerDimensionSizes();
219     begin_srv = (serverIndexBegin[serverRank])[orderPositionInGrid];
220     ni_srv = serverDimensionSizes[serverRank][orderPositionInGrid];
221     end_srv = begin_srv+ni_srv-1;
222   }
223
224   // Send all checked attributes to server
225   void CAxis::sendCheckedAttributes(const std::vector<int>& globalDim, int orderPositionInGrid,
226                                     CServerDistributionDescription::ServerDistributionType distType)
227   {
228     if (!this->areClientAttributesChecked_) checkAttributesOnClient(globalDim,
229                                                                     orderPositionInGrid,
230                                                                     distType);
231     CContext* context=CContext::getCurrent() ;
232
233     if (this->isChecked) return;
234     if (context->hasClient)
235     {
236       sendServerAttribut() ;
237     }
238
239     this->isChecked = true;
240   }
241
242  void CAxis::sendServerAttribut(void)
243  {
244    CContext* context=CContext::getCurrent();
245    CContextClient* client=context->client;
246
247    CEventClient event(getType(),EVENT_ID_SERVER_ATTRIBUT) ;
248    if (client->isServerLeader())
249    {
250      CMessage msg ;
251      msg<<this->getId() ;
252      msg<<ni_srv<<begin_srv<<end_srv;
253      event.push(client->getServerLeader(),1,msg) ;
254      client->sendEvent(event) ;
255    }
256    else client->sendEvent(event) ;
257  }
258
259  void CAxis::recvServerAttribut(CEventServer& event)
260  {
261    CBufferIn* buffer=event.subEvents.begin()->buffer;
262    string axisId ;
263    *buffer>>axisId ;
264    get(axisId)->recvServerAttribut(*buffer) ;
265  }
266
267  void CAxis::recvServerAttribut(CBufferIn& buffer)
268  {
269    int zoom_end = zoom_begin.getValue()+zoom_size.getValue()-1;
270    int ni_srv, begin_srv, end_srv;
271
272    buffer>>ni_srv>>begin_srv>>end_srv;
273
274    zoom_begin_srv = zoom_begin.getValue() > begin_srv ? zoom_begin.getValue() : begin_srv ;
275    zoom_end_srv   = zoom_end < end_srv ? zoom_end : end_srv ;
276    zoom_size_srv  = zoom_end_srv-zoom_begin_srv+1 ;
277
278    if (zoom_size_srv<=0)
279    {
280      zoom_begin_srv=0 ; zoom_end_srv=0 ; zoom_size_srv=0 ;
281    }
282  }
283
284   DEFINE_REF_FUNC(Axis,axis)
285
286   ///---------------------------------------------------------------
287
288} // namespace xios
Note: See TracBrowser for help on using the repository browser.