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