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