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

Last change on this file since 772 was 772, checked in by rlacroix, 8 years ago

Axis/Domain?: Use the new infrastructure to get the output name.

  • 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: 28.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 "context_server.hpp"
11#include "xios_spl.hpp"
12#include "inverse_axis.hpp"
13#include "zoom_axis.hpp"
14#include "interpolate_axis.hpp"
15#include "server_distribution_description.hpp"
16#include "client_server_mapping_distributed.hpp"
17#include "distribution_client.hpp"
18
19namespace xios {
20
21   /// ////////////////////// Définitions ////////////////////// ///
22
23   CAxis::CAxis(void)
24      : CObjectTemplate<CAxis>()
25      , CAxisAttributes(), isChecked(false), relFiles(), areClientAttributesChecked_(false)
26      , isDistributed_(false), hasBounds_(false), isCompressible_(false)
27      , numberWrittenIndexes_(0), totalNumberWrittenIndexes_(0), offsetWrittenIndexes_(0)
28      , transformationMap_(), global_zoom_begin(0), global_zoom_size(0)
29   {
30   }
31
32   CAxis::CAxis(const StdString & id)
33      : CObjectTemplate<CAxis>(id)
34      , CAxisAttributes(), isChecked(false), relFiles(), areClientAttributesChecked_(false)
35      , isDistributed_(false), hasBounds_(false), isCompressible_(false)
36      , numberWrittenIndexes_(0), totalNumberWrittenIndexes_(0), offsetWrittenIndexes_(0)
37      , transformationMap_(), global_zoom_begin(0), global_zoom_size(0)
38   {
39   }
40
41   CAxis::~CAxis(void)
42   { /* Ne rien faire de plus */ }
43
44   ///---------------------------------------------------------------
45
46   const std::set<StdString> & CAxis::getRelFiles(void) const
47   {
48      return (this->relFiles);
49   }
50
51   bool CAxis::IsWritten(const StdString & filename) const
52   {
53      return (this->relFiles.find(filename) != this->relFiles.end());
54   }
55
56   bool CAxis::isWrittenCompressed(const StdString& filename) const
57   {
58      return (this->relFilesCompressed.find(filename) != this->relFilesCompressed.end());
59   }
60
61   bool CAxis::isDistributed(void) const
62   {
63      return isDistributed_;
64   }
65
66   /*!
67    * Test whether the data defined on the axis can be outputted in a compressed way.
68    *
69    * \return true if and only if a mask was defined for this axis
70    */
71   bool CAxis::isCompressible(void) const
72   {
73      return isCompressible_;
74   }
75
76   void CAxis::addRelFile(const StdString & filename)
77   {
78      this->relFiles.insert(filename);
79   }
80
81   void CAxis::addRelFileCompressed(const StdString& filename)
82   {
83      this->relFilesCompressed.insert(filename);
84   }
85
86   //----------------------------------------------------------------
87
88   const std::vector<int>& CAxis::getIndexesToWrite(void) const
89   {
90     return indexesToWrite;
91   }
92
93   /*!
94     Returns the number of indexes written by each server.
95     \return the number of indexes written by each server
96   */
97   int CAxis::getNumberWrittenIndexes() const
98   {
99     return numberWrittenIndexes_;
100   }
101
102   /*!
103     Returns the total number of indexes written by the servers.
104     \return the total number of indexes written by the servers
105   */
106   int CAxis::getTotalNumberWrittenIndexes() const
107   {
108     return totalNumberWrittenIndexes_;
109   }
110
111   /*!
112     Returns the offset of indexes written by each server.
113     \return the offset of indexes written by each server
114   */
115   int CAxis::getOffsetWrittenIndexes() const
116   {
117     return offsetWrittenIndexes_;
118   }
119
120   //----------------------------------------------------------------
121
122   /*!
123    * Compute the minimum buffer size required to send the attributes to the server(s).
124    *
125    * \return A map associating the server rank with its minimum buffer size.
126    */
127   std::map<int, StdSize> CAxis::getAttributesBufferSize()
128   {
129     CContextClient* client = CContext::getCurrent()->client;
130
131     std::map<int, StdSize> attributesSizes = getMinimumBufferSizeForAttributes();
132
133     bool isNonDistributed = (n == n_glo);
134
135     if (client->isServerLeader())
136     {
137       // size estimation for sendServerAttribut
138       size_t size = 6 * sizeof(size_t);
139       // size estimation for sendNonDistributedValue
140       if (isNonDistributed)
141         size = std::max(size, CArray<double,1>::size(n_glo) + (isCompressible_ ? CArray<int,1>::size(n_glo) : 0));
142       size += CEventClient::headerSize + getId().size() + sizeof(size_t);
143
144       const std::list<int>& ranks = client->getRanksServerLeader();
145       for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
146       {
147         if (size > attributesSizes[*itRank])
148           attributesSizes[*itRank] = size;
149       }
150     }
151
152     if (!isNonDistributed)
153     {
154       // size estimation for sendDistributedValue
155       std::map<int, std::vector<size_t> >::const_iterator it, ite = indSrv_.end();
156       for (it = indSrv_.begin(); it != ite; ++it)
157       {
158         size_t sizeIndexEvent = CArray<int,1>::size(it->second.size());
159         if (isCompressible_)
160           sizeIndexEvent += CArray<int,1>::size(indWrittenSrv_[it->first].size());
161
162         size_t sizeValEvent = CArray<double,1>::size(it->second.size());
163         if (hasBounds_)
164           sizeValEvent += CArray<double,2>::size(2 * it->second.size());
165
166         size_t size = CEventClient::headerSize + getId().size() + sizeof(size_t) + std::max(sizeIndexEvent, sizeValEvent);
167         if (size > attributesSizes[it->first])
168           attributesSizes[it->first] = size;
169       }
170     }
171
172     return attributesSizes;
173   }
174
175   //----------------------------------------------------------------
176
177   StdString CAxis::GetName(void)   { return (StdString("axis")); }
178   StdString CAxis::GetDefName(void){ return (CAxis::GetName()); }
179   ENodeType CAxis::GetType(void)   { return (eAxis); }
180
181   //----------------------------------------------------------------
182
183   CAxis* CAxis::createAxis()
184   {
185     CAxis* axis = CAxisGroup::get("axis_definition")->createChild();
186     return axis;
187   }
188
189   void CAxis::checkAttributes(void)
190   {
191      if (this->n_glo.isEmpty())
192        ERROR("CAxis::checkAttributes(void)",
193              << "[ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] "
194              << "The axis is wrongly defined, attribute 'n_glo' must be specified");
195      StdSize size = this->n_glo.getValue();
196
197      isDistributed_ = !this->begin.isEmpty() || !this->n.isEmpty();
198
199      if (!this->begin.isEmpty())
200      {
201        if (begin < 0 || begin > size - 1)
202          ERROR("CAxis::checkAttributes(void)",
203                << "[ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] "
204                << "The axis is wrongly defined, attribute 'begin' (" << begin.getValue() << ") must be non-negative and smaller than size-1 (" << size - 1 << ").");
205      }
206      else this->begin.setValue(0);
207
208      if (!this->n.isEmpty())
209      {
210        if (n < 0 || n > size)
211          ERROR("CAxis::checkAttributes(void)",
212                << "[ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] "
213                << "The axis is wrongly defined, attribute 'n' (" << n.getValue() << ") must be non-negative and smaller than size (" << size << ").");
214      }
215      else this->n.setValue(size);
216
217      StdSize true_size = value.numElements();
218      if (this->n.getValue() != true_size)
219        ERROR("CAxis::checkAttributes(void)",
220              << "[ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] "
221              << "The axis is wrongly defined, attribute 'value' has a different size (" << true_size << ") than the one defined by the \'size\' attribute (" << n.getValue() << ").");
222
223      this->checkData();
224      this->checkZoom();
225      this->checkMask();
226      this->checkBounds();
227   }
228
229   void CAxis::checkData()
230   {
231      if (data_begin.isEmpty()) data_begin.setValue(0);
232
233      if (data_n.isEmpty())
234      {
235        data_n.setValue(n);
236      }
237      else if (data_n.getValue() < 0)
238      {
239        ERROR("CAxis::checkData(void)",
240              << "[ id = " << this->getId() << " , context = '" << CObjectFactory::GetCurrentContextId() << " ] "
241              << "The data size should be strictly positive ('data_n' = " << data_n.getValue() << ").");
242      }
243
244      if (data_index.isEmpty())
245      {
246        data_index.resize(data_n);
247        for (int i = 0; i < data_n; ++i) data_index(i) = i;
248      }
249   }
250
251   void CAxis::checkZoom(void)
252   {
253     if (0 == global_zoom_size) global_zoom_size = this->n_glo.getValue();
254   }
255
256   void CAxis::checkMask()
257   {
258      if (!mask.isEmpty())
259      {
260         if (mask.extent(0) != n)
261           ERROR("CAxis::checkMask(void)",
262                 << "[ id = " << this->getId() << " , context = '" << CObjectFactory::GetCurrentContextId() << " ] "
263                 << "The mask does not have the same size as the local domain." << std::endl
264                 << "Local size is " << n.getValue() << "." << std::endl
265                 << "Mask size is " << mask.extent(0) << ".");
266      }
267      else // (mask.isEmpty())
268      { // If no mask was defined, we create a default one without any masked point.
269         mask.resize(n);
270         for (int i = 0; i < n; ++i)
271         {
272           mask(i) = true;
273         }
274      }
275   }
276
277  void CAxis::checkBounds()
278  {
279    if (!bounds.isEmpty())
280    {
281      if (bounds.extent(0) != 2 || bounds.extent(1) != n)
282        ERROR("CAxis::checkAttributes(void)",
283              << "The bounds array of the axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] must be of dimension 2 x axis size." << std::endl
284              << "Axis size is " << n.getValue() << "." << std::endl
285              << "Bounds size is "<< bounds.extent(0) << " x " << bounds.extent(1) << ".");
286      hasBounds_ = true;
287    }
288    else hasBounds_ = false;
289  }
290
291  void CAxis::checkEligibilityForCompressedOutput()
292  {
293    // We don't check if the mask is valid here, just if a mask has been defined at this point.
294    isCompressible_ = !mask.isEmpty();
295  }
296
297  bool CAxis::dispatchEvent(CEventServer& event)
298   {
299      if (SuperClass::dispatchEvent(event)) return true;
300      else
301      {
302        switch(event.type)
303        {
304           case EVENT_ID_SERVER_ATTRIBUT :
305             recvServerAttribut(event);
306             return true;
307             break;
308           case EVENT_ID_INDEX:
309            recvIndex(event);
310            return true;
311            break;
312          case EVENT_ID_DISTRIBUTED_VALUE:
313            recvDistributedValue(event);
314            return true;
315            break;
316          case EVENT_ID_NON_DISTRIBUTED_VALUE:
317            recvNonDistributedValue(event);
318            return true;
319            break;
320           default :
321             ERROR("bool CAxis::dispatchEvent(CEventServer& event)",
322                    << "Unknown Event");
323           return false;
324         }
325      }
326   }
327
328   void CAxis::checkAttributesOnClient()
329   {
330     if (this->areClientAttributesChecked_) return;
331
332     this->checkAttributes();
333
334     this->areClientAttributesChecked_ = true;
335   }
336
337   // Send all checked attributes to server
338   void CAxis::sendCheckedAttributes(const std::vector<int>& globalDim, int orderPositionInGrid,
339                                     CServerDistributionDescription::ServerDistributionType distType)
340   {
341     if (!this->areClientAttributesChecked_) checkAttributesOnClient();
342     CContext* context = CContext::getCurrent();
343
344     if (this->isChecked) return;
345     if (context->hasClient)
346     {
347       sendServerAttribut(globalDim, orderPositionInGrid, distType);
348       sendValue();
349     }
350
351     this->isChecked = true;
352   }
353
354  void CAxis::sendValue()
355  {
356     if (n.getValue() == n_glo.getValue())
357     {
358       sendNonDistributedValue();
359     }
360     else
361     {
362       computeConnectedServer();
363       sendDistributedValue();
364     }
365  }
366
367  void CAxis::computeConnectedServer()
368  {
369    CContext* context = CContext::getCurrent();
370    CContextClient* client = context->client;
371    int nbServer = client->serverSize;
372    int range, clientSize = client->clientSize;
373
374    size_t ni = this->n.getValue();
375    size_t ibegin = this->begin.getValue();
376    size_t zoom_end = global_zoom_begin+global_zoom_size-1;
377    size_t nZoomCount = 0;
378    for (size_t idx = 0; idx < ni; ++idx)
379    {
380      size_t globalIndex = ibegin + idx;
381
382      if (globalIndex >= global_zoom_begin && globalIndex <= zoom_end) ++nZoomCount;
383    }
384
385    CArray<size_t,1> globalIndexAxis(ni);
386    std::vector<size_t> globalAxisZoom(nZoomCount);
387    nZoomCount = 0;
388    for (size_t idx = 0; idx < ni; ++idx)
389    {
390      size_t globalIndex = ibegin + idx;
391      globalIndexAxis(idx) = globalIndex;
392      if (globalIndex >= global_zoom_begin && globalIndex <= zoom_end)
393      {
394        globalAxisZoom[nZoomCount] = globalIndex;
395        ++nZoomCount;
396      }
397    }
398
399    std::set<int> writtenInd;
400    if (isCompressible_)
401    {
402      for (int idx = 0; idx < data_index.numElements(); ++idx)
403      {
404        int ind = CDistributionClient::getAxisIndex(data_index(idx), data_begin, ni);
405
406        if (ind >= 0 && ind < ni && mask(ind))
407        {
408          ind += ibegin;
409          if (ind >= global_zoom_begin && ind <= zoom_end)
410            writtenInd.insert(ind);
411        }
412      }
413    }
414
415    std::vector<int> nGlobDomain(1);
416    nGlobDomain[0] = n_glo.getValue();
417
418    size_t globalSizeIndex = 1, indexBegin, indexEnd;
419    for (int i = 0; i < nGlobDomain.size(); ++i) globalSizeIndex *= nGlobDomain[i];
420    indexBegin = 0;
421    for (int i = 0; i < clientSize; ++i)
422    {
423      range = globalSizeIndex / clientSize;
424      if (i < (globalSizeIndex%clientSize)) ++range;
425      if (i == client->clientRank) break;
426      indexBegin += range;
427    }
428    indexEnd = indexBegin + range - 1;
429
430    CServerDistributionDescription serverDescription(nGlobDomain);
431    serverDescription.computeServerGlobalIndexInRange(nbServer, std::make_pair<size_t,size_t>(indexBegin, indexEnd), 0);
432    CClientServerMapping* clientServerMap = new CClientServerMappingDistributed(serverDescription.getGlobalIndexRange(), client->intraComm);
433    clientServerMap->computeServerIndexMapping(globalIndexAxis);
434    const std::map<int, std::vector<size_t> >& globalIndexAxisOnServer = clientServerMap->getGlobalIndexOnServer();
435
436    std::map<int, std::vector<size_t> >::const_iterator it = globalIndexAxisOnServer.begin(),
437                                                       ite = globalIndexAxisOnServer.end();
438    std::vector<size_t>::const_iterator itbVec = (globalAxisZoom).begin(),
439                                        iteVec = (globalAxisZoom).end();
440    indSrv_.clear();
441    indWrittenSrv_.clear();
442    for (; it != ite; ++it)
443    {
444      int rank = it->first;
445      const std::vector<size_t>& globalIndexTmp = it->second;
446      int nb = globalIndexTmp.size();
447
448      for (int i = 0; i < nb; ++i)
449      {
450        if (std::binary_search(itbVec, iteVec, globalIndexTmp[i]))
451        {
452          indSrv_[rank].push_back(globalIndexTmp[i]);
453        }
454
455        if (writtenInd.count(globalIndexTmp[i]))
456        {
457          indWrittenSrv_[rank].push_back(globalIndexTmp[i]);
458        }
459      }
460    }
461
462    connectedServerRank_.clear();
463    for (it = globalIndexAxisOnServer.begin(); it != ite; ++it) {
464      connectedServerRank_.push_back(it->first);
465    }
466
467    if (!indSrv_.empty())
468    {
469      connectedServerRank_.clear();
470      for (it = indSrv_.begin(); it != indSrv_.end(); ++it)
471        connectedServerRank_.push_back(it->first);
472    }
473    nbConnectedClients_ = clientServerMap->computeConnectedClients(client->serverSize, client->clientSize, client->intraComm, connectedServerRank_);
474    delete clientServerMap;
475  }
476
477  void CAxis::sendNonDistributedValue()
478  {
479    CContext* context = CContext::getCurrent();
480    CContextClient* client = context->client;
481    CEventClient event(getType(), EVENT_ID_NON_DISTRIBUTED_VALUE);
482
483    int zoom_end = global_zoom_begin + global_zoom_size - 1;
484    int nb = 0;
485    for (size_t idx = 0; idx < n; ++idx)
486    {
487      size_t globalIndex = begin + idx;
488      if (globalIndex >= global_zoom_begin && globalIndex <= zoom_end) ++nb;
489    }
490
491    int nbWritten = 0;
492    if (isCompressible_)
493    {
494      for (int idx = 0; idx < data_index.numElements(); ++idx)
495      {
496        int ind = CDistributionClient::getAxisIndex(data_index(idx), data_begin, n);
497
498        if (ind >= 0 && ind < n && mask(ind))
499        {
500          ind += begin;
501          if (ind >= global_zoom_begin && ind <= zoom_end)
502            ++nbWritten;
503        }
504      }
505    }
506
507    CArray<double,1> val(nb);
508    nb = 0;
509    for (size_t idx = 0; idx < n; ++idx)
510    {
511      size_t globalIndex = begin + idx;
512      if (globalIndex >= global_zoom_begin && globalIndex <= zoom_end)
513      {
514        val(nb) = value(idx);
515        ++nb;
516      }
517    }
518
519    CArray<int, 1> writtenInd(nbWritten);
520    nbWritten = 0;
521    if (isCompressible_)
522    {
523      for (int idx = 0; idx < data_index.numElements(); ++idx)
524      {
525        int ind = CDistributionClient::getAxisIndex(data_index(idx), data_begin, n);
526
527        if (ind >= 0 && ind < n && mask(ind))
528        {
529          ind += begin;
530          if (ind >= global_zoom_begin && ind <= zoom_end)
531          {
532            writtenInd(nbWritten) = ind;
533            ++nbWritten;
534          }
535        }
536      }
537    }
538
539    if (client->isServerLeader())
540    {
541      std::list<CMessage> msgs;
542
543      const std::list<int>& ranks = client->getRanksServerLeader();
544      for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
545      {
546        msgs.push_back(CMessage());
547        CMessage& msg = msgs.back();
548        msg << this->getId();
549        msg << val;
550        if (isCompressible_)
551          msg << writtenInd;
552        event.push(*itRank, 1, msg);
553      }
554      client->sendEvent(event);
555    }
556    else client->sendEvent(event);
557  }
558
559  void CAxis::sendDistributedValue(void)
560  {
561    int ns, n, i, j, ind, nv, idx;
562    CContext* context = CContext::getCurrent();
563    CContextClient* client=context->client;
564
565    // send value for each connected server
566    CEventClient eventIndex(getType(), EVENT_ID_INDEX);
567    CEventClient eventVal(getType(), EVENT_ID_DISTRIBUTED_VALUE);
568
569    list<CMessage> list_msgsIndex, list_msgsVal;
570    list<CArray<int,1> > list_indi;
571    list<CArray<int,1> > list_writtenInd;
572    list<CArray<double,1> > list_val;
573    list<CArray<double,2> > list_bounds;
574
575    std::map<int, std::vector<size_t> >::const_iterator it, iteMap;
576    iteMap = indSrv_.end();
577    for (int k = 0; k < connectedServerRank_.size(); ++k)
578    {
579      int nbData = 0;
580      int rank = connectedServerRank_[k];
581      it = indSrv_.find(rank);
582      if (iteMap != it)
583        nbData = it->second.size();
584
585      list_indi.push_back(CArray<int,1>(nbData));
586      list_val.push_back(CArray<double,1>(nbData));
587
588      if (hasBounds_)
589      {
590        list_bounds.push_back(CArray<double,2>(2,nbData));
591      }
592
593      CArray<int,1>& indi = list_indi.back();
594      CArray<double,1>& val = list_val.back();
595
596      for (n = 0; n < nbData; ++n)
597      {
598        idx = static_cast<int>(it->second[n]);
599        ind = idx - begin;
600
601        val(n) = value(ind);
602        indi(n) = idx;
603
604        if (hasBounds_)
605        {
606          CArray<double,2>& boundsVal = list_bounds.back();
607          boundsVal(0, n) = bounds(0,n);
608          boundsVal(1, n) = bounds(1,n);
609        }
610      }
611
612      list_msgsIndex.push_back(CMessage());
613      list_msgsIndex.back() << this->getId() << list_indi.back();
614
615      if (isCompressible_)
616      {
617        std::vector<int>& writtenIndSrc = indWrittenSrv_[rank];
618        list_writtenInd.push_back(CArray<int,1>(writtenIndSrc.size()));
619        CArray<int,1>& writtenInd = list_writtenInd.back();
620
621        for (n = 0; n < writtenInd.numElements(); ++n)
622          writtenInd(n) = writtenIndSrc[n];
623
624        list_msgsIndex.back() << writtenInd;
625      }
626
627      list_msgsVal.push_back(CMessage());
628      list_msgsVal.back() << this->getId() << list_val.back();
629
630      if (hasBounds_)
631      {
632        list_msgsVal.back() << list_bounds.back();
633      }
634
635      eventIndex.push(rank, nbConnectedClients_[rank], list_msgsIndex.back());
636      eventVal.push(rank, nbConnectedClients_[rank], list_msgsVal.back());
637    }
638
639    client->sendEvent(eventIndex);
640    client->sendEvent(eventVal);
641  }
642
643  void CAxis::recvIndex(CEventServer& event)
644  {
645    CAxis* axis;
646
647    list<CEventServer::SSubEvent>::iterator it;
648    for (it = event.subEvents.begin(); it != event.subEvents.end(); ++it)
649    {
650      CBufferIn* buffer = it->buffer;
651      string axisId;
652      *buffer >> axisId;
653      axis = get(axisId);
654      axis->recvIndex(it->rank, *buffer);
655    }
656
657    if (axis->isCompressible_)
658    {
659      std::sort(axis->indexesToWrite.begin(), axis->indexesToWrite.end());
660
661      CContextServer* server = CContext::getCurrent()->server;
662      axis->numberWrittenIndexes_ = axis->indexesToWrite.size();
663      MPI_Allreduce(&axis->numberWrittenIndexes_, &axis->totalNumberWrittenIndexes_, 1, MPI_INT, MPI_SUM, server->intraComm);
664      MPI_Scan(&axis->numberWrittenIndexes_, &axis->offsetWrittenIndexes_, 1, MPI_INT, MPI_SUM, server->intraComm);
665      axis->offsetWrittenIndexes_ -= axis->numberWrittenIndexes_;
666    }
667  }
668
669  void CAxis::recvIndex(int rank, CBufferIn& buffer)
670  {
671    buffer >> indiSrv_[rank];
672
673    if (isCompressible_)
674    {
675      CArray<int, 1> writtenIndexes;
676      buffer >> writtenIndexes;
677      indexesToWrite.reserve(indexesToWrite.size() + writtenIndexes.numElements());
678      for (int i = 0; i < writtenIndexes.numElements(); ++i)
679        indexesToWrite.push_back(writtenIndexes(i));
680    }
681  }
682
683  void CAxis::recvDistributedValue(CEventServer& event)
684  {
685    list<CEventServer::SSubEvent>::iterator it;
686    for (it = event.subEvents.begin(); it != event.subEvents.end(); ++it)
687    {
688      CBufferIn* buffer = it->buffer;
689      string axisId;
690      *buffer >> axisId;
691      get(axisId)->recvDistributedValue(it->rank, *buffer);
692    }
693  }
694
695  void CAxis::recvDistributedValue(int rank, CBufferIn& buffer)
696  {
697    CArray<int,1> &indi = indiSrv_[rank];
698    CArray<double,1> val;
699    CArray<double,2> boundsVal;
700
701    buffer >> val;
702    if (hasBounds_) buffer >> boundsVal;
703
704    int i, j, ind_srv;
705    for (int ind = 0; ind < indi.numElements(); ++ind)
706    {
707      i = indi(ind);
708      ind_srv = i - zoom_begin_srv;
709      value_srv(ind_srv) = val(ind);
710      if (hasBounds_)
711      {
712        bound_srv(0,ind_srv) = boundsVal(0, ind);
713        bound_srv(1,ind_srv) = boundsVal(1, ind);
714      }
715    }
716  }
717
718   void CAxis::recvNonDistributedValue(CEventServer& event)
719  {
720    CAxis* axis;
721
722    list<CEventServer::SSubEvent>::iterator it;
723    for (it = event.subEvents.begin(); it != event.subEvents.end(); ++it)
724    {
725      CBufferIn* buffer = it->buffer;
726      string axisId;
727      *buffer >> axisId;
728      axis = get(axisId);
729      axis->recvNonDistributedValue(it->rank, *buffer);
730    }
731
732    if (axis->isCompressible_)
733    {
734      std::sort(axis->indexesToWrite.begin(), axis->indexesToWrite.end());
735
736      axis->numberWrittenIndexes_ = axis->totalNumberWrittenIndexes_ = axis->indexesToWrite.size();
737      axis->offsetWrittenIndexes_ = 0;
738    }
739  }
740
741  void CAxis::recvNonDistributedValue(int rank, CBufferIn& buffer)
742  {
743    CArray<double,1> val;
744    buffer >> val;
745
746    for (int ind = 0; ind < val.numElements(); ++ind)
747    {
748      value_srv(ind) = val(ind);
749      if (hasBounds_)
750      {
751        bound_srv(0,ind) = bounds(0,ind);
752        bound_srv(1,ind) = bounds(1,ind);
753      }
754    }
755
756    if (isCompressible_)
757    {
758      CArray<int, 1> writtenIndexes;
759      buffer >> writtenIndexes;
760      indexesToWrite.reserve(indexesToWrite.size() + writtenIndexes.numElements());
761      for (int i = 0; i < writtenIndexes.numElements(); ++i)
762        indexesToWrite.push_back(writtenIndexes(i));
763    }
764  }
765
766  void CAxis::sendServerAttribut(const std::vector<int>& globalDim, int orderPositionInGrid,
767                                 CServerDistributionDescription::ServerDistributionType distType)
768  {
769    CContext* context = CContext::getCurrent();
770    CContextClient* client = context->client;
771
772    CServerDistributionDescription serverDescription(globalDim);
773
774    int nbServer = client->serverSize;
775
776    serverDescription.computeServerDistribution(nbServer, false, distType);
777    std::vector<std::vector<int> > serverIndexBegin = serverDescription.getServerIndexBegin();
778    std::vector<std::vector<int> > serverDimensionSizes = serverDescription.getServerDimensionSizes();
779
780    CEventClient event(getType(),EVENT_ID_SERVER_ATTRIBUT);
781    if (client->isServerLeader())
782    {
783      std::list<CMessage> msgs;
784
785      const std::list<int>& ranks = client->getRanksServerLeader();
786      for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
787      {
788        // Use const int to ensure CMessage holds a copy of the value instead of just a reference
789        const int begin = serverIndexBegin[*itRank][orderPositionInGrid];
790        const int ni    = serverDimensionSizes[*itRank][orderPositionInGrid];
791        const int end   = begin + ni - 1;
792
793        msgs.push_back(CMessage());
794        CMessage& msg = msgs.back();
795        msg << this->getId();
796        msg << ni << begin << end;
797        msg << global_zoom_begin << global_zoom_size;
798        msg << isCompressible_;
799
800        event.push(*itRank,1,msg);
801      }
802      client->sendEvent(event);
803    }
804    else client->sendEvent(event);
805  }
806
807  void CAxis::recvServerAttribut(CEventServer& event)
808  {
809    CBufferIn* buffer = event.subEvents.begin()->buffer;
810    string axisId;
811    *buffer >> axisId;
812    get(axisId)->recvServerAttribut(*buffer);
813  }
814
815  void CAxis::recvServerAttribut(CBufferIn& buffer)
816  {
817    int ni_srv, begin_srv, end_srv, global_zoom_begin_tmp, global_zoom_size_tmp;
818
819    buffer >> ni_srv >> begin_srv >> end_srv;
820    buffer >> global_zoom_begin_tmp >> global_zoom_size_tmp;
821    buffer >> isCompressible_;
822    global_zoom_begin = global_zoom_begin_tmp;
823    global_zoom_size  = global_zoom_size_tmp;
824    int global_zoom_end = global_zoom_begin + global_zoom_size - 1;
825
826    zoom_begin_srv = global_zoom_begin > begin_srv ? global_zoom_begin : begin_srv ;
827    zoom_end_srv   = global_zoom_end < end_srv ? global_zoom_end : end_srv ;
828    zoom_size_srv  = zoom_end_srv - zoom_begin_srv + 1;
829
830    if (zoom_size_srv<=0)
831    {
832      zoom_begin_srv = 0; zoom_end_srv = 0; zoom_size_srv = 0;
833    }
834
835    if (n_glo == n)
836    {
837      zoom_begin_srv = global_zoom_begin;
838      zoom_end_srv   = global_zoom_end; //zoom_end;
839      zoom_size_srv  = zoom_end_srv - zoom_begin_srv + 1;
840    }
841    value_srv.resize(zoom_size_srv);
842    bound_srv.resize(2,zoom_size_srv);
843  }
844
845  bool CAxis::hasTransformation()
846  {
847    return (!transformationMap_.empty());
848  }
849
850  void CAxis::setTransformations(const TransMapTypes& axisTrans)
851  {
852    transformationMap_ = axisTrans;
853  }
854
855  CAxis::TransMapTypes CAxis::getAllTransformations(void)
856  {
857    return transformationMap_;
858  }
859
860  /*!
861    Check the validity of all transformations applied on axis
862  This functions is called AFTER all inherited attributes are solved
863  */
864  void CAxis::checkTransformations()
865  {
866    TransMapTypes::const_iterator itb = transformationMap_.begin(), it,
867                                  ite = transformationMap_.end();
868    for (it = itb; it != ite; ++it)
869    {
870      (it->second)->checkValid(this);
871    }
872  }
873
874  /*!
875   * Go through the hierarchy to find the axis from which the transformations must be inherited
876   */
877  void CAxis::solveInheritanceTransformation()
878  {
879    if (hasTransformation() || !hasDirectAxisReference())
880      return;
881
882    CAxis* axis = this;
883    std::vector<CAxis*> refAxis;
884    while (!axis->hasTransformation() && axis->hasDirectAxisReference())
885    {
886      refAxis.push_back(axis);
887      axis = axis->getDirectAxisReference();
888    }
889
890    if (axis->hasTransformation())
891      for (size_t i = 0; i < refAxis.size(); ++i)
892        refAxis[i]->setTransformations(axis->getAllTransformations());
893  }
894
895  void CAxis::parse(xml::CXMLNode & node)
896  {
897    SuperClass::parse(node);
898
899    if (node.goToChildElement())
900    {
901      StdString inverseAxisDefRoot("inverse_axis_definition");
902      StdString inverse("inverse_axis");
903      StdString zoomAxisDefRoot("zoom_axis_definition");
904      StdString zoom("zoom_axis");
905      StdString interpAxisDefRoot("interpolate_axis_definition");
906      StdString interp("interpolate_axis");
907      do
908      {
909        if (node.getElementName() == inverse) {
910          CInverseAxis* tmp = (CInverseAxisGroup::get(inverseAxisDefRoot))->createChild();
911          tmp->parse(node);
912          transformationMap_.push_back(std::make_pair(TRANS_INVERSE_AXIS,tmp));
913        } else if (node.getElementName() == zoom) {
914          CZoomAxis* tmp = (CZoomAxisGroup::get(zoomAxisDefRoot))->createChild();
915          tmp->parse(node);
916          transformationMap_.push_back(std::make_pair(TRANS_ZOOM_AXIS,tmp));
917        }
918        else if (node.getElementName() == interp) {
919          CInterpolateAxis* tmp = (CInterpolateAxisGroup::get(interpAxisDefRoot))->createChild();
920          tmp->parse(node);
921          transformationMap_.push_back(std::make_pair(TRANS_INTERPOLATE_AXIS,tmp));
922        }
923      } while (node.goToNextElement()) ;
924      node.goToParentElement();
925    }
926  }
927
928  DEFINE_REF_FUNC(Axis,axis)
929
930   ///---------------------------------------------------------------
931
932} // namespace xios
Note: See TracBrowser for help on using the repository browser.