[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 | |
---|
[697] | 17 | |
---|
[335] | 18 | namespace xios { |
---|
[509] | 19 | |
---|
[549] | 20 | shared_ptr<CContextGroup> CContext::root; |
---|
[509] | 21 | |
---|
[219] | 22 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 23 | |
---|
| 24 | CContext::CContext(void) |
---|
| 25 | : CObjectTemplate<CContext>(), CContextAttributes() |
---|
[597] | 26 | , calendar(), hasClient(false), hasServer(false), isPostProcessed(false), finalized(false) |
---|
[730] | 27 | , idServer_(), client(0), server(0) |
---|
[219] | 28 | { /* Ne rien faire de plus */ } |
---|
| 29 | |
---|
| 30 | CContext::CContext(const StdString & id) |
---|
| 31 | : CObjectTemplate<CContext>(id), CContextAttributes() |
---|
[597] | 32 | , calendar(), hasClient(false), hasServer(false), isPostProcessed(false), finalized(false) |
---|
[730] | 33 | , idServer_(), client(0), server(0) |
---|
[219] | 34 | { /* Ne rien faire de plus */ } |
---|
| 35 | |
---|
| 36 | CContext::~CContext(void) |
---|
[509] | 37 | { |
---|
[597] | 38 | delete client; |
---|
| 39 | delete server; |
---|
[300] | 40 | } |
---|
[219] | 41 | |
---|
| 42 | //---------------------------------------------------------------- |
---|
[509] | 43 | //! Get name of context |
---|
[219] | 44 | StdString CContext::GetName(void) { return (StdString("context")); } |
---|
| 45 | StdString CContext::GetDefName(void){ return (CContext::GetName()); } |
---|
| 46 | ENodeType CContext::GetType(void) { return (eContext); } |
---|
| 47 | |
---|
| 48 | //---------------------------------------------------------------- |
---|
[640] | 49 | |
---|
[509] | 50 | /*! |
---|
| 51 | \brief Get context group (context root) |
---|
| 52 | \return Context root |
---|
| 53 | */ |
---|
[347] | 54 | CContextGroup* CContext::getRoot(void) |
---|
[509] | 55 | { |
---|
[549] | 56 | if (root.get()==NULL) root=shared_ptr<CContextGroup>(new CContextGroup(xml::CXMLNode::GetRootName())); |
---|
[509] | 57 | return root.get(); |
---|
[219] | 58 | } |
---|
| 59 | |
---|
[640] | 60 | //---------------------------------------------------------------- |
---|
[509] | 61 | |
---|
| 62 | /*! |
---|
| 63 | \brief Get calendar of a context |
---|
| 64 | \return Calendar |
---|
| 65 | */ |
---|
[343] | 66 | boost::shared_ptr<CCalendar> CContext::getCalendar(void) const |
---|
[219] | 67 | { |
---|
| 68 | return (this->calendar); |
---|
| 69 | } |
---|
[509] | 70 | |
---|
[219] | 71 | //---------------------------------------------------------------- |
---|
[640] | 72 | |
---|
[509] | 73 | /*! |
---|
| 74 | \brief Set a context with a calendar |
---|
| 75 | \param[in] newCalendar new calendar |
---|
| 76 | */ |
---|
[343] | 77 | void CContext::setCalendar(boost::shared_ptr<CCalendar> newCalendar) |
---|
[219] | 78 | { |
---|
| 79 | this->calendar = newCalendar; |
---|
| 80 | } |
---|
[509] | 81 | |
---|
[219] | 82 | //---------------------------------------------------------------- |
---|
[509] | 83 | /*! |
---|
| 84 | \brief Parse xml file and write information into context object |
---|
| 85 | \param [in] node xmld node corresponding in xml file |
---|
| 86 | */ |
---|
[219] | 87 | void CContext::parse(xml::CXMLNode & node) |
---|
| 88 | { |
---|
| 89 | CContext::SuperClass::parse(node); |
---|
| 90 | |
---|
| 91 | // PARSING POUR GESTION DES ENFANTS |
---|
[278] | 92 | xml::THashAttributes attributes = node.getAttributes(); |
---|
[219] | 93 | |
---|
[278] | 94 | if (attributes.end() != attributes.find("src")) |
---|
| 95 | { |
---|
| 96 | StdIFStream ifs ( attributes["src"].c_str() , StdIFStream::in ); |
---|
[462] | 97 | if ( (ifs.rdstate() & std::ifstream::failbit ) != 0 ) |
---|
| 98 | ERROR("void CContext::parse(xml::CXMLNode & node)", |
---|
| 99 | <<endl<< "Can not open <"<<attributes["src"].c_str()<<"> file" ); |
---|
[278] | 100 | if (!ifs.good()) |
---|
| 101 | ERROR("CContext::parse(xml::CXMLNode & node)", |
---|
| 102 | << "[ filename = " << attributes["src"] << " ] Bad xml stream !"); |
---|
[462] | 103 | xml::CXMLParser::ParseInclude(ifs, attributes["src"], *this); |
---|
[278] | 104 | } |
---|
| 105 | |
---|
[219] | 106 | if (node.getElementName().compare(CContext::GetName())) |
---|
[421] | 107 | DEBUG("Le noeud is wrong defined but will be considered as a context !"); |
---|
[219] | 108 | |
---|
| 109 | if (!(node.goToChildElement())) |
---|
| 110 | { |
---|
| 111 | DEBUG("Le context ne contient pas d'enfant !"); |
---|
| 112 | } |
---|
| 113 | else |
---|
| 114 | { |
---|
| 115 | do { // Parcours des contextes pour traitement. |
---|
| 116 | |
---|
| 117 | StdString name = node.getElementName(); |
---|
| 118 | attributes.clear(); |
---|
| 119 | attributes = node.getAttributes(); |
---|
| 120 | |
---|
| 121 | if (attributes.end() != attributes.find("id")) |
---|
[509] | 122 | { |
---|
| 123 | DEBUG(<< "Definition node has an id," |
---|
| 124 | << "it will not be taking account !"); |
---|
| 125 | } |
---|
[219] | 126 | |
---|
| 127 | #define DECLARE_NODE(Name_, name_) \ |
---|
| 128 | if (name.compare(C##Name_##Definition::GetDefName()) == 0) \ |
---|
[549] | 129 | { C##Name_##Definition::create(C##Name_##Definition::GetDefName()) -> parse(node); continue; } |
---|
[219] | 130 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 131 | #include "node_type.conf" |
---|
[286] | 132 | |
---|
[421] | 133 | DEBUG(<< "The element \'" << name |
---|
| 134 | << "\' in the context \'" << CContext::getCurrent()->getId() |
---|
| 135 | << "\' is not a definition !"); |
---|
[219] | 136 | |
---|
| 137 | } while (node.goToNextElement()); |
---|
| 138 | |
---|
| 139 | node.goToParentElement(); // Retour au parent |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | //---------------------------------------------------------------- |
---|
[509] | 144 | //! Show tree structure of context |
---|
[219] | 145 | void CContext::ShowTree(StdOStream & out) |
---|
| 146 | { |
---|
[549] | 147 | StdString currentContextId = CContext::getCurrent() -> getId(); |
---|
[347] | 148 | std::vector<CContext*> def_vector = |
---|
[346] | 149 | CContext::getRoot()->getChildList(); |
---|
[347] | 150 | std::vector<CContext*>::iterator |
---|
[219] | 151 | it = def_vector.begin(), end = def_vector.end(); |
---|
| 152 | |
---|
| 153 | out << "<? xml version=\"1.0\" ?>" << std::endl; |
---|
| 154 | out << "<" << xml::CXMLNode::GetRootName() << " >" << std::endl; |
---|
[509] | 155 | |
---|
[219] | 156 | for (; it != end; it++) |
---|
| 157 | { |
---|
[509] | 158 | CContext* context = *it; |
---|
| 159 | CContext::setCurrent(context->getId()); |
---|
[219] | 160 | out << *context << std::endl; |
---|
| 161 | } |
---|
[509] | 162 | |
---|
[219] | 163 | out << "</" << xml::CXMLNode::GetRootName() << " >" << std::endl; |
---|
[509] | 164 | CContext::setCurrent(currentContextId); |
---|
[219] | 165 | } |
---|
[346] | 166 | |
---|
[509] | 167 | |
---|
[219] | 168 | //---------------------------------------------------------------- |
---|
| 169 | |
---|
[509] | 170 | //! Convert context object into string (to print) |
---|
[219] | 171 | StdString CContext::toString(void) const |
---|
| 172 | { |
---|
| 173 | StdOStringStream oss; |
---|
| 174 | oss << "<" << CContext::GetName() |
---|
| 175 | << " id=\"" << this->getId() << "\" " |
---|
| 176 | << SuperClassAttribute::toString() << ">" << std::endl; |
---|
| 177 | if (!this->hasChild()) |
---|
| 178 | { |
---|
| 179 | //oss << "<!-- No definition -->" << std::endl; // fait planter l'incrémentation |
---|
| 180 | } |
---|
| 181 | else |
---|
| 182 | { |
---|
| 183 | |
---|
| 184 | #define DECLARE_NODE(Name_, name_) \ |
---|
[346] | 185 | if (C##Name_##Definition::has(C##Name_##Definition::GetDefName())) \ |
---|
| 186 | oss << * C##Name_##Definition::get(C##Name_##Definition::GetDefName()) << std::endl; |
---|
[219] | 187 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 188 | #include "node_type.conf" |
---|
| 189 | |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | oss << "</" << CContext::GetName() << " >"; |
---|
| 193 | |
---|
| 194 | return (oss.str()); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | //---------------------------------------------------------------- |
---|
| 198 | |
---|
[509] | 199 | /*! |
---|
| 200 | \brief Find all inheritace among objects in a context. |
---|
| 201 | \param [in] apply (true) write attributes of parent into ones of child if they are empty |
---|
| 202 | (false) write attributes of parent into a new container of child |
---|
| 203 | \param [in] parent unused |
---|
| 204 | */ |
---|
[445] | 205 | void CContext::solveDescInheritance(bool apply, const CAttributeMap * const UNUSED(parent)) |
---|
[219] | 206 | { |
---|
| 207 | #define DECLARE_NODE(Name_, name_) \ |
---|
[354] | 208 | if (C##Name_##Definition::has(C##Name_##Definition::GetDefName())) \ |
---|
[445] | 209 | C##Name_##Definition::get(C##Name_##Definition::GetDefName())->solveDescInheritance(apply); |
---|
[219] | 210 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 211 | #include "node_type.conf" |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | //---------------------------------------------------------------- |
---|
| 215 | |
---|
[509] | 216 | //! Verify if all root definition in the context have child. |
---|
[219] | 217 | bool CContext::hasChild(void) const |
---|
| 218 | { |
---|
| 219 | return ( |
---|
| 220 | #define DECLARE_NODE(Name_, name_) \ |
---|
[346] | 221 | C##Name_##Definition::has(C##Name_##Definition::GetDefName()) || |
---|
[219] | 222 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 223 | #include "node_type.conf" |
---|
| 224 | false); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | //---------------------------------------------------------------- |
---|
| 228 | |
---|
| 229 | void CContext::CleanTree(void) |
---|
| 230 | { |
---|
[549] | 231 | #define DECLARE_NODE(Name_, name_) C##Name_##Definition::ClearAllAttributes(); |
---|
[219] | 232 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 233 | #include "node_type.conf" |
---|
| 234 | } |
---|
| 235 | ///--------------------------------------------------------------- |
---|
[509] | 236 | |
---|
| 237 | //! Initialize client side |
---|
[597] | 238 | void CContext::initClient(MPI_Comm intraComm, MPI_Comm interComm, CContext* cxtServer /*= 0*/) |
---|
[300] | 239 | { |
---|
[549] | 240 | hasClient=true; |
---|
| 241 | client = new CContextClient(this,intraComm, interComm, cxtServer); |
---|
[697] | 242 | registryIn=new CRegistry(intraComm); |
---|
| 243 | registryIn->setPath(getId()) ; |
---|
| 244 | if (client->clientRank==0) registryIn->fromFile("xios_registry.bin") ; |
---|
| 245 | registryIn->bcastRegistry() ; |
---|
| 246 | |
---|
| 247 | registryOut=new CRegistry(intraComm) ; |
---|
| 248 | registryOut->setPath(getId()) ; |
---|
| 249 | |
---|
[597] | 250 | MPI_Comm intraCommServer, interCommServer; |
---|
| 251 | if (cxtServer) // Attached mode |
---|
| 252 | { |
---|
| 253 | intraCommServer = intraComm; |
---|
| 254 | interCommServer = interComm; |
---|
| 255 | } |
---|
| 256 | else |
---|
| 257 | { |
---|
| 258 | MPI_Comm_dup(intraComm, &intraCommServer); |
---|
[655] | 259 | comms.push_back(intraCommServer); |
---|
[597] | 260 | MPI_Comm_dup(interComm, &interCommServer); |
---|
[655] | 261 | comms.push_back(interCommServer); |
---|
[597] | 262 | } |
---|
| 263 | server = new CContextServer(this,intraCommServer,interCommServer); |
---|
[509] | 264 | } |
---|
[219] | 265 | |
---|
[509] | 266 | void CContext::setClientServerBuffer() |
---|
| 267 | { |
---|
[731] | 268 | size_t minBufferSize = CXios::minBufferSize; |
---|
[583] | 269 | #define DECLARE_NODE(Name_, name_) \ |
---|
[731] | 270 | if (minBufferSize < sizeof(C##Name_##Definition)) minBufferSize = sizeof(C##Name_##Definition); |
---|
[583] | 271 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 272 | #include "node_type.conf" |
---|
[719] | 273 | #undef DECLARE_NODE |
---|
| 274 | #undef DECLARE_NODE_PAR |
---|
| 275 | |
---|
[731] | 276 | std::map<int, StdSize> bufferSize = getAttributesBufferSize(); |
---|
| 277 | std::map<int, StdSize>::iterator it, ite = bufferSize.end(); |
---|
| 278 | for (it = bufferSize.begin(); it != ite; ++it) |
---|
| 279 | if (it->second < minBufferSize) it->second = minBufferSize; |
---|
[729] | 280 | |
---|
[731] | 281 | std::map<int, StdSize> dataBufferSize = getDataBufferSize(); |
---|
| 282 | ite = dataBufferSize.end(); |
---|
| 283 | for (it = dataBufferSize.begin(); it != ite; ++it) |
---|
| 284 | if (it->second > bufferSize[it->first]) bufferSize[it->first] = it->second; |
---|
| 285 | |
---|
[729] | 286 | if (client->isServerLeader()) |
---|
[598] | 287 | { |
---|
[729] | 288 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 289 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
[731] | 290 | if (!bufferSize.count(*itRank)) bufferSize[*itRank] = minBufferSize; |
---|
[598] | 291 | } |
---|
[584] | 292 | |
---|
[729] | 293 | if (!bufferSize.empty()) |
---|
| 294 | client->setBufferSize(bufferSize); |
---|
[509] | 295 | } |
---|
| 296 | |
---|
| 297 | //! Verify whether a context is initialized |
---|
[461] | 298 | bool CContext::isInitialized(void) |
---|
| 299 | { |
---|
[549] | 300 | return hasClient; |
---|
[461] | 301 | } |
---|
[509] | 302 | |
---|
| 303 | //! Initialize server |
---|
[597] | 304 | void CContext::initServer(MPI_Comm intraComm,MPI_Comm interComm, CContext* cxtClient /*= 0*/) |
---|
[300] | 305 | { |
---|
[549] | 306 | hasServer=true; |
---|
| 307 | server = new CContextServer(this,intraComm,interComm); |
---|
[697] | 308 | |
---|
| 309 | registryIn=new CRegistry(intraComm); |
---|
| 310 | registryIn->setPath(getId()) ; |
---|
| 311 | if (server->intraCommRank==0) registryIn->fromFile("xios_registry.bin") ; |
---|
| 312 | registryIn->bcastRegistry() ; |
---|
| 313 | registryOut=new CRegistry(intraComm) ; |
---|
| 314 | registryOut->setPath(getId()) ; |
---|
[775] | 315 | |
---|
[597] | 316 | MPI_Comm intraCommClient, interCommClient; |
---|
| 317 | if (cxtClient) // Attached mode |
---|
| 318 | { |
---|
| 319 | intraCommClient = intraComm; |
---|
| 320 | interCommClient = interComm; |
---|
| 321 | } |
---|
| 322 | else |
---|
| 323 | { |
---|
| 324 | MPI_Comm_dup(intraComm, &intraCommClient); |
---|
[655] | 325 | comms.push_back(intraCommClient); |
---|
[597] | 326 | MPI_Comm_dup(interComm, &interCommClient); |
---|
[655] | 327 | comms.push_back(interCommClient); |
---|
[597] | 328 | } |
---|
| 329 | client = new CContextClient(this,intraCommClient,interCommClient, cxtClient); |
---|
[509] | 330 | } |
---|
[300] | 331 | |
---|
[509] | 332 | //! Server side: Put server into a loop in order to listen message from client |
---|
[300] | 333 | bool CContext::eventLoop(void) |
---|
| 334 | { |
---|
[549] | 335 | return server->eventLoop(); |
---|
[509] | 336 | } |
---|
| 337 | |
---|
[597] | 338 | //! Try to send the buffers and receive possible answers |
---|
| 339 | bool CContext::checkBuffersAndListen(void) |
---|
| 340 | { |
---|
| 341 | client->checkBuffers(); |
---|
| 342 | return server->eventLoop(); |
---|
| 343 | } |
---|
| 344 | |
---|
[509] | 345 | //! Terminate a context |
---|
[300] | 346 | void CContext::finalize(void) |
---|
| 347 | { |
---|
[597] | 348 | if (!finalized) |
---|
[300] | 349 | { |
---|
[597] | 350 | finalized = true; |
---|
[697] | 351 | if (hasClient) sendRegistry() ; |
---|
[597] | 352 | client->finalize(); |
---|
| 353 | while (!server->hasFinished()) |
---|
| 354 | { |
---|
| 355 | server->eventLoop(); |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | if (hasServer) |
---|
| 359 | { |
---|
| 360 | closeAllFile(); |
---|
[697] | 361 | registryOut->hierarchicalGatherRegistry() ; |
---|
| 362 | if (server->intraCommRank==0) CXios::globalRegistry->mergeRegistry(*registryOut) ; |
---|
[597] | 363 | } |
---|
[775] | 364 | |
---|
[655] | 365 | for (std::list<MPI_Comm>::iterator it = comms.begin(); it != comms.end(); ++it) |
---|
| 366 | MPI_Comm_free(&(*it)); |
---|
| 367 | comms.clear(); |
---|
[300] | 368 | } |
---|
| 369 | } |
---|
[509] | 370 | |
---|
| 371 | /*! |
---|
| 372 | \brief Close all the context defintion and do processing data |
---|
| 373 | After everything is well defined on client side, they will be processed and sent to server |
---|
| 374 | From the version 2.0, sever and client work no more on the same database. Moreover, client(s) will send |
---|
| 375 | all necessary information to server, from which each server can build its own database. |
---|
| 376 | Because the role of server is to write out field data on a specific netcdf file, |
---|
| 377 | the only information that it needs is the enabled files |
---|
| 378 | and the active fields (fields will be written onto active files) |
---|
| 379 | */ |
---|
[300] | 380 | void CContext::closeDefinition(void) |
---|
| 381 | { |
---|
[584] | 382 | // There is nothing client need to send to server |
---|
[510] | 383 | if (hasClient) |
---|
[509] | 384 | { |
---|
| 385 | // After xml is parsed, there are some more works with post processing |
---|
| 386 | postProcessing(); |
---|
[510] | 387 | } |
---|
[598] | 388 | setClientServerBuffer(); |
---|
[300] | 389 | |
---|
[510] | 390 | if (hasClient && !hasServer) |
---|
| 391 | { |
---|
[509] | 392 | // Send all attributes of current context to server |
---|
| 393 | this->sendAllAttributesToServer(); |
---|
[459] | 394 | |
---|
[549] | 395 | // Send all attributes of current calendar |
---|
| 396 | CCalendarWrapper::get(CCalendarWrapper::GetDefName())->sendAllAttributesToServer(); |
---|
| 397 | |
---|
[509] | 398 | // We have enough information to send to server |
---|
| 399 | // First of all, send all enabled files |
---|
| 400 | sendEnabledFiles(); |
---|
| 401 | |
---|
| 402 | // Then, send all enabled fields |
---|
| 403 | sendEnabledFields(); |
---|
| 404 | |
---|
[540] | 405 | // At last, we have all info of domain and axis, then send them |
---|
[569] | 406 | sendRefDomainsAxis(); |
---|
[540] | 407 | |
---|
[509] | 408 | // After that, send all grid (if any) |
---|
| 409 | sendRefGrid(); |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | // We have a xml tree on the server side and now, it should be also processed |
---|
| 413 | if (hasClient && !hasServer) sendPostProcessing(); |
---|
| 414 | |
---|
| 415 | // There are some processings that should be done after all of above. For example: check mask or index |
---|
[510] | 416 | if (hasClient) |
---|
[509] | 417 | { |
---|
[640] | 418 | this->buildFilterGraphOfEnabledFields(); |
---|
| 419 | buildFilterGraphOfFieldsWithReadAccess(); |
---|
[509] | 420 | this->solveAllRefOfEnabledFields(true); |
---|
| 421 | } |
---|
| 422 | |
---|
[598] | 423 | // Now tell server that it can process all messages from client |
---|
| 424 | if (hasClient && !hasServer) this->sendCloseDefinition(); |
---|
[510] | 425 | |
---|
[584] | 426 | // Nettoyage de l'arborescence |
---|
| 427 | if (hasClient && !hasServer) CleanTree(); // Only on client side?? |
---|
[510] | 428 | |
---|
[598] | 429 | if (hasClient) |
---|
| 430 | { |
---|
| 431 | sendCreateFileHeader(); |
---|
| 432 | |
---|
| 433 | startPrefetchingOfEnabledReadModeFiles(); |
---|
| 434 | } |
---|
[300] | 435 | } |
---|
[509] | 436 | |
---|
[300] | 437 | void CContext::findAllEnabledFields(void) |
---|
| 438 | { |
---|
| 439 | for (unsigned int i = 0; i < this->enabledFiles.size(); i++) |
---|
| 440 | (void)this->enabledFiles[i]->getEnabledFields(); |
---|
| 441 | } |
---|
| 442 | |
---|
[775] | 443 | void CContext::findAllEnabledFieldsInReadModeFiles(void) |
---|
| 444 | { |
---|
| 445 | for (unsigned int i = 0; i < this->enabledReadModeFiles.size(); ++i) |
---|
| 446 | (void)this->enabledReadModeFiles[i]->getEnabledFields(); |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | void CContext::readAttributesOfEnabledFieldsInReadModeFiles() |
---|
| 450 | { |
---|
| 451 | for (unsigned int i = 0; i < this->enabledReadModeFiles.size(); ++i) |
---|
| 452 | (void)this->enabledReadModeFiles[i]->readAttributesOfEnabledFieldsInReadMode(); |
---|
| 453 | } |
---|
| 454 | |
---|
[823] | 455 | void CContext::solveOnlyRefOfEnabledFields(bool sendToServer) |
---|
| 456 | { |
---|
| 457 | int size = this->enabledFiles.size(); |
---|
| 458 | for (int i = 0; i < size; ++i) |
---|
| 459 | { |
---|
| 460 | this->enabledFiles[i]->solveOnlyRefOfEnabledFields(sendToServer); |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | for (int i = 0; i < size; ++i) |
---|
| 464 | { |
---|
| 465 | this->enabledFiles[i]->generateNewTransformationGridDest(); |
---|
| 466 | } |
---|
| 467 | } |
---|
| 468 | |
---|
[509] | 469 | void CContext::solveAllRefOfEnabledFields(bool sendToServer) |
---|
[300] | 470 | { |
---|
[509] | 471 | int size = this->enabledFiles.size(); |
---|
| 472 | for (int i = 0; i < size; ++i) |
---|
| 473 | { |
---|
| 474 | this->enabledFiles[i]->solveAllRefOfEnabledFields(sendToServer); |
---|
| 475 | } |
---|
[300] | 476 | } |
---|
| 477 | |
---|
[640] | 478 | void CContext::buildFilterGraphOfEnabledFields() |
---|
| 479 | { |
---|
| 480 | int size = this->enabledFiles.size(); |
---|
| 481 | for (int i = 0; i < size; ++i) |
---|
| 482 | { |
---|
| 483 | this->enabledFiles[i]->buildFilterGraphOfEnabledFields(garbageCollector); |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | |
---|
[598] | 487 | void CContext::startPrefetchingOfEnabledReadModeFiles() |
---|
| 488 | { |
---|
| 489 | int size = enabledReadModeFiles.size(); |
---|
| 490 | for (int i = 0; i < size; ++i) |
---|
| 491 | { |
---|
| 492 | enabledReadModeFiles[i]->prefetchEnabledReadModeFields(); |
---|
| 493 | } |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | void CContext::checkPrefetchingOfEnabledReadModeFiles() |
---|
| 497 | { |
---|
| 498 | int size = enabledReadModeFiles.size(); |
---|
| 499 | for (int i = 0; i < size; ++i) |
---|
| 500 | { |
---|
| 501 | enabledReadModeFiles[i]->prefetchEnabledReadModeFieldsIfNeeded(); |
---|
| 502 | } |
---|
| 503 | } |
---|
| 504 | |
---|
[593] | 505 | void CContext::findFieldsWithReadAccess(void) |
---|
| 506 | { |
---|
| 507 | fieldsWithReadAccess.clear(); |
---|
| 508 | const vector<CField*> allFields = CField::getAll(); |
---|
| 509 | for (size_t i = 0; i < allFields.size(); ++i) |
---|
| 510 | { |
---|
[740] | 511 | CField* field = allFields[i]; |
---|
| 512 | |
---|
| 513 | if (field->file && !field->file->mode.isEmpty() && field->file->mode == CFile::mode_attr::read) |
---|
| 514 | field->read_access = true; |
---|
| 515 | else if (!field->read_access.isEmpty() && field->read_access && (field->enabled.isEmpty() || field->enabled)) |
---|
| 516 | fieldsWithReadAccess.push_back(field); |
---|
[593] | 517 | } |
---|
| 518 | } |
---|
| 519 | |
---|
| 520 | void CContext::solveAllRefOfFieldsWithReadAccess() |
---|
| 521 | { |
---|
| 522 | for (size_t i = 0; i < fieldsWithReadAccess.size(); ++i) |
---|
| 523 | fieldsWithReadAccess[i]->solveAllReferenceEnabledField(false); |
---|
| 524 | } |
---|
| 525 | |
---|
[640] | 526 | void CContext::buildFilterGraphOfFieldsWithReadAccess() |
---|
| 527 | { |
---|
| 528 | for (size_t i = 0; i < fieldsWithReadAccess.size(); ++i) |
---|
| 529 | fieldsWithReadAccess[i]->buildFilterGraph(garbageCollector, true); |
---|
| 530 | } |
---|
| 531 | |
---|
[445] | 532 | void CContext::solveAllInheritance(bool apply) |
---|
[300] | 533 | { |
---|
| 534 | // Résolution des héritages descendants (cà d des héritages de groupes) |
---|
| 535 | // pour chacun des contextes. |
---|
[445] | 536 | solveDescInheritance(apply); |
---|
[300] | 537 | |
---|
| 538 | // Résolution des héritages par référence au niveau des fichiers. |
---|
[549] | 539 | const vector<CFile*> allFiles=CFile::getAll(); |
---|
[540] | 540 | const vector<CGrid*> allGrids= CGrid::getAll(); |
---|
[300] | 541 | |
---|
[510] | 542 | //if (hasClient && !hasServer) |
---|
| 543 | if (hasClient) |
---|
[540] | 544 | { |
---|
[510] | 545 | for (unsigned int i = 0; i < allFiles.size(); i++) |
---|
| 546 | allFiles[i]->solveFieldRefInheritance(apply); |
---|
[540] | 547 | } |
---|
| 548 | |
---|
| 549 | unsigned int vecSize = allGrids.size(); |
---|
| 550 | unsigned int i = 0; |
---|
| 551 | for (i = 0; i < vecSize; ++i) |
---|
| 552 | allGrids[i]->solveDomainAxisRefInheritance(apply); |
---|
| 553 | |
---|
[300] | 554 | } |
---|
| 555 | |
---|
| 556 | void CContext::findEnabledFiles(void) |
---|
| 557 | { |
---|
[347] | 558 | const std::vector<CFile*> allFiles = CFile::getAll(); |
---|
[300] | 559 | |
---|
| 560 | for (unsigned int i = 0; i < allFiles.size(); i++) |
---|
| 561 | if (!allFiles[i]->enabled.isEmpty()) // Si l'attribut 'enabled' est défini. |
---|
[430] | 562 | { |
---|
[300] | 563 | if (allFiles[i]->enabled.getValue()) // Si l'attribut 'enabled' est fixé à vrai. |
---|
| 564 | enabledFiles.push_back(allFiles[i]); |
---|
[430] | 565 | } |
---|
| 566 | else enabledFiles.push_back(allFiles[i]); // otherwise true by default |
---|
[300] | 567 | |
---|
[509] | 568 | |
---|
[300] | 569 | if (enabledFiles.size() == 0) |
---|
| 570 | DEBUG(<<"Aucun fichier ne va être sorti dans le contexte nommé \"" |
---|
| 571 | << getId() << "\" !"); |
---|
| 572 | } |
---|
| 573 | |
---|
[598] | 574 | void CContext::findEnabledReadModeFiles(void) |
---|
| 575 | { |
---|
| 576 | int size = this->enabledFiles.size(); |
---|
| 577 | for (int i = 0; i < size; ++i) |
---|
| 578 | { |
---|
| 579 | if (!enabledFiles[i]->mode.isEmpty() && enabledFiles[i]->mode.getValue() == CFile::mode_attr::read) |
---|
| 580 | enabledReadModeFiles.push_back(enabledFiles[i]); |
---|
| 581 | } |
---|
| 582 | } |
---|
| 583 | |
---|
[300] | 584 | void CContext::closeAllFile(void) |
---|
| 585 | { |
---|
[347] | 586 | std::vector<CFile*>::const_iterator |
---|
[300] | 587 | it = this->enabledFiles.begin(), end = this->enabledFiles.end(); |
---|
[509] | 588 | |
---|
[300] | 589 | for (; it != end; it++) |
---|
| 590 | { |
---|
| 591 | info(30)<<"Closing File : "<<(*it)->getId()<<endl; |
---|
| 592 | (*it)->close(); |
---|
| 593 | } |
---|
| 594 | } |
---|
[509] | 595 | |
---|
| 596 | /*! |
---|
| 597 | \brief Dispatch event received from client |
---|
| 598 | Whenever a message is received in buffer of server, it will be processed depending on |
---|
| 599 | its event type. A new event type should be added in the switch list to make sure |
---|
| 600 | it processed on server side. |
---|
| 601 | \param [in] event: Received message |
---|
| 602 | */ |
---|
[300] | 603 | bool CContext::dispatchEvent(CEventServer& event) |
---|
| 604 | { |
---|
[509] | 605 | |
---|
[549] | 606 | if (SuperClass::dispatchEvent(event)) return true; |
---|
[300] | 607 | else |
---|
| 608 | { |
---|
| 609 | switch(event.type) |
---|
| 610 | { |
---|
| 611 | case EVENT_ID_CLOSE_DEFINITION : |
---|
[549] | 612 | recvCloseDefinition(event); |
---|
| 613 | return true; |
---|
| 614 | break; |
---|
[584] | 615 | case EVENT_ID_UPDATE_CALENDAR: |
---|
[549] | 616 | recvUpdateCalendar(event); |
---|
| 617 | return true; |
---|
| 618 | break; |
---|
[300] | 619 | case EVENT_ID_CREATE_FILE_HEADER : |
---|
[549] | 620 | recvCreateFileHeader(event); |
---|
| 621 | return true; |
---|
| 622 | break; |
---|
[509] | 623 | case EVENT_ID_POST_PROCESS: |
---|
[549] | 624 | recvPostProcessing(event); |
---|
| 625 | return true; |
---|
[697] | 626 | case EVENT_ID_SEND_REGISTRY: |
---|
| 627 | recvRegistry(event); |
---|
| 628 | return true; |
---|
| 629 | break; |
---|
[584] | 630 | |
---|
[300] | 631 | default : |
---|
| 632 | ERROR("bool CContext::dispatchEvent(CEventServer& event)", |
---|
[549] | 633 | <<"Unknown Event"); |
---|
| 634 | return false; |
---|
[300] | 635 | } |
---|
| 636 | } |
---|
| 637 | } |
---|
[509] | 638 | |
---|
| 639 | //! Client side: Send a message to server to make it close |
---|
[300] | 640 | void CContext::sendCloseDefinition(void) |
---|
| 641 | { |
---|
[549] | 642 | CEventClient event(getType(),EVENT_ID_CLOSE_DEFINITION); |
---|
[300] | 643 | if (client->isServerLeader()) |
---|
| 644 | { |
---|
[549] | 645 | CMessage msg; |
---|
| 646 | msg<<this->getIdServer(); |
---|
[595] | 647 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 648 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 649 | event.push(*itRank,1,msg); |
---|
[549] | 650 | client->sendEvent(event); |
---|
[300] | 651 | } |
---|
[549] | 652 | else client->sendEvent(event); |
---|
[300] | 653 | } |
---|
[509] | 654 | |
---|
| 655 | //! Server side: Receive a message of client announcing a context close |
---|
[300] | 656 | void CContext::recvCloseDefinition(CEventServer& event) |
---|
| 657 | { |
---|
[509] | 658 | |
---|
[300] | 659 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 660 | string id; |
---|
[549] | 661 | *buffer>>id; |
---|
[509] | 662 | get(id)->closeDefinition(); |
---|
[300] | 663 | } |
---|
[509] | 664 | |
---|
| 665 | //! Client side: Send a message to update calendar in each time step |
---|
[300] | 666 | void CContext::sendUpdateCalendar(int step) |
---|
| 667 | { |
---|
| 668 | if (!hasServer) |
---|
| 669 | { |
---|
[549] | 670 | CEventClient event(getType(),EVENT_ID_UPDATE_CALENDAR); |
---|
[300] | 671 | if (client->isServerLeader()) |
---|
| 672 | { |
---|
[549] | 673 | CMessage msg; |
---|
| 674 | msg<<this->getIdServer()<<step; |
---|
[595] | 675 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 676 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 677 | event.push(*itRank,1,msg); |
---|
[549] | 678 | client->sendEvent(event); |
---|
[300] | 679 | } |
---|
[549] | 680 | else client->sendEvent(event); |
---|
[300] | 681 | } |
---|
| 682 | } |
---|
[509] | 683 | |
---|
| 684 | //! Server side: Receive a message of client annoucing calendar update |
---|
[300] | 685 | void CContext::recvUpdateCalendar(CEventServer& event) |
---|
| 686 | { |
---|
| 687 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 688 | string id; |
---|
[549] | 689 | *buffer>>id; |
---|
| 690 | get(id)->recvUpdateCalendar(*buffer); |
---|
[300] | 691 | } |
---|
[509] | 692 | |
---|
| 693 | //! Server side: Receive a message of client annoucing calendar update |
---|
[300] | 694 | void CContext::recvUpdateCalendar(CBufferIn& buffer) |
---|
| 695 | { |
---|
[549] | 696 | int step; |
---|
| 697 | buffer>>step; |
---|
| 698 | updateCalendar(step); |
---|
[300] | 699 | } |
---|
[509] | 700 | |
---|
| 701 | //! Client side: Send a message to create header part of netcdf file |
---|
[300] | 702 | void CContext::sendCreateFileHeader(void) |
---|
| 703 | { |
---|
[549] | 704 | CEventClient event(getType(),EVENT_ID_CREATE_FILE_HEADER); |
---|
[300] | 705 | if (client->isServerLeader()) |
---|
| 706 | { |
---|
[549] | 707 | CMessage msg; |
---|
| 708 | msg<<this->getIdServer(); |
---|
[595] | 709 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 710 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 711 | event.push(*itRank,1,msg) ; |
---|
[549] | 712 | client->sendEvent(event); |
---|
[300] | 713 | } |
---|
[549] | 714 | else client->sendEvent(event); |
---|
[300] | 715 | } |
---|
[509] | 716 | |
---|
| 717 | //! Server side: Receive a message of client annoucing the creation of header part of netcdf file |
---|
[300] | 718 | void CContext::recvCreateFileHeader(CEventServer& event) |
---|
| 719 | { |
---|
| 720 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 721 | string id; |
---|
[549] | 722 | *buffer>>id; |
---|
| 723 | get(id)->recvCreateFileHeader(*buffer); |
---|
[300] | 724 | } |
---|
[509] | 725 | |
---|
| 726 | //! Server side: Receive a message of client annoucing the creation of header part of netcdf file |
---|
[300] | 727 | void CContext::recvCreateFileHeader(CBufferIn& buffer) |
---|
| 728 | { |
---|
[549] | 729 | createFileHeader(); |
---|
[300] | 730 | } |
---|
[509] | 731 | |
---|
| 732 | //! Client side: Send a message to do some post processing on server |
---|
| 733 | void CContext::sendPostProcessing() |
---|
| 734 | { |
---|
| 735 | if (!hasServer) |
---|
| 736 | { |
---|
[549] | 737 | CEventClient event(getType(),EVENT_ID_POST_PROCESS); |
---|
[509] | 738 | if (client->isServerLeader()) |
---|
| 739 | { |
---|
[549] | 740 | CMessage msg; |
---|
[511] | 741 | msg<<this->getIdServer(); |
---|
[595] | 742 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 743 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 744 | event.push(*itRank,1,msg); |
---|
[549] | 745 | client->sendEvent(event); |
---|
[509] | 746 | } |
---|
[549] | 747 | else client->sendEvent(event); |
---|
[509] | 748 | } |
---|
| 749 | } |
---|
| 750 | |
---|
| 751 | //! Server side: Receive a message to do some post processing |
---|
| 752 | void CContext::recvPostProcessing(CEventServer& event) |
---|
| 753 | { |
---|
| 754 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 755 | string id; |
---|
| 756 | *buffer>>id; |
---|
| 757 | get(id)->recvPostProcessing(*buffer); |
---|
| 758 | } |
---|
| 759 | |
---|
| 760 | //! Server side: Receive a message to do some post processing |
---|
| 761 | void CContext::recvPostProcessing(CBufferIn& buffer) |
---|
| 762 | { |
---|
[549] | 763 | CCalendarWrapper::get(CCalendarWrapper::GetDefName())->createCalendar(); |
---|
[509] | 764 | postProcessing(); |
---|
| 765 | } |
---|
| 766 | |
---|
[511] | 767 | const StdString& CContext::getIdServer() |
---|
| 768 | { |
---|
| 769 | if (hasClient) |
---|
| 770 | { |
---|
| 771 | idServer_ = this->getId(); |
---|
| 772 | idServer_ += "_server"; |
---|
| 773 | return idServer_; |
---|
| 774 | } |
---|
| 775 | if (hasServer) return (this->getId()); |
---|
| 776 | } |
---|
| 777 | |
---|
[509] | 778 | /*! |
---|
| 779 | \brief Do some simple post processings after parsing xml file |
---|
| 780 | After the xml file (iodef.xml) is parsed, it is necessary to build all relations among |
---|
| 781 | created object, e.g: inhertance among fields, domain, axis. After that, all fiels as well as their parents (reference fields), |
---|
| 782 | which will be written out into netcdf files, are processed |
---|
| 783 | */ |
---|
| 784 | void CContext::postProcessing() |
---|
| 785 | { |
---|
| 786 | if (isPostProcessed) return; |
---|
| 787 | |
---|
[549] | 788 | // Make sure the calendar was correctly created |
---|
| 789 | if (!calendar) |
---|
| 790 | ERROR("CContext::postProcessing()", << "A calendar must be defined for the context \"" << getId() << "!\"") |
---|
| 791 | else if (calendar->getTimeStep() == NoneDu) |
---|
| 792 | ERROR("CContext::postProcessing()", << "A timestep must be defined for the context \"" << getId() << "!\"") |
---|
[550] | 793 | // Calendar first update to set the current date equals to the start date |
---|
| 794 | calendar->update(0); |
---|
[509] | 795 | |
---|
| 796 | // Find all inheritance in xml structure |
---|
| 797 | this->solveAllInheritance(); |
---|
| 798 | |
---|
[676] | 799 | // Check if some axis, domains or grids are eligible to for compressed indexed output. |
---|
| 800 | // Warning: This must be done after solving the inheritance and before the rest of post-processing |
---|
| 801 | checkAxisDomainsGridsEligibilityForCompressedOutput(); |
---|
| 802 | |
---|
[711] | 803 | // Check if some automatic time series should be generated |
---|
| 804 | // Warning: This must be done after solving the inheritance and before the rest of post-processing |
---|
| 805 | prepareTimeseries(); |
---|
| 806 | |
---|
[509] | 807 | //Initialisation du vecteur 'enabledFiles' contenant la liste des fichiers à sortir. |
---|
| 808 | this->findEnabledFiles(); |
---|
[598] | 809 | this->findEnabledReadModeFiles(); |
---|
[509] | 810 | |
---|
| 811 | // Find all enabled fields of each file |
---|
| 812 | this->findAllEnabledFields(); |
---|
[775] | 813 | this->findAllEnabledFieldsInReadModeFiles(); |
---|
[509] | 814 | |
---|
[775] | 815 | if (hasClient && !hasServer) |
---|
| 816 | { |
---|
| 817 | // Try to read attributes of fields in file then fill in corresponding grid (or domain, axis) |
---|
| 818 | this->readAttributesOfEnabledFieldsInReadModeFiles(); |
---|
| 819 | } |
---|
[823] | 820 | |
---|
| 821 | // Only search and rebuild all reference objects of enable fields, don't transform |
---|
| 822 | this->solveOnlyRefOfEnabledFields(false); |
---|
| 823 | |
---|
[509] | 824 | // Search and rebuild all reference object of enabled fields |
---|
| 825 | this->solveAllRefOfEnabledFields(false); |
---|
[593] | 826 | |
---|
| 827 | // Find all fields with read access from the public API |
---|
| 828 | findFieldsWithReadAccess(); |
---|
| 829 | // and solve the all reference for them |
---|
| 830 | solveAllRefOfFieldsWithReadAccess(); |
---|
| 831 | |
---|
[509] | 832 | isPostProcessed = true; |
---|
| 833 | } |
---|
| 834 | |
---|
[733] | 835 | std::map<int, StdSize> CContext::getAttributesBufferSize() |
---|
[509] | 836 | { |
---|
[731] | 837 | std::map<int, StdSize> attributesSize; |
---|
| 838 | |
---|
| 839 | size_t numEnabledFiles = this->enabledFiles.size(); |
---|
| 840 | for (size_t i = 0; i < numEnabledFiles; ++i) |
---|
| 841 | { |
---|
| 842 | CFile* file = this->enabledFiles[i]; |
---|
| 843 | |
---|
| 844 | std::vector<CField*> enabledFields = file->getEnabledFields(); |
---|
| 845 | size_t numEnabledFields = enabledFields.size(); |
---|
| 846 | for (size_t j = 0; j < numEnabledFields; ++j) |
---|
| 847 | { |
---|
| 848 | const std::map<int, StdSize> mapSize = enabledFields[j]->getGridAttributesBufferSize(); |
---|
| 849 | std::map<int, StdSize>::const_iterator it = mapSize.begin(), itE = mapSize.end(); |
---|
| 850 | for (; it != itE; ++it) |
---|
| 851 | { |
---|
| 852 | // If attributesSize[it->first] does not exist, it will be zero-initialized |
---|
| 853 | // so we can use it safely without checking for its existance |
---|
| 854 | if (attributesSize[it->first] < it->second) |
---|
| 855 | attributesSize[it->first] = it->second; |
---|
| 856 | } |
---|
| 857 | } |
---|
| 858 | } |
---|
| 859 | |
---|
| 860 | return attributesSize; |
---|
| 861 | } |
---|
| 862 | |
---|
[733] | 863 | std::map<int, StdSize> CContext::getDataBufferSize() |
---|
[731] | 864 | { |
---|
[598] | 865 | CFile::mode_attr::t_enum mode = hasClient ? CFile::mode_attr::write : CFile::mode_attr::read; |
---|
| 866 | |
---|
[730] | 867 | std::map<int, StdSize> dataSize; |
---|
[509] | 868 | |
---|
| 869 | // Find all reference domain and axis of all active fields |
---|
[730] | 870 | size_t numEnabledFiles = this->enabledFiles.size(); |
---|
| 871 | for (size_t i = 0; i < numEnabledFiles; ++i) |
---|
[509] | 872 | { |
---|
[598] | 873 | CFile* file = this->enabledFiles[i]; |
---|
| 874 | CFile::mode_attr::t_enum fileMode = file->mode.isEmpty() ? CFile::mode_attr::write : file->mode.getValue(); |
---|
| 875 | |
---|
| 876 | if (fileMode == mode) |
---|
[509] | 877 | { |
---|
[598] | 878 | std::vector<CField*> enabledFields = file->getEnabledFields(); |
---|
[730] | 879 | size_t numEnabledFields = enabledFields.size(); |
---|
| 880 | for (size_t j = 0; j < numEnabledFields; ++j) |
---|
[509] | 881 | { |
---|
[731] | 882 | const std::map<int, StdSize> mapSize = enabledFields[j]->getGridDataBufferSize(); |
---|
[730] | 883 | std::map<int, StdSize>::const_iterator it = mapSize.begin(), itE = mapSize.end(); |
---|
| 884 | for (; it != itE; ++it) |
---|
[509] | 885 | { |
---|
[730] | 886 | // If dataSize[it->first] does not exist, it will be zero-initialized |
---|
| 887 | // so we can use it safely without checking for its existance |
---|
| 888 | if (CXios::isOptPerformance) |
---|
| 889 | dataSize[it->first] += it->second; |
---|
| 890 | else if (dataSize[it->first] < it->second) |
---|
| 891 | dataSize[it->first] = it->second; |
---|
[598] | 892 | } |
---|
[509] | 893 | } |
---|
| 894 | } |
---|
| 895 | } |
---|
| 896 | |
---|
[730] | 897 | return dataSize; |
---|
[509] | 898 | } |
---|
| 899 | |
---|
| 900 | //! Client side: Send infomation of active files (files are enabled to write out) |
---|
| 901 | void CContext::sendEnabledFiles() |
---|
| 902 | { |
---|
| 903 | int size = this->enabledFiles.size(); |
---|
| 904 | |
---|
| 905 | // In a context, each type has a root definition, e.g: axis, domain, field. |
---|
| 906 | // Every object must be a child of one of these root definition. In this case |
---|
| 907 | // all new file objects created on server must be children of the root "file_definition" |
---|
| 908 | StdString fileDefRoot("file_definition"); |
---|
| 909 | CFileGroup* cfgrpPtr = CFileGroup::get(fileDefRoot); |
---|
| 910 | |
---|
| 911 | for (int i = 0; i < size; ++i) |
---|
| 912 | { |
---|
| 913 | cfgrpPtr->sendCreateChild(this->enabledFiles[i]->getId()); |
---|
| 914 | this->enabledFiles[i]->sendAllAttributesToServer(); |
---|
| 915 | this->enabledFiles[i]->sendAddAllVariables(); |
---|
| 916 | } |
---|
| 917 | } |
---|
| 918 | |
---|
| 919 | //! Client side: Send information of active fields (ones are written onto files) |
---|
| 920 | void CContext::sendEnabledFields() |
---|
| 921 | { |
---|
| 922 | int size = this->enabledFiles.size(); |
---|
| 923 | for (int i = 0; i < size; ++i) |
---|
| 924 | { |
---|
| 925 | this->enabledFiles[i]->sendEnabledFields(); |
---|
| 926 | } |
---|
| 927 | } |
---|
| 928 | |
---|
[676] | 929 | //! Client side: Check if the defined axis, domains and grids are eligible for compressed indexed output |
---|
| 930 | void CContext::checkAxisDomainsGridsEligibilityForCompressedOutput() |
---|
| 931 | { |
---|
| 932 | if (!hasClient) return; |
---|
| 933 | |
---|
| 934 | const vector<CAxis*> allAxis = CAxis::getAll(); |
---|
| 935 | for (vector<CAxis*>::const_iterator it = allAxis.begin(); it != allAxis.end(); it++) |
---|
| 936 | (*it)->checkEligibilityForCompressedOutput(); |
---|
| 937 | |
---|
| 938 | const vector<CDomain*> allDomains = CDomain::getAll(); |
---|
| 939 | for (vector<CDomain*>::const_iterator it = allDomains.begin(); it != allDomains.end(); it++) |
---|
| 940 | (*it)->checkEligibilityForCompressedOutput(); |
---|
| 941 | |
---|
| 942 | const vector<CGrid*> allGrids = CGrid::getAll(); |
---|
| 943 | for (vector<CGrid*>::const_iterator it = allGrids.begin(); it != allGrids.end(); it++) |
---|
| 944 | (*it)->checkEligibilityForCompressedOutput(); |
---|
| 945 | } |
---|
| 946 | |
---|
[711] | 947 | //! Client side: Prepare the timeseries by adding the necessary files |
---|
| 948 | void CContext::prepareTimeseries() |
---|
| 949 | { |
---|
| 950 | if (!hasClient) return; |
---|
| 951 | |
---|
| 952 | const std::vector<CFile*> allFiles = CFile::getAll(); |
---|
| 953 | for (size_t i = 0; i < allFiles.size(); i++) |
---|
| 954 | { |
---|
| 955 | CFile* file = allFiles[i]; |
---|
| 956 | |
---|
| 957 | if (!file->timeseries.isEmpty() && file->timeseries != CFile::timeseries_attr::none) |
---|
| 958 | { |
---|
[773] | 959 | StdString tsPrefix = !file->ts_prefix.isEmpty() ? file->ts_prefix : file->getFileOutputName(); |
---|
[711] | 960 | |
---|
| 961 | const std::vector<CField*> allFields = file->getAllFields(); |
---|
| 962 | for (size_t j = 0; j < allFields.size(); j++) |
---|
| 963 | { |
---|
| 964 | CField* field = allFields[j]; |
---|
| 965 | |
---|
| 966 | if (!field->ts_enabled.isEmpty() && field->ts_enabled) |
---|
| 967 | { |
---|
| 968 | CFile* tsFile = CFile::create(); |
---|
| 969 | tsFile->duplicateAttributes(file); |
---|
| 970 | |
---|
| 971 | tsFile->name = tsPrefix + "_"; |
---|
| 972 | if (!field->name.isEmpty()) |
---|
| 973 | tsFile->name.get() += field->name; |
---|
| 974 | else if (field->hasDirectFieldReference()) // We cannot use getBaseFieldReference() just yet |
---|
| 975 | tsFile->name.get() += field->field_ref; |
---|
| 976 | else |
---|
| 977 | tsFile->name.get() += field->getId(); |
---|
| 978 | |
---|
| 979 | if (!field->ts_split_freq.isEmpty()) |
---|
| 980 | tsFile->split_freq = field->ts_split_freq; |
---|
| 981 | |
---|
| 982 | CField* tsField = tsFile->addField(); |
---|
| 983 | tsField->field_ref = field->getId(); |
---|
| 984 | |
---|
| 985 | tsFile->solveFieldRefInheritance(true); |
---|
| 986 | |
---|
| 987 | if (file->timeseries == CFile::timeseries_attr::exclusive) |
---|
| 988 | field->enabled = false; |
---|
| 989 | } |
---|
| 990 | } |
---|
| 991 | |
---|
| 992 | // Finally disable the original file is need be |
---|
| 993 | if (file->timeseries == CFile::timeseries_attr::only) |
---|
| 994 | file->enabled = false; |
---|
| 995 | } |
---|
| 996 | } |
---|
| 997 | } |
---|
| 998 | |
---|
[509] | 999 | //! Client side: Send information of reference grid of active fields |
---|
| 1000 | void CContext::sendRefGrid() |
---|
| 1001 | { |
---|
| 1002 | std::set<StdString> gridIds; |
---|
| 1003 | int sizeFile = this->enabledFiles.size(); |
---|
| 1004 | CFile* filePtr(NULL); |
---|
| 1005 | |
---|
| 1006 | // Firstly, find all reference grids of all active fields |
---|
| 1007 | for (int i = 0; i < sizeFile; ++i) |
---|
| 1008 | { |
---|
| 1009 | filePtr = this->enabledFiles[i]; |
---|
| 1010 | std::vector<CField*> enabledFields = filePtr->getEnabledFields(); |
---|
| 1011 | int sizeField = enabledFields.size(); |
---|
| 1012 | for (int numField = 0; numField < sizeField; ++numField) |
---|
| 1013 | { |
---|
| 1014 | if (0 != enabledFields[numField]->getRelGrid()) |
---|
| 1015 | gridIds.insert(CGrid::get(enabledFields[numField]->getRelGrid())->getId()); |
---|
| 1016 | } |
---|
| 1017 | } |
---|
| 1018 | |
---|
| 1019 | // Create all reference grids on server side |
---|
| 1020 | StdString gridDefRoot("grid_definition"); |
---|
| 1021 | CGridGroup* gridPtr = CGridGroup::get(gridDefRoot); |
---|
| 1022 | std::set<StdString>::const_iterator it, itE = gridIds.end(); |
---|
| 1023 | for (it = gridIds.begin(); it != itE; ++it) |
---|
| 1024 | { |
---|
| 1025 | gridPtr->sendCreateChild(*it); |
---|
| 1026 | CGrid::get(*it)->sendAllAttributesToServer(); |
---|
[540] | 1027 | CGrid::get(*it)->sendAllDomains(); |
---|
| 1028 | CGrid::get(*it)->sendAllAxis(); |
---|
[509] | 1029 | } |
---|
| 1030 | } |
---|
| 1031 | |
---|
| 1032 | |
---|
| 1033 | //! Client side: Send information of reference domain and axis of active fields |
---|
[569] | 1034 | void CContext::sendRefDomainsAxis() |
---|
| 1035 | { |
---|
| 1036 | std::set<StdString> domainIds; |
---|
| 1037 | std::set<StdString> axisIds; |
---|
[509] | 1038 | |
---|
[569] | 1039 | // Find all reference domain and axis of all active fields |
---|
| 1040 | int numEnabledFiles = this->enabledFiles.size(); |
---|
| 1041 | for (int i = 0; i < numEnabledFiles; ++i) |
---|
| 1042 | { |
---|
| 1043 | std::vector<CField*> enabledFields = this->enabledFiles[i]->getEnabledFields(); |
---|
| 1044 | int numEnabledFields = enabledFields.size(); |
---|
| 1045 | for (int j = 0; j < numEnabledFields; ++j) |
---|
| 1046 | { |
---|
| 1047 | const std::pair<StdString, StdString>& prDomAxisId = enabledFields[j]->getRefDomainAxisIds(); |
---|
[619] | 1048 | if ("" != prDomAxisId.first) domainIds.insert(prDomAxisId.first); |
---|
| 1049 | if ("" != prDomAxisId.second) axisIds.insert(prDomAxisId.second); |
---|
[569] | 1050 | } |
---|
| 1051 | } |
---|
| 1052 | |
---|
| 1053 | // Create all reference axis on server side |
---|
| 1054 | std::set<StdString>::iterator itDom, itAxis; |
---|
| 1055 | std::set<StdString>::const_iterator itE; |
---|
| 1056 | |
---|
| 1057 | StdString axiDefRoot("axis_definition"); |
---|
| 1058 | CAxisGroup* axisPtr = CAxisGroup::get(axiDefRoot); |
---|
| 1059 | itE = axisIds.end(); |
---|
| 1060 | for (itAxis = axisIds.begin(); itAxis != itE; ++itAxis) |
---|
| 1061 | { |
---|
| 1062 | if (!itAxis->empty()) |
---|
| 1063 | { |
---|
| 1064 | axisPtr->sendCreateChild(*itAxis); |
---|
| 1065 | CAxis::get(*itAxis)->sendAllAttributesToServer(); |
---|
| 1066 | } |
---|
| 1067 | } |
---|
| 1068 | |
---|
| 1069 | // Create all reference domains on server side |
---|
| 1070 | StdString domDefRoot("domain_definition"); |
---|
| 1071 | CDomainGroup* domPtr = CDomainGroup::get(domDefRoot); |
---|
| 1072 | itE = domainIds.end(); |
---|
| 1073 | for (itDom = domainIds.begin(); itDom != itE; ++itDom) |
---|
| 1074 | { |
---|
| 1075 | if (!itDom->empty()) { |
---|
| 1076 | domPtr->sendCreateChild(*itDom); |
---|
| 1077 | CDomain::get(*itDom)->sendAllAttributesToServer(); |
---|
| 1078 | } |
---|
| 1079 | } |
---|
| 1080 | } |
---|
| 1081 | |
---|
[509] | 1082 | //! Update calendar in each time step |
---|
[300] | 1083 | void CContext::updateCalendar(int step) |
---|
| 1084 | { |
---|
[639] | 1085 | info(50) << "updateCalendar : before : " << calendar->getCurrentDate() << endl; |
---|
[549] | 1086 | calendar->update(step); |
---|
[639] | 1087 | info(50) << "updateCalendar : after : " << calendar->getCurrentDate() << endl; |
---|
| 1088 | |
---|
| 1089 | if (hasClient) |
---|
| 1090 | { |
---|
[806] | 1091 | checkPrefetchingOfEnabledReadModeFiles(); |
---|
[639] | 1092 | garbageCollector.invalidate(calendar->getCurrentDate()); |
---|
| 1093 | } |
---|
[300] | 1094 | } |
---|
[509] | 1095 | |
---|
| 1096 | //! Server side: Create header of netcdf file |
---|
[300] | 1097 | void CContext::createFileHeader(void ) |
---|
| 1098 | { |
---|
[549] | 1099 | vector<CFile*>::const_iterator it; |
---|
[509] | 1100 | |
---|
[300] | 1101 | for (it=enabledFiles.begin(); it != enabledFiles.end(); it++) |
---|
| 1102 | { |
---|
[321] | 1103 | (*it)->initFile(); |
---|
[300] | 1104 | } |
---|
[509] | 1105 | } |
---|
| 1106 | |
---|
| 1107 | //! Get current context |
---|
[347] | 1108 | CContext* CContext::getCurrent(void) |
---|
[300] | 1109 | { |
---|
[549] | 1110 | return CObjectFactory::GetObject<CContext>(CObjectFactory::GetCurrentContextId()).get(); |
---|
[300] | 1111 | } |
---|
[509] | 1112 | |
---|
| 1113 | /*! |
---|
| 1114 | \brief Set context with an id be the current context |
---|
| 1115 | \param [in] id identity of context to be set to current |
---|
| 1116 | */ |
---|
[346] | 1117 | void CContext::setCurrent(const string& id) |
---|
| 1118 | { |
---|
| 1119 | CObjectFactory::SetCurrentContextId(id); |
---|
| 1120 | CGroupFactory::SetCurrentContextId(id); |
---|
| 1121 | } |
---|
[509] | 1122 | |
---|
| 1123 | /*! |
---|
| 1124 | \brief Create a context with specific id |
---|
| 1125 | \param [in] id identity of new context |
---|
| 1126 | \return pointer to the new context or already-existed one with identity id |
---|
| 1127 | */ |
---|
[347] | 1128 | CContext* CContext::create(const StdString& id) |
---|
[346] | 1129 | { |
---|
[549] | 1130 | CContext::setCurrent(id); |
---|
[509] | 1131 | |
---|
[346] | 1132 | bool hasctxt = CContext::has(id); |
---|
[347] | 1133 | CContext* context = CObjectFactory::CreateObject<CContext>(id).get(); |
---|
[549] | 1134 | getRoot(); |
---|
[347] | 1135 | if (!hasctxt) CGroupFactory::AddChild(root, context->getShared()); |
---|
[346] | 1136 | |
---|
| 1137 | #define DECLARE_NODE(Name_, name_) \ |
---|
| 1138 | C##Name_##Definition::create(C##Name_##Definition::GetDefName()); |
---|
| 1139 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 1140 | #include "node_type.conf" |
---|
| 1141 | |
---|
| 1142 | return (context); |
---|
| 1143 | } |
---|
[697] | 1144 | |
---|
| 1145 | |
---|
| 1146 | |
---|
| 1147 | //! Server side: Receive a message to do some post processing |
---|
| 1148 | void CContext::recvRegistry(CEventServer& event) |
---|
| 1149 | { |
---|
| 1150 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 1151 | string id; |
---|
| 1152 | *buffer>>id; |
---|
| 1153 | get(id)->recvRegistry(*buffer); |
---|
| 1154 | } |
---|
| 1155 | |
---|
| 1156 | void CContext::recvRegistry(CBufferIn& buffer) |
---|
| 1157 | { |
---|
| 1158 | if (server->intraCommRank==0) |
---|
| 1159 | { |
---|
| 1160 | CRegistry registry(server->intraComm) ; |
---|
| 1161 | registry.fromBuffer(buffer) ; |
---|
| 1162 | registryOut->mergeRegistry(registry) ; |
---|
| 1163 | } |
---|
| 1164 | } |
---|
| 1165 | |
---|
| 1166 | void CContext::sendRegistry(void) |
---|
| 1167 | { |
---|
| 1168 | registryOut->hierarchicalGatherRegistry() ; |
---|
| 1169 | |
---|
| 1170 | CEventClient event(CContext::GetType(), CContext::EVENT_ID_SEND_REGISTRY); |
---|
| 1171 | if (client->isServerLeader()) |
---|
| 1172 | { |
---|
| 1173 | CMessage msg ; |
---|
| 1174 | msg<<this->getIdServer(); |
---|
| 1175 | if (client->clientRank==0) msg<<*registryOut ; |
---|
| 1176 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 1177 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 1178 | event.push(*itRank,1,msg); |
---|
| 1179 | client->sendEvent(event); |
---|
| 1180 | } |
---|
| 1181 | else client->sendEvent(event); |
---|
| 1182 | } |
---|
| 1183 | |
---|
[335] | 1184 | } // namespace xios |
---|