source: XIOS3/trunk/src/node/context.hpp @ 2509

Last change on this file since 2509 was 2509, checked in by jderouillat, 13 months ago

Add a comparison operator to the filesToWrite_ std:: set to force the file creation ordering

  • 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: 17.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#include "file.hpp"
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_COLLECTIVE=100,
56           EVENT_ID_CLOSE_DEFINITION,EVENT_ID_UPDATE_CALENDAR,
57           EVENT_ID_CONTEXT_FINALIZE,
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           EVENT_ID_NO_COLLECTIVE=1000,
63         };
64
65         /// typedef ///
66         typedef CObjectTemplate<CContext>   SuperClass;
67         typedef CContextAttributes SuperClassAttribute;
68
69      public :
70
71         typedef CContextAttributes RelAttributes;
72         typedef CContext           RelGroup;
73
74         //---------------------------------------------------------
75
76      public :
77
78         /// Constructeurs ///
79         CContext(void);
80         explicit CContext(const StdString & id);
81         CContext(const CContext & context);       // Not implemented yet.
82         CContext(const CContext * const context); // Not implemented yet.
83
84         /// Destructeur ///
85         virtual ~CContext(void);
86
87         static void releaseStaticAllocation(void) ;
88
89         //---------------------------------------------------------
90
91      public :
92
93         /// Mutateurs ///
94         void setCalendar(std::shared_ptr<CCalendar> newCalendar);
95
96         /// Accesseurs ///
97         std::shared_ptr<CCalendar>      getCalendar(void) const;
98
99      public :
100         // Initialize server or client
101         void init(CServerContext* parentServerContext, MPI_Comm intraComm, int serviceType);
102         void initClient(MPI_Comm intraComm, int serviceType);
103         
104         void initServer(MPI_Comm intraComm, int serviceType );
105         
106         bool isInitialized(void);
107
108         StdString dumpClassAttributes(void);
109
110         // Put sever or client into loop state
111         bool eventLoop(bool enableEventsProcessing=true);
112         bool scheduledEventLoop(bool enableEventsProcessing=true) ; 
113         void globalEventLoop(void);
114
115         // Finalize a context
116         void finalize(void);
117
118         bool isFinalized(void);
119         void closeDefinition(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 prepareTimeseries(void);
143         void startPrefetchingOfEnabledReadModeFiles();
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
151         // Distribute files (in write mode) among secondary-server pools according to the estimated data flux
152         void distributeFiles(const vector<CFile*>& files) ;
153         void distributeFilesOnSameService(const vector<CFile*>& files, const string& poolId, const string& serviceId) ;
154         void distributeFileOverOne(const vector<CFile*>& files, const string& poolId, const string& serviceId) ; //!< Distribute files over one single server (no distribution)
155         void distributeFileOverBandwith(const std::vector<CFile*>& files, const string& poolId, const string& serviceId) ; //!< Distribute files overs servers to balance the I/O bandwith
156         void distributeFileOverMemoryBandwith(const std::vector<CFile*>& files, const string& poolId, const string& serviceId) ; //!< Distribute files overs servers to minimize the memory consumption
157         
158       public:
159         // Send context close definition
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 sendEnabledFiles(const std::vector<CFile*>& activeFiles);
167         void sendEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles);
168         void sendRefDomainsAxisScalars(const std::vector<CFile*>& activeFiles);
169         //!< after be gathered to the root process of the context, merged registry is sent to the root process of the servers
170         void sendFinalizeClient(CContextClient* contextClient, const string& contextClientId);
171         
172         public:
173         void sendContextToFileServer(CContextClient* client) ;
174         private:
175         std::set<CContextClient*> sendToFileServer_done_ ;
176         
177         public: 
178         std::string getContextId() {return contextId_;}
179
180         // Client side: Receive and process messages
181         static void recvUpdateCalendar(CEventServer& event);
182         void recvUpdateCalendar(CBufferIn& buffer);
183         static void recvCloseDefinition(CEventServer& event);
184         static void recvSolveInheritanceContext(CEventServer& event);
185         void recvSolveInheritanceContext(CBufferIn& buffer);
186         static void recvFinalizeClient(CEventServer& event) ;
187         void recvFinalizeClient(CBufferIn& buffer);
188       
189       public:
190         void sendCouplerInReady(CContextClient* client);
191       private:
192         set<CContextClient*> sendCouplerInReady_done_;
193       public:
194         static void recvCouplerInReady(CEventServer& event) ;
195         void recvCouplerInReady(CBufferIn& buffer) ; //!< coupler is ready to receive grid definition.
196         set<CContextClient*> couplerInReady_;
197         bool isCouplerInReady(CContextClient* client) { return couplerInReady_.count(client)!=0 ;}
198
199       public:
200        void sendCouplerInCloseDefinition(CContextClient* client) ;
201        set<CContextClient*> sendCouplerInCloseDefinition_done_;
202        static void recvCouplerInCloseDefinition(CEventServer& event) ;
203        void recvCouplerInCloseDefinition(CBufferIn& buffer) ; //!< coupler has finished it defintion, data can be sent     
204        set<CContextClient*> couplerInCloseDefinition_ ;
205        bool isCouplerInCloseDefinition(CContextClient* client) { return couplerInCloseDefinition_.count(client)!=0 ;}
206
207       public:
208        void sendCouplerInContextFinalized(CContextClient* client) ;
209        set<CContextClient*> sendCouplerInContextFinalized_done_;
210        static void recvCouplerInContextFinalized(CEventServer& event) ;
211        void recvCouplerInContextFinalized(CBufferIn& buffer) ; //!< coupler has finished it defintion, data can be sent     
212        set<CContextClient*> couplerInContextFinalized_ ;
213        bool isCouplerInContextFinalized(CContextClient* client) { return couplerInContextFinalized_.count(client)!=0 ;}
214
215       public: 
216        void freeComms(void);                  //!< Free internally allcoated communicators
217
218         // dispatch event
219         static bool dispatchEvent(CEventServer& event);
220
221      public:
222        // Get current context
223        static CContext* getCurrent(void);
224
225        // Get context root
226        static CContextGroup* getRoot(void);
227       
228        // Set current context
229        static void setCurrent(const string& id);
230
231        // Create new context
232        static CContext* create(const string& id = "");
233
234        /// Accesseurs statiques ///
235        static StdString GetName(void);
236        static StdString GetDefName(void);
237        static ENodeType GetType(void);
238
239        static CContextGroup* GetContextGroup(void);
240
241        // Some functions to visualize structure of current context
242        static void ShowTree(StdOStream & out = std::clog);
243        static void CleanTree(void);
244        static void removeContext(const std::string& contextId);
245        static void removeAllContexts(void) ;
246        int getServiceType(void) {return serviceType_;}
247
248      public :
249         // Parse xml node and write all info into context
250         virtual void parse(xml::CXMLNode & node);
251
252         // Visualize a context
253         virtual StdString toString(void) const;
254
255
256         // Solve all inheritance relation in current context
257         virtual void solveDescInheritance(bool apply, const CAttributeMap * const parent = 0);
258
259         // Verify if all root definition in a context have children
260         virtual bool hasChild(void) const;
261
262         bool isProcessingEvent(void) {return isProcessingEvent_;}
263         bool setProcessingEvent(void) {isProcessingEvent_=true ;}
264         bool unsetProcessingEvent(void) {isProcessingEvent_=false ;}
265         
266         void addCouplingChanel(const std::string& contextId, bool out) ;
267
268      public :
269         // Calendar of context
270         std::shared_ptr<CCalendar>   calendar;
271
272         // List of all enabled files (files on which fields are written or read)
273         std::vector<CFile*> enabledFiles;
274         // List of all enabled files in read mode (files on which fields are read)
275         std::vector<CFile*> enabledReadModeFiles;
276         // List of all enabled files in write mode
277         std::vector<CFile*> enabledWriteModeFiles;
278
279         std::vector<CCouplerIn*> enabledCouplerIn;
280         std::vector<CCouplerOut*> enabledCouplerOut;
281
282
283         // List of all enabled fields whose instant data is accessible from the public API
284         // but which are not part of a file
285         std::vector<CField*> fieldsWithReadAccess_;
286         std::vector<CField*> couplerInFields_;
287         std::vector<CField*> fileInFields_;
288
289
290         // Context root
291         static std::shared_ptr<CContextGroup> root;
292
293         // Determine context on client or not
294         bool hasClient;
295
296         // Determine context on server or not
297         bool hasServer;
298      public:
299        void registerFileToWrite(CFile* file) { filesToWrite_.insert(file); } // Add a file that need to be write for example to create headers
300      private: 
301        std::set<CFile*,FilePtrCompare> filesToWrite_ ; 
302
303      private:
304        CContextClient* onlineContextClient_=nullptr ;
305       
306        std::string defaultPoolWriterId_ ;
307        std::string defaultPoolReaderId_ ;
308        std::string defaultPoolGathererId_ ;
309        std::string defaultWriterId_ ;
310        std::string defaultReaderId_ ;
311        std::string defaultGathererId_ ;
312        bool defaultUsingServer2_ ;
313        void setDefaultServices(void) ;
314
315
316        std::map<std::pair<string,string>,std::vector<pair<CContextClient*,CContextServer*>>> serversMap_ ;
317
318        std::vector<CContextClient*> writerClientOut_ ;
319        std::vector<CContextServer*> writerServerOut_ ;
320        std::vector<CContextClient*> writerClientIn_ ;
321        std::vector<CContextServer*> writerServerIn_ ;
322
323        std::vector<CContextClient*> readerClientOut_ ;
324        std::vector<CContextServer*> readerServerOut_ ;
325        std::vector<CContextClient*> readerClientIn_ ;
326        std::vector<CContextServer*> readerServerIn_ ;
327
328
329        std::map<std::string, CContextClient*> clients_ ;
330        std::map<std::string, CContextClient*> servers_ ;
331        std::map<CContextClient*, std::string> clientsId_ ;
332        std::map<CContextServer*, std::string> serversId_ ;
333
334        // list of slave servers (IO server or others)
335        std::vector<CContextClient*> slaveServers_ ;
336
337        // the map containing context client associated to it string id for coupling out ;
338        std::map<std::string, CContextClient*> couplerOutClient_ ;
339        // the map containing context server associated to it string id for coupling out ;
340        std::map<std::string, CContextServer*> couplerOutServer_ ;
341        // the map containing context client associated to it string id for coupling in ;
342        std::map<std::string, CContextClient*> couplerInClient_ ;
343        // the map containing context server associated to it string id for coupling in ;
344        std::map<std::string, CContextServer*> couplerInServer_ ;
345      public:
346         void createClientInterComm(MPI_Comm interCommClient, MPI_Comm interCommServer)  ;
347         void createServerInterComm(void)  ; // obsolete
348         void createServerInterComm_old(void)  ;
349         void createServerInterComm(const string& poolId, const string& serverId, vector<pair<string, pair<CContextClient*,CContextServer*>>>& clientServers ) ;
350         void getServerInterComm(const string& poolId, const string& serviceId,  vector<pair<CContextClient*,CContextServer*>>& clientServers) ;
351         vector<CContextClient*> getContextClient(const string& poolId, const string& serviceId) ;
352         CContextClient* getCouplerInClient(const string& contextId) { return couplerInClient_[contextId] ;}
353         CContextServer* getCouplerInServer(const string& contextId) { return couplerInServer_[contextId] ;}
354         CContextClient* getCouplerOutClient(const string& contextId) { return couplerOutClient_[contextId] ;}
355         CContextServer* getCouplerOutServer(const string& contextId) { return couplerOutServer_[contextId] ;}
356       
357       public: // must be privatize using accessors
358       
359        CRegistry* registryIn=nullptr ;    //!< input registry which is read from file
360        CRegistry* registryOut=nullptr ;   //!< output registry which will be written into file at the finalize
361
362
363        MPI_Comm intraComm_ ; //! context intra communicator
364        int intraCommRank_ ; //! context intra communicator rank
365        int intraCommSize_ ; //! context intra communicator size
366       public: 
367        MPI_Comm getIntraComm(void) {return intraComm_ ;}
368        int getIntraCommRank(void) {return intraCommRank_;}
369        int getIntraCommSize(void) {return intraCommSize_;}
370      private:
371         shared_ptr<CEventScheduler> eventScheduler_ ; //! The local event scheduler for context
372         size_t hashId_ ; //! the local hashId for scheduler
373         size_t timeLine_=0 ;
374         void initEventScheduler(void) ;
375
376         bool isPostProcessed;
377         bool allProcessed;
378         bool finalized;
379         int countChildContextFinalized_;        //!< Counter of child contexts (for now it is the number of secondary server pools)
380         CGarbageCollector garbageCollector;
381         std::list<MPI_Comm> comms; //!< Communicators allocated internally
382
383         int serviceType_;  //!< service associated to the context
384         string contextId_ ; //!< context client id for the servers. For clients this is same as getId()
385         bool isProcessingEvent_ ;
386    private:     
387         CServerContext* parentServerContext_ ;
388    public:
389         CServerContext* getParentServerContext(void) { return parentServerContext_; }
390    private: 
391      bool lockedContext_=false;
392    public: 
393        void lockContext(void) {lockedContext_=true; }
394        void unlockContext(void) {lockedContext_=false; }
395        bool isLockedContext(void) { return lockedContext_;}
396      public: // Some function maybe removed in the near future
397        // virtual void toBinary  (StdOStream & os) const;
398        // virtual void fromBinary(StdIStream & is);
399   }; // class CContext
400
401   ///--------------------------------------------------------------
402
403   // Declare/Define CContextGroup and CContextDefinition
404   DECLARE_GROUP(CContext);
405
406   ///--------------------------------------------------------------
407
408} // namespace xios
409
410#endif // __XIOS_CContext__
Note: See TracBrowser for help on using the repository browser.