[219] | 1 | #include "context.hpp" |
---|
[352] | 2 | #include "attribute_template.hpp" |
---|
| 3 | #include "object_template.hpp" |
---|
| 4 | #include "group_template.hpp" |
---|
[219] | 5 | |
---|
| 6 | #include "calendar_type.hpp" |
---|
[278] | 7 | #include "duration.hpp" |
---|
[219] | 8 | |
---|
[2507] | 9 | #include "online_context_client.hpp" |
---|
[2343] | 10 | #include "legacy_context_client.hpp" |
---|
| 11 | #include "legacy_context_server.hpp" |
---|
| 12 | #include "one_sided_context_client.hpp" |
---|
| 13 | #include "one_sided_context_server.hpp" |
---|
[300] | 14 | #include "nc4_data_output.hpp" |
---|
[346] | 15 | #include "node_type.hpp" |
---|
[352] | 16 | #include "message.hpp" |
---|
| 17 | #include "type.hpp" |
---|
[591] | 18 | #include "xios_spl.hpp" |
---|
[1158] | 19 | #include "timer.hpp" |
---|
[2418] | 20 | #include "mem_checker.hpp" |
---|
[1158] | 21 | #include "memtrack.hpp" |
---|
[1215] | 22 | #include <limits> |
---|
[1349] | 23 | #include <fstream> |
---|
[992] | 24 | #include "server.hpp" |
---|
[1349] | 25 | #include "distribute_file_server2.hpp" |
---|
[1761] | 26 | #include "services_manager.hpp" |
---|
| 27 | #include "contexts_manager.hpp" |
---|
| 28 | #include "cxios.hpp" |
---|
| 29 | #include "client.hpp" |
---|
[1784] | 30 | #include "coupler_in.hpp" |
---|
| 31 | #include "coupler_out.hpp" |
---|
[2022] | 32 | #include "servers_ressource.hpp" |
---|
| 33 | #include "pool_ressource.hpp" |
---|
| 34 | #include "services.hpp" |
---|
| 35 | #include "contexts_manager.hpp" |
---|
[2547] | 36 | #include "thread_manager.hpp" |
---|
[2022] | 37 | #include <chrono> |
---|
| 38 | #include <random> |
---|
[697] | 39 | |
---|
[2022] | 40 | namespace xios |
---|
| 41 | { |
---|
[509] | 42 | |
---|
[1542] | 43 | std::shared_ptr<CContextGroup> CContext::root; |
---|
[509] | 44 | |
---|
[219] | 45 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 46 | |
---|
| 47 | CContext::CContext(void) |
---|
| 48 | : CObjectTemplate<CContext>(), CContextAttributes() |
---|
[983] | 49 | , calendar(), hasClient(false), hasServer(false) |
---|
[1139] | 50 | , isPostProcessed(false), finalized(false) |
---|
[1764] | 51 | , allProcessed(false), countChildContextFinalized_(0), isProcessingEvent_(false) |
---|
[1622] | 52 | |
---|
[2405] | 53 | { /* Ne rien faire de plus */ } |
---|
[219] | 54 | |
---|
| 55 | CContext::CContext(const StdString & id) |
---|
| 56 | : CObjectTemplate<CContext>(id), CContextAttributes() |
---|
[983] | 57 | , calendar(), hasClient(false), hasServer(false) |
---|
[1139] | 58 | , isPostProcessed(false), finalized(false) |
---|
[1764] | 59 | , allProcessed(false), countChildContextFinalized_(0), isProcessingEvent_(false) |
---|
[219] | 60 | { /* Ne rien faire de plus */ } |
---|
| 61 | |
---|
| 62 | CContext::~CContext(void) |
---|
[509] | 63 | { |
---|
[2406] | 64 | for(auto& client : writerClientOut_) delete client ; |
---|
| 65 | for(auto& server : writerServerOut_) delete server ; |
---|
| 66 | |
---|
| 67 | for(auto& client : writerClientIn_) delete client ; |
---|
| 68 | for(auto& server : writerServerIn_) delete server ; |
---|
| 69 | |
---|
| 70 | for(auto& client : readerClientOut_) delete client ; |
---|
| 71 | for(auto& server : readerServerOut_) delete server ; |
---|
| 72 | |
---|
| 73 | for(auto& client : readerClientIn_) delete client ; |
---|
| 74 | for(auto& server : readerServerIn_) delete server ; |
---|
| 75 | |
---|
| 76 | |
---|
[2274] | 77 | if (registryIn!=nullptr) delete registryIn ; |
---|
| 78 | if (registryOut!=nullptr) delete registryOut ; |
---|
[2564] | 79 | |
---|
| 80 | if (onlineContextClient_!=nullptr) delete onlineContextClient_; |
---|
[300] | 81 | } |
---|
[219] | 82 | |
---|
| 83 | //---------------------------------------------------------------- |
---|
[509] | 84 | //! Get name of context |
---|
[219] | 85 | StdString CContext::GetName(void) { return (StdString("context")); } |
---|
| 86 | StdString CContext::GetDefName(void){ return (CContext::GetName()); } |
---|
| 87 | ENodeType CContext::GetType(void) { return (eContext); } |
---|
| 88 | |
---|
| 89 | //---------------------------------------------------------------- |
---|
[640] | 90 | |
---|
[2022] | 91 | void CContext::initEventScheduler(void) |
---|
| 92 | { |
---|
| 93 | SRegisterContextInfo contextInfo ; |
---|
| 94 | CXios::getContextsManager()->getContextInfo(this->getId(), contextInfo, getIntraComm()) ; |
---|
| 95 | |
---|
| 96 | eventScheduler_=CXios::getPoolRessource()->getService(contextInfo.serviceId,contextInfo.partitionId)->getEventScheduler() ; |
---|
| 97 | |
---|
| 98 | // generate unique hash for server |
---|
| 99 | auto time=chrono::system_clock::now().time_since_epoch().count() ; |
---|
| 100 | std::default_random_engine rd(time); // not reproducible from a run to another |
---|
| 101 | std::uniform_int_distribution<size_t> dist; |
---|
| 102 | hashId_=dist(rd) ; |
---|
| 103 | MPI_Bcast(&hashId_,1,MPI_SIZE_T,0,getIntraComm()) ; // Bcast to all server of the context |
---|
| 104 | } |
---|
[509] | 105 | /*! |
---|
| 106 | \brief Get context group (context root) |
---|
| 107 | \return Context root |
---|
| 108 | */ |
---|
[347] | 109 | CContextGroup* CContext::getRoot(void) |
---|
[1622] | 110 | TRY |
---|
[509] | 111 | { |
---|
[1542] | 112 | if (root.get()==NULL) root=std::shared_ptr<CContextGroup>(new CContextGroup(xml::CXMLNode::GetRootName())); |
---|
[509] | 113 | return root.get(); |
---|
[219] | 114 | } |
---|
[1622] | 115 | CATCH |
---|
[219] | 116 | |
---|
[2274] | 117 | void CContext::releaseStaticAllocation(void) |
---|
| 118 | TRY |
---|
| 119 | { |
---|
| 120 | CDomain::releaseStaticAllocation(); |
---|
| 121 | CAxis::releaseStaticAllocation(); |
---|
| 122 | CScalar::releaseStaticAllocation(); |
---|
| 123 | if (root) root.reset() ; |
---|
| 124 | } |
---|
| 125 | CATCH |
---|
| 126 | |
---|
[640] | 127 | //---------------------------------------------------------------- |
---|
[509] | 128 | |
---|
| 129 | /*! |
---|
| 130 | \brief Get calendar of a context |
---|
| 131 | \return Calendar |
---|
| 132 | */ |
---|
[1542] | 133 | std::shared_ptr<CCalendar> CContext::getCalendar(void) const |
---|
[1622] | 134 | TRY |
---|
[219] | 135 | { |
---|
| 136 | return (this->calendar); |
---|
| 137 | } |
---|
[1622] | 138 | CATCH |
---|
[509] | 139 | |
---|
[219] | 140 | //---------------------------------------------------------------- |
---|
[640] | 141 | |
---|
[509] | 142 | /*! |
---|
| 143 | \brief Set a context with a calendar |
---|
| 144 | \param[in] newCalendar new calendar |
---|
| 145 | */ |
---|
[1542] | 146 | void CContext::setCalendar(std::shared_ptr<CCalendar> newCalendar) |
---|
[1622] | 147 | TRY |
---|
[219] | 148 | { |
---|
| 149 | this->calendar = newCalendar; |
---|
| 150 | } |
---|
[1622] | 151 | CATCH_DUMP_ATTR |
---|
[509] | 152 | |
---|
[219] | 153 | //---------------------------------------------------------------- |
---|
[509] | 154 | /*! |
---|
| 155 | \brief Parse xml file and write information into context object |
---|
| 156 | \param [in] node xmld node corresponding in xml file |
---|
| 157 | */ |
---|
[219] | 158 | void CContext::parse(xml::CXMLNode & node) |
---|
[1622] | 159 | TRY |
---|
[219] | 160 | { |
---|
| 161 | CContext::SuperClass::parse(node); |
---|
| 162 | |
---|
| 163 | // PARSING POUR GESTION DES ENFANTS |
---|
[278] | 164 | xml::THashAttributes attributes = node.getAttributes(); |
---|
[219] | 165 | |
---|
[2614] | 166 | if (attributes.end() != attributes.find("src")) xml::CXMLParser::ParseInclude(attributes["src"], *this); |
---|
| 167 | |
---|
[219] | 168 | if (node.getElementName().compare(CContext::GetName())) |
---|
[421] | 169 | DEBUG("Le noeud is wrong defined but will be considered as a context !"); |
---|
[219] | 170 | |
---|
| 171 | if (!(node.goToChildElement())) |
---|
| 172 | { |
---|
| 173 | DEBUG("Le context ne contient pas d'enfant !"); |
---|
| 174 | } |
---|
| 175 | else |
---|
| 176 | { |
---|
| 177 | do { // Parcours des contextes pour traitement. |
---|
| 178 | |
---|
| 179 | StdString name = node.getElementName(); |
---|
| 180 | attributes.clear(); |
---|
| 181 | attributes = node.getAttributes(); |
---|
| 182 | |
---|
| 183 | if (attributes.end() != attributes.find("id")) |
---|
[509] | 184 | { |
---|
| 185 | DEBUG(<< "Definition node has an id," |
---|
| 186 | << "it will not be taking account !"); |
---|
| 187 | } |
---|
[219] | 188 | |
---|
| 189 | #define DECLARE_NODE(Name_, name_) \ |
---|
| 190 | if (name.compare(C##Name_##Definition::GetDefName()) == 0) \ |
---|
[549] | 191 | { C##Name_##Definition::create(C##Name_##Definition::GetDefName()) -> parse(node); continue; } |
---|
[219] | 192 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 193 | #include "node_type.conf" |
---|
[286] | 194 | |
---|
[421] | 195 | DEBUG(<< "The element \'" << name |
---|
| 196 | << "\' in the context \'" << CContext::getCurrent()->getId() |
---|
| 197 | << "\' is not a definition !"); |
---|
[219] | 198 | |
---|
| 199 | } while (node.goToNextElement()); |
---|
| 200 | |
---|
| 201 | node.goToParentElement(); // Retour au parent |
---|
| 202 | } |
---|
| 203 | } |
---|
[1622] | 204 | CATCH_DUMP_ATTR |
---|
[219] | 205 | |
---|
[2614] | 206 | |
---|
| 207 | template <> |
---|
| 208 | void CGroupTemplate<CContext, CContextGroup, CContextAttributes>::parse(xml::CXMLNode & node, bool withAttr, const std::set<StdString>& parseContextList) |
---|
| 209 | TRY |
---|
| 210 | { |
---|
| 211 | StdString name = node.getElementName(); |
---|
| 212 | xml::THashAttributes attributes = node.getAttributes(); |
---|
| 213 | CGroupTemplate<CContext, CContextGroup, CContextAttributes>::SuperClass::parse(node); |
---|
| 214 | if (attributes.end() != attributes.find("src")) xml::CXMLParser::ParseInclude(attributes["src"].c_str(), *this, parseContextList); |
---|
| 215 | |
---|
| 216 | if (!(node.goToChildElement())) |
---|
| 217 | { |
---|
| 218 | // no children ? |
---|
| 219 | } |
---|
| 220 | else |
---|
| 221 | { |
---|
| 222 | do // Parcours pour traitement. |
---|
| 223 | { |
---|
| 224 | |
---|
| 225 | StdString name = node.getElementName(); |
---|
| 226 | attributes.clear(); |
---|
| 227 | attributes = node.getAttributes(); |
---|
| 228 | |
---|
| 229 | if (name.compare(CContextGroup::GetName()) == 0) |
---|
| 230 | { |
---|
| 231 | CContextGroup contextGroup ; |
---|
| 232 | contextGroup.parse(node, true, parseContextList) ; |
---|
| 233 | continue; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | if (name.compare(CContext::GetName()) == 0) |
---|
| 237 | { |
---|
| 238 | if (attributes.end() == attributes.find("id")) |
---|
| 239 | { |
---|
| 240 | DEBUG("The context will not be processed because it is not identified (missing id)"); |
---|
| 241 | continue; |
---|
| 242 | } |
---|
| 243 | else |
---|
| 244 | { |
---|
| 245 | if (parseContextList.empty() || parseContextList.count(attributes["id"]) > 0) |
---|
| 246 | { |
---|
| 247 | CContext::setCurrent(attributes["id"]) ; |
---|
| 248 | CContext* context = CContext::create(attributes["id"]); |
---|
| 249 | context->parse(node); |
---|
| 250 | attributes.clear(); |
---|
| 251 | } |
---|
| 252 | continue; |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | DEBUG(<< "Dans le contexte \'" << CContext::getCurrent()->getId() |
---|
| 257 | << "\', un objet de type \'" << CContextGroup::GetName() |
---|
| 258 | << "\' ne peut contenir qu'un objet de type \'" << CContextGroup::GetName() |
---|
| 259 | << "\' ou de type \'" << CContext::GetName() |
---|
| 260 | << "\' (reçu : " << name << ") !"); |
---|
| 261 | |
---|
| 262 | } while (node.goToNextElement()); |
---|
| 263 | node.goToParentElement(); // Retour au parent |
---|
| 264 | } |
---|
| 265 | } |
---|
| 266 | CATCH_DUMP_ATTR |
---|
| 267 | |
---|
[219] | 268 | //---------------------------------------------------------------- |
---|
[509] | 269 | //! Show tree structure of context |
---|
[219] | 270 | void CContext::ShowTree(StdOStream & out) |
---|
[1622] | 271 | TRY |
---|
[219] | 272 | { |
---|
[549] | 273 | StdString currentContextId = CContext::getCurrent() -> getId(); |
---|
[2603] | 274 | std::map<StdString,CContext*> def_map = |
---|
| 275 | CContext::getRoot()->getChildMap(); |
---|
| 276 | std::map<StdString,CContext*>::iterator |
---|
| 277 | it = def_map.begin(), end = def_map.end(); |
---|
[219] | 278 | |
---|
| 279 | out << "<? xml version=\"1.0\" ?>" << std::endl; |
---|
| 280 | out << "<" << xml::CXMLNode::GetRootName() << " >" << std::endl; |
---|
[509] | 281 | |
---|
[219] | 282 | for (; it != end; it++) |
---|
| 283 | { |
---|
[2603] | 284 | CContext* context = it->second; |
---|
[509] | 285 | CContext::setCurrent(context->getId()); |
---|
[219] | 286 | out << *context << std::endl; |
---|
| 287 | } |
---|
[509] | 288 | |
---|
[219] | 289 | out << "</" << xml::CXMLNode::GetRootName() << " >" << std::endl; |
---|
[509] | 290 | CContext::setCurrent(currentContextId); |
---|
[219] | 291 | } |
---|
[1622] | 292 | CATCH |
---|
[346] | 293 | |
---|
[219] | 294 | //---------------------------------------------------------------- |
---|
| 295 | |
---|
[509] | 296 | //! Convert context object into string (to print) |
---|
[219] | 297 | StdString CContext::toString(void) const |
---|
[1622] | 298 | TRY |
---|
[219] | 299 | { |
---|
| 300 | StdOStringStream oss; |
---|
| 301 | oss << "<" << CContext::GetName() |
---|
| 302 | << " id=\"" << this->getId() << "\" " |
---|
| 303 | << SuperClassAttribute::toString() << ">" << std::endl; |
---|
| 304 | if (!this->hasChild()) |
---|
| 305 | { |
---|
| 306 | //oss << "<!-- No definition -->" << std::endl; // fait planter l'incrémentation |
---|
| 307 | } |
---|
| 308 | else |
---|
| 309 | { |
---|
| 310 | |
---|
| 311 | #define DECLARE_NODE(Name_, name_) \ |
---|
[346] | 312 | if (C##Name_##Definition::has(C##Name_##Definition::GetDefName())) \ |
---|
| 313 | oss << * C##Name_##Definition::get(C##Name_##Definition::GetDefName()) << std::endl; |
---|
[219] | 314 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 315 | #include "node_type.conf" |
---|
| 316 | |
---|
| 317 | } |
---|
| 318 | oss << "</" << CContext::GetName() << " >"; |
---|
| 319 | return (oss.str()); |
---|
| 320 | } |
---|
[1622] | 321 | CATCH |
---|
[219] | 322 | |
---|
| 323 | //---------------------------------------------------------------- |
---|
| 324 | |
---|
[509] | 325 | /*! |
---|
| 326 | \brief Find all inheritace among objects in a context. |
---|
| 327 | \param [in] apply (true) write attributes of parent into ones of child if they are empty |
---|
| 328 | (false) write attributes of parent into a new container of child |
---|
| 329 | \param [in] parent unused |
---|
| 330 | */ |
---|
[445] | 331 | void CContext::solveDescInheritance(bool apply, const CAttributeMap * const UNUSED(parent)) |
---|
[1622] | 332 | TRY |
---|
[219] | 333 | { |
---|
| 334 | #define DECLARE_NODE(Name_, name_) \ |
---|
[354] | 335 | if (C##Name_##Definition::has(C##Name_##Definition::GetDefName())) \ |
---|
[445] | 336 | C##Name_##Definition::get(C##Name_##Definition::GetDefName())->solveDescInheritance(apply); |
---|
[219] | 337 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 338 | #include "node_type.conf" |
---|
| 339 | } |
---|
[1622] | 340 | CATCH_DUMP_ATTR |
---|
[219] | 341 | |
---|
| 342 | //---------------------------------------------------------------- |
---|
| 343 | |
---|
[509] | 344 | //! Verify if all root definition in the context have child. |
---|
[219] | 345 | bool CContext::hasChild(void) const |
---|
[1622] | 346 | TRY |
---|
[219] | 347 | { |
---|
| 348 | return ( |
---|
| 349 | #define DECLARE_NODE(Name_, name_) \ |
---|
[346] | 350 | C##Name_##Definition::has(C##Name_##Definition::GetDefName()) || |
---|
[219] | 351 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 352 | #include "node_type.conf" |
---|
| 353 | false); |
---|
| 354 | } |
---|
[1622] | 355 | CATCH |
---|
[219] | 356 | |
---|
| 357 | //---------------------------------------------------------------- |
---|
| 358 | |
---|
| 359 | void CContext::CleanTree(void) |
---|
[1622] | 360 | TRY |
---|
[219] | 361 | { |
---|
[549] | 362 | #define DECLARE_NODE(Name_, name_) C##Name_##Definition::ClearAllAttributes(); |
---|
[219] | 363 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 364 | #include "node_type.conf" |
---|
| 365 | } |
---|
[1622] | 366 | CATCH |
---|
| 367 | |
---|
[2265] | 368 | void CContext::removeContext(const string& contextId) |
---|
| 369 | { |
---|
| 370 | #define DECLARE_NODE(Name_, name_) CObjectFactory::deleteContext< C##Name_ >(contextId); |
---|
| 371 | #define DECLARE_NODE_PAR(Name_, name_) CObjectFactory::deleteContext< C##Name_ >(contextId); |
---|
| 372 | #include "node_type.conf" |
---|
| 373 | #define DECLARE_NODE(Name_, name_) CObjectFactory::deleteContext< C##Name_##Group >(contextId); |
---|
| 374 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 375 | #include "node_type.conf" |
---|
| 376 | |
---|
| 377 | /* |
---|
| 378 | #define DECLARE_NODE(Name_, name_) CObjectFactory::dumpObjects< C##Name_##Group >(); |
---|
| 379 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 380 | #include "node_type.conf" |
---|
| 381 | |
---|
| 382 | #define DECLARE_NODE(Name_, name_) CObjectFactory::dumpObjects< C##Name_>(); |
---|
| 383 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 384 | #include "node_type.conf" |
---|
| 385 | */ |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | void CContext::removeAllContexts(void) |
---|
| 389 | { |
---|
| 390 | #define DECLARE_NODE(Name_, name_) CObjectFactory::deleteAllContexts< C##Name_ >(); |
---|
| 391 | #define DECLARE_NODE_PAR(Name_, name_) CObjectFactory::deleteAllContexts< C##Name_ >(); |
---|
| 392 | #include "node_type.conf" |
---|
| 393 | #define DECLARE_NODE(Name_, name_) CObjectFactory::deleteAllContexts< C##Name_##Group >(); |
---|
| 394 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 395 | #include "node_type.conf" |
---|
| 396 | /* |
---|
| 397 | #define DECLARE_NODE(Name_, name_) CObjectFactory::dumpObjects< C##Name_##Group >(); |
---|
| 398 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 399 | #include "node_type.conf" |
---|
| 400 | |
---|
| 401 | #define DECLARE_NODE(Name_, name_) CObjectFactory::dumpObjects< C##Name_>(); |
---|
| 402 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 403 | #include "node_type.conf" |
---|
| 404 | */ |
---|
[2274] | 405 | CObjectFactory::deleteAllContexts<CContext>() ; |
---|
| 406 | CObjectFactory::deleteAllContexts<CContextGroup>() ; |
---|
| 407 | CObjectFactory::clearCurrentContextId(); |
---|
| 408 | CGroupFactory::clearCurrentContextId(); |
---|
[2265] | 409 | } |
---|
[219] | 410 | ///--------------------------------------------------------------- |
---|
[509] | 411 | |
---|
[1761] | 412 | |
---|
[1870] | 413 | /*! |
---|
| 414 | * Compute the required buffer size to send the fields data. |
---|
| 415 | * \param maxEventSize [in/out] the size of the bigger event for each connected server |
---|
| 416 | * \param [in] contextClient |
---|
| 417 | * \param [in] bufferForWriting True if buffers are used for sending data for writing |
---|
| 418 | This flag is only true for client and server-1 for communication with server-2 |
---|
| 419 | */ |
---|
| 420 | std::map<int, StdSize> CContext::getDataBufferSize(std::map<int, StdSize>& maxEventSize, |
---|
| 421 | CContextClient* contextClient, bool bufferForWriting /*= "false"*/) |
---|
| 422 | TRY |
---|
| 423 | { |
---|
| 424 | std::map<int, StdSize> dataSize; |
---|
| 425 | |
---|
| 426 | // Find all reference domain and axis of all active fields |
---|
| 427 | std::vector<CFile*>& fileList = bufferForWriting ? this->enabledWriteModeFiles : this->enabledReadModeFiles; |
---|
| 428 | size_t numEnabledFiles = fileList.size(); |
---|
| 429 | for (size_t i = 0; i < numEnabledFiles; ++i) |
---|
| 430 | { |
---|
| 431 | CFile* file = fileList[i]; |
---|
| 432 | if (file->getContextClient() == contextClient) |
---|
| 433 | { |
---|
| 434 | std::vector<CField*> enabledFields = file->getEnabledFields(); |
---|
| 435 | size_t numEnabledFields = enabledFields.size(); |
---|
| 436 | for (size_t j = 0; j < numEnabledFields; ++j) |
---|
| 437 | { |
---|
| 438 | // const std::vector<std::map<int, StdSize> > mapSize = enabledFields[j]->getGridDataBufferSize(contextClient); |
---|
| 439 | const std::map<int, StdSize> mapSize = enabledFields[j]->getGridDataBufferSize(contextClient,bufferForWriting); |
---|
| 440 | std::map<int, StdSize>::const_iterator it = mapSize.begin(), itE = mapSize.end(); |
---|
| 441 | for (; it != itE; ++it) |
---|
| 442 | { |
---|
| 443 | // If dataSize[it->first] does not exist, it will be zero-initialized |
---|
| 444 | // so we can use it safely without checking for its existance |
---|
| 445 | if (CXios::isOptPerformance) |
---|
| 446 | dataSize[it->first] += it->second; |
---|
| 447 | else if (dataSize[it->first] < it->second) |
---|
| 448 | dataSize[it->first] = it->second; |
---|
| 449 | |
---|
| 450 | if (maxEventSize[it->first] < it->second) |
---|
| 451 | maxEventSize[it->first] = it->second; |
---|
| 452 | } |
---|
| 453 | } |
---|
| 454 | } |
---|
| 455 | } |
---|
| 456 | return dataSize; |
---|
| 457 | } |
---|
| 458 | CATCH_DUMP_ATTR |
---|
| 459 | |
---|
| 460 | /*! |
---|
| 461 | * Compute the required buffer size to send the attributes (mostly those grid related). |
---|
| 462 | * \param maxEventSize [in/out] the size of the bigger event for each connected server |
---|
| 463 | * \param [in] contextClient |
---|
| 464 | * \param [in] bufferForWriting True if buffers are used for sending data for writing |
---|
| 465 | This flag is only true for client and server-1 for communication with server-2 |
---|
| 466 | */ |
---|
| 467 | std::map<int, StdSize> CContext::getAttributesBufferSize(std::map<int, StdSize>& maxEventSize, |
---|
| 468 | CContextClient* contextClient, bool bufferForWriting /*= "false"*/) |
---|
| 469 | TRY |
---|
| 470 | { |
---|
| 471 | // As calendar attributes are sent even if there are no active files or fields, maps are initialized according the size of calendar attributes |
---|
| 472 | std::map<int, StdSize> attributesSize = CCalendarWrapper::get(CCalendarWrapper::GetDefName())->getMinimumBufferSizeForAttributes(contextClient); |
---|
| 473 | maxEventSize = CCalendarWrapper::get(CCalendarWrapper::GetDefName())->getMinimumBufferSizeForAttributes(contextClient); |
---|
| 474 | |
---|
| 475 | std::vector<CFile*>& fileList = this->enabledFiles; |
---|
| 476 | size_t numEnabledFiles = fileList.size(); |
---|
| 477 | for (size_t i = 0; i < numEnabledFiles; ++i) |
---|
| 478 | { |
---|
| 479 | // CFile* file = this->enabledWriteModeFiles[i]; |
---|
| 480 | CFile* file = fileList[i]; |
---|
| 481 | std::vector<CField*> enabledFields = file->getEnabledFields(); |
---|
| 482 | size_t numEnabledFields = enabledFields.size(); |
---|
| 483 | for (size_t j = 0; j < numEnabledFields; ++j) |
---|
| 484 | { |
---|
| 485 | const std::map<int, StdSize> mapSize = enabledFields[j]->getGridAttributesBufferSize(contextClient, bufferForWriting); |
---|
| 486 | std::map<int, StdSize>::const_iterator it = mapSize.begin(), itE = mapSize.end(); |
---|
| 487 | for (; it != itE; ++it) |
---|
| 488 | { |
---|
| 489 | // If attributesSize[it->first] does not exist, it will be zero-initialized |
---|
| 490 | // so we can use it safely without checking for its existence |
---|
| 491 | if (attributesSize[it->first] < it->second) |
---|
| 492 | attributesSize[it->first] = it->second; |
---|
| 493 | |
---|
| 494 | if (maxEventSize[it->first] < it->second) |
---|
| 495 | maxEventSize[it->first] = it->second; |
---|
| 496 | } |
---|
| 497 | } |
---|
| 498 | } |
---|
| 499 | return attributesSize; |
---|
| 500 | } |
---|
| 501 | CATCH_DUMP_ATTR |
---|
| 502 | |
---|
| 503 | |
---|
| 504 | |
---|
[509] | 505 | //! Verify whether a context is initialized |
---|
[461] | 506 | bool CContext::isInitialized(void) |
---|
[1622] | 507 | TRY |
---|
[461] | 508 | { |
---|
[549] | 509 | return hasClient; |
---|
[461] | 510 | } |
---|
[1622] | 511 | CATCH_DUMP_ATTR |
---|
[509] | 512 | |
---|
[1761] | 513 | |
---|
| 514 | void CContext::init(CServerContext* parentServerContext, MPI_Comm intraComm, int serviceType) |
---|
| 515 | TRY |
---|
| 516 | { |
---|
| 517 | parentServerContext_ = parentServerContext ; |
---|
| 518 | if (serviceType==CServicesManager::CLIENT) |
---|
| 519 | initClient(intraComm, serviceType) ; |
---|
| 520 | else |
---|
| 521 | initServer(intraComm, serviceType) ; |
---|
[2022] | 522 | initEventScheduler() ; |
---|
[1761] | 523 | } |
---|
| 524 | CATCH_DUMP_ATTR |
---|
| 525 | |
---|
| 526 | |
---|
| 527 | |
---|
| 528 | //! Initialize client side |
---|
| 529 | void CContext::initClient(MPI_Comm intraComm, int serviceType) |
---|
| 530 | TRY |
---|
| 531 | { |
---|
| 532 | intraComm_=intraComm ; |
---|
[1784] | 533 | MPI_Comm_rank(intraComm_, &intraCommRank_) ; |
---|
| 534 | MPI_Comm_size(intraComm_, &intraCommSize_) ; |
---|
| 535 | |
---|
[1761] | 536 | serviceType_ = CServicesManager::CLIENT ; |
---|
| 537 | if (serviceType_==CServicesManager::CLIENT) |
---|
| 538 | { |
---|
| 539 | hasClient=true ; |
---|
| 540 | hasServer=false ; |
---|
| 541 | } |
---|
| 542 | contextId_ = getId() ; |
---|
| 543 | |
---|
| 544 | string contextRegistryId=getId() ; |
---|
[2209] | 545 | registryIn=new CRegistry(CXios::getRegistryManager()->getRegistryIn()); |
---|
[1761] | 546 | registryIn->setPath(contextRegistryId) ; |
---|
| 547 | |
---|
| 548 | registryOut=new CRegistry(intraComm_) ; |
---|
| 549 | registryOut->setPath(contextRegistryId) ; |
---|
| 550 | |
---|
| 551 | } |
---|
| 552 | CATCH_DUMP_ATTR |
---|
| 553 | |
---|
| 554 | |
---|
| 555 | void CContext::initServer(MPI_Comm intraComm, int serviceType) |
---|
| 556 | TRY |
---|
| 557 | { |
---|
| 558 | hasServer=true; |
---|
| 559 | intraComm_=intraComm ; |
---|
[1784] | 560 | MPI_Comm_rank(intraComm_, &intraCommRank_) ; |
---|
| 561 | MPI_Comm_size(intraComm_, &intraCommSize_) ; |
---|
| 562 | |
---|
[1761] | 563 | serviceType_=serviceType ; |
---|
| 564 | |
---|
| 565 | if (serviceType_==CServicesManager::GATHERER) |
---|
| 566 | { |
---|
| 567 | hasClient=true ; |
---|
| 568 | hasServer=true ; |
---|
| 569 | } |
---|
[2407] | 570 | else if (serviceType_==CServicesManager::WRITER || serviceType_==CServicesManager::READER) |
---|
[1761] | 571 | { |
---|
| 572 | hasClient=false ; |
---|
| 573 | hasServer=true ; |
---|
| 574 | } |
---|
| 575 | |
---|
| 576 | CXios::getContextsManager()->getContextId(getId(), contextId_, intraComm) ; |
---|
| 577 | |
---|
[2209] | 578 | string contextRegistryId=getId() ; |
---|
| 579 | registryIn=new CRegistry(CXios::getRegistryManager()->getRegistryIn()); |
---|
| 580 | registryIn->setPath(contextRegistryId) ; |
---|
| 581 | |
---|
| 582 | registryOut=new CRegistry(intraComm_) ; |
---|
| 583 | registryOut->setPath(contextRegistryId) ; |
---|
[1761] | 584 | |
---|
| 585 | } |
---|
| 586 | CATCH_DUMP_ATTR |
---|
| 587 | |
---|
| 588 | |
---|
| 589 | void CContext::createClientInterComm(MPI_Comm interCommClient, MPI_Comm interCommServer) // for servers |
---|
| 590 | TRY |
---|
| 591 | { |
---|
| 592 | MPI_Comm intraCommClient ; |
---|
[2589] | 593 | xios::MPI_Comm_dup(intraComm_, &intraCommClient); |
---|
[1761] | 594 | comms.push_back(intraCommClient); |
---|
[2406] | 595 | |
---|
| 596 | CContextServer* server ; |
---|
| 597 | CContextClient* client ; |
---|
| 598 | |
---|
[2343] | 599 | server = CContextServer::getNew(this,intraComm_, interCommServer); // check if we need to dupl. intraComm_ ? |
---|
| 600 | client = CContextClient::getNew(this,intraCommClient,interCommClient); |
---|
[1853] | 601 | client->setAssociatedServer(server) ; |
---|
[2406] | 602 | server->setAssociatedClient(client) ; |
---|
[2407] | 603 | |
---|
| 604 | if (serviceType_ == CServicesManager::GATHERER || serviceType_ == CServicesManager::WRITER) |
---|
| 605 | { |
---|
| 606 | writerServerIn_.push_back(server) ; |
---|
| 607 | writerClientIn_.push_back(client) ; |
---|
| 608 | } |
---|
| 609 | else if (serviceType_ == CServicesManager::READER) |
---|
| 610 | { |
---|
| 611 | readerServerIn_.push_back(server) ; |
---|
| 612 | readerClientIn_.push_back(client) ; |
---|
| 613 | } |
---|
[1761] | 614 | } |
---|
| 615 | CATCH_DUMP_ATTR |
---|
| 616 | |
---|
[2405] | 617 | |
---|
| 618 | void CContext::createServerInterComm(const string& poolId, const string& serverId, vector<pair<string, pair<CContextClient*,CContextServer*>>>& clientServers ) |
---|
| 619 | TRY |
---|
| 620 | { |
---|
| 621 | MPI_Comm interCommClient, interCommServer ; |
---|
| 622 | int commRank ; |
---|
| 623 | MPI_Comm_rank(intraComm_,&commRank) ; |
---|
| 624 | |
---|
| 625 | int nbPartitions ; |
---|
| 626 | if (commRank==0) |
---|
| 627 | { |
---|
[2547] | 628 | while (! CXios::getServicesManager()->getServiceNbPartitions(poolId, serverId, 0, nbPartitions)) yield() ; |
---|
| 629 | for(int i=0 ; i<nbPartitions; i++) |
---|
| 630 | while (!CXios::getContextsManager()->createServerContext(poolId, serverId, i, getContextId())) yield() ; |
---|
[2405] | 631 | } |
---|
[2547] | 632 | synchronize() ; |
---|
| 633 | setCurrent(getId()) ; // getCurrent/setCurrent may be supress, it can cause a lot of trouble |
---|
[2405] | 634 | MPI_Bcast(&nbPartitions, 1, MPI_INT, 0, intraComm_) ; |
---|
| 635 | |
---|
| 636 | MPI_Comm interComm ; |
---|
| 637 | for(int i=0 ; i<nbPartitions; i++) |
---|
| 638 | { |
---|
[2588] | 639 | while (!parentServerContext_->createIntercomm(poolId, serverId, i, getContextId(), intraComm_, interCommClient, interCommServer, false)) yield() ; |
---|
[2405] | 640 | int type ; |
---|
[2547] | 641 | if (commRank==0) while (!CXios::getServicesManager()->getServiceType(poolId, serverId, 0, type)) yield(); |
---|
| 642 | synchronize() ; |
---|
[2405] | 643 | MPI_Bcast(&type,1,MPI_INT,0,intraComm_) ; |
---|
[2547] | 644 | |
---|
[2405] | 645 | string fullServerId=CXios::getContextsManager()->getServerContextName(poolId, serverId, i, type, getContextId()) ; |
---|
| 646 | |
---|
| 647 | MPI_Comm intraCommClient, intraCommServer ; |
---|
| 648 | |
---|
| 649 | intraCommClient=intraComm_ ; |
---|
[2589] | 650 | xios::MPI_Comm_dup(intraComm_, &intraCommServer) ; |
---|
[2580] | 651 | CXios::getMpiGarbageCollector().registerCommunicator(intraCommServer) ; |
---|
[2405] | 652 | |
---|
| 653 | CContextClient* client = CContextClient::getNew(this, intraCommClient, interCommClient) ; |
---|
| 654 | CContextServer* server = CContextServer::getNew(this, intraCommServer, interCommServer) ; |
---|
| 655 | client->setAssociatedServer(server) ; |
---|
| 656 | server->setAssociatedClient(client) ; |
---|
| 657 | |
---|
| 658 | clientServers.push_back({fullServerId,{client,server}}) ; |
---|
[2407] | 659 | clientsId_[client] = fullServerId ; |
---|
| 660 | serversId_[server] = fullServerId ; |
---|
[2405] | 661 | } |
---|
[2410] | 662 | setCurrent(getId()) ; // Back on main context |
---|
[2405] | 663 | } |
---|
| 664 | CATCH_DUMP_ATTR |
---|
| 665 | |
---|
[2458] | 666 | |
---|
| 667 | // obsolete |
---|
[1761] | 668 | void CContext::createServerInterComm(void) |
---|
| 669 | TRY |
---|
| 670 | { |
---|
[2405] | 671 | vector<pair<string, pair<CContextClient*,CContextServer*>>> clientServers ; |
---|
| 672 | |
---|
| 673 | if (serviceType_ == CServicesManager::CLIENT) |
---|
| 674 | { |
---|
[2547] | 675 | if (CXios::usingServer2) createServerInterComm(CXios::defaultPoolId, CXios::defaultGathererId, clientServers) ; |
---|
[2407] | 676 | else createServerInterComm(CXios::defaultPoolId, CXios::defaultWriterId, clientServers) ; |
---|
[2405] | 677 | |
---|
[2406] | 678 | writerClientOut_.push_back(clientServers[0].second.first) ; |
---|
| 679 | writerServerOut_.push_back(clientServers[0].second.second) ; |
---|
[2407] | 680 | |
---|
| 681 | clientServers.clear() ; |
---|
| 682 | |
---|
[2547] | 683 | createServerInterComm(CXios::defaultPoolId, CXios::defaultReaderId, clientServers) ; |
---|
[2407] | 684 | readerClientOut_.push_back(clientServers[0].second.first) ; |
---|
| 685 | readerServerOut_.push_back(clientServers[0].second.second) ; |
---|
| 686 | |
---|
| 687 | |
---|
[2405] | 688 | } |
---|
| 689 | else if (serviceType_ == CServicesManager::GATHERER) |
---|
| 690 | { |
---|
[2407] | 691 | createServerInterComm(CXios::defaultPoolId, CXios::defaultWriterId, clientServers) ; |
---|
[2405] | 692 | for(auto& clientServer : clientServers) |
---|
| 693 | { |
---|
[2406] | 694 | writerClientOut_.push_back(clientServer.second.first) ; |
---|
| 695 | writerServerOut_.push_back(clientServer.second.second) ; |
---|
[2405] | 696 | } |
---|
| 697 | } |
---|
| 698 | |
---|
| 699 | } |
---|
| 700 | CATCH_DUMP_ATTR |
---|
| 701 | |
---|
[2458] | 702 | |
---|
| 703 | void CContext::getServerInterComm(const string& poolId, const string& serviceId, vector<pair<CContextClient*,CContextServer*>>& clientServers) |
---|
| 704 | { |
---|
| 705 | vector<pair<string, pair<CContextClient*,CContextServer*>>> retClientServers ; |
---|
| 706 | |
---|
| 707 | auto it=serversMap_.find(make_pair(poolId,serviceId)) ; |
---|
| 708 | if (it!=serversMap_.end()) clientServers=it->second ; |
---|
| 709 | else |
---|
| 710 | { |
---|
[2547] | 711 | createServerInterComm(poolId, serviceId, retClientServers) ; |
---|
[2458] | 712 | for(auto& retClientServer : retClientServers) clientServers.push_back(retClientServer.second) ; |
---|
| 713 | |
---|
| 714 | int serviceType ; |
---|
[2547] | 715 | if (intraCommRank_==0) while(!CXios::getServicesManager()->getServiceType(poolId, serviceId, 0, serviceType)) yield(); |
---|
| 716 | synchronize() ; |
---|
[2458] | 717 | MPI_Bcast(&serviceType,1,MPI_INT,0,intraComm_) ; |
---|
| 718 | |
---|
| 719 | for(auto& clientServer : clientServers) |
---|
| 720 | { |
---|
| 721 | if (serviceType==CServicesManager::WRITER) { writerClientOut_.push_back(clientServer.first) ; writerServerOut_.push_back(clientServer.second) ; } |
---|
| 722 | else if (serviceType==CServicesManager::READER) { readerClientOut_.push_back(clientServer.first) ; readerServerOut_.push_back(clientServer.second) ; } |
---|
| 723 | else if (serviceType==CServicesManager::GATHERER) { writerClientOut_.push_back(clientServer.first) ; writerServerOut_.push_back(clientServer.second) ; } |
---|
| 724 | } |
---|
| 725 | serversMap_.insert(make_pair(make_pair(poolId,serviceId),clientServers)) ; |
---|
| 726 | } |
---|
| 727 | |
---|
| 728 | } |
---|
| 729 | |
---|
| 730 | vector<CContextClient*> CContext::getContextClient(const string& poolId, const string& serviceId) |
---|
| 731 | { |
---|
[2507] | 732 | vector<CContextClient*> ret ; |
---|
| 733 | |
---|
| 734 | if (serviceId=="attached") ret.push_back(onlineContextClient_) ; |
---|
| 735 | else |
---|
| 736 | { |
---|
| 737 | vector<pair<CContextClient*,CContextServer*>> clientServers ; |
---|
| 738 | getServerInterComm(poolId, serviceId, clientServers ) ; |
---|
| 739 | for(auto& clientServer : clientServers) ret.push_back(clientServer.first) ; |
---|
| 740 | } |
---|
[2458] | 741 | return ret ; |
---|
| 742 | } |
---|
| 743 | |
---|
| 744 | |
---|
[2130] | 745 | void CContext::globalEventLoop(void) |
---|
| 746 | { |
---|
[2547] | 747 | if (CThreadManager::isUsingThreads()) CThreadManager::yield(); |
---|
| 748 | else |
---|
| 749 | { |
---|
| 750 | lockContext() ; |
---|
| 751 | CXios::getDaemonsManager()->eventLoop() ; |
---|
| 752 | unlockContext() ; |
---|
| 753 | setCurrent(getId()) ; |
---|
| 754 | } |
---|
[2130] | 755 | } |
---|
| 756 | |
---|
[2547] | 757 | void CContext::yield(void) |
---|
| 758 | { |
---|
| 759 | if (CThreadManager::isUsingThreads()) |
---|
| 760 | { |
---|
| 761 | CThreadManager::yield(); |
---|
| 762 | setCurrent(getId()) ; |
---|
| 763 | } |
---|
| 764 | else |
---|
| 765 | { |
---|
| 766 | lockContext() ; |
---|
| 767 | CXios::getDaemonsManager()->eventLoop() ; |
---|
| 768 | unlockContext() ; |
---|
| 769 | setCurrent(getId()) ; |
---|
| 770 | } |
---|
| 771 | } |
---|
| 772 | |
---|
| 773 | void CContext::synchronize(void) |
---|
| 774 | { |
---|
| 775 | bool out, finished; |
---|
| 776 | size_t timeLine=timeLine_ ; |
---|
| 777 | |
---|
| 778 | timeLine_++ ; |
---|
| 779 | eventScheduler_->registerEvent(timeLine, hashId_) ; |
---|
| 780 | |
---|
| 781 | out = eventScheduler_->queryEvent(timeLine,hashId_) ; |
---|
| 782 | if (out) eventScheduler_->popEvent() ; |
---|
| 783 | while (!out) |
---|
| 784 | { |
---|
| 785 | yield() ; |
---|
| 786 | out = eventScheduler_->queryEvent(timeLine,hashId_) ; |
---|
| 787 | if (out) eventScheduler_->popEvent() ; |
---|
| 788 | } |
---|
| 789 | } |
---|
| 790 | |
---|
[2123] | 791 | bool CContext::scheduledEventLoop(bool enableEventsProcessing) |
---|
[1761] | 792 | { |
---|
[2123] | 793 | bool out, finished; |
---|
[2022] | 794 | size_t timeLine=timeLine_ ; |
---|
| 795 | if (serviceType_==CServicesManager::CLIENT) |
---|
[1761] | 796 | { |
---|
[2022] | 797 | timeLine_++ ; |
---|
| 798 | eventScheduler_->registerEvent(timeLine, hashId_) ; |
---|
[1761] | 799 | } |
---|
| 800 | |
---|
[2022] | 801 | do |
---|
| 802 | { |
---|
[2123] | 803 | finished=eventLoop(enableEventsProcessing) ; |
---|
[2230] | 804 | if (serviceType_==CServicesManager::CLIENT) |
---|
| 805 | { |
---|
| 806 | out = eventScheduler_->queryEvent(timeLine,hashId_) ; |
---|
| 807 | if (out) eventScheduler_->popEvent() ; |
---|
| 808 | } |
---|
| 809 | |
---|
[2123] | 810 | else out=true ; |
---|
| 811 | } while(!out) ; |
---|
[1875] | 812 | |
---|
[2123] | 813 | return finished ; |
---|
| 814 | } |
---|
[2022] | 815 | |
---|
[2123] | 816 | bool CContext::eventLoop(bool enableEventsProcessing) |
---|
| 817 | { |
---|
[2199] | 818 | bool finished(true); |
---|
[2321] | 819 | if (isLockedContext()) enableEventsProcessing=false; |
---|
[2130] | 820 | |
---|
[2124] | 821 | setCurrent(getId()) ; |
---|
| 822 | |
---|
[2406] | 823 | if (!finalized) |
---|
[2123] | 824 | { |
---|
[2406] | 825 | for(auto client : writerClientOut_) client->eventLoop(); |
---|
| 826 | for(auto server : writerServerOut_) finished &= server->eventLoop(enableEventsProcessing); |
---|
| 827 | for(auto client : writerClientIn_) client->eventLoop(); |
---|
| 828 | for(auto server : writerServerIn_) finished &= server->eventLoop(enableEventsProcessing); |
---|
[2407] | 829 | for(auto client : readerClientOut_) client->eventLoop(); |
---|
| 830 | for(auto server : readerServerOut_) finished &= server->eventLoop(enableEventsProcessing); |
---|
| 831 | for(auto client : readerClientIn_) client->eventLoop(); |
---|
| 832 | for(auto server : readerServerIn_) finished &= server->eventLoop(enableEventsProcessing); |
---|
[2406] | 833 | for(auto couplerOut : couplerOutClient_) couplerOut.second->eventLoop(); |
---|
| 834 | for(auto couplerIn : couplerInClient_) couplerIn.second->eventLoop(); |
---|
[2458] | 835 | //for(auto couplerOut : couplerOutServer_) couplerOut.second->eventLoop(enableEventsProcessing); |
---|
| 836 | //for(auto couplerIn : couplerInServer_) couplerIn.second->eventLoop(enableEventsProcessing); |
---|
| 837 | for(auto couplerOut : couplerOutServer_) couplerOut.second->eventLoop(); |
---|
| 838 | for(auto couplerIn : couplerInServer_) couplerIn.second->eventLoop(); |
---|
[2123] | 839 | } |
---|
[2124] | 840 | setCurrent(getId()) ; |
---|
[1761] | 841 | return finalized && finished ; |
---|
| 842 | } |
---|
| 843 | |
---|
[1875] | 844 | void CContext::addCouplingChanel(const std::string& fullContextId, bool out) |
---|
[1784] | 845 | { |
---|
| 846 | int contextLeader ; |
---|
| 847 | |
---|
[1875] | 848 | if (out) |
---|
| 849 | { |
---|
| 850 | if (couplerOutClient_.find(fullContextId)==couplerOutClient_.end()) |
---|
| 851 | { |
---|
[2547] | 852 | while(!CXios::getContextsManager()->getContextLeader(fullContextId, contextLeader, getIntraComm())) yield(); |
---|
| 853 | synchronize() ; |
---|
[1784] | 854 | |
---|
[1875] | 855 | MPI_Comm interComm, interCommClient, interCommServer ; |
---|
| 856 | MPI_Comm intraCommClient, intraCommServer ; |
---|
| 857 | |
---|
[2589] | 858 | xios::MPI_Intercomm_create(getIntraComm(), 0, CXios::getXiosComm(), contextLeader, 0, &interComm) ; |
---|
[1875] | 859 | |
---|
[2589] | 860 | xios::MPI_Comm_dup(intraComm_, &intraCommClient) ; |
---|
| 861 | xios::MPI_Comm_dup(intraComm_, &intraCommServer) ; |
---|
| 862 | xios::MPI_Comm_dup(interComm, &interCommClient) ; |
---|
| 863 | xios::MPI_Comm_dup(interComm, &interCommServer) ; |
---|
[2343] | 864 | CContextClient* client = CContextClient::getNew(this, intraCommClient, interCommClient); |
---|
[2603] | 865 | |
---|
[2343] | 866 | CContextServer* server = CContextServer::getNew(this, intraCommServer, interCommServer); |
---|
[1875] | 867 | client->setAssociatedServer(server) ; |
---|
| 868 | server->setAssociatedClient(client) ; |
---|
[2589] | 869 | xios::MPI_Comm_free(&interComm) ; |
---|
[1875] | 870 | couplerOutClient_[fullContextId] = client ; |
---|
| 871 | couplerOutServer_[fullContextId] = server ; |
---|
| 872 | } |
---|
| 873 | } |
---|
| 874 | else if (couplerInClient_.find(fullContextId)==couplerInClient_.end()) |
---|
| 875 | { |
---|
[2547] | 876 | while(!CXios::getContextsManager()->getContextLeader(fullContextId, contextLeader, getIntraComm())) yield() ; |
---|
| 877 | synchronize() ; |
---|
[1875] | 878 | |
---|
[1784] | 879 | MPI_Comm interComm, interCommClient, interCommServer ; |
---|
| 880 | MPI_Comm intraCommClient, intraCommServer ; |
---|
| 881 | |
---|
[2589] | 882 | xios::MPI_Intercomm_create(getIntraComm(), 0, CXios::getXiosComm(), contextLeader, 0, &interComm) ; |
---|
[1784] | 883 | |
---|
[2589] | 884 | xios::MPI_Comm_dup(intraComm_, &intraCommClient) ; |
---|
| 885 | xios::MPI_Comm_dup(intraComm_, &intraCommServer) ; |
---|
| 886 | xios::MPI_Comm_dup(interComm, &interCommServer) ; |
---|
| 887 | xios::MPI_Comm_dup(interComm, &interCommClient) ; |
---|
[2343] | 888 | CContextServer* server = CContextServer::getNew(this, intraCommServer, interCommServer); |
---|
| 889 | CContextClient* client = CContextClient::getNew(this, intraCommClient, interCommClient); |
---|
[1875] | 890 | client->setAssociatedServer(server) ; |
---|
| 891 | server->setAssociatedClient(client) ; |
---|
[2589] | 892 | xios::MPI_Comm_free(&interComm) ; |
---|
[1784] | 893 | |
---|
[1875] | 894 | couplerInClient_[fullContextId] = client ; |
---|
| 895 | couplerInServer_[fullContextId] = server ; |
---|
| 896 | } |
---|
[1784] | 897 | } |
---|
[1764] | 898 | |
---|
[1761] | 899 | void CContext::finalize(void) |
---|
| 900 | TRY |
---|
| 901 | { |
---|
[1764] | 902 | registryOut->hierarchicalGatherRegistry() ; |
---|
[2406] | 903 | if (intraCommRank_==0) CXios::getRegistryManager()->merge(*registryOut) ; |
---|
[1764] | 904 | |
---|
[1853] | 905 | if (serviceType_==CServicesManager::CLIENT) |
---|
[1761] | 906 | { |
---|
[1875] | 907 | triggerLateFields() ; |
---|
| 908 | |
---|
| 909 | // inform couplerIn that I am finished |
---|
| 910 | for(auto& couplerInClient : couplerInClient_) sendCouplerInContextFinalized(couplerInClient.second) ; |
---|
| 911 | |
---|
| 912 | // wait until received message from couplerOut that they have finished |
---|
| 913 | bool couplersInFinalized ; |
---|
| 914 | do |
---|
| 915 | { |
---|
| 916 | couplersInFinalized=true ; |
---|
| 917 | for(auto& couplerOutClient : couplerOutClient_) couplersInFinalized &= isCouplerInContextFinalized(couplerOutClient.second) ; |
---|
[2547] | 918 | if (CThreadManager::isUsingThreads()) yield() ; |
---|
| 919 | else globalEventLoop() ; |
---|
[1875] | 920 | } while (!couplersInFinalized) ; |
---|
| 921 | |
---|
[2407] | 922 | CContextClient* client ; |
---|
| 923 | CContextServer* server ; |
---|
[2406] | 924 | |
---|
[2458] | 925 | /* |
---|
[2407] | 926 | if (writerClientOut_.size()!=0) |
---|
| 927 | { |
---|
| 928 | client=writerClientOut_[0] ; |
---|
| 929 | server=writerServerOut_[0] ; |
---|
[2406] | 930 | |
---|
[2407] | 931 | info(100)<<"DEBUG: context "<<getId()<<" Send client finalize to writer"<<endl ; |
---|
| 932 | client->finalize(); |
---|
| 933 | info(100)<<"DEBUG: context "<<getId()<<" Client finalize sent to writer"<<endl ; |
---|
| 934 | while (client->havePendingRequests()) client->eventLoop(); |
---|
| 935 | info(100)<<"DEBUG: context "<<getId()<<" no pending request on writer ok"<<endl ; |
---|
| 936 | bool notifiedFinalized=false ; |
---|
| 937 | do |
---|
| 938 | { |
---|
| 939 | notifiedFinalized = client->isNotifiedFinalized() ; |
---|
| 940 | } while (!notifiedFinalized) ; |
---|
| 941 | |
---|
| 942 | server->releaseBuffers(); |
---|
| 943 | client->releaseBuffers(); |
---|
| 944 | info(100)<<"DEBUG: context "<<getId()<<" release client writer ok"<<endl ; |
---|
| 945 | } |
---|
[2458] | 946 | */ |
---|
[2407] | 947 | |
---|
[2458] | 948 | for(int n=0; n<writerClientOut_.size() ; n++) |
---|
| 949 | { |
---|
| 950 | client=writerClientOut_[n] ; |
---|
| 951 | server=writerServerOut_[n] ; |
---|
| 952 | |
---|
| 953 | info(100)<<"DEBUG: context "<<getId()<<" Send client finalize to writer"<<endl ; |
---|
| 954 | client->finalize(); |
---|
| 955 | info(100)<<"DEBUG: context "<<getId()<<" Client finalize sent to writer"<<endl ; |
---|
| 956 | bool bufferReleased; |
---|
| 957 | do |
---|
| 958 | { |
---|
| 959 | client->eventLoop(); |
---|
| 960 | bufferReleased = !client->havePendingRequests(); |
---|
| 961 | } while (!bufferReleased); |
---|
| 962 | info(100)<<"DEBUG: context "<<getId()<<" no pending request on writer ok"<<endl ; |
---|
| 963 | |
---|
| 964 | bool notifiedFinalized=false ; |
---|
| 965 | do |
---|
| 966 | { |
---|
| 967 | notifiedFinalized=client->isNotifiedFinalized() ; |
---|
| 968 | } while (!notifiedFinalized) ; |
---|
| 969 | server->releaseBuffers(); |
---|
| 970 | client->releaseBuffers(); |
---|
| 971 | info(100)<<"DEBUG: context "<<getId()<<" release client writer ok"<<endl ; |
---|
| 972 | } |
---|
| 973 | |
---|
| 974 | |
---|
[2407] | 975 | if (readerClientOut_.size()!=0) |
---|
[1761] | 976 | { |
---|
[2407] | 977 | client=readerClientOut_[0] ; |
---|
| 978 | server=readerServerOut_[0] ; |
---|
[2258] | 979 | |
---|
[2407] | 980 | info(100)<<"DEBUG: context "<<getId()<<" Send client finalize to reader"<<endl ; |
---|
| 981 | client->finalize(); |
---|
| 982 | info(100)<<"DEBUG: context "<<getId()<<" Client finalize sent to reader"<<endl ; |
---|
| 983 | while (client->havePendingRequests()) client->eventLoop(); |
---|
| 984 | info(100)<<"DEBUG: context "<<getId()<<" no pending request on reader ok"<<endl ; |
---|
| 985 | bool notifiedFinalized=false ; |
---|
| 986 | do |
---|
| 987 | { |
---|
[2605] | 988 | client->eventLoop(); |
---|
| 989 | server-> eventLoop(); |
---|
[2407] | 990 | notifiedFinalized = client->isNotifiedFinalized() ; |
---|
| 991 | } while (!notifiedFinalized) ; |
---|
[2595] | 992 | bool pendingEvent=true; |
---|
| 993 | do |
---|
| 994 | { |
---|
| 995 | server-> eventLoop(); |
---|
| 996 | pendingEvent = server->hasPendingEvent() ; |
---|
| 997 | } while (pendingEvent) ; |
---|
[2407] | 998 | |
---|
| 999 | server->releaseBuffers(); |
---|
| 1000 | client->releaseBuffers(); |
---|
| 1001 | info(100)<<"DEBUG: context "<<getId()<<" release client reader ok"<<endl ; |
---|
| 1002 | } |
---|
[2547] | 1003 | closeAllFile() ; |
---|
[1764] | 1004 | } |
---|
[1853] | 1005 | else if (serviceType_==CServicesManager::GATHERER) |
---|
[1764] | 1006 | { |
---|
[2547] | 1007 | CContextClient* client ; |
---|
| 1008 | CContextServer* server ; |
---|
| 1009 | |
---|
| 1010 | for(int n=0; n<writerClientOut_.size() ; n++) |
---|
| 1011 | { |
---|
| 1012 | client=writerClientOut_[n] ; |
---|
| 1013 | server=writerServerOut_[n] ; |
---|
| 1014 | |
---|
| 1015 | client->finalize(); |
---|
| 1016 | bool bufferReleased; |
---|
| 1017 | do |
---|
| 1018 | { |
---|
| 1019 | client->eventLoop(); |
---|
| 1020 | bufferReleased = !client->havePendingRequests(); |
---|
| 1021 | } while (!bufferReleased); |
---|
[1761] | 1022 | |
---|
[2547] | 1023 | bool notifiedFinalized=false ; |
---|
| 1024 | do |
---|
| 1025 | { |
---|
| 1026 | notifiedFinalized=client->isNotifiedFinalized() ; |
---|
| 1027 | } while (!notifiedFinalized) ; |
---|
| 1028 | server->releaseBuffers(); |
---|
| 1029 | client->releaseBuffers(); |
---|
[1761] | 1030 | } |
---|
[1764] | 1031 | closeAllFile(); |
---|
[2547] | 1032 | writerClientIn_[0]->releaseBuffers(); |
---|
| 1033 | writerServerIn_[0]->releaseBuffers(); |
---|
[2406] | 1034 | //ym writerClientIn & writerServerIn not released here ==> to check !! |
---|
[1764] | 1035 | } |
---|
[2407] | 1036 | else if (serviceType_==CServicesManager::WRITER) |
---|
[1764] | 1037 | { |
---|
| 1038 | closeAllFile(); |
---|
[2406] | 1039 | writerClientIn_[0]->releaseBuffers(); |
---|
| 1040 | writerServerIn_[0]->releaseBuffers(); |
---|
[1764] | 1041 | } |
---|
[2407] | 1042 | else if (serviceType_==CServicesManager::READER) |
---|
| 1043 | { |
---|
| 1044 | closeAllFile(); |
---|
| 1045 | readerClientIn_[0]->releaseBuffers(); |
---|
| 1046 | readerServerIn_[0]->releaseBuffers(); |
---|
| 1047 | } |
---|
[1764] | 1048 | |
---|
| 1049 | freeComms() ; |
---|
[1761] | 1050 | |
---|
[1764] | 1051 | parentServerContext_->freeComm() ; |
---|
| 1052 | finalized = true; |
---|
| 1053 | info(20)<<"CContext: Context <"<<getId()<<"> is finalized."<<endl; |
---|
[1761] | 1054 | } |
---|
[1622] | 1055 | CATCH_DUMP_ATTR |
---|
[1054] | 1056 | |
---|
[1071] | 1057 | //! Free internally allocated communicators |
---|
| 1058 | void CContext::freeComms(void) |
---|
[1622] | 1059 | TRY |
---|
[1071] | 1060 | { |
---|
[1639] | 1061 | for (std::list<MPI_Comm>::iterator it = comms.begin(); it != comms.end(); ++it) |
---|
[2589] | 1062 | xios::MPI_Comm_free(&(*it)); |
---|
[1071] | 1063 | comms.clear(); |
---|
| 1064 | } |
---|
[1622] | 1065 | CATCH_DUMP_ATTR |
---|
[509] | 1066 | |
---|
[1973] | 1067 | |
---|
[2458] | 1068 | void CContext::setDefaultServices(void) |
---|
| 1069 | { |
---|
[2547] | 1070 | if (!CXios::isUsingServer()) |
---|
| 1071 | { |
---|
| 1072 | defaultPoolWriterId_ = CXios::defaultPoolId ; |
---|
| 1073 | defaultPoolReaderId_ = CXios::defaultPoolId ; |
---|
| 1074 | defaultPoolGathererId_ = CXios::defaultPoolId ; |
---|
| 1075 | defaultWriterId_ = "attached" ; |
---|
| 1076 | defaultReaderId_ = "attached" ; |
---|
| 1077 | defaultGathererId_ = "attached" ; |
---|
| 1078 | defaultUsingServer2_ = false; |
---|
| 1079 | } |
---|
| 1080 | else |
---|
| 1081 | { |
---|
| 1082 | defaultPoolWriterId_ = CXios::defaultPoolId ; |
---|
| 1083 | defaultPoolReaderId_ = CXios::defaultPoolId ; |
---|
| 1084 | defaultPoolGathererId_ = CXios::defaultPoolId ; |
---|
| 1085 | defaultWriterId_ = CXios::defaultWriterId ; |
---|
| 1086 | defaultReaderId_ = CXios::defaultReaderId ; |
---|
| 1087 | defaultGathererId_ = CXios::defaultGathererId ; |
---|
| 1088 | defaultUsingServer2_ = CXios::usingServer2 ; |
---|
[2458] | 1089 | |
---|
[2547] | 1090 | if (!default_pool.isEmpty()) defaultPoolWriterId_ = defaultPoolReaderId_= defaultPoolGathererId_= default_pool ; |
---|
| 1091 | if (!default_pool_writer.isEmpty()) defaultPoolWriterId_ = default_pool_writer ; |
---|
| 1092 | if (!default_pool_reader.isEmpty()) defaultPoolReaderId_ = default_pool_reader ; |
---|
| 1093 | if (!default_pool_gatherer.isEmpty()) defaultPoolGathererId_ = default_pool_gatherer ; |
---|
| 1094 | if (!default_writer.isEmpty()) defaultWriterId_ = default_writer ; |
---|
| 1095 | if (!default_reader.isEmpty()) defaultWriterId_ = default_reader ; |
---|
| 1096 | if (!default_gatherer.isEmpty()) defaultGathererId_ = default_gatherer ; |
---|
| 1097 | if (!default_using_server2.isEmpty()) defaultUsingServer2_ = default_using_server2 ; |
---|
| 1098 | } |
---|
[2458] | 1099 | } |
---|
| 1100 | |
---|
[1025] | 1101 | /*! |
---|
| 1102 | \brief Close all the context defintion and do processing data |
---|
| 1103 | After everything is well defined on client side, they will be processed and sent to server |
---|
| 1104 | From the version 2.0, sever and client work no more on the same database. Moreover, client(s) will send |
---|
| 1105 | all necessary information to server, from which each server can build its own database. |
---|
| 1106 | Because the role of server is to write out field data on a specific netcdf file, |
---|
| 1107 | the only information that it needs is the enabled files |
---|
| 1108 | and the active fields (fields will be written onto active files) |
---|
| 1109 | */ |
---|
[1869] | 1110 | void CContext::closeDefinition(void) |
---|
| 1111 | TRY |
---|
| 1112 | { |
---|
[2418] | 1113 | CMemChecker::logMem( "CContext::closeDefinition" ); |
---|
| 1114 | |
---|
[1869] | 1115 | CTimer::get("Context : close definition").resume() ; |
---|
[2507] | 1116 | |
---|
| 1117 | onlineContextClient_=CContextClient::getNew<CContextClient::online>(this,intraComm_, intraComm_); |
---|
[2458] | 1118 | |
---|
[1869] | 1119 | // create intercommunicator with servers. |
---|
| 1120 | // not sure it is the good place to be called here |
---|
[2458] | 1121 | //createServerInterComm() ; |
---|
[1622] | 1122 | |
---|
[1869] | 1123 | |
---|
| 1124 | // After xml is parsed, there are some more works with post processing |
---|
| 1125 | // postProcessing(); |
---|
| 1126 | |
---|
[1871] | 1127 | |
---|
[1869] | 1128 | // Make sure the calendar was correctly created |
---|
[1871] | 1129 | if (serviceType_!=CServicesManager::CLIENT) CCalendarWrapper::get(CCalendarWrapper::GetDefName())->createCalendar(); |
---|
[1869] | 1130 | if (!calendar) |
---|
| 1131 | ERROR("CContext::postProcessing()", << "A calendar must be defined for the context \"" << getId() << "!\"") |
---|
| 1132 | else if (calendar->getTimeStep() == NoneDu) |
---|
| 1133 | ERROR("CContext::postProcessing()", << "A timestep must be defined for the context \"" << getId() << "!\"") |
---|
| 1134 | // Calendar first update to set the current date equals to the start date |
---|
| 1135 | calendar->update(0); |
---|
| 1136 | |
---|
| 1137 | // Résolution des héritages descendants (cà d des héritages de groupes) |
---|
| 1138 | // pour chacun des contextes. |
---|
| 1139 | solveDescInheritance(true); |
---|
[2458] | 1140 | setDefaultServices() ; |
---|
[1869] | 1141 | // Check if some axis, domains or grids are eligible to for compressed indexed output. |
---|
| 1142 | // Warning: This must be done after solving the inheritance and before the rest of post-processing |
---|
| 1143 | // --> later ???? checkAxisDomainsGridsEligibilityForCompressedOutput(); |
---|
| 1144 | |
---|
| 1145 | // Check if some automatic time series should be generated |
---|
| 1146 | // Warning: This must be done after solving the inheritance and before the rest of post-processing |
---|
| 1147 | |
---|
| 1148 | // The timeseries should only be prepared in client |
---|
| 1149 | prepareTimeseries(); |
---|
| 1150 | |
---|
| 1151 | //Initialisation du vecteur 'enabledFiles' contenant la liste des fichiers à sortir. |
---|
| 1152 | findEnabledFiles(); |
---|
[2337] | 1153 | |
---|
| 1154 | // Solve inheritance for field to know if enabled or not. |
---|
| 1155 | for (auto field : CField::getAll()) field->solveRefInheritance(); |
---|
| 1156 | |
---|
[1869] | 1157 | findEnabledWriteModeFiles(); |
---|
| 1158 | findEnabledReadModeFiles(); |
---|
| 1159 | findEnabledCouplerIn(); |
---|
| 1160 | findEnabledCouplerOut(); |
---|
| 1161 | createCouplerInterCommunicator() ; |
---|
| 1162 | |
---|
| 1163 | // Find all enabled fields of each file |
---|
[1870] | 1164 | vector<CField*>&& fileOutField = findAllEnabledFieldsInFileOut(this->enabledWriteModeFiles); |
---|
| 1165 | vector<CField*>&& fileInField = findAllEnabledFieldsInFileIn(this->enabledReadModeFiles); |
---|
[1875] | 1166 | vector<CField*>&& couplerOutField = findAllEnabledFieldsCouplerOut(this->enabledCouplerOut); |
---|
| 1167 | vector<CField*>&& couplerInField = findAllEnabledFieldsCouplerIn(this->enabledCouplerIn); |
---|
[1869] | 1168 | findFieldsWithReadAccess(); |
---|
[1870] | 1169 | vector<CField*>& fieldWithReadAccess = fieldsWithReadAccess_ ; |
---|
| 1170 | vector<CField*> fieldModelIn ; // fields potentially from model |
---|
[2337] | 1171 | |
---|
[1871] | 1172 | // define if files are on clientSied or serverSide |
---|
| 1173 | if (serviceType_==CServicesManager::CLIENT) |
---|
| 1174 | { |
---|
| 1175 | for (auto& file : enabledWriteModeFiles) file->setClientSide() ; |
---|
| 1176 | for (auto& file : enabledReadModeFiles) file->setClientSide() ; |
---|
| 1177 | } |
---|
| 1178 | else |
---|
| 1179 | { |
---|
| 1180 | for (auto& file : enabledWriteModeFiles) file->setServerSide() ; |
---|
| 1181 | for (auto& file : enabledReadModeFiles) file->setServerSide() ; |
---|
| 1182 | } |
---|
| 1183 | |
---|
[1875] | 1184 | |
---|
| 1185 | for (auto& field : couplerInField) |
---|
| 1186 | { |
---|
| 1187 | field->unsetGridCompleted() ; |
---|
| 1188 | } |
---|
[1869] | 1189 | // find all field potentially at workflow end |
---|
| 1190 | vector<CField*> endWorkflowFields ; |
---|
[1875] | 1191 | endWorkflowFields.reserve(fileOutField.size()+couplerOutField.size()+fieldWithReadAccess.size()) ; |
---|
[1869] | 1192 | endWorkflowFields.insert(endWorkflowFields.end(),fileOutField.begin(), fileOutField.end()) ; |
---|
[1875] | 1193 | endWorkflowFields.insert(endWorkflowFields.end(),couplerOutField.begin(), couplerOutField.end()) ; |
---|
[1869] | 1194 | endWorkflowFields.insert(endWorkflowFields.end(),fieldWithReadAccess.begin(), fieldWithReadAccess.end()) ; |
---|
| 1195 | |
---|
[1875] | 1196 | bool workflowGraphIsCompleted ; |
---|
[2022] | 1197 | |
---|
[1875] | 1198 | bool first=true ; |
---|
[2022] | 1199 | |
---|
[1875] | 1200 | do |
---|
| 1201 | { |
---|
| 1202 | workflowGraphIsCompleted=true; |
---|
| 1203 | for(auto endWorkflowField : endWorkflowFields) |
---|
| 1204 | { |
---|
| 1205 | workflowGraphIsCompleted &= endWorkflowField->buildWorkflowGraph(garbageCollector) ; |
---|
| 1206 | } |
---|
[2022] | 1207 | |
---|
[1875] | 1208 | for(auto couplerIn : enabledCouplerIn) couplerIn->assignContext() ; |
---|
| 1209 | for(auto field : couplerInField) field->makeGridAliasForCoupling(); |
---|
| 1210 | for(auto field : couplerInField) this->sendCouplerInReady(field->getContextClient()) ; |
---|
[1869] | 1211 | |
---|
[1875] | 1212 | |
---|
| 1213 | // assign context to coupler out and related fields |
---|
| 1214 | for(auto couplerOut : enabledCouplerOut) couplerOut->assignContext() ; |
---|
| 1215 | // for now supose that all coupling out endpoint are succesfull. The difficultie is client/server buffer evaluation |
---|
| 1216 | for(auto field : couplerOutField) |
---|
| 1217 | { |
---|
[1973] | 1218 | // connect to couplerOut -> to do |
---|
[1875] | 1219 | } |
---|
[2022] | 1220 | |
---|
[1875] | 1221 | bool couplersReady ; |
---|
| 1222 | do |
---|
| 1223 | { |
---|
| 1224 | couplersReady=true ; |
---|
| 1225 | for(auto field : couplerOutField) |
---|
| 1226 | { |
---|
| 1227 | bool ready = isCouplerInReady(field->getContextClient()) ; |
---|
| 1228 | if (ready) field->sendFieldToCouplerOut() ; |
---|
| 1229 | couplersReady &= ready ; |
---|
| 1230 | } |
---|
[2123] | 1231 | this->scheduledEventLoop() ; |
---|
[2022] | 1232 | |
---|
[1875] | 1233 | } while (!couplersReady) ; |
---|
[2022] | 1234 | |
---|
[1875] | 1235 | first=false ; |
---|
[2123] | 1236 | this->scheduledEventLoop() ; |
---|
[2022] | 1237 | |
---|
[1875] | 1238 | } while (!workflowGraphIsCompleted) ; |
---|
| 1239 | |
---|
[2022] | 1240 | |
---|
[1875] | 1241 | for( auto field : couplerInField) couplerInFields_.push_back(field) ; |
---|
| 1242 | |
---|
[1870] | 1243 | // get all field coming potentially from model |
---|
| 1244 | for (auto field : CField::getAll() ) if (field->getModelIn()) fieldModelIn.push_back(field) ; |
---|
| 1245 | |
---|
| 1246 | // Distribute files between secondary servers according to the data size => assign a context to a file and then to fields |
---|
[2458] | 1247 | |
---|
| 1248 | if (serviceType_==CServicesManager::CLIENT || serviceType_==CServicesManager::GATHERER) distributeFiles(this->enabledWriteModeFiles) ; |
---|
| 1249 | /* |
---|
| 1250 | if (serviceType_==CServicesManager::CLIENT ) |
---|
| 1251 | { |
---|
| 1252 | if (CXios::usingServer2) distributeFiles(this->enabledWriteModeFiles, defaultPoolGathererId_, defaultGathererId_); |
---|
| 1253 | else distributeFiles(this->enabledWriteModeFiles, defaultPoolWriterId_, defaultWriterId_); |
---|
| 1254 | } |
---|
| 1255 | if (serviceType_==CServicesManager::GATHERER ) distributeFiles(this->enabledWriteModeFiles, defaultPoolWriterId_, defaultWriterId_); |
---|
| 1256 | */ |
---|
[1869] | 1257 | |
---|
[1883] | 1258 | // client side, assign context for file reading |
---|
[2458] | 1259 | // if (serviceType_==CServicesManager::CLIENT) for(auto file : this->enabledReadModeFiles) file->setContextClient(readerClientOut_[0]) ; |
---|
| 1260 | if (serviceType_==CServicesManager::CLIENT) for(auto file : this->enabledReadModeFiles) |
---|
| 1261 | { |
---|
| 1262 | string poolReaderId ; |
---|
| 1263 | string readerId ; |
---|
| 1264 | file->getReaderServicesId(defaultPoolReaderId_, defaultReaderId_, poolReaderId, readerId) ; |
---|
| 1265 | file->setContextClient(poolReaderId, readerId, 0) ; |
---|
| 1266 | } |
---|
| 1267 | |
---|
[1883] | 1268 | // server side, assign context where to send file data read |
---|
[2407] | 1269 | if (serviceType_==CServicesManager::READER) for(auto file : this->enabledReadModeFiles) file->setContextClient(readerClientIn_[0]) ; |
---|
[1870] | 1270 | |
---|
[2397] | 1271 | |
---|
| 1272 | // workflow startpoint => data from server on client side |
---|
| 1273 | // important : sendFieldToInputFileServer must be done prior sendFieldToFileServer because for the first case the grid remoteConnectorIn |
---|
| 1274 | // and grid remoteConnectorOut will be computed, and in the second case only the remoteConnectorOut. |
---|
| 1275 | if (serviceType_==CServicesManager::CLIENT) |
---|
| 1276 | { |
---|
| 1277 | for(auto field : fileInField) |
---|
[2507] | 1278 | if (field->getContextClient()->getType() != CContextClient::online) field->sendFieldToInputFileServer() ; |
---|
| 1279 | |
---|
| 1280 | for(auto field : fileInField) |
---|
[2397] | 1281 | { |
---|
[2507] | 1282 | if (field->getContextClient()->getType() == CContextClient::online) field->connectToOnlineReader(garbageCollector) ; |
---|
| 1283 | else field->connectToServerInput(garbageCollector) ; // connect the field to server filter |
---|
[2397] | 1284 | fileInFields_.push_back(field) ; |
---|
| 1285 | } |
---|
| 1286 | } |
---|
| 1287 | |
---|
[1870] | 1288 | // workflow endpoint => sent to IO/SERVER |
---|
[1869] | 1289 | if (serviceType_==CServicesManager::CLIENT || serviceType_==CServicesManager::GATHERER) |
---|
| 1290 | { |
---|
| 1291 | for(auto field : fileOutField) |
---|
| 1292 | { |
---|
[2507] | 1293 | if (field->getContextClient()->getType() == CContextClient::online) field->connectToOnlineWriter(garbageCollector) ; |
---|
| 1294 | else field->connectToFileServer(garbageCollector) ; // connect the field to server filter |
---|
[1869] | 1295 | } |
---|
[2507] | 1296 | for(auto field : fileOutField) |
---|
| 1297 | if (field->getContextClient()->getType() != CContextClient::online) field->sendFieldToFileServer() ; |
---|
[1869] | 1298 | } |
---|
| 1299 | |
---|
[1871] | 1300 | // workflow endpoint => write to file |
---|
[2407] | 1301 | if (serviceType_==CServicesManager::WRITER) |
---|
[1871] | 1302 | { |
---|
| 1303 | for(auto field : fileOutField) |
---|
| 1304 | { |
---|
| 1305 | field->connectToFileWriter(garbageCollector) ; // connect the field to server filter |
---|
| 1306 | } |
---|
| 1307 | } |
---|
| 1308 | |
---|
| 1309 | // workflow endpoint => Send data from server to client |
---|
[2407] | 1310 | if (serviceType_==CServicesManager::READER || serviceType_==CServicesManager::GATHERER) |
---|
[1871] | 1311 | { |
---|
[1883] | 1312 | for(auto field : fileInField) |
---|
| 1313 | { |
---|
[2507] | 1314 | if (field->getContextClient()->getType() == CContextClient::online) field->connectToOnlineReader(garbageCollector) ; |
---|
| 1315 | else field->connectToServerToClient(garbageCollector) ; |
---|
[1883] | 1316 | } |
---|
[1871] | 1317 | } |
---|
[1869] | 1318 | |
---|
[1871] | 1319 | // workflow endpoint => sent to model on client side |
---|
| 1320 | if (serviceType_==CServicesManager::CLIENT) |
---|
| 1321 | { |
---|
| 1322 | for(auto field : fieldWithReadAccess) field->connectToModelOutput(garbageCollector) ; |
---|
| 1323 | } |
---|
| 1324 | |
---|
| 1325 | |
---|
[1870] | 1326 | // workflow startpoint => data from model |
---|
| 1327 | if (serviceType_==CServicesManager::CLIENT) |
---|
| 1328 | { |
---|
| 1329 | for(auto field : fieldModelIn) |
---|
| 1330 | { |
---|
| 1331 | field->connectToModelInput(garbageCollector) ; // connect the field to server filter |
---|
| 1332 | // grid index will be computed on the fly |
---|
| 1333 | } |
---|
| 1334 | } |
---|
[1871] | 1335 | |
---|
| 1336 | // workflow startpoint => data from client on server side |
---|
[2407] | 1337 | if (serviceType_==CServicesManager::WRITER || serviceType_==CServicesManager::GATHERER) |
---|
[1871] | 1338 | { |
---|
| 1339 | for(auto field : fieldModelIn) |
---|
| 1340 | { |
---|
| 1341 | field->connectToClientInput(garbageCollector) ; // connect the field to server filter |
---|
| 1342 | } |
---|
| 1343 | } |
---|
[1869] | 1344 | |
---|
[1875] | 1345 | |
---|
| 1346 | for(auto field : couplerInField) |
---|
| 1347 | { |
---|
| 1348 | field->connectToCouplerIn(garbageCollector) ; // connect the field to server filter |
---|
| 1349 | } |
---|
| 1350 | |
---|
| 1351 | |
---|
| 1352 | for(auto field : couplerOutField) |
---|
| 1353 | { |
---|
| 1354 | field->connectToCouplerOut(garbageCollector) ; // for now the same kind of filter that for file server |
---|
| 1355 | } |
---|
| 1356 | |
---|
[1871] | 1357 | // workflow startpoint => data read from file on server side |
---|
[2407] | 1358 | if (serviceType_==CServicesManager::READER) |
---|
[1871] | 1359 | { |
---|
[1883] | 1360 | for(auto field : fileInField) |
---|
| 1361 | { |
---|
| 1362 | field->connectToFileReader(garbageCollector) ; |
---|
| 1363 | } |
---|
[1871] | 1364 | } |
---|
[1875] | 1365 | |
---|
| 1366 | // construct slave server list |
---|
[2407] | 1367 | map<string, CContextClient*> slaves ; // need an ordered list ; |
---|
[2325] | 1368 | if (serviceType_==CServicesManager::CLIENT) |
---|
[1875] | 1369 | { |
---|
[2507] | 1370 | for(auto field : fileOutField) |
---|
| 1371 | if (field->getContextClient()->getType()!=CContextClient::online) slaves[clientsId_[field->getContextClient()]] = field->getContextClient() ; |
---|
| 1372 | for(auto field : fileInField) |
---|
| 1373 | if (field->getContextClient()->getType()!=CContextClient::online) slaves[clientsId_[field->getContextClient()]] = field->getContextClient() ; |
---|
[1875] | 1374 | } |
---|
[2325] | 1375 | else if (serviceType_==CServicesManager::GATHERER) |
---|
[2407] | 1376 | for(auto field : fileOutField) slaves[clientsId_[field->getContextClient()]] = field->getContextClient() ; |
---|
| 1377 | for(auto& slave : slaves) slaveServers_.push_back(slave.second) ; |
---|
[1870] | 1378 | |
---|
[2407] | 1379 | |
---|
[1875] | 1380 | for(auto& slaveServer : slaveServers_) sendCloseDefinition(slaveServer) ; |
---|
[1871] | 1381 | |
---|
[2507] | 1382 | createFileHeader(); |
---|
[1871] | 1383 | |
---|
[2507] | 1384 | //if (serviceType_==CServicesManager::CLIENT) startPrefetchingOfEnabledReadModeFiles(); |
---|
| 1385 | if (serviceType_==CServicesManager::CLIENT) |
---|
| 1386 | for(auto field : fileInField) field->sendReadDataRequest(getCalendar()->getCurrentDate()); |
---|
| 1387 | |
---|
[1875] | 1388 | // send signal to couplerIn context that definition phasis is done |
---|
[1871] | 1389 | |
---|
[1875] | 1390 | for(auto& couplerInClient : couplerInClient_) sendCouplerInCloseDefinition(couplerInClient.second) ; |
---|
[1871] | 1391 | |
---|
[1875] | 1392 | // wait until all couplerIn signal that closeDefition is done. |
---|
| 1393 | bool ok; |
---|
| 1394 | do |
---|
| 1395 | { |
---|
| 1396 | ok = true ; |
---|
| 1397 | for(auto& couplerOutClient : couplerOutClient_) ok &= isCouplerInCloseDefinition(couplerOutClient.second) ; |
---|
[2123] | 1398 | this->scheduledEventLoop() ; |
---|
[1875] | 1399 | } while (!ok) ; |
---|
[1871] | 1400 | |
---|
[2130] | 1401 | // Now evaluate the size of the context client buffers |
---|
| 1402 | map<CContextClient*,map<int,size_t>> fieldBufferEvaluation ; |
---|
| 1403 | for(auto field : fileOutField) field->evaluateBufferSize(fieldBufferEvaluation, CXios::isOptPerformance) ; // output to server |
---|
| 1404 | for(auto field : couplerOutField) field->evaluateBufferSize(fieldBufferEvaluation, CXios::isOptPerformance) ; // output to coupler |
---|
[2326] | 1405 | for(auto field : fileInField) field->evaluateBufferSize(fieldBufferEvaluation, CXios::isOptPerformance) ; // server to client (for io servers) |
---|
[2130] | 1406 | |
---|
| 1407 | // fix size for each context client |
---|
| 1408 | for(auto& it : fieldBufferEvaluation) it.first->setBufferSize(it.second) ; |
---|
| 1409 | |
---|
[2458] | 1410 | |
---|
[1973] | 1411 | CTimer::get("Context : close definition").suspend() ; |
---|
[2419] | 1412 | CMemChecker::logMem( "CContext::closeDefinition_END" ); |
---|
[1869] | 1413 | } |
---|
| 1414 | CATCH_DUMP_ATTR |
---|
| 1415 | |
---|
| 1416 | |
---|
[1973] | 1417 | vector<CField*> CContext::findAllEnabledFieldsInFileOut(const std::vector<CFile*>& activeFiles) |
---|
[1622] | 1418 | TRY |
---|
[1025] | 1419 | { |
---|
[1869] | 1420 | vector<CField*> fields ; |
---|
| 1421 | for(auto file : activeFiles) |
---|
| 1422 | { |
---|
| 1423 | const vector<CField*>&& fieldList=file->getEnabledFields() ; |
---|
| 1424 | for(auto field : fieldList) field->setFileOut(file) ; |
---|
| 1425 | fields.insert(fields.end(),fieldList.begin(),fieldList.end()); |
---|
| 1426 | } |
---|
| 1427 | return fields ; |
---|
| 1428 | } |
---|
| 1429 | CATCH_DUMP_ATTR |
---|
| 1430 | |
---|
| 1431 | vector<CField*> CContext::findAllEnabledFieldsInFileIn(const std::vector<CFile*>& activeFiles) |
---|
| 1432 | TRY |
---|
| 1433 | { |
---|
| 1434 | vector<CField*> fields ; |
---|
| 1435 | for(auto file : activeFiles) |
---|
| 1436 | { |
---|
| 1437 | const vector<CField*>&& fieldList=file->getEnabledFields() ; |
---|
| 1438 | for(auto field : fieldList) field->setFileIn(file) ; |
---|
| 1439 | fields.insert(fields.end(),fieldList.begin(),fieldList.end()); |
---|
| 1440 | } |
---|
| 1441 | return fields ; |
---|
| 1442 | } |
---|
| 1443 | CATCH_DUMP_ATTR |
---|
| 1444 | |
---|
| 1445 | vector<CField*> CContext::findAllEnabledFieldsCouplerOut(const std::vector<CCouplerOut*>& activeCouplerOut) |
---|
| 1446 | TRY |
---|
| 1447 | { |
---|
| 1448 | vector<CField*> fields ; |
---|
| 1449 | for (auto couplerOut :activeCouplerOut) |
---|
| 1450 | { |
---|
| 1451 | const vector<CField*>&& fieldList=couplerOut->getEnabledFields() ; |
---|
| 1452 | for(auto field : fieldList) field->setCouplerOut(couplerOut) ; |
---|
| 1453 | fields.insert(fields.end(),fieldList.begin(),fieldList.end()); |
---|
| 1454 | } |
---|
| 1455 | return fields ; |
---|
| 1456 | } |
---|
| 1457 | CATCH_DUMP_ATTR |
---|
| 1458 | |
---|
| 1459 | vector<CField*> CContext::findAllEnabledFieldsCouplerIn(const std::vector<CCouplerIn*>& activeCouplerIn) |
---|
| 1460 | TRY |
---|
| 1461 | { |
---|
| 1462 | vector<CField*> fields ; |
---|
| 1463 | for (auto couplerIn :activeCouplerIn) |
---|
| 1464 | { |
---|
| 1465 | const vector<CField*>&& fieldList=couplerIn->getEnabledFields() ; |
---|
| 1466 | for(auto field : fieldList) field->setCouplerIn(couplerIn) ; |
---|
| 1467 | fields.insert(fields.end(),fieldList.begin(),fieldList.end()); |
---|
| 1468 | } |
---|
| 1469 | return fields ; |
---|
| 1470 | } |
---|
| 1471 | CATCH_DUMP_ATTR |
---|
| 1472 | |
---|
[1973] | 1473 | /*! |
---|
| 1474 | * Send context attribute and calendar to file server, it must be done once by context file server |
---|
| 1475 | * \param[in] client : context client to send |
---|
| 1476 | */ |
---|
| 1477 | void CContext::sendContextToFileServer(CContextClient* client) |
---|
| 1478 | { |
---|
| 1479 | if (sendToFileServer_done_.count(client)!=0) return ; |
---|
| 1480 | else sendToFileServer_done_.insert(client) ; |
---|
| 1481 | |
---|
| 1482 | this->sendAllAttributesToServer(client); // Send all attributes of current context to server |
---|
| 1483 | CCalendarWrapper::get(CCalendarWrapper::GetDefName())->sendAllAttributesToServer(client); // Send all attributes of current cale |
---|
| 1484 | } |
---|
[1869] | 1485 | |
---|
[1973] | 1486 | |
---|
[775] | 1487 | void CContext::readAttributesOfEnabledFieldsInReadModeFiles() |
---|
[1622] | 1488 | TRY |
---|
[775] | 1489 | { |
---|
| 1490 | for (unsigned int i = 0; i < this->enabledReadModeFiles.size(); ++i) |
---|
| 1491 | (void)this->enabledReadModeFiles[i]->readAttributesOfEnabledFieldsInReadMode(); |
---|
| 1492 | } |
---|
[1622] | 1493 | CATCH_DUMP_ATTR |
---|
[775] | 1494 | |
---|
[598] | 1495 | void CContext::startPrefetchingOfEnabledReadModeFiles() |
---|
[1622] | 1496 | TRY |
---|
[598] | 1497 | { |
---|
| 1498 | int size = enabledReadModeFiles.size(); |
---|
| 1499 | for (int i = 0; i < size; ++i) |
---|
| 1500 | { |
---|
| 1501 | enabledReadModeFiles[i]->prefetchEnabledReadModeFields(); |
---|
| 1502 | } |
---|
| 1503 | } |
---|
[1622] | 1504 | CATCH_DUMP_ATTR |
---|
[598] | 1505 | |
---|
[1318] | 1506 | void CContext::doPostTimestepOperationsForEnabledReadModeFiles() |
---|
[1622] | 1507 | TRY |
---|
[598] | 1508 | { |
---|
| 1509 | int size = enabledReadModeFiles.size(); |
---|
| 1510 | for (int i = 0; i < size; ++i) |
---|
| 1511 | { |
---|
[1318] | 1512 | enabledReadModeFiles[i]->doPostTimestepOperationsForEnabledReadModeFields(); |
---|
[598] | 1513 | } |
---|
| 1514 | } |
---|
[1622] | 1515 | CATCH_DUMP_ATTR |
---|
[598] | 1516 | |
---|
[593] | 1517 | void CContext::findFieldsWithReadAccess(void) |
---|
[1622] | 1518 | TRY |
---|
[593] | 1519 | { |
---|
[1869] | 1520 | fieldsWithReadAccess_.clear(); |
---|
[593] | 1521 | const vector<CField*> allFields = CField::getAll(); |
---|
| 1522 | for (size_t i = 0; i < allFields.size(); ++i) |
---|
| 1523 | { |
---|
[740] | 1524 | CField* field = allFields[i]; |
---|
[1869] | 1525 | if (!field->read_access.isEmpty() && field->read_access && (field->enabled.isEmpty() || field->enabled)) |
---|
| 1526 | { |
---|
| 1527 | fieldsWithReadAccess_.push_back(field); |
---|
| 1528 | field->setModelOut() ; |
---|
| 1529 | } |
---|
[593] | 1530 | } |
---|
| 1531 | } |
---|
[1622] | 1532 | CATCH_DUMP_ATTR |
---|
[593] | 1533 | |
---|
[1984] | 1534 | |
---|
[445] | 1535 | void CContext::solveAllInheritance(bool apply) |
---|
[1622] | 1536 | TRY |
---|
[300] | 1537 | { |
---|
| 1538 | // Résolution des héritages descendants (cà d des héritages de groupes) |
---|
| 1539 | // pour chacun des contextes. |
---|
[445] | 1540 | solveDescInheritance(apply); |
---|
[300] | 1541 | |
---|
| 1542 | // Résolution des héritages par référence au niveau des fichiers. |
---|
[549] | 1543 | const vector<CFile*> allFiles=CFile::getAll(); |
---|
[1784] | 1544 | const vector<CCouplerIn*> allCouplerIn=CCouplerIn::getAll(); |
---|
| 1545 | const vector<CCouplerOut*> allCouplerOut=CCouplerOut::getAll(); |
---|
[540] | 1546 | const vector<CGrid*> allGrids= CGrid::getAll(); |
---|
[300] | 1547 | |
---|
[1853] | 1548 | if (serviceType_==CServicesManager::CLIENT) |
---|
[540] | 1549 | { |
---|
[510] | 1550 | for (unsigned int i = 0; i < allFiles.size(); i++) |
---|
| 1551 | allFiles[i]->solveFieldRefInheritance(apply); |
---|
[1784] | 1552 | |
---|
| 1553 | for (unsigned int i = 0; i < allCouplerIn.size(); i++) |
---|
| 1554 | allCouplerIn[i]->solveFieldRefInheritance(apply); |
---|
| 1555 | |
---|
| 1556 | for (unsigned int i = 0; i < allCouplerOut.size(); i++) |
---|
| 1557 | allCouplerOut[i]->solveFieldRefInheritance(apply); |
---|
[540] | 1558 | } |
---|
| 1559 | |
---|
| 1560 | unsigned int vecSize = allGrids.size(); |
---|
| 1561 | unsigned int i = 0; |
---|
| 1562 | for (i = 0; i < vecSize; ++i) |
---|
[1869] | 1563 | allGrids[i]->solveElementsRefInheritance(apply); |
---|
[540] | 1564 | |
---|
[300] | 1565 | } |
---|
[1622] | 1566 | CATCH_DUMP_ATTR |
---|
[300] | 1567 | |
---|
| 1568 | void CContext::findEnabledFiles(void) |
---|
[1622] | 1569 | TRY |
---|
[300] | 1570 | { |
---|
[347] | 1571 | const std::vector<CFile*> allFiles = CFile::getAll(); |
---|
[1158] | 1572 | const CDate& initDate = calendar->getInitDate(); |
---|
[300] | 1573 | |
---|
| 1574 | for (unsigned int i = 0; i < allFiles.size(); i++) |
---|
| 1575 | if (!allFiles[i]->enabled.isEmpty()) // Si l'attribut 'enabled' est défini. |
---|
[430] | 1576 | { |
---|
[300] | 1577 | if (allFiles[i]->enabled.getValue()) // Si l'attribut 'enabled' est fixé à vrai. |
---|
[1158] | 1578 | { |
---|
[1489] | 1579 | if (allFiles[i]->output_freq.isEmpty()) |
---|
[1488] | 1580 | { |
---|
| 1581 | ERROR("CContext::findEnabledFiles()", |
---|
| 1582 | << "Mandatory attribute output_freq must be defined for file \""<<allFiles[i]->getFileOutputName() |
---|
| 1583 | <<" \".") |
---|
| 1584 | } |
---|
[1158] | 1585 | if ((initDate + allFiles[i]->output_freq.getValue()) < (initDate + this->getCalendar()->getTimeStep())) |
---|
| 1586 | { |
---|
| 1587 | error(0)<<"WARNING: void CContext::findEnabledFiles()"<<endl |
---|
| 1588 | << "Output frequency in file \""<<allFiles[i]->getFileOutputName() |
---|
| 1589 | <<"\" is less than the time step. File will not be written."<<endl; |
---|
| 1590 | } |
---|
| 1591 | else |
---|
[300] | 1592 | enabledFiles.push_back(allFiles[i]); |
---|
[1158] | 1593 | } |
---|
[2337] | 1594 | else // Si l'attribut 'enabled' est fixé à faux. |
---|
| 1595 | { |
---|
| 1596 | // disabled all fields contained in file (used in findFieldsWithReadAccess through field_ref dependencies) |
---|
| 1597 | const vector<CField*>&& fieldList=allFiles[i]->getEnabledFields() ; |
---|
| 1598 | for(auto field : fieldList) field->enabled.setValue(false); |
---|
| 1599 | } |
---|
[430] | 1600 | } |
---|
[1158] | 1601 | else |
---|
| 1602 | { |
---|
[1489] | 1603 | if (allFiles[i]->output_freq.isEmpty()) |
---|
[1488] | 1604 | { |
---|
| 1605 | ERROR("CContext::findEnabledFiles()", |
---|
| 1606 | << "Mandatory attribute output_freq must be defined for file \""<<allFiles[i]->getFileOutputName() |
---|
| 1607 | <<" \".") |
---|
| 1608 | } |
---|
[1158] | 1609 | if ( (initDate + allFiles[i]->output_freq.getValue()) < (initDate + this->getCalendar()->getTimeStep())) |
---|
| 1610 | { |
---|
| 1611 | error(0)<<"WARNING: void CContext::findEnabledFiles()"<<endl |
---|
| 1612 | << "Output frequency in file \""<<allFiles[i]->getFileOutputName() |
---|
| 1613 | <<"\" is less than the time step. File will not be written."<<endl; |
---|
| 1614 | } |
---|
| 1615 | else |
---|
| 1616 | enabledFiles.push_back(allFiles[i]); // otherwise true by default |
---|
| 1617 | } |
---|
[300] | 1618 | |
---|
| 1619 | if (enabledFiles.size() == 0) |
---|
| 1620 | DEBUG(<<"Aucun fichier ne va être sorti dans le contexte nommé \"" |
---|
| 1621 | << getId() << "\" !"); |
---|
[1021] | 1622 | |
---|
[300] | 1623 | } |
---|
[1622] | 1624 | CATCH_DUMP_ATTR |
---|
[300] | 1625 | |
---|
[1784] | 1626 | void CContext::findEnabledCouplerIn(void) |
---|
| 1627 | TRY |
---|
| 1628 | { |
---|
| 1629 | const std::vector<CCouplerIn*> allCouplerIn = CCouplerIn::getAll(); |
---|
| 1630 | bool enabled ; |
---|
| 1631 | for (size_t i = 0; i < allCouplerIn.size(); i++) |
---|
| 1632 | { |
---|
| 1633 | if (allCouplerIn[i]->enabled.isEmpty()) enabled=true ; |
---|
| 1634 | else enabled=allCouplerIn[i]->enabled ; |
---|
| 1635 | if (enabled) enabledCouplerIn.push_back(allCouplerIn[i]) ; |
---|
| 1636 | } |
---|
| 1637 | } |
---|
| 1638 | CATCH_DUMP_ATTR |
---|
| 1639 | |
---|
| 1640 | void CContext::findEnabledCouplerOut(void) |
---|
| 1641 | TRY |
---|
| 1642 | { |
---|
| 1643 | const std::vector<CCouplerOut*> allCouplerOut = CCouplerOut::getAll(); |
---|
| 1644 | bool enabled ; |
---|
| 1645 | for (size_t i = 0; i < allCouplerOut.size(); i++) |
---|
| 1646 | { |
---|
| 1647 | if (allCouplerOut[i]->enabled.isEmpty()) enabled=true ; |
---|
| 1648 | else enabled=allCouplerOut[i]->enabled ; |
---|
| 1649 | if (enabled) enabledCouplerOut.push_back(allCouplerOut[i]) ; |
---|
| 1650 | } |
---|
| 1651 | } |
---|
| 1652 | CATCH_DUMP_ATTR |
---|
| 1653 | |
---|
| 1654 | |
---|
| 1655 | |
---|
| 1656 | |
---|
[1869] | 1657 | void CContext::distributeFiles(const vector<CFile*>& files) |
---|
[1622] | 1658 | TRY |
---|
[1208] | 1659 | { |
---|
[2458] | 1660 | map< pair<string,string>, vector<CFile*>> fileMaps ; |
---|
| 1661 | for(auto& file : files) |
---|
| 1662 | { |
---|
| 1663 | string poolWriterId ; |
---|
| 1664 | string poolGathererId ; |
---|
| 1665 | string writerId ; |
---|
| 1666 | string gathererId ; |
---|
| 1667 | bool usingServer2 ; |
---|
| 1668 | |
---|
| 1669 | file->getWriterServicesId(defaultUsingServer2_, defaultPoolWriterId_, defaultWriterId_, defaultPoolGathererId_, defaultGathererId_, |
---|
| 1670 | usingServer2, poolWriterId, writerId, poolGathererId, gathererId) ; |
---|
| 1671 | if (serviceType_==CServicesManager::CLIENT && usingServer2) fileMaps[make_pair(poolGathererId,gathererId)].push_back(file) ; |
---|
| 1672 | else fileMaps[make_pair(poolWriterId,writerId)].push_back(file) ; |
---|
| 1673 | } |
---|
| 1674 | for(auto& it : fileMaps) distributeFilesOnSameService(it.second, it.first.first, it.first.second) ; |
---|
| 1675 | } |
---|
| 1676 | CATCH_DUMP_ATTR |
---|
| 1677 | |
---|
| 1678 | |
---|
| 1679 | void CContext::distributeFilesOnSameService(const vector<CFile*>& files, const string& poolId, const string& serviceId) |
---|
| 1680 | TRY |
---|
| 1681 | { |
---|
[1349] | 1682 | bool distFileMemory=false ; |
---|
| 1683 | distFileMemory=CXios::getin<bool>("server2_dist_file_memory", distFileMemory); |
---|
| 1684 | |
---|
[2458] | 1685 | auto writers = getContextClient(poolId, serviceId) ; |
---|
| 1686 | int nbPools = writers.size() ; |
---|
| 1687 | |
---|
| 1688 | if (nbPools==1) distributeFileOverOne(files, poolId, serviceId) ; |
---|
| 1689 | else if (distFileMemory) distributeFileOverMemoryBandwith(files, poolId, serviceId) ; |
---|
| 1690 | else distributeFileOverBandwith(files, poolId, serviceId) ; |
---|
[1349] | 1691 | } |
---|
[1622] | 1692 | CATCH_DUMP_ATTR |
---|
[1349] | 1693 | |
---|
[2458] | 1694 | void CContext::distributeFileOverOne(const vector<CFile*>& files, const string& poolId, const string& serviceId) |
---|
[2406] | 1695 | TRY |
---|
| 1696 | { |
---|
[2458] | 1697 | for(auto& file : files) file->setContextClient(poolId, serviceId,0) ; |
---|
[2406] | 1698 | } |
---|
| 1699 | CATCH_DUMP_ATTR |
---|
| 1700 | |
---|
[2458] | 1701 | void CContext::distributeFileOverBandwith(const vector<CFile*>& files, const string& poolId, const string& serviceId) |
---|
[1622] | 1702 | TRY |
---|
[1349] | 1703 | { |
---|
[1215] | 1704 | double eps=std::numeric_limits<double>::epsilon()*10 ; |
---|
| 1705 | |
---|
[1869] | 1706 | std::ofstream ofs(("distribute_file_"+getId()+".dat").c_str(), std::ofstream::out); |
---|
[2458] | 1707 | auto writers = getContextClient(poolId, serviceId) ; |
---|
| 1708 | int nbPools = writers.size(); |
---|
| 1709 | //int nbPools = writerClientOut_.size(); |
---|
[1212] | 1710 | |
---|
[1869] | 1711 | // (1) Find all enabled files in write mode |
---|
| 1712 | // for (int i = 0; i < this->enabledFiles.size(); ++i) |
---|
| 1713 | // { |
---|
| 1714 | // if (enabledFiles[i]->mode.isEmpty() || (!enabledFiles[i]->mode.isEmpty() && enabledFiles[i]->mode.getValue() == CFile::mode_attr::write )) |
---|
| 1715 | // enabledWriteModeFiles.push_back(enabledFiles[i]); |
---|
| 1716 | // } |
---|
[1208] | 1717 | |
---|
[1869] | 1718 | // (2) Estimate the data volume for each file |
---|
| 1719 | int size = files.size(); |
---|
| 1720 | std::vector<std::pair<double, CFile*> > dataSizeMap; |
---|
| 1721 | double dataPerPool = 0; |
---|
| 1722 | int nfield=0 ; |
---|
| 1723 | ofs<<size<<endl ; |
---|
| 1724 | for (size_t i = 0; i < size; ++i) |
---|
| 1725 | { |
---|
| 1726 | CFile* file = files[i]; |
---|
| 1727 | ofs<<file->getId()<<endl ; |
---|
| 1728 | StdSize dataSize=0; |
---|
| 1729 | std::vector<CField*> enabledFields = file->getEnabledFields(); |
---|
| 1730 | size_t numEnabledFields = enabledFields.size(); |
---|
| 1731 | ofs<<numEnabledFields<<endl ; |
---|
| 1732 | for (size_t j = 0; j < numEnabledFields; ++j) |
---|
[1208] | 1733 | { |
---|
[1869] | 1734 | dataSize += enabledFields[j]->getGlobalWrittenSize() ; |
---|
| 1735 | ofs<<enabledFields[j]->getGrid()->getId()<<endl ; |
---|
| 1736 | ofs<<enabledFields[j]->getGlobalWrittenSize()<<endl ; |
---|
| 1737 | } |
---|
| 1738 | double outFreqSec = (Time)(calendar->getCurrentDate()+file->output_freq)-(Time)(calendar->getCurrentDate()) ; |
---|
| 1739 | double dataSizeSec= dataSize/ outFreqSec; |
---|
| 1740 | ofs<<dataSizeSec<<endl ; |
---|
| 1741 | nfield++ ; |
---|
[1215] | 1742 | // add epsilon*nField to dataSizeSec in order to preserve reproductive ordering when sorting |
---|
[1869] | 1743 | dataSizeMap.push_back(make_pair(dataSizeSec + dataSizeSec * eps * nfield , file)); |
---|
| 1744 | dataPerPool += dataSizeSec; |
---|
| 1745 | } |
---|
| 1746 | dataPerPool /= nbPools; |
---|
| 1747 | std::sort(dataSizeMap.begin(), dataSizeMap.end()); |
---|
[1208] | 1748 | |
---|
[1869] | 1749 | // (3) Assign contextClient to each enabled file |
---|
[1215] | 1750 | |
---|
[1869] | 1751 | std::multimap<double,int> poolDataSize ; |
---|
[1215] | 1752 | // multimap is not garanty to preserve stable sorting in c++98 but it seems it does for c++11 |
---|
| 1753 | |
---|
[1869] | 1754 | int j; |
---|
| 1755 | double dataSize ; |
---|
| 1756 | for (j = 0 ; j < nbPools ; ++j) poolDataSize.insert(std::pair<double,int>(0.,j)) ; |
---|
| 1757 | |
---|
| 1758 | for (int i = dataSizeMap.size()-1; i >= 0; --i) |
---|
[1208] | 1759 | { |
---|
[1869] | 1760 | dataSize=(*poolDataSize.begin()).first ; |
---|
| 1761 | j=(*poolDataSize.begin()).second ; |
---|
[2458] | 1762 | dataSizeMap[i].second->setContextClient(poolId, serviceId, j); |
---|
[1869] | 1763 | dataSize+=dataSizeMap[i].first; |
---|
| 1764 | poolDataSize.erase(poolDataSize.begin()) ; |
---|
| 1765 | poolDataSize.insert(std::pair<double,int>(dataSize,j)) ; |
---|
[1208] | 1766 | } |
---|
[1869] | 1767 | |
---|
| 1768 | for (std::multimap<double,int>:: iterator it=poolDataSize.begin() ; it!=poolDataSize.end(); ++it) info(30)<<"Load Balancing for servers (perfect=1) : "<<it->second<<" : ratio "<<it->first*1./dataPerPool<<endl ; |
---|
[1208] | 1769 | } |
---|
[1622] | 1770 | CATCH_DUMP_ATTR |
---|
[1208] | 1771 | |
---|
[2458] | 1772 | void CContext::distributeFileOverMemoryBandwith(const vector<CFile*>& filesList, const string& poolId, const string& serviceId) |
---|
[1622] | 1773 | TRY |
---|
[1349] | 1774 | { |
---|
[2458] | 1775 | auto writers = getContextClient(poolId, serviceId) ; |
---|
| 1776 | int nbPools = writers.size(); |
---|
| 1777 | |
---|
[1869] | 1778 | double ratio=0.5 ; |
---|
| 1779 | ratio=CXios::getin<double>("server2_dist_file_memory_ratio", ratio); |
---|
[1349] | 1780 | |
---|
[1869] | 1781 | int nFiles = filesList.size(); |
---|
| 1782 | vector<SDistFile> files(nFiles); |
---|
| 1783 | vector<SDistGrid> grids; |
---|
| 1784 | map<string,int> gridMap ; |
---|
| 1785 | string gridId; |
---|
| 1786 | int gridIndex=0 ; |
---|
[1349] | 1787 | |
---|
[1869] | 1788 | for (size_t i = 0; i < nFiles; ++i) |
---|
| 1789 | { |
---|
| 1790 | StdSize dataSize=0; |
---|
| 1791 | CFile* file = filesList[i]; |
---|
| 1792 | std::vector<CField*> enabledFields = file->getEnabledFields(); |
---|
| 1793 | size_t numEnabledFields = enabledFields.size(); |
---|
[1349] | 1794 | |
---|
[1869] | 1795 | files[i].id_=file->getId() ; |
---|
| 1796 | files[i].nbGrids_=numEnabledFields; |
---|
| 1797 | files[i].assignedGrid_ = new int[files[i].nbGrids_] ; |
---|
[1349] | 1798 | |
---|
[1869] | 1799 | for (size_t j = 0; j < numEnabledFields; ++j) |
---|
| 1800 | { |
---|
| 1801 | gridId=enabledFields[j]->getGrid()->getId() ; |
---|
| 1802 | if (gridMap.find(gridId)==gridMap.end()) |
---|
[1349] | 1803 | { |
---|
[1869] | 1804 | gridMap[gridId]=gridIndex ; |
---|
| 1805 | SDistGrid newGrid; |
---|
| 1806 | grids.push_back(newGrid) ; |
---|
| 1807 | gridIndex++ ; |
---|
[1349] | 1808 | } |
---|
[1869] | 1809 | files[i].assignedGrid_[j]=gridMap[gridId] ; |
---|
| 1810 | grids[files[i].assignedGrid_[j]].size_=enabledFields[j]->getGlobalWrittenSize() ; |
---|
| 1811 | dataSize += enabledFields[j]->getGlobalWrittenSize() ; // usefull |
---|
[1349] | 1812 | } |
---|
[1869] | 1813 | double outFreqSec = (Time)(calendar->getCurrentDate()+file->output_freq)-(Time)(calendar->getCurrentDate()) ; |
---|
| 1814 | files[i].bandwith_= dataSize/ outFreqSec ; |
---|
| 1815 | } |
---|
[1349] | 1816 | |
---|
[1869] | 1817 | double bandwith=0 ; |
---|
| 1818 | double memory=0 ; |
---|
[1349] | 1819 | |
---|
[1869] | 1820 | for(int i=0; i<nFiles; i++) bandwith+=files[i].bandwith_ ; |
---|
| 1821 | for(int i=0; i<nFiles; i++) files[i].bandwith_ = files[i].bandwith_/bandwith * ratio ; |
---|
[1349] | 1822 | |
---|
[1869] | 1823 | for(int i=0; i<grids.size(); i++) memory+=grids[i].size_ ; |
---|
| 1824 | for(int i=0; i<grids.size(); i++) grids[i].size_ = grids[i].size_ / memory * (1.0-ratio) ; |
---|
[1349] | 1825 | |
---|
[1869] | 1826 | distributeFileOverServer2(nbPools, grids.size(), &grids[0], nFiles, &files[0]) ; |
---|
[1349] | 1827 | |
---|
[1869] | 1828 | vector<double> memorySize(nbPools,0.) ; |
---|
| 1829 | vector< set<int> > serverGrids(nbPools) ; |
---|
| 1830 | vector<double> bandwithSize(nbPools,0.) ; |
---|
[1349] | 1831 | |
---|
[1869] | 1832 | for (size_t i = 0; i < nFiles; ++i) |
---|
| 1833 | { |
---|
| 1834 | bandwithSize[files[i].assignedServer_] += files[i].bandwith_* bandwith /ratio ; |
---|
| 1835 | for(int j=0 ; j<files[i].nbGrids_;j++) |
---|
[1349] | 1836 | { |
---|
[1869] | 1837 | if (serverGrids[files[i].assignedServer_].find(files[i].assignedGrid_[j]) == serverGrids[files[i].assignedServer_].end()) |
---|
[1349] | 1838 | { |
---|
[1869] | 1839 | memorySize[files[i].assignedServer_]+= grids[files[i].assignedGrid_[j]].size_ * memory / (1.0-ratio); |
---|
| 1840 | serverGrids[files[i].assignedServer_].insert(files[i].assignedGrid_[j]) ; |
---|
[1349] | 1841 | } |
---|
| 1842 | } |
---|
[2458] | 1843 | filesList[i]->setContextClient(poolId, serviceId, files[i].assignedServer_) ; |
---|
[1869] | 1844 | delete [] files[i].assignedGrid_ ; |
---|
| 1845 | } |
---|
[1349] | 1846 | |
---|
[1869] | 1847 | for (int i = 0; i < nbPools; ++i) info(100)<<"Pool server level2 "<<i<<" assigned file bandwith "<<bandwithSize[i]*86400.*4./1024/1024.<<" Mb / days"<<endl ; |
---|
| 1848 | for (int i = 0; i < nbPools; ++i) info(100)<<"Pool server level2 "<<i<<" assigned grid memory "<<memorySize[i]*100/1024./1024.<<" Mb"<<endl ; |
---|
[1349] | 1849 | |
---|
| 1850 | } |
---|
[1622] | 1851 | CATCH_DUMP_ATTR |
---|
[1349] | 1852 | |
---|
[1232] | 1853 | /*! |
---|
| 1854 | Find all files in write mode |
---|
| 1855 | */ |
---|
| 1856 | void CContext::findEnabledWriteModeFiles(void) |
---|
[1622] | 1857 | TRY |
---|
[1232] | 1858 | { |
---|
| 1859 | int size = this->enabledFiles.size(); |
---|
| 1860 | for (int i = 0; i < size; ++i) |
---|
| 1861 | { |
---|
| 1862 | if (enabledFiles[i]->mode.isEmpty() || |
---|
| 1863 | (!enabledFiles[i]->mode.isEmpty() && enabledFiles[i]->mode.getValue() == CFile::mode_attr::write )) |
---|
| 1864 | enabledWriteModeFiles.push_back(enabledFiles[i]); |
---|
| 1865 | } |
---|
| 1866 | } |
---|
[1622] | 1867 | CATCH_DUMP_ATTR |
---|
[1232] | 1868 | |
---|
| 1869 | /*! |
---|
| 1870 | Find all files in read mode |
---|
| 1871 | */ |
---|
[598] | 1872 | void CContext::findEnabledReadModeFiles(void) |
---|
[1622] | 1873 | TRY |
---|
[598] | 1874 | { |
---|
| 1875 | int size = this->enabledFiles.size(); |
---|
| 1876 | for (int i = 0; i < size; ++i) |
---|
| 1877 | { |
---|
| 1878 | if (!enabledFiles[i]->mode.isEmpty() && enabledFiles[i]->mode.getValue() == CFile::mode_attr::read) |
---|
| 1879 | enabledReadModeFiles.push_back(enabledFiles[i]); |
---|
| 1880 | } |
---|
| 1881 | } |
---|
[1622] | 1882 | CATCH_DUMP_ATTR |
---|
[598] | 1883 | |
---|
[300] | 1884 | void CContext::closeAllFile(void) |
---|
[1622] | 1885 | TRY |
---|
[300] | 1886 | { |
---|
[347] | 1887 | std::vector<CFile*>::const_iterator |
---|
[300] | 1888 | it = this->enabledFiles.begin(), end = this->enabledFiles.end(); |
---|
[509] | 1889 | |
---|
[300] | 1890 | for (; it != end; it++) |
---|
| 1891 | { |
---|
| 1892 | info(30)<<"Closing File : "<<(*it)->getId()<<endl; |
---|
| 1893 | (*it)->close(); |
---|
| 1894 | } |
---|
| 1895 | } |
---|
[1622] | 1896 | CATCH_DUMP_ATTR |
---|
[509] | 1897 | |
---|
| 1898 | /*! |
---|
| 1899 | \brief Dispatch event received from client |
---|
| 1900 | Whenever a message is received in buffer of server, it will be processed depending on |
---|
| 1901 | its event type. A new event type should be added in the switch list to make sure |
---|
| 1902 | it processed on server side. |
---|
| 1903 | \param [in] event: Received message |
---|
| 1904 | */ |
---|
[300] | 1905 | bool CContext::dispatchEvent(CEventServer& event) |
---|
[1622] | 1906 | TRY |
---|
[300] | 1907 | { |
---|
[509] | 1908 | |
---|
[549] | 1909 | if (SuperClass::dispatchEvent(event)) return true; |
---|
[300] | 1910 | else |
---|
| 1911 | { |
---|
| 1912 | switch(event.type) |
---|
| 1913 | { |
---|
| 1914 | case EVENT_ID_CLOSE_DEFINITION : |
---|
[549] | 1915 | recvCloseDefinition(event); |
---|
| 1916 | return true; |
---|
| 1917 | break; |
---|
[584] | 1918 | case EVENT_ID_UPDATE_CALENDAR: |
---|
[549] | 1919 | recvUpdateCalendar(event); |
---|
| 1920 | return true; |
---|
| 1921 | break; |
---|
[1875] | 1922 | case EVENT_ID_COUPLER_IN_READY: |
---|
| 1923 | recvCouplerInReady(event); |
---|
| 1924 | return true; |
---|
| 1925 | break; |
---|
| 1926 | case EVENT_ID_COUPLER_IN_CLOSE_DEFINITION: |
---|
| 1927 | recvCouplerInCloseDefinition(event); |
---|
| 1928 | return true; |
---|
| 1929 | break; |
---|
| 1930 | case EVENT_ID_COUPLER_IN_CONTEXT_FINALIZED: |
---|
| 1931 | recvCouplerInContextFinalized(event); |
---|
| 1932 | return true; |
---|
| 1933 | break; |
---|
[300] | 1934 | default : |
---|
| 1935 | ERROR("bool CContext::dispatchEvent(CEventServer& event)", |
---|
[549] | 1936 | <<"Unknown Event"); |
---|
| 1937 | return false; |
---|
[300] | 1938 | } |
---|
| 1939 | } |
---|
| 1940 | } |
---|
[1622] | 1941 | CATCH |
---|
[509] | 1942 | |
---|
[1875] | 1943 | // ! Client side: Send a message to server to make it close |
---|
| 1944 | void CContext::sendCloseDefinition(CContextClient* client) |
---|
| 1945 | TRY |
---|
| 1946 | { |
---|
| 1947 | if (sendCloseDefinition_done_.count(client)!=0) return ; |
---|
| 1948 | else sendCloseDefinition_done_.insert(client) ; |
---|
[509] | 1949 | |
---|
[1875] | 1950 | CEventClient event(getType(),EVENT_ID_CLOSE_DEFINITION); |
---|
| 1951 | if (client->isServerLeader()) |
---|
| 1952 | { |
---|
| 1953 | CMessage msg; |
---|
| 1954 | for(auto rank : client->getRanksServerLeader()) event.push(rank,1,msg); |
---|
| 1955 | client->sendEvent(event); |
---|
| 1956 | } |
---|
| 1957 | else client->sendEvent(event); |
---|
| 1958 | } |
---|
| 1959 | CATCH_DUMP_ATTR |
---|
| 1960 | |
---|
[509] | 1961 | //! Server side: Receive a message of client announcing a context close |
---|
[300] | 1962 | void CContext::recvCloseDefinition(CEventServer& event) |
---|
[1622] | 1963 | TRY |
---|
[300] | 1964 | { |
---|
| 1965 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
[1848] | 1966 | getCurrent()->closeDefinition(); |
---|
[300] | 1967 | } |
---|
[1622] | 1968 | CATCH |
---|
[509] | 1969 | |
---|
| 1970 | //! Client side: Send a message to update calendar in each time step |
---|
[300] | 1971 | void CContext::sendUpdateCalendar(int step) |
---|
[1622] | 1972 | TRY |
---|
[300] | 1973 | { |
---|
[1875] | 1974 | for(auto client : slaveServers_) |
---|
| 1975 | { |
---|
[2324] | 1976 | CEventClient event(getType(),EVENT_ID_UPDATE_CALENDAR); |
---|
[1875] | 1977 | if (client->isServerLeader()) |
---|
| 1978 | { |
---|
| 1979 | CMessage msg; |
---|
| 1980 | msg<<step; |
---|
| 1981 | for (auto& rank : client->getRanksServerLeader() ) event.push(rank,1,msg); |
---|
| 1982 | client->sendEvent(event); |
---|
| 1983 | } |
---|
| 1984 | else client->sendEvent(event); |
---|
[1009] | 1985 | } |
---|
[300] | 1986 | } |
---|
[1622] | 1987 | CATCH_DUMP_ATTR |
---|
[509] | 1988 | |
---|
| 1989 | //! Server side: Receive a message of client annoucing calendar update |
---|
[300] | 1990 | void CContext::recvUpdateCalendar(CEventServer& event) |
---|
[1622] | 1991 | TRY |
---|
[300] | 1992 | { |
---|
| 1993 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
[1848] | 1994 | getCurrent()->recvUpdateCalendar(*buffer); |
---|
[300] | 1995 | } |
---|
[1622] | 1996 | CATCH |
---|
[509] | 1997 | |
---|
| 1998 | //! Server side: Receive a message of client annoucing calendar update |
---|
[300] | 1999 | void CContext::recvUpdateCalendar(CBufferIn& buffer) |
---|
[1622] | 2000 | TRY |
---|
[300] | 2001 | { |
---|
[549] | 2002 | int step; |
---|
| 2003 | buffer>>step; |
---|
| 2004 | updateCalendar(step); |
---|
[1853] | 2005 | if (serviceType_==CServicesManager::GATHERER) |
---|
[987] | 2006 | { |
---|
| 2007 | sendUpdateCalendar(step); |
---|
| 2008 | } |
---|
[300] | 2009 | } |
---|
[1622] | 2010 | CATCH_DUMP_ATTR |
---|
[509] | 2011 | |
---|
[1853] | 2012 | |
---|
[1784] | 2013 | void CContext::createCouplerInterCommunicator(void) |
---|
| 2014 | TRY |
---|
| 2015 | { |
---|
[1878] | 2016 | int rank=this->getIntraCommRank() ; |
---|
| 2017 | map<string,list<CCouplerOut*>> listCouplerOut ; |
---|
| 2018 | map<string,list<CCouplerIn*>> listCouplerIn ; |
---|
| 2019 | |
---|
| 2020 | for(auto couplerOut : enabledCouplerOut) listCouplerOut[couplerOut->getCouplingContextId()].push_back(couplerOut) ; |
---|
| 2021 | for(auto couplerIn : enabledCouplerIn) listCouplerIn[couplerIn->getCouplingContextId()].push_back(couplerIn) ; |
---|
| 2022 | |
---|
| 2023 | CCouplerManager* couplerManager = CXios::getCouplerManager() ; |
---|
| 2024 | if (rank==0) |
---|
[1784] | 2025 | { |
---|
[1878] | 2026 | for(auto couplerOut : listCouplerOut) couplerManager->registerCoupling(this->getContextId(),couplerOut.first) ; |
---|
| 2027 | for(auto couplerIn : listCouplerIn) couplerManager->registerCoupling(couplerIn.first,this->getContextId()) ; |
---|
[1784] | 2028 | } |
---|
| 2029 | |
---|
[1878] | 2030 | do |
---|
[1784] | 2031 | { |
---|
[1878] | 2032 | for(auto couplerOut : listCouplerOut) |
---|
| 2033 | { |
---|
| 2034 | bool isNextCoupling ; |
---|
| 2035 | if (rank==0) isNextCoupling = couplerManager->isNextCoupling(this->getContextId(),couplerOut.first) ; |
---|
| 2036 | MPI_Bcast(&isNextCoupling,1,MPI_C_BOOL, 0, getIntraComm()) ; |
---|
| 2037 | if (isNextCoupling) |
---|
| 2038 | { |
---|
| 2039 | addCouplingChanel(couplerOut.first, true) ; |
---|
| 2040 | listCouplerOut.erase(couplerOut.first) ; |
---|
| 2041 | break ; |
---|
| 2042 | } |
---|
| 2043 | } |
---|
| 2044 | for(auto couplerIn : listCouplerIn) |
---|
| 2045 | { |
---|
| 2046 | bool isNextCoupling ; |
---|
| 2047 | if (rank==0) isNextCoupling = couplerManager->isNextCoupling(couplerIn.first,this->getContextId()); |
---|
| 2048 | MPI_Bcast(&isNextCoupling,1,MPI_C_BOOL, 0, getIntraComm()) ; |
---|
| 2049 | if (isNextCoupling) |
---|
| 2050 | { |
---|
| 2051 | addCouplingChanel(couplerIn.first, false) ; |
---|
| 2052 | listCouplerIn.erase(couplerIn.first) ; |
---|
| 2053 | break ; |
---|
| 2054 | } |
---|
| 2055 | } |
---|
| 2056 | |
---|
| 2057 | } while (!listCouplerOut.empty() || !listCouplerIn.empty()) ; |
---|
| 2058 | |
---|
[1784] | 2059 | } |
---|
| 2060 | CATCH_DUMP_ATTR |
---|
| 2061 | |
---|
[1870] | 2062 | |
---|
| 2063 | //! Client side: Send infomation of active files (files are enabled to write out) |
---|
[1232] | 2064 | void CContext::sendEnabledFiles(const std::vector<CFile*>& activeFiles) |
---|
[1622] | 2065 | TRY |
---|
[509] | 2066 | { |
---|
[1232] | 2067 | int size = activeFiles.size(); |
---|
[509] | 2068 | |
---|
| 2069 | // In a context, each type has a root definition, e.g: axis, domain, field. |
---|
| 2070 | // Every object must be a child of one of these root definition. In this case |
---|
| 2071 | // all new file objects created on server must be children of the root "file_definition" |
---|
| 2072 | StdString fileDefRoot("file_definition"); |
---|
| 2073 | CFileGroup* cfgrpPtr = CFileGroup::get(fileDefRoot); |
---|
[1158] | 2074 | |
---|
[509] | 2075 | for (int i = 0; i < size; ++i) |
---|
| 2076 | { |
---|
[1232] | 2077 | CFile* f = activeFiles[i]; |
---|
| 2078 | cfgrpPtr->sendCreateChild(f->getId(),f->getContextClient()); |
---|
| 2079 | f->sendAllAttributesToServer(f->getContextClient()); |
---|
| 2080 | f->sendAddAllVariables(f->getContextClient()); |
---|
[509] | 2081 | } |
---|
| 2082 | } |
---|
[1622] | 2083 | CATCH_DUMP_ATTR |
---|
[509] | 2084 | |
---|
| 2085 | //! Client side: Send information of active fields (ones are written onto files) |
---|
[1232] | 2086 | void CContext::sendEnabledFieldsInFiles(const std::vector<CFile*>& activeFiles) |
---|
[1622] | 2087 | TRY |
---|
[509] | 2088 | { |
---|
[1232] | 2089 | int size = activeFiles.size(); |
---|
[509] | 2090 | for (int i = 0; i < size; ++i) |
---|
| 2091 | { |
---|
[1232] | 2092 | activeFiles[i]->sendEnabledFields(activeFiles[i]->getContextClient()); |
---|
[509] | 2093 | } |
---|
| 2094 | } |
---|
[1622] | 2095 | CATCH_DUMP_ATTR |
---|
[509] | 2096 | |
---|
[1956] | 2097 | |
---|
[711] | 2098 | //! Client side: Prepare the timeseries by adding the necessary files |
---|
| 2099 | void CContext::prepareTimeseries() |
---|
[1622] | 2100 | TRY |
---|
[711] | 2101 | { |
---|
| 2102 | const std::vector<CFile*> allFiles = CFile::getAll(); |
---|
| 2103 | for (size_t i = 0; i < allFiles.size(); i++) |
---|
| 2104 | { |
---|
| 2105 | CFile* file = allFiles[i]; |
---|
| 2106 | |
---|
[1158] | 2107 | std::vector<CVariable*> fileVars, fieldVars, vars = file->getAllVariables(); |
---|
| 2108 | for (size_t k = 0; k < vars.size(); k++) |
---|
| 2109 | { |
---|
| 2110 | CVariable* var = vars[k]; |
---|
| 2111 | |
---|
| 2112 | if (var->ts_target.isEmpty() |
---|
| 2113 | || var->ts_target == CVariable::ts_target_attr::file || var->ts_target == CVariable::ts_target_attr::both) |
---|
| 2114 | fileVars.push_back(var); |
---|
| 2115 | |
---|
| 2116 | if (!var->ts_target.isEmpty() |
---|
| 2117 | && (var->ts_target == CVariable::ts_target_attr::field || var->ts_target == CVariable::ts_target_attr::both)) |
---|
| 2118 | fieldVars.push_back(var); |
---|
| 2119 | } |
---|
| 2120 | |
---|
[711] | 2121 | if (!file->timeseries.isEmpty() && file->timeseries != CFile::timeseries_attr::none) |
---|
| 2122 | { |
---|
[1158] | 2123 | StdString fileNameStr("%file_name%") ; |
---|
| 2124 | StdString tsPrefix = !file->ts_prefix.isEmpty() ? file->ts_prefix : fileNameStr ; |
---|
| 2125 | |
---|
| 2126 | StdString fileName=file->getFileOutputName(); |
---|
| 2127 | size_t pos=tsPrefix.find(fileNameStr) ; |
---|
| 2128 | while (pos!=std::string::npos) |
---|
| 2129 | { |
---|
| 2130 | tsPrefix=tsPrefix.replace(pos,fileNameStr.size(),fileName) ; |
---|
| 2131 | pos=tsPrefix.find(fileNameStr) ; |
---|
| 2132 | } |
---|
| 2133 | |
---|
[711] | 2134 | const std::vector<CField*> allFields = file->getAllFields(); |
---|
| 2135 | for (size_t j = 0; j < allFields.size(); j++) |
---|
| 2136 | { |
---|
| 2137 | CField* field = allFields[j]; |
---|
| 2138 | |
---|
| 2139 | if (!field->ts_enabled.isEmpty() && field->ts_enabled) |
---|
| 2140 | { |
---|
| 2141 | CFile* tsFile = CFile::create(); |
---|
| 2142 | tsFile->duplicateAttributes(file); |
---|
| 2143 | |
---|
[1158] | 2144 | // Add variables originating from file and targeted to timeserie file |
---|
| 2145 | for (size_t k = 0; k < fileVars.size(); k++) |
---|
| 2146 | tsFile->getVirtualVariableGroup()->addChild(fileVars[k]); |
---|
| 2147 | |
---|
| 2148 | |
---|
[711] | 2149 | tsFile->name = tsPrefix + "_"; |
---|
| 2150 | if (!field->name.isEmpty()) |
---|
| 2151 | tsFile->name.get() += field->name; |
---|
| 2152 | else if (field->hasDirectFieldReference()) // We cannot use getBaseFieldReference() just yet |
---|
| 2153 | tsFile->name.get() += field->field_ref; |
---|
| 2154 | else |
---|
| 2155 | tsFile->name.get() += field->getId(); |
---|
| 2156 | |
---|
| 2157 | if (!field->ts_split_freq.isEmpty()) |
---|
| 2158 | tsFile->split_freq = field->ts_split_freq; |
---|
| 2159 | |
---|
| 2160 | CField* tsField = tsFile->addField(); |
---|
| 2161 | tsField->field_ref = field->getId(); |
---|
| 2162 | |
---|
[1158] | 2163 | // Add variables originating from file and targeted to timeserie field |
---|
| 2164 | for (size_t k = 0; k < fieldVars.size(); k++) |
---|
| 2165 | tsField->getVirtualVariableGroup()->addChild(fieldVars[k]); |
---|
| 2166 | |
---|
| 2167 | vars = field->getAllVariables(); |
---|
| 2168 | for (size_t k = 0; k < vars.size(); k++) |
---|
| 2169 | { |
---|
| 2170 | CVariable* var = vars[k]; |
---|
| 2171 | |
---|
| 2172 | // Add variables originating from field and targeted to timeserie field |
---|
| 2173 | if (var->ts_target.isEmpty() |
---|
| 2174 | || var->ts_target == CVariable::ts_target_attr::field || var->ts_target == CVariable::ts_target_attr::both) |
---|
| 2175 | tsField->getVirtualVariableGroup()->addChild(var); |
---|
| 2176 | |
---|
| 2177 | // Add variables originating from field and targeted to timeserie file |
---|
| 2178 | if (!var->ts_target.isEmpty() |
---|
| 2179 | && (var->ts_target == CVariable::ts_target_attr::file || var->ts_target == CVariable::ts_target_attr::both)) |
---|
| 2180 | tsFile->getVirtualVariableGroup()->addChild(var); |
---|
| 2181 | } |
---|
| 2182 | |
---|
[711] | 2183 | tsFile->solveFieldRefInheritance(true); |
---|
| 2184 | |
---|
| 2185 | if (file->timeseries == CFile::timeseries_attr::exclusive) |
---|
| 2186 | field->enabled = false; |
---|
| 2187 | } |
---|
| 2188 | } |
---|
| 2189 | |
---|
| 2190 | // Finally disable the original file is need be |
---|
| 2191 | if (file->timeseries == CFile::timeseries_attr::only) |
---|
| 2192 | file->enabled = false; |
---|
| 2193 | } |
---|
| 2194 | } |
---|
| 2195 | } |
---|
[1622] | 2196 | CATCH_DUMP_ATTR |
---|
[711] | 2197 | |
---|
[1974] | 2198 | |
---|
[1232] | 2199 | //! Client side: Send information of reference domain, axis and scalar of active fields |
---|
| 2200 | void CContext::sendRefDomainsAxisScalars(const std::vector<CFile*>& activeFiles) |
---|
[1622] | 2201 | TRY |
---|
[569] | 2202 | { |
---|
[1784] | 2203 | std::set<pair<StdString,CContextClient*>> domainIds, axisIds, scalarIds; |
---|
[509] | 2204 | |
---|
[569] | 2205 | // Find all reference domain and axis of all active fields |
---|
[1232] | 2206 | int numEnabledFiles = activeFiles.size(); |
---|
[569] | 2207 | for (int i = 0; i < numEnabledFiles; ++i) |
---|
| 2208 | { |
---|
[1232] | 2209 | std::vector<CField*> enabledFields = activeFiles[i]->getEnabledFields(); |
---|
[569] | 2210 | int numEnabledFields = enabledFields.size(); |
---|
| 2211 | for (int j = 0; j < numEnabledFields; ++j) |
---|
| 2212 | { |
---|
[1784] | 2213 | CContextClient* contextClient=enabledFields[j]->getContextClient() ; |
---|
[887] | 2214 | const std::vector<StdString>& prDomAxisScalarId = enabledFields[j]->getRefDomainAxisIds(); |
---|
[1784] | 2215 | if ("" != prDomAxisScalarId[0]) domainIds.insert(make_pair(prDomAxisScalarId[0],contextClient)); |
---|
| 2216 | if ("" != prDomAxisScalarId[1]) axisIds.insert(make_pair(prDomAxisScalarId[1],contextClient)); |
---|
| 2217 | if ("" != prDomAxisScalarId[2]) scalarIds.insert(make_pair(prDomAxisScalarId[2],contextClient)); |
---|
[569] | 2218 | } |
---|
| 2219 | } |
---|
| 2220 | |
---|
| 2221 | // Create all reference axis on server side |
---|
[887] | 2222 | std::set<StdString>::iterator itDom, itAxis, itScalar; |
---|
[569] | 2223 | std::set<StdString>::const_iterator itE; |
---|
| 2224 | |
---|
[887] | 2225 | StdString scalarDefRoot("scalar_definition"); |
---|
| 2226 | CScalarGroup* scalarPtr = CScalarGroup::get(scalarDefRoot); |
---|
[1784] | 2227 | |
---|
| 2228 | for (auto itScalar = scalarIds.begin(); itScalar != scalarIds.end(); ++itScalar) |
---|
[887] | 2229 | { |
---|
[1784] | 2230 | if (!itScalar->first.empty()) |
---|
[887] | 2231 | { |
---|
[1784] | 2232 | scalarPtr->sendCreateChild(itScalar->first,itScalar->second); |
---|
| 2233 | CScalar::get(itScalar->first)->sendAllAttributesToServer(itScalar->second); |
---|
[887] | 2234 | } |
---|
| 2235 | } |
---|
| 2236 | |
---|
[569] | 2237 | StdString axiDefRoot("axis_definition"); |
---|
| 2238 | CAxisGroup* axisPtr = CAxisGroup::get(axiDefRoot); |
---|
[1784] | 2239 | |
---|
| 2240 | for (auto itAxis = axisIds.begin(); itAxis != axisIds.end(); ++itAxis) |
---|
[569] | 2241 | { |
---|
[1784] | 2242 | if (!itAxis->first.empty()) |
---|
[569] | 2243 | { |
---|
[1784] | 2244 | axisPtr->sendCreateChild(itAxis->first, itAxis->second); |
---|
| 2245 | CAxis::get(itAxis->first)->sendAllAttributesToServer(itAxis->second); |
---|
[569] | 2246 | } |
---|
| 2247 | } |
---|
| 2248 | |
---|
| 2249 | // Create all reference domains on server side |
---|
| 2250 | StdString domDefRoot("domain_definition"); |
---|
| 2251 | CDomainGroup* domPtr = CDomainGroup::get(domDefRoot); |
---|
[1784] | 2252 | |
---|
| 2253 | for (auto itDom = domainIds.begin(); itDom != domainIds.end(); ++itDom) |
---|
[569] | 2254 | { |
---|
[1784] | 2255 | if (!itDom->first.empty()) { |
---|
| 2256 | domPtr->sendCreateChild(itDom->first, itDom->second); |
---|
| 2257 | CDomain::get(itDom->first)->sendAllAttributesToServer(itDom->second); |
---|
[569] | 2258 | } |
---|
| 2259 | } |
---|
| 2260 | } |
---|
[1622] | 2261 | CATCH_DUMP_ATTR |
---|
[569] | 2262 | |
---|
[1875] | 2263 | void CContext::triggerLateFields(void) |
---|
| 2264 | TRY |
---|
| 2265 | { |
---|
| 2266 | for(auto& field : fileInFields_) field->triggerLateField() ; |
---|
| 2267 | for(auto& field : couplerInFields_) field->triggerLateField() ; |
---|
| 2268 | } |
---|
| 2269 | CATCH_DUMP_ATTR |
---|
| 2270 | |
---|
[509] | 2271 | //! Update calendar in each time step |
---|
[300] | 2272 | void CContext::updateCalendar(int step) |
---|
[1622] | 2273 | TRY |
---|
[300] | 2274 | { |
---|
[1357] | 2275 | int prevStep = calendar->getStep(); |
---|
| 2276 | |
---|
| 2277 | if (prevStep < step) |
---|
[639] | 2278 | { |
---|
[1853] | 2279 | if (serviceType_==CServicesManager::CLIENT) // For now we only use server level 1 to read data |
---|
[1358] | 2280 | { |
---|
[1875] | 2281 | triggerLateFields(); |
---|
[1358] | 2282 | } |
---|
| 2283 | |
---|
[1357] | 2284 | info(50) << "updateCalendar : before : " << calendar->getCurrentDate() << endl; |
---|
| 2285 | calendar->update(step); |
---|
| 2286 | info(50) << "updateCalendar : after : " << calendar->getCurrentDate() << endl; |
---|
| 2287 | #ifdef XIOS_MEMTRACK_LIGHT |
---|
| 2288 | info(50) << " Current memory used by XIOS : "<< MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte, at timestep "<<step<<" of context "<<this->getId()<<endl ; |
---|
| 2289 | #endif |
---|
| 2290 | |
---|
[1853] | 2291 | if (serviceType_==CServicesManager::CLIENT) // For now we only use server level 1 to read data |
---|
[1357] | 2292 | { |
---|
[2507] | 2293 | // doPostTimestepOperationsForEnabledReadModeFiles(); |
---|
| 2294 | for(auto& field : fileInFields_) field->sendReadDataRequestIfNeeded() ; |
---|
[1357] | 2295 | garbageCollector.invalidate(calendar->getCurrentDate()); |
---|
| 2296 | } |
---|
[2426] | 2297 | CMemChecker::logMem( "CContext::updateCalendar_"+std::to_string(step) ); |
---|
[639] | 2298 | } |
---|
[1357] | 2299 | else if (prevStep == step) |
---|
| 2300 | info(50) << "updateCalendar: already at step " << step << ", no operation done." << endl; |
---|
| 2301 | else // if (prevStep > step) |
---|
| 2302 | ERROR("void CContext::updateCalendar(int step)", |
---|
| 2303 | << "Illegal calendar update: previous step was " << prevStep << ", new step " << step << "is in the past!") |
---|
[300] | 2304 | } |
---|
[1622] | 2305 | CATCH_DUMP_ATTR |
---|
[509] | 2306 | |
---|
[1232] | 2307 | void CContext::initReadFiles(void) |
---|
[1622] | 2308 | TRY |
---|
[1232] | 2309 | { |
---|
| 2310 | vector<CFile*>::const_iterator it; |
---|
| 2311 | |
---|
| 2312 | for (it=enabledReadModeFiles.begin(); it != enabledReadModeFiles.end(); it++) |
---|
| 2313 | { |
---|
| 2314 | (*it)->initRead(); |
---|
| 2315 | } |
---|
| 2316 | } |
---|
[1622] | 2317 | CATCH_DUMP_ATTR |
---|
[1232] | 2318 | |
---|
[2507] | 2319 | //! Create header of netcdf file |
---|
[1232] | 2320 | void CContext::createFileHeader(void) |
---|
[1622] | 2321 | TRY |
---|
[300] | 2322 | { |
---|
[2507] | 2323 | for(auto& file : filesToWrite_) file->initWrite(); |
---|
[509] | 2324 | } |
---|
[1622] | 2325 | CATCH_DUMP_ATTR |
---|
[509] | 2326 | |
---|
| 2327 | //! Get current context |
---|
[347] | 2328 | CContext* CContext::getCurrent(void) |
---|
[1622] | 2329 | TRY |
---|
[300] | 2330 | { |
---|
[549] | 2331 | return CObjectFactory::GetObject<CContext>(CObjectFactory::GetCurrentContextId()).get(); |
---|
[300] | 2332 | } |
---|
[1622] | 2333 | CATCH |
---|
[509] | 2334 | |
---|
| 2335 | /*! |
---|
| 2336 | \brief Set context with an id be the current context |
---|
| 2337 | \param [in] id identity of context to be set to current |
---|
| 2338 | */ |
---|
[346] | 2339 | void CContext::setCurrent(const string& id) |
---|
[1622] | 2340 | TRY |
---|
[346] | 2341 | { |
---|
| 2342 | CObjectFactory::SetCurrentContextId(id); |
---|
| 2343 | CGroupFactory::SetCurrentContextId(id); |
---|
| 2344 | } |
---|
[1622] | 2345 | CATCH |
---|
[509] | 2346 | |
---|
| 2347 | /*! |
---|
| 2348 | \brief Create a context with specific id |
---|
| 2349 | \param [in] id identity of new context |
---|
| 2350 | \return pointer to the new context or already-existed one with identity id |
---|
| 2351 | */ |
---|
[347] | 2352 | CContext* CContext::create(const StdString& id) |
---|
[1622] | 2353 | TRY |
---|
[346] | 2354 | { |
---|
[549] | 2355 | CContext::setCurrent(id); |
---|
[509] | 2356 | |
---|
[346] | 2357 | bool hasctxt = CContext::has(id); |
---|
[347] | 2358 | CContext* context = CObjectFactory::CreateObject<CContext>(id).get(); |
---|
[549] | 2359 | getRoot(); |
---|
[347] | 2360 | if (!hasctxt) CGroupFactory::AddChild(root, context->getShared()); |
---|
[346] | 2361 | |
---|
| 2362 | #define DECLARE_NODE(Name_, name_) \ |
---|
| 2363 | C##Name_##Definition::create(C##Name_##Definition::GetDefName()); |
---|
| 2364 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 2365 | #include "node_type.conf" |
---|
| 2366 | |
---|
| 2367 | return (context); |
---|
| 2368 | } |
---|
[1622] | 2369 | CATCH |
---|
[697] | 2370 | |
---|
[1764] | 2371 | |
---|
| 2372 | void CContext::sendFinalizeClient(CContextClient* contextClient, const string& contextClientId) |
---|
| 2373 | TRY |
---|
| 2374 | { |
---|
| 2375 | CEventClient event(getType(),EVENT_ID_CONTEXT_FINALIZE_CLIENT); |
---|
| 2376 | if (contextClient->isServerLeader()) |
---|
| 2377 | { |
---|
| 2378 | CMessage msg; |
---|
| 2379 | msg<<contextClientId ; |
---|
| 2380 | const std::list<int>& ranks = contextClient->getRanksServerLeader(); |
---|
| 2381 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 2382 | event.push(*itRank,1,msg); |
---|
| 2383 | contextClient->sendEvent(event); |
---|
| 2384 | } |
---|
| 2385 | else contextClient->sendEvent(event); |
---|
| 2386 | } |
---|
| 2387 | CATCH_DUMP_ATTR |
---|
| 2388 | |
---|
| 2389 | |
---|
| 2390 | void CContext::recvFinalizeClient(CEventServer& event) |
---|
| 2391 | TRY |
---|
| 2392 | { |
---|
| 2393 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 2394 | string id; |
---|
| 2395 | *buffer>>id; |
---|
| 2396 | get(id)->recvFinalizeClient(*buffer); |
---|
| 2397 | } |
---|
| 2398 | CATCH |
---|
| 2399 | |
---|
| 2400 | void CContext::recvFinalizeClient(CBufferIn& buffer) |
---|
| 2401 | TRY |
---|
| 2402 | { |
---|
| 2403 | countChildContextFinalized_++ ; |
---|
| 2404 | } |
---|
| 2405 | CATCH_DUMP_ATTR |
---|
| 2406 | |
---|
| 2407 | |
---|
| 2408 | |
---|
| 2409 | |
---|
[1875] | 2410 | //! Client side: Send a message announcing that context can receive grid definition from coupling |
---|
| 2411 | void CContext::sendCouplerInReady(CContextClient* client) |
---|
| 2412 | TRY |
---|
| 2413 | { |
---|
| 2414 | if (sendCouplerInReady_done_.count(client)!=0) return ; |
---|
| 2415 | else sendCouplerInReady_done_.insert(client) ; |
---|
| 2416 | |
---|
| 2417 | CEventClient event(getType(),EVENT_ID_COUPLER_IN_READY); |
---|
| 2418 | |
---|
| 2419 | if (client->isServerLeader()) |
---|
| 2420 | { |
---|
| 2421 | CMessage msg; |
---|
| 2422 | msg<<this->getId(); |
---|
| 2423 | for (auto& rank : client->getRanksServerLeader()) event.push(rank,1,msg); |
---|
| 2424 | client->sendEvent(event); |
---|
| 2425 | } |
---|
| 2426 | else client->sendEvent(event); |
---|
| 2427 | } |
---|
| 2428 | CATCH_DUMP_ATTR |
---|
| 2429 | |
---|
| 2430 | //! Server side: Receive a message announcing that context can send grid definition for context coupling |
---|
| 2431 | void CContext::recvCouplerInReady(CEventServer& event) |
---|
| 2432 | TRY |
---|
| 2433 | { |
---|
| 2434 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 2435 | getCurrent()->recvCouplerInReady(*buffer); |
---|
| 2436 | } |
---|
| 2437 | CATCH |
---|
| 2438 | |
---|
| 2439 | //! Server side: Receive a message announcing that context can send grid definition for context coupling |
---|
| 2440 | void CContext::recvCouplerInReady(CBufferIn& buffer) |
---|
| 2441 | TRY |
---|
| 2442 | { |
---|
| 2443 | string contextId ; |
---|
| 2444 | buffer>>contextId; |
---|
| 2445 | couplerInReady_.insert(getCouplerOutClient(contextId)) ; |
---|
| 2446 | } |
---|
| 2447 | CATCH_DUMP_ATTR |
---|
| 2448 | |
---|
| 2449 | |
---|
| 2450 | |
---|
| 2451 | |
---|
| 2452 | |
---|
| 2453 | //! Client side: Send a message announcing that a coupling context have done it closeDefinition, so data can be sent now. |
---|
| 2454 | void CContext::sendCouplerInCloseDefinition(CContextClient* client) |
---|
| 2455 | TRY |
---|
| 2456 | { |
---|
| 2457 | if (sendCouplerInCloseDefinition_done_.count(client)!=0) return ; |
---|
| 2458 | else sendCouplerInCloseDefinition_done_.insert(client) ; |
---|
| 2459 | |
---|
| 2460 | CEventClient event(getType(),EVENT_ID_COUPLER_IN_CLOSE_DEFINITION); |
---|
| 2461 | |
---|
| 2462 | if (client->isServerLeader()) |
---|
| 2463 | { |
---|
| 2464 | CMessage msg; |
---|
| 2465 | msg<<this->getId(); |
---|
| 2466 | for (auto& rank : client->getRanksServerLeader()) event.push(rank,1,msg); |
---|
| 2467 | client->sendEvent(event); |
---|
| 2468 | } |
---|
| 2469 | else client->sendEvent(event); |
---|
| 2470 | } |
---|
| 2471 | CATCH_DUMP_ATTR |
---|
| 2472 | |
---|
| 2473 | //! Server side: Receive a message announcing that a coupling context have done it closeDefinition, so data can be sent now. |
---|
| 2474 | void CContext::recvCouplerInCloseDefinition(CEventServer& event) |
---|
| 2475 | TRY |
---|
| 2476 | { |
---|
| 2477 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 2478 | getCurrent()->recvCouplerInCloseDefinition(*buffer); |
---|
| 2479 | } |
---|
| 2480 | CATCH |
---|
| 2481 | |
---|
| 2482 | //! Server side: Receive a message announcing that a coupling context have done it closeDefinition, so data can be sent now. |
---|
| 2483 | void CContext::recvCouplerInCloseDefinition(CBufferIn& buffer) |
---|
| 2484 | TRY |
---|
| 2485 | { |
---|
| 2486 | string contextId ; |
---|
| 2487 | buffer>>contextId; |
---|
| 2488 | couplerInCloseDefinition_.insert(getCouplerOutClient(contextId)) ; |
---|
| 2489 | } |
---|
| 2490 | CATCH_DUMP_ATTR |
---|
| 2491 | |
---|
| 2492 | |
---|
| 2493 | |
---|
| 2494 | |
---|
| 2495 | //! Client side: Send a message announcing that a coupling context have done it contextFinalize, so it can also close it own context. |
---|
| 2496 | void CContext::sendCouplerInContextFinalized(CContextClient* client) |
---|
| 2497 | TRY |
---|
| 2498 | { |
---|
| 2499 | if (sendCouplerInContextFinalized_done_.count(client)!=0) return ; |
---|
| 2500 | else sendCouplerInContextFinalized_done_.insert(client) ; |
---|
| 2501 | |
---|
| 2502 | CEventClient event(getType(),EVENT_ID_COUPLER_IN_CONTEXT_FINALIZED); |
---|
| 2503 | |
---|
| 2504 | if (client->isServerLeader()) |
---|
| 2505 | { |
---|
| 2506 | CMessage msg; |
---|
| 2507 | msg<<this->getId(); |
---|
| 2508 | for (auto& rank : client->getRanksServerLeader()) event.push(rank,1,msg); |
---|
| 2509 | client->sendEvent(event); |
---|
| 2510 | } |
---|
| 2511 | else client->sendEvent(event); |
---|
| 2512 | } |
---|
| 2513 | CATCH_DUMP_ATTR |
---|
| 2514 | |
---|
| 2515 | //! Server side: Receive a message announcing that a coupling context have done it contextFinalize, so it can also close it own context. |
---|
| 2516 | void CContext::recvCouplerInContextFinalized(CEventServer& event) |
---|
| 2517 | TRY |
---|
| 2518 | { |
---|
| 2519 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 2520 | getCurrent()->recvCouplerInContextFinalized(*buffer); |
---|
| 2521 | } |
---|
| 2522 | CATCH |
---|
| 2523 | |
---|
| 2524 | //! Server side: Receive a message announcing that a coupling context have done it contextFinalize, so it can also close it own context. |
---|
| 2525 | void CContext::recvCouplerInContextFinalized(CBufferIn& buffer) |
---|
| 2526 | TRY |
---|
| 2527 | { |
---|
| 2528 | string contextId ; |
---|
| 2529 | buffer>>contextId; |
---|
| 2530 | couplerInContextFinalized_.insert(getCouplerOutClient(contextId)) ; |
---|
| 2531 | } |
---|
| 2532 | CATCH_DUMP_ATTR |
---|
| 2533 | |
---|
| 2534 | |
---|
| 2535 | |
---|
| 2536 | |
---|
[1130] | 2537 | /*! |
---|
| 2538 | * \fn bool CContext::isFinalized(void) |
---|
[1139] | 2539 | * Context is finalized if it received context post finalize event. |
---|
[1130] | 2540 | */ |
---|
[1054] | 2541 | bool CContext::isFinalized(void) |
---|
[1622] | 2542 | TRY |
---|
[1054] | 2543 | { |
---|
[1139] | 2544 | return finalized; |
---|
[1054] | 2545 | } |
---|
[1622] | 2546 | CATCH_DUMP_ATTR |
---|
| 2547 | ///-------------------------------------------------------------- |
---|
| 2548 | StdString CContext::dumpClassAttributes(void) |
---|
| 2549 | { |
---|
| 2550 | StdString str; |
---|
| 2551 | str.append("enabled files=\""); |
---|
| 2552 | int size = this->enabledFiles.size(); |
---|
| 2553 | for (int i = 0; i < size; ++i) |
---|
| 2554 | { |
---|
| 2555 | str.append(enabledFiles[i]->getId()); |
---|
| 2556 | str.append(" "); |
---|
| 2557 | } |
---|
| 2558 | str.append("\""); |
---|
| 2559 | return str; |
---|
| 2560 | } |
---|
[1054] | 2561 | |
---|
[335] | 2562 | } // namespace xios |
---|