source: XIOS/dev/dev_ym/XIOS_COUPLING/src/node/grid.hpp @ 1881

Last change on this file since 1881 was 1881, checked in by ymipsl, 4 years ago

XIOS coupling Branch

  • fix timestamp problem when receiving field from other coupling context
  • fix deadlock : when receiving index from coupling context, a collective communication was involved which is forbiden

YM

  • 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: 30.4 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   /// ////////////////////// Declarations ////////////////////// ///
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
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) ;
92
93         /**
94          * Get the local data grid size, ie the size of the compressed grid (inside the workflow)
95          * \return The size od the compressed grid
96          */
97         StdSize  getLocalDataSize(void) ;
98
99         /// Entrees-sorties de champs
100         template <int n>
101         void inputField(const CArray<double,n>& field, CArray<double,1>& stored) ;
102         template <int n>
103         void maskField(const CArray<double,n>& field, CArray<double,1>& stored) ;
104         template <int n>
105         void outputField(const CArray<double,1>& stored, CArray<double,n>& field) ; 
106         template <int n>
107         void uncompressField(const CArray<double,n>& data, CArray<double,1>& outData) ; 
108
109         virtual void parse(xml::CXMLNode& node);
110
111         /// Destructeur ///
112         virtual ~CGrid(void);
113
114      public:
115
116         /// Accesseurs statiques ///
117         static StdString GetName(void);
118         static StdString GetDefName(void);
119
120         static ENodeType GetType(void);
121
122         /// Instanciateurs Statiques ///
123         static CGrid* createGrid(CDomain* domain);
124         static CGrid* createGrid(CDomain* domain, CAxis* axis);
125         static CGrid* createGrid(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
126                                  const CArray<int,1>& axisDomainOrder = CArray<int,1>());
127         static CGrid* createGrid(StdString id, const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
128                                  const std::vector<CScalar*>& scalars, const CArray<int,1>& axisDomainOrder = CArray<int,1>());
129         static CGrid* createGrid(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
130                                  const std::vector<CScalar*>& scalars, const CArray<int,1>& axisDomainOrder);
131         static StdString generateId(const std::vector<CDomain*>& domains, const std::vector<CAxis*>& axis,
132                                     const std::vector<CScalar*>& scalars, const CArray<int,1>& axisDomainOrder = CArray<int,1>());
133         static StdString generateId(const CGrid* gridSrc, const CGrid* gridDest);
134         static CGrid* cloneGrid(const StdString& idNewGrid, CGrid* gridSrc);
135
136      public:           
137         void computeIndexServer(void);
138         void computeIndex(void);
139         void computeIndexScalarGrid();
140         void computeWrittenIndex();
141         void solveDomainAxisRef(bool areAttributesChecked);
142         void checkElementsAttributes(void) ;
143
144         void solveDomainRef(bool checkAtt);
145         void solveAxisRef(bool checkAtt);
146         void solveScalarRef(bool checkAtt);
147         void solveElementsRefInheritance(bool apply = true);
148        // void solveTransformations();
149         void solveDomainAxisBaseRef();
150
151         CDomain* addDomain(const std::string& id=StdString());
152         CAxis* addAxis(const std::string& id=StdString());
153         CScalar* addScalar(const std::string& id=StdString());
154
155      public:
156         void sendGridToFileServer(CContextClient* client) ;
157      private:
158         std::set<CContextClient*> sendGridToFileServer_done_ ;
159     
160      public:
161         void sendGridToCouplerOut(CContextClient* client, const string& fieldId) ;
162      private:
163         std::set<CContextClient*> sendGridToCouplerOut_done_ ;
164
165      public:
166         void makeAliasForCoupling(const string& fieldId) ;
167
168      public:
169         void sendAddDomain(const std::string& id,CContextClient* contextClient);
170         void sendAddAxis(const std::string& id,CContextClient* contextClient);
171         void sendAddScalar(const std::string& id,CContextClient* contextClient);
172         void sendAllDomains(CContextClient* contextClient);
173         void sendAllAxis(CContextClient* contextClient);
174         void sendAllScalars(CContextClient* contextClient);
175
176         static void recvAddDomain(CEventServer& event);
177         void recvAddDomain(CBufferIn& buffer);
178         static void recvAddAxis(CEventServer& event);
179         void recvAddAxis(CBufferIn& buffer);
180         static void recvAddScalar(CEventServer& event);
181         void recvAddScalar(CBufferIn& buffer);
182
183         static bool dispatchEvent(CEventServer& event);
184         static void recvIndex(CEventServer& event);
185         void recvIndex(vector<int> ranks, vector<CBufferIn*> buffers, CContextServer* server);
186       
187       public: 
188         void sendIndex(CContextClient* client, const string& gridId="");
189       private:
190          set<CContextClient*> sendIndex_done_ ;
191       
192       public:
193         void sendIndexScalarGrid(CContextClient* client, const string& gridId="");
194       private:
195          set<CContextClient*> sendIndexScalarGrid_done_ ;
196     
197       public:
198         void setContextClient(CContextClient* contextClient);
199
200         void computeDomConServer();
201         std::map<int, int> getDomConServerSide();
202         std::map<int, StdSize> getAttributesBufferSize(CContextClient* client, bool bufferForWriting = false);
203         std::map<int, StdSize> getDataBufferSize(CContextClient* client, const std::string& id = "", bool bufferForWriting = false);
204         std::vector<StdString> getDomainList();
205         std::vector<StdString> getAxisList();
206         std::vector<StdString> getScalarList();
207         std::vector<CDomain*> getDomains();
208         std::vector<CAxis*> getAxis();
209         std::vector<CScalar*> getScalars();
210         CDomain* getDomain(int domainIndex);
211         CAxis* getAxis(int axisIndex);
212         CScalar* getScalar(int scalarIndex);
213         std::vector<int> getAxisOrder();
214         std::vector<int> getGlobalDimension();
215         bool isScalarGrid() const;         
216
217         bool doGridHaveDataToWrite();
218         bool doGridHaveDataDistributed(CContextClient* client = 0);
219         size_t getWrittenDataSize() ;
220         int getNumberWrittenIndexes() const;
221         int getTotalNumberWrittenIndexes() const;
222         int getOffsetWrittenIndexes() const;
223
224         CGridTransformation* getTransformations();
225
226         void transformGrid(CGrid* transformGridSrc);
227         
228         void prepareTransformGrid(CGrid* transformGridSrc);
229         bool prepareTransformGrid_done_ = false ;
230
231         void makeTransformGrid(void); 
232         bool makeTransformGrid_done_ = false ;
233         
234         std::vector<std::string> getAuxInputTransformGrid(void) ; 
235
236         void completeGrid(CGrid* transformGridSrc = 0);
237         void doAutoDistribution(CGrid* transformGridSrc);
238         bool isTransformed();
239         void setTransformed();
240         bool isGenerated();
241         void setGenerated();
242         void addTransGridSource(CGrid* gridSrc);
243         std::map<CGrid*, std::pair<bool,StdString> >& getTransGridSource();
244         bool hasTransform();
245         size_t getGlobalWrittenSize(void) ;
246         
247         bool isCompleted(void) ;
248         void setCompleted(void) ;
249         void unsetCompleted(void) ;
250
251
252         bool hasMask(void) const;
253         void checkMask(void);
254         void createMask(void);
255         void modifyMask(const CArray<int,1>& indexToModify, bool valueToModify = false);
256         void modifyMaskSize(const std::vector<int>& newDimensionSize, bool newValue = false);
257
258         void computeGridGlobalDimension(const std::vector<CDomain*>& domains,
259                                         const std::vector<CAxis*>& axis,
260                                         const std::vector<CScalar*>& scalars,
261                                         const CArray<int,1>& axisDomainOrder);
262
263         void computeGridIndexToFileServer(CContextClient* client) ;
264 
265     private:
266        /** Client-like distribution calculated based on the knowledge of the entire grid */
267       CDistributionClient* clientDistribution_;
268     public: 
269       void computeClientDistribution(void) ;
270     private:
271       bool computeClientDistribution_done_ = false ;
272     public:
273       CDistributionClient* getClientDistribution(void); 
274
275     private:   
276       /** Server-like distribution calculated upon receiving indexes */
277       CDistributionServer* serverDistribution_;
278       void computeServerDistribution(void) ;
279       bool computeServerDistribution_done_=false ;
280     public: 
281       CDistributionServer* getServerDistribution(void) { if (!computeServerDistribution_done_) computeServerDistribution() ; return serverDistribution_ ;}
282
283
284     private:
285       template<int N>
286       void checkGridMask(CArray<bool,N>& gridMask,
287                          const std::vector<CArray<bool,1>* >& domainMasks,
288                          const std::vector<CArray<bool,1>* >& axisMasks,
289                          const CArray<int,1>& axisDomainOrder,
290                          bool createMask = false);
291        template<int N>
292        void modifyGridMask(CArray<bool,N>& gridMask, const CArray<int,1>& indexToModify, bool valueToModify);
293
294        template<int N>
295        void modifyGridMaskSize(CArray<bool,N>& gridMask, const std::vector<int>& eachDimSize, bool newValue);
296
297        void storeField_arr(const double* const data, CArray<double, 1>& stored) ;
298        void restoreField_arr(const CArray<double, 1>& stored, double* const data) ;
299        void uncompressField_arr(const double* const data, CArray<double, 1>& outData) ;
300        void maskField_arr(const double* const data, CArray<double, 1>& stored) ;
301
302        void setVirtualDomainGroup(CDomainGroup* newVDomainGroup);
303        void setVirtualAxisGroup(CAxisGroup* newVAxisGroup);
304        void setVirtualScalarGroup(CScalarGroup* newVScalarGroup);
305
306        void setDomainList(const std::vector<CDomain*> domains = std::vector<CDomain*>());
307        void setAxisList(const std::vector<CAxis*> axis = std::vector<CAxis*>());
308        void setScalarList(const std::vector<CScalar*> scalars = std::vector<CScalar*>());
309
310        CDomainGroup* getVirtualDomainGroup() const;
311        CAxisGroup* getVirtualAxisGroup() const;
312        CScalarGroup* getVirtualScalarGroup() const;
313
314        void checkAttributesAfterTransformation();
315        void setTransformationAlgorithms();
316        void computeIndexByElement(const std::vector<std::unordered_map<size_t,std::vector<int> > >& indexServerOnElement,
317                                   const CContextClient* client,
318                                   CClientServerMapping::GlobalIndexMap& globalIndexOnServer);
319        int computeGridGlobalDimension(std::vector<int>& globalDim,
320                                       const std::vector<CDomain*> domains,
321                                       const std::vector<CAxis*> axis,
322                                       const std::vector<CScalar*> scalars,
323                                       const CArray<int,1>& axisDomainOrder);
324        int getDistributedDimension();
325
326        void computeConnectedClients(CContextClient* client);
327        set<CContextClient*> computeConnectedClients_done_ ;
328
329        void computeConnectedClientsScalarGrid(CContextClient* client); 
330        set<CContextClient*> computeConnectedClientsScalarGrid_done_ ;
331
332      public:
333/** Array containing the local index of the grid
334 *  storeIndex_client[local_workflow_grid_index] -> local_model_grid_index.
335 *  Used to store field from model into the worklow, or to return field into models. 
336 *  The size of the array is the number of local index of the workflow grid */       
337        CArray<int, 1> storeIndex_client_;
338        void computeStoreIndex_client(void) ;
339        bool computeStoreIndex_client_done_ = false ;
340        CArray<int, 1>& getStoreIndex_client(void) { if (!computeStoreIndex_client_done_) computeStoreIndex_client() ; return storeIndex_client_ ;}
341
342/** Array containing the grid mask masked defined by the mask_nd grid attribute.       
343  * The corresponding masked field value provided by the model will be replaced by a NaN value
344  * in the workflow.  */
345        CArray<bool, 1> storeMask_client_;
346        void computeStoreMask_client(void) ;
347        bool computeStoreMask_client_done_ = false ;
348        CArray<bool, 1>& getStoreMask_client(void) { if (!computeStoreMask_client_done_) computeStoreMask_client() ; return storeMask_client_ ;}
349
350/** Map containing the indexes on client side that will be sent to each connected server.
351  * storeIndex_toSrv[&contextClient] -> map concerning the contextClient for which the data will be sent (client side)
352  * storeIndex_toSrv[&contextClient][rank] -> array of indexes that will be sent to each "rank" of the connected servers
353  * storeIndex_toSrv[&contextClient][rank](index_of_buffer_sent_to_server) -> local index of the field of the workflow
354  * grid that will be sent to server */
355         std::map<CContextClient*, map<int, CArray<int, 1> > > storeIndex_toSrv_;
356
357
358/** Map containing the indexes on client side that will be received from each connected server.
359  * This map is used to agreggate field data received from server (for reading) into a single array, which will be an entry
360  * point of the worklow on client side.
361  * storeIndex_toSrv[rank] -> array of indexes that will be received from each "rank" of the connected servers
362  * storeIndex_toSrv[rank](index_of_buffer_received_from_server) -> local index of the field in the "workflow grid"
363  * that has been received from server */
364         std::map<int, CArray<int, 1> > storeIndex_fromSrv_; // Support, for now, reading with level-1 server
365
366
367/** Maps storing the number of participating clients for data sent a specific server for a given contextClient, identified
368  * by the servers communicator size. In future must be direcly identified by context.
369  * nbSender_[context_server_size] -> map the number of client sender by connected rank of servers
370  * nbSender_[context_server_size] [rank_server] -> the number of client participating to a send message for a server of rank "rank_server"
371  * Usefull to indicate in a message the number of participant needed by the transfer protocol */
372         std::map<int, std::map<int,int> > nbSenders_;
373
374      private:
375/** Maps storing the number of participating servers for data sent a specific client for a given contextClient.
376  * Symetric of nbSenders_, but for server side which want to send data to client.
377  * nbReadSender_[context_client_size] -> map the number of server sender by connected rank of clients
378  * nbReadSender_[context_client_size] [rank_client] -> the number of server participating to a send message for a client of rank "rank_client"
379  * Usefull to indicate in a message the number of participant needed by the transfer protocol */
380         std::map<CContextClient*, std::map<int,int> > nbReadSenders_;
381      public:
382         std::map<int,int>& getNbReadSenders(CContextClient* client) 
383         { if (nbReadSenders_.count(client)==0) computeNbReadSenders(client) ; return nbReadSenders_[client] ;}
384      private:
385         void computeNbReadSenders(CContextClient* client) ;
386     
387
388// Manh Ha's comment: " A client receives global index from other clients (via recvIndex)
389// then does mapping these index into local index of STORE_CLIENTINDEX
390// In this way, store_clientIndex can be used as an input of a source filter
391// Maybe we need a flag to determine whether a client wants to write. TODO "
392
393      private:
394       /** Map storing received data on server side. This map is the equivalent to the storeIndex_client, but for data received from client
395        * instead that from model. This map is used to concatenate data received from several clients into a single array on server side
396        * which match the local workflow grid.
397        * outLocalIndexStoreOnClient_[client_rank] -> Array of index from client of rank "client_rank"
398        * outLocalIndexStoreOnClient_[client_rank](index of buffer from client) -> local index of the workflow grid
399        * The map is created in CGrid::computeClientIndex and filled upon receiving data in CField::recvUpdateData().
400        * Symetrically it is also used to send data from a server to several client for reading case. */
401        map<int, CArray<size_t, 1>> outLocalIndexStoreOnClient_; 
402      public:
403         void computeOutLocalIndexStoreOnClient(void) ;
404      private:
405         bool computeOutLocalIndexStoreOnClient_done_ = false ; 
406      public:   
407         map<int, CArray<size_t, 1>>& getOutLocalIndexStoreOnClient(void) 
408         { if (!computeOutLocalIndexStoreOnClient_done_) computeOutLocalIndexStoreOnClient(); return outLocalIndexStoreOnClient_ ; }
409
410      public:
411/** Indexes calculated based on server-like distribution.
412 *  They are used for writing/reading data and only calculated for server level that does the writing/reading.
413 *  Along with localIndexToWriteOnClient, these indexes are used to correctly place incoming data.
414 *  size of the array : numberWrittenIndexes_ : number of index written in a compressed way
415 *  localIndexToWriteOnServer_(compressed_written_index) : -> local uncompressed index that will be written in the file */
416         CArray<size_t,1> localIndexToWriteOnServer_;
417
418/** Indexes calculated based on client-like distribution.
419  * They are used for writing/reading data and only calculated for server level that does the writing/reading.
420  * Along with localIndexToWriteOnServer, these indexes are used to correctly place incoming data.
421  * size of the array : numberWrittenIndexes_
422  * localIndexToWriteOnClient_(compressed_written_index) -> local index of the workflow grid*/
423         CArray<size_t,1> localIndexToWriteOnClient_;
424
425      public: 
426        bool isDataDistributed(void) ; 
427      private:
428
429/** Clients that have to send a grid. There can be multiple clients in case of secondary server, otherwise only one client. */
430        std::list<CContextClient*> clients;
431        std::set<CContextClient*> clientsSet;
432
433      private: 
434        /** Map storing received indexes on server side sent by clients. Key = sender rank, value = global index array.
435            Later, the global indexes received will be mapped onto local index computed with the local distribution.
436            outGlobalIndexFromClient_[rank] -> array of global index send by client of rank "rank"
437            outGlobalIndexFromClient_[rank](n) -> global index of datav n sent by client
438        */     
439        map<int, CArray<size_t, 1> > outGlobalIndexFromClient_;
440      public: 
441        map<int, CArray<size_t, 1> >& getOutGlobalIndexFromClient() { return outGlobalIndexFromClient_ ;}
442
443      private:
444        bool isChecked;
445        bool isDomainAxisChecked;
446        bool isIndexSent;
447
448        CDomainGroup* vDomainGroup_;
449        CAxisGroup* vAxisGroup_;
450        CScalarGroup* vScalarGroup_;
451        std::vector<std::string> axisList_, domList_, scalarList_;
452        bool isAxisListSet, isDomListSet, isScalarListSet;
453
454        CClientServerMapping* clientServerMap_;
455        int numberWrittenIndexes_, totalNumberWrittenIndexes_, offsetWrittenIndexes_;
456
457/** Map storing local ranks of connected receivers. Key = size of receiver's intracomm.
458  * It is calculated in computeConnectedClients(). */
459        std::map<int, std::vector<int> > connectedServerRank_;
460
461/** Map storing the size of data to be send. Key = size of receiver's intracomm
462  * It is calculated in computeConnectedClients(). */
463        std::map<int, std::map<int,size_t> > connectedDataSize_;
464
465/** Ranks of connected receivers in case of reading. It is calculated in recvIndex(). */
466        std::vector<int> connectedServerRankRead_;
467
468/** Size of data to be send in case of reading. It is calculated in recvIndex(). */
469        std::map<int,size_t> connectedDataSizeRead_;
470     
471         //! True if and only if the data defined on the grid can be outputted in a compressed way
472        bool isCompressible_;
473        std::set<std::string> relFilesCompressed;
474
475        bool isTransformed_, isGenerated_;
476        bool computedWrittenIndex_;
477       
478        std::vector<int> axisPositionInGrid_;
479        void computeAxisPositionInGrid(void) ;
480        bool computeAxisPositionInGrid_done_ = false ;
481        std::vector<int>& getAxisPositionInGrid(void) { if (!computeAxisPositionInGrid_done_) computeAxisPositionInGrid() ; return axisPositionInGrid_ ;}
482
483        CGridTransformation* transformations_;
484        bool hasDomainAxisBaseRef_;       
485        std::map<CGrid*, std::pair<bool,StdString> > gridSrc_;
486        bool hasTransform_;
487
488/** Map storing global indexes of server-like (band-wise) distribution for sending to receivers (client side).
489  * Key = size of receiver's intracomm (i.e. number of servers)
490  * ~ map<int, umap<int, std::vector<size_t> >> globalIndexOnServer_
491  * globalIndexOnServer_[servers_size] -> map for a distribution of size "servers_size" (number of servers)
492  * globalIndexOnServer_[servers_size][server_rank] -> array of global index managed by server of rank "server_rank"
493  * globalIndexOnServer_[servers_size][server_rank][n] -> global index of data to be send to the server by client based on sub element of the grid.
494  * -> grid masking is not included.
495  */
496//        std::map<CContextClient*, CClientServerMapping::GlobalIndexMap> globalIndexOnServer_;
497        std::map<int, CClientServerMapping::GlobalIndexMap> globalIndexOnServer_;
498
499
500/** List order of axis and domain in a grid, if there is a domain, it will take value 2, axis 1, scalar 0 */
501        std::vector<int> order_;
502
503   }; // class CGrid
504
505   ///--------------------------------------------------------------
506
507   template <int n>
508   void CGrid::inputField(const CArray<double,n>& field, CArray<double,1>& stored)
509   TRY
510   {
511//#ifdef __XIOS_DEBUG
512      if (this->getDataSize() != field.numElements())
513         ERROR("void CGrid::inputField(const  CArray<double,n>& field, CArray<double,1>& stored) const",
514                << "[ Awaiting data of size = " << this->getDataSize() << ", "
515                << "Received data size = "      << field.numElements() << " ] "
516                << "The data array does not have the right size! "
517                << "Grid = " << this->getId())
518//#endif
519      this->storeField_arr(field.dataFirst(), stored);
520   }
521   CATCH
522
523   template <int n>
524   void CGrid::maskField(const CArray<double,n>& field, CArray<double,1>& stored)
525   {
526//#ifdef __XIOS_DEBUG
527      if (this->getDataSize() != field.numElements())
528         ERROR("void CGrid::inputField(const  CArray<double,n>& field, CArray<double,1>& stored) const",
529                << "[ Awaiting data of size = " << this->getDataSize() << ", "
530                << "Received data size = "      << field.numElements() << " ] "
531                << "The data array does not have the right size! "
532                << "Grid = " << this->getId())
533//#endif
534      this->maskField_arr(field.dataFirst(), stored);
535   }
536
537   template <int n>
538   void CGrid::outputField(const CArray<double,1>& stored, CArray<double,n>& field)
539   TRY
540   {
541//#ifdef __XIOS_DEBUG
542      if (this->getDataSize() != field.numElements())
543         ERROR("void CGrid::outputField(const CArray<double,1>& stored, CArray<double,n>& field) const",
544                << "[ Size of the data = " << this->getDataSize() << ", "
545                << "Output data size = "   << field.numElements() << " ] "
546                << "The ouput array does not have the right size! "
547                << "Grid = " << this->getId())
548//#endif
549      this->restoreField_arr(stored, field.dataFirst());
550   }
551   CATCH
552
553   /*!
554     This function removes the effect of mask on received data on the server.
555     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
556     \data data received data with masking effect on the server
557     \outData data without masking effect
558   */
559   template <int N>
560   void CGrid::uncompressField(const CArray<double,N>& data, CArray<double,1>& outData)
561   TRY
562   {     
563     uncompressField_arr(data.dataFirst(), outData);
564   }
565   CATCH
566
567   template<int N>
568   void CGrid::checkGridMask(CArray<bool,N>& gridMask,
569                             const std::vector<CArray<bool,1>* >& domainMasks,
570                             const std::vector<CArray<bool,1>* >& axisMasks,
571                             const CArray<int,1>& axisDomainOrder,
572                             bool createMask)
573   TRY
574   {
575     int idx = 0;
576     int numElement = axisDomainOrder.numElements();
577     int dim = domainMasks.size() * 2 + axisMasks.size();
578     std::vector<CDomain*> domainP = this->getDomains();
579     std::vector<CAxis*> axisP = this->getAxis();
580
581     std::vector<int> idxLoop(dim,0), indexMap(numElement), eachDimSize(dim);
582     std::vector<int> currentIndex(dim);
583     int idxDomain = 0, idxAxis = 0;
584    for (int i = 0; i < numElement; ++i)
585    {
586      indexMap[i] = idx;
587      if (2 == axisDomainOrder(i)) {
588          eachDimSize[indexMap[i]]   = domainP[idxDomain]->ni;
589          eachDimSize[indexMap[i]+1] = domainP[idxDomain]->nj;
590          idx += 2; ++idxDomain;
591      }
592      else if (1 == axisDomainOrder(i)) {
593//        eachDimSize[indexMap[i]] = axisMasks[idxAxis]->numElements();
594        eachDimSize[indexMap[i]] = axisP[idxAxis]->n;
595        ++idx; ++idxAxis;
596      }
597      else {};
598    }
599
600    if (!gridMask.isEmpty() && !createMask)
601    {
602      for (int i = 0; i < dim; ++i)
603      {
604        if (gridMask.extent(i) != eachDimSize[i])
605          ERROR("CGrid::checkMask(void)",
606                << "The mask has one dimension whose size is different from the one of the local grid." << std::endl
607                << "Local size of dimension " << i << " is " << eachDimSize[i] << "." << std::endl
608                << "Mask size for dimension " << i << " is " << gridMask.extent(i) << "." << std::endl
609                << "Grid = " << this->getId())
610      }
611    }
612    else {
613        CArrayBoolTraits<CArray<bool,N> >::resizeArray(gridMask,eachDimSize);
614        gridMask = true;
615    }
616
617    int ssize = gridMask.numElements();
618    idx = 0;
619    while (idx < ssize)
620    {
621      for (int i = 0; i < dim-1; ++i)
622      {
623        if (idxLoop[i] == eachDimSize[i])
624        {
625          idxLoop[i] = 0;
626          ++idxLoop[i+1];
627        }
628      }
629
630      // Find out outer index
631      idxDomain = idxAxis = 0;
632      bool maskValue = true;
633      for (int i = 0; i < numElement; ++i)
634      {
635        if (2 == axisDomainOrder(i))
636        {
637          int idxTmp = idxLoop[indexMap[i]] + idxLoop[indexMap[i]+1] * eachDimSize[indexMap[i]];
638          if (idxTmp < (*domainMasks[idxDomain]).numElements())
639            maskValue = maskValue && (*domainMasks[idxDomain])(idxTmp);
640          else
641            maskValue = false;
642          ++idxDomain;
643        }
644        else if (1 == axisDomainOrder(i))
645        {
646          int idxTmp = idxLoop[indexMap[i]];
647          if (idxTmp < (*axisMasks[idxAxis]).numElements())
648            maskValue = maskValue && (*axisMasks[idxAxis])(idxTmp);
649          else
650            maskValue = false;
651
652          ++idxAxis;
653        }
654      }
655
656      int maskIndex = idxLoop[0];
657      int mulDim = 1;
658      for (int k = 1; k < dim; ++k)
659      {
660        mulDim *= eachDimSize[k-1];
661        maskIndex += idxLoop[k]*mulDim;
662      }
663      *(gridMask.dataFirst()+maskIndex) &= maskValue;
664
665      ++idxLoop[0];
666      ++idx;
667    }
668   }
669   CATCH_DUMP_ATTR
670
671   template<int N>
672   void CGrid::modifyGridMaskSize(CArray<bool,N>& gridMask,
673                                  const std::vector<int>& eachDimSize,
674                                  bool newValue)
675   TRY
676   {
677      if (N != eachDimSize.size())
678      {
679        // ERROR("CGrid::modifyGridMaskSize(CArray<bool,N>& gridMask,
680        //                                  const std::vector<int>& eachDimSize,
681        //                                  bool newValue)",
682        //       << "Dimension size of the mask is different from input dimension size." << std::endl
683        //       << "Mask dimension is " << N << "." << std::endl
684        //       << "Input dimension is " << eachDimSize.size() << "." << std::endl
685        //       << "Grid = " << this->getId())
686      }
687      CArrayBoolTraits<CArray<bool,N> >::resizeArray(gridMask,eachDimSize);
688      gridMask = newValue;
689   }
690   CATCH_DUMP_ATTR
691                                 
692
693   /*!
694     Modify the current mask of grid, the local index to be modified will take value false
695     \param [in/out] gridMask current mask of grid
696     \param [in] indexToModify local index to modify
697   */
698   template<int N>
699   void CGrid::modifyGridMask(CArray<bool,N>& gridMask, const CArray<int,1>& indexToModify, bool valueToModify)
700   TRY
701   {     
702     int num = indexToModify.numElements();
703     for (int idx = 0; idx < num; ++idx)
704     {
705       *(gridMask.dataFirst()+indexToModify(idx)) = valueToModify;
706     }
707   }
708   CATCH_DUMP_ATTR
709
710   ///--------------------------------------------------------------
711
712
713
714   // Declare/Define CGridGroup and CGridDefinition
715   DECLARE_GROUP(CGrid);
716
717   ///--------------------------------------------------------------
718
719} // namespace xios
720
721#endif // __XIOS_CGrid__
Note: See TracBrowser for help on using the repository browser.