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