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

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

Modifications allowing to avoid recalculation of the grid distribution if receivers are of the same size.

  • 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: 20.1 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>
[1250]97         void outputField(const CArray<double,1>& stored, CArray<double,n>& field) const; 
98         template <int n>
99         void uncompressField(const CArray<double,n>& data, CArray<double,1>& outData) const; 
[509]100
[650]101         virtual void parse(xml::CXMLNode& node);
[540]102
[219]103         /// Destructeur ///
104         virtual ~CGrid(void);
105
[676]106      public:
[219]107
108         /// Accesseurs statiques ///
109         static StdString GetName(void);
110         static StdString GetDefName(void);
[509]111
[219]112         static ENodeType GetType(void);
113
114         /// Instanciateurs Statiques ///
[347]115         static CGrid* createGrid(CDomain* domain);
116         static CGrid* createGrid(CDomain* domain, CAxis* axis);
[745]117         static CGrid* createGrid(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
[887]118                                  const CArray<int,1>& axisDomainOrder = CArray<int,1>());
[745]119         static CGrid* createGrid(StdString id, const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
[887]120                                  const std::vector<CScalar*>& scalars, const CArray<int,1>& axisDomainOrder = CArray<int,1>());
121         static CGrid* createGrid(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
122                                  const std::vector<CScalar*>& scalars, const CArray<int,1>& axisDomainOrder);
[745]123         static StdString generateId(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
[887]124                                     const std::vector<CScalar*>& scalars, const CArray<int,1>& axisDomainOrder = CArray<int,1>());
[823]125         static StdString generateId(const CGrid* gridSrc, const CGrid* gridDest);
126         static CGrid* cloneGrid(const StdString& idNewGrid, CGrid* gridSrc);
[219]127
[1144]128      public:           
[219]129         void computeIndexServer(void);
130         void computeIndex(void);
[586]131         void computeIndexScalarGrid();
[1129]132         void computeWrittenIndex();
[219]133
[509]134         void solveDomainRef(bool checkAtt);
135         void solveAxisRef(bool checkAtt);
[887]136         void solveScalarRef(bool checkAtt);
[540]137         void solveDomainAxisRefInheritance(bool apply = true);
[619]138         void solveTransformations();
[775]139         void solveDomainAxisBaseRef();
[509]140
[835]141         CDomain* addDomain(const std::string& id=StdString());
142         CAxis* addAxis(const std::string& id=StdString());
[887]143         CScalar* addScalar(const std::string& id=StdString());
[540]144         void sendAddDomain(const std::string& id="");
145         void sendAddAxis(const std::string& id="");
[887]146         void sendAddScalar(const std::string& id="");
[540]147         void sendAllDomains();
148         void sendAllAxis();
[887]149         void sendAllScalars();
[540]150
[650]151         static void recvAddDomain(CEventServer& event);
152         void recvAddDomain(CBufferIn& buffer);
153         static void recvAddAxis(CEventServer& event);
154         void recvAddAxis(CBufferIn& buffer);
[887]155         static void recvAddScalar(CEventServer& event);
156         void recvAddScalar(CBufferIn& buffer);
[540]157
[650]158         static bool dispatchEvent(CEventServer& event);
159         static void recvIndex(CEventServer& event);
160         void recvIndex(vector<int> ranks, vector<CBufferIn*> buffers);
161         void sendIndex(void);
[586]162         void sendIndexScalarGrid();
[509]163
164         void computeDomConServer();
165         std::map<int, int> getDomConServerSide();
[1236]166         std::map<int, StdSize> getAttributesBufferSize(CContextClient* client);
167         std::map<int, StdSize> getDataBufferSize(CContextClient* client, const std::string& id = "");
[540]168         std::vector<StdString> getDomainList();
169         std::vector<StdString> getAxisList();
[887]170         std::vector<StdString> getScalarList();
[540]171         std::vector<CDomain*> getDomains();
172         std::vector<CAxis*> getAxis();
[887]173         std::vector<CScalar*> getScalars();
[943]174         CDomain* getDomain(int domainIndex);
175         CAxis* getAxis(int axisIndex);
176         CScalar* getScalar(int scalarIndex);
[551]177         std::vector<int> getAxisOrder();
[567]178         std::vector<int> getGlobalDimension();
[1236]179         bool isScalarGrid() const;         
[540]180
[567]181         bool doGridHaveDataToWrite();
[1235]182         bool doGridHaveDataDistributed(CContextClient* client = 0);
[567]183         size_t getWrittenDataSize() const;
[676]184         int getNumberWrittenIndexes() const;
185         int getTotalNumberWrittenIndexes() const;
186         int getOffsetWrittenIndexes() const;
[567]187
[834]188         CDistributionServer* getDistributionServer();
189         CDistributionClient* getDistributionClient();
[620]190         CGridTransformation* getTransformations();
[567]191
[632]192         void transformGrid(CGrid* transformGridSrc);
[775]193         void completeGrid(CGrid* transformGridSrc = 0);
[687]194         void doAutoDistribution(CGrid* transformGridSrc);
[619]195         bool isTransformed();
196         void setTransformed();
[890]197         bool isGenerated();
198         void setGenerated();
[823]199         void addTransGridSource(CGrid* gridSrc);
200         std::map<CGrid*, std::pair<bool,StdString> >& getTransGridSource();
[824]201         bool hasTransform();
[1215]202         size_t getGlobalWrittenSize(void) ;
[1236]203
[219]204      public:
[650]205         CArray<int, 1> storeIndex_client;
[509]206
[1263]207/** Map containing indexes that will be sent in sendIndex(). */
[1243]208         std::map<CContextClient*, map<int, CArray<int, 1> > > storeIndex_toSrv;
209
[1263]210/** Map storing the number of senders. Key = size of receiver's intracomm */
211         std::map<int, std::map<int,int> > nbSenders;
212
213         std::map<CContextClient*, std::map<int,int> > nbReadSenders;
214
[1236]215         map<int, CArray<int, 1> > storeIndex_fromSrv; // Support, for now, reading with level-1 server
[1243]216
217
[1129]218         map<int, CArray<size_t, 1> > outIndexFromClient, compressedOutIndexFromClient, outGlobalIndexFromClient;
219
220         // A client receives global index from other clients (via recvIndex)
221         // then does mapping these index into local index of STORE_CLIENTINDEX
222         // In this way, store_clientIndex can be used as an input of a source filter
223         // Maybe we need a flag to determine whether a client wants to write. TODO
224         map<int, CArray<size_t, 1> > outLocalIndexStoreOnClient; 
225
[1243]226/** Indexes calculated based on server distribution (serverDistribution_). They are used for writing data into a file. */
227         CArray<size_t,1> localIndexToWriteOnServer;
[1129]228
[1243]229/** Indexes calculated based on client distribution (clientDistribution_). They are not used at all.
230    They should be the same as localIndexToWriteOnServer and potentially can be used as an additional check.*/
231         CArray<size_t,1> localIndexToWriteOnClient;
232
[1025]233         CArray<size_t,1> indexFromClients;
[650]234         void checkMask(void);
[821]235         void createMask(void);
[1129]236         void modifyMask(const CArray<int,1>& indexToModify, bool valueToModify = false);
237         void modifyMaskSize(const std::vector<int>& newDimensionSize, bool newValue = false);
[509]238
[742]239         void computeGridGlobalDimension(const std::vector<CDomain*>& domains,
240                                         const std::vector<CAxis*>& axis,
[887]241                                         const std::vector<CScalar*>& scalars,
242                                         const CArray<int,1>& axisDomainOrder);
[742]243
[540]244      private:
[567]245       template<int N>
246       void checkGridMask(CArray<bool,N>& gridMask,
[664]247                          const std::vector<CArray<bool,1>* >& domainMasks,
[567]248                          const std::vector<CArray<bool,1>* >& axisMasks,
[887]249                          const CArray<int,1>& axisDomainOrder,
[821]250                          bool createMask = false);
[623]251        template<int N>
[1129]252        void modifyGridMask(CArray<bool,N>& gridMask, const CArray<int,1>& indexToModify, bool valueToModify);
[623]253
[1129]254        template<int N>
255        void modifyGridMaskSize(CArray<bool,N>& gridMask, const std::vector<int>& eachDimSize, bool newValue);
256
[1144]257        void storeField_arr(const double* const data, CArray<double, 1>& stored) const;
258        void restoreField_arr(const CArray<double, 1>& stored, double* const data) const;
[1250]259        void uncompressField_arr(const double* const data, CArray<double, 1>& outData) const;
[1144]260
[540]261        void setVirtualDomainGroup(CDomainGroup* newVDomainGroup);
262        void setVirtualAxisGroup(CAxisGroup* newVAxisGroup);
[887]263        void setVirtualScalarGroup(CScalarGroup* newVScalarGroup);
[567]264
[887]265        void setDomainList(const std::vector<CDomain*> domains = std::vector<CDomain*>());
[540]266        void setAxisList(const std::vector<CAxis*> axis = std::vector<CAxis*>());
[887]267        void setScalarList(const std::vector<CScalar*> scalars = std::vector<CScalar*>());
[540]268
[887]269        CDomainGroup* getVirtualDomainGroup() const;
[540]270        CAxisGroup* getVirtualAxisGroup() const;
[887]271        CScalarGroup* getVirtualScalarGroup() const;
[619]272
[657]273        void checkAttributesAfterTransformation();
[619]274        void setTransformationAlgorithms();
[865]275        void computeIndexByElement(const std::vector<boost::unordered_map<size_t,std::vector<int> > >& indexServerOnElement,
[1263]276                                   const CContextClient* client,
[865]277                                   CClientServerMapping::GlobalIndexMap& globalIndexOnServer);
[1158]278        int computeGridGlobalDimension(std::vector<int>& globalDim,
279                                       const std::vector<CDomain*> domains,
280                                       const std::vector<CAxis*> axis,
281                                       const std::vector<CScalar*> scalars,
282                                       const CArray<int,1>& axisDomainOrder);
283        int getDistributedDimension();
[619]284
[1025]285        void computeClientIndex();
286        void computeConnectedClients();
[1202]287        void computeClientIndexScalarGrid(); 
288        void computeConnectedClientsScalarGrid(); 
[1025]289
[540]290      private:
[1236]291         bool isChecked;
292         bool isDomainAxisChecked;
293         bool isIndexSent;
294
[540]295        CDomainGroup* vDomainGroup_;
296        CAxisGroup* vAxisGroup_;
[887]297        CScalarGroup* vScalarGroup_;
298        std::vector<std::string> axisList_, domList_, scalarList_;
299        bool isAxisListSet, isDomListSet, isScalarListSet;
[567]300
[1243]301/** Distribution calculated in computeClientIndex() based on the knowledge of the entire grid */
[551]302        CDistributionClient* clientDistribution_;
[1243]303
304/** Distribution calculated upon receiving indexes */
[552]305        CDistributionServer* serverDistribution_;
[1243]306
[568]307        CClientServerMapping* clientServerMap_;
[567]308        size_t writtenDataSize_;
[676]309        int numberWrittenIndexes_, totalNumberWrittenIndexes_, offsetWrittenIndexes_;
[1243]310
[1263]311/** Map storing local ranks of connected receivers. Key = size of receiver's intracomm */
312        std::map<int, std::vector<int> > connectedServerRank_;
[1243]313
[1263]314/** Map storing the size of data to be send. Key = size of receiver's intracomm */
315        std::map<int, std::map<int,size_t> > connectedDataSize_;
[1243]316
[1165]317        bool isDataDistributed_;       
[676]318         //! True if and only if the data defined on the grid can be outputted in a compressed way
319        bool isCompressible_;
320        std::set<std::string> relFilesCompressed;
[619]321
[1129]322        bool isTransformed_, isGenerated_;
323        bool computedWrittenIndex_;
[620]324        std::vector<int> axisPositionInGrid_;
325        CGridTransformation* transformations_;
[1158]326        bool hasDomainAxisBaseRef_;       
[823]327        std::map<CGrid*, std::pair<bool,StdString> > gridSrc_;
[824]328        bool hasTransform_;
[1243]329
[1263]330/** Map storing global indexes of server-like (band-wise) distribution for sending to receivers.
331  * Key = size of receiver's intracomm.
332  */
333//        std::map<CContextClient*, CClientServerMapping::GlobalIndexMap> globalIndexOnServer_;
334        std::map<int, CClientServerMapping::GlobalIndexMap> globalIndexOnServer_;
[1243]335
[1263]336
[1243]337/** List order of axis and domain in a grid, if there is a domain, it will take value 1 (true), axis 0 (false) */
[887]338        std::vector<int> order_;
[1243]339
[219]340   }; // class CGrid
341
342   ///--------------------------------------------------------------
343
[369]344   template <int n>
[567]345   void CGrid::inputField(const CArray<double,n>& field, CArray<double,1>& stored) const
[219]346   {
[369]347      if (this->getDataSize() != field.numElements())
348         ERROR("void CGrid::inputField(const  CArray<double,n>& field, CArray<double,1>& stored) const",
[680]349                << "[ Awaiting data of size = " << this->getDataSize() << ", "
[421]350                << "Received data size = "      << field.numElements() << " ] "
[955]351                << "The data array does not have the right size! "
352                << "Grid = " << this->GetName())
[650]353      this->storeField_arr(field.dataFirst(), stored);
[219]354   }
355
[593]356   template <int n>
357   void CGrid::outputField(const CArray<double,1>& stored, CArray<double,n>& field) const
358   {
359      if (this->getDataSize() != field.numElements())
360         ERROR("void CGrid::outputField(const CArray<double,1>& stored, CArray<double,n>& field) const",
361                << "[ Size of the data = " << this->getDataSize() << ", "
362                << "Output data size = "   << field.numElements() << " ] "
[955]363                << "The ouput array does not have the right size! "
364                << "Grid = " << this->GetName())
[593]365      this->restoreField_arr(stored, field.dataFirst());
366   }
367
[1250]368   /*!
369     This function removes the effect of mask on received data on the server.
370     This function only serve for the checking purpose. TODO: Something must be done to seperate mask and data_index from each other in received data
371     \data data received data with masking effect on the server
372     \outData data without masking effect
373   */
374   template <int N>
375   void CGrid::uncompressField(const CArray<double,N>& data, CArray<double,1>& outData) const
376   {     
377     uncompressField_arr(data.dataFirst(), outData);
378   }
379
[567]380   template<int N>
381   void CGrid::checkGridMask(CArray<bool,N>& gridMask,
[664]382                             const std::vector<CArray<bool,1>* >& domainMasks,
[567]383                             const std::vector<CArray<bool,1>* >& axisMasks,
[887]384                             const CArray<int,1>& axisDomainOrder,
[821]385                             bool createMask)
[567]386   {
387     int idx = 0;
388     int numElement = axisDomainOrder.numElements();
[650]389     int dim = domainMasks.size() * 2 + axisMasks.size();
[664]390     std::vector<CDomain*> domainP = this->getDomains();
[567]391
392     std::vector<int> idxLoop(dim,0), indexMap(numElement), eachDimSize(dim);
393     std::vector<int> currentIndex(dim);
394     int idxDomain = 0, idxAxis = 0;
395    for (int i = 0; i < numElement; ++i)
396    {
397      indexMap[i] = idx;
[887]398      if (2 == axisDomainOrder(i)) {
[664]399          eachDimSize[indexMap[i]]   = domainP[idxDomain]->ni;
400          eachDimSize[indexMap[i]+1] = domainP[idxDomain]->nj;
[567]401          idx += 2; ++idxDomain;
402      }
[887]403      else if (1 == axisDomainOrder(i)) {
[567]404        eachDimSize[indexMap[i]] = axisMasks[idxAxis]->numElements();
405        ++idx; ++idxAxis;
406      }
[976]407      else {};
[567]408    }
409
[821]410    if (!gridMask.isEmpty() && !createMask)
[567]411    {
412      for (int i = 0; i < dim; ++i)
413      {
414        if (gridMask.extent(i) != eachDimSize[i])
[680]415          ERROR("CGrid::checkMask(void)",
416                << "The mask has one dimension whose size is different from the one of the local grid." << std::endl
417                << "Local size of dimension " << i << " is " << eachDimSize[i] << "." << std::endl
[955]418                << "Mask size for dimension " << i << " is " << gridMask.extent(i) << "." << std::endl
419                << "Grid = " << this->GetName())
[567]420      }
421    }
422    else {
423        CArrayBoolTraits<CArray<bool,N> >::resizeArray(gridMask,eachDimSize);
424        gridMask = true;
425    }
426
427    int ssize = gridMask.numElements();
428    idx = 0;
429    while (idx < ssize)
430    {
431      for (int i = 0; i < dim-1; ++i)
432      {
433        if (idxLoop[i] == eachDimSize[i])
434        {
435          idxLoop[i] = 0;
436          ++idxLoop[i+1];
437        }
438      }
439
440      // Find out outer index
441      idxDomain = idxAxis = 0;
442      bool maskValue = true;
443      for (int i = 0; i < numElement; ++i)
444      {
[887]445        if (2 == axisDomainOrder(i))
[567]446        {
[664]447          maskValue = maskValue && (*domainMasks[idxDomain])(idxLoop[indexMap[i]] + idxLoop[indexMap[i]+1] * eachDimSize[indexMap[i]]);
[567]448          ++idxDomain;
449        }
[887]450        else if (1 == axisDomainOrder(i))
[567]451        {
452          maskValue = maskValue && (*axisMasks[idxAxis])(idxLoop[indexMap[i]]);
453          ++idxAxis;
454        }
455      }
456
457      int maskIndex = idxLoop[0];
458      int mulDim = 1;
459      for (int k = 1; k < dim; ++k)
460      {
461        mulDim *= eachDimSize[k-1];
462        maskIndex += idxLoop[k]*mulDim;
463      }
[623]464      *(gridMask.dataFirst()+maskIndex) &= maskValue;
[567]465
466      ++idxLoop[0];
467      ++idx;
468    }
469
470   }
471
[1129]472   template<int N>
473   void CGrid::modifyGridMaskSize(CArray<bool,N>& gridMask,
474                                  const std::vector<int>& eachDimSize,
475                                  bool newValue)
476   {
477      if (N != eachDimSize.size())
478      {
479        // ERROR("CGrid::modifyGridMaskSize(CArray<bool,N>& gridMask,
480        //                                  const std::vector<int>& eachDimSize,
481        //                                  bool newValue)",
482        //       << "Dimension size of the mask is different from input dimension size." << std::endl
483        //       << "Mask dimension is " << N << "." << std::endl
484        //       << "Input dimension is " << eachDimSize.size() << "." << std::endl
485        //       << "Grid = " << this->GetName())
486      }
487      CArrayBoolTraits<CArray<bool,N> >::resizeArray(gridMask,eachDimSize);
488      gridMask = newValue;
489   }
490                                 
491
[623]492   /*!
493     Modify the current mask of grid, the local index to be modified will take value false
494     \param [in/out] gridMask current mask of grid
495     \param [in] indexToModify local index to modify
496   */
497   template<int N>
[1129]498   void CGrid::modifyGridMask(CArray<bool,N>& gridMask, const CArray<int,1>& indexToModify, bool valueToModify)
499   {     
[623]500     int num = indexToModify.numElements();
501     for (int idx = 0; idx < num; ++idx)
502     {
503       *(gridMask.dataFirst()+indexToModify(idx)) = valueToModify;
504     }
505   }
[219]506   ///--------------------------------------------------------------
507
508   // Declare/Define CGridGroup and CGridDefinition
509   DECLARE_GROUP(CGrid);
510
511   ///--------------------------------------------------------------
512
[335]513} // namespace xios
[219]514
[591]515#endif // __XIOS_CGrid__
Note: See TracBrowser for help on using the repository browser.