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

Last change on this file since 823 was 823, checked in by mhnguyen, 8 years ago

Implementing grid destination clone in case of two grid source

+) Clone attributes of grid destination as well as its transformation
+) Clean some redundant codes

Test
+) All tests pass

  • 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: 30.5 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_(), hasValue(false)
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_(), hasValue(false)
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::fillInValues(const CArray<double,1>& values)
190   {
191     this->value = values;
192   }
193
194   void CAxis::checkAttributes(void)
195   {
196      if (this->n_glo.isEmpty())
197        ERROR("CAxis::checkAttributes(void)",
198              << "[ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] "
199              << "The axis is wrongly defined, attribute 'n_glo' must be specified");
200      StdSize size = this->n_glo.getValue();
201
202      isDistributed_ = !this->begin.isEmpty() || !this->n.isEmpty();
203
204      if (!this->begin.isEmpty())
205      {
206        if (begin < 0 || begin > size - 1)
207          ERROR("CAxis::checkAttributes(void)",
208                << "[ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] "
209                << "The axis is wrongly defined, attribute 'begin' (" << begin.getValue() << ") must be non-negative and smaller than size-1 (" << size - 1 << ").");
210      }
211      else this->begin.setValue(0);
212
213      if (!this->n.isEmpty())
214      {
215        if (n < 0 || n > size)
216          ERROR("CAxis::checkAttributes(void)",
217                << "[ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] "
218                << "The axis is wrongly defined, attribute 'n' (" << n.getValue() << ") must be non-negative and smaller than size (" << size << ").");
219      }
220      else this->n.setValue(size);
221
222      if (!this->value.isEmpty())
223      {
224        StdSize true_size = value.numElements();
225        if (this->n.getValue() != true_size)
226          ERROR("CAxis::checkAttributes(void)",
227                << "[ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] "
228                << "The axis is wrongly defined, attribute 'value' has a different size (" << true_size << ") than the one defined by the \'size\' attribute (" << n.getValue() << ").");
229        this->hasValue = true;
230      }
231
232      if (this->index.isEmpty())
233      {
234        index.resize(n);
235        for (int i = 0; i < n; ++i) index(i) = i+begin;
236      }
237
238      this->checkData();
239      this->checkZoom();
240      this->checkMask();
241      this->checkBounds();
242   }
243
244   void CAxis::checkData()
245   {
246      if (data_begin.isEmpty()) data_begin.setValue(0);
247
248      if (data_n.isEmpty())
249      {
250        data_n.setValue(n);
251      }
252      else if (data_n.getValue() < 0)
253      {
254        ERROR("CAxis::checkData(void)",
255              << "[ id = " << this->getId() << " , context = '" << CObjectFactory::GetCurrentContextId() << " ] "
256              << "The data size should be strictly positive ('data_n' = " << data_n.getValue() << ").");
257      }
258
259      if (data_index.isEmpty())
260      {
261        data_index.resize(data_n);
262        for (int i = 0; i < data_n; ++i) data_index(i) = i;
263      }
264   }
265
266   void CAxis::checkZoom(void)
267   {
268     if (global_zoom_begin.isEmpty()) global_zoom_begin.setValue(0);
269     if (global_zoom_n.isEmpty()) global_zoom_n.setValue(n_glo.getValue());
270   }
271
272   void CAxis::checkMask()
273   {
274      if (!mask.isEmpty())
275      {
276         if (mask.extent(0) != n)
277           ERROR("CAxis::checkMask(void)",
278                 << "[ id = " << this->getId() << " , context = '" << CObjectFactory::GetCurrentContextId() << " ] "
279                 << "The mask does not have the same size as the local domain." << std::endl
280                 << "Local size is " << n.getValue() << "." << std::endl
281                 << "Mask size is " << mask.extent(0) << ".");
282      }
283      else // (mask.isEmpty())
284      { // If no mask was defined, we create a default one without any masked point.
285         mask.resize(n);
286         for (int i = 0; i < n; ++i)
287         {
288           mask(i) = true;
289         }
290      }
291   }
292
293  void CAxis::checkBounds()
294  {
295    if (!bounds.isEmpty())
296    {
297      if (bounds.extent(0) != 2 || bounds.extent(1) != n)
298        ERROR("CAxis::checkAttributes(void)",
299              << "The bounds array of the axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] must be of dimension 2 x axis size." << std::endl
300              << "Axis size is " << n.getValue() << "." << std::endl
301              << "Bounds size is "<< bounds.extent(0) << " x " << bounds.extent(1) << ".");
302      hasBounds_ = true;
303    }
304    else hasBounds_ = false;
305  }
306
307  void CAxis::checkEligibilityForCompressedOutput()
308  {
309    // We don't check if the mask is valid here, just if a mask has been defined at this point.
310    isCompressible_ = !mask.isEmpty();
311  }
312
313  bool CAxis::dispatchEvent(CEventServer& event)
314   {
315      if (SuperClass::dispatchEvent(event)) return true;
316      else
317      {
318        switch(event.type)
319        {
320           case EVENT_ID_SERVER_ATTRIBUT :
321             recvServerAttribut(event);
322             return true;
323             break;
324           case EVENT_ID_INDEX:
325            recvIndex(event);
326            return true;
327            break;
328          case EVENT_ID_DISTRIBUTED_VALUE:
329            recvDistributedValue(event);
330            return true;
331            break;
332          case EVENT_ID_NON_DISTRIBUTED_VALUE:
333            recvNonDistributedValue(event);
334            return true;
335            break;
336           default :
337             ERROR("bool CAxis::dispatchEvent(CEventServer& event)",
338                    << "Unknown Event");
339           return false;
340         }
341      }
342   }
343
344   void CAxis::checkAttributesOnClient()
345   {
346     if (this->areClientAttributesChecked_) return;
347
348     this->checkAttributes();
349
350     this->areClientAttributesChecked_ = true;
351   }
352
353   // Send all checked attributes to server
354   void CAxis::sendCheckedAttributes(const std::vector<int>& globalDim, int orderPositionInGrid,
355                                     CServerDistributionDescription::ServerDistributionType distType)
356   {
357     if (!this->areClientAttributesChecked_) checkAttributesOnClient();
358     CContext* context = CContext::getCurrent();
359
360     if (this->isChecked) return;
361     if (context->hasClient)
362     {
363       sendServerAttribut(globalDim, orderPositionInGrid, distType);
364       if (hasValue) sendValue(globalDim, orderPositionInGrid, distType);
365     }
366
367     this->isChecked = true;
368   }
369
370  void CAxis::sendValue(const std::vector<int>& globalDim, int orderPositionInGrid,
371                        CServerDistributionDescription::ServerDistributionType distType)
372  {
373     if (n.getValue() == n_glo.getValue())
374     {
375       sendNonDistributedValue();
376     }
377     else
378     {
379       computeConnectedServer(globalDim, orderPositionInGrid, distType);
380       sendDistributedValue();
381     }
382  }
383
384  void CAxis::computeConnectedServer(const std::vector<int>& globalDim, int orderPositionInGrid,
385                                     CServerDistributionDescription::ServerDistributionType distType)
386  {
387    CContext* context = CContext::getCurrent();
388    CContextClient* client = context->client;
389    int nbServer = client->serverSize;
390    int range, clientSize = client->clientSize;
391
392    size_t ni = this->n.getValue();
393    size_t ibegin = this->begin.getValue();
394    size_t zoom_end = global_zoom_begin+global_zoom_n-1;
395    size_t nZoomCount = 0;
396    size_t nbIndex = index.numElements();
397    for (size_t idx = 0; idx < nbIndex; ++idx)
398    {
399      size_t globalIndex = index(idx);
400      if (globalIndex >= global_zoom_begin && globalIndex <= zoom_end) ++nZoomCount;
401    }
402
403    CArray<size_t,1> globalIndexAxis(nbIndex);
404    std::vector<size_t> globalAxisZoom(nZoomCount);
405    nZoomCount = 0;
406    for (size_t idx = 0; idx < nbIndex; ++idx)
407    {
408      size_t globalIndex = index(idx);
409      globalIndexAxis(idx) = globalIndex;
410      if (globalIndex >= global_zoom_begin && globalIndex <= zoom_end)
411      {
412        globalAxisZoom[nZoomCount] = globalIndex;
413        ++nZoomCount;
414      }
415    }
416
417    std::set<int> writtenInd;
418    if (isCompressible_)
419    {
420      for (int idx = 0; idx < data_index.numElements(); ++idx)
421      {
422        int ind = CDistributionClient::getAxisIndex(data_index(idx), data_begin, ni);
423
424        if (ind >= 0 && ind < ni && mask(ind))
425        {
426          ind += ibegin;
427          if (ind >= global_zoom_begin && ind <= zoom_end)
428            writtenInd.insert(ind);
429        }
430      }
431    }
432
433    CServerDistributionDescription serverDescriptionGlobal(globalDim, nbServer);
434    int distributedDimensionOnServer = serverDescriptionGlobal.getDimensionDistributed();
435    std::map<int, std::vector<size_t> > globalIndexAxisOnServer;
436    if (distributedDimensionOnServer == orderPositionInGrid) // So we have distributed axis on client side and also on server side*
437    {
438      std::vector<int> nGlobAxis(1);
439      nGlobAxis[0] = n_glo.getValue();
440
441      size_t globalSizeIndex = 1, indexBegin, indexEnd;
442      for (int i = 0; i < nGlobAxis.size(); ++i) globalSizeIndex *= nGlobAxis[i];
443      indexBegin = 0;
444      for (int i = 0; i < clientSize; ++i)
445      {
446        range = globalSizeIndex / clientSize;
447        if (i < (globalSizeIndex%clientSize)) ++range;
448        if (i == client->clientRank) break;
449        indexBegin += range;
450      }
451      indexEnd = indexBegin + range - 1;
452
453      CServerDistributionDescription serverDescription(nGlobAxis, nbServer);
454      serverDescription.computeServerGlobalIndexInRange(std::make_pair<size_t,size_t>(indexBegin, indexEnd));
455      CClientServerMapping* clientServerMap = new CClientServerMappingDistributed(serverDescription.getGlobalIndexRange(), client->intraComm);
456      clientServerMap->computeServerIndexMapping(globalIndexAxis);
457      globalIndexAxisOnServer = clientServerMap->getGlobalIndexOnServer();
458      delete clientServerMap;
459    }
460    else
461    {
462      std::vector<size_t> globalIndexServer(n_glo.getValue());
463      for (size_t idx = 0; idx < n_glo.getValue(); ++idx)
464      {
465        globalIndexServer[idx] = idx;
466      }
467
468      for (int idx = 0; idx < nbServer; ++idx)
469      {
470        globalIndexAxisOnServer[idx] = globalIndexServer;
471      }
472    }
473
474    std::map<int, std::vector<size_t> >::const_iterator it = globalIndexAxisOnServer.begin(),
475                                                       ite = globalIndexAxisOnServer.end();
476    std::vector<size_t>::const_iterator itbVec = (globalAxisZoom).begin(),
477                                        iteVec = (globalAxisZoom).end();
478    indSrv_.clear();
479    indWrittenSrv_.clear();
480    for (; it != ite; ++it)
481    {
482      int rank = it->first;
483      const std::vector<size_t>& globalIndexTmp = it->second;
484      int nb = globalIndexTmp.size();
485
486      for (int i = 0; i < nb; ++i)
487      {
488        if (std::binary_search(itbVec, iteVec, globalIndexTmp[i]))
489        {
490          indSrv_[rank].push_back(globalIndexTmp[i]);
491        }
492
493        if (writtenInd.count(globalIndexTmp[i]))
494        {
495          indWrittenSrv_[rank].push_back(globalIndexTmp[i]);
496        }
497      }
498    }
499
500    connectedServerRank_.clear();
501    for (it = globalIndexAxisOnServer.begin(); it != ite; ++it) {
502      connectedServerRank_.push_back(it->first);
503    }
504
505    if (!indSrv_.empty())
506    {
507      connectedServerRank_.clear();
508      for (it = indSrv_.begin(); it != indSrv_.end(); ++it)
509        connectedServerRank_.push_back(it->first);
510    }
511    nbConnectedClients_ = CClientServerMapping::computeConnectedClients(client->serverSize, client->clientSize, client->intraComm, connectedServerRank_);
512  }
513
514  void CAxis::sendNonDistributedValue()
515  {
516    CContext* context = CContext::getCurrent();
517    CContextClient* client = context->client;
518    CEventClient event(getType(), EVENT_ID_NON_DISTRIBUTED_VALUE);
519
520    int zoom_end = global_zoom_begin + global_zoom_n - 1;
521    int nb = 0;
522    for (size_t idx = 0; idx < n; ++idx)
523    {
524      size_t globalIndex = begin + idx;
525      if (globalIndex >= global_zoom_begin && globalIndex <= zoom_end) ++nb;
526    }
527
528    int nbWritten = 0;
529    if (isCompressible_)
530    {
531      for (int idx = 0; idx < data_index.numElements(); ++idx)
532      {
533        int ind = CDistributionClient::getAxisIndex(data_index(idx), data_begin, n);
534
535        if (ind >= 0 && ind < n && mask(ind))
536        {
537          ind += begin;
538          if (ind >= global_zoom_begin && ind <= zoom_end)
539            ++nbWritten;
540        }
541      }
542    }
543
544    CArray<double,1> val(nb);
545    nb = 0;
546    for (size_t idx = 0; idx < n; ++idx)
547    {
548      size_t globalIndex = begin + idx;
549      if (globalIndex >= global_zoom_begin && globalIndex <= zoom_end)
550      {
551        val(nb) = value(idx);
552        ++nb;
553      }
554    }
555
556    CArray<int, 1> writtenInd(nbWritten);
557    nbWritten = 0;
558    if (isCompressible_)
559    {
560      for (int idx = 0; idx < data_index.numElements(); ++idx)
561      {
562        int ind = CDistributionClient::getAxisIndex(data_index(idx), data_begin, n);
563
564        if (ind >= 0 && ind < n && mask(ind))
565        {
566          ind += begin;
567          if (ind >= global_zoom_begin && ind <= zoom_end)
568          {
569            writtenInd(nbWritten) = ind;
570            ++nbWritten;
571          }
572        }
573      }
574    }
575
576    if (client->isServerLeader())
577    {
578      std::list<CMessage> msgs;
579
580      const std::list<int>& ranks = client->getRanksServerLeader();
581      for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
582      {
583        msgs.push_back(CMessage());
584        CMessage& msg = msgs.back();
585        msg << this->getId();
586        msg << val;
587        if (isCompressible_)
588          msg << writtenInd;
589        event.push(*itRank, 1, msg);
590      }
591      client->sendEvent(event);
592    }
593    else client->sendEvent(event);
594  }
595
596  void CAxis::sendDistributedValue(void)
597  {
598    int ns, n, i, j, ind, nv, idx;
599    CContext* context = CContext::getCurrent();
600    CContextClient* client=context->client;
601
602    // send value for each connected server
603    CEventClient eventIndex(getType(), EVENT_ID_INDEX);
604    CEventClient eventVal(getType(), EVENT_ID_DISTRIBUTED_VALUE);
605
606    list<CMessage> list_msgsIndex, list_msgsVal;
607    list<CArray<int,1> > list_indi;
608    list<CArray<int,1> > list_writtenInd;
609    list<CArray<double,1> > list_val;
610    list<CArray<double,2> > list_bounds;
611
612    std::map<int, std::vector<size_t> >::const_iterator it, iteMap;
613    iteMap = indSrv_.end();
614    for (int k = 0; k < connectedServerRank_.size(); ++k)
615    {
616      int nbData = 0;
617      int rank = connectedServerRank_[k];
618      it = indSrv_.find(rank);
619      if (iteMap != it)
620        nbData = it->second.size();
621
622      list_indi.push_back(CArray<int,1>(nbData));
623      list_val.push_back(CArray<double,1>(nbData));
624
625      if (hasBounds_)
626      {
627        list_bounds.push_back(CArray<double,2>(2,nbData));
628      }
629
630      CArray<int,1>& indi = list_indi.back();
631      CArray<double,1>& val = list_val.back();
632
633      for (n = 0; n < nbData; ++n)
634      {
635        idx = static_cast<int>(it->second[n]);
636        ind = idx - begin;
637
638        val(n) = value(ind);
639        indi(n) = idx;
640
641        if (hasBounds_)
642        {
643          CArray<double,2>& boundsVal = list_bounds.back();
644          boundsVal(0, n) = bounds(0,n);
645          boundsVal(1, n) = bounds(1,n);
646        }
647      }
648
649      list_msgsIndex.push_back(CMessage());
650      list_msgsIndex.back() << this->getId() << list_indi.back();
651
652      if (isCompressible_)
653      {
654        std::vector<int>& writtenIndSrc = indWrittenSrv_[rank];
655        list_writtenInd.push_back(CArray<int,1>(writtenIndSrc.size()));
656        CArray<int,1>& writtenInd = list_writtenInd.back();
657
658        for (n = 0; n < writtenInd.numElements(); ++n)
659          writtenInd(n) = writtenIndSrc[n];
660
661        list_msgsIndex.back() << writtenInd;
662      }
663
664      list_msgsVal.push_back(CMessage());
665      list_msgsVal.back() << this->getId() << list_val.back();
666
667      if (hasBounds_)
668      {
669        list_msgsVal.back() << list_bounds.back();
670      }
671
672      eventIndex.push(rank, nbConnectedClients_[rank], list_msgsIndex.back());
673      eventVal.push(rank, nbConnectedClients_[rank], list_msgsVal.back());
674    }
675
676    client->sendEvent(eventIndex);
677    client->sendEvent(eventVal);
678  }
679
680  void CAxis::recvIndex(CEventServer& event)
681  {
682    CAxis* axis;
683
684    list<CEventServer::SSubEvent>::iterator it;
685    for (it = event.subEvents.begin(); it != event.subEvents.end(); ++it)
686    {
687      CBufferIn* buffer = it->buffer;
688      string axisId;
689      *buffer >> axisId;
690      axis = get(axisId);
691      axis->recvIndex(it->rank, *buffer);
692    }
693
694    if (axis->isCompressible_)
695    {
696      std::sort(axis->indexesToWrite.begin(), axis->indexesToWrite.end());
697
698      CContextServer* server = CContext::getCurrent()->server;
699      axis->numberWrittenIndexes_ = axis->indexesToWrite.size();
700      MPI_Allreduce(&axis->numberWrittenIndexes_, &axis->totalNumberWrittenIndexes_, 1, MPI_INT, MPI_SUM, server->intraComm);
701      MPI_Scan(&axis->numberWrittenIndexes_, &axis->offsetWrittenIndexes_, 1, MPI_INT, MPI_SUM, server->intraComm);
702      axis->offsetWrittenIndexes_ -= axis->numberWrittenIndexes_;
703    }
704  }
705
706  void CAxis::recvIndex(int rank, CBufferIn& buffer)
707  {
708    buffer >> indiSrv_[rank];
709
710    if (isCompressible_)
711    {
712      CArray<int, 1> writtenIndexes;
713      buffer >> writtenIndexes;
714      indexesToWrite.reserve(indexesToWrite.size() + writtenIndexes.numElements());
715      for (int i = 0; i < writtenIndexes.numElements(); ++i)
716        indexesToWrite.push_back(writtenIndexes(i));
717    }
718  }
719
720  void CAxis::recvDistributedValue(CEventServer& event)
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      get(axisId)->recvDistributedValue(it->rank, *buffer);
729    }
730  }
731
732  void CAxis::recvDistributedValue(int rank, CBufferIn& buffer)
733  {
734    CArray<int,1> &indi = indiSrv_[rank];
735    CArray<double,1> val;
736    CArray<double,2> boundsVal;
737
738    buffer >> val;
739    if (hasBounds_) buffer >> boundsVal;
740
741    int i, j, ind_srv;
742    for (int ind = 0; ind < indi.numElements(); ++ind)
743    {
744      i = indi(ind);
745      ind_srv = i - zoom_begin_srv;
746      value_srv(ind_srv) = val(ind);
747      if (hasBounds_)
748      {
749        bound_srv(0,ind_srv) = boundsVal(0, ind);
750        bound_srv(1,ind_srv) = boundsVal(1, ind);
751      }
752    }
753  }
754
755   void CAxis::recvNonDistributedValue(CEventServer& event)
756  {
757    CAxis* axis;
758
759    list<CEventServer::SSubEvent>::iterator it;
760    for (it = event.subEvents.begin(); it != event.subEvents.end(); ++it)
761    {
762      CBufferIn* buffer = it->buffer;
763      string axisId;
764      *buffer >> axisId;
765      axis = get(axisId);
766      axis->recvNonDistributedValue(it->rank, *buffer);
767    }
768
769    if (axis->isCompressible_)
770    {
771      std::sort(axis->indexesToWrite.begin(), axis->indexesToWrite.end());
772
773      axis->numberWrittenIndexes_ = axis->totalNumberWrittenIndexes_ = axis->indexesToWrite.size();
774      axis->offsetWrittenIndexes_ = 0;
775    }
776  }
777
778  void CAxis::recvNonDistributedValue(int rank, CBufferIn& buffer)
779  {
780    CArray<double,1> val;
781    buffer >> val;
782
783    for (int ind = 0; ind < val.numElements(); ++ind)
784    {
785      value_srv(ind) = val(ind);
786      if (hasBounds_)
787      {
788        bound_srv(0,ind) = bounds(0,ind);
789        bound_srv(1,ind) = bounds(1,ind);
790      }
791    }
792
793    if (isCompressible_)
794    {
795      CArray<int, 1> writtenIndexes;
796      buffer >> writtenIndexes;
797      indexesToWrite.reserve(indexesToWrite.size() + writtenIndexes.numElements());
798      for (int i = 0; i < writtenIndexes.numElements(); ++i)
799        indexesToWrite.push_back(writtenIndexes(i));
800    }
801  }
802
803  void CAxis::sendServerAttribut(const std::vector<int>& globalDim, int orderPositionInGrid,
804                                 CServerDistributionDescription::ServerDistributionType distType)
805  {
806    CContext* context = CContext::getCurrent();
807    CContextClient* client = context->client;
808    int nbServer = client->serverSize;
809
810    CServerDistributionDescription serverDescription(globalDim, nbServer);
811    serverDescription.computeServerDistribution();
812
813    std::vector<std::vector<int> > serverIndexBegin = serverDescription.getServerIndexBegin();
814    std::vector<std::vector<int> > serverDimensionSizes = serverDescription.getServerDimensionSizes();
815
816    CEventClient event(getType(),EVENT_ID_SERVER_ATTRIBUT);
817    if (client->isServerLeader())
818    {
819      std::list<CMessage> msgs;
820
821      const std::list<int>& ranks = client->getRanksServerLeader();
822      for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
823      {
824        // Use const int to ensure CMessage holds a copy of the value instead of just a reference
825        const int begin = serverIndexBegin[*itRank][orderPositionInGrid];
826        const int ni    = serverDimensionSizes[*itRank][orderPositionInGrid];
827        const int end   = begin + ni - 1;
828
829        msgs.push_back(CMessage());
830        CMessage& msg = msgs.back();
831        msg << this->getId();
832        msg << ni << begin << end;
833        msg << global_zoom_begin.getValue() << global_zoom_n.getValue();
834        msg << isCompressible_;
835
836        event.push(*itRank,1,msg);
837      }
838      client->sendEvent(event);
839    }
840    else client->sendEvent(event);
841  }
842
843  void CAxis::recvServerAttribut(CEventServer& event)
844  {
845    CBufferIn* buffer = event.subEvents.begin()->buffer;
846    string axisId;
847    *buffer >> axisId;
848    get(axisId)->recvServerAttribut(*buffer);
849  }
850
851  void CAxis::recvServerAttribut(CBufferIn& buffer)
852  {
853    int ni_srv, begin_srv, end_srv, global_zoom_begin_tmp, global_zoom_n_tmp;
854
855    buffer >> ni_srv >> begin_srv >> end_srv;
856    buffer >> global_zoom_begin_tmp >> global_zoom_n_tmp;
857    buffer >> isCompressible_;
858    global_zoom_begin = global_zoom_begin_tmp;
859    global_zoom_n  = global_zoom_n_tmp;
860    int global_zoom_end = global_zoom_begin + global_zoom_n - 1;
861
862    zoom_begin_srv = global_zoom_begin > begin_srv ? global_zoom_begin : begin_srv ;
863    zoom_end_srv   = global_zoom_end < end_srv ? global_zoom_end : end_srv ;
864    zoom_size_srv  = zoom_end_srv - zoom_begin_srv + 1;
865
866    if (zoom_size_srv<=0)
867    {
868      zoom_begin_srv = 0; zoom_end_srv = 0; zoom_size_srv = 0;
869    }
870
871    if (n_glo == n)
872    {
873      zoom_begin_srv = global_zoom_begin;
874      zoom_end_srv   = global_zoom_end; //zoom_end;
875      zoom_size_srv  = zoom_end_srv - zoom_begin_srv + 1;
876    }
877    if (hasValue)
878    {
879      value_srv.resize(zoom_size_srv);
880      if (hasBounds_)  bound_srv.resize(2,zoom_size_srv);
881    }
882  }
883
884  bool CAxis::hasTransformation()
885  {
886    return (!transformationMap_.empty());
887  }
888
889  void CAxis::setTransformations(const TransMapTypes& axisTrans)
890  {
891    transformationMap_ = axisTrans;
892  }
893
894  CAxis::TransMapTypes CAxis::getAllTransformations(void)
895  {
896    return transformationMap_;
897  }
898
899  /*!
900    Check the validity of all transformations applied on axis
901  This functions is called AFTER all inherited attributes are solved
902  */
903  void CAxis::checkTransformations()
904  {
905    TransMapTypes::const_iterator itb = transformationMap_.begin(), it,
906                                  ite = transformationMap_.end();
907    for (it = itb; it != ite; ++it)
908    {
909      (it->second)->checkValid(this);
910    }
911  }
912
913  void CAxis::duplicateTransformation(CAxis* src)
914  {
915    if (src->hasTransformation())
916    {
917      this->setTransformations(src->getAllTransformations());
918    }
919  }
920
921  /*!
922   * Go through the hierarchy to find the axis from which the transformations must be inherited
923   */
924  void CAxis::solveInheritanceTransformation()
925  {
926    if (hasTransformation() || !hasDirectAxisReference())
927      return;
928
929    CAxis* axis = this;
930    std::vector<CAxis*> refAxis;
931    while (!axis->hasTransformation() && axis->hasDirectAxisReference())
932    {
933      refAxis.push_back(axis);
934      axis = axis->getDirectAxisReference();
935    }
936
937    if (axis->hasTransformation())
938      for (size_t i = 0; i < refAxis.size(); ++i)
939        refAxis[i]->setTransformations(axis->getAllTransformations());
940  }
941
942  void CAxis::parse(xml::CXMLNode & node)
943  {
944    SuperClass::parse(node);
945
946    if (node.goToChildElement())
947    {
948      StdString inverseAxisDefRoot("inverse_axis_definition");
949      StdString inverse("inverse_axis");
950      StdString zoomAxisDefRoot("zoom_axis_definition");
951      StdString zoom("zoom_axis");
952      StdString interpAxisDefRoot("interpolate_axis_definition");
953      StdString interp("interpolate_axis");
954      do
955      {
956        StdString nodeId("");
957        if (node.getAttributes().end() != node.getAttributes().find("id"))
958        { nodeId = node.getAttributes()["id"]; }
959
960        if (node.getElementName() == inverse) {
961          CInverseAxis* tmp = (CInverseAxisGroup::get(inverseAxisDefRoot))->createChild(nodeId);
962          tmp->parse(node);
963          transformationMap_.push_back(std::make_pair(TRANS_INVERSE_AXIS,tmp));
964        }
965        else if (node.getElementName() == zoom) {
966          CZoomAxis* tmp = (CZoomAxisGroup::get(zoomAxisDefRoot))->createChild(nodeId);
967          tmp->parse(node);
968          transformationMap_.push_back(std::make_pair(TRANS_ZOOM_AXIS,tmp));
969        }
970        else if (node.getElementName() == interp) {
971          CInterpolateAxis* tmp = (CInterpolateAxisGroup::get(interpAxisDefRoot))->createChild(nodeId);
972          tmp->parse(node);
973          transformationMap_.push_back(std::make_pair(TRANS_INTERPOLATE_AXIS,tmp));
974        }
975      } while (node.goToNextElement()) ;
976      node.goToParentElement();
977    }
978  }
979
980  DEFINE_REF_FUNC(Axis,axis)
981
982   ///---------------------------------------------------------------
983
984} // namespace xios
Note: See TracBrowser for help on using the repository browser.