source: XIOS/trunk/src/node/grid.hpp @ 865

Last change on this file since 865 was 865, checked in by mhnguyen, 8 years ago

Optimizing codes: Change the way to compute distribution of grid

+) Instead of using DHT on grid, we make use of it only for elements of grid
+) Make change to calculation of server distribution.

Test
+) On Curie
+) Two times faster than the precedent commit.

  • 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: 14.8 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 "array_new.hpp"
12#include "attribute_array.hpp"
13#include "distribution_server.hpp"
14#include "client_server_mapping.hpp"
15#include "utils.hpp"
16#include "transformation_enum.hpp"
17
18namespace xios {
19
20   /// ////////////////////// Déclarations ////////////////////// ///
21
22   class CGridGroup;
23   class CGridAttributes;
24   class CDomainGroup;
25   class CAxisGroup;
26   class CGrid;
27   class CDistributionClient;
28   class CDistributionServer;
29   class CServerDistributionDescription;
30   class CClientServerMapping;
31   class CGridTransformation;
32
33   ///--------------------------------------------------------------
34
35   // Declare/Define CGridAttribute
36   BEGIN_DECLARE_ATTRIBUTE_MAP(CGrid)
37#  include "grid_attribute.conf"
38   END_DECLARE_ATTRIBUTE_MAP(CGrid)
39
40   ///--------------------------------------------------------------
41
42   class CGrid
43      : public CObjectTemplate<CGrid>
44      , public CGridAttributes
45   {
46         /// typedef ///
47         typedef CObjectTemplate<CGrid>   SuperClass;
48         typedef CGridAttributes SuperClassAttribute;
49
50      public:
51
52         typedef CGridAttributes RelAttributes;
53         typedef CGridGroup      RelGroup;
54
55         enum EEventId
56         {
57           EVENT_ID_INDEX, EVENT_ID_ADD_DOMAIN, EVENT_ID_ADD_AXIS
58         };
59
60         enum EElementType
61         {
62           GRID_ONLY_AXIS, GRID_ONLY_DOMAIN, GRID_AXIS_DOMAIN
63         };
64
65         /// Constructeurs ///
66         CGrid(void);
67         explicit CGrid(const StdString& id);
68         CGrid(const CGrid& grid);       // Not implemented yet.
69         CGrid(const CGrid* const grid); // Not implemented yet.
70
71         /// Traitements ///
72//         void solveReference(void);
73
74         void checkEligibilityForCompressedOutput();
75
76         void solveDomainAxisRef(bool areAttributesChecked);
77
78         void checkMaskIndex(bool doCalculateIndex);
79
80 //        virtual void toBinary  (StdOStream& os) const;
81//         virtual void fromBinary(StdIStream& is);
82
83         void addRelFileCompressed(const StdString& filename);
84
85         /// Tests ///
86         bool isCompressible(void) const;
87         bool isWrittenCompressed(const StdString& filename) const;
88
89      public:
90
91         /// Accesseurs ///
92         StdSize getDimension(void) const;
93
94         StdSize  getDataSize(void) const;
95
96         /// Entrées-sorties de champs ///
97         template <int n>
98         void inputField(const CArray<double,n>& field, CArray<double,1>& stored) const;
99         template <int n>
100         void outputField(const CArray<double,1>& stored, CArray<double,n>& field) const;
101
102         void outputField(int rank, const CArray<double,1>& stored, double* field);
103         void inputField(int rank, const double* const field, CArray<double,1>& stored);
104
105         void outputCompressedField(int rank, const CArray<double,1>& stored, double* field);
106
107         virtual void parse(xml::CXMLNode& node);
108
109         /// Destructeur ///
110         virtual ~CGrid(void);
111
112      public:
113
114         /// Accesseurs statiques ///
115         static StdString GetName(void);
116         static StdString GetDefName(void);
117
118         static ENodeType GetType(void);
119
120         /// Instanciateurs Statiques ///
121         static CGrid* createGrid(CDomain* domain);
122         static CGrid* createGrid(CDomain* domain, CAxis* axis);
123         static CGrid* createGrid(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
124                                  const CArray<bool,1>& axisDomainOrder = CArray<bool,1>());
125         static CGrid* createGrid(StdString id, const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
126                                  const CArray<bool,1>& axisDomainOrder = CArray<bool,1>());
127         static StdString generateId(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
128                                     const CArray<bool,1>& axisDomainOrder = CArray<bool,1>());
129         static StdString generateId(const CGrid* gridSrc, const CGrid* gridDest);
130         static CGrid* cloneGrid(const StdString& idNewGrid, CGrid* gridSrc);
131
132      public:
133
134         /// Entrées-sorties de champs (interne) ///
135         void storeField_arr(const double* const data, CArray<double,1>& stored) const;
136         void restoreField_arr(const CArray<double,1>& stored, double* const data) const;
137
138         /// Traitements protégés ///
139         void computeIndexServer(void);
140         void computeIndex(void);
141         void computeIndexScalarGrid();
142         void computeCompressedIndex();
143
144         void solveDomainRef(bool checkAtt);
145         void solveAxisRef(bool checkAtt);
146         void solveDomainAxisRefInheritance(bool apply = true);
147         void solveTransformations();
148         void solveDomainAxisBaseRef();
149
150         CDomain* addDomain(const std::string& id=StdString());
151         CAxis* addAxis(const std::string& id=StdString());
152         void sendAddDomain(const std::string& id="");
153         void sendAddAxis(const std::string& id="");
154         void sendAllDomains();
155         void sendAllAxis();
156
157         static void recvAddDomain(CEventServer& event);
158         void recvAddDomain(CBufferIn& buffer);
159         static void recvAddAxis(CEventServer& event);
160         void recvAddAxis(CBufferIn& buffer);
161
162         static bool dispatchEvent(CEventServer& event);
163         static void recvIndex(CEventServer& event);
164         void recvIndex(vector<int> ranks, vector<CBufferIn*> buffers);
165         void sendIndex(void);
166         void sendIndexScalarGrid();
167
168         void computeDomConServer();
169         std::map<int, int> getDomConServerSide();
170         std::map<int, StdSize> getAttributesBufferSize();
171         std::map<int, StdSize> getDataBufferSize(const std::string& id = "");
172         std::vector<StdString> getDomainList();
173         std::vector<StdString> getAxisList();
174         std::vector<CDomain*> getDomains();
175         std::vector<CAxis*> getAxis();
176         std::vector<int> getAxisOrder();
177         std::vector<int> getGlobalDimension();
178         bool isScalarGrid() const;
179         std::vector<int> getAxisPositionInGrid() const;
180
181         bool doGridHaveDataToWrite();
182         bool doGridHaveDataDistributed();
183         size_t getWrittenDataSize() const;
184         int getNumberWrittenIndexes() const;
185         int getTotalNumberWrittenIndexes() const;
186         int getOffsetWrittenIndexes() const;
187
188         CDistributionServer* getDistributionServer();
189         CDistributionClient* getDistributionClient();
190         CGridTransformation* getTransformations();
191
192         void transformGrid(CGrid* transformGridSrc);
193         void completeGrid(CGrid* transformGridSrc = 0);
194         void doAutoDistribution(CGrid* transformGridSrc);
195         bool isTransformed();
196         void setTransformed();
197         void addTransGridSource(CGrid* gridSrc);
198         std::map<CGrid*, std::pair<bool,StdString> >& getTransGridSource();
199         bool hasTransform();
200
201      public:
202
203         /// Propriétés privées ///
204         bool isChecked;
205         bool isDomainAxisChecked;
206         bool isIndexSent;
207
208         CArray<int, 1> storeIndex_client;
209
210         map<int, CArray<int, 1> > storeIndex_toSrv;
211         map<int,int> nbSenders;
212
213         map<int, CArray<size_t, 1> > outIndexFromClient, compressedOutIndexFromClient;
214         void checkMask(void);
215         void createMask(void);
216         void modifyMask(const CArray<int,1>& indexToModify);
217
218         void computeGridGlobalDimension(const std::vector<CDomain*>& domains,
219                                         const std::vector<CAxis*>& axis,
220                                         const CArray<bool,1>& axisDomainOrder);
221
222      private:
223       template<int N>
224       void checkGridMask(CArray<bool,N>& gridMask,
225                          const std::vector<CArray<bool,1>* >& domainMasks,
226                          const std::vector<CArray<bool,1>* >& axisMasks,
227                          const CArray<bool,1>& axisDomainOrder,
228                          bool createMask = false);
229        template<int N>
230        void modifyGridMask(CArray<bool,N>& gridMask, const CArray<int,1>& indexToModify);
231
232        void setVirtualDomainGroup(CDomainGroup* newVDomainGroup);
233        void setVirtualDomainGroup();
234        void setVirtualAxisGroup(CAxisGroup* newVAxisGroup);
235        void setVirtualAxisGroup();
236
237        void setAxisList(const std::vector<CAxis*> axis = std::vector<CAxis*>());
238        void setDomainList(const std::vector<CDomain*> domains = std::vector<CDomain*>());
239
240        CAxisGroup* getVirtualAxisGroup() const;
241        CDomainGroup* getVirtualDomainGroup() const;
242
243        void checkAttributesAfterTransformation();
244        void setTransformationAlgorithms();
245        void computeIndexByElement(const std::vector<boost::unordered_map<size_t,std::vector<int> > >& indexServerOnElement,
246                                   CClientServerMapping::GlobalIndexMap& globalIndexOnServer);
247
248
249      private:
250        CDomainGroup* vDomainGroup_;
251        CAxisGroup* vAxisGroup_;
252        std::vector<std::string> axisList_, domList_;
253        bool isAxisListSet, isDomListSet;
254
255        CDistributionClient* clientDistribution_;
256        CDistributionServer* serverDistribution_;
257        CClientServerMapping* clientServerMap_;
258        size_t writtenDataSize_;
259        int numberWrittenIndexes_, totalNumberWrittenIndexes_, offsetWrittenIndexes_;
260        std::map<int,size_t> connectedDataSize_;
261        std::vector<int> connectedServerRank_;
262        bool isDataDistributed_;
263        int positionDimensionDistributed_;
264         //! True if and only if the data defined on the grid can be outputted in a compressed way
265        bool isCompressible_;
266        std::set<std::string> relFilesCompressed;
267
268        bool isTransformed_;
269        std::vector<int> axisPositionInGrid_;
270        CGridTransformation* transformations_;
271        bool hasDomainAxisBaseRef_;
272        std::vector<int> globalDim_;
273        std::map<CGrid*, std::pair<bool,StdString> > gridSrc_;
274        bool hasTransform_;
275        CClientServerMapping::GlobalIndexMap globalIndexOnServer_;
276            // List order of axis and domain in a grid, if there is a domain, it will take value 1 (true), axis 0 (false)
277        std::vector<bool> order_;
278   }; // class CGrid
279
280   ///--------------------------------------------------------------
281
282   template <int n>
283   void CGrid::inputField(const CArray<double,n>& field, CArray<double,1>& stored) const
284   {
285      if (this->getDataSize() != field.numElements())
286         ERROR("void CGrid::inputField(const  CArray<double,n>& field, CArray<double,1>& stored) const",
287                << "[ Awaiting data of size = " << this->getDataSize() << ", "
288                << "Received data size = "      << field.numElements() << " ] "
289                << "The data array does not have the right size!")
290      this->storeField_arr(field.dataFirst(), stored);
291   }
292
293   template <int n>
294   void CGrid::outputField(const CArray<double,1>& stored, CArray<double,n>& field) const
295   {
296      if (this->getDataSize() != field.numElements())
297         ERROR("void CGrid::outputField(const CArray<double,1>& stored, CArray<double,n>& field) const",
298                << "[ Size of the data = " << this->getDataSize() << ", "
299                << "Output data size = "   << field.numElements() << " ] "
300                << "The ouput array does not have the right size!")
301      this->restoreField_arr(stored, field.dataFirst());
302   }
303
304   template<int N>
305   void CGrid::checkGridMask(CArray<bool,N>& gridMask,
306                             const std::vector<CArray<bool,1>* >& domainMasks,
307                             const std::vector<CArray<bool,1>* >& axisMasks,
308                             const CArray<bool,1>& axisDomainOrder,
309                             bool createMask)
310   {
311     int idx = 0;
312     int numElement = axisDomainOrder.numElements();
313     int dim = domainMasks.size() * 2 + axisMasks.size();
314     std::vector<CDomain*> domainP = this->getDomains();
315
316     std::vector<int> idxLoop(dim,0), indexMap(numElement), eachDimSize(dim);
317     std::vector<int> currentIndex(dim);
318     int idxDomain = 0, idxAxis = 0;
319    for (int i = 0; i < numElement; ++i)
320    {
321      indexMap[i] = idx;
322      if (true == axisDomainOrder(i)) {
323          eachDimSize[indexMap[i]]   = domainP[idxDomain]->ni;
324          eachDimSize[indexMap[i]+1] = domainP[idxDomain]->nj;
325          idx += 2; ++idxDomain;
326      }
327      else {
328        eachDimSize[indexMap[i]] = axisMasks[idxAxis]->numElements();
329        ++idx; ++idxAxis;
330      }
331    }
332
333    if (!gridMask.isEmpty() && !createMask)
334    {
335      for (int i = 0; i < dim; ++i)
336      {
337        if (gridMask.extent(i) != eachDimSize[i])
338          ERROR("CGrid::checkMask(void)",
339                << "The mask has one dimension whose size is different from the one of the local grid." << std::endl
340                << "Local size of dimension " << i << " is " << eachDimSize[i] << "." << std::endl
341                << "Mask size for dimension " << i << " is " << gridMask.extent(i) << ".");
342      }
343    }
344    else {
345        CArrayBoolTraits<CArray<bool,N> >::resizeArray(gridMask,eachDimSize);
346        gridMask = true;
347    }
348
349    int ssize = gridMask.numElements();
350    idx = 0;
351    while (idx < ssize)
352    {
353      for (int i = 0; i < dim-1; ++i)
354      {
355        if (idxLoop[i] == eachDimSize[i])
356        {
357          idxLoop[i] = 0;
358          ++idxLoop[i+1];
359        }
360      }
361
362      // Find out outer index
363      idxDomain = idxAxis = 0;
364      bool maskValue = true;
365      for (int i = 0; i < numElement; ++i)
366      {
367        if (axisDomainOrder(i))
368        {
369          maskValue = maskValue && (*domainMasks[idxDomain])(idxLoop[indexMap[i]] + idxLoop[indexMap[i]+1] * eachDimSize[indexMap[i]]);
370          ++idxDomain;
371        }
372        else
373        {
374          maskValue = maskValue && (*axisMasks[idxAxis])(idxLoop[indexMap[i]]);
375          ++idxAxis;
376        }
377      }
378
379      int maskIndex = idxLoop[0];
380      int mulDim = 1;
381      for (int k = 1; k < dim; ++k)
382      {
383        mulDim *= eachDimSize[k-1];
384        maskIndex += idxLoop[k]*mulDim;
385      }
386      *(gridMask.dataFirst()+maskIndex) &= maskValue;
387
388      ++idxLoop[0];
389      ++idx;
390    }
391
392   }
393
394   /*!
395     Modify the current mask of grid, the local index to be modified will take value false
396     \param [in/out] gridMask current mask of grid
397     \param [in] indexToModify local index to modify
398   */
399   template<int N>
400   void CGrid::modifyGridMask(CArray<bool,N>& gridMask, const CArray<int,1>& indexToModify)
401   {
402     bool valueToModify = false;
403     int num = indexToModify.numElements();
404     for (int idx = 0; idx < num; ++idx)
405     {
406       *(gridMask.dataFirst()+indexToModify(idx)) = valueToModify;
407     }
408   }
409   ///--------------------------------------------------------------
410
411   // Declare/Define CGridGroup and CGridDefinition
412   DECLARE_GROUP(CGrid);
413
414   ///--------------------------------------------------------------
415
416} // namespace xios
417
418#endif // __XIOS_CGrid__
Note: See TracBrowser for help on using the repository browser.