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

Last change on this file since 1243 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
Line 
1#ifndef __XIOS_CGrid__
2#define __XIOS_CGrid__
3
4/// XIOS headers ///
5#include "xios_spl.hpp"
6#include "group_factory.hpp"
7
8#include "declare_group.hpp"
9#include "domain.hpp"
10#include "axis.hpp"
11#include "scalar.hpp"
12#include "array_new.hpp"
13#include "attribute_array.hpp"
14#include "distribution_server.hpp"
15#include "client_server_mapping.hpp"
16#include "utils.hpp"
17#include "transformation_enum.hpp"
18
19namespace xios {
20
21   /// ////////////////////// Déclarations ////////////////////// ///
22
23   class CGridGroup;
24   class CGridAttributes;
25   class CDomainGroup;
26   class CAxisGroup;
27   class CScalarGroup;
28   class CGrid;
29   class CDistributionClient;
30   class CDistributionServer;
31   class CServerDistributionDescription;
32   class CClientServerMapping;
33   class CGridTransformation;
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
52      public:
53
54         typedef CGridAttributes RelAttributes;
55         typedef CGridGroup      RelGroup;
56
57         enum EEventId
58         {
59           EVENT_ID_INDEX, EVENT_ID_ADD_DOMAIN, EVENT_ID_ADD_AXIS, EVENT_ID_ADD_SCALAR
60         };
61
62         /// Constructeurs ///
63         CGrid(void);
64         explicit CGrid(const StdString& id);
65         CGrid(const CGrid& grid);       // Not implemented yet.
66         CGrid(const CGrid* const grid); // Not implemented yet.
67
68         /// Traitements ///
69//         void solveReference(void);
70
71         void checkEligibilityForCompressedOutput();
72
73         void solveDomainAxisRef(bool areAttributesChecked);
74
75         void checkMaskIndex(bool doCalculateIndex);
76
77 //        virtual void toBinary  (StdOStream& os) const;
78//         virtual void fromBinary(StdIStream& is);
79
80         void addRelFileCompressed(const StdString& filename);
81
82         /// Tests ///
83         bool isCompressible(void) const;
84         bool isWrittenCompressed(const StdString& filename) const;
85
86      public:
87
88         /// Accesseurs ///
89         StdSize getDimension(void);
90
91         StdSize  getDataSize(void) const;
92
93         /// Entrées-sorties de champs
94         template <int n>
95         void inputField(const CArray<double,n>& field, CArray<double,1>& stored) const;
96         template <int n>
97         void outputField(const CArray<double,1>& stored, CArray<double,n>& field) const;     
98
99         virtual void parse(xml::CXMLNode& node);
100
101         /// Destructeur ///
102         virtual ~CGrid(void);
103
104      public:
105
106         /// Accesseurs statiques ///
107         static StdString GetName(void);
108         static StdString GetDefName(void);
109
110         static ENodeType GetType(void);
111
112         /// Instanciateurs Statiques ///
113         static CGrid* createGrid(CDomain* domain);
114         static CGrid* createGrid(CDomain* domain, CAxis* axis);
115         static CGrid* createGrid(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
116                                  const CArray<int,1>& axisDomainOrder = CArray<int,1>());
117         static CGrid* createGrid(StdString id, const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
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);
121         static StdString generateId(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
122                                     const std::vector<CScalar*>& scalars, const CArray<int,1>& axisDomainOrder = CArray<int,1>());
123         static StdString generateId(const CGrid* gridSrc, const CGrid* gridDest);
124         static CGrid* cloneGrid(const StdString& idNewGrid, CGrid* gridSrc);
125
126      public:           
127         void computeIndexServer(void);
128         void computeIndex(void);
129         void computeIndexScalarGrid();
130         void computeWrittenIndex();
131
132         void solveDomainRef(bool checkAtt);
133         void solveAxisRef(bool checkAtt);
134         void solveScalarRef(bool checkAtt);
135         void solveDomainAxisRefInheritance(bool apply = true);
136         void solveTransformations();
137         void solveDomainAxisBaseRef();
138
139         CDomain* addDomain(const std::string& id=StdString());
140         CAxis* addAxis(const std::string& id=StdString());
141         CScalar* addScalar(const std::string& id=StdString());
142         void sendAddDomain(const std::string& id="");
143         void sendAddAxis(const std::string& id="");
144         void sendAddScalar(const std::string& id="");
145         void sendAllDomains();
146         void sendAllAxis();
147         void sendAllScalars();
148
149         static void recvAddDomain(CEventServer& event);
150         void recvAddDomain(CBufferIn& buffer);
151         static void recvAddAxis(CEventServer& event);
152         void recvAddAxis(CBufferIn& buffer);
153         static void recvAddScalar(CEventServer& event);
154         void recvAddScalar(CBufferIn& buffer);
155
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);
160         void sendIndexScalarGrid();
161
162         void computeDomConServer();
163         std::map<int, int> getDomConServerSide();
164         std::map<int, StdSize> getAttributesBufferSize(CContextClient* client);
165         std::map<int, StdSize> getDataBufferSize(CContextClient* client, const std::string& id = "");
166         std::vector<StdString> getDomainList();
167         std::vector<StdString> getAxisList();
168         std::vector<StdString> getScalarList();
169         std::vector<CDomain*> getDomains();
170         std::vector<CAxis*> getAxis();
171         std::vector<CScalar*> getScalars();
172         CDomain* getDomain(int domainIndex);
173         CAxis* getAxis(int axisIndex);
174         CScalar* getScalar(int scalarIndex);
175         std::vector<int> getAxisOrder();
176         std::vector<int> getGlobalDimension();
177         bool isScalarGrid() const;         
178
179         bool doGridHaveDataToWrite();
180         bool doGridHaveDataDistributed(CContextClient* client = 0);
181         size_t getWrittenDataSize() const;
182         int getNumberWrittenIndexes() const;
183         int getTotalNumberWrittenIndexes() const;
184         int getOffsetWrittenIndexes() const;
185
186         CDistributionServer* getDistributionServer();
187         CDistributionClient* getDistributionClient();
188         CGridTransformation* getTransformations();
189
190         void transformGrid(CGrid* transformGridSrc);
191         void completeGrid(CGrid* transformGridSrc = 0);
192         void doAutoDistribution(CGrid* transformGridSrc);
193         bool isTransformed();
194         void setTransformed();
195         bool isGenerated();
196         void setGenerated();
197         void addTransGridSource(CGrid* gridSrc);
198         std::map<CGrid*, std::pair<bool,StdString> >& getTransGridSource();
199         bool hasTransform();
200         size_t getGlobalWrittenSize(void) ;
201
202      public:
203         CArray<int, 1> storeIndex_client;
204
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
208         map<int, CArray<int, 1> > storeIndex_fromSrv; // Support, for now, reading with level-1 server
209
210
211         std::map<CContextClient*, std::map<int,int> > nbSenders, nbReadSenders;
212
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
221/** Indexes calculated based on server distribution (serverDistribution_). They are used for writing data into a file. */
222         CArray<size_t,1> localIndexToWriteOnServer;
223
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
228         CArray<size_t,1> indexFromClients;
229         void checkMask(void);
230         void createMask(void);
231         void modifyMask(const CArray<int,1>& indexToModify, bool valueToModify = false);
232         void modifyMaskSize(const std::vector<int>& newDimensionSize, bool newValue = false);
233
234         void computeGridGlobalDimension(const std::vector<CDomain*>& domains,
235                                         const std::vector<CAxis*>& axis,
236                                         const std::vector<CScalar*>& scalars,
237                                         const CArray<int,1>& axisDomainOrder);
238
239      private:
240       template<int N>
241       void checkGridMask(CArray<bool,N>& gridMask,
242                          const std::vector<CArray<bool,1>* >& domainMasks,
243                          const std::vector<CArray<bool,1>* >& axisMasks,
244                          const CArray<int,1>& axisDomainOrder,
245                          bool createMask = false);
246        template<int N>
247        void modifyGridMask(CArray<bool,N>& gridMask, const CArray<int,1>& indexToModify, bool valueToModify);
248
249        template<int N>
250        void modifyGridMaskSize(CArray<bool,N>& gridMask, const std::vector<int>& eachDimSize, bool newValue);
251
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
255        void setVirtualDomainGroup(CDomainGroup* newVDomainGroup);
256        void setVirtualAxisGroup(CAxisGroup* newVAxisGroup);
257        void setVirtualScalarGroup(CScalarGroup* newVScalarGroup);
258
259        void setDomainList(const std::vector<CDomain*> domains = std::vector<CDomain*>());
260        void setAxisList(const std::vector<CAxis*> axis = std::vector<CAxis*>());
261        void setScalarList(const std::vector<CScalar*> scalars = std::vector<CScalar*>());
262
263        CDomainGroup* getVirtualDomainGroup() const;
264        CAxisGroup* getVirtualAxisGroup() const;
265        CScalarGroup* getVirtualScalarGroup() const;
266
267        void checkAttributesAfterTransformation();
268        void setTransformationAlgorithms();
269        void computeIndexByElement(const std::vector<boost::unordered_map<size_t,std::vector<int> > >& indexServerOnElement,
270                                   CClientServerMapping::GlobalIndexMap& globalIndexOnServer);
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();
277
278        void computeClientIndex();
279        void computeConnectedClients();
280        void computeClientIndexScalarGrid(); 
281        void computeConnectedClientsScalarGrid(); 
282
283      private:
284         bool isChecked;
285         bool isDomainAxisChecked;
286         bool isIndexSent;
287
288        CDomainGroup* vDomainGroup_;
289        CAxisGroup* vAxisGroup_;
290        CScalarGroup* vScalarGroup_;
291        std::vector<std::string> axisList_, domList_, scalarList_;
292        bool isAxisListSet, isDomListSet, isScalarListSet;
293
294/** Distribution calculated in computeClientIndex() based on the knowledge of the entire grid */
295        CDistributionClient* clientDistribution_;
296
297/** Distribution calculated upon receiving indexes */
298        CDistributionServer* serverDistribution_;
299
300        CClientServerMapping* clientServerMap_;
301        size_t writtenDataSize_;
302        int numberWrittenIndexes_, totalNumberWrittenIndexes_, offsetWrittenIndexes_;
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 (?) */
308        std::map<CContextClient*, std::map<int,size_t> > connectedDataSize_;
309
310        bool isDataDistributed_;       
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;
314
315        bool isTransformed_, isGenerated_;
316        bool computedWrittenIndex_;
317        std::vector<int> axisPositionInGrid_;
318        CGridTransformation* transformations_;
319        bool hasDomainAxisBaseRef_;       
320        std::map<CGrid*, std::pair<bool,StdString> > gridSrc_;
321        bool hasTransform_;
322
323/** Map storing data size that will be sent to connected servers. In future: change the key to the server size (?) */
324        std::map<CContextClient*, CClientServerMapping::GlobalIndexMap> globalIndexOnServer_;
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) */
327        std::vector<int> order_;
328
329   }; // class CGrid
330
331   ///--------------------------------------------------------------
332
333   template <int n>
334   void CGrid::inputField(const CArray<double,n>& field, CArray<double,1>& stored) const
335   {
336      if (this->getDataSize() != field.numElements())
337         ERROR("void CGrid::inputField(const  CArray<double,n>& field, CArray<double,1>& stored) const",
338                << "[ Awaiting data of size = " << this->getDataSize() << ", "
339                << "Received data size = "      << field.numElements() << " ] "
340                << "The data array does not have the right size! "
341                << "Grid = " << this->GetName())
342      this->storeField_arr(field.dataFirst(), stored);
343   }
344
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() << " ] "
352                << "The ouput array does not have the right size! "
353                << "Grid = " << this->GetName())
354      this->restoreField_arr(stored, field.dataFirst());
355   }
356
357   template<int N>
358   void CGrid::checkGridMask(CArray<bool,N>& gridMask,
359                             const std::vector<CArray<bool,1>* >& domainMasks,
360                             const std::vector<CArray<bool,1>* >& axisMasks,
361                             const CArray<int,1>& axisDomainOrder,
362                             bool createMask)
363   {
364     int idx = 0;
365     int numElement = axisDomainOrder.numElements();
366     int dim = domainMasks.size() * 2 + axisMasks.size();
367     std::vector<CDomain*> domainP = this->getDomains();
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;
375      if (2 == axisDomainOrder(i)) {
376          eachDimSize[indexMap[i]]   = domainP[idxDomain]->ni;
377          eachDimSize[indexMap[i]+1] = domainP[idxDomain]->nj;
378          idx += 2; ++idxDomain;
379      }
380      else if (1 == axisDomainOrder(i)) {
381        eachDimSize[indexMap[i]] = axisMasks[idxAxis]->numElements();
382        ++idx; ++idxAxis;
383      }
384      else {};
385    }
386
387    if (!gridMask.isEmpty() && !createMask)
388    {
389      for (int i = 0; i < dim; ++i)
390      {
391        if (gridMask.extent(i) != eachDimSize[i])
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
395                << "Mask size for dimension " << i << " is " << gridMask.extent(i) << "." << std::endl
396                << "Grid = " << this->GetName())
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      {
422        if (2 == axisDomainOrder(i))
423        {
424          maskValue = maskValue && (*domainMasks[idxDomain])(idxLoop[indexMap[i]] + idxLoop[indexMap[i]+1] * eachDimSize[indexMap[i]]);
425          ++idxDomain;
426        }
427        else if (1 == axisDomainOrder(i))
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      }
441      *(gridMask.dataFirst()+maskIndex) &= maskValue;
442
443      ++idxLoop[0];
444      ++idx;
445    }
446
447   }
448
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
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>
475   void CGrid::modifyGridMask(CArray<bool,N>& gridMask, const CArray<int,1>& indexToModify, bool valueToModify)
476   {     
477     int num = indexToModify.numElements();
478     for (int idx = 0; idx < num; ++idx)
479     {
480       *(gridMask.dataFirst()+indexToModify(idx)) = valueToModify;
481     }
482   }
483   ///--------------------------------------------------------------
484
485   // Declare/Define CGridGroup and CGridDefinition
486   DECLARE_GROUP(CGrid);
487
488   ///--------------------------------------------------------------
489
490} // namespace xios
491
492#endif // __XIOS_CGrid__
Note: See TracBrowser for help on using the repository browser.