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