source: XIOS/dev/dev_olga/src/node/axis.cpp @ 1025

Last change on this file since 1025 was 1025, checked in by mhnguyen, 7 years ago

Merging working version of coupler

+) Add some changes of domain and axis: Retransfer the atttributes in a generic ways for each level of client (or server)
+) Remove some spoiled files from the previous commits

Test
+) No test

  • 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: 41.4 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      , isClientAfterTransformationChecked(false)
27      , isDistributed_(false), hasBounds_(false), isCompressible_(false)
28      , numberWrittenIndexes_(0), totalNumberWrittenIndexes_(0), offsetWrittenIndexes_(0)
29      , transformationMap_(), hasValue(false)
30   {
31   }
32
33   CAxis::CAxis(const StdString & id)
34      : CObjectTemplate<CAxis>(id)
35      , CAxisAttributes(), isChecked(false), relFiles(), areClientAttributesChecked_(false)
36      , isClientAfterTransformationChecked(false)
37      , isDistributed_(false), hasBounds_(false), isCompressible_(false)
38      , numberWrittenIndexes_(0), totalNumberWrittenIndexes_(0), offsetWrittenIndexes_(0)
39      , transformationMap_(), hasValue(false)
40   {
41   }
42
43   CAxis::~CAxis(void)
44   { /* Ne rien faire de plus */ }
45
46   std::map<StdString, ETranformationType> CAxis::transformationMapList_ = std::map<StdString, ETranformationType>();
47   bool CAxis::dummyTransformationMapList_ = CAxis::initializeTransformationMap(CAxis::transformationMapList_);
48   bool CAxis::initializeTransformationMap(std::map<StdString, ETranformationType>& m)
49   {
50     m["zoom_axis"] = TRANS_ZOOM_AXIS;
51     m["interpolate_axis"] = TRANS_INTERPOLATE_AXIS;
52     m["inverse_axis"] = TRANS_INVERSE_AXIS;
53     m["reduce_domain"] = TRANS_REDUCE_DOMAIN_TO_AXIS;
54     m["extract_domain"] = TRANS_EXTRACT_DOMAIN_TO_AXIS;
55   }
56
57   ///---------------------------------------------------------------
58
59   const std::set<StdString> & CAxis::getRelFiles(void) const
60   {
61      return (this->relFiles);
62   }
63
64   bool CAxis::IsWritten(const StdString & filename) const
65   {
66      return (this->relFiles.find(filename) != this->relFiles.end());
67   }
68
69   bool CAxis::isWrittenCompressed(const StdString& filename) const
70   {
71      return (this->relFilesCompressed.find(filename) != this->relFilesCompressed.end());
72   }
73
74   bool CAxis::isDistributed(void) const
75   {
76      return isDistributed_;
77   }
78
79   /*!
80    * Test whether the data defined on the axis can be outputted in a compressed way.
81    *
82    * \return true if and only if a mask was defined for this axis
83    */
84   bool CAxis::isCompressible(void) const
85   {
86      return isCompressible_;
87   }
88
89   void CAxis::addRelFile(const StdString & filename)
90   {
91      this->relFiles.insert(filename);
92   }
93
94   void CAxis::addRelFileCompressed(const StdString& filename)
95   {
96      this->relFilesCompressed.insert(filename);
97   }
98
99   //----------------------------------------------------------------
100
101   const std::vector<int>& CAxis::getIndexesToWrite(void) const
102   {
103     return indexesToWrite;
104   }
105
106   /*!
107     Returns the number of indexes written by each server.
108     \return the number of indexes written by each server
109   */
110   int CAxis::getNumberWrittenIndexes() const
111   {
112     return numberWrittenIndexes_;
113   }
114
115   /*!
116     Returns the total number of indexes written by the servers.
117     \return the total number of indexes written by the servers
118   */
119   int CAxis::getTotalNumberWrittenIndexes() const
120   {
121     return totalNumberWrittenIndexes_;
122   }
123
124   /*!
125     Returns the offset of indexes written by each server.
126     \return the offset of indexes written by each server
127   */
128   int CAxis::getOffsetWrittenIndexes() const
129   {
130     return offsetWrittenIndexes_;
131   }
132
133   /*!
134     Returns the start of indexes written by each server.
135     \return the start of indexes written by each server
136   */
137   int CAxis::getStartWriteIndex() const
138   {
139     return start_write_index_;
140   }
141
142   /*!
143     Returns the count of indexes written by each server.
144     \return the count of indexes written by each server
145   */
146   int CAxis::getCountWriteIndex() const
147   {
148     return count_write_index_;
149   }
150
151   /*!
152     Returns the local data written by each server.     
153   */
154   int CAxis::getLocalWriteSize() const
155   {
156     return local_write_size_;
157   }
158
159   /*!
160     Returns the global data written by all server.     
161   */
162   int CAxis::getGlobalWriteSize() const
163   {
164     return global_write_size_;
165   }
166
167   //----------------------------------------------------------------
168
169   /*!
170    * Compute the minimum buffer size required to send the attributes to the server(s).
171    *
172    * \return A map associating the server rank with its minimum buffer size.
173    */
174   std::map<int, StdSize> CAxis::getAttributesBufferSize()
175   {
176     CContextClient* client = CContext::getCurrent()->client;
177
178     std::map<int, StdSize> attributesSizes = getMinimumBufferSizeForAttributes();
179
180     bool isNonDistributed = (n == n_glo);
181
182     if (client->isServerLeader())
183     {
184       // size estimation for sendServerAttribut
185       size_t size = 6 * sizeof(size_t);
186       // size estimation for sendNonDistributedValue
187       if (isNonDistributed)
188         size = std::max(size, CArray<double,1>::size(n_glo) + (isCompressible_ ? CArray<int,1>::size(n_glo) : 0));
189       size += CEventClient::headerSize + getId().size() + sizeof(size_t);
190
191       const std::list<int>& ranks = client->getRanksServerLeader();
192       for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
193       {
194         if (size > attributesSizes[*itRank])
195           attributesSizes[*itRank] = size;
196       }
197     }
198
199     if (!isNonDistributed)
200     {
201       // size estimation for sendDistributedValue
202       boost::unordered_map<int, vector<size_t> >::const_iterator it, ite = indSrv_.end();
203       for (it = indSrv_.begin(); it != ite; ++it)
204       {
205         size_t sizeIndexEvent = CArray<int,1>::size(it->second.size());
206         if (isCompressible_)
207           sizeIndexEvent += CArray<int,1>::size(indWrittenSrv_[it->first].size());
208
209         size_t sizeValEvent = CArray<double,1>::size(it->second.size());
210         if (hasBounds_)
211           sizeValEvent += CArray<double,2>::size(2 * it->second.size());
212
213         size_t size = CEventClient::headerSize + getId().size() + sizeof(size_t) + std::max(sizeIndexEvent, sizeValEvent);
214         if (size > attributesSizes[it->first])
215           attributesSizes[it->first] = size;
216       }
217     }
218
219     return attributesSizes;
220   }
221
222   //----------------------------------------------------------------
223
224   StdString CAxis::GetName(void)   { return (StdString("axis")); }
225   StdString CAxis::GetDefName(void){ return (CAxis::GetName()); }
226   ENodeType CAxis::GetType(void)   { return (eAxis); }
227
228   //----------------------------------------------------------------
229
230   CAxis* CAxis::createAxis()
231   {
232     CAxis* axis = CAxisGroup::get("axis_definition")->createChild();
233     return axis;
234   }
235
236   void CAxis::fillInValues(const CArray<double,1>& values)
237   {
238     this->value = values;
239   }
240
241   void CAxis::checkAttributes(void)
242   {
243      if (this->n_glo.isEmpty())
244        ERROR("CAxis::checkAttributes(void)",
245              << "[ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] "
246              << "The axis is wrongly defined, attribute 'n_glo' must be specified");
247      StdSize size = this->n_glo.getValue();
248
249      if (!this->index.isEmpty())
250      {
251        if (n.isEmpty()) n = index.numElements();
252
253        // It's not so correct but if begin is not the first value of index
254        // then data on the local axis has user-defined distribution. In this case, begin has no meaning.
255        if (begin.isEmpty()) begin = index(0);         
256      }
257      else 
258      {
259        if (!this->begin.isEmpty())
260        {
261          if (begin < 0 || begin > size - 1)
262            ERROR("CAxis::checkAttributes(void)",
263                  << "[ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] "
264                  << "The axis is wrongly defined, attribute 'begin' (" << begin.getValue() << ") must be non-negative and smaller than size-1 (" << size - 1 << ").");
265        }
266        else this->begin.setValue(0);
267
268        if (!this->n.isEmpty())
269        {
270          if (n < 0 || n > size)
271            ERROR("CAxis::checkAttributes(void)",
272                  << "[ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] "
273                  << "The axis is wrongly defined, attribute 'n' (" << n.getValue() << ") must be non-negative and smaller than size (" << size << ").");
274        }
275        else this->n.setValue(size);
276
277        {
278          index.resize(n);
279          for (int i = 0; i < n; ++i) index(i) = i+begin;
280        }
281      }
282
283      if (!this->value.isEmpty())
284      {
285        StdSize true_size = value.numElements();
286        if (this->n.getValue() != true_size)
287          ERROR("CAxis::checkAttributes(void)",
288                << "[ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] "
289                << "The axis is wrongly defined, attribute 'value' has a different size (" << true_size << ") than the one defined by the \'size\' attribute (" << n.getValue() << ").");
290        this->hasValue = true;
291      }
292
293      this->checkData();
294      this->checkZoom();
295      this->checkMask();
296      this->checkBounds();
297
298      isDistributed_ = (!this->begin.isEmpty() && !this->n.isEmpty() && (this->begin + this->n < this->n_glo)) ||
299                       (!this->n.isEmpty() && (this->n != this->n_glo));
300   }
301
302   void CAxis::checkData()
303   {
304      if (data_begin.isEmpty()) data_begin.setValue(0);
305
306      if (data_n.isEmpty())
307      {
308        data_n.setValue(n);
309      }
310      else if (data_n.getValue() < 0)
311      {
312        ERROR("CAxis::checkData(void)",
313              << "[ id = " << this->getId() << " , context = '" << CObjectFactory::GetCurrentContextId() << " ] "
314              << "The data size should be strictly positive ('data_n' = " << data_n.getValue() << ").");
315      }
316
317      if (data_index.isEmpty())
318      {
319        data_index.resize(data_n);
320        for (int i = 0; i < data_n; ++i) data_index(i) = i;
321      }
322   }
323
324   void CAxis::checkZoom(void)
325   {
326     if (global_zoom_begin.isEmpty()) global_zoom_begin.setValue(0);
327     if (global_zoom_n.isEmpty()) global_zoom_n.setValue(n_glo.getValue());
328     if (zoom_index.isEmpty())
329     {
330       zoom_index.setValue(index.getValue());
331     }
332   }
333
334   void CAxis::checkMask()
335   {
336      if (!mask.isEmpty())
337      {
338         if (mask.extent(0) != n)
339           ERROR("CAxis::checkMask(void)",
340                 << "[ id = " << this->getId() << " , context = '" << CObjectFactory::GetCurrentContextId() << " ] "
341                 << "The mask does not have the same size as the local domain." << std::endl
342                 << "Local size is " << n.getValue() << "." << std::endl
343                 << "Mask size is " << mask.extent(0) << ".");
344      }
345      else // (mask.isEmpty())
346      { // If no mask was defined, we create a default one without any masked point.
347         mask.resize(n);
348         for (int i = 0; i < n; ++i)
349         {
350           mask(i) = true;
351         }
352      }
353   }
354
355  void CAxis::checkBounds()
356  {
357    if (!bounds.isEmpty())
358    {
359      if (bounds.extent(0) != 2 || bounds.extent(1) != n)
360        ERROR("CAxis::checkAttributes(void)",
361              << "The bounds array of the axis [ id = '" << getId() << "' , context = '" << CObjectFactory::GetCurrentContextId() << "' ] must be of dimension 2 x axis size." << std::endl
362              << "Axis size is " << n.getValue() << "." << std::endl
363              << "Bounds size is "<< bounds.extent(0) << " x " << bounds.extent(1) << ".");
364      hasBounds_ = true;
365    }
366    else hasBounds_ = false;
367  }
368
369  void CAxis::checkEligibilityForCompressedOutput()
370  {
371    // We don't check if the mask is valid here, just if a mask has been defined at this point.
372    isCompressible_ = !mask.isEmpty();
373  }
374
375  bool CAxis::dispatchEvent(CEventServer& event)
376   {
377      if (SuperClass::dispatchEvent(event)) return true;
378      else
379      {
380        switch(event.type)
381        {
382           // case EVENT_ID_SERVER_ATTRIBUT :
383           //   recvServerAttribut(event);
384           //   return true;
385           //   break;
386          //  case EVENT_ID_INDEX:
387          //   recvIndex(event);
388          //   return true;
389          //   break;
390          case EVENT_ID_NON_DISTRIBUTED_ATTRIBUTES:
391            recvNonDistributedAttributes(event);
392            return true;
393            break;
394          case EVENT_ID_DISTRIBUTED_ATTRIBUTES:
395            recvDistributedAttributes(event);
396            return true;
397            break;
398           default :
399             ERROR("bool CAxis::dispatchEvent(CEventServer& event)",
400                    << "Unknown Event");
401           return false;
402         }
403      }
404   }
405
406   void CAxis::checkAttributesOnClient()
407   {
408     if (this->areClientAttributesChecked_) return;
409
410     this->checkAttributes();
411
412     this->areClientAttributesChecked_ = true;
413   }
414
415   void CAxis::checkAttributesOnClientAfterTransformation(const std::vector<int>& globalDim, int orderPositionInGrid,
416                                                          CServerDistributionDescription::ServerDistributionType distType)
417   {
418     CContext* context=CContext::getCurrent() ;
419
420     if (this->isClientAfterTransformationChecked) return;
421     if (context->hasClient)
422     {
423       if (index.numElements() != n_glo.getValue()) computeConnectedServer(globalDim, orderPositionInGrid, distType);
424     }
425
426     this->isClientAfterTransformationChecked = true;
427   }
428
429   // Send all checked attributes to server
430   void CAxis::sendCheckedAttributes(const std::vector<int>& globalDim, int orderPositionInGrid,
431                                     CServerDistributionDescription::ServerDistributionType distType)
432   {
433     if (!this->areClientAttributesChecked_) checkAttributesOnClient();
434     if (!this->isClientAfterTransformationChecked) checkAttributesOnClientAfterTransformation(globalDim, orderPositionInGrid, distType);
435     CContext* context = CContext::getCurrent();
436
437     if (this->isChecked) return;
438     if (context->hasClient) sendAttributes();   
439
440     this->isChecked = true;
441   }
442
443  void CAxis::sendAttributes()
444  {
445     if (index.numElements() == n_glo.getValue())
446       sendNonDistributedAttributes();
447     else
448       sendDistributedAttributes();
449  }
450
451  void CAxis::computeConnectedServer(const std::vector<int>& globalDim, int orderPositionInGrid,
452                                     CServerDistributionDescription::ServerDistributionType distType)
453  {
454    CContext* context = CContext::getCurrent();
455    CContextClient* client = (0 != context->clientPrimServer) ? context->clientPrimServer : context->client;
456    int nbServer = client->serverSize;
457    int range, clientSize = client->clientSize;
458    int rank = client->clientRank;
459
460    // size_t ni = this->n.getValue();
461    // size_t ibegin = this->begin.getValue();
462    // size_t zoom_end = global_zoom_begin+global_zoom_n-1;
463    // size_t nZoomCount = 0;
464    size_t nbIndex = index.numElements();
465    for (size_t idx = 0; idx < nbIndex; ++idx)
466    {
467      globalLocalIndexMap_[index(idx)] = idx;
468      // size_t globalIndex = index(idx);
469      // if (globalIndex >= global_zoom_begin && globalIndex <= zoom_end) ++nZoomCount;
470    }
471
472    // CArray<size_t,1> globalIndexAxis(nbIndex);
473    // std::vector<size_t> globalAxisZoom(nZoomCount);
474    // nZoomCount = 0;
475    // for (size_t idx = 0; idx < nbIndex; ++idx)
476    // {
477    //   size_t globalIndex = index(idx);
478    //   globalIndexAxis(idx) = globalIndex;
479    //   if (globalIndex >= global_zoom_begin && globalIndex <= zoom_end)
480    //   {
481    //     globalAxisZoom[nZoomCount] = globalIndex;
482    //     ++nZoomCount;
483    //   }
484    // }
485
486    // std::set<int> writtenInd;
487    // if (isCompressible_)
488    // {
489    //   for (int idx = 0; idx < data_index.numElements(); ++idx)
490    //   {
491    //     int ind = CDistributionClient::getAxisIndex(data_index(idx), data_begin, ni);
492
493    //     if (ind >= 0 && ind < ni && mask(ind))
494    //     {
495    //       ind += ibegin;
496    //       if (ind >= global_zoom_begin && ind <= zoom_end)
497    //         writtenInd.insert(ind);
498    //     }
499    //   }
500    // }
501
502    CServerDistributionDescription serverDescriptionGlobal(globalDim, nbServer, distType);
503    int distributedDimensionOnServer = serverDescriptionGlobal.getDimensionDistributed();
504    CClientServerMapping::GlobalIndexMap globalIndexAxisOnServer;
505    if (distributedDimensionOnServer == orderPositionInGrid) // So we have distributed axis on client side and also on server side*
506    {
507      std::vector<int> nGlobAxis(1);
508      nGlobAxis[0] = n_glo.getValue();
509
510      size_t globalSizeIndex = 1, indexBegin, indexEnd;
511      for (int i = 0; i < nGlobAxis.size(); ++i) globalSizeIndex *= nGlobAxis[i];
512      indexBegin = 0;
513      if (globalSizeIndex <= clientSize)
514      {
515        indexBegin = rank%globalSizeIndex;
516        indexEnd = indexBegin;
517      }
518      else
519      {
520        for (int i = 0; i < clientSize; ++i)
521        {
522          range = globalSizeIndex / clientSize;
523          if (i < (globalSizeIndex%clientSize)) ++range;
524          if (i == client->clientRank) break;
525          indexBegin += range;
526        }
527        indexEnd = indexBegin + range - 1;
528      }
529
530      CArray<size_t,1> globalIndex(index.numElements());
531      for (size_t idx = 0; idx < globalIndex.numElements(); ++idx)
532        globalIndex(idx) = index(idx);
533
534      CServerDistributionDescription serverDescription(nGlobAxis, nbServer);
535      serverDescription.computeServerGlobalIndexInRange(std::make_pair<size_t,size_t>(indexBegin, indexEnd));
536      CClientServerMapping* clientServerMap = new CClientServerMappingDistributed(serverDescription.getGlobalIndexRange(), client->intraComm);
537      clientServerMap->computeServerIndexMapping(globalIndex);
538      globalIndexAxisOnServer = clientServerMap->getGlobalIndexOnServer();
539      delete clientServerMap;
540    }
541    else
542    {
543      std::vector<size_t> globalIndexServer(n_glo.getValue());
544      for (size_t idx = 0; idx < n_glo.getValue(); ++idx)
545      {
546        globalIndexServer[idx] = idx;
547      }
548
549      for (int idx = 0; idx < nbServer; ++idx)
550      {
551        globalIndexAxisOnServer[idx] = globalIndexServer;
552      }
553    }
554
555    indSrv_.swap(globalIndexAxisOnServer);
556
557    // CClientServerMapping::GlobalIndexMap::const_iterator it = globalIndexAxisOnServer.begin(),
558    //                                                      ite = globalIndexAxisOnServer.end();
559    CClientServerMapping::GlobalIndexMap::const_iterator it = indSrv_.begin(),
560                                                         ite = indSrv_.end();
561    // std::vector<size_t>::const_iterator itbVec = (globalAxisZoom).begin(),
562    //                                     iteVec = (globalAxisZoom).end();
563    // indSrv_.clear();
564    // indWrittenSrv_.clear();
565    // for (; it != ite; ++it)
566    // {
567    //   int rank = it->first;
568    //   const std::vector<size_t>& globalIndexTmp = it->second;
569    //   int nb = globalIndexTmp.size();
570
571    //   for (int i = 0; i < nb; ++i)
572    //   {
573    //     if (std::binary_search(itbVec, iteVec, globalIndexTmp[i]))
574    //     {
575    //       indSrv_[rank].push_back(globalIndexTmp[i]);
576    //     }
577
578    //     if (writtenInd.count(globalIndexTmp[i]))
579    //     {
580    //       indWrittenSrv_[rank].push_back(globalIndexTmp[i]);
581    //     }
582    //   }
583    // }
584
585    connectedServerRank_.clear();
586    for (it = indSrv_.begin(); it != ite; ++it) {
587      connectedServerRank_.push_back(it->first);
588    }
589
590    // if (!indSrv_.empty())
591    // {
592    //   std::map<int, vector<size_t> >::const_iterator itIndSrv  = indSrv_.begin(),
593    //                                                  iteIndSrv = indSrv_.end();
594    //   connectedServerRank_.clear();
595    //   for (; itIndSrv != iteIndSrv; ++itIndSrv)
596    //     connectedServerRank_.push_back(itIndSrv->first);
597    // }
598    nbConnectedClients_ = CClientServerMapping::computeConnectedClients(client->serverSize, client->clientSize, client->intraComm, connectedServerRank_);
599  }
600
601
602  // void CAxis::computeConnectedServer(const std::vector<int>& globalDim, int orderPositionInGrid,
603  //                                    CServerDistributionDescription::ServerDistributionType distType)
604  // {
605  //   CContext* context = CContext::getCurrent();
606  //   CContextClient* client = (0 != context->clientPrimServer) ? context->clientPrimServer : context->client;
607  //   int nbServer = client->serverSize;
608  //   int range, clientSize = client->clientSize;
609  //   int rank = client->clientRank;
610
611  //   size_t ni = this->n.getValue();
612  //   size_t ibegin = this->begin.getValue();
613  //   size_t zoom_end = global_zoom_begin+global_zoom_n-1;
614  //   size_t nZoomCount = 0;
615  //   size_t nbIndex = index.numElements();
616  //   for (size_t idx = 0; idx < nbIndex; ++idx)
617  //   {
618  //     size_t globalIndex = index(idx);
619  //     if (globalIndex >= global_zoom_begin && globalIndex <= zoom_end) ++nZoomCount;
620  //   }
621
622  //   CArray<size_t,1> globalIndexAxis(nbIndex);
623  //   std::vector<size_t> globalAxisZoom(nZoomCount);
624  //   nZoomCount = 0;
625  //   for (size_t idx = 0; idx < nbIndex; ++idx)
626  //   {
627  //     size_t globalIndex = index(idx);
628  //     globalIndexAxis(idx) = globalIndex;
629  //     if (globalIndex >= global_zoom_begin && globalIndex <= zoom_end)
630  //     {
631  //       globalAxisZoom[nZoomCount] = globalIndex;
632  //       ++nZoomCount;
633  //     }
634  //   }
635
636  //   std::set<int> writtenInd;
637  //   if (isCompressible_)
638  //   {
639  //     for (int idx = 0; idx < data_index.numElements(); ++idx)
640  //     {
641  //       int ind = CDistributionClient::getAxisIndex(data_index(idx), data_begin, ni);
642
643  //       if (ind >= 0 && ind < ni && mask(ind))
644  //       {
645  //         ind += ibegin;
646  //         if (ind >= global_zoom_begin && ind <= zoom_end)
647  //           writtenInd.insert(ind);
648  //       }
649  //     }
650  //   }
651
652  //   CServerDistributionDescription serverDescriptionGlobal(globalDim, nbServer);
653  //   int distributedDimensionOnServer = serverDescriptionGlobal.getDimensionDistributed();
654  //   CClientServerMapping::GlobalIndexMap globalIndexAxisOnServer;
655  //   if (distributedDimensionOnServer == orderPositionInGrid) // So we have distributed axis on client side and also on server side*
656  //   {
657  //     std::vector<int> nGlobAxis(1);
658  //     nGlobAxis[0] = n_glo.getValue();
659
660  //     size_t globalSizeIndex = 1, indexBegin, indexEnd;
661  //     for (int i = 0; i < nGlobAxis.size(); ++i) globalSizeIndex *= nGlobAxis[i];
662  //     indexBegin = 0;
663  //     if (globalSizeIndex <= clientSize)
664  //     {
665  //       indexBegin = rank%globalSizeIndex;
666  //       indexEnd = indexBegin;
667  //     }
668  //     else
669  //     {
670  //       for (int i = 0; i < clientSize; ++i)
671  //       {
672  //         range = globalSizeIndex / clientSize;
673  //         if (i < (globalSizeIndex%clientSize)) ++range;
674  //         if (i == client->clientRank) break;
675  //         indexBegin += range;
676  //       }
677  //       indexEnd = indexBegin + range - 1;
678  //     }
679
680  //     CServerDistributionDescription serverDescription(nGlobAxis, nbServer);
681  //     serverDescription.computeServerGlobalIndexInRange(std::make_pair<size_t,size_t>(indexBegin, indexEnd));
682  //     CClientServerMapping* clientServerMap = new CClientServerMappingDistributed(serverDescription.getGlobalIndexRange(), client->intraComm);
683  //     clientServerMap->computeServerIndexMapping(globalIndexAxis);
684  //     globalIndexAxisOnServer = clientServerMap->getGlobalIndexOnServer();
685  //     delete clientServerMap;
686  //   }
687  //   else
688  //   {
689  //     std::vector<size_t> globalIndexServer(n_glo.getValue());
690  //     for (size_t idx = 0; idx < n_glo.getValue(); ++idx)
691  //     {
692  //       globalIndexServer[idx] = idx;
693  //     }
694
695  //     for (int idx = 0; idx < nbServer; ++idx)
696  //     {
697  //       globalIndexAxisOnServer[idx] = globalIndexServer;
698  //     }
699  //   }
700
701  //   CClientServerMapping::GlobalIndexMap::const_iterator it = globalIndexAxisOnServer.begin(),
702  //                                                        ite = globalIndexAxisOnServer.end();
703  //   std::vector<size_t>::const_iterator itbVec = (globalAxisZoom).begin(),
704  //                                       iteVec = (globalAxisZoom).end();
705  //   indSrv_.clear();
706  //   indWrittenSrv_.clear();
707  //   for (; it != ite; ++it)
708  //   {
709  //     int rank = it->first;
710  //     const std::vector<size_t>& globalIndexTmp = it->second;
711  //     int nb = globalIndexTmp.size();
712
713  //     for (int i = 0; i < nb; ++i)
714  //     {
715  //       if (std::binary_search(itbVec, iteVec, globalIndexTmp[i]))
716  //       {
717  //         indSrv_[rank].push_back(globalIndexTmp[i]);
718  //       }
719
720  //       if (writtenInd.count(globalIndexTmp[i]))
721  //       {
722  //         indWrittenSrv_[rank].push_back(globalIndexTmp[i]);
723  //       }
724  //     }
725  //   }
726
727  //   connectedServerRank_.clear();
728  //   for (it = globalIndexAxisOnServer.begin(); it != ite; ++it) {
729  //     connectedServerRank_.push_back(it->first);
730  //   }
731
732  //   if (!indSrv_.empty())
733  //   {
734  //     std::map<int, vector<size_t> >::const_iterator itIndSrv  = indSrv_.begin(),
735  //                                                    iteIndSrv = indSrv_.end();
736  //     connectedServerRank_.clear();
737  //     for (; itIndSrv != iteIndSrv; ++itIndSrv)
738  //       connectedServerRank_.push_back(itIndSrv->first);
739  //   }
740  //   nbConnectedClients_ = CClientServerMapping::computeConnectedClients(client->serverSize, client->clientSize, client->intraComm, connectedServerRank_);
741  // }
742
743  void CAxis::sendNonDistributedAttributes()
744  {
745    CContext* context = CContext::getCurrent();
746    CContextClient* client = (0 != context->clientPrimServer) ? context->clientPrimServer : context->client;
747   
748    CEventClient event(getType(), EVENT_ID_NON_DISTRIBUTED_ATTRIBUTES);
749    size_t nbIndex = index.numElements();
750    size_t nbDataIndex = 0;
751
752    for (int idx = 0; idx < data_index.numElements(); ++idx)
753    {     
754      int ind = data_index(idx);
755      if (ind >= 0 && ind < nbIndex) ++nbDataIndex;
756    }
757
758    CArray<int,1> dataIndex(nbDataIndex);
759    nbDataIndex = 0;
760    for (int idx = 0; idx < data_index.numElements(); ++idx)
761    {     
762      int ind = data_index(idx);
763      if (ind >= 0 && ind < nbIndex)
764      {
765        dataIndex(nbDataIndex) = ind;
766        ++nbDataIndex;
767      } 
768    }
769
770    if (client->isServerLeader())
771    { 
772      std::list<CMessage> msgs;
773
774      const std::list<int>& ranks = client->getRanksServerLeader();
775      for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
776      {
777        msgs.push_back(CMessage());
778        CMessage& msg = msgs.back();
779        msg << this->getId();
780        msg << index.getValue() << dataIndex << zoom_index.getValue() << mask.getValue();
781        msg << hasValue;
782        if (hasValue) msg << value.getValue();
783
784        msg << hasBounds_;
785        if (hasBounds_) msg << bounds.getValue();
786       
787        event.push(*itRank, 1, msg);
788      }
789      client->sendEvent(event);
790    }
791    else client->sendEvent(event);
792  }
793
794  void CAxis::recvNonDistributedAttributes(CEventServer& event)
795  {
796    list<CEventServer::SSubEvent>::iterator it;
797    for (it = event.subEvents.begin(); it != event.subEvents.end(); ++it)
798    {
799      CBufferIn* buffer = it->buffer;
800      string axisId;
801      *buffer >> axisId;
802      get(axisId)->recvNonDistributedAttributes(it->rank, *buffer);
803    }
804  }
805
806  void CAxis::recvNonDistributedAttributes(int rank, CBufferIn& buffer)
807  { 
808    CArray<int,1> tmp_index, tmp_data_index, tmp_zoom_index;
809    CArray<bool,1> tmp_mask;
810    CArray<double,1> tmp_val;
811    CArray<double,2> tmp_bnds;
812
813    buffer >> tmp_index;
814    index.reference(tmp_index);
815    buffer >> tmp_data_index;
816    data_index.reference(tmp_data_index);
817    buffer >> tmp_zoom_index;
818    zoom_index.reference(tmp_zoom_index);
819    buffer >> tmp_mask;
820    mask.reference(tmp_mask);
821    buffer >> hasValue;
822    if (hasValue)
823    {
824      buffer >> tmp_val;
825      value.reference(tmp_val);
826    }
827
828    buffer >> hasBounds_;
829    if (hasBounds_)
830    {
831      buffer >> tmp_bnds;
832      bounds.reference(tmp_bnds);
833    }
834
835    {     
836      count_write_index_ = zoom_index.numElements();           
837      start_write_index_ = 0;
838      local_write_size_ = count_write_index_;
839      global_write_size_ = count_write_index_;
840    }
841  }
842
843  void CAxis::sendDistributedAttributes(void)
844  {
845    int ns, n, i, j, ind, nv, idx;
846    CContext* context = CContext::getCurrent();
847    CContextClient* client = (0 != context->clientPrimServer) ? context->clientPrimServer : context->client;
848
849    CEventClient eventData(getType(), EVENT_ID_DISTRIBUTED_ATTRIBUTES);
850   
851    list<CMessage> listData;
852    list<CArray<int,1> > list_indi, list_dataInd, list_zoomInd;   
853    list<CArray<bool,1> > list_mask;
854    list<CArray<double,1> > list_val;
855    list<CArray<double,2> > list_bounds;
856
857    int nbIndex = index.numElements();
858    CArray<int,1> dataIndex(nbIndex);
859    dataIndex = -1;
860    for (int idx = 0; idx < data_index.numElements(); ++idx)
861    {
862      if (0 <= data_index(idx) && data_index(idx) < nbIndex)
863        dataIndex(idx) = data_index(idx);
864    }
865
866    boost::unordered_map<int, std::vector<size_t> >::const_iterator it, iteMap;
867    iteMap = indSrv_.end();
868    for (int k = 0; k < connectedServerRank_.size(); ++k)
869    {
870      int nbData = 0;
871      int rank = connectedServerRank_[k];
872      int nbSendingClient = nbConnectedClients_[rank];
873      it = indSrv_.find(rank);
874      if (iteMap != it)
875        nbData = it->second.size();
876
877      list_indi.push_back(CArray<int,1>(nbData));
878      list_dataInd.push_back(CArray<int,1>(nbData));
879      list_zoomInd.push_back(CArray<int,1>(nbData));
880      list_mask.push_back(CArray<bool,1>(nbData));
881
882
883      if (hasValue)
884        list_val.push_back(CArray<double,1>(nbData));
885
886      if (hasBounds_)
887      {
888        list_bounds.push_back(CArray<double,2>(2,nbData));
889      }
890
891      CArray<int,1>& indi = list_indi.back();
892      CArray<int,1>& dataIndi = list_dataInd.back();
893      CArray<int,1>& zoomIndi = list_zoomInd.back();
894      CArray<bool,1>& maskIndi = list_mask.back();     
895
896      for (n = 0; n < nbData; ++n)
897      {
898        idx = static_cast<int>(it->second[n]);
899        indi(n) = idx;
900
901        ind = globalLocalIndexMap_[idx];
902        dataIndi(n) = dataIndex(ind);
903        maskIndi(n) = mask(ind);
904        zoomIndi(n) = zoom_index(ind);       
905
906        if (hasValue)
907        {
908          CArray<double,1>& val = list_val.back();
909          val(n) = value(ind);
910        }
911
912        if (hasBounds_)
913        {
914          CArray<double,2>& boundsVal = list_bounds.back();
915          boundsVal(0, n) = bounds(0,n);
916          boundsVal(1, n) = bounds(1,n);
917        }
918      }
919
920      listData.push_back(CMessage());
921      listData.back() << this->getId() 
922                      << list_indi.back() << list_dataInd.back() << list_zoomInd.back() << list_mask.back()
923                      << hasValue;
924      if (hasValue)
925        listData.back() << list_val.back();
926      listData.back() << hasBounds_;
927      if (hasBounds_)
928        listData.back() << list_bounds.back();
929
930      eventData.push(rank, nbConnectedClients_[rank], listData.back());
931    }
932
933    client->sendEvent(eventData);
934  }
935
936  void CAxis::recvDistributedAttributes(CEventServer& event)
937  {
938    string axisId;
939    vector<int> ranks;
940    vector<CBufferIn*> buffers;
941
942    list<CEventServer::SSubEvent>::iterator it;
943    for (it = event.subEvents.begin(); it != event.subEvents.end(); ++it)
944    {
945      ranks.push_back(it->rank);
946      CBufferIn* buffer = it->buffer;
947      *buffer >> axisId;
948      buffers.push_back(buffer);
949    }
950    get(axisId)->recvDistributedAttributes(ranks, buffers);
951  }
952
953  void CAxis::recvDistributedAttributes(vector<int>& ranks, vector<CBufferIn*> buffers)
954  {
955    int nbReceived = ranks.size();
956    vector<CArray<int,1> > vec_indi(nbReceived), vec_dataInd(nbReceived), vec_zoomInd(nbReceived);   
957    vector<CArray<bool,1> > vec_mask(nbReceived);
958    vector<CArray<double,1> > vec_val(nbReceived);
959    vector<CArray<double,2> > vec_bounds(nbReceived);
960   
961    for (int idx = 0; idx < nbReceived; ++idx)
962    {     
963      CBufferIn& buffer = *buffers[idx];
964      buffer >> vec_indi[idx];
965      buffer >> vec_dataInd[idx];
966      buffer >> vec_zoomInd[idx];
967      buffer >> vec_mask[idx];
968
969      buffer >> hasValue;
970      if (hasValue)
971        buffer >> vec_val[idx];
972      buffer >> hasBounds_;
973      if (hasBounds_)
974        buffer >> vec_bounds[idx];
975    }
976
977    int nbData = 0;
978    for (int idx = 0; idx < nbReceived; ++idx)
979    {
980      nbData += vec_indi[idx].numElements();
981    }
982   
983    index.resize(nbData);
984    CArray<int,1> nonCompressedData(nbData);   
985    mask.resize(nbData);
986    if (hasValue)
987      value.resize(nbData);
988    if (hasBounds_)
989      bounds.resize(2,nbData);
990
991    nbData = 0;
992    for (int idx = 0; idx < nbReceived; ++idx)
993    {
994      CArray<int,1>& indi = vec_indi[idx];
995      CArray<int,1>& dataIndi = vec_dataInd[idx];
996      CArray<bool,1>& maskIndi = vec_mask[idx];
997      int nb = indi.numElements();
998      for (int n = 0; n < nb; ++n)
999      {
1000        index(nbData) = indi(n);
1001        nonCompressedData(nbData) = (0 <= dataIndi(n)) ? nbData : -1;
1002        mask(nbData) = maskIndi(n);
1003        if (hasValue)
1004          value(nbData) = vec_val[idx](n);
1005        if (hasBounds_)
1006        {
1007          bounds(0,nbData) = vec_bounds[idx](0,n);
1008          bounds(1,nbData) = vec_bounds[idx](1,n);
1009        }
1010        ++nbData;
1011      }
1012    }
1013
1014    int nbIndex = index.numElements();
1015    int nbCompressedData = 0; 
1016    for (int idx = 0; idx < nonCompressedData.numElements(); ++idx)
1017    {
1018      if (0 <= nonCompressedData(idx) && nonCompressedData(idx) < nbIndex)
1019        ++nbCompressedData;       
1020    }
1021
1022    data_index.resize(nbCompressedData);
1023    nbCompressedData = 0;
1024    for (int idx = 0; idx < nonCompressedData.numElements(); ++idx)
1025    {
1026      if (0 <= nonCompressedData(idx) && nonCompressedData(idx) < nbIndex)
1027      {
1028        data_index(nbCompressedData) = nonCompressedData(idx);
1029        ++nbCompressedData;       
1030      }
1031    }
1032
1033    int nbZoomIndex = 0;
1034    for (int idx = 0; idx < nbReceived; ++idx)
1035    {
1036      nbZoomIndex += vec_zoomInd[idx].numElements();
1037    }
1038
1039    zoom_index.resize(nbZoomIndex);
1040    nbZoomIndex = 0;
1041    CArray<int,1>& zoom_Index_Tmp = this->zoom_index;
1042    for (int idx = 0; idx < nbReceived; ++idx)
1043    {     
1044      CArray<int,1> tmp = zoom_Index_Tmp(Range(nbZoomIndex, nbZoomIndex + vec_zoomInd[idx].numElements()-1));
1045      tmp = vec_zoomInd[idx];
1046
1047      nbZoomIndex += vec_zoomInd[idx].numElements();
1048    }
1049
1050
1051    {
1052      CContextServer* server = CContext::getCurrent()->server;
1053      count_write_index_ = zoom_index.numElements();     
1054      MPI_Scan(&count_write_index_, &start_write_index_, 1, MPI_INT, MPI_SUM, server->intraComm);
1055      global_write_size_ = start_write_index_;
1056      start_write_index_ -= count_write_index_;
1057      local_write_size_ = count_write_index_;     
1058    }
1059  }
1060
1061  // void CAxis::sendServerAttribut(const std::vector<int>& globalDim, int orderPositionInGrid,
1062  //                                CServerDistributionDescription::ServerDistributionType distType)
1063  // {
1064  //   CContext* context = CContext::getCurrent();
1065
1066  //   CContextClient* contextClientTmp = (0 != context->clientPrimServer) ? context->clientPrimServer
1067  //                                                                       : context->client;
1068
1069   
1070  //   int nbServer = contextClientTmp->serverSize;
1071
1072  //   CServerDistributionDescription serverDescription(globalDim, nbServer);
1073  //   serverDescription.computeServerDistribution();
1074
1075  //   std::vector<std::vector<int> > serverIndexBegin = serverDescription.getServerIndexBegin();
1076  //   std::vector<std::vector<int> > serverDimensionSizes = serverDescription.getServerDimensionSizes();
1077
1078  //   globalDimGrid.resize(globalDim.size());
1079  //   for (int idx = 0; idx < globalDim.size(); ++idx) globalDimGrid(idx) = globalDim[idx];
1080
1081  //   CEventClient event(getType(),EVENT_ID_SERVER_ATTRIBUT);
1082  //   if (contextClientTmp->isServerLeader())
1083  //   {
1084  //     std::list<CMessage> msgs;
1085
1086  //     const std::list<int>& ranks = contextClientTmp->getRanksServerLeader();
1087  //     for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
1088  //     {
1089  //       // Use const int to ensure CMessage holds a copy of the value instead of just a reference
1090  //       const int begin = serverIndexBegin[*itRank][orderPositionInGrid];
1091  //       const int ni    = serverDimensionSizes[*itRank][orderPositionInGrid];
1092  //       const int end   = begin + ni - 1;
1093
1094  //       msgs.push_back(CMessage());
1095  //       CMessage& msg = msgs.back();
1096  //       msg << this->getId();
1097  //       msg << ni << begin << end;
1098  //       msg << global_zoom_begin.getValue() << global_zoom_n.getValue();
1099  //       msg << isCompressible_;
1100  //       msg << orderPositionInGrid;
1101  //       msg << globalDimGrid;
1102
1103  //       event.push(*itRank,1,msg);
1104  //     }
1105  //     contextClientTmp->sendEvent(event);
1106  //   }
1107  //   else contextClientTmp->sendEvent(event);
1108  // }
1109
1110  // void CAxis::recvServerAttribut(CEventServer& event)
1111  // {
1112  //   CBufferIn* buffer = event.subEvents.begin()->buffer;
1113  //   string axisId;
1114  //   *buffer >> axisId;
1115  //   get(axisId)->recvServerAttribut(*buffer);
1116    {
1117      CContextClient* contextClientTmp = (context->hasServer) ? context->clientPrimServer[i]
1118    }
1119
1120  //   CContext* context = CContext::getCurrent();
1121  //   if (context->hasClient && context->hasServer)
1122  //   {
1123  //     std::vector<int> globalDim(get(axisId)->globalDimGrid.numElements());
1124  //     for (int idx = 0; idx < globalDim.size(); ++idx) globalDim[idx] = get(axisId)->globalDimGrid(idx);
1125  //     get(axisId)->sendServerAttribut(globalDim, get(axisId)->orderPosInGrid,
1126  //                                     CServerDistributionDescription::BAND_DISTRIBUTION);
1127  //   }
1128  // }
1129
1130  // void CAxis::recvServerAttribut(CBufferIn& buffer)
1131  // {
1132  //   int ni_srv, begin_srv, end_srv, global_zoom_begin_tmp, global_zoom_n_tmp;
1133
1134  //   buffer >> ni_srv >> begin_srv >> end_srv;
1135  //   buffer >> global_zoom_begin_tmp >> global_zoom_n_tmp;
1136  //   buffer >> isCompressible_;
1137  //   buffer >> orderPosInGrid;
1138  //   buffer >> globalDimGrid;
1139
1140  //   global_zoom_begin = global_zoom_begin_tmp;
1141  //   global_zoom_n  = global_zoom_n_tmp;
1142  //   int global_zoom_end = global_zoom_begin + global_zoom_n - 1;
1143
1144  //   zoom_begin_srv = global_zoom_begin > begin_srv ? global_zoom_begin : begin_srv ;
1145  //   zoom_end_srv   = global_zoom_end < end_srv ? global_zoom_end : end_srv ;
1146  //   zoom_size_srv  = zoom_end_srv - zoom_begin_srv + 1;
1147
1148  //   if (zoom_size_srv<=0)
1149  //   {
1150  //     zoom_begin_srv = 0; zoom_end_srv = 0; zoom_size_srv = 0;
1151  //   }
1152
1153  //   if (n_glo == n)
1154  //   {
1155  //     zoom_begin_srv = global_zoom_begin;
1156  //     zoom_end_srv   = global_zoom_end; //zoom_end;
1157  //     zoom_size_srv  = zoom_end_srv - zoom_begin_srv + 1;
1158  //   }
1159  //   if (hasValue)
1160  //   {
1161  //     value_srv.resize(zoom_size_srv);
1162  //     if (hasBounds_)  bound_srv.resize(2,zoom_size_srv);
1163  //   }
1164  // }
1165
1166  CTransformation<CAxis>* CAxis::addTransformation(ETranformationType transType, const StdString& id)
1167  {
1168    transformationMap_.push_back(std::make_pair(transType, CTransformation<CAxis>::createTransformation(transType,id)));
1169    return transformationMap_.back().second;
1170  }
1171
1172  bool CAxis::hasTransformation()
1173  {
1174    return (!transformationMap_.empty());
1175  }
1176
1177  void CAxis::setTransformations(const TransMapTypes& axisTrans)
1178  {
1179    transformationMap_ = axisTrans;
1180  }
1181
1182  CAxis::TransMapTypes CAxis::getAllTransformations(void)
1183  {
1184    return transformationMap_;
1185  }
1186
1187  void CAxis::duplicateTransformation(CAxis* src)
1188  {
1189    if (src->hasTransformation())
1190    {
1191      this->setTransformations(src->getAllTransformations());
1192    }
1193  }
1194
1195  /*!
1196   * Go through the hierarchy to find the axis from which the transformations must be inherited
1197   */
1198  void CAxis::solveInheritanceTransformation()
1199  {
1200    if (hasTransformation() || !hasDirectAxisReference())
1201      return;
1202
1203    CAxis* axis = this;
1204    std::vector<CAxis*> refAxis;
1205    while (!axis->hasTransformation() && axis->hasDirectAxisReference())
1206    {
1207      refAxis.push_back(axis);
1208      axis = axis->getDirectAxisReference();
1209    }
1210
1211    if (axis->hasTransformation())
1212      for (size_t i = 0; i < refAxis.size(); ++i)
1213        refAxis[i]->setTransformations(axis->getAllTransformations());
1214  }
1215
1216  void CAxis::parse(xml::CXMLNode & node)
1217  {
1218    SuperClass::parse(node);
1219
1220    if (node.goToChildElement())
1221    {
1222      StdString nodeElementName;
1223      do
1224      {
1225        StdString nodeId("");
1226        if (node.getAttributes().end() != node.getAttributes().find("id"))
1227        { nodeId = node.getAttributes()["id"]; }
1228
1229        nodeElementName = node.getElementName();
1230        std::map<StdString, ETranformationType>::const_iterator ite = transformationMapList_.end(), it;
1231        it = transformationMapList_.find(nodeElementName);
1232        if (ite != it)
1233        {
1234          transformationMap_.push_back(std::make_pair(it->second, CTransformation<CAxis>::createTransformation(it->second,
1235                                                                                                               nodeId,
1236                                                                                                               &node)));
1237        }
1238        else
1239        {
1240          ERROR("void CAxis::parse(xml::CXMLNode & node)",
1241                << "The transformation " << nodeElementName << " has not been supported yet.");
1242        }
1243      } while (node.goToNextElement()) ;
1244      node.goToParentElement();
1245    }
1246  }
1247
1248  DEFINE_REF_FUNC(Axis,axis)
1249
1250   ///---------------------------------------------------------------
1251
1252} // namespace xios
Note: See TracBrowser for help on using the repository browser.