[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] | 19 | namespace 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 | { |
---|
| 55 | EVENT_ID_CLOSE_DEFINITION,EVENT_ID_UPDATE_CALENDAR, |
---|
[509] | 56 | EVENT_ID_CREATE_FILE_HEADER,EVENT_ID_CONTEXT_FINALIZE, |
---|
[1764] | 57 | EVENT_ID_CONTEXT_FINALIZE_CLIENT, |
---|
[1875] | 58 | EVENT_ID_COUPLER_IN_READY, |
---|
| 59 | EVENT_ID_COUPLER_IN_CLOSE_DEFINITION, |
---|
| 60 | EVENT_ID_COUPLER_IN_CONTEXT_FINALIZED, |
---|
[549] | 61 | }; |
---|
[509] | 62 | |
---|
[219] | 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 | |
---|
[2274] | 85 | static void releaseStaticAllocation(void) ; |
---|
| 86 | |
---|
[219] | 87 | //--------------------------------------------------------- |
---|
| 88 | |
---|
| 89 | public : |
---|
[509] | 90 | |
---|
[219] | 91 | /// Mutateurs /// |
---|
[1542] | 92 | void setCalendar(std::shared_ptr<CCalendar> newCalendar); |
---|
[509] | 93 | |
---|
[219] | 94 | /// Accesseurs /// |
---|
[1542] | 95 | std::shared_ptr<CCalendar> getCalendar(void) const; |
---|
[219] | 96 | |
---|
| 97 | public : |
---|
[509] | 98 | // Initialize server or client |
---|
[1761] | 99 | void init(CServerContext* parentServerContext, MPI_Comm intraComm, int serviceType); |
---|
| 100 | void initClient(MPI_Comm intraComm, int serviceType); |
---|
| 101 | |
---|
| 102 | void initServer(MPI_Comm intraComm, int serviceType ); |
---|
| 103 | void createClientInterComm(MPI_Comm interCommClient, MPI_Comm interCommServer) ; |
---|
| 104 | void createServerInterComm(void) ; |
---|
| 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); |
---|
[509] | 119 | |
---|
[1130] | 120 | void closeDefinition(void); |
---|
| 121 | |
---|
[1869] | 122 | // to be removed |
---|
| 123 | std::vector<CField*> findAllEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles); |
---|
[509] | 124 | // Some functions to process context |
---|
[1869] | 125 | std::vector<CField*> findAllEnabledFieldsInFileOut(const std::vector<CFile*>& activeFiles); |
---|
| 126 | std::vector<CField*> findAllEnabledFieldsInFileIn(const std::vector<CFile*>& activeFiles); |
---|
| 127 | std::vector<CField*> findAllEnabledFieldsCouplerOut(const std::vector<CCouplerOut*>& activeCouplerOut); |
---|
| 128 | std::vector<CField*> findAllEnabledFieldsCouplerIn(const std::vector<CCouplerIn*>& activeCouplerIn); |
---|
[1232] | 129 | // void findAllEnabledFields(void); |
---|
| 130 | // void findAllEnabledFieldsInReadModeFiles(void); |
---|
[775] | 131 | void readAttributesOfEnabledFieldsInReadModeFiles(); |
---|
[549] | 132 | void solveAllInheritance(bool apply=true); |
---|
[300] | 133 | void findEnabledFiles(void); |
---|
[1784] | 134 | void findEnabledCouplerIn(void); |
---|
| 135 | void findEnabledCouplerOut(void); |
---|
| 136 | void createCouplerInterCommunicator(void) ; |
---|
[1232] | 137 | void findEnabledWriteModeFiles(void); |
---|
[598] | 138 | void findEnabledReadModeFiles(void); |
---|
[549] | 139 | void closeAllFile(void); |
---|
| 140 | void updateCalendar(int step); |
---|
[1232] | 141 | void createFileHeader(void); |
---|
| 142 | void initReadFiles(void); |
---|
[1973] | 143 | void prepareTimeseries(void); |
---|
[1358] | 144 | void postProcessFilterGraph(); |
---|
[598] | 145 | void startPrefetchingOfEnabledReadModeFiles(); |
---|
[1358] | 146 | void doPreTimestepOperationsForEnabledReadModeFiles(); |
---|
[1318] | 147 | void doPostTimestepOperationsForEnabledReadModeFiles(); |
---|
[593] | 148 | void findFieldsWithReadAccess(void); |
---|
[1875] | 149 | void triggerLateFields(void) ; |
---|
[2002] | 150 | |
---|
[1330] | 151 | std::map<int, StdSize> getAttributesBufferSize(std::map<int, StdSize>& maxEventSize, CContextClient* contextClient, bool bufferForWriting = false); |
---|
| 152 | std::map<int, StdSize> getDataBufferSize(std::map<int, StdSize>& maxEventSize, CContextClient* contextClient, bool bufferForWriting = false); |
---|
[509] | 153 | |
---|
[1212] | 154 | // Distribute files (in write mode) among secondary-server pools according to the estimated data flux |
---|
[1869] | 155 | void distributeFiles(const std::vector<CFile*>& files); |
---|
| 156 | void distributeFileOverBandwith(const std::vector<CFile*>& files) ; |
---|
| 157 | void distributeFileOverMemoryBandwith(const std::vector<CFile*>& files) ; |
---|
[1349] | 158 | |
---|
[509] | 159 | // Send context close definition |
---|
[549] | 160 | void sendCloseDefinition(void); |
---|
[1875] | 161 | public: |
---|
| 162 | void sendCloseDefinition(CContextClient* client) ; |
---|
| 163 | private: |
---|
| 164 | set<CContextClient*> sendCloseDefinition_done_ ; |
---|
| 165 | public: |
---|
[509] | 166 | // There are something to send on closing context defintion |
---|
[549] | 167 | void sendUpdateCalendar(int step); |
---|
| 168 | void sendCreateFileHeader(void); |
---|
[1232] | 169 | void sendEnabledFiles(const std::vector<CFile*>& activeFiles); |
---|
| 170 | void sendEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles); |
---|
| 171 | void sendRefDomainsAxisScalars(const std::vector<CFile*>& activeFiles); |
---|
[983] | 172 | //!< after be gathered to the root process of the context, merged registry is sent to the root process of the servers |
---|
[1764] | 173 | void sendFinalizeClient(CContextClient* contextClient, const string& contextClientId); |
---|
[1870] | 174 | |
---|
| 175 | public: |
---|
| 176 | void sendContextToFileServer(CContextClient* client) ; |
---|
| 177 | private: |
---|
| 178 | std::set<CContextClient*> sendToFileServer_done_ ; |
---|
| 179 | |
---|
| 180 | public: |
---|
[1761] | 181 | std::string getContextId() {return contextId_;} |
---|
[511] | 182 | |
---|
[509] | 183 | // Client side: Receive and process messages |
---|
[549] | 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); |
---|
[509] | 189 | static void recvSolveInheritanceContext(CEventServer& event); |
---|
| 190 | void recvSolveInheritanceContext(CBufferIn& buffer); |
---|
[1764] | 191 | static void recvFinalizeClient(CEventServer& event) ; |
---|
| 192 | void recvFinalizeClient(CBufferIn& buffer); |
---|
[1875] | 193 | |
---|
| 194 | public: |
---|
| 195 | void sendCouplerInReady(CContextClient* client); |
---|
| 196 | private: |
---|
| 197 | set<CContextClient*> sendCouplerInReady_done_; |
---|
| 198 | public: |
---|
| 199 | static void recvCouplerInReady(CEventServer& event) ; |
---|
| 200 | void recvCouplerInReady(CBufferIn& buffer) ; //!< coupler is ready to receive grid definition. |
---|
| 201 | set<CContextClient*> couplerInReady_; |
---|
| 202 | bool isCouplerInReady(CContextClient* client) { return couplerInReady_.count(client)!=0 ;} |
---|
[1054] | 203 | |
---|
[1875] | 204 | public: |
---|
| 205 | void sendCouplerInCloseDefinition(CContextClient* client) ; |
---|
| 206 | set<CContextClient*> sendCouplerInCloseDefinition_done_; |
---|
| 207 | static void recvCouplerInCloseDefinition(CEventServer& event) ; |
---|
| 208 | void recvCouplerInCloseDefinition(CBufferIn& buffer) ; //!< coupler has finished it defintion, data can be sent |
---|
| 209 | set<CContextClient*> couplerInCloseDefinition_ ; |
---|
| 210 | bool isCouplerInCloseDefinition(CContextClient* client) { return couplerInCloseDefinition_.count(client)!=0 ;} |
---|
| 211 | |
---|
| 212 | public: |
---|
| 213 | void sendCouplerInContextFinalized(CContextClient* client) ; |
---|
| 214 | set<CContextClient*> sendCouplerInContextFinalized_done_; |
---|
| 215 | static void recvCouplerInContextFinalized(CEventServer& event) ; |
---|
| 216 | void recvCouplerInContextFinalized(CBufferIn& buffer) ; //!< coupler has finished it defintion, data can be sent |
---|
| 217 | set<CContextClient*> couplerInContextFinalized_ ; |
---|
| 218 | bool isCouplerInContextFinalized(CContextClient* client) { return couplerInContextFinalized_.count(client)!=0 ;} |
---|
| 219 | |
---|
| 220 | public: |
---|
| 221 | void freeComms(void); //!< Free internally allcoated communicators |
---|
| 222 | void releaseClientBuffers(void); //! Deallocate buffers allocated by clientContexts |
---|
| 223 | |
---|
[509] | 224 | // dispatch event |
---|
[549] | 225 | static bool dispatchEvent(CEventServer& event); |
---|
[509] | 226 | |
---|
| 227 | public: |
---|
| 228 | // Get current context |
---|
| 229 | static CContext* getCurrent(void); |
---|
| 230 | |
---|
| 231 | // Get context root |
---|
| 232 | static CContextGroup* getRoot(void); |
---|
[2274] | 233 | |
---|
[509] | 234 | // Set current context |
---|
| 235 | static void setCurrent(const string& id); |
---|
| 236 | |
---|
| 237 | // Create new context |
---|
| 238 | static CContext* create(const string& id = ""); |
---|
| 239 | |
---|
| 240 | /// Accesseurs statiques /// |
---|
| 241 | static StdString GetName(void); |
---|
| 242 | static StdString GetDefName(void); |
---|
| 243 | static ENodeType GetType(void); |
---|
| 244 | |
---|
| 245 | static CContextGroup* GetContextGroup(void); |
---|
| 246 | |
---|
| 247 | // Some functions to visualize structure of current context |
---|
| 248 | static void ShowTree(StdOStream & out = std::clog); |
---|
| 249 | static void CleanTree(void); |
---|
[2265] | 250 | static void removeContext(const std::string& contextId); |
---|
| 251 | static void removeAllContexts(void) ; |
---|
[1761] | 252 | int getServiceType(void) {return serviceType_;} |
---|
[509] | 253 | |
---|
[219] | 254 | public : |
---|
[509] | 255 | // Parse xml node and write all info into context |
---|
[219] | 256 | virtual void parse(xml::CXMLNode & node); |
---|
| 257 | |
---|
[509] | 258 | // Visualize a context |
---|
[219] | 259 | virtual StdString toString(void) const; |
---|
[509] | 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 | |
---|
[1761] | 268 | bool isProcessingEvent(void) {return isProcessingEvent_;} |
---|
| 269 | bool setProcessingEvent(void) {isProcessingEvent_=true ;} |
---|
| 270 | bool unsetProcessingEvent(void) {isProcessingEvent_=false ;} |
---|
[1869] | 271 | MPI_Comm getIntraComm(void) {return intraComm_ ;} |
---|
| 272 | int getIntraCommRank(void) {return intraCommRank_;} |
---|
| 273 | int getIntraCommSize(void) {return intraCommSize_;} |
---|
[1054] | 274 | |
---|
[1784] | 275 | void addCouplingChanel(const std::string& contextId, bool out) ; |
---|
| 276 | |
---|
[300] | 277 | public : |
---|
[509] | 278 | // Calendar of context |
---|
[1542] | 279 | std::shared_ptr<CCalendar> calendar; |
---|
[509] | 280 | |
---|
[598] | 281 | // List of all enabled files (files on which fields are written or read) |
---|
[347] | 282 | std::vector<CFile*> enabledFiles; |
---|
[598] | 283 | // List of all enabled files in read mode (files on which fields are read) |
---|
| 284 | std::vector<CFile*> enabledReadModeFiles; |
---|
[1208] | 285 | // List of all enabled files in write mode |
---|
| 286 | std::vector<CFile*> enabledWriteModeFiles; |
---|
[509] | 287 | |
---|
[1784] | 288 | std::vector<CCouplerIn*> enabledCouplerIn; |
---|
| 289 | std::vector<CCouplerOut*> enabledCouplerOut; |
---|
| 290 | |
---|
| 291 | |
---|
[740] | 292 | // List of all enabled fields whose instant data is accessible from the public API |
---|
| 293 | // but which are not part of a file |
---|
[1869] | 294 | std::vector<CField*> fieldsWithReadAccess_; |
---|
[1875] | 295 | std::vector<CField*> couplerInFields_; |
---|
| 296 | std::vector<CField*> fileInFields_; |
---|
[593] | 297 | |
---|
[1875] | 298 | |
---|
[509] | 299 | // Context root |
---|
[1542] | 300 | static std::shared_ptr<CContextGroup> root; |
---|
[219] | 301 | |
---|
[509] | 302 | // Determine context on client or not |
---|
[549] | 303 | bool hasClient; |
---|
[300] | 304 | |
---|
[509] | 305 | // Determine context on server or not |
---|
[549] | 306 | bool hasServer; |
---|
[509] | 307 | |
---|
[1130] | 308 | CContextServer* server; //!< Concrete context server |
---|
| 309 | CContextClient* client; //!< Concrete contex client |
---|
[1071] | 310 | std::vector<CContextServer*> serverPrimServer; |
---|
| 311 | std::vector<CContextClient*> clientPrimServer; |
---|
[1784] | 312 | |
---|
[1875] | 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 | |
---|
[1761] | 331 | std::vector<std::string> primServerId_; |
---|
[1071] | 332 | |
---|
[2274] | 333 | CRegistry* registryIn=nullptr ; //!< input registry which is read from file |
---|
| 334 | CRegistry* registryOut=nullptr ; //!< output registry which will be written into file at the finalize |
---|
[775] | 335 | |
---|
[1761] | 336 | |
---|
| 337 | MPI_Comm intraComm_ ; //! context intra communicator |
---|
[1784] | 338 | int intraCommRank_ ; //! context intra communicator rank |
---|
| 339 | int intraCommSize_ ; //! context intra communicator size |
---|
| 340 | |
---|
[509] | 341 | private: |
---|
[2022] | 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 | |
---|
[509] | 347 | bool isPostProcessed; |
---|
[1025] | 348 | bool allProcessed; |
---|
[1139] | 349 | bool finalized; |
---|
[1764] | 350 | int countChildContextFinalized_; //!< Counter of child contexts (for now it is the number of secondary server pools) |
---|
[639] | 351 | CGarbageCollector garbageCollector; |
---|
[1639] | 352 | std::list<MPI_Comm> comms; //!< Communicators allocated internally |
---|
[509] | 353 | |
---|
[1761] | 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_ ; |
---|
[2130] | 357 | private: |
---|
[1761] | 358 | CServerContext* parentServerContext_ ; |
---|
[2130] | 359 | public: |
---|
| 360 | CServerContext* getParentServerContext(void) { return parentServerContext_; } |
---|
| 361 | private: |
---|
| 362 | bool lockedContext_=false; |
---|
| 363 | public: |
---|
| 364 | void lockContext(void) {lockedContext_=true; } |
---|
[2204] | 365 | void unlockContext(void) {lockedContext_=false; } |
---|
[2130] | 366 | bool isLockedContext(void) { return lockedContext_;} |
---|
[509] | 367 | public: // Some function maybe removed in the near future |
---|
| 368 | // virtual void toBinary (StdOStream & os) const; |
---|
| 369 | // virtual void fromBinary(StdIStream & is); |
---|
[219] | 370 | }; // class CContext |
---|
| 371 | |
---|
| 372 | ///-------------------------------------------------------------- |
---|
| 373 | |
---|
| 374 | // Declare/Define CContextGroup and CContextDefinition |
---|
| 375 | DECLARE_GROUP(CContext); |
---|
| 376 | |
---|
| 377 | ///-------------------------------------------------------------- |
---|
| 378 | |
---|
[335] | 379 | } // namespace xios |
---|
[219] | 380 | |
---|
[591] | 381 | #endif // __XIOS_CContext__ |
---|