[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 | |
---|
[1056] | 22 | /// ////////////////////// Dfinitions ////////////////////// /// |
---|
[219] | 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 | { |
---|
[1056] | 179 | //oss << "<!-- No definition -->" << std::endl; // fait planter l'incrmentation |
---|
[219] | 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 |
---|
[1053] | 238 | void CContext::initClient(ep_lib::MPI_Comm intraComm, ep_lib::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 | |
---|
[1053] | 250 | ep_lib::MPI_Comm intraCommServer, interCommServer; |
---|
[597] | 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); |
---|
[1063] | 262 | |
---|
[597] | 263 | } |
---|
| 264 | server = new CContextServer(this,intraCommServer,interCommServer); |
---|
[509] | 265 | } |
---|
[219] | 266 | |
---|
[509] | 267 | void CContext::setClientServerBuffer() |
---|
| 268 | { |
---|
[731] | 269 | size_t minBufferSize = CXios::minBufferSize; |
---|
[583] | 270 | #define DECLARE_NODE(Name_, name_) \ |
---|
[731] | 271 | if (minBufferSize < sizeof(C##Name_##Definition)) minBufferSize = sizeof(C##Name_##Definition); |
---|
[583] | 272 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 273 | #include "node_type.conf" |
---|
[719] | 274 | #undef DECLARE_NODE |
---|
| 275 | #undef DECLARE_NODE_PAR |
---|
| 276 | |
---|
[917] | 277 | std::map<int, StdSize> maxEventSize; |
---|
| 278 | std::map<int, StdSize> bufferSize = getAttributesBufferSize(maxEventSize); |
---|
| 279 | std::map<int, StdSize> dataBufferSize = getDataBufferSize(maxEventSize); |
---|
[729] | 280 | |
---|
[909] | 281 | std::map<int, StdSize>::iterator it, ite = dataBufferSize.end(); |
---|
[731] | 282 | for (it = dataBufferSize.begin(); it != ite; ++it) |
---|
| 283 | if (it->second > bufferSize[it->first]) bufferSize[it->first] = it->second; |
---|
| 284 | |
---|
[909] | 285 | ite = bufferSize.end(); |
---|
| 286 | for (it = bufferSize.begin(); it != ite; ++it) |
---|
| 287 | { |
---|
| 288 | it->second *= CXios::bufferSizeFactor; |
---|
| 289 | if (it->second < minBufferSize) it->second = minBufferSize; |
---|
| 290 | } |
---|
| 291 | |
---|
[917] | 292 | // We consider that the minimum buffer size is also the minimum event size |
---|
| 293 | ite = maxEventSize.end(); |
---|
| 294 | for (it = maxEventSize.begin(); it != ite; ++it) |
---|
| 295 | if (it->second < minBufferSize) it->second = minBufferSize; |
---|
| 296 | |
---|
[729] | 297 | if (client->isServerLeader()) |
---|
[598] | 298 | { |
---|
[729] | 299 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 300 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
[917] | 301 | if (!bufferSize.count(*itRank)) bufferSize[*itRank] = maxEventSize[*itRank] = minBufferSize; |
---|
[598] | 302 | } |
---|
[584] | 303 | |
---|
[917] | 304 | client->setBufferSize(bufferSize, maxEventSize); |
---|
[509] | 305 | } |
---|
| 306 | |
---|
| 307 | //! Verify whether a context is initialized |
---|
[461] | 308 | bool CContext::isInitialized(void) |
---|
| 309 | { |
---|
[549] | 310 | return hasClient; |
---|
[461] | 311 | } |
---|
[509] | 312 | |
---|
| 313 | //! Initialize server |
---|
[1053] | 314 | void CContext::initServer(ep_lib::MPI_Comm intraComm, ep_lib::MPI_Comm interComm, CContext* cxtClient /*= 0*/) |
---|
[300] | 315 | { |
---|
[549] | 316 | hasServer=true; |
---|
| 317 | server = new CContextServer(this,intraComm,interComm); |
---|
[697] | 318 | |
---|
| 319 | registryIn=new CRegistry(intraComm); |
---|
| 320 | registryIn->setPath(getId()) ; |
---|
| 321 | if (server->intraCommRank==0) registryIn->fromFile("xios_registry.bin") ; |
---|
| 322 | registryIn->bcastRegistry() ; |
---|
| 323 | registryOut=new CRegistry(intraComm) ; |
---|
| 324 | registryOut->setPath(getId()) ; |
---|
[775] | 325 | |
---|
[1053] | 326 | ep_lib::MPI_Comm intraCommClient, interCommClient; |
---|
[597] | 327 | if (cxtClient) // Attached mode |
---|
| 328 | { |
---|
| 329 | intraCommClient = intraComm; |
---|
| 330 | interCommClient = interComm; |
---|
| 331 | } |
---|
| 332 | else |
---|
| 333 | { |
---|
| 334 | MPI_Comm_dup(intraComm, &intraCommClient); |
---|
[655] | 335 | comms.push_back(intraCommClient); |
---|
[597] | 336 | MPI_Comm_dup(interComm, &interCommClient); |
---|
[655] | 337 | comms.push_back(interCommClient); |
---|
[597] | 338 | } |
---|
| 339 | client = new CContextClient(this,intraCommClient,interCommClient, cxtClient); |
---|
[509] | 340 | } |
---|
[300] | 341 | |
---|
[1037] | 342 | //! Server side: Put server into a loop in order to listen message from client |
---|
| 343 | bool CContext::eventLoop(void) |
---|
| 344 | { |
---|
| 345 | return server->eventLoop(); |
---|
| 346 | } |
---|
| 347 | |
---|
[597] | 348 | //! Try to send the buffers and receive possible answers |
---|
| 349 | bool CContext::checkBuffersAndListen(void) |
---|
| 350 | { |
---|
| 351 | client->checkBuffers(); |
---|
[1037] | 352 | return server->eventLoop(); |
---|
[597] | 353 | } |
---|
| 354 | |
---|
[509] | 355 | //! Terminate a context |
---|
[300] | 356 | void CContext::finalize(void) |
---|
| 357 | { |
---|
[1074] | 358 | |
---|
[597] | 359 | if (!finalized) |
---|
[300] | 360 | { |
---|
[597] | 361 | finalized = true; |
---|
[697] | 362 | if (hasClient) sendRegistry() ; |
---|
[597] | 363 | client->finalize(); |
---|
| 364 | while (!server->hasFinished()) |
---|
| 365 | { |
---|
| 366 | server->eventLoop(); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | if (hasServer) |
---|
| 370 | { |
---|
| 371 | closeAllFile(); |
---|
[697] | 372 | registryOut->hierarchicalGatherRegistry() ; |
---|
| 373 | if (server->intraCommRank==0) CXios::globalRegistry->mergeRegistry(*registryOut) ; |
---|
[597] | 374 | } |
---|
[775] | 375 | |
---|
[1053] | 376 | for (std::list<ep_lib::MPI_Comm>::iterator it = comms.begin(); it != comms.end(); ++it) |
---|
[655] | 377 | MPI_Comm_free(&(*it)); |
---|
| 378 | comms.clear(); |
---|
[300] | 379 | } |
---|
| 380 | } |
---|
[509] | 381 | |
---|
| 382 | /*! |
---|
| 383 | \brief Close all the context defintion and do processing data |
---|
| 384 | After everything is well defined on client side, they will be processed and sent to server |
---|
| 385 | From the version 2.0, sever and client work no more on the same database. Moreover, client(s) will send |
---|
| 386 | all necessary information to server, from which each server can build its own database. |
---|
| 387 | Because the role of server is to write out field data on a specific netcdf file, |
---|
| 388 | the only information that it needs is the enabled files |
---|
| 389 | and the active fields (fields will be written onto active files) |
---|
| 390 | */ |
---|
[300] | 391 | void CContext::closeDefinition(void) |
---|
| 392 | { |
---|
[1037] | 393 | |
---|
[584] | 394 | // There is nothing client need to send to server |
---|
[510] | 395 | if (hasClient) |
---|
[509] | 396 | { |
---|
| 397 | // After xml is parsed, there are some more works with post processing |
---|
[1037] | 398 | postProcessing(); |
---|
[510] | 399 | } |
---|
[1070] | 400 | setClientServerBuffer(); |
---|
[300] | 401 | |
---|
[510] | 402 | if (hasClient && !hasServer) |
---|
| 403 | { |
---|
[509] | 404 | // Send all attributes of current context to server |
---|
[1070] | 405 | this->sendAllAttributesToServer(); |
---|
[459] | 406 | |
---|
[549] | 407 | // Send all attributes of current calendar |
---|
| 408 | CCalendarWrapper::get(CCalendarWrapper::GetDefName())->sendAllAttributesToServer(); |
---|
| 409 | |
---|
[509] | 410 | // We have enough information to send to server |
---|
| 411 | // First of all, send all enabled files |
---|
[1070] | 412 | sendEnabledFiles(); |
---|
[509] | 413 | |
---|
| 414 | // Then, send all enabled fields |
---|
[1070] | 415 | sendEnabledFields(); |
---|
[509] | 416 | |
---|
[540] | 417 | // At last, we have all info of domain and axis, then send them |
---|
[1070] | 418 | sendRefDomainsAxis(); |
---|
[509] | 419 | // After that, send all grid (if any) |
---|
[1070] | 420 | sendRefGrid(); |
---|
[509] | 421 | } |
---|
| 422 | |
---|
| 423 | // We have a xml tree on the server side and now, it should be also processed |
---|
[1037] | 424 | if (hasClient && !hasServer) sendPostProcessing(); |
---|
[509] | 425 | |
---|
| 426 | // There are some processings that should be done after all of above. For example: check mask or index |
---|
[510] | 427 | if (hasClient) |
---|
[509] | 428 | { |
---|
[1070] | 429 | this->buildFilterGraphOfEnabledFields(); |
---|
| 430 | buildFilterGraphOfFieldsWithReadAccess(); |
---|
| 431 | this->solveAllRefOfEnabledFields(true); |
---|
[509] | 432 | } |
---|
| 433 | |
---|
[598] | 434 | // Now tell server that it can process all messages from client |
---|
| 435 | if (hasClient && !hasServer) this->sendCloseDefinition(); |
---|
[510] | 436 | |
---|
[584] | 437 | // Nettoyage de l'arborescence |
---|
[1037] | 438 | if (hasClient && !hasServer) CleanTree(); |
---|
[510] | 439 | |
---|
[598] | 440 | if (hasClient) |
---|
| 441 | { |
---|
[1070] | 442 | sendCreateFileHeader(); |
---|
[598] | 443 | |
---|
[1070] | 444 | startPrefetchingOfEnabledReadModeFiles(); |
---|
[598] | 445 | } |
---|
[300] | 446 | } |
---|
[509] | 447 | |
---|
[300] | 448 | void CContext::findAllEnabledFields(void) |
---|
| 449 | { |
---|
| 450 | for (unsigned int i = 0; i < this->enabledFiles.size(); i++) |
---|
| 451 | (void)this->enabledFiles[i]->getEnabledFields(); |
---|
| 452 | } |
---|
| 453 | |
---|
[775] | 454 | void CContext::findAllEnabledFieldsInReadModeFiles(void) |
---|
| 455 | { |
---|
| 456 | for (unsigned int i = 0; i < this->enabledReadModeFiles.size(); ++i) |
---|
| 457 | (void)this->enabledReadModeFiles[i]->getEnabledFields(); |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | void CContext::readAttributesOfEnabledFieldsInReadModeFiles() |
---|
| 461 | { |
---|
| 462 | for (unsigned int i = 0; i < this->enabledReadModeFiles.size(); ++i) |
---|
| 463 | (void)this->enabledReadModeFiles[i]->readAttributesOfEnabledFieldsInReadMode(); |
---|
| 464 | } |
---|
| 465 | |
---|
[823] | 466 | void CContext::solveOnlyRefOfEnabledFields(bool sendToServer) |
---|
| 467 | { |
---|
| 468 | int size = this->enabledFiles.size(); |
---|
| 469 | for (int i = 0; i < size; ++i) |
---|
| 470 | { |
---|
| 471 | this->enabledFiles[i]->solveOnlyRefOfEnabledFields(sendToServer); |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | for (int i = 0; i < size; ++i) |
---|
| 475 | { |
---|
| 476 | this->enabledFiles[i]->generateNewTransformationGridDest(); |
---|
| 477 | } |
---|
| 478 | } |
---|
| 479 | |
---|
[509] | 480 | void CContext::solveAllRefOfEnabledFields(bool sendToServer) |
---|
[300] | 481 | { |
---|
[509] | 482 | int size = this->enabledFiles.size(); |
---|
[1037] | 483 | |
---|
[509] | 484 | for (int i = 0; i < size; ++i) |
---|
| 485 | { |
---|
| 486 | this->enabledFiles[i]->solveAllRefOfEnabledFields(sendToServer); |
---|
| 487 | } |
---|
[300] | 488 | } |
---|
| 489 | |
---|
[640] | 490 | void CContext::buildFilterGraphOfEnabledFields() |
---|
| 491 | { |
---|
| 492 | int size = this->enabledFiles.size(); |
---|
| 493 | for (int i = 0; i < size; ++i) |
---|
| 494 | { |
---|
| 495 | this->enabledFiles[i]->buildFilterGraphOfEnabledFields(garbageCollector); |
---|
| 496 | } |
---|
| 497 | } |
---|
| 498 | |
---|
[598] | 499 | void CContext::startPrefetchingOfEnabledReadModeFiles() |
---|
| 500 | { |
---|
| 501 | int size = enabledReadModeFiles.size(); |
---|
| 502 | for (int i = 0; i < size; ++i) |
---|
| 503 | { |
---|
| 504 | enabledReadModeFiles[i]->prefetchEnabledReadModeFields(); |
---|
| 505 | } |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | void CContext::checkPrefetchingOfEnabledReadModeFiles() |
---|
| 509 | { |
---|
| 510 | int size = enabledReadModeFiles.size(); |
---|
| 511 | for (int i = 0; i < size; ++i) |
---|
| 512 | { |
---|
| 513 | enabledReadModeFiles[i]->prefetchEnabledReadModeFieldsIfNeeded(); |
---|
| 514 | } |
---|
| 515 | } |
---|
| 516 | |
---|
[593] | 517 | void CContext::findFieldsWithReadAccess(void) |
---|
| 518 | { |
---|
| 519 | fieldsWithReadAccess.clear(); |
---|
| 520 | const vector<CField*> allFields = CField::getAll(); |
---|
| 521 | for (size_t i = 0; i < allFields.size(); ++i) |
---|
| 522 | { |
---|
[740] | 523 | CField* field = allFields[i]; |
---|
| 524 | |
---|
| 525 | if (field->file && !field->file->mode.isEmpty() && field->file->mode == CFile::mode_attr::read) |
---|
| 526 | field->read_access = true; |
---|
| 527 | else if (!field->read_access.isEmpty() && field->read_access && (field->enabled.isEmpty() || field->enabled)) |
---|
| 528 | fieldsWithReadAccess.push_back(field); |
---|
[593] | 529 | } |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | void CContext::solveAllRefOfFieldsWithReadAccess() |
---|
| 533 | { |
---|
| 534 | for (size_t i = 0; i < fieldsWithReadAccess.size(); ++i) |
---|
| 535 | fieldsWithReadAccess[i]->solveAllReferenceEnabledField(false); |
---|
| 536 | } |
---|
| 537 | |
---|
[640] | 538 | void CContext::buildFilterGraphOfFieldsWithReadAccess() |
---|
| 539 | { |
---|
| 540 | for (size_t i = 0; i < fieldsWithReadAccess.size(); ++i) |
---|
| 541 | fieldsWithReadAccess[i]->buildFilterGraph(garbageCollector, true); |
---|
| 542 | } |
---|
| 543 | |
---|
[445] | 544 | void CContext::solveAllInheritance(bool apply) |
---|
[300] | 545 | { |
---|
[1056] | 546 | // Rsolution des hritages descendants (cd des hritages de groupes) |
---|
[300] | 547 | // pour chacun des contextes. |
---|
[445] | 548 | solveDescInheritance(apply); |
---|
[300] | 549 | |
---|
[1056] | 550 | // Rsolution des hritages par rfrence au niveau des fichiers. |
---|
[549] | 551 | const vector<CFile*> allFiles=CFile::getAll(); |
---|
[540] | 552 | const vector<CGrid*> allGrids= CGrid::getAll(); |
---|
[300] | 553 | |
---|
[510] | 554 | //if (hasClient && !hasServer) |
---|
| 555 | if (hasClient) |
---|
[540] | 556 | { |
---|
[510] | 557 | for (unsigned int i = 0; i < allFiles.size(); i++) |
---|
| 558 | allFiles[i]->solveFieldRefInheritance(apply); |
---|
[540] | 559 | } |
---|
| 560 | |
---|
| 561 | unsigned int vecSize = allGrids.size(); |
---|
| 562 | unsigned int i = 0; |
---|
| 563 | for (i = 0; i < vecSize; ++i) |
---|
| 564 | allGrids[i]->solveDomainAxisRefInheritance(apply); |
---|
| 565 | |
---|
[300] | 566 | } |
---|
| 567 | |
---|
| 568 | void CContext::findEnabledFiles(void) |
---|
| 569 | { |
---|
[347] | 570 | const std::vector<CFile*> allFiles = CFile::getAll(); |
---|
[300] | 571 | |
---|
| 572 | for (unsigned int i = 0; i < allFiles.size(); i++) |
---|
[1056] | 573 | if (!allFiles[i]->enabled.isEmpty()) // Si l'attribut 'enabled' est dfini. |
---|
[430] | 574 | { |
---|
[1056] | 575 | if (allFiles[i]->enabled.getValue()) // Si l'attribut 'enabled' est fix vrai. |
---|
[300] | 576 | enabledFiles.push_back(allFiles[i]); |
---|
[430] | 577 | } |
---|
[1037] | 578 | else enabledFiles.push_back(allFiles[i]); // otherwise true by default |
---|
[300] | 579 | |
---|
[1037] | 580 | |
---|
[300] | 581 | if (enabledFiles.size() == 0) |
---|
[1056] | 582 | DEBUG(<<"Aucun fichier ne va tre sorti dans le contexte nomm \"" |
---|
[300] | 583 | << getId() << "\" !"); |
---|
| 584 | } |
---|
| 585 | |
---|
[598] | 586 | void CContext::findEnabledReadModeFiles(void) |
---|
| 587 | { |
---|
| 588 | int size = this->enabledFiles.size(); |
---|
| 589 | for (int i = 0; i < size; ++i) |
---|
| 590 | { |
---|
| 591 | if (!enabledFiles[i]->mode.isEmpty() && enabledFiles[i]->mode.getValue() == CFile::mode_attr::read) |
---|
| 592 | enabledReadModeFiles.push_back(enabledFiles[i]); |
---|
| 593 | } |
---|
| 594 | } |
---|
| 595 | |
---|
[300] | 596 | void CContext::closeAllFile(void) |
---|
| 597 | { |
---|
[347] | 598 | std::vector<CFile*>::const_iterator |
---|
[300] | 599 | it = this->enabledFiles.begin(), end = this->enabledFiles.end(); |
---|
[509] | 600 | |
---|
[300] | 601 | for (; it != end; it++) |
---|
| 602 | { |
---|
| 603 | info(30)<<"Closing File : "<<(*it)->getId()<<endl; |
---|
| 604 | (*it)->close(); |
---|
| 605 | } |
---|
| 606 | } |
---|
[509] | 607 | |
---|
| 608 | /*! |
---|
| 609 | \brief Dispatch event received from client |
---|
| 610 | Whenever a message is received in buffer of server, it will be processed depending on |
---|
| 611 | its event type. A new event type should be added in the switch list to make sure |
---|
| 612 | it processed on server side. |
---|
| 613 | \param [in] event: Received message |
---|
| 614 | */ |
---|
[300] | 615 | bool CContext::dispatchEvent(CEventServer& event) |
---|
| 616 | { |
---|
[509] | 617 | |
---|
[549] | 618 | if (SuperClass::dispatchEvent(event)) return true; |
---|
[300] | 619 | else |
---|
| 620 | { |
---|
| 621 | switch(event.type) |
---|
| 622 | { |
---|
| 623 | case EVENT_ID_CLOSE_DEFINITION : |
---|
[549] | 624 | recvCloseDefinition(event); |
---|
| 625 | return true; |
---|
| 626 | break; |
---|
[584] | 627 | case EVENT_ID_UPDATE_CALENDAR: |
---|
[549] | 628 | recvUpdateCalendar(event); |
---|
| 629 | return true; |
---|
| 630 | break; |
---|
[300] | 631 | case EVENT_ID_CREATE_FILE_HEADER : |
---|
[549] | 632 | recvCreateFileHeader(event); |
---|
| 633 | return true; |
---|
| 634 | break; |
---|
[509] | 635 | case EVENT_ID_POST_PROCESS: |
---|
[549] | 636 | recvPostProcessing(event); |
---|
| 637 | return true; |
---|
[697] | 638 | case EVENT_ID_SEND_REGISTRY: |
---|
| 639 | recvRegistry(event); |
---|
| 640 | return true; |
---|
| 641 | break; |
---|
[584] | 642 | |
---|
[300] | 643 | default : |
---|
| 644 | ERROR("bool CContext::dispatchEvent(CEventServer& event)", |
---|
[549] | 645 | <<"Unknown Event"); |
---|
| 646 | return false; |
---|
[300] | 647 | } |
---|
| 648 | } |
---|
| 649 | } |
---|
[509] | 650 | |
---|
| 651 | //! Client side: Send a message to server to make it close |
---|
[300] | 652 | void CContext::sendCloseDefinition(void) |
---|
| 653 | { |
---|
[549] | 654 | CEventClient event(getType(),EVENT_ID_CLOSE_DEFINITION); |
---|
[300] | 655 | if (client->isServerLeader()) |
---|
| 656 | { |
---|
[549] | 657 | CMessage msg; |
---|
| 658 | msg<<this->getIdServer(); |
---|
[595] | 659 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 660 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 661 | event.push(*itRank,1,msg); |
---|
[549] | 662 | client->sendEvent(event); |
---|
[300] | 663 | } |
---|
[549] | 664 | else client->sendEvent(event); |
---|
[300] | 665 | } |
---|
[509] | 666 | |
---|
| 667 | //! Server side: Receive a message of client announcing a context close |
---|
[300] | 668 | void CContext::recvCloseDefinition(CEventServer& event) |
---|
| 669 | { |
---|
[509] | 670 | |
---|
[300] | 671 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 672 | string id; |
---|
[549] | 673 | *buffer>>id; |
---|
[509] | 674 | get(id)->closeDefinition(); |
---|
[300] | 675 | } |
---|
[509] | 676 | |
---|
| 677 | //! Client side: Send a message to update calendar in each time step |
---|
[300] | 678 | void CContext::sendUpdateCalendar(int step) |
---|
| 679 | { |
---|
| 680 | if (!hasServer) |
---|
| 681 | { |
---|
[549] | 682 | CEventClient event(getType(),EVENT_ID_UPDATE_CALENDAR); |
---|
[300] | 683 | if (client->isServerLeader()) |
---|
| 684 | { |
---|
[549] | 685 | CMessage msg; |
---|
| 686 | msg<<this->getIdServer()<<step; |
---|
[595] | 687 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 688 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 689 | event.push(*itRank,1,msg); |
---|
[549] | 690 | client->sendEvent(event); |
---|
[300] | 691 | } |
---|
[549] | 692 | else client->sendEvent(event); |
---|
[300] | 693 | } |
---|
| 694 | } |
---|
[509] | 695 | |
---|
| 696 | //! Server side: Receive a message of client annoucing calendar update |
---|
[300] | 697 | void CContext::recvUpdateCalendar(CEventServer& event) |
---|
| 698 | { |
---|
| 699 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 700 | string id; |
---|
[549] | 701 | *buffer>>id; |
---|
| 702 | get(id)->recvUpdateCalendar(*buffer); |
---|
[300] | 703 | } |
---|
[509] | 704 | |
---|
| 705 | //! Server side: Receive a message of client annoucing calendar update |
---|
[300] | 706 | void CContext::recvUpdateCalendar(CBufferIn& buffer) |
---|
| 707 | { |
---|
[549] | 708 | int step; |
---|
| 709 | buffer>>step; |
---|
| 710 | updateCalendar(step); |
---|
[300] | 711 | } |
---|
[509] | 712 | |
---|
| 713 | //! Client side: Send a message to create header part of netcdf file |
---|
[300] | 714 | void CContext::sendCreateFileHeader(void) |
---|
| 715 | { |
---|
[549] | 716 | CEventClient event(getType(),EVENT_ID_CREATE_FILE_HEADER); |
---|
[300] | 717 | if (client->isServerLeader()) |
---|
| 718 | { |
---|
[549] | 719 | CMessage msg; |
---|
| 720 | msg<<this->getIdServer(); |
---|
[595] | 721 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 722 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 723 | event.push(*itRank,1,msg) ; |
---|
[549] | 724 | client->sendEvent(event); |
---|
[300] | 725 | } |
---|
[549] | 726 | else client->sendEvent(event); |
---|
[300] | 727 | } |
---|
[509] | 728 | |
---|
| 729 | //! Server side: Receive a message of client annoucing the creation of header part of netcdf file |
---|
[300] | 730 | void CContext::recvCreateFileHeader(CEventServer& event) |
---|
| 731 | { |
---|
| 732 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 733 | string id; |
---|
[549] | 734 | *buffer>>id; |
---|
| 735 | get(id)->recvCreateFileHeader(*buffer); |
---|
[300] | 736 | } |
---|
[509] | 737 | |
---|
| 738 | //! Server side: Receive a message of client annoucing the creation of header part of netcdf file |
---|
[300] | 739 | void CContext::recvCreateFileHeader(CBufferIn& buffer) |
---|
| 740 | { |
---|
[549] | 741 | createFileHeader(); |
---|
[300] | 742 | } |
---|
[509] | 743 | |
---|
| 744 | //! Client side: Send a message to do some post processing on server |
---|
| 745 | void CContext::sendPostProcessing() |
---|
| 746 | { |
---|
| 747 | if (!hasServer) |
---|
| 748 | { |
---|
[549] | 749 | CEventClient event(getType(),EVENT_ID_POST_PROCESS); |
---|
[509] | 750 | if (client->isServerLeader()) |
---|
| 751 | { |
---|
[549] | 752 | CMessage msg; |
---|
[511] | 753 | msg<<this->getIdServer(); |
---|
[595] | 754 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 755 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 756 | event.push(*itRank,1,msg); |
---|
[549] | 757 | client->sendEvent(event); |
---|
[509] | 758 | } |
---|
[549] | 759 | else client->sendEvent(event); |
---|
[509] | 760 | } |
---|
| 761 | } |
---|
| 762 | |
---|
| 763 | //! Server side: Receive a message to do some post processing |
---|
| 764 | void CContext::recvPostProcessing(CEventServer& event) |
---|
| 765 | { |
---|
| 766 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 767 | string id; |
---|
| 768 | *buffer>>id; |
---|
| 769 | get(id)->recvPostProcessing(*buffer); |
---|
| 770 | } |
---|
| 771 | |
---|
| 772 | //! Server side: Receive a message to do some post processing |
---|
| 773 | void CContext::recvPostProcessing(CBufferIn& buffer) |
---|
| 774 | { |
---|
[549] | 775 | CCalendarWrapper::get(CCalendarWrapper::GetDefName())->createCalendar(); |
---|
[509] | 776 | postProcessing(); |
---|
| 777 | } |
---|
| 778 | |
---|
[511] | 779 | const StdString& CContext::getIdServer() |
---|
| 780 | { |
---|
| 781 | if (hasClient) |
---|
| 782 | { |
---|
| 783 | idServer_ = this->getId(); |
---|
| 784 | idServer_ += "_server"; |
---|
| 785 | return idServer_; |
---|
| 786 | } |
---|
| 787 | if (hasServer) return (this->getId()); |
---|
| 788 | } |
---|
| 789 | |
---|
[509] | 790 | /*! |
---|
| 791 | \brief Do some simple post processings after parsing xml file |
---|
| 792 | After the xml file (iodef.xml) is parsed, it is necessary to build all relations among |
---|
| 793 | created object, e.g: inhertance among fields, domain, axis. After that, all fiels as well as their parents (reference fields), |
---|
| 794 | which will be written out into netcdf files, are processed |
---|
| 795 | */ |
---|
| 796 | void CContext::postProcessing() |
---|
| 797 | { |
---|
[1037] | 798 | int myRank; |
---|
| 799 | MPI_Comm_rank(MPI_COMM_WORLD, &myRank); |
---|
| 800 | |
---|
| 801 | //printf("myRank = %d, in postProcessing, isPostProcessed = %d\n", myRank, isPostProcessed); |
---|
[509] | 802 | if (isPostProcessed) return; |
---|
| 803 | |
---|
[549] | 804 | // Make sure the calendar was correctly created |
---|
| 805 | if (!calendar) |
---|
| 806 | ERROR("CContext::postProcessing()", << "A calendar must be defined for the context \"" << getId() << "!\"") |
---|
| 807 | else if (calendar->getTimeStep() == NoneDu) |
---|
| 808 | ERROR("CContext::postProcessing()", << "A timestep must be defined for the context \"" << getId() << "!\"") |
---|
[550] | 809 | // Calendar first update to set the current date equals to the start date |
---|
[1037] | 810 | calendar->update(0); //printf("myRank = %d, calendar->update(0) OK\n", myRank); |
---|
[509] | 811 | |
---|
| 812 | // Find all inheritance in xml structure |
---|
[1037] | 813 | this->solveAllInheritance(); //printf("myRank = %d, this->solveAllInheritance OK\n", myRank); |
---|
[509] | 814 | |
---|
[676] | 815 | // Check if some axis, domains or grids are eligible to for compressed indexed output. |
---|
| 816 | // Warning: This must be done after solving the inheritance and before the rest of post-processing |
---|
[1037] | 817 | checkAxisDomainsGridsEligibilityForCompressedOutput(); //printf("myRank = %d, checkAxisDomainsGridsEligibilityForCompressedOutput OK\n", myRank); |
---|
[676] | 818 | |
---|
[711] | 819 | // Check if some automatic time series should be generated |
---|
| 820 | // Warning: This must be done after solving the inheritance and before the rest of post-processing |
---|
[1037] | 821 | prepareTimeseries(); //printf("myRank = %d, prepareTimeseries OK\n", myRank); |
---|
[711] | 822 | |
---|
[1056] | 823 | //Initialisation du vecteur 'enabledFiles' contenant la liste des fichiers sortir. |
---|
[1037] | 824 | this->findEnabledFiles(); //printf("myRank = %d, this->findEnabledFiles OK\n", myRank); |
---|
| 825 | this->findEnabledReadModeFiles(); //printf("myRank = %d, this->findEnabledReadModeFiles OK\n", myRank); |
---|
[509] | 826 | |
---|
| 827 | // Find all enabled fields of each file |
---|
[1037] | 828 | this->findAllEnabledFields(); //printf("myRank = %d, this->findAllEnabledFields OK\n", myRank); |
---|
| 829 | this->findAllEnabledFieldsInReadModeFiles(); //printf("myRank = %d, this->findAllEnabledFieldsInReadModeFiles OK\n", myRank); |
---|
[509] | 830 | |
---|
[775] | 831 | if (hasClient && !hasServer) |
---|
| 832 | { |
---|
| 833 | // Try to read attributes of fields in file then fill in corresponding grid (or domain, axis) |
---|
[1037] | 834 | this->readAttributesOfEnabledFieldsInReadModeFiles(); //printf("myRank = %d, this->readAttributesOfEnabledFieldsInReadModeFiles OK\n", myRank); |
---|
[775] | 835 | } |
---|
[823] | 836 | |
---|
| 837 | // Only search and rebuild all reference objects of enable fields, don't transform |
---|
[1037] | 838 | this->solveOnlyRefOfEnabledFields(false); //printf("myRank = %d, this->solveOnlyRefOfEnabledFields(false) OK\n", myRank); |
---|
[823] | 839 | |
---|
[509] | 840 | // Search and rebuild all reference object of enabled fields |
---|
[1037] | 841 | this->solveAllRefOfEnabledFields(false); //printf("myRank = %d, this->solveAllRefOfEnabledFields(false) OK\n", myRank); |
---|
[593] | 842 | |
---|
| 843 | // Find all fields with read access from the public API |
---|
[1037] | 844 | findFieldsWithReadAccess(); //printf("myRank = %d, findFieldsWithReadAccess OK\n", myRank); |
---|
[593] | 845 | // and solve the all reference for them |
---|
[1037] | 846 | solveAllRefOfFieldsWithReadAccess(); //printf("myRank = %d, solveAllRefOfFieldsWithReadAccess OK\n", myRank); |
---|
[593] | 847 | |
---|
[509] | 848 | isPostProcessed = true; |
---|
| 849 | } |
---|
| 850 | |
---|
[917] | 851 | /*! |
---|
| 852 | * Compute the required buffer size to send the attributes (mostly those grid related). |
---|
| 853 | * |
---|
| 854 | * \param maxEventSize [in/out] the size of the bigger event for each connected server |
---|
| 855 | */ |
---|
| 856 | std::map<int, StdSize> CContext::getAttributesBufferSize(std::map<int, StdSize>& maxEventSize) |
---|
[509] | 857 | { |
---|
[731] | 858 | std::map<int, StdSize> attributesSize; |
---|
| 859 | |
---|
[908] | 860 | if (hasClient) |
---|
[731] | 861 | { |
---|
[908] | 862 | size_t numEnabledFiles = this->enabledFiles.size(); |
---|
| 863 | for (size_t i = 0; i < numEnabledFiles; ++i) |
---|
| 864 | { |
---|
| 865 | CFile* file = this->enabledFiles[i]; |
---|
[731] | 866 | |
---|
[908] | 867 | std::vector<CField*> enabledFields = file->getEnabledFields(); |
---|
| 868 | size_t numEnabledFields = enabledFields.size(); |
---|
| 869 | for (size_t j = 0; j < numEnabledFields; ++j) |
---|
[731] | 870 | { |
---|
[908] | 871 | const std::map<int, StdSize> mapSize = enabledFields[j]->getGridAttributesBufferSize(); |
---|
| 872 | std::map<int, StdSize>::const_iterator it = mapSize.begin(), itE = mapSize.end(); |
---|
| 873 | for (; it != itE; ++it) |
---|
| 874 | { |
---|
| 875 | // If attributesSize[it->first] does not exist, it will be zero-initialized |
---|
| 876 | // so we can use it safely without checking for its existance |
---|
| 877 | if (attributesSize[it->first] < it->second) |
---|
| 878 | attributesSize[it->first] = it->second; |
---|
[917] | 879 | |
---|
| 880 | if (maxEventSize[it->first] < it->second) |
---|
| 881 | maxEventSize[it->first] = it->second; |
---|
[908] | 882 | } |
---|
[731] | 883 | } |
---|
| 884 | } |
---|
| 885 | } |
---|
| 886 | |
---|
| 887 | return attributesSize; |
---|
| 888 | } |
---|
| 889 | |
---|
[917] | 890 | /*! |
---|
| 891 | * Compute the required buffer size to send the fields data. |
---|
| 892 | * |
---|
| 893 | * \param maxEventSize [in/out] the size of the bigger event for each connected server |
---|
| 894 | */ |
---|
| 895 | std::map<int, StdSize> CContext::getDataBufferSize(std::map<int, StdSize>& maxEventSize) |
---|
[731] | 896 | { |
---|
[598] | 897 | CFile::mode_attr::t_enum mode = hasClient ? CFile::mode_attr::write : CFile::mode_attr::read; |
---|
| 898 | |
---|
[730] | 899 | std::map<int, StdSize> dataSize; |
---|
[509] | 900 | |
---|
| 901 | // Find all reference domain and axis of all active fields |
---|
[730] | 902 | size_t numEnabledFiles = this->enabledFiles.size(); |
---|
| 903 | for (size_t i = 0; i < numEnabledFiles; ++i) |
---|
[509] | 904 | { |
---|
[598] | 905 | CFile* file = this->enabledFiles[i]; |
---|
| 906 | CFile::mode_attr::t_enum fileMode = file->mode.isEmpty() ? CFile::mode_attr::write : file->mode.getValue(); |
---|
| 907 | |
---|
| 908 | if (fileMode == mode) |
---|
[509] | 909 | { |
---|
[598] | 910 | std::vector<CField*> enabledFields = file->getEnabledFields(); |
---|
[730] | 911 | size_t numEnabledFields = enabledFields.size(); |
---|
| 912 | for (size_t j = 0; j < numEnabledFields; ++j) |
---|
[509] | 913 | { |
---|
[731] | 914 | const std::map<int, StdSize> mapSize = enabledFields[j]->getGridDataBufferSize(); |
---|
[730] | 915 | std::map<int, StdSize>::const_iterator it = mapSize.begin(), itE = mapSize.end(); |
---|
| 916 | for (; it != itE; ++it) |
---|
[509] | 917 | { |
---|
[730] | 918 | // If dataSize[it->first] does not exist, it will be zero-initialized |
---|
| 919 | // so we can use it safely without checking for its existance |
---|
| 920 | if (CXios::isOptPerformance) |
---|
| 921 | dataSize[it->first] += it->second; |
---|
| 922 | else if (dataSize[it->first] < it->second) |
---|
| 923 | dataSize[it->first] = it->second; |
---|
[917] | 924 | |
---|
| 925 | if (maxEventSize[it->first] < it->second) |
---|
| 926 | maxEventSize[it->first] = it->second; |
---|
[598] | 927 | } |
---|
[509] | 928 | } |
---|
| 929 | } |
---|
| 930 | } |
---|
| 931 | |
---|
[730] | 932 | return dataSize; |
---|
[509] | 933 | } |
---|
| 934 | |
---|
| 935 | //! Client side: Send infomation of active files (files are enabled to write out) |
---|
| 936 | void CContext::sendEnabledFiles() |
---|
| 937 | { |
---|
| 938 | int size = this->enabledFiles.size(); |
---|
| 939 | |
---|
| 940 | // In a context, each type has a root definition, e.g: axis, domain, field. |
---|
| 941 | // Every object must be a child of one of these root definition. In this case |
---|
| 942 | // all new file objects created on server must be children of the root "file_definition" |
---|
| 943 | StdString fileDefRoot("file_definition"); |
---|
| 944 | CFileGroup* cfgrpPtr = CFileGroup::get(fileDefRoot); |
---|
| 945 | |
---|
| 946 | for (int i = 0; i < size; ++i) |
---|
| 947 | { |
---|
| 948 | cfgrpPtr->sendCreateChild(this->enabledFiles[i]->getId()); |
---|
| 949 | this->enabledFiles[i]->sendAllAttributesToServer(); |
---|
| 950 | this->enabledFiles[i]->sendAddAllVariables(); |
---|
| 951 | } |
---|
| 952 | } |
---|
| 953 | |
---|
| 954 | //! Client side: Send information of active fields (ones are written onto files) |
---|
| 955 | void CContext::sendEnabledFields() |
---|
| 956 | { |
---|
| 957 | int size = this->enabledFiles.size(); |
---|
| 958 | for (int i = 0; i < size; ++i) |
---|
| 959 | { |
---|
| 960 | this->enabledFiles[i]->sendEnabledFields(); |
---|
| 961 | } |
---|
| 962 | } |
---|
| 963 | |
---|
[676] | 964 | //! Client side: Check if the defined axis, domains and grids are eligible for compressed indexed output |
---|
| 965 | void CContext::checkAxisDomainsGridsEligibilityForCompressedOutput() |
---|
| 966 | { |
---|
| 967 | if (!hasClient) return; |
---|
| 968 | |
---|
| 969 | const vector<CAxis*> allAxis = CAxis::getAll(); |
---|
| 970 | for (vector<CAxis*>::const_iterator it = allAxis.begin(); it != allAxis.end(); it++) |
---|
| 971 | (*it)->checkEligibilityForCompressedOutput(); |
---|
| 972 | |
---|
| 973 | const vector<CDomain*> allDomains = CDomain::getAll(); |
---|
| 974 | for (vector<CDomain*>::const_iterator it = allDomains.begin(); it != allDomains.end(); it++) |
---|
| 975 | (*it)->checkEligibilityForCompressedOutput(); |
---|
| 976 | |
---|
| 977 | const vector<CGrid*> allGrids = CGrid::getAll(); |
---|
| 978 | for (vector<CGrid*>::const_iterator it = allGrids.begin(); it != allGrids.end(); it++) |
---|
| 979 | (*it)->checkEligibilityForCompressedOutput(); |
---|
| 980 | } |
---|
| 981 | |
---|
[711] | 982 | //! Client side: Prepare the timeseries by adding the necessary files |
---|
| 983 | void CContext::prepareTimeseries() |
---|
| 984 | { |
---|
| 985 | if (!hasClient) return; |
---|
| 986 | |
---|
| 987 | const std::vector<CFile*> allFiles = CFile::getAll(); |
---|
| 988 | for (size_t i = 0; i < allFiles.size(); i++) |
---|
| 989 | { |
---|
| 990 | CFile* file = allFiles[i]; |
---|
| 991 | |
---|
| 992 | if (!file->timeseries.isEmpty() && file->timeseries != CFile::timeseries_attr::none) |
---|
| 993 | { |
---|
[773] | 994 | StdString tsPrefix = !file->ts_prefix.isEmpty() ? file->ts_prefix : file->getFileOutputName(); |
---|
[711] | 995 | |
---|
| 996 | const std::vector<CField*> allFields = file->getAllFields(); |
---|
| 997 | for (size_t j = 0; j < allFields.size(); j++) |
---|
| 998 | { |
---|
| 999 | CField* field = allFields[j]; |
---|
| 1000 | |
---|
| 1001 | if (!field->ts_enabled.isEmpty() && field->ts_enabled) |
---|
| 1002 | { |
---|
| 1003 | CFile* tsFile = CFile::create(); |
---|
| 1004 | tsFile->duplicateAttributes(file); |
---|
[928] | 1005 | tsFile->setVirtualVariableGroup(file->getVirtualVariableGroup()); |
---|
[711] | 1006 | |
---|
| 1007 | tsFile->name = tsPrefix + "_"; |
---|
| 1008 | if (!field->name.isEmpty()) |
---|
| 1009 | tsFile->name.get() += field->name; |
---|
| 1010 | else if (field->hasDirectFieldReference()) // We cannot use getBaseFieldReference() just yet |
---|
| 1011 | tsFile->name.get() += field->field_ref; |
---|
| 1012 | else |
---|
| 1013 | tsFile->name.get() += field->getId(); |
---|
| 1014 | |
---|
| 1015 | if (!field->ts_split_freq.isEmpty()) |
---|
| 1016 | tsFile->split_freq = field->ts_split_freq; |
---|
| 1017 | |
---|
| 1018 | CField* tsField = tsFile->addField(); |
---|
| 1019 | tsField->field_ref = field->getId(); |
---|
[958] | 1020 | tsField->setVirtualVariableGroup(field->getVirtualVariableGroup()); |
---|
[711] | 1021 | |
---|
| 1022 | tsFile->solveFieldRefInheritance(true); |
---|
| 1023 | |
---|
| 1024 | if (file->timeseries == CFile::timeseries_attr::exclusive) |
---|
| 1025 | field->enabled = false; |
---|
| 1026 | } |
---|
| 1027 | } |
---|
| 1028 | |
---|
| 1029 | // Finally disable the original file is need be |
---|
| 1030 | if (file->timeseries == CFile::timeseries_attr::only) |
---|
| 1031 | file->enabled = false; |
---|
| 1032 | } |
---|
| 1033 | } |
---|
| 1034 | } |
---|
| 1035 | |
---|
[509] | 1036 | //! Client side: Send information of reference grid of active fields |
---|
| 1037 | void CContext::sendRefGrid() |
---|
| 1038 | { |
---|
| 1039 | std::set<StdString> gridIds; |
---|
| 1040 | int sizeFile = this->enabledFiles.size(); |
---|
| 1041 | CFile* filePtr(NULL); |
---|
| 1042 | |
---|
| 1043 | // Firstly, find all reference grids of all active fields |
---|
| 1044 | for (int i = 0; i < sizeFile; ++i) |
---|
| 1045 | { |
---|
| 1046 | filePtr = this->enabledFiles[i]; |
---|
| 1047 | std::vector<CField*> enabledFields = filePtr->getEnabledFields(); |
---|
| 1048 | int sizeField = enabledFields.size(); |
---|
| 1049 | for (int numField = 0; numField < sizeField; ++numField) |
---|
| 1050 | { |
---|
| 1051 | if (0 != enabledFields[numField]->getRelGrid()) |
---|
| 1052 | gridIds.insert(CGrid::get(enabledFields[numField]->getRelGrid())->getId()); |
---|
| 1053 | } |
---|
| 1054 | } |
---|
| 1055 | |
---|
| 1056 | // Create all reference grids on server side |
---|
| 1057 | StdString gridDefRoot("grid_definition"); |
---|
| 1058 | CGridGroup* gridPtr = CGridGroup::get(gridDefRoot); |
---|
| 1059 | std::set<StdString>::const_iterator it, itE = gridIds.end(); |
---|
| 1060 | for (it = gridIds.begin(); it != itE; ++it) |
---|
| 1061 | { |
---|
| 1062 | gridPtr->sendCreateChild(*it); |
---|
| 1063 | CGrid::get(*it)->sendAllAttributesToServer(); |
---|
[540] | 1064 | CGrid::get(*it)->sendAllDomains(); |
---|
| 1065 | CGrid::get(*it)->sendAllAxis(); |
---|
[887] | 1066 | CGrid::get(*it)->sendAllScalars(); |
---|
[509] | 1067 | } |
---|
| 1068 | } |
---|
| 1069 | |
---|
| 1070 | |
---|
| 1071 | //! Client side: Send information of reference domain and axis of active fields |
---|
[569] | 1072 | void CContext::sendRefDomainsAxis() |
---|
| 1073 | { |
---|
[887] | 1074 | std::set<StdString> domainIds, axisIds, scalarIds; |
---|
[509] | 1075 | |
---|
[569] | 1076 | // Find all reference domain and axis of all active fields |
---|
| 1077 | int numEnabledFiles = this->enabledFiles.size(); |
---|
| 1078 | for (int i = 0; i < numEnabledFiles; ++i) |
---|
| 1079 | { |
---|
| 1080 | std::vector<CField*> enabledFields = this->enabledFiles[i]->getEnabledFields(); |
---|
| 1081 | int numEnabledFields = enabledFields.size(); |
---|
| 1082 | for (int j = 0; j < numEnabledFields; ++j) |
---|
| 1083 | { |
---|
[887] | 1084 | const std::vector<StdString>& prDomAxisScalarId = enabledFields[j]->getRefDomainAxisIds(); |
---|
| 1085 | if ("" != prDomAxisScalarId[0]) domainIds.insert(prDomAxisScalarId[0]); |
---|
| 1086 | if ("" != prDomAxisScalarId[1]) axisIds.insert(prDomAxisScalarId[1]); |
---|
| 1087 | if ("" != prDomAxisScalarId[2]) scalarIds.insert(prDomAxisScalarId[2]); |
---|
[569] | 1088 | } |
---|
| 1089 | } |
---|
| 1090 | |
---|
| 1091 | // Create all reference axis on server side |
---|
[887] | 1092 | std::set<StdString>::iterator itDom, itAxis, itScalar; |
---|
[569] | 1093 | std::set<StdString>::const_iterator itE; |
---|
| 1094 | |
---|
[887] | 1095 | StdString scalarDefRoot("scalar_definition"); |
---|
| 1096 | CScalarGroup* scalarPtr = CScalarGroup::get(scalarDefRoot); |
---|
| 1097 | itE = scalarIds.end(); |
---|
| 1098 | for (itScalar = scalarIds.begin(); itScalar != itE; ++itScalar) |
---|
| 1099 | { |
---|
| 1100 | if (!itScalar->empty()) |
---|
| 1101 | { |
---|
| 1102 | scalarPtr->sendCreateChild(*itScalar); |
---|
| 1103 | CScalar::get(*itScalar)->sendAllAttributesToServer(); |
---|
| 1104 | } |
---|
| 1105 | } |
---|
| 1106 | |
---|
[569] | 1107 | StdString axiDefRoot("axis_definition"); |
---|
| 1108 | CAxisGroup* axisPtr = CAxisGroup::get(axiDefRoot); |
---|
| 1109 | itE = axisIds.end(); |
---|
| 1110 | for (itAxis = axisIds.begin(); itAxis != itE; ++itAxis) |
---|
| 1111 | { |
---|
| 1112 | if (!itAxis->empty()) |
---|
| 1113 | { |
---|
| 1114 | axisPtr->sendCreateChild(*itAxis); |
---|
| 1115 | CAxis::get(*itAxis)->sendAllAttributesToServer(); |
---|
| 1116 | } |
---|
| 1117 | } |
---|
| 1118 | |
---|
| 1119 | // Create all reference domains on server side |
---|
| 1120 | StdString domDefRoot("domain_definition"); |
---|
| 1121 | CDomainGroup* domPtr = CDomainGroup::get(domDefRoot); |
---|
| 1122 | itE = domainIds.end(); |
---|
| 1123 | for (itDom = domainIds.begin(); itDom != itE; ++itDom) |
---|
| 1124 | { |
---|
| 1125 | if (!itDom->empty()) { |
---|
| 1126 | domPtr->sendCreateChild(*itDom); |
---|
| 1127 | CDomain::get(*itDom)->sendAllAttributesToServer(); |
---|
| 1128 | } |
---|
| 1129 | } |
---|
| 1130 | } |
---|
| 1131 | |
---|
[509] | 1132 | //! Update calendar in each time step |
---|
[300] | 1133 | void CContext::updateCalendar(int step) |
---|
| 1134 | { |
---|
[639] | 1135 | info(50) << "updateCalendar : before : " << calendar->getCurrentDate() << endl; |
---|
[549] | 1136 | calendar->update(step); |
---|
[639] | 1137 | info(50) << "updateCalendar : after : " << calendar->getCurrentDate() << endl; |
---|
| 1138 | |
---|
| 1139 | if (hasClient) |
---|
| 1140 | { |
---|
[806] | 1141 | checkPrefetchingOfEnabledReadModeFiles(); |
---|
[639] | 1142 | garbageCollector.invalidate(calendar->getCurrentDate()); |
---|
| 1143 | } |
---|
[300] | 1144 | } |
---|
[509] | 1145 | |
---|
| 1146 | //! Server side: Create header of netcdf file |
---|
[300] | 1147 | void CContext::createFileHeader(void ) |
---|
| 1148 | { |
---|
[549] | 1149 | vector<CFile*>::const_iterator it; |
---|
[509] | 1150 | |
---|
[300] | 1151 | for (it=enabledFiles.begin(); it != enabledFiles.end(); it++) |
---|
| 1152 | { |
---|
[321] | 1153 | (*it)->initFile(); |
---|
[300] | 1154 | } |
---|
[509] | 1155 | } |
---|
| 1156 | |
---|
| 1157 | //! Get current context |
---|
[347] | 1158 | CContext* CContext::getCurrent(void) |
---|
[300] | 1159 | { |
---|
[549] | 1160 | return CObjectFactory::GetObject<CContext>(CObjectFactory::GetCurrentContextId()).get(); |
---|
[300] | 1161 | } |
---|
[509] | 1162 | |
---|
| 1163 | /*! |
---|
| 1164 | \brief Set context with an id be the current context |
---|
| 1165 | \param [in] id identity of context to be set to current |
---|
| 1166 | */ |
---|
[346] | 1167 | void CContext::setCurrent(const string& id) |
---|
| 1168 | { |
---|
| 1169 | CObjectFactory::SetCurrentContextId(id); |
---|
| 1170 | CGroupFactory::SetCurrentContextId(id); |
---|
| 1171 | } |
---|
[509] | 1172 | |
---|
| 1173 | /*! |
---|
| 1174 | \brief Create a context with specific id |
---|
| 1175 | \param [in] id identity of new context |
---|
| 1176 | \return pointer to the new context or already-existed one with identity id |
---|
| 1177 | */ |
---|
[347] | 1178 | CContext* CContext::create(const StdString& id) |
---|
[346] | 1179 | { |
---|
[549] | 1180 | CContext::setCurrent(id); |
---|
[509] | 1181 | |
---|
[346] | 1182 | bool hasctxt = CContext::has(id); |
---|
[347] | 1183 | CContext* context = CObjectFactory::CreateObject<CContext>(id).get(); |
---|
[549] | 1184 | getRoot(); |
---|
[347] | 1185 | if (!hasctxt) CGroupFactory::AddChild(root, context->getShared()); |
---|
[346] | 1186 | |
---|
| 1187 | #define DECLARE_NODE(Name_, name_) \ |
---|
| 1188 | C##Name_##Definition::create(C##Name_##Definition::GetDefName()); |
---|
| 1189 | #define DECLARE_NODE_PAR(Name_, name_) |
---|
| 1190 | #include "node_type.conf" |
---|
| 1191 | |
---|
| 1192 | return (context); |
---|
| 1193 | } |
---|
[697] | 1194 | |
---|
| 1195 | |
---|
| 1196 | |
---|
| 1197 | //! Server side: Receive a message to do some post processing |
---|
| 1198 | void CContext::recvRegistry(CEventServer& event) |
---|
| 1199 | { |
---|
| 1200 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 1201 | string id; |
---|
| 1202 | *buffer>>id; |
---|
| 1203 | get(id)->recvRegistry(*buffer); |
---|
| 1204 | } |
---|
| 1205 | |
---|
| 1206 | void CContext::recvRegistry(CBufferIn& buffer) |
---|
| 1207 | { |
---|
| 1208 | if (server->intraCommRank==0) |
---|
| 1209 | { |
---|
| 1210 | CRegistry registry(server->intraComm) ; |
---|
| 1211 | registry.fromBuffer(buffer) ; |
---|
| 1212 | registryOut->mergeRegistry(registry) ; |
---|
| 1213 | } |
---|
| 1214 | } |
---|
| 1215 | |
---|
| 1216 | void CContext::sendRegistry(void) |
---|
| 1217 | { |
---|
| 1218 | registryOut->hierarchicalGatherRegistry() ; |
---|
| 1219 | |
---|
| 1220 | CEventClient event(CContext::GetType(), CContext::EVENT_ID_SEND_REGISTRY); |
---|
| 1221 | if (client->isServerLeader()) |
---|
| 1222 | { |
---|
| 1223 | CMessage msg ; |
---|
| 1224 | msg<<this->getIdServer(); |
---|
| 1225 | if (client->clientRank==0) msg<<*registryOut ; |
---|
| 1226 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 1227 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 1228 | event.push(*itRank,1,msg); |
---|
| 1229 | client->sendEvent(event); |
---|
[1037] | 1230 | } |
---|
| 1231 | else client->sendEvent(event); |
---|
[697] | 1232 | } |
---|
| 1233 | |
---|
[335] | 1234 | } // namespace xios |
---|