source: XIOS3/dev/XIOS_ATTACHED/src/node/context.hpp @ 2482

Last change on this file since 2482 was 2482, checked in by ymipsl, 15 months ago

First guess in supression of attached mode replaced by online reader and write filters

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: 17.5 KB
RevLine 
[591]1#ifndef __XIOS_CContext__
2#define __XIOS_CContext__
[219]3
[591]4/// XIOS headers ///
5#include "xios_spl.hpp"
[352]6//#include "node_type.hpp"
[549]7#include "calendar_wrapper.hpp"
[219]8
9#include "declare_group.hpp"
[300]10#include "data_output.hpp"
[639]11#include "garbage_collector.hpp"
[697]12#include "registry.hpp"
[382]13#include "mpi.hpp"
[1761]14#include "services_manager.hpp"
15#include "server_context.hpp"
[2022]16#include "event_scheduler.hpp"
[300]17
[219]18
[2022]19namespace xios
20{
[549]21   class CContextClient;
22   class CContextServer;
[509]23
24
[219]25   /// ////////////////////// Déclarations ////////////////////// ///
26   class CContextGroup;
27   class CContextAttributes;
28   class CContext;
[352]29   class CFile;
[1784]30   class CCouplerIn ;
31   class CCouplerOut ;
[219]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   ///--------------------------------------------------------------
[509]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  */
[219]48   class CContext
49      : public CObjectTemplate<CContext>
50      , public CContextAttributes
51   {
[300]52         public :
53         enum EEventId
54         {
[2326]55           EVENT_ID_COLLECTIVE=100,
[300]56           EVENT_ID_CLOSE_DEFINITION,EVENT_ID_UPDATE_CALENDAR,
[2405]57           EVENT_ID_CONTEXT_FINALIZE,
[1764]58           EVENT_ID_CONTEXT_FINALIZE_CLIENT,
[1875]59           EVENT_ID_COUPLER_IN_READY,
60           EVENT_ID_COUPLER_IN_CLOSE_DEFINITION,
61           EVENT_ID_COUPLER_IN_CONTEXT_FINALIZED,
[2326]62           EVENT_ID_NO_COLLECTIVE=1000,
[549]63         };
[509]64
[219]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
[2274]87         static void releaseStaticAllocation(void) ;
88
[219]89         //---------------------------------------------------------
90
91      public :
[509]92
[219]93         /// Mutateurs ///
[1542]94         void setCalendar(std::shared_ptr<CCalendar> newCalendar);
[509]95
[219]96         /// Accesseurs ///
[1542]97         std::shared_ptr<CCalendar>      getCalendar(void) const;
[219]98
99      public :
[509]100         // Initialize server or client
[1761]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 );
[2458]105         
[549]106         bool isInitialized(void);
[219]107
[1622]108         StdString dumpClassAttributes(void);
109
[509]110         // Put sever or client into loop state
[1761]111         bool eventLoop(bool enableEventsProcessing=true);
[2123]112         bool scheduledEventLoop(bool enableEventsProcessing=true) ; 
[1764]113         void globalEventLoop(void);
[597]114
[509]115         // Finalize a context
[549]116         void finalize(void);
[1764]117
[1071]118         bool isFinalized(void);
[1130]119         void closeDefinition(void);
120
[1869]121         // to be removed     
122         std::vector<CField*> findAllEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles);
[509]123         // Some functions to process context
[1869]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);
[1232]128         // void findAllEnabledFields(void);
129         // void findAllEnabledFieldsInReadModeFiles(void);
[775]130         void readAttributesOfEnabledFieldsInReadModeFiles();
[549]131         void solveAllInheritance(bool apply=true);
[300]132         void findEnabledFiles(void);
[1784]133         void findEnabledCouplerIn(void);
134         void findEnabledCouplerOut(void);
135         void createCouplerInterCommunicator(void) ;
[1232]136         void findEnabledWriteModeFiles(void);
[598]137         void findEnabledReadModeFiles(void);
[549]138         void closeAllFile(void);
139         void updateCalendar(int step);
[1232]140         void createFileHeader(void);
141         void initReadFiles(void);
[1973]142         void prepareTimeseries(void);
[598]143         void startPrefetchingOfEnabledReadModeFiles();
[1318]144         void doPostTimestepOperationsForEnabledReadModeFiles();
[593]145         void findFieldsWithReadAccess(void);
[1875]146         void triggerLateFields(void) ;
[2002]147         
[1330]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);
[509]150
[1212]151         // Distribute files (in write mode) among secondary-server pools according to the estimated data flux
[2458]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
[1349]157         
[2406]158       public:
[509]159         // Send context close definition
[1875]160         void sendCloseDefinition(CContextClient* client) ;
161       private:
162         set<CContextClient*> sendCloseDefinition_done_ ;
163       public:
[509]164         // There are something to send on closing context defintion
[549]165         void sendUpdateCalendar(int step);
[1232]166         void sendEnabledFiles(const std::vector<CFile*>& activeFiles);
167         void sendEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles);
168         void sendRefDomainsAxisScalars(const std::vector<CFile*>& activeFiles);
[983]169         //!< after be gathered to the root process of the context, merged registry is sent to the root process of the servers
[1764]170         void sendFinalizeClient(CContextClient* contextClient, const string& contextClientId);
[1870]171         
172         public:
173         void sendContextToFileServer(CContextClient* client) ;
174         private:
175         std::set<CContextClient*> sendToFileServer_done_ ;
176         
177         public: 
[1761]178         std::string getContextId() {return contextId_;}
[511]179
[509]180         // Client side: Receive and process messages
[549]181         static void recvUpdateCalendar(CEventServer& event);
182         void recvUpdateCalendar(CBufferIn& buffer);
183         static void recvCloseDefinition(CEventServer& event);
[509]184         static void recvSolveInheritanceContext(CEventServer& event);
185         void recvSolveInheritanceContext(CBufferIn& buffer);
[1764]186         static void recvFinalizeClient(CEventServer& event) ;
187         void recvFinalizeClient(CBufferIn& buffer);
[1875]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 ;}
[1054]198
[1875]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
[509]218         // dispatch event
[549]219         static bool dispatchEvent(CEventServer& event);
[509]220
221      public:
222        // Get current context
223        static CContext* getCurrent(void);
224
225        // Get context root
226        static CContextGroup* getRoot(void);
[2274]227       
[509]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);
[2265]244        static void removeContext(const std::string& contextId);
245        static void removeAllContexts(void) ;
[1761]246        int getServiceType(void) {return serviceType_;}
[509]247
[219]248      public :
[509]249         // Parse xml node and write all info into context
[219]250         virtual void parse(xml::CXMLNode & node);
251
[509]252         // Visualize a context
[219]253         virtual StdString toString(void) const;
[509]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
[1761]262         bool isProcessingEvent(void) {return isProcessingEvent_;}
263         bool setProcessingEvent(void) {isProcessingEvent_=true ;}
264         bool unsetProcessingEvent(void) {isProcessingEvent_=false ;}
[2458]265         
[1784]266         void addCouplingChanel(const std::string& contextId, bool out) ;
267
[300]268      public :
[509]269         // Calendar of context
[1542]270         std::shared_ptr<CCalendar>   calendar;
[509]271
[598]272         // List of all enabled files (files on which fields are written or read)
[347]273         std::vector<CFile*> enabledFiles;
[598]274         // List of all enabled files in read mode (files on which fields are read)
275         std::vector<CFile*> enabledReadModeFiles;
[1208]276         // List of all enabled files in write mode
277         std::vector<CFile*> enabledWriteModeFiles;
[509]278
[1784]279         std::vector<CCouplerIn*> enabledCouplerIn;
280         std::vector<CCouplerOut*> enabledCouplerOut;
281
282
[740]283         // List of all enabled fields whose instant data is accessible from the public API
284         // but which are not part of a file
[1869]285         std::vector<CField*> fieldsWithReadAccess_;
[1875]286         std::vector<CField*> couplerInFields_;
287         std::vector<CField*> fileInFields_;
[593]288
[1875]289
[509]290         // Context root
[1542]291         static std::shared_ptr<CContextGroup> root;
[219]292
[509]293         // Determine context on client or not
[549]294         bool hasClient;
[300]295
[509]296         // Determine context on server or not
[549]297         bool hasServer;
[2482]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*> filesToWrite_ ; 
[509]302
[1875]303      private:
[2482]304        CContextClient* onlineContextClient_=nullptr ;
305       
[2458]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) ;
[2405]314
[2458]315
316        std::map<std::pair<string,string>,std::vector<pair<CContextClient*,CContextServer*>>> serversMap_ ;
317
[2406]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
[2405]329        std::map<std::string, CContextClient*> clients_ ;
330        std::map<std::string, CContextClient*> servers_ ;
[2407]331        std::map<CContextClient*, std::string> clientsId_ ;
332        std::map<CContextServer*, std::string> serversId_ ;
[2405]333
[2407]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_ ;
[1875]345      public:
[2458]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) ;
[1875]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] ;}
[2458]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
[1071]361
[775]362
[1761]363        MPI_Comm intraComm_ ; //! context intra communicator
[1784]364        int intraCommRank_ ; //! context intra communicator rank
365        int intraCommSize_ ; //! context intra communicator size
[2458]366       public: 
367        MPI_Comm getIntraComm(void) {return intraComm_ ;}
368        int getIntraCommRank(void) {return intraCommRank_;}
369        int getIntraCommSize(void) {return intraCommSize_;}
[509]370      private:
[2404]371         shared_ptr<CEventScheduler> eventScheduler_ ; //! The local event scheduler for context
[2022]372         size_t hashId_ ; //! the local hashId for scheduler
373         size_t timeLine_=0 ;
374         void initEventScheduler(void) ;
375
[509]376         bool isPostProcessed;
[1025]377         bool allProcessed;
[1139]378         bool finalized;
[1764]379         int countChildContextFinalized_;        //!< Counter of child contexts (for now it is the number of secondary server pools)
[639]380         CGarbageCollector garbageCollector;
[1639]381         std::list<MPI_Comm> comms; //!< Communicators allocated internally
[509]382
[1761]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_ ;
[2130]386    private:     
[1761]387         CServerContext* parentServerContext_ ;
[2130]388    public:
389         CServerContext* getParentServerContext(void) { return parentServerContext_; }
390    private: 
391      bool lockedContext_=false;
392    public: 
393        void lockContext(void) {lockedContext_=true; }
[2204]394        void unlockContext(void) {lockedContext_=false; }
[2130]395        bool isLockedContext(void) { return lockedContext_;}
[509]396      public: // Some function maybe removed in the near future
397        // virtual void toBinary  (StdOStream & os) const;
398        // virtual void fromBinary(StdIStream & is);
[219]399   }; // class CContext
400
401   ///--------------------------------------------------------------
402
403   // Declare/Define CContextGroup and CContextDefinition
404   DECLARE_GROUP(CContext);
405
406   ///--------------------------------------------------------------
407
[335]408} // namespace xios
[219]409
[591]410#endif // __XIOS_CContext__
Note: See TracBrowser for help on using the repository browser.