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

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

Generating interface for transformations

+) Update Fortran interface for other transformations
+) Remove some redundant files
+) Update test to new interface

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