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

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

Big update on on going work related to data distribution and transfer between clients and servers.

  • move all related file into distribution directorie
  • implement the concept of data "View"
  • implement the concept of "connector" which make the data transfer between 2 differents "Views"

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