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

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

Graph intermediate commit to a tmp branch. Integrate latest modifications of branch coupling

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