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

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

XIOS coupling branch
Some updates.

First coupling test is beginning to work...

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.7 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 checkAxisDomainsGridsEligibilityForCompressedOutput();
143         void prepareTimeseries(void);
144         void solveOnlyRefOfEnabledFields(void);         
145         void buildFilterGraphOfEnabledFields();
146         void postProcessFilterGraph();
147         void startPrefetchingOfEnabledReadModeFiles();
148         void doPreTimestepOperationsForEnabledReadModeFiles();
149         void doPostTimestepOperationsForEnabledReadModeFiles();
150         void findFieldsWithReadAccess(void);
151         void solveAllRefOfFieldsWithReadAccess();
152         void buildFilterGraphOfFieldsWithReadAccess();
153         void postProcessing();
154         void postProcessingGlobalAttributes();         
155         void triggerLateFields(void) ;
156         void solveAllRefOfEnabledFieldsAndTransform(void);
157         void checkGridEnabledFields();
158         void checkGridEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles);
159         void sendGridEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles); 
160         void sendGridComponentEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles)       ;
161
162         std::map<int, StdSize> getAttributesBufferSize(std::map<int, StdSize>& maxEventSize, CContextClient* contextClient, bool bufferForWriting = false);
163         std::map<int, StdSize> getDataBufferSize(std::map<int, StdSize>& maxEventSize, CContextClient* contextClient, bool bufferForWriting = false);
164         void setClientServerBuffer(CContextClient* contextClient, bool bufferForWriting = false); // old interface to be removed
165         void setClientServerBuffer(vector<CField*>& fields, bool bufferForWriting) ; 
166
167         // Distribute files (in write mode) among secondary-server pools according to the estimated data flux
168         void distributeFiles(const std::vector<CFile*>& files);
169         void distributeFileOverBandwith(const std::vector<CFile*>& files) ;
170         void distributeFileOverMemoryBandwith(const std::vector<CFile*>& files) ;
171         
172         // Send context close definition
173         void sendCloseDefinition(void);
174       public:
175         void sendCloseDefinition(CContextClient* client) ;
176       private:
177         set<CContextClient*> sendCloseDefinition_done_ ;
178       public:
179         // There are something to send on closing context defintion
180         void sendUpdateCalendar(int step);
181         void sendCreateFileHeader(void);
182         void sendEnabledFiles(const std::vector<CFile*>& activeFiles);
183         void sendEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles);
184         void sendRefDomainsAxisScalars(const std::vector<CFile*>& activeFiles);
185         void sendRefGrid(const std::vector<CFile*>& activeFiles);
186         void sendPostProcessing();
187         void sendPostProcessingGlobalAttributes();
188         void sendProcessingGridOfEnabledFields();
189         //!< after be gathered to the root process of the context, merged registry is sent to the root process of the servers
190         void sendRegistry(void) ;
191         void sendFinalizeClient(CContextClient* contextClient, const string& contextClientId);
192         
193         public:
194         void sendContextToFileServer(CContextClient* client) ;
195         private:
196         std::set<CContextClient*> sendToFileServer_done_ ;
197         
198         public: 
199         std::string getContextId() {return contextId_;}
200
201         // Client side: Receive and process messages
202         static void recvUpdateCalendar(CEventServer& event);
203         void recvUpdateCalendar(CBufferIn& buffer);
204         static void recvCloseDefinition(CEventServer& event);
205         static void recvCreateFileHeader(CEventServer& event);
206         void recvCreateFileHeader(CBufferIn& buffer);
207         static void recvSolveInheritanceContext(CEventServer& event);
208         void recvSolveInheritanceContext(CBufferIn& buffer);
209         static void recvPostProcessing(CEventServer& event);
210         void recvPostProcessing(CBufferIn& buffer);
211         static void recvProcessingGridOfEnabledFields(CEventServer& event);
212         static void recvPostProcessingGlobalAttributes(CEventServer& event);
213         void recvPostProcessingGlobalAttributes(CBufferIn& buffer);
214         static void recvRegistry(CEventServer& event) ;
215         void recvRegistry(CBufferIn& buffer) ; //!< registry is received by the servers
216         static void recvFinalizeClient(CEventServer& event) ;
217         void recvFinalizeClient(CBufferIn& buffer);
218       
219       public:
220         void sendCouplerInReady(CContextClient* client);
221       private:
222         set<CContextClient*> sendCouplerInReady_done_;
223       public:
224         static void recvCouplerInReady(CEventServer& event) ;
225         void recvCouplerInReady(CBufferIn& buffer) ; //!< coupler is ready to receive grid definition.
226         set<CContextClient*> couplerInReady_;
227         bool isCouplerInReady(CContextClient* client) { return couplerInReady_.count(client)!=0 ;}
228
229       public:
230        void sendCouplerInCloseDefinition(CContextClient* client) ;
231        set<CContextClient*> sendCouplerInCloseDefinition_done_;
232        static void recvCouplerInCloseDefinition(CEventServer& event) ;
233        void recvCouplerInCloseDefinition(CBufferIn& buffer) ; //!< coupler has finished it defintion, data can be sent     
234        set<CContextClient*> couplerInCloseDefinition_ ;
235        bool isCouplerInCloseDefinition(CContextClient* client) { return couplerInCloseDefinition_.count(client)!=0 ;}
236
237       public:
238        void sendCouplerInContextFinalized(CContextClient* client) ;
239        set<CContextClient*> sendCouplerInContextFinalized_done_;
240        static void recvCouplerInContextFinalized(CEventServer& event) ;
241        void recvCouplerInContextFinalized(CBufferIn& buffer) ; //!< coupler has finished it defintion, data can be sent     
242        set<CContextClient*> couplerInContextFinalized_ ;
243        bool isCouplerInContextFinalized(CContextClient* client) { return couplerInContextFinalized_.count(client)!=0 ;}
244
245       public: 
246        void freeComms(void);                  //!< Free internally allcoated communicators
247        void releaseClientBuffers(void);       //! Deallocate buffers allocated by clientContexts
248
249         // dispatch event
250         static bool dispatchEvent(CEventServer& event);
251
252      public:
253        // Get current context
254        static CContext* getCurrent(void);
255
256        // Get context root
257        static CContextGroup* getRoot(void);
258
259        // Set current context
260        static void setCurrent(const string& id);
261
262        // Create new context
263        static CContext* create(const string& id = "");
264
265        /// Accesseurs statiques ///
266        static StdString GetName(void);
267        static StdString GetDefName(void);
268        static ENodeType GetType(void);
269
270        static CContextGroup* GetContextGroup(void);
271
272        // Some functions to visualize structure of current context
273        static void ShowTree(StdOStream & out = std::clog);
274        static void CleanTree(void);
275        int getServiceType(void) {return serviceType_;}
276
277      public :
278         // Parse xml node and write all info into context
279         virtual void parse(xml::CXMLNode & node);
280
281         // Visualize a context
282         virtual StdString toString(void) const;
283
284
285         // Solve all inheritance relation in current context
286         virtual void solveDescInheritance(bool apply, const CAttributeMap * const parent = 0);
287
288         // Verify if all root definition in a context have children
289         virtual bool hasChild(void) const;
290
291         bool isProcessingEvent(void) {return isProcessingEvent_;}
292         bool setProcessingEvent(void) {isProcessingEvent_=true ;}
293         bool unsetProcessingEvent(void) {isProcessingEvent_=false ;}
294         MPI_Comm getIntraComm(void) {return intraComm_ ;}
295         int getIntraCommRank(void) {return intraCommRank_;}
296         int getIntraCommSize(void) {return intraCommSize_;}
297
298         void addCouplingChanel(const std::string& contextId, bool out) ;
299
300      public :
301         // Calendar of context
302         std::shared_ptr<CCalendar>   calendar;
303
304         // List of all enabled files (files on which fields are written or read)
305         std::vector<CFile*> enabledFiles;
306         // List of all enabled files in read mode (files on which fields are read)
307         std::vector<CFile*> enabledReadModeFiles;
308         // List of all enabled files in write mode
309         std::vector<CFile*> enabledWriteModeFiles;
310
311         std::vector<CCouplerIn*> enabledCouplerIn;
312         std::vector<CCouplerOut*> enabledCouplerOut;
313
314
315         // List of all enabled fields whose instant data is accessible from the public API
316         // but which are not part of a file
317         std::vector<CField*> fieldsWithReadAccess_;
318         std::vector<CField*> couplerInFields_;
319         std::vector<CField*> fileInFields_;
320
321
322         // Context root
323         static std::shared_ptr<CContextGroup> root;
324
325         // Determine context on client or not
326         bool hasClient;
327
328         // Determine context on server or not
329         bool hasServer;
330
331         CContextServer* server;    //!< Concrete context server
332         CContextClient* client;    //!< Concrete contex client
333         std::vector<CContextServer*> serverPrimServer;
334         std::vector<CContextClient*> clientPrimServer;
335
336         // list of slave servers (IO server or others)
337         set<CContextClient*> slaveServers_ ;
338      private:
339         // the map containing context client associated to it string id for coupling out ;
340         std::map<std::string, CContextClient*> couplerOutClient_ ;
341         // the map containing context server associated to it string id for coupling out ;
342         std::map<std::string, CContextServer*> couplerOutServer_ ;
343         // the map containing context client associated to it string id for coupling in ;
344         std::map<std::string, CContextClient*> couplerInClient_ ;
345         // the map containing context server associated to it string id for coupling in ;
346         std::map<std::string, CContextServer*> couplerInServer_ ;
347      public:
348         CContextClient* getCouplerInClient(const string& contextId) { return couplerInClient_[contextId] ;}
349         CContextServer* getCouplerInServer(const string& contextId) { return couplerInServer_[contextId] ;}
350         CContextClient* getCouplerOutClient(const string& contextId) { return couplerOutClient_[contextId] ;}
351         CContextServer* getCouplerOutServer(const string& contextId) { return couplerOutServer_[contextId] ;}
352     
353 
354         std::vector<std::string> primServerId_;
355
356         CRegistry* registryIn ;    //!< input registry which is read from file
357         CRegistry* registryOut ;   //!< output registry which will be written into file at the finalize
358
359
360        MPI_Comm intraComm_ ; //! context intra communicator
361        int intraCommRank_ ; //! context intra communicator rank
362        int intraCommSize_ ; //! context intra communicator size
363       
364      private:
365         bool isPostProcessed;
366         bool allProcessed;
367         bool finalized;
368         int countChildContextFinalized_;        //!< Counter of child contexts (for now it is the number of secondary server pools)
369         CGarbageCollector garbageCollector;
370         std::list<MPI_Comm> comms; //!< Communicators allocated internally
371
372         int serviceType_;  //!< service associated to the context
373         string contextId_ ; //!< context client id for the servers. For clients this is same as getId()
374         bool isProcessingEvent_ ;
375         CServerContext* parentServerContext_ ;
376
377      public: // Some function maybe removed in the near future
378        // virtual void toBinary  (StdOStream & os) const;
379        // virtual void fromBinary(StdIStream & is);
380   }; // class CContext
381
382   ///--------------------------------------------------------------
383
384   // Declare/Define CContextGroup and CContextDefinition
385   DECLARE_GROUP(CContext);
386
387   ///--------------------------------------------------------------
388
389} // namespace xios
390
391#endif // __XIOS_CContext__
Note: See TracBrowser for help on using the repository browser.