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

Last change on this file since 2230 was 2209, checked in by ymipsl, 3 years ago

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