source: XIOS/dev/dev_ym/XIOS_COUPLING/src/node/context.hpp @ 1960

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

Xios coupling

  • fix problem when sending grid mask from client to server
  • remove methods about grid compression, which will be managed in other ways

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
File size: 16.6 KB
Line 
1#ifndef __XIOS_CContext__
2#define __XIOS_CContext__
3
4/// XIOS headers ///
5#include "xios_spl.hpp"
6//#include "node_type.hpp"
7#include "calendar_wrapper.hpp"
8
9#include "declare_group.hpp"
10#include "data_output.hpp"
11#include "garbage_collector.hpp"
12#include "registry.hpp"
13#include "mpi.hpp"
14#include "services_manager.hpp"
15#include "server_context.hpp"
16
17
18namespace xios {
19   class CContextClient;
20   class CContextServer;
21
22
23   /// ////////////////////// Déclarations ////////////////////// ///
24   class CContextGroup;
25   class CContextAttributes;
26   class CContext;
27   class CFile;
28   class CCouplerIn ;
29   class CCouplerOut ;
30   ///--------------------------------------------------------------
31
32   // Declare/Define CFileAttribute
33   BEGIN_DECLARE_ATTRIBUTE_MAP(CContext)
34#  include "context_attribute.conf"
35   END_DECLARE_ATTRIBUTE_MAP(CContext)
36
37   ///--------------------------------------------------------------
38  /*!
39  \class CContext
40   This class corresponds to the concrete presentation of context in xml file and in play an essential role in XIOS
41   Each object of this class contains all root definition of elements: files, fiels, domains, axis, etc, ... from which
42   we can have access to each element.
43   In fact, every thing must a be inside a particuliar context. After the xml file (iodef.xml) is parsed,
44   object of the class is created and its contains all information of other elements in the xml file.
45  */
46   class CContext
47      : public CObjectTemplate<CContext>
48      , public CContextAttributes
49   {
50         public :
51         enum EEventId
52         {
53           EVENT_ID_CLOSE_DEFINITION,EVENT_ID_UPDATE_CALENDAR,
54           EVENT_ID_CREATE_FILE_HEADER,EVENT_ID_CONTEXT_FINALIZE,
55           EVENT_ID_POST_PROCESS, EVENT_ID_SEND_REGISTRY,
56           EVENT_ID_POST_PROCESS_GLOBAL_ATTRIBUTES,
57           EVENT_ID_PROCESS_GRID_ENABLED_FIELDS,
58           EVENT_ID_CONTEXT_FINALIZE_CLIENT,
59           EVENT_ID_COUPLER_IN_READY,
60           EVENT_ID_COUPLER_IN_CLOSE_DEFINITION,
61           EVENT_ID_COUPLER_IN_CONTEXT_FINALIZED,
62         };
63
64         /// typedef ///
65         typedef CObjectTemplate<CContext>   SuperClass;
66         typedef CContextAttributes SuperClassAttribute;
67
68      public :
69
70         typedef CContextAttributes RelAttributes;
71         typedef CContext           RelGroup;
72
73         //---------------------------------------------------------
74
75      public :
76
77         /// Constructeurs ///
78         CContext(void);
79         explicit CContext(const StdString & id);
80         CContext(const CContext & context);       // Not implemented yet.
81         CContext(const CContext * const context); // Not implemented yet.
82
83         /// Destructeur ///
84         virtual ~CContext(void);
85
86         //---------------------------------------------------------
87
88      public :
89
90         /// Mutateurs ///
91         void setCalendar(std::shared_ptr<CCalendar> newCalendar);
92
93         /// Accesseurs ///
94         std::shared_ptr<CCalendar>      getCalendar(void) const;
95
96      public :
97         // Initialize server or client
98         void init(CServerContext* parentServerContext, MPI_Comm intraComm, int serviceType);
99         void initClient(MPI_Comm intraComm, int serviceType);
100         
101         void initServer(MPI_Comm intraComm, int serviceType );
102         void createClientInterComm(MPI_Comm interCommClient, MPI_Comm interCommServer)  ;
103         void createServerInterComm(void)  ;
104
105         bool isInitialized(void);
106
107         StdString dumpClassAttributes(void);
108
109         // Put sever or client into loop state
110         bool eventLoop(bool enableEventsProcessing=true);
111         void globalEventLoop(void);
112
113         // Finalize a context
114         void finalize(void);
115
116         bool isFinalized(void);
117
118         void closeDefinition(void);
119         void closeDefinition_old(void);
120
121         // to be removed     
122         std::vector<CField*> findAllEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles);
123         // Some functions to process context
124         std::vector<CField*> findAllEnabledFieldsInFileOut(const std::vector<CFile*>& activeFiles);
125         std::vector<CField*> findAllEnabledFieldsInFileIn(const std::vector<CFile*>& activeFiles);
126         std::vector<CField*> findAllEnabledFieldsCouplerOut(const std::vector<CCouplerOut*>& activeCouplerOut);
127         std::vector<CField*> findAllEnabledFieldsCouplerIn(const std::vector<CCouplerIn*>& activeCouplerIn);
128         // void findAllEnabledFields(void);
129         // void findAllEnabledFieldsInReadModeFiles(void);
130         void readAttributesOfEnabledFieldsInReadModeFiles();
131         void solveAllInheritance(bool apply=true);
132         void findEnabledFiles(void);
133         void findEnabledCouplerIn(void);
134         void findEnabledCouplerOut(void);
135         void createCouplerInterCommunicator(void) ;
136         void findEnabledWriteModeFiles(void);
137         void findEnabledReadModeFiles(void);
138         void closeAllFile(void);
139         void updateCalendar(int step);
140         void createFileHeader(void);
141         void initReadFiles(void);
142        void prepareTimeseries(void);
143         void solveOnlyRefOfEnabledFields(void);         
144         void buildFilterGraphOfEnabledFields();
145         void postProcessFilterGraph();
146         void startPrefetchingOfEnabledReadModeFiles();
147         void doPreTimestepOperationsForEnabledReadModeFiles();
148         void doPostTimestepOperationsForEnabledReadModeFiles();
149         void findFieldsWithReadAccess(void);
150         void solveAllRefOfFieldsWithReadAccess();
151         void buildFilterGraphOfFieldsWithReadAccess();
152         void postProcessing();
153         void postProcessingGlobalAttributes();         
154         void triggerLateFields(void) ;
155         void solveAllRefOfEnabledFieldsAndTransform(void);
156         void checkGridEnabledFields();
157         void checkGridEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles);
158         void sendGridEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles); 
159         void sendGridComponentEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles)       ;
160
161         std::map<int, StdSize> getAttributesBufferSize(std::map<int, StdSize>& maxEventSize, CContextClient* contextClient, bool bufferForWriting = false);
162         std::map<int, StdSize> getDataBufferSize(std::map<int, StdSize>& maxEventSize, CContextClient* contextClient, bool bufferForWriting = false);
163         void setClientServerBuffer(CContextClient* contextClient, bool bufferForWriting = false); // old interface to be removed
164         void setClientServerBuffer(vector<CField*>& fields, bool bufferForWriting) ; 
165
166         // Distribute files (in write mode) among secondary-server pools according to the estimated data flux
167         void distributeFiles(const std::vector<CFile*>& files);
168         void distributeFileOverBandwith(const std::vector<CFile*>& files) ;
169         void distributeFileOverMemoryBandwith(const std::vector<CFile*>& files) ;
170         
171         // Send context close definition
172         void sendCloseDefinition(void);
173       public:
174         void sendCloseDefinition(CContextClient* client) ;
175       private:
176         set<CContextClient*> sendCloseDefinition_done_ ;
177       public:
178         // There are something to send on closing context defintion
179         void sendUpdateCalendar(int step);
180         void sendCreateFileHeader(void);
181         void sendEnabledFiles(const std::vector<CFile*>& activeFiles);
182         void sendEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles);
183         void sendRefDomainsAxisScalars(const std::vector<CFile*>& activeFiles);
184         void sendRefGrid(const std::vector<CFile*>& activeFiles);
185         void sendPostProcessing();
186         void sendPostProcessingGlobalAttributes();
187         void sendProcessingGridOfEnabledFields();
188         //!< after be gathered to the root process of the context, merged registry is sent to the root process of the servers
189         void sendRegistry(void) ;
190         void sendFinalizeClient(CContextClient* contextClient, const string& contextClientId);
191         
192         public:
193         void sendContextToFileServer(CContextClient* client) ;
194         private:
195         std::set<CContextClient*> sendToFileServer_done_ ;
196         
197         public: 
198         std::string getContextId() {return contextId_;}
199
200         // Client side: Receive and process messages
201         static void recvUpdateCalendar(CEventServer& event);
202         void recvUpdateCalendar(CBufferIn& buffer);
203         static void recvCloseDefinition(CEventServer& event);
204         static void recvCreateFileHeader(CEventServer& event);
205         void recvCreateFileHeader(CBufferIn& buffer);
206         static void recvSolveInheritanceContext(CEventServer& event);
207         void recvSolveInheritanceContext(CBufferIn& buffer);
208         static void recvPostProcessing(CEventServer& event);
209         void recvPostProcessing(CBufferIn& buffer);
210         static void recvProcessingGridOfEnabledFields(CEventServer& event);
211         static void recvPostProcessingGlobalAttributes(CEventServer& event);
212         void recvPostProcessingGlobalAttributes(CBufferIn& buffer);
213         static void recvRegistry(CEventServer& event) ;
214         void recvRegistry(CBufferIn& buffer) ; //!< registry is received by the servers
215         static void recvFinalizeClient(CEventServer& event) ;
216         void recvFinalizeClient(CBufferIn& buffer);
217       
218       public:
219         void sendCouplerInReady(CContextClient* client);
220       private:
221         set<CContextClient*> sendCouplerInReady_done_;
222       public:
223         static void recvCouplerInReady(CEventServer& event) ;
224         void recvCouplerInReady(CBufferIn& buffer) ; //!< coupler is ready to receive grid definition.
225         set<CContextClient*> couplerInReady_;
226         bool isCouplerInReady(CContextClient* client) { return couplerInReady_.count(client)!=0 ;}
227
228       public:
229        void sendCouplerInCloseDefinition(CContextClient* client) ;
230        set<CContextClient*> sendCouplerInCloseDefinition_done_;
231        static void recvCouplerInCloseDefinition(CEventServer& event) ;
232        void recvCouplerInCloseDefinition(CBufferIn& buffer) ; //!< coupler has finished it defintion, data can be sent     
233        set<CContextClient*> couplerInCloseDefinition_ ;
234        bool isCouplerInCloseDefinition(CContextClient* client) { return couplerInCloseDefinition_.count(client)!=0 ;}
235
236       public:
237        void sendCouplerInContextFinalized(CContextClient* client) ;
238        set<CContextClient*> sendCouplerInContextFinalized_done_;
239        static void recvCouplerInContextFinalized(CEventServer& event) ;
240        void recvCouplerInContextFinalized(CBufferIn& buffer) ; //!< coupler has finished it defintion, data can be sent     
241        set<CContextClient*> couplerInContextFinalized_ ;
242        bool isCouplerInContextFinalized(CContextClient* client) { return couplerInContextFinalized_.count(client)!=0 ;}
243
244       public: 
245        void freeComms(void);                  //!< Free internally allcoated communicators
246        void releaseClientBuffers(void);       //! Deallocate buffers allocated by clientContexts
247
248         // dispatch event
249         static bool dispatchEvent(CEventServer& event);
250
251      public:
252        // Get current context
253        static CContext* getCurrent(void);
254
255        // Get context root
256        static CContextGroup* getRoot(void);
257
258        // Set current context
259        static void setCurrent(const string& id);
260
261        // Create new context
262        static CContext* create(const string& id = "");
263
264        /// Accesseurs statiques ///
265        static StdString GetName(void);
266        static StdString GetDefName(void);
267        static ENodeType GetType(void);
268
269        static CContextGroup* GetContextGroup(void);
270
271        // Some functions to visualize structure of current context
272        static void ShowTree(StdOStream & out = std::clog);
273        static void CleanTree(void);
274        int getServiceType(void) {return serviceType_;}
275
276      public :
277         // Parse xml node and write all info into context
278         virtual void parse(xml::CXMLNode & node);
279
280         // Visualize a context
281         virtual StdString toString(void) const;
282
283
284         // Solve all inheritance relation in current context
285         virtual void solveDescInheritance(bool apply, const CAttributeMap * const parent = 0);
286
287         // Verify if all root definition in a context have children
288         virtual bool hasChild(void) const;
289
290         bool isProcessingEvent(void) {return isProcessingEvent_;}
291         bool setProcessingEvent(void) {isProcessingEvent_=true ;}
292         bool unsetProcessingEvent(void) {isProcessingEvent_=false ;}
293         MPI_Comm getIntraComm(void) {return intraComm_ ;}
294         int getIntraCommRank(void) {return intraCommRank_;}
295         int getIntraCommSize(void) {return intraCommSize_;}
296
297         void addCouplingChanel(const std::string& contextId, bool out) ;
298
299      public :
300         // Calendar of context
301         std::shared_ptr<CCalendar>   calendar;
302
303         // List of all enabled files (files on which fields are written or read)
304         std::vector<CFile*> enabledFiles;
305         // List of all enabled files in read mode (files on which fields are read)
306         std::vector<CFile*> enabledReadModeFiles;
307         // List of all enabled files in write mode
308         std::vector<CFile*> enabledWriteModeFiles;
309
310         std::vector<CCouplerIn*> enabledCouplerIn;
311         std::vector<CCouplerOut*> enabledCouplerOut;
312
313
314         // List of all enabled fields whose instant data is accessible from the public API
315         // but which are not part of a file
316         std::vector<CField*> fieldsWithReadAccess_;
317         std::vector<CField*> couplerInFields_;
318         std::vector<CField*> fileInFields_;
319
320
321         // Context root
322         static std::shared_ptr<CContextGroup> root;
323
324         // Determine context on client or not
325         bool hasClient;
326
327         // Determine context on server or not
328         bool hasServer;
329
330         CContextServer* server;    //!< Concrete context server
331         CContextClient* client;    //!< Concrete contex client
332         std::vector<CContextServer*> serverPrimServer;
333         std::vector<CContextClient*> clientPrimServer;
334
335         // list of slave servers (IO server or others)
336         set<CContextClient*> slaveServers_ ;
337      private:
338         // the map containing context client associated to it string id for coupling out ;
339         std::map<std::string, CContextClient*> couplerOutClient_ ;
340         // the map containing context server associated to it string id for coupling out ;
341         std::map<std::string, CContextServer*> couplerOutServer_ ;
342         // the map containing context client associated to it string id for coupling in ;
343         std::map<std::string, CContextClient*> couplerInClient_ ;
344         // the map containing context server associated to it string id for coupling in ;
345         std::map<std::string, CContextServer*> couplerInServer_ ;
346      public:
347         CContextClient* getCouplerInClient(const string& contextId) { return couplerInClient_[contextId] ;}
348         CContextServer* getCouplerInServer(const string& contextId) { return couplerInServer_[contextId] ;}
349         CContextClient* getCouplerOutClient(const string& contextId) { return couplerOutClient_[contextId] ;}
350         CContextServer* getCouplerOutServer(const string& contextId) { return couplerOutServer_[contextId] ;}
351     
352 
353         std::vector<std::string> primServerId_;
354
355         CRegistry* registryIn ;    //!< input registry which is read from file
356         CRegistry* registryOut ;   //!< output registry which will be written into file at the finalize
357
358
359        MPI_Comm intraComm_ ; //! context intra communicator
360        int intraCommRank_ ; //! context intra communicator rank
361        int intraCommSize_ ; //! context intra communicator size
362       
363      private:
364         bool isPostProcessed;
365         bool allProcessed;
366         bool finalized;
367         int countChildContextFinalized_;        //!< Counter of child contexts (for now it is the number of secondary server pools)
368         CGarbageCollector garbageCollector;
369         std::list<MPI_Comm> comms; //!< Communicators allocated internally
370
371         int serviceType_;  //!< service associated to the context
372         string contextId_ ; //!< context client id for the servers. For clients this is same as getId()
373         bool isProcessingEvent_ ;
374         CServerContext* parentServerContext_ ;
375
376      public: // Some function maybe removed in the near future
377        // virtual void toBinary  (StdOStream & os) const;
378        // virtual void fromBinary(StdIStream & is);
379   }; // class CContext
380
381   ///--------------------------------------------------------------
382
383   // Declare/Define CContextGroup and CContextDefinition
384   DECLARE_GROUP(CContext);
385
386   ///--------------------------------------------------------------
387
388} // namespace xios
389
390#endif // __XIOS_CContext__
Note: See TracBrowser for help on using the repository browser.