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

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

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