[490] | 1 | #include "globalScopeData.hpp" |
---|
[591] | 2 | #include "xios_spl.hpp" |
---|
[300] | 3 | #include "cxios.hpp" |
---|
[342] | 4 | #include "client.hpp" |
---|
[300] | 5 | #include <boost/functional/hash.hpp> |
---|
| 6 | #include "type.hpp" |
---|
| 7 | #include "context.hpp" |
---|
| 8 | #include "context_client.hpp" |
---|
| 9 | #include "oasis_cinterface.hpp" |
---|
[382] | 10 | #include "mpi.hpp" |
---|
[347] | 11 | #include "timer.hpp" |
---|
[400] | 12 | #include "buffer_client.hpp" |
---|
[1587] | 13 | #include "string_tools.hpp" |
---|
[1761] | 14 | #include "ressources_manager.hpp" |
---|
| 15 | #include "services_manager.hpp" |
---|
| 16 | #include <functional> |
---|
| 17 | #include <cstdio> |
---|
[2146] | 18 | #include "workflow_graph.hpp" |
---|
[2274] | 19 | #include "release_static_allocation.hpp" |
---|
[2418] | 20 | #include "mem_checker.hpp" |
---|
[300] | 21 | |
---|
[335] | 22 | namespace xios |
---|
[490] | 23 | { |
---|
[300] | 24 | |
---|
[1761] | 25 | const double serverPublishDefaultTimeout=10; |
---|
| 26 | |
---|
[2332] | 27 | MPI_Comm CClient::intraComm_ ; |
---|
| 28 | MPI_Comm CClient::interComm_ ; |
---|
[1761] | 29 | MPI_Comm CClient::clientsComm_ ; |
---|
| 30 | |
---|
[1639] | 31 | std::list<MPI_Comm> CClient::contextInterComms; |
---|
[1158] | 32 | int CClient::serverLeader ; |
---|
[300] | 33 | bool CClient::is_MPI_Initialized ; |
---|
[1148] | 34 | int CClient::rank_ = INVALID_RANK; |
---|
[490] | 35 | StdOFStream CClient::m_infoStream; |
---|
[523] | 36 | StdOFStream CClient::m_errorStream; |
---|
[1761] | 37 | CPoolRessource* CClient::poolRessource_=nullptr ; |
---|
| 38 | |
---|
[2332] | 39 | MPI_Comm& CClient::getInterComm(void) { return (interComm_); } |
---|
[1587] | 40 | |
---|
[983] | 41 | ///--------------------------------------------------------------- |
---|
| 42 | /*! |
---|
| 43 | * \fn void CClient::initialize(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm) |
---|
| 44 | * Function creates intraComm (CClient::intraComm) for client group with id=codeId and interComm (CClient::interComm) between client and server groups. |
---|
| 45 | * \param [in] codeId identity of context. |
---|
| 46 | * \param [in/out] localComm local communicator. |
---|
| 47 | * \param [in/out] returnComm (intra)communicator of client group. |
---|
| 48 | */ |
---|
| 49 | |
---|
[1639] | 50 | void CClient::initialize(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm) |
---|
[300] | 51 | { |
---|
[1761] | 52 | |
---|
| 53 | MPI_Comm clientComm ; |
---|
| 54 | // initialize MPI if not initialized |
---|
[300] | 55 | int initialized ; |
---|
[1639] | 56 | MPI_Initialized(&initialized) ; |
---|
[300] | 57 | if (initialized) is_MPI_Initialized=true ; |
---|
| 58 | else is_MPI_Initialized=false ; |
---|
[1761] | 59 | |
---|
| 60 | MPI_Comm globalComm=CXios::getGlobalComm() ; |
---|
| 61 | |
---|
| 62 | ///////////////////////////////////////// |
---|
| 63 | ///////////// PART 1 //////////////////// |
---|
| 64 | ///////////////////////////////////////// |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | // localComm isn't given |
---|
| 68 | if (localComm == MPI_COMM_NULL) |
---|
| 69 | { |
---|
| 70 | |
---|
| 71 | // don't use OASIS |
---|
| 72 | if (!CXios::usingOasis) |
---|
| 73 | { |
---|
| 74 | |
---|
| 75 | if (!is_MPI_Initialized) |
---|
| 76 | { |
---|
| 77 | MPI_Init(NULL, NULL); |
---|
| 78 | } |
---|
| 79 | CTimer::get("XIOS").resume() ; |
---|
| 80 | CTimer::get("XIOS init/finalize",false).resume() ; |
---|
| 81 | |
---|
| 82 | // split the global communicator |
---|
| 83 | // get hash from all model to attribute a unique color (int) and then split to get client communicator |
---|
| 84 | // every mpi process of globalComm (MPI_COMM_WORLD) must participate |
---|
| 85 | |
---|
| 86 | int commRank, commSize ; |
---|
| 87 | MPI_Comm_rank(globalComm,&commRank) ; |
---|
| 88 | MPI_Comm_size(globalComm,&commSize) ; |
---|
| 89 | |
---|
| 90 | std::hash<string> hashString ; |
---|
| 91 | size_t hashClient=hashString(codeId) ; |
---|
| 92 | |
---|
| 93 | size_t* hashAll = new size_t[commSize] ; |
---|
[2238] | 94 | MPI_Allgather(&hashClient,1,MPI_SIZE_T,hashAll,1,MPI_SIZE_T,globalComm) ; |
---|
[1761] | 95 | |
---|
| 96 | int color=0 ; |
---|
[2238] | 97 | map<size_t,int> listHash ; |
---|
| 98 | for(int i=0 ; i<=commSize ; i++) |
---|
| 99 | if (listHash.count(hashAll[i])==0) |
---|
[1761] | 100 | { |
---|
[2238] | 101 | listHash[hashAll[i]]=color ; |
---|
[1761] | 102 | color=color+1 ; |
---|
| 103 | } |
---|
[2238] | 104 | color=listHash[hashClient] ; |
---|
[1761] | 105 | delete[] hashAll ; |
---|
| 106 | |
---|
| 107 | MPI_Comm_split(globalComm, color, commRank, &clientComm) ; |
---|
| 108 | } |
---|
[2335] | 109 | else |
---|
[1761] | 110 | { |
---|
[2335] | 111 | ERROR("void CClient::initialize(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm)", <<"OASIS usage is set. In these conditions, XIOS initialization needs the local_comm created by OASIS."<<endl) ; |
---|
[1761] | 112 | } |
---|
| 113 | } |
---|
| 114 | else // localComm is given |
---|
| 115 | { |
---|
| 116 | MPI_Comm_dup(localComm,&clientComm) ; |
---|
[2333] | 117 | MPI_Comm_dup(localComm,&intraComm_) ; |
---|
| 118 | |
---|
| 119 | if (CXios::usingServer) |
---|
| 120 | { |
---|
| 121 | MPI_Comm_rank(intraComm_,&rank_) ; |
---|
| 122 | } |
---|
| 123 | |
---|
[1761] | 124 | } |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | ///////////////////////////////////////// |
---|
| 128 | ///////////// PART 2 //////////////////// |
---|
| 129 | ///////////////////////////////////////// |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | // Create the XIOS communicator for every process which is related |
---|
| 133 | // to XIOS, as well on client side as on server side |
---|
| 134 | |
---|
| 135 | MPI_Comm xiosGlobalComm ; |
---|
| 136 | string strIds=CXios::getin<string>("clients_code_id","") ; |
---|
| 137 | vector<string> clientsCodeId=splitRegex(strIds,"\\s*,\\s*") ; |
---|
| 138 | if (strIds.empty()) |
---|
| 139 | { |
---|
| 140 | // no code Ids given, suppose XIOS initialisation is global |
---|
| 141 | int commRank, commGlobalRank, serverLeader, clientLeader,serverRemoteLeader,clientRemoteLeader ; |
---|
| 142 | MPI_Comm splitComm,interComm ; |
---|
| 143 | MPI_Comm_rank(globalComm,&commGlobalRank) ; |
---|
| 144 | MPI_Comm_split(globalComm, 0, commGlobalRank, &splitComm) ; |
---|
| 145 | int splitCommSize, globalCommSize ; |
---|
| 146 | |
---|
| 147 | MPI_Comm_size(splitComm,&splitCommSize) ; |
---|
| 148 | MPI_Comm_size(globalComm,&globalCommSize) ; |
---|
| 149 | if (splitCommSize==globalCommSize) // no server |
---|
| 150 | { |
---|
| 151 | MPI_Comm_dup(globalComm,&xiosGlobalComm) ; |
---|
| 152 | CXios::setXiosComm(xiosGlobalComm) ; |
---|
| 153 | } |
---|
| 154 | else |
---|
| 155 | { |
---|
| 156 | MPI_Comm_rank(splitComm,&commRank) ; |
---|
| 157 | if (commRank==0) clientLeader=commGlobalRank ; |
---|
| 158 | else clientLeader=0 ; |
---|
| 159 | serverLeader=0 ; |
---|
| 160 | MPI_Allreduce(&clientLeader,&clientRemoteLeader,1,MPI_INT,MPI_SUM,globalComm) ; |
---|
| 161 | MPI_Allreduce(&serverLeader,&serverRemoteLeader,1,MPI_INT,MPI_SUM,globalComm) ; |
---|
| 162 | MPI_Intercomm_create(splitComm, 0, globalComm, serverRemoteLeader,1341,&interComm) ; |
---|
| 163 | MPI_Intercomm_merge(interComm,true,&xiosGlobalComm) ; |
---|
| 164 | CXios::setXiosComm(xiosGlobalComm) ; |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | else |
---|
| 168 | { |
---|
| 169 | |
---|
| 170 | xiosGlobalCommByFileExchange(clientComm, codeId) ; |
---|
| 171 | |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | int commRank ; |
---|
| 175 | MPI_Comm_rank(CXios::getXiosComm(), &commRank) ; |
---|
| 176 | MPI_Comm_split(CXios::getXiosComm(),false,commRank, &clientsComm_) ; |
---|
| 177 | |
---|
| 178 | // is using server or not ? |
---|
| 179 | int xiosCommSize, clientsCommSize ; |
---|
| 180 | MPI_Comm_size(CXios::getXiosComm(), &xiosCommSize) ; |
---|
| 181 | MPI_Comm_size(clientsComm_, &clientsCommSize) ; |
---|
| 182 | if (xiosCommSize==clientsCommSize) CXios::setUsingServer() ; |
---|
| 183 | else CXios::setNotUsingServer() ; |
---|
| 184 | |
---|
| 185 | ///////////////////////////////////////// |
---|
| 186 | ///////////// PART 3 //////////////////// |
---|
| 187 | ///////////////////////////////////////// |
---|
| 188 | |
---|
| 189 | CXios::launchDaemonsManager(false) ; |
---|
| 190 | poolRessource_ = new CPoolRessource(clientComm, codeId) ; |
---|
| 191 | |
---|
| 192 | ///////////////////////////////////////// |
---|
| 193 | ///////////// PART 4 //////////////////// |
---|
| 194 | ///////////////////////////////////////// |
---|
| 195 | |
---|
| 196 | returnComm = clientComm ; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | |
---|
| 200 | void CClient::xiosGlobalCommByFileExchange(MPI_Comm clientComm, const string& codeId) |
---|
| 201 | { |
---|
| 202 | |
---|
| 203 | MPI_Comm globalComm=CXios::getGlobalComm() ; |
---|
| 204 | MPI_Comm xiosGlobalComm ; |
---|
| 205 | |
---|
| 206 | string strIds=CXios::getin<string>("clients_code_id","") ; |
---|
| 207 | vector<string> clientsCodeId=splitRegex(strIds,"\\s*,\\s*") ; |
---|
| 208 | |
---|
| 209 | int commRank, globalRank, clientRank, serverRank ; |
---|
| 210 | MPI_Comm_rank(clientComm, &commRank) ; |
---|
| 211 | MPI_Comm_rank(globalComm, &globalRank) ; |
---|
| 212 | string clientFileName("__xios_publisher::"+codeId+"__to_remove__") ; |
---|
| 213 | |
---|
| 214 | int error ; |
---|
| 215 | |
---|
| 216 | if (commRank==0) // if root process publish name |
---|
| 217 | { |
---|
| 218 | std::ofstream ofs (clientFileName, std::ofstream::out); |
---|
| 219 | ofs<<globalRank ; |
---|
| 220 | ofs.close(); |
---|
| 221 | |
---|
| 222 | // get server root rank |
---|
| 223 | |
---|
| 224 | std::ifstream ifs ; |
---|
| 225 | string fileName=("__xios_publisher::"+CXios::xiosCodeId+"__to_remove__") ; |
---|
| 226 | |
---|
| 227 | double timeout = CXios::getin<double>("server_puplish_timeout",serverPublishDefaultTimeout) ; |
---|
| 228 | double time ; |
---|
| 229 | |
---|
| 230 | do |
---|
| 231 | { |
---|
| 232 | CTimer::get("server_publish_timeout").resume() ; |
---|
| 233 | ifs.clear() ; |
---|
| 234 | ifs.open(fileName, std::ifstream::in) ; |
---|
| 235 | CTimer::get("server_publish_timeout").suspend() ; |
---|
| 236 | } while (ifs.fail() && CTimer::get("server_publish_timeout").getCumulatedTime()<timeout) ; |
---|
| 237 | |
---|
| 238 | if (CTimer::get("server_publish_timeout").getCumulatedTime()>=timeout || ifs.fail()) |
---|
| 239 | { |
---|
| 240 | ifs.clear() ; |
---|
| 241 | ifs.close() ; |
---|
| 242 | ifs.clear() ; |
---|
| 243 | error=true ; |
---|
| 244 | } |
---|
| 245 | else |
---|
| 246 | { |
---|
| 247 | ifs>>serverRank ; |
---|
| 248 | ifs.close() ; |
---|
| 249 | error=false ; |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | } |
---|
| 253 | MPI_Bcast(&error,1,MPI_INT,0,clientComm) ; |
---|
| 254 | |
---|
| 255 | if (error==false) // you have a server |
---|
| 256 | { |
---|
| 257 | MPI_Comm intraComm ; |
---|
| 258 | MPI_Comm_dup(clientComm,&intraComm) ; |
---|
| 259 | MPI_Comm interComm ; |
---|
| 260 | |
---|
| 261 | int pos=0 ; |
---|
| 262 | for(int i=0 ; codeId!=clientsCodeId[i]; i++) pos=pos+1 ; |
---|
| 263 | |
---|
| 264 | bool high=true ; |
---|
| 265 | for(int i=pos ; i<clientsCodeId.size(); i++) |
---|
| 266 | { |
---|
| 267 | MPI_Intercomm_create(intraComm, 0, globalComm, serverRank, 3141, &interComm); |
---|
| 268 | MPI_Comm_free(&intraComm) ; |
---|
| 269 | MPI_Intercomm_merge(interComm,high, &intraComm ) ; |
---|
| 270 | high=false ; |
---|
[2333] | 271 | if (i==pos) { |
---|
| 272 | interComm_=interComm ; |
---|
| 273 | } |
---|
[1761] | 274 | } |
---|
| 275 | xiosGlobalComm=intraComm ; |
---|
| 276 | } |
---|
| 277 | else // no server detected |
---|
| 278 | { |
---|
| 279 | vector<int> clientsRank(clientsCodeId.size()) ; |
---|
| 280 | |
---|
| 281 | if (commRank==0) |
---|
| 282 | { |
---|
| 283 | for(int i=0;i<clientsRank.size();i++) |
---|
| 284 | { |
---|
| 285 | std::ifstream ifs ; |
---|
| 286 | string fileName=("__xios_publisher::"+clientsCodeId[i]+"__to_remove__") ; |
---|
| 287 | do |
---|
| 288 | { |
---|
| 289 | ifs.clear() ; |
---|
| 290 | ifs.open(fileName, std::ifstream::in) ; |
---|
| 291 | } while (ifs.fail()) ; |
---|
| 292 | ifs>>clientsRank[i] ; |
---|
| 293 | ifs.close() ; |
---|
| 294 | } |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | int client ; |
---|
| 298 | MPI_Comm intraComm ; |
---|
| 299 | MPI_Comm_dup(clientComm,&intraComm) ; |
---|
| 300 | MPI_Comm interComm ; |
---|
| 301 | |
---|
| 302 | int pos=0 ; |
---|
| 303 | for(int i=0 ; codeId!=clientsCodeId[i]; i++) pos=pos+1 ; |
---|
| 304 | |
---|
| 305 | bool high=true ; |
---|
| 306 | for(int i=pos+1 ; i<clientsCodeId.size(); i++) |
---|
| 307 | { |
---|
| 308 | if (codeId==clientsCodeId[0]) // first model play the server rule |
---|
| 309 | { |
---|
| 310 | MPI_Intercomm_create(intraComm, 0, globalComm, clientsRank[i], 3141, &interComm); |
---|
| 311 | MPI_Intercomm_merge(interComm,false, &intraComm ) ; |
---|
| 312 | } |
---|
| 313 | else |
---|
| 314 | { |
---|
| 315 | MPI_Intercomm_create(intraComm, 0, globalComm, clientsRank[0], 3141, &interComm); |
---|
| 316 | MPI_Intercomm_merge(interComm,high, &intraComm ) ; |
---|
| 317 | high=false ; |
---|
| 318 | } |
---|
[2333] | 319 | if (i==pos) { |
---|
| 320 | interComm_=interComm ; // NOT TESTED ! |
---|
| 321 | } |
---|
[1761] | 322 | } |
---|
| 323 | xiosGlobalComm=intraComm ; |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | MPI_Barrier(xiosGlobalComm); |
---|
| 327 | if (commRank==0) std::remove(clientFileName.c_str()) ; |
---|
| 328 | MPI_Barrier(xiosGlobalComm); |
---|
| 329 | |
---|
| 330 | CXios::setXiosComm(xiosGlobalComm) ; |
---|
| 331 | |
---|
[1764] | 332 | MPI_Comm commUnfree ; |
---|
| 333 | MPI_Comm_dup(clientComm, &commUnfree ) ; |
---|
| 334 | |
---|
[1761] | 335 | } |
---|
| 336 | |
---|
[1765] | 337 | // to check on other architecture |
---|
[1761] | 338 | void CClient::xiosGlobalCommByPublishing(MPI_Comm clientComm, const string& codeId) |
---|
| 339 | { |
---|
| 340 | |
---|
| 341 | // untested. need to be developped an a true MPI compliant library |
---|
| 342 | |
---|
| 343 | /* |
---|
| 344 | // try to discover other client/server |
---|
| 345 | // do you have a xios server ? |
---|
| 346 | char portName[MPI_MAX_PORT_NAME]; |
---|
| 347 | int ierr ; |
---|
| 348 | int commRank ; |
---|
| 349 | MPI_Comm_rank(clientComm,&commRank) ; |
---|
| 350 | |
---|
| 351 | MPI_Barrier(globalComm) ; |
---|
| 352 | if (commRank==0) |
---|
| 353 | { |
---|
| 354 | |
---|
| 355 | MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN ); |
---|
| 356 | const char* serviceName=CXios::xiosCodeId.c_str() ; |
---|
| 357 | ierr=MPI_Lookup_name(CXios::xiosCodeId.c_str(), MPI_INFO_NULL, portName); |
---|
| 358 | MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL ); |
---|
| 359 | } |
---|
| 360 | ierr=MPI_SUCCESS ; |
---|
| 361 | MPI_Bcast(&ierr,1,MPI_INT,0,clientComm) ; |
---|
| 362 | |
---|
| 363 | if (ierr==MPI_SUCCESS) // you have a server |
---|
| 364 | { |
---|
| 365 | MPI_Comm intraComm=clientComm ; |
---|
| 366 | MPI_Comm interComm ; |
---|
| 367 | for(int i=0 ; i<clientsCodeId.size(); i++) |
---|
| 368 | { |
---|
| 369 | MPI_Comm_connect(portName, MPI_INFO_NULL, 0, intraComm, &interComm); |
---|
| 370 | MPI_Intercomm_merge(interComm, true, &intraComm ) ; |
---|
| 371 | } |
---|
| 372 | xiosGlobalComm=intraComm ; |
---|
| 373 | } |
---|
| 374 | else // you don't have any server |
---|
| 375 | { |
---|
| 376 | if (codeId==clientsCodeId[0]) // first code will publish his name |
---|
| 377 | { |
---|
| 378 | |
---|
| 379 | if (commRank==0) // if root process publish name |
---|
| 380 | { |
---|
| 381 | MPI_Open_port(MPI_INFO_NULL, portName); |
---|
| 382 | MPI_Publish_name(CXios::xiosCodeId.c_str(), MPI_INFO_NULL, portName); |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | MPI_Comm intraComm=clientComm ; |
---|
| 386 | MPI_Comm interComm ; |
---|
| 387 | for(int i=0 ; i<clientsCodeId.size()-1; i++) |
---|
| 388 | { |
---|
| 389 | MPI_Comm_accept(portName, MPI_INFO_NULL, 0, intraComm, &interComm); |
---|
| 390 | MPI_Intercomm_merge(interComm,false, &intraComm ) ; |
---|
| 391 | } |
---|
| 392 | } |
---|
| 393 | else // other clients are connecting to the first one |
---|
| 394 | { |
---|
| 395 | if (commRank==0) |
---|
| 396 | { |
---|
| 397 | |
---|
| 398 | MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN ); |
---|
| 399 | ierr=MPI_Lookup_name(CXios::xiosCodeId.c_str(), MPI_INFO_NULL, portName); |
---|
| 400 | MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL ); |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | MPI_Bcast(&ierr,1,MPI_INT,0,clientComm) ; |
---|
| 404 | |
---|
| 405 | if (ierr==MPI_SUCCESS) // you can connect |
---|
| 406 | { |
---|
| 407 | MPI_Comm intraComm=clientComm ; |
---|
| 408 | MPI_Comm interComm ; |
---|
| 409 | for(int i=0 ; i<clientsCodeId.size()-1; i++) |
---|
| 410 | { |
---|
| 411 | MPI_Comm_connect(portName, MPI_INFO_NULL, 0, intraComm, &interComm); |
---|
| 412 | MPI_Intercomm_merge(interComm, true, &intraComm ) ; |
---|
| 413 | } |
---|
| 414 | xiosGlobalComm=intraComm ; |
---|
| 415 | } |
---|
| 416 | } |
---|
| 417 | } |
---|
| 418 | */ |
---|
| 419 | } |
---|
| 420 | |
---|
[490] | 421 | |
---|
[1765] | 422 | ///--------------------------------------------------------------- |
---|
| 423 | /*! |
---|
| 424 | * \fn void CClient::registerContext(const string& id, MPI_Comm contextComm) |
---|
| 425 | * \brief Sends a request to create a context to server. Creates client/server contexts. |
---|
| 426 | * \param [in] id id of context. |
---|
| 427 | * \param [in] contextComm. |
---|
| 428 | * Function is only called by client. |
---|
| 429 | */ |
---|
[1761] | 430 | void CClient::registerContext(const string& id, MPI_Comm contextComm) |
---|
| 431 | { |
---|
| 432 | int commRank, commSize ; |
---|
| 433 | MPI_Comm_rank(contextComm,&commRank) ; |
---|
| 434 | MPI_Comm_size(contextComm,&commSize) ; |
---|
| 435 | |
---|
| 436 | getPoolRessource()->createService(contextComm, id, 0, CServicesManager::CLIENT, 1) ; |
---|
[2407] | 437 | getPoolRessource()->createService(contextComm, id+"_"+CXios::defaultWriterId, 0, CServicesManager::WRITER, 1) ; |
---|
| 438 | getPoolRessource()->createService(contextComm, id+"_"+CXios::defaultReaderId, 0, CServicesManager::READER, 1) ; |
---|
[1761] | 439 | |
---|
| 440 | if (commRank==0) while (!CXios::getServicesManager()->hasService(getPoolRessource()->getId(), id, 0)) { CXios::getDaemonsManager()->eventLoop();} |
---|
| 441 | |
---|
| 442 | if (commRank==0) CXios::getContextsManager()->createServerContext(getPoolRessource()->getId(), id, 0, id) ; |
---|
| 443 | int type=CServicesManager::CLIENT ; |
---|
| 444 | string name = CXios::getContextsManager()->getServerContextName(getPoolRessource()->getId(), id, 0, type, id) ; |
---|
[2238] | 445 | double time ; |
---|
| 446 | double lastTime=0 ; |
---|
[2260] | 447 | double latency=0 ; |
---|
[2238] | 448 | bool out=false ; |
---|
| 449 | while (!out) |
---|
[1761] | 450 | { |
---|
[2238] | 451 | time=MPI_Wtime() ; |
---|
| 452 | if (time-lastTime > latency) |
---|
| 453 | { |
---|
| 454 | out=CXios::getContextsManager()->hasContext(name, contextComm); |
---|
| 455 | lastTime=time ; |
---|
| 456 | } |
---|
| 457 | if (!out) CXios::getDaemonsManager()->eventLoop() ; |
---|
[1761] | 458 | } |
---|
| 459 | |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | |
---|
[490] | 463 | |
---|
[1587] | 464 | /*! |
---|
| 465 | * \fn void CClient::callOasisEnddef(void) |
---|
| 466 | * \brief Send the order to the servers to call "oasis_enddef". It must be done by each compound of models before calling oasis_enddef on client side |
---|
| 467 | * Function is only called by client. |
---|
| 468 | */ |
---|
| 469 | void CClient::callOasisEnddef(void) |
---|
| 470 | { |
---|
| 471 | bool oasisEnddef=CXios::getin<bool>("call_oasis_enddef",true) ; |
---|
| 472 | if (!oasisEnddef) ERROR("void CClient::callOasisEnddef(void)", <<"Function xios_oasis_enddef called but variable <call_oasis_enddef> is set to false."<<endl |
---|
| 473 | <<"Variable <call_oasis_enddef> must be set to true"<<endl) ; |
---|
[2333] | 474 | if (!CXios::isClient) // != isServer (change recently ) |
---|
[1587] | 475 | // Attached mode |
---|
| 476 | { |
---|
| 477 | // nothing to do |
---|
| 478 | } |
---|
| 479 | else |
---|
| 480 | { |
---|
| 481 | int rank ; |
---|
| 482 | int msg=0 ; |
---|
| 483 | |
---|
[2332] | 484 | MPI_Comm_rank(intraComm_,&rank) ; |
---|
[1587] | 485 | if (rank==0) |
---|
| 486 | { |
---|
[2332] | 487 | MPI_Send(&msg,1,MPI_INT,0,5,interComm_) ; // tags oasis_endded = 5 |
---|
[1587] | 488 | } |
---|
| 489 | |
---|
| 490 | } |
---|
| 491 | } |
---|
| 492 | |
---|
[300] | 493 | void CClient::finalize(void) |
---|
| 494 | { |
---|
[1761] | 495 | |
---|
| 496 | MPI_Barrier(clientsComm_) ; |
---|
| 497 | int commRank ; |
---|
| 498 | MPI_Comm_rank(clientsComm_, &commRank) ; |
---|
| 499 | if (commRank==0) CXios::getRessourcesManager()->finalize() ; |
---|
| 500 | |
---|
| 501 | CTimer::get("XIOS init/finalize",false).suspend() ; |
---|
| 502 | CTimer::get("XIOS").suspend() ; |
---|
[2266] | 503 | CXios::finalizeDaemonsManager() ; |
---|
[2274] | 504 | finalizePoolRessource() ; |
---|
[2265] | 505 | CContext::removeAllContexts() ; // free memory for related context |
---|
[1764] | 506 | |
---|
[2310] | 507 | CXios::getMpiGarbageCollector().release() ; // release unfree MPI ressources |
---|
[1761] | 508 | if (!is_MPI_Initialized) |
---|
| 509 | { |
---|
[2335] | 510 | if (!CXios::usingOasis) MPI_Finalize() ; |
---|
[1761] | 511 | } |
---|
| 512 | |
---|
| 513 | info(20) << "Client side context is finalized"<<endl ; |
---|
| 514 | report(0) <<" Performance report : Whole time from XIOS init and finalize: "<< CTimer::get("XIOS init/finalize").getCumulatedTime()<<" s"<<endl ; |
---|
| 515 | report(0) <<" Performance report : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ; |
---|
| 516 | report(0)<< " Performance report : time spent for waiting free buffer : "<< CTimer::get("Blocking time").getCumulatedTime()<<" s"<<endl ; |
---|
| 517 | report(0)<< " Performance report : Ratio : "<< CTimer::get("Blocking time").getCumulatedTime()/CTimer::get("XIOS init/finalize").getCumulatedTime()*100.<<" %"<<endl ; |
---|
| 518 | report(0)<< " Performance report : This ratio must be close to zero. Otherwise it may be usefull to increase buffer size or numbers of server"<<endl ; |
---|
| 519 | // report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ; |
---|
| 520 | report(0)<< " Memory report : Minimum buffer size required : " << CClientBuffer::maxRequestSize << " bytes" << endl ; |
---|
| 521 | report(0)<< " Memory report : increasing it by a factor will increase performance, depending of the volume of data wrote in file at each time step of the file"<<endl ; |
---|
| 522 | report(100)<<CTimer::getAllCumulatedTime()<<endl ; |
---|
[2418] | 523 | report(100)<<CMemChecker::getAllCumulatedMem()<<endl ; |
---|
[2146] | 524 | CWorkflowGraph::drawWorkFlowGraph_client(); |
---|
[2274] | 525 | |
---|
| 526 | xios::releaseStaticAllocation() ; |
---|
| 527 | |
---|
[1761] | 528 | } |
---|
| 529 | |
---|
[2274] | 530 | void CClient::finalizePoolRessource() |
---|
| 531 | { |
---|
| 532 | delete poolRessource_ ; poolRessource_=nullptr ; |
---|
| 533 | } |
---|
[1761] | 534 | |
---|
[1148] | 535 | /*! |
---|
[1243] | 536 | * Return global rank without oasis and current rank in model intraComm in case of oasis |
---|
[1148] | 537 | */ |
---|
[490] | 538 | int CClient::getRank() |
---|
| 539 | { |
---|
[1148] | 540 | return rank_; |
---|
[490] | 541 | } |
---|
| 542 | |
---|
[523] | 543 | /*! |
---|
| 544 | * Open a file specified by a suffix and an extension and use it for the given file buffer. |
---|
| 545 | * The file name will be suffix+rank+extension. |
---|
| 546 | * |
---|
| 547 | * \param fileName[in] protype file name |
---|
| 548 | * \param ext [in] extension of the file |
---|
| 549 | * \param fb [in/out] the file buffer |
---|
| 550 | */ |
---|
| 551 | void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb) |
---|
| 552 | { |
---|
| 553 | StdStringStream fileNameClient; |
---|
| 554 | int numDigit = 0; |
---|
| 555 | int size = 0; |
---|
[1233] | 556 | int rank; |
---|
[1761] | 557 | MPI_Comm_size(CXios::getGlobalComm(), &size); |
---|
| 558 | MPI_Comm_rank(CXios::getGlobalComm(),&rank); |
---|
[523] | 559 | while (size) |
---|
| 560 | { |
---|
| 561 | size /= 10; |
---|
| 562 | ++numDigit; |
---|
| 563 | } |
---|
[497] | 564 | |
---|
[1761] | 565 | fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << rank << ext; |
---|
[1233] | 566 | |
---|
[523] | 567 | fb->open(fileNameClient.str().c_str(), std::ios::out); |
---|
| 568 | if (!fb->is_open()) |
---|
| 569 | ERROR("void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)", |
---|
[1542] | 570 | << std::endl << "Can not open <" << fileNameClient.str() << "> file to write the client log(s)."); |
---|
[523] | 571 | } |
---|
[490] | 572 | |
---|
[523] | 573 | /*! |
---|
| 574 | * \brief Open a file stream to write the info logs |
---|
| 575 | * Open a file stream with a specific file name suffix+rank |
---|
| 576 | * to write the info logs. |
---|
| 577 | * \param fileName [in] protype file name |
---|
| 578 | */ |
---|
| 579 | void CClient::openInfoStream(const StdString& fileName) |
---|
| 580 | { |
---|
| 581 | std::filebuf* fb = m_infoStream.rdbuf(); |
---|
| 582 | openStream(fileName, ".out", fb); |
---|
[490] | 583 | |
---|
[523] | 584 | info.write2File(fb); |
---|
| 585 | report.write2File(fb); |
---|
| 586 | } |
---|
[490] | 587 | |
---|
[523] | 588 | //! Write the info logs to standard output |
---|
| 589 | void CClient::openInfoStream() |
---|
| 590 | { |
---|
| 591 | info.write2StdOut(); |
---|
| 592 | report.write2StdOut(); |
---|
| 593 | } |
---|
[490] | 594 | |
---|
[523] | 595 | //! Close the info logs file if it opens |
---|
| 596 | void CClient::closeInfoStream() |
---|
| 597 | { |
---|
| 598 | if (m_infoStream.is_open()) m_infoStream.close(); |
---|
| 599 | } |
---|
[490] | 600 | |
---|
[523] | 601 | /*! |
---|
| 602 | * \brief Open a file stream to write the error log |
---|
| 603 | * Open a file stream with a specific file name suffix+rank |
---|
| 604 | * to write the error log. |
---|
| 605 | * \param fileName [in] protype file name |
---|
| 606 | */ |
---|
| 607 | void CClient::openErrorStream(const StdString& fileName) |
---|
| 608 | { |
---|
| 609 | std::filebuf* fb = m_errorStream.rdbuf(); |
---|
| 610 | openStream(fileName, ".err", fb); |
---|
| 611 | |
---|
| 612 | error.write2File(fb); |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | //! Write the error log to standard error output |
---|
| 616 | void CClient::openErrorStream() |
---|
| 617 | { |
---|
| 618 | error.write2StdErr(); |
---|
| 619 | } |
---|
| 620 | |
---|
| 621 | //! Close the error log file if it opens |
---|
| 622 | void CClient::closeErrorStream() |
---|
| 623 | { |
---|
| 624 | if (m_errorStream.is_open()) m_errorStream.close(); |
---|
| 625 | } |
---|
[300] | 626 | } |
---|