source: XIOS/dev/dev_trunk_graph/src/node/context.hpp @ 2019

Last change on this file since 2019 was 2019, checked in by yushan, 9 months ago

Graph intermedia commit to a tmp branch

  • 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.3 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_SEND_REGISTRY,
56           EVENT_ID_CONTEXT_FINALIZE_CLIENT,
57           EVENT_ID_COUPLER_IN_READY,
58           EVENT_ID_COUPLER_IN_CLOSE_DEFINITION,
59           EVENT_ID_COUPLER_IN_CONTEXT_FINALIZED,
60         };
61
62         /// typedef ///
63         typedef CObjectTemplate<CContext>   SuperClass;
64         typedef CContextAttributes SuperClassAttribute;
65
66      public :
67
68         typedef CContextAttributes RelAttributes;
69         typedef CContext           RelGroup;
70
71         //---------------------------------------------------------
72
73      public :
74
75         /// Constructeurs ///
76         CContext(void);
77         explicit CContext(const StdString & id);
78         CContext(const CContext & context);       // Not implemented yet.
79         CContext(const CContext * const context); // Not implemented yet.
80
81         /// Destructeur ///
82         virtual ~CContext(void);
83
84         //---------------------------------------------------------
85
86      public :
87
88         /// Mutateurs ///
89         void setCalendar(std::shared_ptr<CCalendar> newCalendar);
90
91         /// Accesseurs ///
92         std::shared_ptr<CCalendar>      getCalendar(void) const;
93
94      public :
95         // Initialize server or client
96         void init(CServerContext* parentServerContext, MPI_Comm intraComm, int serviceType);
97         void initClient(MPI_Comm intraComm, int serviceType);
98         
99         void initServer(MPI_Comm intraComm, int serviceType );
100         void createClientInterComm(MPI_Comm interCommClient, MPI_Comm interCommServer)  ;
101         void createServerInterComm(void)  ;
102
103         bool isInitialized(void);
104
105         StdString dumpClassAttributes(void);
106
107         // Put sever or client into loop state
108         bool eventLoop(bool enableEventsProcessing=true);
109         void globalEventLoop(void);
110
111         // Finalize a context
112         void finalize(void);
113
114         bool isFinalized(void);
115
116         void closeDefinition(void);
117
118         // to be removed     
119         std::vector<CField*> findAllEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles);
120         // Some functions to process context
121         std::vector<CField*> findAllEnabledFieldsInFileOut(const std::vector<CFile*>& activeFiles);
122         std::vector<CField*> findAllEnabledFieldsInFileIn(const std::vector<CFile*>& activeFiles);
123         std::vector<CField*> findAllEnabledFieldsCouplerOut(const std::vector<CCouplerOut*>& activeCouplerOut);
124         std::vector<CField*> findAllEnabledFieldsCouplerIn(const std::vector<CCouplerIn*>& activeCouplerIn);
125         // void findAllEnabledFields(void);
126         // void findAllEnabledFieldsInReadModeFiles(void);
127         void readAttributesOfEnabledFieldsInReadModeFiles();
128         void solveAllInheritance(bool apply=true);
129         void findEnabledFiles(void);
130         void findEnabledCouplerIn(void);
131         void findEnabledCouplerOut(void);
132         void createCouplerInterCommunicator(void) ;
133         void findEnabledWriteModeFiles(void);
134         void findEnabledReadModeFiles(void);
135         void closeAllFile(void);
136         void updateCalendar(int step);
137         void createFileHeader(void);
138         void initReadFiles(void);
139         void prepareTimeseries(void);
140         void postProcessFilterGraph();
141         void startPrefetchingOfEnabledReadModeFiles();
142         void doPreTimestepOperationsForEnabledReadModeFiles();
143         void doPostTimestepOperationsForEnabledReadModeFiles();
144         void findFieldsWithReadAccess(void);
145         void triggerLateFields(void) ;
146         
147         std::map<int, StdSize> getAttributesBufferSize(std::map<int, StdSize>& maxEventSize, CContextClient* contextClient, bool bufferForWriting = false);
148         std::map<int, StdSize> getDataBufferSize(std::map<int, StdSize>& maxEventSize, CContextClient* contextClient, bool bufferForWriting = false);
149         void setClientServerBuffer(CContextClient* contextClient, bool bufferForWriting = false); // old interface to be removed
150         void setClientServerBuffer(vector<CField*>& fields, bool bufferForWriting) ; 
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 sendRegistry(void) ;
172         void sendFinalizeClient(CContextClient* contextClient, const string& contextClientId);
173         
174         public:
175         void sendContextToFileServer(CContextClient* client) ;
176         private:
177         std::set<CContextClient*> sendToFileServer_done_ ;
178         
179         public: 
180         std::string getContextId() {return contextId_;}
181
182         // Client side: Receive and process messages
183         static void recvUpdateCalendar(CEventServer& event);
184         void recvUpdateCalendar(CBufferIn& buffer);
185         static void recvCloseDefinition(CEventServer& event);
186         static void recvCreateFileHeader(CEventServer& event);
187         void recvCreateFileHeader(CBufferIn& buffer);
188         static void recvSolveInheritanceContext(CEventServer& event);
189         void recvSolveInheritanceContext(CBufferIn& buffer);
190         static void recvRegistry(CEventServer& event) ;
191         void recvRegistry(CBufferIn& buffer) ; //!< registry is received by the servers
192         static void recvFinalizeClient(CEventServer& event) ;
193         void recvFinalizeClient(CBufferIn& buffer);
194       
195       public:
196         void sendCouplerInReady(CContextClient* client);
197       private:
198         set<CContextClient*> sendCouplerInReady_done_;
199       public:
200         static void recvCouplerInReady(CEventServer& event) ;
201         void recvCouplerInReady(CBufferIn& buffer) ; //!< coupler is ready to receive grid definition.
202         set<CContextClient*> couplerInReady_;
203         bool isCouplerInReady(CContextClient* client) { return couplerInReady_.count(client)!=0 ;}
204
205       public:
206        void sendCouplerInCloseDefinition(CContextClient* client) ;
207        set<CContextClient*> sendCouplerInCloseDefinition_done_;
208        static void recvCouplerInCloseDefinition(CEventServer& event) ;
209        void recvCouplerInCloseDefinition(CBufferIn& buffer) ; //!< coupler has finished it defintion, data can be sent     
210        set<CContextClient*> couplerInCloseDefinition_ ;
211        bool isCouplerInCloseDefinition(CContextClient* client) { return couplerInCloseDefinition_.count(client)!=0 ;}
212
213       public:
214        void sendCouplerInContextFinalized(CContextClient* client) ;
215        set<CContextClient*> sendCouplerInContextFinalized_done_;
216        static void recvCouplerInContextFinalized(CEventServer& event) ;
217        void recvCouplerInContextFinalized(CBufferIn& buffer) ; //!< coupler has finished it defintion, data can be sent     
218        set<CContextClient*> couplerInContextFinalized_ ;
219        bool isCouplerInContextFinalized(CContextClient* client) { return couplerInContextFinalized_.count(client)!=0 ;}
220
221       public: 
222        void freeComms(void);                  //!< Free internally allcoated communicators
223        void releaseClientBuffers(void);       //! Deallocate buffers allocated by clientContexts
224
225         // dispatch event
226         static bool dispatchEvent(CEventServer& event);
227
228      public:
229        // Get current context
230        static CContext* getCurrent(void);
231
232        // Get context root
233        static CContextGroup* getRoot(void);
234
235        // Set current context
236        static void setCurrent(const string& id);
237
238        // Create new context
239        static CContext* create(const string& id = "");
240
241        /// Accesseurs statiques ///
242        static StdString GetName(void);
243        static StdString GetDefName(void);
244        static ENodeType GetType(void);
245
246        static CContextGroup* GetContextGroup(void);
247
248        // Some functions to visualize structure of current context
249        static void ShowTree(StdOStream & out = std::clog);
250        static void CleanTree(void);
251        int getServiceType(void) {return serviceType_;}
252
253      public :
254         // Parse xml node and write all info into context
255         virtual void parse(xml::CXMLNode & node);
256
257         // Visualize a context
258         virtual StdString toString(void) const;
259
260
261         // Solve all inheritance relation in current context
262         virtual void solveDescInheritance(bool apply, const CAttributeMap * const parent = 0);
263
264         // Verify if all root definition in a context have children
265         virtual bool hasChild(void) const;
266
267         bool isProcessingEvent(void) {return isProcessingEvent_;}
268         bool setProcessingEvent(void) {isProcessingEvent_=true ;}
269         bool unsetProcessingEvent(void) {isProcessingEvent_=false ;}
270         MPI_Comm getIntraComm(void) {return intraComm_ ;}
271         int getIntraCommRank(void) {return intraCommRank_;}
272         int getIntraCommSize(void) {return intraCommSize_;}
273
274         void addCouplingChanel(const std::string& contextId, bool out) ;
275
276      public :
277         // Calendar of context
278         std::shared_ptr<CCalendar>   calendar;
279
280         // List of all enabled files (files on which fields are written or read)
281         std::vector<CFile*> enabledFiles;
282         // List of all enabled files in read mode (files on which fields are read)
283         std::vector<CFile*> enabledReadModeFiles;
284         // List of all enabled files in write mode
285         std::vector<CFile*> enabledWriteModeFiles;
286
287         std::vector<CCouplerIn*> enabledCouplerIn;
288         std::vector<CCouplerOut*> enabledCouplerOut;
289
290
291         // List of all enabled fields whose instant data is accessible from the public API
292         // but which are not part of a file
293         std::vector<CField*> fieldsWithReadAccess_;
294         std::vector<CField*> couplerInFields_;
295         std::vector<CField*> fileInFields_;
296
297
298         // Context root
299         static std::shared_ptr<CContextGroup> root;
300
301         // Determine context on client or not
302         bool hasClient;
303
304         // Determine context on server or not
305         bool hasServer;
306
307         CContextServer* server;    //!< Concrete context server
308         CContextClient* client;    //!< Concrete contex client
309         std::vector<CContextServer*> serverPrimServer;
310         std::vector<CContextClient*> clientPrimServer;
311
312         // list of slave servers (IO server or others)
313         set<CContextClient*> slaveServers_ ;
314      private:
315         // the map containing context client associated to it string id for coupling out ;
316         std::map<std::string, CContextClient*> couplerOutClient_ ;
317         // the map containing context server associated to it string id for coupling out ;
318         std::map<std::string, CContextServer*> couplerOutServer_ ;
319         // the map containing context client associated to it string id for coupling in ;
320         std::map<std::string, CContextClient*> couplerInClient_ ;
321         // the map containing context server associated to it string id for coupling in ;
322         std::map<std::string, CContextServer*> couplerInServer_ ;
323      public:
324         CContextClient* getCouplerInClient(const string& contextId) { return couplerInClient_[contextId] ;}
325         CContextServer* getCouplerInServer(const string& contextId) { return couplerInServer_[contextId] ;}
326         CContextClient* getCouplerOutClient(const string& contextId) { return couplerOutClient_[contextId] ;}
327         CContextServer* getCouplerOutServer(const string& contextId) { return couplerOutServer_[contextId] ;}
328     
329 
330         std::vector<std::string> primServerId_;
331
332         CRegistry* registryIn ;    //!< input registry which is read from file
333         CRegistry* registryOut ;   //!< output registry which will be written into file at the finalize
334
335
336        MPI_Comm intraComm_ ; //! context intra communicator
337        int intraCommRank_ ; //! context intra communicator rank
338        int intraCommSize_ ; //! context intra communicator size
339       
340      private:
341         bool isPostProcessed;
342         bool allProcessed;
343         bool finalized;
344         int countChildContextFinalized_;        //!< Counter of child contexts (for now it is the number of secondary server pools)
345         CGarbageCollector garbageCollector;
346         std::list<MPI_Comm> comms; //!< Communicators allocated internally
347
348         int serviceType_;  //!< service associated to the context
349         string contextId_ ; //!< context client id for the servers. For clients this is same as getId()
350         bool isProcessingEvent_ ;
351         CServerContext* parentServerContext_ ;
352
353      public: // Some function maybe removed in the near future
354        // virtual void toBinary  (StdOStream & os) const;
355        // virtual void fromBinary(StdIStream & is);
356   }; // class CContext
357
358   ///--------------------------------------------------------------
359
360   // Declare/Define CContextGroup and CContextDefinition
361   DECLARE_GROUP(CContext);
362
363   ///--------------------------------------------------------------
364
365} // namespace xios
366
367#endif // __XIOS_CContext__
Note: See TracBrowser for help on using the repository browser.