[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" |
---|
[219] | 16 | |
---|
[992] | 17 | #include "server.hpp" |
---|
[697] | 18 | |
---|
[335] | 19 | namespace xios { |
---|
[509] | 20 | |
---|
[549] | 21 | shared_ptr<CContextGroup> CContext::root; |
---|
[509] | 22 | |
---|
[219] | 23 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 24 | |
---|
| 25 | CContext::CContext(void) |
---|
| 26 | : CObjectTemplate<CContext>(), CContextAttributes() |
---|
[983] | 27 | , calendar(), hasClient(false), hasServer(false) |
---|
[1130] | 28 | , isPostProcessed(false)//, finalized(false) |
---|
[1009] | 29 | , idServer_(), client(0), server(0) |
---|
[1130] | 30 | , allProcessed(false), countChildCtx_(0) |
---|
[219] | 31 | { /* Ne rien faire de plus */ } |
---|
| 32 | |
---|
| 33 | CContext::CContext(const StdString & id) |
---|
| 34 | : CObjectTemplate<CContext>(id), CContextAttributes() |
---|
[983] | 35 | , calendar(), hasClient(false), hasServer(false) |
---|
[1130] | 36 | , isPostProcessed(false)//, finalized(false) |
---|
[1009] | 37 | , idServer_(), client(0), server(0) |
---|
[1130] | 38 | , allProcessed(false), countChildCtx_(0) |
---|
[219] | 39 | { /* Ne rien faire de plus */ } |
---|
| 40 | |
---|
| 41 | CContext::~CContext(void) |
---|
[509] | 42 | { |
---|
[597] | 43 | delete client; |
---|
| 44 | delete server; |
---|
[1071] | 45 | for (std::vector<CContextClient*>::iterator it = clientPrimServer.begin(); it != clientPrimServer.end(); it++) delete *it; |
---|
| 46 | for (std::vector<CContextServer*>::iterator it = serverPrimServer.begin(); it != serverPrimServer.end(); it++) delete *it; |
---|
[300] | 47 | } |
---|
[219] | 48 | |
---|
| 49 | //---------------------------------------------------------------- |
---|
[509] | 50 | //! Get name of context |
---|
[219] | 51 | StdString CContext::GetName(void) { return (StdString("context")); } |
---|
| 52 | StdString CContext::GetDefName(void){ return (CContext::GetName()); } |
---|
| 53 | ENodeType CContext::GetType(void) { return (eContext); } |
---|
| 54 | |
---|
| 55 | //---------------------------------------------------------------- |
---|
[640] | 56 | |
---|
[509] | 57 | /*! |
---|
| 58 | \brief Get context group (context root) |
---|
| 59 | \return Context root |
---|
| 60 | */ |
---|
[347] | 61 | CContextGroup* CContext::getRoot(void) |
---|
[509] | 62 | { |
---|
[549] | 63 | if (root.get()==NULL) root=shared_ptr<CContextGroup>(new CContextGroup(xml::CXMLNode::GetRootName())); |
---|
[509] | 64 | return root.get(); |
---|
[219] | 65 | } |
---|
| 66 | |
---|
[640] | 67 | //---------------------------------------------------------------- |
---|
[509] | 68 | |
---|
| 69 | /*! |
---|
| 70 | \brief Get calendar of a context |
---|
| 71 | \return Calendar |
---|
| 72 | */ |
---|
[343] | 73 | boost::shared_ptr<CCalendar> CContext::getCalendar(void) const |
---|
[219] | 74 | { |
---|
| 75 | return (this->calendar); |
---|
| 76 | } |
---|
[509] | 77 | |
---|
[219] | 78 | //---------------------------------------------------------------- |
---|
[640] | 79 | |
---|
[509] | 80 | /*! |
---|
| 81 | \brief Set a context with a calendar |
---|
| 82 | \param[in] newCalendar new calendar |
---|
| 83 | */ |
---|
[343] | 84 | void CContext::setCalendar(boost::shared_ptr<CCalendar> newCalendar) |
---|
[219] | 85 | { |
---|
| 86 | this->calendar = newCalendar; |
---|
| 87 | } |
---|
[509] | 88 | |
---|
[219] | 89 | //---------------------------------------------------------------- |
---|
[509] | 90 | /*! |
---|
| 91 | \brief Parse xml file and write information into context object |
---|
| 92 | \param [in] node xmld node corresponding in xml file |
---|
| 93 | */ |
---|
[219] | 94 | void CContext::parse(xml::CXMLNode & node) |
---|
| 95 | { |
---|
| 96 | CContext::SuperClass::parse(node); |
---|
| 97 | |
---|
| 98 | // PARSING POUR GESTION DES ENFANTS |
---|
[278] | 99 | xml::THashAttributes attributes = node.getAttributes(); |
---|
[219] | 100 | |
---|
[278] | 101 | if (attributes.end() != attributes.find("src")) |
---|
| 102 | { |
---|
| 103 | StdIFStream ifs ( attributes["src"].c_str() , StdIFStream::in ); |
---|
[462] | 104 | if ( (ifs.rdstate() & std::ifstream::failbit ) != 0 ) |
---|
| 105 | ERROR("void CContext::parse(xml::CXMLNode & node)", |
---|
| 106 | <<endl<< "Can not open <"<<attributes["src"].c_str()<<"> file" ); |
---|
[278] | 107 | if (!ifs.good()) |
---|
| 108 | ERROR("CContext::parse(xml::CXMLNode & node)", |
---|
| 109 | << "[ filename = " << attributes["src"] << " ] Bad xml stream !"); |
---|
[462] | 110 | xml::CXMLParser::ParseInclude(ifs, attributes["src"], *this); |
---|
[278] | 111 | } |
---|
| 112 | |
---|
[219] | 113 | if (node.getElementName().compare(CContext::GetName())) |
---|
[421] | 114 | DEBUG("Le noeud is wrong defined but will be considered as a context !"); |
---|
[219] | 115 | |
---|
| 116 | if (!(node.goToChildElement())) |
---|
| 117 | { |
---|
| 118 | DEBUG("Le context ne contient pas d'enfant !"); |
---|
| 119 | } |
---|
| 120 | else |
---|
| 121 | { |
---|
| 122 | do { // Parcours des contextes pour traitement. |
---|
| 123 | |
---|
| 124 | StdString name = node.getElementName(); |
---|
| 125 | attributes.clear(); |
---|
| 126 | attributes = node.getAttributes(); |
---|
| 127 | |
---|
| 128 | if (attributes.end() != attributes.find("id")) |
---|
[509] | 129 | { |
---|
| 130 | DEBUG(<< "Definition node has an id," |
---|
| 131 | << "it will not be taking account !"); |
---|
| 132 | } |
---|
[219] | 133 | |
---|
| 134 | #define DECLARE_NODE(Name_, name_) \ |
---|
| 135 | if (name.compare(C##Name_##Definition::GetDefName()) == 0) \ |
---|
[549] | 136 | { C##Name_##Definition::create(C##Name_##Definition::GetDefName()) -> parse(node); continue; } |
---|
[219] | 137 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 138 | #include "node_type.conf" |
---|
[286] | 139 | |
---|
[421] | 140 | DEBUG(<< "The element \'" << name |
---|
| 141 | << "\' in the context \'" << CContext::getCurrent()->getId() |
---|
| 142 | << "\' is not a definition !"); |
---|
[219] | 143 | |
---|
| 144 | } while (node.goToNextElement()); |
---|
| 145 | |
---|
| 146 | node.goToParentElement(); // Retour au parent |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | //---------------------------------------------------------------- |
---|
[509] | 151 | //! Show tree structure of context |
---|
[219] | 152 | void CContext::ShowTree(StdOStream & out) |
---|
| 153 | { |
---|
[549] | 154 | StdString currentContextId = CContext::getCurrent() -> getId(); |
---|
[347] | 155 | std::vector<CContext*> def_vector = |
---|
[346] | 156 | CContext::getRoot()->getChildList(); |
---|
[347] | 157 | std::vector<CContext*>::iterator |
---|
[219] | 158 | it = def_vector.begin(), end = def_vector.end(); |
---|
| 159 | |
---|
| 160 | out << "<? xml version=\"1.0\" ?>" << std::endl; |
---|
| 161 | out << "<" << xml::CXMLNode::GetRootName() << " >" << std::endl; |
---|
[509] | 162 | |
---|
[219] | 163 | for (; it != end; it++) |
---|
| 164 | { |
---|
[509] | 165 | CContext* context = *it; |
---|
| 166 | CContext::setCurrent(context->getId()); |
---|
[219] | 167 | out << *context << std::endl; |
---|
| 168 | } |
---|
[509] | 169 | |
---|
[219] | 170 | out << "</" << xml::CXMLNode::GetRootName() << " >" << std::endl; |
---|
[509] | 171 | CContext::setCurrent(currentContextId); |
---|
[219] | 172 | } |
---|
[346] | 173 | |
---|
[509] | 174 | |
---|
[219] | 175 | //---------------------------------------------------------------- |
---|
| 176 | |
---|
[509] | 177 | //! Convert context object into string (to print) |
---|
[219] | 178 | StdString CContext::toString(void) const |
---|
| 179 | { |
---|
| 180 | StdOStringStream oss; |
---|
| 181 | oss << "<" << CContext::GetName() |
---|
| 182 | << " id=\"" << this->getId() << "\" " |
---|
| 183 | << SuperClassAttribute::toString() << ">" << std::endl; |
---|
| 184 | if (!this->hasChild()) |
---|
| 185 | { |
---|
| 186 | //oss << "<!-- No definition -->" << std::endl; // fait planter l'incrémentation |
---|
| 187 | } |
---|
| 188 | else |
---|
| 189 | { |
---|
| 190 | |
---|
| 191 | #define DECLARE_NODE(Name_, name_) \ |
---|
[346] | 192 | if (C##Name_##Definition::has(C##Name_##Definition::GetDefName())) \ |
---|
| 193 | oss << * C##Name_##Definition::get(C##Name_##Definition::GetDefName()) << std::endl; |
---|
[219] | 194 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 195 | #include "node_type.conf" |
---|
| 196 | |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | oss << "</" << CContext::GetName() << " >"; |
---|
| 200 | |
---|
| 201 | return (oss.str()); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | //---------------------------------------------------------------- |
---|
| 205 | |
---|
[509] | 206 | /*! |
---|
| 207 | \brief Find all inheritace among objects in a context. |
---|
| 208 | \param [in] apply (true) write attributes of parent into ones of child if they are empty |
---|
| 209 | (false) write attributes of parent into a new container of child |
---|
| 210 | \param [in] parent unused |
---|
| 211 | */ |
---|
[445] | 212 | void CContext::solveDescInheritance(bool apply, const CAttributeMap * const UNUSED(parent)) |
---|
[219] | 213 | { |
---|
| 214 | #define DECLARE_NODE(Name_, name_) \ |
---|
[354] | 215 | if (C##Name_##Definition::has(C##Name_##Definition::GetDefName())) \ |
---|
[445] | 216 | C##Name_##Definition::get(C##Name_##Definition::GetDefName())->solveDescInheritance(apply); |
---|
[219] | 217 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 218 | #include "node_type.conf" |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | //---------------------------------------------------------------- |
---|
| 222 | |
---|
[509] | 223 | //! Verify if all root definition in the context have child. |
---|
[219] | 224 | bool CContext::hasChild(void) const |
---|
| 225 | { |
---|
| 226 | return ( |
---|
| 227 | #define DECLARE_NODE(Name_, name_) \ |
---|
[346] | 228 | C##Name_##Definition::has(C##Name_##Definition::GetDefName()) || |
---|
[219] | 229 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 230 | #include "node_type.conf" |
---|
| 231 | false); |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | //---------------------------------------------------------------- |
---|
| 235 | |
---|
| 236 | void CContext::CleanTree(void) |
---|
| 237 | { |
---|
[549] | 238 | #define DECLARE_NODE(Name_, name_) C##Name_##Definition::ClearAllAttributes(); |
---|
[219] | 239 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 240 | #include "node_type.conf" |
---|
| 241 | } |
---|
| 242 | ///--------------------------------------------------------------- |
---|
[509] | 243 | |
---|
| 244 | //! Initialize client side |
---|
[597] | 245 | void CContext::initClient(MPI_Comm intraComm, MPI_Comm interComm, CContext* cxtServer /*= 0*/) |
---|
[300] | 246 | { |
---|
[983] | 247 | |
---|
| 248 | hasClient = true; |
---|
[1071] | 249 | MPI_Comm intraCommServer, interCommServer; |
---|
[1054] | 250 | |
---|
[1021] | 251 | if (CServer::serverLevel != 1) |
---|
[1071] | 252 | // initClient is called by client |
---|
[983] | 253 | { |
---|
| 254 | client = new CContextClient(this, intraComm, interComm, cxtServer); |
---|
[1054] | 255 | if (cxtServer) // Attached mode |
---|
| 256 | { |
---|
| 257 | intraCommServer = intraComm; |
---|
| 258 | interCommServer = interComm; |
---|
| 259 | } |
---|
| 260 | else |
---|
| 261 | { |
---|
[1071] | 262 | MPI_Comm_dup(intraComm, &intraCommServer); |
---|
| 263 | comms.push_back(intraCommServer); |
---|
| 264 | MPI_Comm_dup(interComm, &interCommServer); |
---|
| 265 | comms.push_back(interCommServer); |
---|
[1054] | 266 | } |
---|
[1071] | 267 | registryIn=new CRegistry(intraComm); |
---|
| 268 | registryIn->setPath(getId()) ; |
---|
| 269 | if (client->clientRank==0) registryIn->fromFile("xios_registry.bin") ; |
---|
| 270 | registryIn->bcastRegistry() ; |
---|
| 271 | registryOut=new CRegistry(intraComm) ; |
---|
| 272 | registryOut->setPath(getId()) ; |
---|
| 273 | |
---|
| 274 | server = new CContextServer(this, intraCommServer, interCommServer); |
---|
[983] | 275 | } |
---|
[1021] | 276 | else |
---|
[1054] | 277 | // initClient is called by primary server |
---|
[983] | 278 | { |
---|
[1009] | 279 | clientPrimServer.push_back(new CContextClient(this, intraComm, interComm)); |
---|
[1071] | 280 | MPI_Comm_dup(intraComm, &intraCommServer); |
---|
| 281 | comms.push_back(intraCommServer); |
---|
| 282 | MPI_Comm_dup(interComm, &interCommServer); |
---|
| 283 | comms.push_back(interCommServer); |
---|
| 284 | serverPrimServer.push_back(new CContextServer(this, intraCommServer, interCommServer)); |
---|
[983] | 285 | } |
---|
[509] | 286 | } |
---|
[219] | 287 | |
---|
[509] | 288 | void CContext::setClientServerBuffer() |
---|
| 289 | { |
---|
[731] | 290 | size_t minBufferSize = CXios::minBufferSize; |
---|
[583] | 291 | #define DECLARE_NODE(Name_, name_) \ |
---|
[731] | 292 | if (minBufferSize < sizeof(C##Name_##Definition)) minBufferSize = sizeof(C##Name_##Definition); |
---|
[583] | 293 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 294 | #include "node_type.conf" |
---|
[719] | 295 | #undef DECLARE_NODE |
---|
| 296 | #undef DECLARE_NODE_PAR |
---|
| 297 | |
---|
[917] | 298 | std::map<int, StdSize> maxEventSize; |
---|
| 299 | std::map<int, StdSize> bufferSize = getAttributesBufferSize(maxEventSize); |
---|
| 300 | std::map<int, StdSize> dataBufferSize = getDataBufferSize(maxEventSize); |
---|
[729] | 301 | |
---|
[909] | 302 | std::map<int, StdSize>::iterator it, ite = dataBufferSize.end(); |
---|
[731] | 303 | for (it = dataBufferSize.begin(); it != ite; ++it) |
---|
| 304 | if (it->second > bufferSize[it->first]) bufferSize[it->first] = it->second; |
---|
| 305 | |
---|
[909] | 306 | ite = bufferSize.end(); |
---|
| 307 | for (it = bufferSize.begin(); it != ite; ++it) |
---|
| 308 | { |
---|
| 309 | it->second *= CXios::bufferSizeFactor; |
---|
| 310 | if (it->second < minBufferSize) it->second = minBufferSize; |
---|
| 311 | } |
---|
| 312 | |
---|
[917] | 313 | // We consider that the minimum buffer size is also the minimum event size |
---|
| 314 | ite = maxEventSize.end(); |
---|
| 315 | for (it = maxEventSize.begin(); it != ite; ++it) |
---|
| 316 | if (it->second < minBufferSize) it->second = minBufferSize; |
---|
| 317 | |
---|
[729] | 318 | if (client->isServerLeader()) |
---|
[598] | 319 | { |
---|
[729] | 320 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 321 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
[917] | 322 | if (!bufferSize.count(*itRank)) bufferSize[*itRank] = maxEventSize[*itRank] = minBufferSize; |
---|
[598] | 323 | } |
---|
[983] | 324 | client->setBufferSize(bufferSize, maxEventSize); |
---|
[584] | 325 | |
---|
[983] | 326 | // If it is primary server pool, also set buffer for clientPrimServer. |
---|
| 327 | if (hasClient && hasServer) |
---|
| 328 | { |
---|
[1009] | 329 | for (int i = 0; i < clientPrimServer.size(); ++i) |
---|
[983] | 330 | { |
---|
[1009] | 331 | if (clientPrimServer[i]->isServerLeader()) |
---|
| 332 | { |
---|
| 333 | const std::list<int>& ranks = clientPrimServer[i]->getRanksServerLeader(); |
---|
| 334 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 335 | if (!bufferSize.count(*itRank)) bufferSize[*itRank] = maxEventSize[*itRank] = minBufferSize; |
---|
| 336 | } |
---|
| 337 | clientPrimServer[i]->setBufferSize(bufferSize, maxEventSize); |
---|
[983] | 338 | } |
---|
| 339 | } |
---|
[509] | 340 | } |
---|
| 341 | |
---|
| 342 | //! Verify whether a context is initialized |
---|
[461] | 343 | bool CContext::isInitialized(void) |
---|
| 344 | { |
---|
[549] | 345 | return hasClient; |
---|
[461] | 346 | } |
---|
[509] | 347 | |
---|
[983] | 348 | void CContext::initServer(MPI_Comm intraComm, MPI_Comm interComm, CContext* cxtClient /*= 0*/) |
---|
[300] | 349 | { |
---|
[549] | 350 | hasServer=true; |
---|
| 351 | server = new CContextServer(this,intraComm,interComm); |
---|
[697] | 352 | |
---|
[1071] | 353 | registryIn=new CRegistry(intraComm); |
---|
| 354 | registryIn->setPath(getId()) ; |
---|
| 355 | if (server->intraCommRank==0) registryIn->fromFile("xios_registry.bin") ; |
---|
| 356 | registryIn->bcastRegistry() ; |
---|
| 357 | registryOut=new CRegistry(intraComm) ; |
---|
| 358 | registryOut->setPath(getId()) ; |
---|
[775] | 359 | |
---|
[597] | 360 | MPI_Comm intraCommClient, interCommClient; |
---|
| 361 | if (cxtClient) // Attached mode |
---|
| 362 | { |
---|
| 363 | intraCommClient = intraComm; |
---|
| 364 | interCommClient = interComm; |
---|
| 365 | } |
---|
| 366 | else |
---|
| 367 | { |
---|
[1071] | 368 | MPI_Comm_dup(intraComm, &intraCommClient); |
---|
| 369 | comms.push_back(intraCommClient); |
---|
| 370 | MPI_Comm_dup(interComm, &interCommClient); |
---|
| 371 | comms.push_back(interCommClient); |
---|
[597] | 372 | } |
---|
[1130] | 373 | client = new CContextClient(this,intraCommClient,interCommClient,cxtClient); |
---|
[509] | 374 | } |
---|
[300] | 375 | |
---|
[597] | 376 | //! Try to send the buffers and receive possible answers |
---|
[1130] | 377 | bool CContext::checkBuffersAndListen(void) |
---|
| 378 | { |
---|
| 379 | bool clientReady, serverFinished; |
---|
[1054] | 380 | |
---|
[1130] | 381 | // Only classical servers are non-blocking |
---|
| 382 | if (CServer::serverLevel == 0) |
---|
| 383 | { |
---|
| 384 | client->checkBuffers(); |
---|
| 385 | bool hasTmpBufferedEvent = client->hasTemporarilyBufferedEvent(); |
---|
| 386 | if (hasTmpBufferedEvent) |
---|
| 387 | hasTmpBufferedEvent = !client->sendTemporarilyBufferedEvent(); |
---|
| 388 | // Don't process events if there is a temporarily buffered event |
---|
| 389 | return server->eventLoop(!hasTmpBufferedEvent); |
---|
| 390 | } |
---|
| 391 | else if (CServer::serverLevel == 1) |
---|
| 392 | { |
---|
| 393 | client->checkBuffers(); |
---|
| 394 | bool serverFinished = server->eventLoop(); |
---|
| 395 | bool serverPrimFinished = true; |
---|
| 396 | for (int i = 0; i < clientPrimServer.size(); ++i) |
---|
| 397 | { |
---|
| 398 | clientPrimServer[i]->checkBuffers(); |
---|
| 399 | serverPrimFinished *= serverPrimServer[i]->eventLoop(); |
---|
| 400 | } |
---|
| 401 | return ( serverFinished && serverPrimFinished); |
---|
| 402 | } |
---|
[1054] | 403 | |
---|
[1130] | 404 | else if (CServer::serverLevel == 2) |
---|
| 405 | { |
---|
| 406 | client->checkBuffers(); |
---|
| 407 | return server->eventLoop(); |
---|
| 408 | } |
---|
| 409 | } |
---|
[1054] | 410 | |
---|
[509] | 411 | //! Terminate a context |
---|
[300] | 412 | void CContext::finalize(void) |
---|
| 413 | { |
---|
[1130] | 414 | // Send registry upon calling the function the first time |
---|
| 415 | if (countChildCtx_ == 0) |
---|
| 416 | if (hasClient) sendRegistry() ; |
---|
| 417 | |
---|
| 418 | // Client: |
---|
| 419 | // (1) blocking send context finalize to its server |
---|
| 420 | // (2) blocking receive context finalize from its server |
---|
| 421 | if (CXios::isClient) |
---|
[1009] | 422 | { |
---|
[1130] | 423 | // Make sure that client (model) enters the loop only once |
---|
| 424 | if (countChildCtx_ < 1) |
---|
| 425 | { |
---|
| 426 | ++countChildCtx_; |
---|
[983] | 427 | |
---|
[1130] | 428 | client->finalize(); |
---|
| 429 | while (client->havePendingRequests()) |
---|
| 430 | client->checkBuffers(); |
---|
[1054] | 431 | |
---|
[1130] | 432 | while (!server->hasFinished()) |
---|
| 433 | server->eventLoop(); |
---|
| 434 | } |
---|
| 435 | } |
---|
| 436 | // Server: non-blocking send context finalize |
---|
| 437 | else if (CXios::isServer) |
---|
| 438 | { |
---|
| 439 | // First context finalize message received from a model => send context finalize to its child contexts (if any) |
---|
| 440 | if (countChildCtx_ == 0) |
---|
[1009] | 441 | for (int i = 0; i < clientPrimServer.size(); ++i) |
---|
| 442 | clientPrimServer[i]->finalize(); |
---|
[983] | 443 | |
---|
[1130] | 444 | // (Last) context finalized message received => send context finalize to its parent context |
---|
| 445 | if (countChildCtx_ == clientPrimServer.size()) |
---|
| 446 | client->finalize(); |
---|
[983] | 447 | |
---|
[1130] | 448 | ++countChildCtx_; |
---|
| 449 | } |
---|
[992] | 450 | |
---|
[1130] | 451 | // If in mode attache call postFinalize |
---|
| 452 | if (CXios::isServer && CXios::isClient) |
---|
| 453 | postFinalize(); |
---|
[300] | 454 | } |
---|
[1077] | 455 | |
---|
[1130] | 456 | /*! |
---|
| 457 | * \fn void CContext::postFinalize(void) |
---|
| 458 | * Close files, gather registries, , and make deallocations. |
---|
| 459 | * Function is called when a context is finalized (it has nothing to receive and nothing to send). |
---|
| 460 | */ |
---|
| 461 | void CContext::postFinalize(void) |
---|
| 462 | { |
---|
| 463 | info(20)<<"Context <"<<getId()<<"> is finalized."<<endl; |
---|
| 464 | |
---|
| 465 | // if (hasServer && !hasClient) |
---|
| 466 | { |
---|
| 467 | closeAllFile(); |
---|
| 468 | registryOut->hierarchicalGatherRegistry() ; |
---|
| 469 | if (server->intraCommRank==0) CXios::globalRegistry->mergeRegistry(*registryOut) ; |
---|
| 470 | } |
---|
| 471 | |
---|
| 472 | //! Free internally allocated communicators |
---|
| 473 | for (std::list<MPI_Comm>::iterator it = comms.begin(); it != comms.end(); ++it) |
---|
| 474 | MPI_Comm_free(&(*it)); |
---|
| 475 | comms.clear(); |
---|
| 476 | |
---|
| 477 | //! Deallocate client buffers |
---|
| 478 | client->releaseBuffers(); |
---|
| 479 | for (int i = 0; i < clientPrimServer.size(); ++i) |
---|
| 480 | clientPrimServer[i]->releaseBuffers(); |
---|
| 481 | } |
---|
| 482 | |
---|
[1071] | 483 | //! Free internally allocated communicators |
---|
| 484 | void CContext::freeComms(void) |
---|
| 485 | { |
---|
| 486 | for (std::list<MPI_Comm>::iterator it = comms.begin(); it != comms.end(); ++it) |
---|
| 487 | MPI_Comm_free(&(*it)); |
---|
| 488 | comms.clear(); |
---|
| 489 | } |
---|
[509] | 490 | |
---|
[1130] | 491 | //! Deallocate buffers allocated by clientContexts |
---|
| 492 | void CContext::releaseClientBuffers(void) |
---|
| 493 | { |
---|
| 494 | client->releaseBuffers(); |
---|
| 495 | for (int i = 0; i < clientPrimServer.size(); ++i) |
---|
| 496 | clientPrimServer[i]->releaseBuffers(); |
---|
| 497 | } |
---|
| 498 | |
---|
[1025] | 499 | void CContext::postProcessingGlobalAttributes() |
---|
[300] | 500 | { |
---|
[1025] | 501 | if (allProcessed) return; |
---|
| 502 | |
---|
[1099] | 503 | // if (hasClient) |
---|
| 504 | // { |
---|
[509] | 505 | // After xml is parsed, there are some more works with post processing |
---|
| 506 | postProcessing(); |
---|
[983] | 507 | |
---|
[1099] | 508 | // Check grid and calculate its distribution |
---|
| 509 | checkGridEnabledFields(); |
---|
| 510 | |
---|
| 511 | //} |
---|
| 512 | |
---|
| 513 | |
---|
| 514 | |
---|
[598] | 515 | setClientServerBuffer(); |
---|
[300] | 516 | |
---|
[983] | 517 | if (hasClient) |
---|
[510] | 518 | { |
---|
[509] | 519 | // Send all attributes of current context to server |
---|
| 520 | this->sendAllAttributesToServer(); |
---|
[459] | 521 | |
---|
[549] | 522 | // Send all attributes of current calendar |
---|
| 523 | CCalendarWrapper::get(CCalendarWrapper::GetDefName())->sendAllAttributesToServer(); |
---|
| 524 | |
---|
[509] | 525 | // We have enough information to send to server |
---|
[1021] | 526 | // First of all, send all enabled files |
---|
| 527 | sendEnabledFiles(); |
---|
[509] | 528 | |
---|
[1021] | 529 | // Then, send all enabled fields |
---|
| 530 | sendEnabledFields(); |
---|
| 531 | |
---|
[540] | 532 | // At last, we have all info of domain and axis, then send them |
---|
[569] | 533 | sendRefDomainsAxis(); |
---|
[1021] | 534 | |
---|
| 535 | // After that, send all grid (if any) |
---|
[1130] | 536 | sendRefGrid(); |
---|
[1021] | 537 | |
---|
[983] | 538 | // We have a xml tree on the server side and now, it should be also processed |
---|
| 539 | sendPostProcessing(); |
---|
[1099] | 540 | sendGridEnabledFields(); |
---|
[983] | 541 | } |
---|
[1025] | 542 | allProcessed = true; |
---|
| 543 | } |
---|
[983] | 544 | |
---|
[1025] | 545 | void CContext::sendPostProcessingGlobalAttributes() |
---|
| 546 | { |
---|
[1027] | 547 | // Use correct context client to send message |
---|
[1030] | 548 | // int nbSrvPools = (hasServer) ? clientPrimServer.size() : 1; |
---|
| 549 | int nbSrvPools = (this->hasServer) ? (this->hasClient ? this->clientPrimServer.size() : 0) : 1; |
---|
[1027] | 550 | for (int i = 0; i < nbSrvPools; ++i) |
---|
| 551 | { |
---|
| 552 | CContextClient* contextClientTmp = (0 != clientPrimServer.size()) ? clientPrimServer[i] : client; |
---|
| 553 | CEventClient event(getType(),EVENT_ID_POST_PROCESS_GLOBAL_ATTRIBUTES); |
---|
[987] | 554 | |
---|
[1027] | 555 | if (contextClientTmp->isServerLeader()) |
---|
| 556 | { |
---|
| 557 | CMessage msg; |
---|
[1099] | 558 | if (hasServer) |
---|
| 559 | msg<<this->getIdServer(i); |
---|
| 560 | else |
---|
| 561 | msg<<this->getIdServer(); |
---|
[1027] | 562 | const std::list<int>& ranks = contextClientTmp->getRanksServerLeader(); |
---|
| 563 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 564 | event.push(*itRank,1,msg); |
---|
| 565 | contextClientTmp->sendEvent(event); |
---|
| 566 | } |
---|
| 567 | else contextClientTmp->sendEvent(event); |
---|
[1025] | 568 | } |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | void CContext::recvPostProcessingGlobalAttributes(CEventServer& event) |
---|
| 572 | { |
---|
| 573 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 574 | string id; |
---|
| 575 | *buffer>>id; |
---|
[1099] | 576 | get(id)->recvPostProcessingGlobalAttributes(*buffer); |
---|
[1025] | 577 | } |
---|
| 578 | |
---|
| 579 | void CContext::recvPostProcessingGlobalAttributes(CBufferIn& buffer) |
---|
[1130] | 580 | { |
---|
| 581 | // CCalendarWrapper::get(CCalendarWrapper::GetDefName())->createCalendar(); |
---|
[1025] | 582 | postProcessingGlobalAttributes(); |
---|
| 583 | } |
---|
| 584 | |
---|
| 585 | /*! |
---|
| 586 | \brief Close all the context defintion and do processing data |
---|
| 587 | After everything is well defined on client side, they will be processed and sent to server |
---|
| 588 | From the version 2.0, sever and client work no more on the same database. Moreover, client(s) will send |
---|
| 589 | all necessary information to server, from which each server can build its own database. |
---|
| 590 | Because the role of server is to write out field data on a specific netcdf file, |
---|
| 591 | the only information that it needs is the enabled files |
---|
| 592 | and the active fields (fields will be written onto active files) |
---|
| 593 | */ |
---|
| 594 | void CContext::closeDefinition(void) |
---|
| 595 | { |
---|
| 596 | postProcessingGlobalAttributes(); |
---|
[983] | 597 | |
---|
[1130] | 598 | if (hasClient) sendPostProcessingGlobalAttributes(); |
---|
| 599 | |
---|
| 600 | // There are some processings that should be done after all of above. For example: check mask or index |
---|
[1025] | 601 | this->buildFilterGraphOfEnabledFields(); |
---|
| 602 | |
---|
[1130] | 603 | if (hasClient && !hasServer) |
---|
[509] | 604 | { |
---|
[1130] | 605 | buildFilterGraphOfFieldsWithReadAccess(); |
---|
[509] | 606 | } |
---|
| 607 | |
---|
[1099] | 608 | // if (hasClient) this->solveAllRefOfEnabledFields(true); |
---|
| 609 | checkGridEnabledFields(); |
---|
| 610 | // sendGridEnabledFields(); |
---|
| 611 | |
---|
[1025] | 612 | if (hasClient) this->sendProcessingGridOfEnabledFields(); |
---|
[1077] | 613 | if (hasClient) this->sendCloseDefinition(); |
---|
[510] | 614 | |
---|
[584] | 615 | // Nettoyage de l'arborescence |
---|
[983] | 616 | if (hasClient) CleanTree(); // Only on client side?? |
---|
[510] | 617 | |
---|
[598] | 618 | if (hasClient) |
---|
| 619 | { |
---|
| 620 | sendCreateFileHeader(); |
---|
[983] | 621 | if (!hasServer) startPrefetchingOfEnabledReadModeFiles(); |
---|
[598] | 622 | } |
---|
[300] | 623 | } |
---|
[509] | 624 | |
---|
[300] | 625 | void CContext::findAllEnabledFields(void) |
---|
| 626 | { |
---|
| 627 | for (unsigned int i = 0; i < this->enabledFiles.size(); i++) |
---|
| 628 | (void)this->enabledFiles[i]->getEnabledFields(); |
---|
| 629 | } |
---|
| 630 | |
---|
[775] | 631 | void CContext::findAllEnabledFieldsInReadModeFiles(void) |
---|
| 632 | { |
---|
| 633 | for (unsigned int i = 0; i < this->enabledReadModeFiles.size(); ++i) |
---|
| 634 | (void)this->enabledReadModeFiles[i]->getEnabledFields(); |
---|
| 635 | } |
---|
| 636 | |
---|
| 637 | void CContext::readAttributesOfEnabledFieldsInReadModeFiles() |
---|
| 638 | { |
---|
| 639 | for (unsigned int i = 0; i < this->enabledReadModeFiles.size(); ++i) |
---|
| 640 | (void)this->enabledReadModeFiles[i]->readAttributesOfEnabledFieldsInReadMode(); |
---|
| 641 | } |
---|
| 642 | |
---|
[1025] | 643 | void CContext::solveAllEnabledFields() |
---|
| 644 | { |
---|
| 645 | int size = this->enabledFiles.size(); |
---|
| 646 | for (int i = 0; i < size; ++i) |
---|
| 647 | { |
---|
| 648 | this->enabledFiles[i]->solveOnlyRefOfEnabledFields(false); |
---|
| 649 | } |
---|
| 650 | |
---|
| 651 | for (int i = 0; i < size; ++i) |
---|
| 652 | { |
---|
| 653 | this->enabledFiles[i]->generateNewTransformationGridDest(); |
---|
| 654 | } |
---|
| 655 | } |
---|
| 656 | |
---|
[1099] | 657 | void CContext::sendGridEnabledFields() |
---|
[1025] | 658 | { |
---|
| 659 | int size = this->enabledFiles.size(); |
---|
| 660 | for (int i = 0; i < size; ++i) |
---|
[1099] | 661 | { |
---|
[1025] | 662 | this->enabledFiles[i]->sendGridOfEnabledFields(); |
---|
| 663 | } |
---|
| 664 | } |
---|
| 665 | |
---|
[1099] | 666 | void CContext::checkGridEnabledFields() |
---|
| 667 | { |
---|
| 668 | int size = this->enabledFiles.size(); |
---|
| 669 | for (int i = 0; i < size; ++i) |
---|
| 670 | { |
---|
| 671 | this->enabledFiles[i]->checkGridOfEnabledFields(); |
---|
| 672 | } |
---|
| 673 | } |
---|
[1025] | 674 | |
---|
[823] | 675 | void CContext::solveOnlyRefOfEnabledFields(bool sendToServer) |
---|
| 676 | { |
---|
| 677 | int size = this->enabledFiles.size(); |
---|
| 678 | for (int i = 0; i < size; ++i) |
---|
| 679 | { |
---|
| 680 | this->enabledFiles[i]->solveOnlyRefOfEnabledFields(sendToServer); |
---|
| 681 | } |
---|
| 682 | |
---|
| 683 | for (int i = 0; i < size; ++i) |
---|
| 684 | { |
---|
| 685 | this->enabledFiles[i]->generateNewTransformationGridDest(); |
---|
| 686 | } |
---|
[1130] | 687 | |
---|
[823] | 688 | } |
---|
| 689 | |
---|
[1129] | 690 | void CContext::solveAllRefOfEnabledFieldsAndTransform(bool sendToServer) |
---|
[300] | 691 | { |
---|
[509] | 692 | int size = this->enabledFiles.size(); |
---|
| 693 | for (int i = 0; i < size; ++i) |
---|
| 694 | { |
---|
[1129] | 695 | this->enabledFiles[i]->solveAllRefOfEnabledFieldsAndTransform(sendToServer); |
---|
[509] | 696 | } |
---|
[300] | 697 | } |
---|
| 698 | |
---|
[640] | 699 | void CContext::buildFilterGraphOfEnabledFields() |
---|
| 700 | { |
---|
| 701 | int size = this->enabledFiles.size(); |
---|
| 702 | for (int i = 0; i < size; ++i) |
---|
| 703 | { |
---|
| 704 | this->enabledFiles[i]->buildFilterGraphOfEnabledFields(garbageCollector); |
---|
| 705 | } |
---|
| 706 | } |
---|
| 707 | |
---|
[598] | 708 | void CContext::startPrefetchingOfEnabledReadModeFiles() |
---|
| 709 | { |
---|
| 710 | int size = enabledReadModeFiles.size(); |
---|
| 711 | for (int i = 0; i < size; ++i) |
---|
| 712 | { |
---|
| 713 | enabledReadModeFiles[i]->prefetchEnabledReadModeFields(); |
---|
| 714 | } |
---|
| 715 | } |
---|
| 716 | |
---|
| 717 | void CContext::checkPrefetchingOfEnabledReadModeFiles() |
---|
| 718 | { |
---|
| 719 | int size = enabledReadModeFiles.size(); |
---|
| 720 | for (int i = 0; i < size; ++i) |
---|
| 721 | { |
---|
| 722 | enabledReadModeFiles[i]->prefetchEnabledReadModeFieldsIfNeeded(); |
---|
| 723 | } |
---|
| 724 | } |
---|
| 725 | |
---|
[593] | 726 | void CContext::findFieldsWithReadAccess(void) |
---|
| 727 | { |
---|
| 728 | fieldsWithReadAccess.clear(); |
---|
| 729 | const vector<CField*> allFields = CField::getAll(); |
---|
| 730 | for (size_t i = 0; i < allFields.size(); ++i) |
---|
| 731 | { |
---|
[740] | 732 | CField* field = allFields[i]; |
---|
| 733 | |
---|
| 734 | if (field->file && !field->file->mode.isEmpty() && field->file->mode == CFile::mode_attr::read) |
---|
| 735 | field->read_access = true; |
---|
| 736 | else if (!field->read_access.isEmpty() && field->read_access && (field->enabled.isEmpty() || field->enabled)) |
---|
| 737 | fieldsWithReadAccess.push_back(field); |
---|
[593] | 738 | } |
---|
| 739 | } |
---|
| 740 | |
---|
| 741 | void CContext::solveAllRefOfFieldsWithReadAccess() |
---|
| 742 | { |
---|
| 743 | for (size_t i = 0; i < fieldsWithReadAccess.size(); ++i) |
---|
| 744 | fieldsWithReadAccess[i]->solveAllReferenceEnabledField(false); |
---|
| 745 | } |
---|
| 746 | |
---|
[640] | 747 | void CContext::buildFilterGraphOfFieldsWithReadAccess() |
---|
| 748 | { |
---|
| 749 | for (size_t i = 0; i < fieldsWithReadAccess.size(); ++i) |
---|
| 750 | fieldsWithReadAccess[i]->buildFilterGraph(garbageCollector, true); |
---|
| 751 | } |
---|
| 752 | |
---|
[445] | 753 | void CContext::solveAllInheritance(bool apply) |
---|
[300] | 754 | { |
---|
| 755 | // Résolution des héritages descendants (cà d des héritages de groupes) |
---|
| 756 | // pour chacun des contextes. |
---|
[445] | 757 | solveDescInheritance(apply); |
---|
[300] | 758 | |
---|
| 759 | // Résolution des héritages par référence au niveau des fichiers. |
---|
[549] | 760 | const vector<CFile*> allFiles=CFile::getAll(); |
---|
[540] | 761 | const vector<CGrid*> allGrids= CGrid::getAll(); |
---|
[300] | 762 | |
---|
[983] | 763 | if (hasClient && !hasServer) |
---|
| 764 | //if (hasClient) |
---|
[540] | 765 | { |
---|
[510] | 766 | for (unsigned int i = 0; i < allFiles.size(); i++) |
---|
| 767 | allFiles[i]->solveFieldRefInheritance(apply); |
---|
[540] | 768 | } |
---|
| 769 | |
---|
| 770 | unsigned int vecSize = allGrids.size(); |
---|
| 771 | unsigned int i = 0; |
---|
| 772 | for (i = 0; i < vecSize; ++i) |
---|
| 773 | allGrids[i]->solveDomainAxisRefInheritance(apply); |
---|
| 774 | |
---|
[300] | 775 | } |
---|
| 776 | |
---|
| 777 | void CContext::findEnabledFiles(void) |
---|
| 778 | { |
---|
[347] | 779 | const std::vector<CFile*> allFiles = CFile::getAll(); |
---|
[300] | 780 | |
---|
| 781 | for (unsigned int i = 0; i < allFiles.size(); i++) |
---|
| 782 | if (!allFiles[i]->enabled.isEmpty()) // Si l'attribut 'enabled' est défini. |
---|
[430] | 783 | { |
---|
[300] | 784 | if (allFiles[i]->enabled.getValue()) // Si l'attribut 'enabled' est fixé à vrai. |
---|
| 785 | enabledFiles.push_back(allFiles[i]); |
---|
[430] | 786 | } |
---|
| 787 | else enabledFiles.push_back(allFiles[i]); // otherwise true by default |
---|
[300] | 788 | |
---|
[509] | 789 | |
---|
[300] | 790 | if (enabledFiles.size() == 0) |
---|
| 791 | DEBUG(<<"Aucun fichier ne va être sorti dans le contexte nommé \"" |
---|
| 792 | << getId() << "\" !"); |
---|
[1021] | 793 | |
---|
| 794 | // Assigning contextClient to each enabled file |
---|
| 795 | if (hasClient) |
---|
| 796 | { |
---|
| 797 | for (int i = 0; i < enabledFiles.size(); ++i) |
---|
| 798 | { |
---|
| 799 | if (hasServer) |
---|
| 800 | { |
---|
| 801 | int srvId = i % clientPrimServer.size(); |
---|
| 802 | enabledFiles[i]->setContextClient(clientPrimServer[srvId]); |
---|
| 803 | } |
---|
| 804 | else |
---|
| 805 | enabledFiles[i]->setContextClient(client); |
---|
| 806 | } |
---|
| 807 | } |
---|
[300] | 808 | } |
---|
| 809 | |
---|
[598] | 810 | void CContext::findEnabledReadModeFiles(void) |
---|
| 811 | { |
---|
| 812 | int size = this->enabledFiles.size(); |
---|
| 813 | for (int i = 0; i < size; ++i) |
---|
| 814 | { |
---|
| 815 | if (!enabledFiles[i]->mode.isEmpty() && enabledFiles[i]->mode.getValue() == CFile::mode_attr::read) |
---|
| 816 | enabledReadModeFiles.push_back(enabledFiles[i]); |
---|
| 817 | } |
---|
| 818 | } |
---|
| 819 | |
---|
[300] | 820 | void CContext::closeAllFile(void) |
---|
| 821 | { |
---|
[347] | 822 | std::vector<CFile*>::const_iterator |
---|
[300] | 823 | it = this->enabledFiles.begin(), end = this->enabledFiles.end(); |
---|
[509] | 824 | |
---|
[300] | 825 | for (; it != end; it++) |
---|
| 826 | { |
---|
| 827 | info(30)<<"Closing File : "<<(*it)->getId()<<endl; |
---|
| 828 | (*it)->close(); |
---|
| 829 | } |
---|
| 830 | } |
---|
[509] | 831 | |
---|
| 832 | /*! |
---|
| 833 | \brief Dispatch event received from client |
---|
| 834 | Whenever a message is received in buffer of server, it will be processed depending on |
---|
| 835 | its event type. A new event type should be added in the switch list to make sure |
---|
| 836 | it processed on server side. |
---|
| 837 | \param [in] event: Received message |
---|
| 838 | */ |
---|
[300] | 839 | bool CContext::dispatchEvent(CEventServer& event) |
---|
| 840 | { |
---|
[509] | 841 | |
---|
[549] | 842 | if (SuperClass::dispatchEvent(event)) return true; |
---|
[300] | 843 | else |
---|
| 844 | { |
---|
| 845 | switch(event.type) |
---|
| 846 | { |
---|
| 847 | case EVENT_ID_CLOSE_DEFINITION : |
---|
[549] | 848 | recvCloseDefinition(event); |
---|
| 849 | return true; |
---|
| 850 | break; |
---|
[584] | 851 | case EVENT_ID_UPDATE_CALENDAR: |
---|
[549] | 852 | recvUpdateCalendar(event); |
---|
| 853 | return true; |
---|
| 854 | break; |
---|
[300] | 855 | case EVENT_ID_CREATE_FILE_HEADER : |
---|
[549] | 856 | recvCreateFileHeader(event); |
---|
| 857 | return true; |
---|
| 858 | break; |
---|
[509] | 859 | case EVENT_ID_POST_PROCESS: |
---|
[549] | 860 | recvPostProcessing(event); |
---|
| 861 | return true; |
---|
[697] | 862 | case EVENT_ID_SEND_REGISTRY: |
---|
| 863 | recvRegistry(event); |
---|
| 864 | return true; |
---|
[1025] | 865 | break; |
---|
| 866 | case EVENT_ID_POST_PROCESS_GLOBAL_ATTRIBUTES: |
---|
| 867 | recvPostProcessingGlobalAttributes(event); |
---|
| 868 | return true; |
---|
| 869 | break; |
---|
| 870 | case EVENT_ID_PROCESS_GRID_ENABLED_FIELDS: |
---|
| 871 | recvProcessingGridOfEnabledFields(event); |
---|
| 872 | return true; |
---|
| 873 | break; |
---|
[300] | 874 | default : |
---|
| 875 | ERROR("bool CContext::dispatchEvent(CEventServer& event)", |
---|
[549] | 876 | <<"Unknown Event"); |
---|
| 877 | return false; |
---|
[300] | 878 | } |
---|
| 879 | } |
---|
| 880 | } |
---|
[509] | 881 | |
---|
| 882 | //! Client side: Send a message to server to make it close |
---|
[300] | 883 | void CContext::sendCloseDefinition(void) |
---|
| 884 | { |
---|
[987] | 885 | // Use correct context client to send message |
---|
[1030] | 886 | int nbSrvPools = (this->hasServer) ? (this->hasClient ? this->clientPrimServer.size() : 0) : 1; |
---|
[1009] | 887 | for (int i = 0; i < nbSrvPools; ++i) |
---|
[300] | 888 | { |
---|
[1009] | 889 | CContextClient* contextClientTmp = (hasServer) ? clientPrimServer[i] : client; |
---|
| 890 | CEventClient event(getType(),EVENT_ID_CLOSE_DEFINITION); |
---|
| 891 | if (contextClientTmp->isServerLeader()) |
---|
| 892 | { |
---|
| 893 | CMessage msg; |
---|
| 894 | if (hasServer) |
---|
| 895 | msg<<this->getIdServer(i); |
---|
| 896 | else |
---|
| 897 | msg<<this->getIdServer(); |
---|
| 898 | const std::list<int>& ranks = contextClientTmp->getRanksServerLeader(); |
---|
| 899 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 900 | event.push(*itRank,1,msg); |
---|
| 901 | contextClientTmp->sendEvent(event); |
---|
| 902 | } |
---|
| 903 | else contextClientTmp->sendEvent(event); |
---|
[300] | 904 | } |
---|
| 905 | } |
---|
[509] | 906 | |
---|
| 907 | //! Server side: Receive a message of client announcing a context close |
---|
[300] | 908 | void CContext::recvCloseDefinition(CEventServer& event) |
---|
| 909 | { |
---|
| 910 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 911 | string id; |
---|
[549] | 912 | *buffer>>id; |
---|
[509] | 913 | get(id)->closeDefinition(); |
---|
[300] | 914 | } |
---|
[509] | 915 | |
---|
| 916 | //! Client side: Send a message to update calendar in each time step |
---|
[300] | 917 | void CContext::sendUpdateCalendar(int step) |
---|
| 918 | { |
---|
[987] | 919 | // Use correct context client to send message |
---|
[1030] | 920 | int nbSrvPools = (this->hasServer) ? (this->hasClient ? this->clientPrimServer.size() : 0) : 1; |
---|
[1009] | 921 | for (int i = 0; i < nbSrvPools; ++i) |
---|
| 922 | { |
---|
| 923 | CContextClient* contextClientTmp = (hasServer) ? clientPrimServer[i] : client; |
---|
| 924 | CEventClient event(getType(),EVENT_ID_UPDATE_CALENDAR); |
---|
[987] | 925 | |
---|
[1009] | 926 | if (contextClientTmp->isServerLeader()) |
---|
| 927 | { |
---|
| 928 | CMessage msg; |
---|
| 929 | if (hasServer) |
---|
| 930 | msg<<this->getIdServer(i)<<step; |
---|
| 931 | else |
---|
| 932 | msg<<this->getIdServer()<<step; |
---|
| 933 | const std::list<int>& ranks = contextClientTmp->getRanksServerLeader(); |
---|
| 934 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 935 | event.push(*itRank,1,msg); |
---|
| 936 | contextClientTmp->sendEvent(event); |
---|
| 937 | } |
---|
| 938 | else contextClientTmp->sendEvent(event); |
---|
| 939 | } |
---|
[300] | 940 | } |
---|
[509] | 941 | |
---|
| 942 | //! Server side: Receive a message of client annoucing calendar update |
---|
[300] | 943 | void CContext::recvUpdateCalendar(CEventServer& event) |
---|
| 944 | { |
---|
| 945 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 946 | string id; |
---|
[549] | 947 | *buffer>>id; |
---|
| 948 | get(id)->recvUpdateCalendar(*buffer); |
---|
[300] | 949 | } |
---|
[509] | 950 | |
---|
| 951 | //! Server side: Receive a message of client annoucing calendar update |
---|
[300] | 952 | void CContext::recvUpdateCalendar(CBufferIn& buffer) |
---|
| 953 | { |
---|
[549] | 954 | int step; |
---|
| 955 | buffer>>step; |
---|
| 956 | updateCalendar(step); |
---|
[987] | 957 | if (hasClient && hasServer) |
---|
| 958 | { |
---|
| 959 | sendUpdateCalendar(step); |
---|
| 960 | } |
---|
[300] | 961 | } |
---|
[509] | 962 | |
---|
| 963 | //! Client side: Send a message to create header part of netcdf file |
---|
[300] | 964 | void CContext::sendCreateFileHeader(void) |
---|
| 965 | { |
---|
[987] | 966 | // Use correct context client to send message |
---|
[1030] | 967 | // int nbSrvPools = (hasServer) ? clientPrimServer.size() : 1; |
---|
| 968 | int nbSrvPools = (this->hasServer) ? (this->hasClient ? this->clientPrimServer.size() : 0) : 1; |
---|
[1009] | 969 | for (int i = 0; i < nbSrvPools; ++i) |
---|
| 970 | { |
---|
| 971 | CContextClient* contextClientTmp = (hasServer) ? clientPrimServer[i] : client; |
---|
| 972 | CEventClient event(getType(),EVENT_ID_CREATE_FILE_HEADER); |
---|
[983] | 973 | |
---|
[1009] | 974 | if (contextClientTmp->isServerLeader()) |
---|
| 975 | { |
---|
| 976 | CMessage msg; |
---|
| 977 | if (hasServer) |
---|
| 978 | msg<<this->getIdServer(i); |
---|
| 979 | else |
---|
| 980 | msg<<this->getIdServer(); |
---|
| 981 | const std::list<int>& ranks = contextClientTmp->getRanksServerLeader(); |
---|
| 982 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 983 | event.push(*itRank,1,msg) ; |
---|
| 984 | contextClientTmp->sendEvent(event); |
---|
| 985 | } |
---|
| 986 | else contextClientTmp->sendEvent(event); |
---|
[300] | 987 | } |
---|
| 988 | } |
---|
[509] | 989 | |
---|
| 990 | //! Server side: Receive a message of client annoucing the creation of header part of netcdf file |
---|
[300] | 991 | void CContext::recvCreateFileHeader(CEventServer& event) |
---|
| 992 | { |
---|
| 993 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 994 | string id; |
---|
[549] | 995 | *buffer>>id; |
---|
| 996 | get(id)->recvCreateFileHeader(*buffer); |
---|
[300] | 997 | } |
---|
[509] | 998 | |
---|
| 999 | //! Server side: Receive a message of client annoucing the creation of header part of netcdf file |
---|
[300] | 1000 | void CContext::recvCreateFileHeader(CBufferIn& buffer) |
---|
| 1001 | { |
---|
[987] | 1002 | // The creation of header file should be delegated to server2, for now |
---|
[1025] | 1003 | // if (hasClient && hasServer) |
---|
| 1004 | // { |
---|
| 1005 | // sendCreateFileHeader(); |
---|
| 1006 | // } |
---|
[987] | 1007 | |
---|
| 1008 | if (!hasClient && hasServer) |
---|
| 1009 | createFileHeader(); |
---|
[300] | 1010 | } |
---|
[509] | 1011 | |
---|
| 1012 | //! Client side: Send a message to do some post processing on server |
---|
[1025] | 1013 | void CContext::sendProcessingGridOfEnabledFields() |
---|
| 1014 | { |
---|
| 1015 | // Use correct context client to send message |
---|
[1030] | 1016 | // int nbSrvPools = (hasServer) ? clientPrimServer.size() : 1; |
---|
| 1017 | int nbSrvPools = (this->hasServer) ? (this->hasClient ? this->clientPrimServer.size() : 0) : 1; |
---|
[1027] | 1018 | for (int i = 0; i < nbSrvPools; ++i) |
---|
| 1019 | { |
---|
| 1020 | CContextClient* contextClientTmp = (0 != clientPrimServer.size()) ? clientPrimServer[i] : client; |
---|
| 1021 | CEventClient event(getType(),EVENT_ID_PROCESS_GRID_ENABLED_FIELDS); |
---|
[1025] | 1022 | |
---|
[1027] | 1023 | if (contextClientTmp->isServerLeader()) |
---|
| 1024 | { |
---|
| 1025 | CMessage msg; |
---|
[1099] | 1026 | if (hasServer) |
---|
| 1027 | msg<<this->getIdServer(i); |
---|
| 1028 | else |
---|
| 1029 | msg<<this->getIdServer(); |
---|
[1027] | 1030 | const std::list<int>& ranks = contextClientTmp->getRanksServerLeader(); |
---|
| 1031 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 1032 | event.push(*itRank,1,msg); |
---|
| 1033 | contextClientTmp->sendEvent(event); |
---|
| 1034 | } |
---|
| 1035 | else contextClientTmp->sendEvent(event); |
---|
[1025] | 1036 | } |
---|
| 1037 | } |
---|
| 1038 | |
---|
| 1039 | //! Server side: Receive a message to do some post processing |
---|
| 1040 | void CContext::recvProcessingGridOfEnabledFields(CEventServer& event) |
---|
| 1041 | { |
---|
| 1042 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 1043 | string id; |
---|
| 1044 | *buffer>>id; |
---|
[1099] | 1045 | //get(id)->solveOnlyRefOfEnabledFields(false); |
---|
[1025] | 1046 | } |
---|
| 1047 | |
---|
| 1048 | //! Client side: Send a message to do some post processing on server |
---|
[509] | 1049 | void CContext::sendPostProcessing() |
---|
| 1050 | { |
---|
[987] | 1051 | // Use correct context client to send message |
---|
[1030] | 1052 | // int nbSrvPools = (hasServer) ? clientPrimServer.size() : 1; |
---|
| 1053 | int nbSrvPools = (this->hasServer) ? (this->hasClient ? this->clientPrimServer.size() : 0) : 1; |
---|
[1009] | 1054 | for (int i = 0; i < nbSrvPools; ++i) |
---|
[509] | 1055 | { |
---|
[1009] | 1056 | CContextClient* contextClientTmp = (hasServer) ? clientPrimServer[i] : client; |
---|
| 1057 | CEventClient event(getType(),EVENT_ID_POST_PROCESS); |
---|
| 1058 | if (contextClientTmp->isServerLeader()) |
---|
| 1059 | { |
---|
| 1060 | CMessage msg; |
---|
| 1061 | if (hasServer) |
---|
| 1062 | msg<<this->getIdServer(i); |
---|
| 1063 | else |
---|
| 1064 | msg<<this->getIdServer(); |
---|
| 1065 | const std::list<int>& ranks = contextClientTmp->getRanksServerLeader(); |
---|
| 1066 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
[987] | 1067 | event.push(*itRank,1,msg); |
---|
[1009] | 1068 | contextClientTmp->sendEvent(event); |
---|
| 1069 | } |
---|
| 1070 | else contextClientTmp->sendEvent(event); |
---|
[509] | 1071 | } |
---|
| 1072 | } |
---|
| 1073 | |
---|
| 1074 | //! Server side: Receive a message to do some post processing |
---|
| 1075 | void CContext::recvPostProcessing(CEventServer& event) |
---|
| 1076 | { |
---|
| 1077 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 1078 | string id; |
---|
| 1079 | *buffer>>id; |
---|
| 1080 | get(id)->recvPostProcessing(*buffer); |
---|
| 1081 | } |
---|
| 1082 | |
---|
| 1083 | //! Server side: Receive a message to do some post processing |
---|
| 1084 | void CContext::recvPostProcessing(CBufferIn& buffer) |
---|
| 1085 | { |
---|
[549] | 1086 | CCalendarWrapper::get(CCalendarWrapper::GetDefName())->createCalendar(); |
---|
[509] | 1087 | postProcessing(); |
---|
| 1088 | } |
---|
| 1089 | |
---|
[511] | 1090 | const StdString& CContext::getIdServer() |
---|
| 1091 | { |
---|
| 1092 | if (hasClient) |
---|
| 1093 | { |
---|
| 1094 | idServer_ = this->getId(); |
---|
| 1095 | idServer_ += "_server"; |
---|
| 1096 | return idServer_; |
---|
| 1097 | } |
---|
| 1098 | if (hasServer) return (this->getId()); |
---|
| 1099 | } |
---|
| 1100 | |
---|
[1009] | 1101 | const StdString& CContext::getIdServer(const int i) |
---|
| 1102 | { |
---|
| 1103 | idServer_ = this->getId(); |
---|
| 1104 | idServer_ += "_server_"; |
---|
| 1105 | idServer_ += boost::lexical_cast<string>(i); |
---|
| 1106 | return idServer_; |
---|
| 1107 | } |
---|
| 1108 | |
---|
| 1109 | |
---|
[509] | 1110 | /*! |
---|
| 1111 | \brief Do some simple post processings after parsing xml file |
---|
| 1112 | After the xml file (iodef.xml) is parsed, it is necessary to build all relations among |
---|
| 1113 | created object, e.g: inhertance among fields, domain, axis. After that, all fiels as well as their parents (reference fields), |
---|
| 1114 | which will be written out into netcdf files, are processed |
---|
| 1115 | */ |
---|
| 1116 | void CContext::postProcessing() |
---|
| 1117 | { |
---|
| 1118 | if (isPostProcessed) return; |
---|
| 1119 | |
---|
[549] | 1120 | // Make sure the calendar was correctly created |
---|
| 1121 | if (!calendar) |
---|
| 1122 | ERROR("CContext::postProcessing()", << "A calendar must be defined for the context \"" << getId() << "!\"") |
---|
| 1123 | else if (calendar->getTimeStep() == NoneDu) |
---|
| 1124 | ERROR("CContext::postProcessing()", << "A timestep must be defined for the context \"" << getId() << "!\"") |
---|
[550] | 1125 | // Calendar first update to set the current date equals to the start date |
---|
| 1126 | calendar->update(0); |
---|
[509] | 1127 | |
---|
| 1128 | // Find all inheritance in xml structure |
---|
| 1129 | this->solveAllInheritance(); |
---|
| 1130 | |
---|
[992] | 1131 | // ShowTree(info(10)); |
---|
[983] | 1132 | |
---|
[676] | 1133 | // Check if some axis, domains or grids are eligible to for compressed indexed output. |
---|
| 1134 | // Warning: This must be done after solving the inheritance and before the rest of post-processing |
---|
[1025] | 1135 | checkAxisDomainsGridsEligibilityForCompressedOutput(); |
---|
[676] | 1136 | |
---|
[711] | 1137 | // Check if some automatic time series should be generated |
---|
| 1138 | // Warning: This must be done after solving the inheritance and before the rest of post-processing |
---|
[1025] | 1139 | // prepareTimeseries(); |
---|
[711] | 1140 | |
---|
[1025] | 1141 | // The timeseries should only be prepared in client |
---|
| 1142 | if (hasClient && !hasServer) prepareTimeseries(); |
---|
| 1143 | |
---|
[509] | 1144 | //Initialisation du vecteur 'enabledFiles' contenant la liste des fichiers à sortir. |
---|
| 1145 | this->findEnabledFiles(); |
---|
[1025] | 1146 | // this->findEnabledReadModeFiles(); |
---|
| 1147 | // For now, only read files with client and only one level server |
---|
| 1148 | if (hasClient && !hasServer) this->findEnabledReadModeFiles(); |
---|
[509] | 1149 | |
---|
[1025] | 1150 | |
---|
[509] | 1151 | // Find all enabled fields of each file |
---|
| 1152 | this->findAllEnabledFields(); |
---|
[1025] | 1153 | // this->findAllEnabledFieldsInReadModeFiles(); |
---|
| 1154 | // For now, only read files with client and only one level server |
---|
| 1155 | if (hasClient && !hasServer) this->findAllEnabledFieldsInReadModeFiles(); |
---|
[509] | 1156 | |
---|
[983] | 1157 | // if (hasClient) |
---|
[775] | 1158 | if (hasClient && !hasServer) |
---|
| 1159 | { |
---|
| 1160 | // Try to read attributes of fields in file then fill in corresponding grid (or domain, axis) |
---|
| 1161 | this->readAttributesOfEnabledFieldsInReadModeFiles(); |
---|
| 1162 | } |
---|
[823] | 1163 | |
---|
[1025] | 1164 | // // Only search and rebuild all reference objects of enable fields, don't transform |
---|
| 1165 | // this->solveOnlyRefOfEnabledFields(false); |
---|
| 1166 | |
---|
| 1167 | // // Search and rebuild all reference object of enabled fields |
---|
| 1168 | // this->solveAllRefOfEnabledFields(false); |
---|
| 1169 | |
---|
| 1170 | // // Find all fields with read access from the public API |
---|
| 1171 | // findFieldsWithReadAccess(); |
---|
| 1172 | // // and solve the all reference for them |
---|
| 1173 | // solveAllRefOfFieldsWithReadAccess(); |
---|
| 1174 | |
---|
[823] | 1175 | // Only search and rebuild all reference objects of enable fields, don't transform |
---|
[1099] | 1176 | this->solveOnlyRefOfEnabledFields(false); |
---|
[823] | 1177 | |
---|
[1099] | 1178 | // Search and rebuild all reference object of enabled fields, and transform |
---|
[1129] | 1179 | // this->solveAllEnabledFields |
---|
| 1180 | this->solveAllRefOfEnabledFieldsAndTransform(false); |
---|
[1099] | 1181 | // // Check grid and calculate its distribution |
---|
| 1182 | // if (hasClient) checkGridEnabledFields(); |
---|
| 1183 | |
---|
[593] | 1184 | // Find all fields with read access from the public API |
---|
[1025] | 1185 | if (hasClient && !hasServer) findFieldsWithReadAccess(); |
---|
[593] | 1186 | // and solve the all reference for them |
---|
[1025] | 1187 | if (hasClient && !hasServer) solveAllRefOfFieldsWithReadAccess(); |
---|
[593] | 1188 | |
---|
[509] | 1189 | isPostProcessed = true; |
---|
| 1190 | } |
---|
| 1191 | |
---|
[917] | 1192 | /*! |
---|
| 1193 | * Compute the required buffer size to send the attributes (mostly those grid related). |
---|
| 1194 | * |
---|
| 1195 | * \param maxEventSize [in/out] the size of the bigger event for each connected server |
---|
| 1196 | */ |
---|
| 1197 | std::map<int, StdSize> CContext::getAttributesBufferSize(std::map<int, StdSize>& maxEventSize) |
---|
[509] | 1198 | { |
---|
[731] | 1199 | std::map<int, StdSize> attributesSize; |
---|
| 1200 | |
---|
[908] | 1201 | if (hasClient) |
---|
[731] | 1202 | { |
---|
[908] | 1203 | size_t numEnabledFiles = this->enabledFiles.size(); |
---|
| 1204 | for (size_t i = 0; i < numEnabledFiles; ++i) |
---|
| 1205 | { |
---|
| 1206 | CFile* file = this->enabledFiles[i]; |
---|
[731] | 1207 | |
---|
[908] | 1208 | std::vector<CField*> enabledFields = file->getEnabledFields(); |
---|
| 1209 | size_t numEnabledFields = enabledFields.size(); |
---|
| 1210 | for (size_t j = 0; j < numEnabledFields; ++j) |
---|
[731] | 1211 | { |
---|
[908] | 1212 | const std::map<int, StdSize> mapSize = enabledFields[j]->getGridAttributesBufferSize(); |
---|
| 1213 | std::map<int, StdSize>::const_iterator it = mapSize.begin(), itE = mapSize.end(); |
---|
| 1214 | for (; it != itE; ++it) |
---|
| 1215 | { |
---|
| 1216 | // If attributesSize[it->first] does not exist, it will be zero-initialized |
---|
| 1217 | // so we can use it safely without checking for its existance |
---|
| 1218 | if (attributesSize[it->first] < it->second) |
---|
| 1219 | attributesSize[it->first] = it->second; |
---|
[917] | 1220 | |
---|
| 1221 | if (maxEventSize[it->first] < it->second) |
---|
| 1222 | maxEventSize[it->first] = it->second; |
---|
[908] | 1223 | } |
---|
[731] | 1224 | } |
---|
| 1225 | } |
---|
| 1226 | } |
---|
| 1227 | |
---|
| 1228 | return attributesSize; |
---|
| 1229 | } |
---|
| 1230 | |
---|
[917] | 1231 | /*! |
---|
| 1232 | * Compute the required buffer size to send the fields data. |
---|
| 1233 | * |
---|
| 1234 | * \param maxEventSize [in/out] the size of the bigger event for each connected server |
---|
| 1235 | */ |
---|
| 1236 | std::map<int, StdSize> CContext::getDataBufferSize(std::map<int, StdSize>& maxEventSize) |
---|
[731] | 1237 | { |
---|
[598] | 1238 | CFile::mode_attr::t_enum mode = hasClient ? CFile::mode_attr::write : CFile::mode_attr::read; |
---|
| 1239 | |
---|
[730] | 1240 | std::map<int, StdSize> dataSize; |
---|
[509] | 1241 | |
---|
| 1242 | // Find all reference domain and axis of all active fields |
---|
[730] | 1243 | size_t numEnabledFiles = this->enabledFiles.size(); |
---|
| 1244 | for (size_t i = 0; i < numEnabledFiles; ++i) |
---|
[509] | 1245 | { |
---|
[598] | 1246 | CFile* file = this->enabledFiles[i]; |
---|
| 1247 | CFile::mode_attr::t_enum fileMode = file->mode.isEmpty() ? CFile::mode_attr::write : file->mode.getValue(); |
---|
| 1248 | |
---|
| 1249 | if (fileMode == mode) |
---|
[509] | 1250 | { |
---|
[598] | 1251 | std::vector<CField*> enabledFields = file->getEnabledFields(); |
---|
[730] | 1252 | size_t numEnabledFields = enabledFields.size(); |
---|
| 1253 | for (size_t j = 0; j < numEnabledFields; ++j) |
---|
[509] | 1254 | { |
---|
[1099] | 1255 | const std::vector<std::map<int, StdSize> > mapSize = enabledFields[j]->getGridDataBufferSize(); |
---|
| 1256 | for (size_t c = 0; c < mapSize.size(); ++c) |
---|
[509] | 1257 | { |
---|
[1099] | 1258 | std::map<int, StdSize>::const_iterator it = mapSize[c].begin(), itE = mapSize[c].end(); |
---|
| 1259 | for (; it != itE; ++it) |
---|
| 1260 | { |
---|
| 1261 | // If dataSize[it->first] does not exist, it will be zero-initialized |
---|
| 1262 | // so we can use it safely without checking for its existance |
---|
| 1263 | if (CXios::isOptPerformance) |
---|
| 1264 | dataSize[it->first] += it->second; |
---|
| 1265 | else if (dataSize[it->first] < it->second) |
---|
| 1266 | dataSize[it->first] = it->second; |
---|
[917] | 1267 | |
---|
[1099] | 1268 | if (maxEventSize[it->first] < it->second) |
---|
| 1269 | maxEventSize[it->first] = it->second; |
---|
| 1270 | } |
---|
[598] | 1271 | } |
---|
[509] | 1272 | } |
---|
| 1273 | } |
---|
| 1274 | } |
---|
| 1275 | |
---|
[730] | 1276 | return dataSize; |
---|
[509] | 1277 | } |
---|
| 1278 | |
---|
| 1279 | //! Client side: Send infomation of active files (files are enabled to write out) |
---|
| 1280 | void CContext::sendEnabledFiles() |
---|
| 1281 | { |
---|
| 1282 | int size = this->enabledFiles.size(); |
---|
| 1283 | |
---|
| 1284 | // In a context, each type has a root definition, e.g: axis, domain, field. |
---|
| 1285 | // Every object must be a child of one of these root definition. In this case |
---|
| 1286 | // all new file objects created on server must be children of the root "file_definition" |
---|
| 1287 | StdString fileDefRoot("file_definition"); |
---|
| 1288 | CFileGroup* cfgrpPtr = CFileGroup::get(fileDefRoot); |
---|
| 1289 | for (int i = 0; i < size; ++i) |
---|
| 1290 | { |
---|
[1021] | 1291 | cfgrpPtr->sendCreateChild(this->enabledFiles[i]->getId(),enabledFiles[i]->getContextClient()); |
---|
| 1292 | this->enabledFiles[i]->sendAllAttributesToServer(enabledFiles[i]->getContextClient()); |
---|
| 1293 | this->enabledFiles[i]->sendAddAllVariables(enabledFiles[i]->getContextClient()); |
---|
[509] | 1294 | } |
---|
| 1295 | } |
---|
| 1296 | |
---|
| 1297 | //! Client side: Send information of active fields (ones are written onto files) |
---|
| 1298 | void CContext::sendEnabledFields() |
---|
| 1299 | { |
---|
| 1300 | int size = this->enabledFiles.size(); |
---|
| 1301 | for (int i = 0; i < size; ++i) |
---|
| 1302 | { |
---|
[1021] | 1303 | this->enabledFiles[i]->sendEnabledFields(enabledFiles[i]->getContextClient()); |
---|
[509] | 1304 | } |
---|
| 1305 | } |
---|
| 1306 | |
---|
[676] | 1307 | //! Client side: Check if the defined axis, domains and grids are eligible for compressed indexed output |
---|
| 1308 | void CContext::checkAxisDomainsGridsEligibilityForCompressedOutput() |
---|
| 1309 | { |
---|
| 1310 | if (!hasClient) return; |
---|
| 1311 | |
---|
| 1312 | const vector<CAxis*> allAxis = CAxis::getAll(); |
---|
| 1313 | for (vector<CAxis*>::const_iterator it = allAxis.begin(); it != allAxis.end(); it++) |
---|
| 1314 | (*it)->checkEligibilityForCompressedOutput(); |
---|
| 1315 | |
---|
| 1316 | const vector<CDomain*> allDomains = CDomain::getAll(); |
---|
| 1317 | for (vector<CDomain*>::const_iterator it = allDomains.begin(); it != allDomains.end(); it++) |
---|
| 1318 | (*it)->checkEligibilityForCompressedOutput(); |
---|
| 1319 | |
---|
| 1320 | const vector<CGrid*> allGrids = CGrid::getAll(); |
---|
| 1321 | for (vector<CGrid*>::const_iterator it = allGrids.begin(); it != allGrids.end(); it++) |
---|
| 1322 | (*it)->checkEligibilityForCompressedOutput(); |
---|
| 1323 | } |
---|
| 1324 | |
---|
[711] | 1325 | //! Client side: Prepare the timeseries by adding the necessary files |
---|
| 1326 | void CContext::prepareTimeseries() |
---|
| 1327 | { |
---|
| 1328 | if (!hasClient) return; |
---|
| 1329 | |
---|
| 1330 | const std::vector<CFile*> allFiles = CFile::getAll(); |
---|
| 1331 | for (size_t i = 0; i < allFiles.size(); i++) |
---|
| 1332 | { |
---|
| 1333 | CFile* file = allFiles[i]; |
---|
| 1334 | |
---|
| 1335 | if (!file->timeseries.isEmpty() && file->timeseries != CFile::timeseries_attr::none) |
---|
| 1336 | { |
---|
[773] | 1337 | StdString tsPrefix = !file->ts_prefix.isEmpty() ? file->ts_prefix : file->getFileOutputName(); |
---|
[711] | 1338 | |
---|
| 1339 | const std::vector<CField*> allFields = file->getAllFields(); |
---|
| 1340 | for (size_t j = 0; j < allFields.size(); j++) |
---|
| 1341 | { |
---|
| 1342 | CField* field = allFields[j]; |
---|
| 1343 | |
---|
| 1344 | if (!field->ts_enabled.isEmpty() && field->ts_enabled) |
---|
| 1345 | { |
---|
| 1346 | CFile* tsFile = CFile::create(); |
---|
| 1347 | tsFile->duplicateAttributes(file); |
---|
[928] | 1348 | tsFile->setVirtualVariableGroup(file->getVirtualVariableGroup()); |
---|
[711] | 1349 | |
---|
| 1350 | tsFile->name = tsPrefix + "_"; |
---|
| 1351 | if (!field->name.isEmpty()) |
---|
| 1352 | tsFile->name.get() += field->name; |
---|
| 1353 | else if (field->hasDirectFieldReference()) // We cannot use getBaseFieldReference() just yet |
---|
| 1354 | tsFile->name.get() += field->field_ref; |
---|
| 1355 | else |
---|
| 1356 | tsFile->name.get() += field->getId(); |
---|
| 1357 | |
---|
| 1358 | if (!field->ts_split_freq.isEmpty()) |
---|
| 1359 | tsFile->split_freq = field->ts_split_freq; |
---|
| 1360 | |
---|
| 1361 | CField* tsField = tsFile->addField(); |
---|
| 1362 | tsField->field_ref = field->getId(); |
---|
[958] | 1363 | tsField->setVirtualVariableGroup(field->getVirtualVariableGroup()); |
---|
[711] | 1364 | |
---|
| 1365 | tsFile->solveFieldRefInheritance(true); |
---|
| 1366 | |
---|
| 1367 | if (file->timeseries == CFile::timeseries_attr::exclusive) |
---|
| 1368 | field->enabled = false; |
---|
| 1369 | } |
---|
| 1370 | } |
---|
| 1371 | |
---|
| 1372 | // Finally disable the original file is need be |
---|
| 1373 | if (file->timeseries == CFile::timeseries_attr::only) |
---|
| 1374 | file->enabled = false; |
---|
| 1375 | } |
---|
| 1376 | } |
---|
| 1377 | } |
---|
| 1378 | |
---|
[509] | 1379 | //! Client side: Send information of reference grid of active fields |
---|
| 1380 | void CContext::sendRefGrid() |
---|
| 1381 | { |
---|
| 1382 | std::set<StdString> gridIds; |
---|
| 1383 | int sizeFile = this->enabledFiles.size(); |
---|
| 1384 | CFile* filePtr(NULL); |
---|
| 1385 | |
---|
| 1386 | // Firstly, find all reference grids of all active fields |
---|
| 1387 | for (int i = 0; i < sizeFile; ++i) |
---|
| 1388 | { |
---|
| 1389 | filePtr = this->enabledFiles[i]; |
---|
| 1390 | std::vector<CField*> enabledFields = filePtr->getEnabledFields(); |
---|
| 1391 | int sizeField = enabledFields.size(); |
---|
| 1392 | for (int numField = 0; numField < sizeField; ++numField) |
---|
| 1393 | { |
---|
| 1394 | if (0 != enabledFields[numField]->getRelGrid()) |
---|
| 1395 | gridIds.insert(CGrid::get(enabledFields[numField]->getRelGrid())->getId()); |
---|
| 1396 | } |
---|
| 1397 | } |
---|
| 1398 | |
---|
| 1399 | // Create all reference grids on server side |
---|
| 1400 | StdString gridDefRoot("grid_definition"); |
---|
| 1401 | CGridGroup* gridPtr = CGridGroup::get(gridDefRoot); |
---|
| 1402 | std::set<StdString>::const_iterator it, itE = gridIds.end(); |
---|
| 1403 | for (it = gridIds.begin(); it != itE; ++it) |
---|
| 1404 | { |
---|
| 1405 | gridPtr->sendCreateChild(*it); |
---|
| 1406 | CGrid::get(*it)->sendAllAttributesToServer(); |
---|
[540] | 1407 | CGrid::get(*it)->sendAllDomains(); |
---|
| 1408 | CGrid::get(*it)->sendAllAxis(); |
---|
[887] | 1409 | CGrid::get(*it)->sendAllScalars(); |
---|
[509] | 1410 | } |
---|
| 1411 | } |
---|
| 1412 | |
---|
| 1413 | |
---|
| 1414 | //! Client side: Send information of reference domain and axis of active fields |
---|
[569] | 1415 | void CContext::sendRefDomainsAxis() |
---|
| 1416 | { |
---|
[887] | 1417 | std::set<StdString> domainIds, axisIds, scalarIds; |
---|
[509] | 1418 | |
---|
[569] | 1419 | // Find all reference domain and axis of all active fields |
---|
| 1420 | int numEnabledFiles = this->enabledFiles.size(); |
---|
| 1421 | for (int i = 0; i < numEnabledFiles; ++i) |
---|
| 1422 | { |
---|
| 1423 | std::vector<CField*> enabledFields = this->enabledFiles[i]->getEnabledFields(); |
---|
| 1424 | int numEnabledFields = enabledFields.size(); |
---|
| 1425 | for (int j = 0; j < numEnabledFields; ++j) |
---|
| 1426 | { |
---|
[887] | 1427 | const std::vector<StdString>& prDomAxisScalarId = enabledFields[j]->getRefDomainAxisIds(); |
---|
| 1428 | if ("" != prDomAxisScalarId[0]) domainIds.insert(prDomAxisScalarId[0]); |
---|
| 1429 | if ("" != prDomAxisScalarId[1]) axisIds.insert(prDomAxisScalarId[1]); |
---|
| 1430 | if ("" != prDomAxisScalarId[2]) scalarIds.insert(prDomAxisScalarId[2]); |
---|
[569] | 1431 | } |
---|
| 1432 | } |
---|
| 1433 | |
---|
| 1434 | // Create all reference axis on server side |
---|
[887] | 1435 | std::set<StdString>::iterator itDom, itAxis, itScalar; |
---|
[569] | 1436 | std::set<StdString>::const_iterator itE; |
---|
| 1437 | |
---|
[887] | 1438 | StdString scalarDefRoot("scalar_definition"); |
---|
| 1439 | CScalarGroup* scalarPtr = CScalarGroup::get(scalarDefRoot); |
---|
| 1440 | itE = scalarIds.end(); |
---|
| 1441 | for (itScalar = scalarIds.begin(); itScalar != itE; ++itScalar) |
---|
| 1442 | { |
---|
| 1443 | if (!itScalar->empty()) |
---|
| 1444 | { |
---|
| 1445 | scalarPtr->sendCreateChild(*itScalar); |
---|
| 1446 | CScalar::get(*itScalar)->sendAllAttributesToServer(); |
---|
| 1447 | } |
---|
| 1448 | } |
---|
| 1449 | |
---|
[569] | 1450 | StdString axiDefRoot("axis_definition"); |
---|
| 1451 | CAxisGroup* axisPtr = CAxisGroup::get(axiDefRoot); |
---|
| 1452 | itE = axisIds.end(); |
---|
| 1453 | for (itAxis = axisIds.begin(); itAxis != itE; ++itAxis) |
---|
| 1454 | { |
---|
| 1455 | if (!itAxis->empty()) |
---|
| 1456 | { |
---|
| 1457 | axisPtr->sendCreateChild(*itAxis); |
---|
| 1458 | CAxis::get(*itAxis)->sendAllAttributesToServer(); |
---|
| 1459 | } |
---|
| 1460 | } |
---|
| 1461 | |
---|
| 1462 | // Create all reference domains on server side |
---|
| 1463 | StdString domDefRoot("domain_definition"); |
---|
| 1464 | CDomainGroup* domPtr = CDomainGroup::get(domDefRoot); |
---|
| 1465 | itE = domainIds.end(); |
---|
| 1466 | for (itDom = domainIds.begin(); itDom != itE; ++itDom) |
---|
| 1467 | { |
---|
| 1468 | if (!itDom->empty()) { |
---|
| 1469 | domPtr->sendCreateChild(*itDom); |
---|
| 1470 | CDomain::get(*itDom)->sendAllAttributesToServer(); |
---|
| 1471 | } |
---|
| 1472 | } |
---|
| 1473 | } |
---|
| 1474 | |
---|
[509] | 1475 | //! Update calendar in each time step |
---|
[300] | 1476 | void CContext::updateCalendar(int step) |
---|
| 1477 | { |
---|
[1130] | 1478 | info(50) <<"Context "<< this->getId() <<" updateCalendar : before : " << calendar->getCurrentDate() << endl; |
---|
[549] | 1479 | calendar->update(step); |
---|
[1130] | 1480 | info(50) <<"Context "<< this->getId() << " updateCalendar : after : " << calendar->getCurrentDate() << endl; |
---|
[639] | 1481 | |
---|
| 1482 | if (hasClient) |
---|
| 1483 | { |
---|
[806] | 1484 | checkPrefetchingOfEnabledReadModeFiles(); |
---|
[639] | 1485 | garbageCollector.invalidate(calendar->getCurrentDate()); |
---|
| 1486 | } |
---|
[300] | 1487 | } |
---|
[509] | 1488 | |
---|
| 1489 | //! Server side: Create header of netcdf file |
---|
[300] | 1490 | void CContext::createFileHeader(void ) |
---|
| 1491 | { |
---|
[549] | 1492 | vector<CFile*>::const_iterator it; |
---|
[509] | 1493 | |
---|
[300] | 1494 | for (it=enabledFiles.begin(); it != enabledFiles.end(); it++) |
---|
| 1495 | { |
---|
[321] | 1496 | (*it)->initFile(); |
---|
[300] | 1497 | } |
---|
[509] | 1498 | } |
---|
| 1499 | |
---|
| 1500 | //! Get current context |
---|
[347] | 1501 | CContext* CContext::getCurrent(void) |
---|
[300] | 1502 | { |
---|
[549] | 1503 | return CObjectFactory::GetObject<CContext>(CObjectFactory::GetCurrentContextId()).get(); |
---|
[300] | 1504 | } |
---|
[509] | 1505 | |
---|
| 1506 | /*! |
---|
| 1507 | \brief Set context with an id be the current context |
---|
| 1508 | \param [in] id identity of context to be set to current |
---|
| 1509 | */ |
---|
[346] | 1510 | void CContext::setCurrent(const string& id) |
---|
| 1511 | { |
---|
| 1512 | CObjectFactory::SetCurrentContextId(id); |
---|
| 1513 | CGroupFactory::SetCurrentContextId(id); |
---|
| 1514 | } |
---|
[509] | 1515 | |
---|
| 1516 | /*! |
---|
| 1517 | \brief Create a context with specific id |
---|
| 1518 | \param [in] id identity of new context |
---|
| 1519 | \return pointer to the new context or already-existed one with identity id |
---|
| 1520 | */ |
---|
[347] | 1521 | CContext* CContext::create(const StdString& id) |
---|
[346] | 1522 | { |
---|
[549] | 1523 | CContext::setCurrent(id); |
---|
[509] | 1524 | |
---|
[346] | 1525 | bool hasctxt = CContext::has(id); |
---|
[347] | 1526 | CContext* context = CObjectFactory::CreateObject<CContext>(id).get(); |
---|
[549] | 1527 | getRoot(); |
---|
[347] | 1528 | if (!hasctxt) CGroupFactory::AddChild(root, context->getShared()); |
---|
[346] | 1529 | |
---|
| 1530 | #define DECLARE_NODE(Name_, name_) \ |
---|
| 1531 | C##Name_##Definition::create(C##Name_##Definition::GetDefName()); |
---|
| 1532 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 1533 | #include "node_type.conf" |
---|
| 1534 | |
---|
| 1535 | return (context); |
---|
| 1536 | } |
---|
[697] | 1537 | |
---|
| 1538 | |
---|
| 1539 | //! Server side: Receive a message to do some post processing |
---|
| 1540 | void CContext::recvRegistry(CEventServer& event) |
---|
| 1541 | { |
---|
| 1542 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 1543 | string id; |
---|
| 1544 | *buffer>>id; |
---|
| 1545 | get(id)->recvRegistry(*buffer); |
---|
| 1546 | } |
---|
| 1547 | |
---|
| 1548 | void CContext::recvRegistry(CBufferIn& buffer) |
---|
| 1549 | { |
---|
| 1550 | if (server->intraCommRank==0) |
---|
| 1551 | { |
---|
| 1552 | CRegistry registry(server->intraComm) ; |
---|
| 1553 | registry.fromBuffer(buffer) ; |
---|
| 1554 | registryOut->mergeRegistry(registry) ; |
---|
| 1555 | } |
---|
| 1556 | } |
---|
| 1557 | |
---|
| 1558 | void CContext::sendRegistry(void) |
---|
[987] | 1559 | { |
---|
[697] | 1560 | registryOut->hierarchicalGatherRegistry() ; |
---|
| 1561 | |
---|
[1009] | 1562 | // Use correct context client to send message |
---|
[1030] | 1563 | int nbSrvPools = (this->hasServer) ? (this->hasClient ? this->clientPrimServer.size() : 0) : 1; |
---|
[1009] | 1564 | for (int i = 0; i < nbSrvPools; ++i) |
---|
| 1565 | { |
---|
| 1566 | CContextClient* contextClientTmp = (hasServer) ? clientPrimServer[i] : client; |
---|
| 1567 | CEventClient event(CContext::GetType(), CContext::EVENT_ID_SEND_REGISTRY); |
---|
[1021] | 1568 | if (contextClientTmp->isServerLeader()) |
---|
[1009] | 1569 | { |
---|
| 1570 | CMessage msg ; |
---|
| 1571 | if (hasServer) |
---|
| 1572 | msg<<this->getIdServer(i); |
---|
| 1573 | else |
---|
| 1574 | msg<<this->getIdServer(); |
---|
| 1575 | if (contextClientTmp->clientRank==0) msg<<*registryOut ; |
---|
[1021] | 1576 | const std::list<int>& ranks = contextClientTmp->getRanksServerLeader(); |
---|
[1009] | 1577 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 1578 | event.push(*itRank,1,msg); |
---|
| 1579 | contextClientTmp->sendEvent(event); |
---|
| 1580 | } |
---|
| 1581 | else contextClientTmp->sendEvent(event); |
---|
| 1582 | } |
---|
[697] | 1583 | } |
---|
| 1584 | |
---|
[1130] | 1585 | /*! |
---|
| 1586 | * \fn bool CContext::isFinalized(void) |
---|
| 1587 | * Context is finalized if: |
---|
| 1588 | * (1) it has received all context finalize events |
---|
| 1589 | * (2) it has no pending events to send. |
---|
| 1590 | */ |
---|
[1054] | 1591 | bool CContext::isFinalized(void) |
---|
| 1592 | { |
---|
[1130] | 1593 | if (countChildCtx_==clientPrimServer.size()+1) |
---|
| 1594 | { |
---|
| 1595 | bool buffersReleased = !client->havePendingRequests(); |
---|
| 1596 | for (int i = 0; i < clientPrimServer.size(); ++i) |
---|
| 1597 | buffersReleased *= !clientPrimServer[i]->havePendingRequests(); |
---|
| 1598 | return buffersReleased; |
---|
| 1599 | } |
---|
| 1600 | else |
---|
| 1601 | return false; |
---|
[1054] | 1602 | } |
---|
| 1603 | |
---|
[335] | 1604 | } // namespace xios |
---|