source: XIOS/dev/XIOS_DEV_CMIP6/src/node/grid.hpp @ 1248

Last change on this file since 1248 was 1243, checked in by oabramkina, 7 years ago

Modifications allowing secondary-server pools of different size. It's controlled by a new parameter number_pools_server2.
Tests: complete, toy, ipsl.

This functionality is to be used in future. For now it is disabled and XIOS works as before with one process assigned per pool.

  • 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: 19.2 KB
RevLine 
[591]1#ifndef __XIOS_CGrid__
2#define __XIOS_CGrid__
[219]3
[591]4/// XIOS headers ///
5#include "xios_spl.hpp"
[219]6#include "group_factory.hpp"
7
8#include "declare_group.hpp"
9#include "domain.hpp"
10#include "axis.hpp"
[887]11#include "scalar.hpp"
[369]12#include "array_new.hpp"
13#include "attribute_array.hpp"
[552]14#include "distribution_server.hpp"
[553]15#include "client_server_mapping.hpp"
[567]16#include "utils.hpp"
[619]17#include "transformation_enum.hpp"
[219]18
[335]19namespace xios {
[509]20
[219]21   /// ////////////////////// Déclarations ////////////////////// ///
22
23   class CGridGroup;
24   class CGridAttributes;
[540]25   class CDomainGroup;
26   class CAxisGroup;
[887]27   class CScalarGroup;
[219]28   class CGrid;
[551]29   class CDistributionClient;
[552]30   class CDistributionServer;
[553]31   class CServerDistributionDescription;
32   class CClientServerMapping;
[620]33   class CGridTransformation;
[219]34
35   ///--------------------------------------------------------------
36
37   // Declare/Define CGridAttribute
38   BEGIN_DECLARE_ATTRIBUTE_MAP(CGrid)
39#  include "grid_attribute.conf"
40   END_DECLARE_ATTRIBUTE_MAP(CGrid)
41
42   ///--------------------------------------------------------------
43
44   class CGrid
45      : public CObjectTemplate<CGrid>
46      , public CGridAttributes
47   {
48         /// typedef ///
49         typedef CObjectTemplate<CGrid>   SuperClass;
50         typedef CGridAttributes SuperClassAttribute;
51
[676]52      public:
[219]53
54         typedef CGridAttributes RelAttributes;
55         typedef CGridGroup      RelGroup;
56
[300]57         enum EEventId
58         {
[887]59           EVENT_ID_INDEX, EVENT_ID_ADD_DOMAIN, EVENT_ID_ADD_AXIS, EVENT_ID_ADD_SCALAR
[650]60         };
[509]61
[219]62         /// Constructeurs ///
63         CGrid(void);
[650]64         explicit CGrid(const StdString& id);
65         CGrid(const CGrid& grid);       // Not implemented yet.
66         CGrid(const CGrid* const grid); // Not implemented yet.
[219]67
68         /// Traitements ///
[509]69//         void solveReference(void);
[219]70
[676]71         void checkEligibilityForCompressedOutput();
72
[509]73         void solveDomainAxisRef(bool areAttributesChecked);
74
75         void checkMaskIndex(bool doCalculateIndex);
76
[650]77 //        virtual void toBinary  (StdOStream& os) const;
78//         virtual void fromBinary(StdIStream& is);
[219]79
[676]80         void addRelFileCompressed(const StdString& filename);
81
[219]82         /// Tests ///
[676]83         bool isCompressible(void) const;
84         bool isWrittenCompressed(const StdString& filename) const;
[219]85
[676]86      public:
[219]87
88         /// Accesseurs ///
[1158]89         StdSize getDimension(void);
[509]90
[286]91         StdSize  getDataSize(void) const;
[219]92
[1144]93         /// Entrées-sorties de champs
[369]94         template <int n>
[593]95         void inputField(const CArray<double,n>& field, CArray<double,1>& stored) const;
96         template <int n>
[1144]97         void outputField(const CArray<double,1>& stored, CArray<double,n>& field) const;     
[509]98
[650]99         virtual void parse(xml::CXMLNode& node);
[540]100
[219]101         /// Destructeur ///
102         virtual ~CGrid(void);
103
[676]104      public:
[219]105
106         /// Accesseurs statiques ///
107         static StdString GetName(void);
108         static StdString GetDefName(void);
[509]109
[219]110         static ENodeType GetType(void);
111
112         /// Instanciateurs Statiques ///
[347]113         static CGrid* createGrid(CDomain* domain);
114         static CGrid* createGrid(CDomain* domain, CAxis* axis);
[745]115         static CGrid* createGrid(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
[887]116                                  const CArray<int,1>& axisDomainOrder = CArray<int,1>());
[745]117         static CGrid* createGrid(StdString id, const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
[887]118                                  const std::vector<CScalar*>& scalars, const CArray<int,1>& axisDomainOrder = CArray<int,1>());
119         static CGrid* createGrid(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
120                                  const std::vector<CScalar*>& scalars, const CArray<int,1>& axisDomainOrder);
[745]121         static StdString generateId(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
[887]122                                     const std::vector<CScalar*>& scalars, const CArray<int,1>& axisDomainOrder = CArray<int,1>());
[823]123         static StdString generateId(const CGrid* gridSrc, const CGrid* gridDest);
124         static CGrid* cloneGrid(const StdString& idNewGrid, CGrid* gridSrc);
[219]125
[1144]126      public:           
[219]127         void computeIndexServer(void);
128         void computeIndex(void);
[586]129         void computeIndexScalarGrid();
[1129]130         void computeWrittenIndex();
[219]131
[509]132         void solveDomainRef(bool checkAtt);
133         void solveAxisRef(bool checkAtt);
[887]134         void solveScalarRef(bool checkAtt);
[540]135         void solveDomainAxisRefInheritance(bool apply = true);
[619]136         void solveTransformations();
[775]137         void solveDomainAxisBaseRef();
[509]138
[835]139         CDomain* addDomain(const std::string& id=StdString());
140         CAxis* addAxis(const std::string& id=StdString());
[887]141         CScalar* addScalar(const std::string& id=StdString());
[540]142         void sendAddDomain(const std::string& id="");
143         void sendAddAxis(const std::string& id="");
[887]144         void sendAddScalar(const std::string& id="");
[540]145         void sendAllDomains();
146         void sendAllAxis();
[887]147         void sendAllScalars();
[540]148
[650]149         static void recvAddDomain(CEventServer& event);
150         void recvAddDomain(CBufferIn& buffer);
151         static void recvAddAxis(CEventServer& event);
152         void recvAddAxis(CBufferIn& buffer);
[887]153         static void recvAddScalar(CEventServer& event);
154         void recvAddScalar(CBufferIn& buffer);
[540]155
[650]156         static bool dispatchEvent(CEventServer& event);
157         static void recvIndex(CEventServer& event);
158         void recvIndex(vector<int> ranks, vector<CBufferIn*> buffers);
159         void sendIndex(void);
[586]160         void sendIndexScalarGrid();
[509]161
162         void computeDomConServer();
163         std::map<int, int> getDomConServerSide();
[1236]164         std::map<int, StdSize> getAttributesBufferSize(CContextClient* client);
165         std::map<int, StdSize> getDataBufferSize(CContextClient* client, const std::string& id = "");
[540]166         std::vector<StdString> getDomainList();
167         std::vector<StdString> getAxisList();
[887]168         std::vector<StdString> getScalarList();
[540]169         std::vector<CDomain*> getDomains();
170         std::vector<CAxis*> getAxis();
[887]171         std::vector<CScalar*> getScalars();
[943]172         CDomain* getDomain(int domainIndex);
173         CAxis* getAxis(int axisIndex);
174         CScalar* getScalar(int scalarIndex);
[551]175         std::vector<int> getAxisOrder();
[567]176         std::vector<int> getGlobalDimension();
[1236]177         bool isScalarGrid() const;         
[540]178
[567]179         bool doGridHaveDataToWrite();
[1235]180         bool doGridHaveDataDistributed(CContextClient* client = 0);
[567]181         size_t getWrittenDataSize() const;
[676]182         int getNumberWrittenIndexes() const;
183         int getTotalNumberWrittenIndexes() const;
184         int getOffsetWrittenIndexes() const;
[567]185
[834]186         CDistributionServer* getDistributionServer();
187         CDistributionClient* getDistributionClient();
[620]188         CGridTransformation* getTransformations();
[567]189
[632]190         void transformGrid(CGrid* transformGridSrc);
[775]191         void completeGrid(CGrid* transformGridSrc = 0);
[687]192         void doAutoDistribution(CGrid* transformGridSrc);
[619]193         bool isTransformed();
194         void setTransformed();
[890]195         bool isGenerated();
196         void setGenerated();
[823]197         void addTransGridSource(CGrid* gridSrc);
198         std::map<CGrid*, std::pair<bool,StdString> >& getTransGridSource();
[824]199         bool hasTransform();
[1215]200         size_t getGlobalWrittenSize(void) ;
[1236]201
[219]202      public:
[650]203         CArray<int, 1> storeIndex_client;
[509]204
[1243]205/** Map containing indexes that will be sent in sendIndex(). In future: change the key to pair<distrType, serverSize> (?) */
206         std::map<CContextClient*, map<int, CArray<int, 1> > > storeIndex_toSrv;
207
[1236]208         map<int, CArray<int, 1> > storeIndex_fromSrv; // Support, for now, reading with level-1 server
[1243]209
210
[1236]211         std::map<CContextClient*, std::map<int,int> > nbSenders, nbReadSenders;
[509]212
[1129]213         map<int, CArray<size_t, 1> > outIndexFromClient, compressedOutIndexFromClient, outGlobalIndexFromClient;
214
215         // A client receives global index from other clients (via recvIndex)
216         // then does mapping these index into local index of STORE_CLIENTINDEX
217         // In this way, store_clientIndex can be used as an input of a source filter
218         // Maybe we need a flag to determine whether a client wants to write. TODO
219         map<int, CArray<size_t, 1> > outLocalIndexStoreOnClient; 
220
[1243]221/** Indexes calculated based on server distribution (serverDistribution_). They are used for writing data into a file. */
222         CArray<size_t,1> localIndexToWriteOnServer;
[1129]223
[1243]224/** Indexes calculated based on client distribution (clientDistribution_). They are not used at all.
225    They should be the same as localIndexToWriteOnServer and potentially can be used as an additional check.*/
226         CArray<size_t,1> localIndexToWriteOnClient;
227
[1025]228         CArray<size_t,1> indexFromClients;
[650]229         void checkMask(void);
[821]230         void createMask(void);
[1129]231         void modifyMask(const CArray<int,1>& indexToModify, bool valueToModify = false);
232         void modifyMaskSize(const std::vector<int>& newDimensionSize, bool newValue = false);
[509]233
[742]234         void computeGridGlobalDimension(const std::vector<CDomain*>& domains,
235                                         const std::vector<CAxis*>& axis,
[887]236                                         const std::vector<CScalar*>& scalars,
237                                         const CArray<int,1>& axisDomainOrder);
[742]238
[540]239      private:
[567]240       template<int N>
241       void checkGridMask(CArray<bool,N>& gridMask,
[664]242                          const std::vector<CArray<bool,1>* >& domainMasks,
[567]243                          const std::vector<CArray<bool,1>* >& axisMasks,
[887]244                          const CArray<int,1>& axisDomainOrder,
[821]245                          bool createMask = false);
[623]246        template<int N>
[1129]247        void modifyGridMask(CArray<bool,N>& gridMask, const CArray<int,1>& indexToModify, bool valueToModify);
[623]248
[1129]249        template<int N>
250        void modifyGridMaskSize(CArray<bool,N>& gridMask, const std::vector<int>& eachDimSize, bool newValue);
251
[1144]252        void storeField_arr(const double* const data, CArray<double, 1>& stored) const;
253        void restoreField_arr(const CArray<double, 1>& stored, double* const data) const;
254
[540]255        void setVirtualDomainGroup(CDomainGroup* newVDomainGroup);
256        void setVirtualAxisGroup(CAxisGroup* newVAxisGroup);
[887]257        void setVirtualScalarGroup(CScalarGroup* newVScalarGroup);
[567]258
[887]259        void setDomainList(const std::vector<CDomain*> domains = std::vector<CDomain*>());
[540]260        void setAxisList(const std::vector<CAxis*> axis = std::vector<CAxis*>());
[887]261        void setScalarList(const std::vector<CScalar*> scalars = std::vector<CScalar*>());
[540]262
[887]263        CDomainGroup* getVirtualDomainGroup() const;
[540]264        CAxisGroup* getVirtualAxisGroup() const;
[887]265        CScalarGroup* getVirtualScalarGroup() const;
[619]266
[657]267        void checkAttributesAfterTransformation();
[619]268        void setTransformationAlgorithms();
[865]269        void computeIndexByElement(const std::vector<boost::unordered_map<size_t,std::vector<int> > >& indexServerOnElement,
270                                   CClientServerMapping::GlobalIndexMap& globalIndexOnServer);
[1158]271        int computeGridGlobalDimension(std::vector<int>& globalDim,
272                                       const std::vector<CDomain*> domains,
273                                       const std::vector<CAxis*> axis,
274                                       const std::vector<CScalar*> scalars,
275                                       const CArray<int,1>& axisDomainOrder);
276        int getDistributedDimension();
[619]277
[1025]278        void computeClientIndex();
279        void computeConnectedClients();
[1202]280        void computeClientIndexScalarGrid(); 
281        void computeConnectedClientsScalarGrid(); 
[1025]282
[540]283      private:
[1236]284         bool isChecked;
285         bool isDomainAxisChecked;
286         bool isIndexSent;
287
[540]288        CDomainGroup* vDomainGroup_;
289        CAxisGroup* vAxisGroup_;
[887]290        CScalarGroup* vScalarGroup_;
291        std::vector<std::string> axisList_, domList_, scalarList_;
292        bool isAxisListSet, isDomListSet, isScalarListSet;
[567]293
[1243]294/** Distribution calculated in computeClientIndex() based on the knowledge of the entire grid */
[551]295        CDistributionClient* clientDistribution_;
[1243]296
297/** Distribution calculated upon receiving indexes */
[552]298        CDistributionServer* serverDistribution_;
[1243]299
[568]300        CClientServerMapping* clientServerMap_;
[567]301        size_t writtenDataSize_;
[676]302        int numberWrittenIndexes_, totalNumberWrittenIndexes_, offsetWrittenIndexes_;
[1243]303
304/** Map storing ranks of connected servers. In future: change the key to the server size (?) */
305        std::map<CContextClient*, std::vector<int> > connectedServerRank_;
306
307/** Map storing data size that will be sent to connected servers. In future: change the key to the server size (?) */
[1236]308        std::map<CContextClient*, std::map<int,size_t> > connectedDataSize_;
[1243]309
[1165]310        bool isDataDistributed_;       
[676]311         //! True if and only if the data defined on the grid can be outputted in a compressed way
312        bool isCompressible_;
313        std::set<std::string> relFilesCompressed;
[619]314
[1129]315        bool isTransformed_, isGenerated_;
316        bool computedWrittenIndex_;
[620]317        std::vector<int> axisPositionInGrid_;
318        CGridTransformation* transformations_;
[1158]319        bool hasDomainAxisBaseRef_;       
[823]320        std::map<CGrid*, std::pair<bool,StdString> > gridSrc_;
[824]321        bool hasTransform_;
[1243]322
323/** Map storing data size that will be sent to connected servers. In future: change the key to the server size (?) */
[1236]324        std::map<CContextClient*, CClientServerMapping::GlobalIndexMap> globalIndexOnServer_;
[1243]325
326/** List order of axis and domain in a grid, if there is a domain, it will take value 1 (true), axis 0 (false) */
[887]327        std::vector<int> order_;
[1243]328
[219]329   }; // class CGrid
330
331   ///--------------------------------------------------------------
332
[369]333   template <int n>
[567]334   void CGrid::inputField(const CArray<double,n>& field, CArray<double,1>& stored) const
[219]335   {
[369]336      if (this->getDataSize() != field.numElements())
337         ERROR("void CGrid::inputField(const  CArray<double,n>& field, CArray<double,1>& stored) const",
[680]338                << "[ Awaiting data of size = " << this->getDataSize() << ", "
[421]339                << "Received data size = "      << field.numElements() << " ] "
[955]340                << "The data array does not have the right size! "
341                << "Grid = " << this->GetName())
[650]342      this->storeField_arr(field.dataFirst(), stored);
[219]343   }
344
[593]345   template <int n>
346   void CGrid::outputField(const CArray<double,1>& stored, CArray<double,n>& field) const
347   {
348      if (this->getDataSize() != field.numElements())
349         ERROR("void CGrid::outputField(const CArray<double,1>& stored, CArray<double,n>& field) const",
350                << "[ Size of the data = " << this->getDataSize() << ", "
351                << "Output data size = "   << field.numElements() << " ] "
[955]352                << "The ouput array does not have the right size! "
353                << "Grid = " << this->GetName())
[593]354      this->restoreField_arr(stored, field.dataFirst());
355   }
356
[567]357   template<int N>
358   void CGrid::checkGridMask(CArray<bool,N>& gridMask,
[664]359                             const std::vector<CArray<bool,1>* >& domainMasks,
[567]360                             const std::vector<CArray<bool,1>* >& axisMasks,
[887]361                             const CArray<int,1>& axisDomainOrder,
[821]362                             bool createMask)
[567]363   {
364     int idx = 0;
365     int numElement = axisDomainOrder.numElements();
[650]366     int dim = domainMasks.size() * 2 + axisMasks.size();
[664]367     std::vector<CDomain*> domainP = this->getDomains();
[567]368
369     std::vector<int> idxLoop(dim,0), indexMap(numElement), eachDimSize(dim);
370     std::vector<int> currentIndex(dim);
371     int idxDomain = 0, idxAxis = 0;
372    for (int i = 0; i < numElement; ++i)
373    {
374      indexMap[i] = idx;
[887]375      if (2 == axisDomainOrder(i)) {
[664]376          eachDimSize[indexMap[i]]   = domainP[idxDomain]->ni;
377          eachDimSize[indexMap[i]+1] = domainP[idxDomain]->nj;
[567]378          idx += 2; ++idxDomain;
379      }
[887]380      else if (1 == axisDomainOrder(i)) {
[567]381        eachDimSize[indexMap[i]] = axisMasks[idxAxis]->numElements();
382        ++idx; ++idxAxis;
383      }
[976]384      else {};
[567]385    }
386
[821]387    if (!gridMask.isEmpty() && !createMask)
[567]388    {
389      for (int i = 0; i < dim; ++i)
390      {
391        if (gridMask.extent(i) != eachDimSize[i])
[680]392          ERROR("CGrid::checkMask(void)",
393                << "The mask has one dimension whose size is different from the one of the local grid." << std::endl
394                << "Local size of dimension " << i << " is " << eachDimSize[i] << "." << std::endl
[955]395                << "Mask size for dimension " << i << " is " << gridMask.extent(i) << "." << std::endl
396                << "Grid = " << this->GetName())
[567]397      }
398    }
399    else {
400        CArrayBoolTraits<CArray<bool,N> >::resizeArray(gridMask,eachDimSize);
401        gridMask = true;
402    }
403
404    int ssize = gridMask.numElements();
405    idx = 0;
406    while (idx < ssize)
407    {
408      for (int i = 0; i < dim-1; ++i)
409      {
410        if (idxLoop[i] == eachDimSize[i])
411        {
412          idxLoop[i] = 0;
413          ++idxLoop[i+1];
414        }
415      }
416
417      // Find out outer index
418      idxDomain = idxAxis = 0;
419      bool maskValue = true;
420      for (int i = 0; i < numElement; ++i)
421      {
[887]422        if (2 == axisDomainOrder(i))
[567]423        {
[664]424          maskValue = maskValue && (*domainMasks[idxDomain])(idxLoop[indexMap[i]] + idxLoop[indexMap[i]+1] * eachDimSize[indexMap[i]]);
[567]425          ++idxDomain;
426        }
[887]427        else if (1 == axisDomainOrder(i))
[567]428        {
429          maskValue = maskValue && (*axisMasks[idxAxis])(idxLoop[indexMap[i]]);
430          ++idxAxis;
431        }
432      }
433
434      int maskIndex = idxLoop[0];
435      int mulDim = 1;
436      for (int k = 1; k < dim; ++k)
437      {
438        mulDim *= eachDimSize[k-1];
439        maskIndex += idxLoop[k]*mulDim;
440      }
[623]441      *(gridMask.dataFirst()+maskIndex) &= maskValue;
[567]442
443      ++idxLoop[0];
444      ++idx;
445    }
446
447   }
448
[1129]449   template<int N>
450   void CGrid::modifyGridMaskSize(CArray<bool,N>& gridMask,
451                                  const std::vector<int>& eachDimSize,
452                                  bool newValue)
453   {
454      if (N != eachDimSize.size())
455      {
456        // ERROR("CGrid::modifyGridMaskSize(CArray<bool,N>& gridMask,
457        //                                  const std::vector<int>& eachDimSize,
458        //                                  bool newValue)",
459        //       << "Dimension size of the mask is different from input dimension size." << std::endl
460        //       << "Mask dimension is " << N << "." << std::endl
461        //       << "Input dimension is " << eachDimSize.size() << "." << std::endl
462        //       << "Grid = " << this->GetName())
463      }
464      CArrayBoolTraits<CArray<bool,N> >::resizeArray(gridMask,eachDimSize);
465      gridMask = newValue;
466   }
467                                 
468
[623]469   /*!
470     Modify the current mask of grid, the local index to be modified will take value false
471     \param [in/out] gridMask current mask of grid
472     \param [in] indexToModify local index to modify
473   */
474   template<int N>
[1129]475   void CGrid::modifyGridMask(CArray<bool,N>& gridMask, const CArray<int,1>& indexToModify, bool valueToModify)
476   {     
[623]477     int num = indexToModify.numElements();
478     for (int idx = 0; idx < num; ++idx)
479     {
480       *(gridMask.dataFirst()+indexToModify(idx)) = valueToModify;
481     }
482   }
[219]483   ///--------------------------------------------------------------
484
485   // Declare/Define CGridGroup and CGridDefinition
486   DECLARE_GROUP(CGrid);
487
488   ///--------------------------------------------------------------
489
[335]490} // namespace xios
[219]491
[591]492#endif // __XIOS_CGrid__
Note: See TracBrowser for help on using the repository browser.