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

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

Adding a new transformation: Reduce a domain to an axis

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