[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, |
---|
[1973] | 57 | EVENT_ID_SEND_REGISTRY, |
---|
[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, |
---|
[549] | 62 | }; |
---|
[509] | 63 | |
---|
[219] | 64 | /// typedef /// |
---|
| 65 | typedef CObjectTemplate<CContext> SuperClass; |
---|
| 66 | typedef CContextAttributes SuperClassAttribute; |
---|
| 67 | |
---|
| 68 | public : |
---|
| 69 | |
---|
| 70 | typedef CContextAttributes RelAttributes; |
---|
| 71 | typedef CContext RelGroup; |
---|
| 72 | |
---|
| 73 | //--------------------------------------------------------- |
---|
| 74 | |
---|
| 75 | public : |
---|
| 76 | |
---|
| 77 | /// Constructeurs /// |
---|
| 78 | CContext(void); |
---|
| 79 | explicit CContext(const StdString & id); |
---|
| 80 | CContext(const CContext & context); // Not implemented yet. |
---|
| 81 | CContext(const CContext * const context); // Not implemented yet. |
---|
| 82 | |
---|
| 83 | /// Destructeur /// |
---|
| 84 | virtual ~CContext(void); |
---|
| 85 | |
---|
| 86 | //--------------------------------------------------------- |
---|
| 87 | |
---|
| 88 | public : |
---|
[509] | 89 | |
---|
[219] | 90 | /// Mutateurs /// |
---|
[1542] | 91 | void setCalendar(std::shared_ptr<CCalendar> newCalendar); |
---|
[509] | 92 | |
---|
[219] | 93 | /// Accesseurs /// |
---|
[1542] | 94 | std::shared_ptr<CCalendar> getCalendar(void) const; |
---|
[219] | 95 | |
---|
| 96 | public : |
---|
[509] | 97 | // Initialize server or client |
---|
[1761] | 98 | void init(CServerContext* parentServerContext, MPI_Comm intraComm, int serviceType); |
---|
| 99 | void initClient(MPI_Comm intraComm, int serviceType); |
---|
| 100 | |
---|
| 101 | void initServer(MPI_Comm intraComm, int serviceType ); |
---|
| 102 | void createClientInterComm(MPI_Comm interCommClient, MPI_Comm interCommServer) ; |
---|
| 103 | void createServerInterComm(void) ; |
---|
| 104 | |
---|
[549] | 105 | bool isInitialized(void); |
---|
[219] | 106 | |
---|
[1622] | 107 | StdString dumpClassAttributes(void); |
---|
| 108 | |
---|
[509] | 109 | // Put sever or client into loop state |
---|
[1761] | 110 | bool eventLoop(bool enableEventsProcessing=true); |
---|
[1764] | 111 | void globalEventLoop(void); |
---|
[597] | 112 | |
---|
[509] | 113 | // Finalize a context |
---|
[549] | 114 | void finalize(void); |
---|
[1764] | 115 | |
---|
[1071] | 116 | bool isFinalized(void); |
---|
[509] | 117 | |
---|
[1130] | 118 | void closeDefinition(void); |
---|
| 119 | |
---|
[1869] | 120 | // to be removed |
---|
| 121 | std::vector<CField*> findAllEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles); |
---|
[509] | 122 | // Some functions to process context |
---|
[1869] | 123 | std::vector<CField*> findAllEnabledFieldsInFileOut(const std::vector<CFile*>& activeFiles); |
---|
| 124 | std::vector<CField*> findAllEnabledFieldsInFileIn(const std::vector<CFile*>& activeFiles); |
---|
| 125 | std::vector<CField*> findAllEnabledFieldsCouplerOut(const std::vector<CCouplerOut*>& activeCouplerOut); |
---|
| 126 | std::vector<CField*> findAllEnabledFieldsCouplerIn(const std::vector<CCouplerIn*>& activeCouplerIn); |
---|
[1232] | 127 | // void findAllEnabledFields(void); |
---|
| 128 | // void findAllEnabledFieldsInReadModeFiles(void); |
---|
[775] | 129 | void readAttributesOfEnabledFieldsInReadModeFiles(); |
---|
[549] | 130 | void solveAllInheritance(bool apply=true); |
---|
[300] | 131 | void findEnabledFiles(void); |
---|
[1784] | 132 | void findEnabledCouplerIn(void); |
---|
| 133 | void findEnabledCouplerOut(void); |
---|
| 134 | void createCouplerInterCommunicator(void) ; |
---|
[1232] | 135 | void findEnabledWriteModeFiles(void); |
---|
[598] | 136 | void findEnabledReadModeFiles(void); |
---|
[549] | 137 | void closeAllFile(void); |
---|
| 138 | void updateCalendar(int step); |
---|
[1232] | 139 | void createFileHeader(void); |
---|
| 140 | void initReadFiles(void); |
---|
[1973] | 141 | void prepareTimeseries(void); |
---|
[1358] | 142 | void postProcessFilterGraph(); |
---|
[598] | 143 | void startPrefetchingOfEnabledReadModeFiles(); |
---|
[1358] | 144 | void doPreTimestepOperationsForEnabledReadModeFiles(); |
---|
[1318] | 145 | void doPostTimestepOperationsForEnabledReadModeFiles(); |
---|
[593] | 146 | void findFieldsWithReadAccess(void); |
---|
[1875] | 147 | void triggerLateFields(void) ; |
---|
[2002] | 148 | |
---|
[1330] | 149 | std::map<int, StdSize> getAttributesBufferSize(std::map<int, StdSize>& maxEventSize, CContextClient* contextClient, bool bufferForWriting = false); |
---|
| 150 | std::map<int, StdSize> getDataBufferSize(std::map<int, StdSize>& maxEventSize, CContextClient* contextClient, bool bufferForWriting = false); |
---|
[1870] | 151 | void setClientServerBuffer(CContextClient* contextClient, bool bufferForWriting = false); // old interface to be removed |
---|
| 152 | void setClientServerBuffer(vector<CField*>& fields, bool bufferForWriting) ; |
---|
[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 |
---|
| 173 | void sendRegistry(void) ; |
---|
[1764] | 174 | void sendFinalizeClient(CContextClient* contextClient, const string& contextClientId); |
---|
[1870] | 175 | |
---|
| 176 | public: |
---|
| 177 | void sendContextToFileServer(CContextClient* client) ; |
---|
| 178 | private: |
---|
| 179 | std::set<CContextClient*> sendToFileServer_done_ ; |
---|
| 180 | |
---|
| 181 | public: |
---|
[1761] | 182 | std::string getContextId() {return contextId_;} |
---|
[511] | 183 | |
---|
[509] | 184 | // Client side: Receive and process messages |
---|
[549] | 185 | static void recvUpdateCalendar(CEventServer& event); |
---|
| 186 | void recvUpdateCalendar(CBufferIn& buffer); |
---|
| 187 | static void recvCloseDefinition(CEventServer& event); |
---|
| 188 | static void recvCreateFileHeader(CEventServer& event); |
---|
| 189 | void recvCreateFileHeader(CBufferIn& buffer); |
---|
[509] | 190 | static void recvSolveInheritanceContext(CEventServer& event); |
---|
| 191 | void recvSolveInheritanceContext(CBufferIn& buffer); |
---|
[697] | 192 | static void recvRegistry(CEventServer& event) ; |
---|
[983] | 193 | void recvRegistry(CBufferIn& buffer) ; //!< registry is received by the servers |
---|
[1764] | 194 | static void recvFinalizeClient(CEventServer& event) ; |
---|
| 195 | void recvFinalizeClient(CBufferIn& buffer); |
---|
[1875] | 196 | |
---|
| 197 | public: |
---|
| 198 | void sendCouplerInReady(CContextClient* client); |
---|
| 199 | private: |
---|
| 200 | set<CContextClient*> sendCouplerInReady_done_; |
---|
| 201 | public: |
---|
| 202 | static void recvCouplerInReady(CEventServer& event) ; |
---|
| 203 | void recvCouplerInReady(CBufferIn& buffer) ; //!< coupler is ready to receive grid definition. |
---|
| 204 | set<CContextClient*> couplerInReady_; |
---|
| 205 | bool isCouplerInReady(CContextClient* client) { return couplerInReady_.count(client)!=0 ;} |
---|
[1054] | 206 | |
---|
[1875] | 207 | public: |
---|
| 208 | void sendCouplerInCloseDefinition(CContextClient* client) ; |
---|
| 209 | set<CContextClient*> sendCouplerInCloseDefinition_done_; |
---|
| 210 | static void recvCouplerInCloseDefinition(CEventServer& event) ; |
---|
| 211 | void recvCouplerInCloseDefinition(CBufferIn& buffer) ; //!< coupler has finished it defintion, data can be sent |
---|
| 212 | set<CContextClient*> couplerInCloseDefinition_ ; |
---|
| 213 | bool isCouplerInCloseDefinition(CContextClient* client) { return couplerInCloseDefinition_.count(client)!=0 ;} |
---|
| 214 | |
---|
| 215 | public: |
---|
| 216 | void sendCouplerInContextFinalized(CContextClient* client) ; |
---|
| 217 | set<CContextClient*> sendCouplerInContextFinalized_done_; |
---|
| 218 | static void recvCouplerInContextFinalized(CEventServer& event) ; |
---|
| 219 | void recvCouplerInContextFinalized(CBufferIn& buffer) ; //!< coupler has finished it defintion, data can be sent |
---|
| 220 | set<CContextClient*> couplerInContextFinalized_ ; |
---|
| 221 | bool isCouplerInContextFinalized(CContextClient* client) { return couplerInContextFinalized_.count(client)!=0 ;} |
---|
| 222 | |
---|
| 223 | public: |
---|
| 224 | void freeComms(void); //!< Free internally allcoated communicators |
---|
| 225 | void releaseClientBuffers(void); //! Deallocate buffers allocated by clientContexts |
---|
| 226 | |
---|
[509] | 227 | // dispatch event |
---|
[549] | 228 | static bool dispatchEvent(CEventServer& event); |
---|
[509] | 229 | |
---|
| 230 | public: |
---|
| 231 | // Get current context |
---|
| 232 | static CContext* getCurrent(void); |
---|
| 233 | |
---|
| 234 | // Get context root |
---|
| 235 | static CContextGroup* getRoot(void); |
---|
| 236 | |
---|
| 237 | // Set current context |
---|
| 238 | static void setCurrent(const string& id); |
---|
| 239 | |
---|
| 240 | // Create new context |
---|
| 241 | static CContext* create(const string& id = ""); |
---|
| 242 | |
---|
| 243 | /// Accesseurs statiques /// |
---|
| 244 | static StdString GetName(void); |
---|
| 245 | static StdString GetDefName(void); |
---|
| 246 | static ENodeType GetType(void); |
---|
| 247 | |
---|
| 248 | static CContextGroup* GetContextGroup(void); |
---|
| 249 | |
---|
| 250 | // Some functions to visualize structure of current context |
---|
| 251 | static void ShowTree(StdOStream & out = std::clog); |
---|
| 252 | static void CleanTree(void); |
---|
[1761] | 253 | int getServiceType(void) {return serviceType_;} |
---|
[509] | 254 | |
---|
[219] | 255 | public : |
---|
[509] | 256 | // Parse xml node and write all info into context |
---|
[219] | 257 | virtual void parse(xml::CXMLNode & node); |
---|
| 258 | |
---|
[509] | 259 | // Visualize a context |
---|
[219] | 260 | virtual StdString toString(void) const; |
---|
[509] | 261 | |
---|
| 262 | |
---|
| 263 | // Solve all inheritance relation in current context |
---|
| 264 | virtual void solveDescInheritance(bool apply, const CAttributeMap * const parent = 0); |
---|
| 265 | |
---|
| 266 | // Verify if all root definition in a context have children |
---|
| 267 | virtual bool hasChild(void) const; |
---|
| 268 | |
---|
[1761] | 269 | bool isProcessingEvent(void) {return isProcessingEvent_;} |
---|
| 270 | bool setProcessingEvent(void) {isProcessingEvent_=true ;} |
---|
| 271 | bool unsetProcessingEvent(void) {isProcessingEvent_=false ;} |
---|
[1869] | 272 | MPI_Comm getIntraComm(void) {return intraComm_ ;} |
---|
| 273 | int getIntraCommRank(void) {return intraCommRank_;} |
---|
| 274 | int getIntraCommSize(void) {return intraCommSize_;} |
---|
[1054] | 275 | |
---|
[1784] | 276 | void addCouplingChanel(const std::string& contextId, bool out) ; |
---|
| 277 | |
---|
[300] | 278 | public : |
---|
[509] | 279 | // Calendar of context |
---|
[1542] | 280 | std::shared_ptr<CCalendar> calendar; |
---|
[509] | 281 | |
---|
[598] | 282 | // List of all enabled files (files on which fields are written or read) |
---|
[347] | 283 | std::vector<CFile*> enabledFiles; |
---|
[598] | 284 | // List of all enabled files in read mode (files on which fields are read) |
---|
| 285 | std::vector<CFile*> enabledReadModeFiles; |
---|
[1208] | 286 | // List of all enabled files in write mode |
---|
| 287 | std::vector<CFile*> enabledWriteModeFiles; |
---|
[509] | 288 | |
---|
[1784] | 289 | std::vector<CCouplerIn*> enabledCouplerIn; |
---|
| 290 | std::vector<CCouplerOut*> enabledCouplerOut; |
---|
| 291 | |
---|
| 292 | |
---|
[740] | 293 | // List of all enabled fields whose instant data is accessible from the public API |
---|
| 294 | // but which are not part of a file |
---|
[1869] | 295 | std::vector<CField*> fieldsWithReadAccess_; |
---|
[1875] | 296 | std::vector<CField*> couplerInFields_; |
---|
| 297 | std::vector<CField*> fileInFields_; |
---|
[593] | 298 | |
---|
[1875] | 299 | |
---|
[509] | 300 | // Context root |
---|
[1542] | 301 | static std::shared_ptr<CContextGroup> root; |
---|
[219] | 302 | |
---|
[509] | 303 | // Determine context on client or not |
---|
[549] | 304 | bool hasClient; |
---|
[300] | 305 | |
---|
[509] | 306 | // Determine context on server or not |
---|
[549] | 307 | bool hasServer; |
---|
[509] | 308 | |
---|
[1130] | 309 | CContextServer* server; //!< Concrete context server |
---|
| 310 | CContextClient* client; //!< Concrete contex client |
---|
[1071] | 311 | std::vector<CContextServer*> serverPrimServer; |
---|
| 312 | std::vector<CContextClient*> clientPrimServer; |
---|
[1784] | 313 | |
---|
[1875] | 314 | // list of slave servers (IO server or others) |
---|
| 315 | set<CContextClient*> slaveServers_ ; |
---|
| 316 | private: |
---|
| 317 | // the map containing context client associated to it string id for coupling out ; |
---|
| 318 | std::map<std::string, CContextClient*> couplerOutClient_ ; |
---|
| 319 | // the map containing context server associated to it string id for coupling out ; |
---|
| 320 | std::map<std::string, CContextServer*> couplerOutServer_ ; |
---|
| 321 | // the map containing context client associated to it string id for coupling in ; |
---|
| 322 | std::map<std::string, CContextClient*> couplerInClient_ ; |
---|
| 323 | // the map containing context server associated to it string id for coupling in ; |
---|
| 324 | std::map<std::string, CContextServer*> couplerInServer_ ; |
---|
| 325 | public: |
---|
| 326 | CContextClient* getCouplerInClient(const string& contextId) { return couplerInClient_[contextId] ;} |
---|
| 327 | CContextServer* getCouplerInServer(const string& contextId) { return couplerInServer_[contextId] ;} |
---|
| 328 | CContextClient* getCouplerOutClient(const string& contextId) { return couplerOutClient_[contextId] ;} |
---|
| 329 | CContextServer* getCouplerOutServer(const string& contextId) { return couplerOutServer_[contextId] ;} |
---|
| 330 | |
---|
| 331 | |
---|
[1761] | 332 | std::vector<std::string> primServerId_; |
---|
[1071] | 333 | |
---|
[1130] | 334 | CRegistry* registryIn ; //!< input registry which is read from file |
---|
| 335 | CRegistry* registryOut ; //!< output registry which will be written into file at the finalize |
---|
[775] | 336 | |
---|
[1761] | 337 | |
---|
| 338 | MPI_Comm intraComm_ ; //! context intra communicator |
---|
[1784] | 339 | int intraCommRank_ ; //! context intra communicator rank |
---|
| 340 | int intraCommSize_ ; //! context intra communicator size |
---|
| 341 | |
---|
[509] | 342 | private: |
---|
[2022] | 343 | CEventScheduler* eventScheduler_ ; //! The local event scheduler for context |
---|
| 344 | size_t hashId_ ; //! the local hashId for scheduler |
---|
| 345 | size_t timeLine_=0 ; |
---|
| 346 | void initEventScheduler(void) ; |
---|
| 347 | |
---|
[509] | 348 | bool isPostProcessed; |
---|
[1025] | 349 | bool allProcessed; |
---|
[1139] | 350 | bool finalized; |
---|
[1764] | 351 | int countChildContextFinalized_; //!< Counter of child contexts (for now it is the number of secondary server pools) |
---|
[639] | 352 | CGarbageCollector garbageCollector; |
---|
[1639] | 353 | std::list<MPI_Comm> comms; //!< Communicators allocated internally |
---|
[509] | 354 | |
---|
[1761] | 355 | int serviceType_; //!< service associated to the context |
---|
| 356 | string contextId_ ; //!< context client id for the servers. For clients this is same as getId() |
---|
| 357 | bool isProcessingEvent_ ; |
---|
| 358 | CServerContext* parentServerContext_ ; |
---|
| 359 | |
---|
[509] | 360 | public: // Some function maybe removed in the near future |
---|
| 361 | // virtual void toBinary (StdOStream & os) const; |
---|
| 362 | // virtual void fromBinary(StdIStream & is); |
---|
[219] | 363 | }; // class CContext |
---|
| 364 | |
---|
| 365 | ///-------------------------------------------------------------- |
---|
| 366 | |
---|
| 367 | // Declare/Define CContextGroup and CContextDefinition |
---|
| 368 | DECLARE_GROUP(CContext); |
---|
| 369 | |
---|
| 370 | ///-------------------------------------------------------------- |
---|
| 371 | |
---|
[335] | 372 | } // namespace xios |
---|
[219] | 373 | |
---|
[591] | 374 | #endif // __XIOS_CContext__ |
---|