[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" |
---|
[1646] | 14 | #ifdef _usingEP |
---|
[1601] | 15 | using namespace ep_lib; |
---|
[1646] | 16 | #endif |
---|
[300] | 17 | |
---|
[335] | 18 | namespace xios |
---|
[490] | 19 | { |
---|
[300] | 20 | |
---|
| 21 | MPI_Comm CClient::intraComm ; |
---|
| 22 | MPI_Comm CClient::interComm ; |
---|
[1601] | 23 | std::list<MPI_Comm> *CClient::contextInterComms_ptr = 0; |
---|
[1158] | 24 | int CClient::serverLeader ; |
---|
[300] | 25 | bool CClient::is_MPI_Initialized ; |
---|
[1148] | 26 | int CClient::rank_ = INVALID_RANK; |
---|
[490] | 27 | StdOFStream CClient::m_infoStream; |
---|
[523] | 28 | StdOFStream CClient::m_errorStream; |
---|
[1601] | 29 | |
---|
| 30 | StdOFStream CClient::array_infoStream[16]; |
---|
| 31 | |
---|
[992] | 32 | MPI_Comm& CClient::getInterComm(void) { return (interComm); } |
---|
[1601] | 33 | |
---|
[983] | 34 | ///--------------------------------------------------------------- |
---|
| 35 | /*! |
---|
| 36 | * \fn void CClient::initialize(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm) |
---|
| 37 | * Function creates intraComm (CClient::intraComm) for client group with id=codeId and interComm (CClient::interComm) between client and server groups. |
---|
| 38 | * \param [in] codeId identity of context. |
---|
| 39 | * \param [in/out] localComm local communicator. |
---|
| 40 | * \param [in/out] returnComm (intra)communicator of client group. |
---|
| 41 | */ |
---|
| 42 | |
---|
| 43 | void CClient::initialize(const string& codeId, MPI_Comm& localComm, MPI_Comm& returnComm) |
---|
[300] | 44 | { |
---|
| 45 | int initialized ; |
---|
| 46 | MPI_Initialized(&initialized) ; |
---|
| 47 | if (initialized) is_MPI_Initialized=true ; |
---|
| 48 | else is_MPI_Initialized=false ; |
---|
[1152] | 49 | int rank ; |
---|
[490] | 50 | |
---|
[300] | 51 | // don't use OASIS |
---|
| 52 | if (!CXios::usingOasis) |
---|
| 53 | { |
---|
[983] | 54 | // localComm isn't given |
---|
[300] | 55 | if (localComm == MPI_COMM_NULL) |
---|
| 56 | { |
---|
[490] | 57 | if (!is_MPI_Initialized) |
---|
[300] | 58 | { |
---|
[925] | 59 | MPI_Init(NULL, NULL); |
---|
[300] | 60 | } |
---|
[359] | 61 | CTimer::get("XIOS").resume() ; |
---|
[1158] | 62 | CTimer::get("XIOS init/finalize").resume() ; |
---|
[490] | 63 | boost::hash<string> hashString ; |
---|
| 64 | |
---|
[1158] | 65 | unsigned long hashClient=hashString(codeId) ; |
---|
| 66 | unsigned long hashServer=hashString(CXios::xiosCodeId) ; |
---|
[300] | 67 | unsigned long* hashAll ; |
---|
| 68 | int size ; |
---|
| 69 | int myColor ; |
---|
| 70 | int i,c ; |
---|
| 71 | MPI_Comm newComm ; |
---|
[490] | 72 | |
---|
[300] | 73 | MPI_Comm_size(CXios::globalComm,&size) ; |
---|
[1148] | 74 | MPI_Comm_rank(CXios::globalComm,&rank_); |
---|
[490] | 75 | |
---|
[300] | 76 | hashAll=new unsigned long[size] ; |
---|
[490] | 77 | |
---|
[1158] | 78 | MPI_Allgather(&hashClient,1,MPI_LONG,hashAll,1,MPI_LONG,CXios::globalComm) ; |
---|
[300] | 79 | |
---|
| 80 | map<unsigned long, int> colors ; |
---|
| 81 | map<unsigned long, int> leaders ; |
---|
[490] | 82 | |
---|
[300] | 83 | for(i=0,c=0;i<size;i++) |
---|
| 84 | { |
---|
| 85 | if (colors.find(hashAll[i])==colors.end()) |
---|
| 86 | { |
---|
| 87 | colors[hashAll[i]] =c ; |
---|
| 88 | leaders[hashAll[i]]=i ; |
---|
| 89 | c++ ; |
---|
| 90 | } |
---|
| 91 | } |
---|
[490] | 92 | |
---|
[491] | 93 | // Verify whether we are on server mode or not |
---|
| 94 | CXios::setNotUsingServer(); |
---|
| 95 | for (i=0; i < size; ++i) |
---|
| 96 | { |
---|
[1158] | 97 | if (hashServer == hashAll[i]) |
---|
[491] | 98 | { |
---|
| 99 | CXios::setUsingServer(); |
---|
| 100 | break; |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
[1243] | 104 | myColor=colors[hashClient]; |
---|
[1148] | 105 | MPI_Comm_split(CXios::globalComm,myColor,rank_,&intraComm) ; |
---|
[300] | 106 | |
---|
| 107 | if (CXios::usingServer) |
---|
[490] | 108 | { |
---|
[300] | 109 | int clientLeader=leaders[hashClient] ; |
---|
[1148] | 110 | serverLeader=leaders[hashServer] ; |
---|
[493] | 111 | int intraCommSize, intraCommRank ; |
---|
| 112 | MPI_Comm_size(intraComm,&intraCommSize) ; |
---|
| 113 | MPI_Comm_rank(intraComm,&intraCommRank) ; |
---|
[1601] | 114 | |
---|
| 115 | MPI_Intercomm_create(intraComm, 0, CXios::globalComm, serverLeader, 0, &interComm) ; |
---|
| 116 | #pragma omp critical (_output) |
---|
| 117 | { |
---|
| 118 | info(50)<<"intercommCreate::client "<<rank_<<" intraCommSize : "<<intraCommSize |
---|
[1148] | 119 | <<" intraCommRank :"<<intraCommRank<<" clientLeader "<< serverLeader<<endl ; |
---|
[1601] | 120 | } |
---|
[300] | 121 | } |
---|
| 122 | else |
---|
| 123 | { |
---|
| 124 | MPI_Comm_dup(intraComm,&interComm) ; |
---|
| 125 | } |
---|
| 126 | delete [] hashAll ; |
---|
| 127 | } |
---|
| 128 | // localComm argument is given |
---|
[490] | 129 | else |
---|
[300] | 130 | { |
---|
| 131 | if (CXios::usingServer) |
---|
[490] | 132 | { |
---|
[300] | 133 | //ERROR("void CClient::initialize(const string& codeId,MPI_Comm& localComm,MPI_Comm& returnComm)", << " giving a local communictor is not compatible with using server mode") ; |
---|
| 134 | } |
---|
| 135 | else |
---|
| 136 | { |
---|
| 137 | MPI_Comm_dup(localComm,&intraComm) ; |
---|
| 138 | MPI_Comm_dup(intraComm,&interComm) ; |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | // using OASIS |
---|
| 143 | else |
---|
| 144 | { |
---|
[983] | 145 | // localComm isn't given |
---|
[300] | 146 | if (localComm == MPI_COMM_NULL) |
---|
| 147 | { |
---|
| 148 | if (!is_MPI_Initialized) oasis_init(codeId) ; |
---|
[655] | 149 | oasis_get_localcomm(localComm) ; |
---|
[300] | 150 | } |
---|
[655] | 151 | MPI_Comm_dup(localComm,&intraComm) ; |
---|
| 152 | |
---|
[359] | 153 | CTimer::get("XIOS").resume() ; |
---|
[1158] | 154 | CTimer::get("XIOS init/finalize").resume() ; |
---|
[511] | 155 | |
---|
| 156 | if (CXios::usingServer) |
---|
[300] | 157 | { |
---|
| 158 | MPI_Status status ; |
---|
[1231] | 159 | MPI_Comm_rank(intraComm,&rank_) ; |
---|
[506] | 160 | |
---|
[300] | 161 | oasis_get_intercomm(interComm,CXios::xiosCodeId) ; |
---|
[1231] | 162 | if (rank_==0) MPI_Recv(&serverLeader,1, MPI_INT, 0, 0, interComm, &status) ; |
---|
[300] | 163 | MPI_Bcast(&serverLeader,1,MPI_INT,0,intraComm) ; |
---|
| 164 | } |
---|
| 165 | else MPI_Comm_dup(intraComm,&interComm) ; |
---|
| 166 | } |
---|
[490] | 167 | |
---|
[300] | 168 | MPI_Comm_dup(intraComm,&returnComm) ; |
---|
| 169 | } |
---|
[490] | 170 | |
---|
[983] | 171 | ///--------------------------------------------------------------- |
---|
| 172 | /*! |
---|
| 173 | * \fn void CClient::registerContext(const string& id, MPI_Comm contextComm) |
---|
[1021] | 174 | * \brief Sends a request to create a context to server. Creates client/server contexts. |
---|
[983] | 175 | * \param [in] id id of context. |
---|
| 176 | * \param [in] contextComm. |
---|
[1021] | 177 | * Function is only called by client. |
---|
[983] | 178 | */ |
---|
| 179 | void CClient::registerContext(const string& id, MPI_Comm contextComm) |
---|
[300] | 180 | { |
---|
[346] | 181 | CContext::setCurrent(id) ; |
---|
[511] | 182 | CContext* context=CContext::create(id); |
---|
| 183 | StdString idServer(id); |
---|
| 184 | idServer += "_server"; |
---|
[490] | 185 | |
---|
[983] | 186 | if (CXios::isServer && !context->hasServer) |
---|
| 187 | // Attached mode |
---|
[300] | 188 | { |
---|
[983] | 189 | MPI_Comm contextInterComm ; |
---|
| 190 | MPI_Comm_dup(contextComm,&contextInterComm) ; |
---|
| 191 | CContext* contextServer = CContext::create(idServer); |
---|
| 192 | |
---|
| 193 | // Firstly, initialize context on client side |
---|
| 194 | context->initClient(contextComm,contextInterComm, contextServer); |
---|
| 195 | |
---|
| 196 | // Secondly, initialize context on server side |
---|
| 197 | contextServer->initServer(contextComm,contextInterComm, context); |
---|
| 198 | |
---|
| 199 | // Finally, we should return current context to context client |
---|
| 200 | CContext::setCurrent(id); |
---|
| 201 | |
---|
[1601] | 202 | if(contextInterComms_ptr == NULL) contextInterComms_ptr = new std::list<MPI_Comm>; |
---|
| 203 | contextInterComms_ptr->push_back(contextInterComm); |
---|
[983] | 204 | } |
---|
| 205 | else |
---|
| 206 | { |
---|
[300] | 207 | int size,rank,globalRank ; |
---|
| 208 | size_t message_size ; |
---|
| 209 | int leaderRank ; |
---|
| 210 | MPI_Comm contextInterComm ; |
---|
[490] | 211 | |
---|
[300] | 212 | MPI_Comm_size(contextComm,&size) ; |
---|
| 213 | MPI_Comm_rank(contextComm,&rank) ; |
---|
| 214 | MPI_Comm_rank(CXios::globalComm,&globalRank) ; |
---|
| 215 | if (rank!=0) globalRank=0 ; |
---|
[490] | 216 | |
---|
[300] | 217 | CMessage msg ; |
---|
[511] | 218 | msg<<idServer<<size<<globalRank ; |
---|
| 219 | // msg<<id<<size<<globalRank ; |
---|
[300] | 220 | |
---|
| 221 | int messageSize=msg.size() ; |
---|
[1158] | 222 | char * buff = new char[messageSize] ; |
---|
| 223 | CBufferOut buffer((void*)buff,messageSize) ; |
---|
[300] | 224 | buffer<<msg ; |
---|
[490] | 225 | |
---|
[1158] | 226 | MPI_Send((void*)buff,buffer.count(),MPI_CHAR,serverLeader,1,CXios::globalComm) ; |
---|
| 227 | |
---|
| 228 | MPI_Intercomm_create(contextComm,0,CXios::globalComm,serverLeader,10+globalRank,&contextInterComm) ; |
---|
[1601] | 229 | #pragma omp critical (_output) |
---|
[1646] | 230 | { |
---|
| 231 | info(10)<<"Register new Context : "<<id<<endl ; |
---|
| 232 | } |
---|
[1148] | 233 | MPI_Comm inter ; |
---|
| 234 | MPI_Intercomm_merge(contextInterComm,0,&inter) ; |
---|
| 235 | MPI_Barrier(inter) ; |
---|
[490] | 236 | |
---|
[1148] | 237 | context->initClient(contextComm,contextInterComm) ; |
---|
[490] | 238 | |
---|
[1601] | 239 | if(contextInterComms_ptr == NULL) contextInterComms_ptr = new std::list<MPI_Comm>; |
---|
| 240 | contextInterComms_ptr->push_back(contextInterComm); |
---|
| 241 | |
---|
[1148] | 242 | MPI_Comm_free(&inter); |
---|
[1009] | 243 | delete [] buff ; |
---|
[300] | 244 | |
---|
| 245 | } |
---|
| 246 | } |
---|
[490] | 247 | |
---|
[1587] | 248 | /*! |
---|
| 249 | * \fn void CClient::callOasisEnddef(void) |
---|
| 250 | * \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 |
---|
| 251 | * Function is only called by client. |
---|
| 252 | */ |
---|
| 253 | void CClient::callOasisEnddef(void) |
---|
| 254 | { |
---|
| 255 | bool oasisEnddef=CXios::getin<bool>("call_oasis_enddef",true) ; |
---|
| 256 | if (!oasisEnddef) ERROR("void CClient::callOasisEnddef(void)", <<"Function xios_oasis_enddef called but variable <call_oasis_enddef> is set to false."<<endl |
---|
| 257 | <<"Variable <call_oasis_enddef> must be set to true"<<endl) ; |
---|
| 258 | if (CXios::isServer) |
---|
| 259 | // Attached mode |
---|
| 260 | { |
---|
| 261 | // nothing to do |
---|
| 262 | } |
---|
| 263 | else |
---|
| 264 | { |
---|
| 265 | int rank ; |
---|
| 266 | int msg=0 ; |
---|
| 267 | |
---|
| 268 | MPI_Comm_rank(intraComm,&rank) ; |
---|
| 269 | if (rank==0) |
---|
| 270 | { |
---|
| 271 | MPI_Send(&msg,1,MPI_INT,0,5,interComm) ; // tags oasis_endded = 5 |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | |
---|
[300] | 278 | void CClient::finalize(void) |
---|
| 279 | { |
---|
| 280 | int rank ; |
---|
| 281 | int msg=0 ; |
---|
[697] | 282 | |
---|
| 283 | MPI_Comm_rank(intraComm,&rank) ; |
---|
| 284 | |
---|
[992] | 285 | if (!CXios::isServer) |
---|
[300] | 286 | { |
---|
[490] | 287 | MPI_Comm_rank(intraComm,&rank) ; |
---|
| 288 | if (rank==0) |
---|
[332] | 289 | { |
---|
| 290 | MPI_Send(&msg,1,MPI_INT,0,0,interComm) ; |
---|
| 291 | } |
---|
[300] | 292 | } |
---|
[490] | 293 | |
---|
[1601] | 294 | for (std::list<MPI_Comm>::iterator it = contextInterComms_ptr->begin(); it != contextInterComms_ptr->end(); it++) |
---|
[655] | 295 | MPI_Comm_free(&(*it)); |
---|
| 296 | MPI_Comm_free(&interComm); |
---|
| 297 | MPI_Comm_free(&intraComm); |
---|
[361] | 298 | |
---|
[1158] | 299 | CTimer::get("XIOS init/finalize").suspend() ; |
---|
[655] | 300 | CTimer::get("XIOS").suspend() ; |
---|
| 301 | |
---|
[300] | 302 | if (!is_MPI_Initialized) |
---|
| 303 | { |
---|
[1601] | 304 | //if (CXios::usingOasis) oasis_finalize(); |
---|
| 305 | //else |
---|
| 306 | MPI_Finalize() ; |
---|
[300] | 307 | } |
---|
[1601] | 308 | #pragma omp critical (_output) |
---|
[1646] | 309 | { |
---|
| 310 | info(20) << "Client side context is finalized"<<endl ; |
---|
| 311 | } |
---|
[1601] | 312 | |
---|
| 313 | #pragma omp critical (_output) |
---|
| 314 | { |
---|
| 315 | report(0) <<" Performance report : Whole time from XIOS init and finalize: "<< CTimer::get("XIOS init/finalize").getCumulatedTime()<<" s"<<endl ; |
---|
| 316 | report(0) <<" Performance report : total time spent for XIOS : "<< CTimer::get("XIOS").getCumulatedTime()<<" s"<<endl ; |
---|
| 317 | report(0)<< " Performance report : time spent for waiting free buffer : "<< CTimer::get("Blocking time").getCumulatedTime()<<" s"<<endl ; |
---|
| 318 | report(0)<< " Performance report : Ratio : "<< CTimer::get("Blocking time").getCumulatedTime()/CTimer::get("XIOS init/finalize").getCumulatedTime()*100.<<" %"<<endl ; |
---|
| 319 | 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 ; |
---|
[511] | 320 | // report(0)<< " Memory report : Current buffer_size : "<<CXios::bufferSize<<endl ; |
---|
[1601] | 321 | report(0)<< " Memory report : Minimum buffer size required : " << CClientBuffer::maxRequestSize << " bytes" << endl ; |
---|
| 322 | 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 ; |
---|
[1628] | 323 | report(0)<<CTimer::getAllCumulatedTime()<<endl ; |
---|
[1601] | 324 | } |
---|
[400] | 325 | } |
---|
[490] | 326 | |
---|
[1148] | 327 | /*! |
---|
[1243] | 328 | * Return global rank without oasis and current rank in model intraComm in case of oasis |
---|
[1148] | 329 | */ |
---|
[490] | 330 | int CClient::getRank() |
---|
| 331 | { |
---|
[1148] | 332 | return rank_; |
---|
[490] | 333 | } |
---|
| 334 | |
---|
[523] | 335 | /*! |
---|
| 336 | * Open a file specified by a suffix and an extension and use it for the given file buffer. |
---|
| 337 | * The file name will be suffix+rank+extension. |
---|
| 338 | * |
---|
| 339 | * \param fileName[in] protype file name |
---|
| 340 | * \param ext [in] extension of the file |
---|
| 341 | * \param fb [in/out] the file buffer |
---|
| 342 | */ |
---|
| 343 | void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb) |
---|
| 344 | { |
---|
| 345 | StdStringStream fileNameClient; |
---|
| 346 | int numDigit = 0; |
---|
| 347 | int size = 0; |
---|
[1233] | 348 | int rank; |
---|
[523] | 349 | MPI_Comm_size(CXios::globalComm, &size); |
---|
| 350 | while (size) |
---|
| 351 | { |
---|
| 352 | size /= 10; |
---|
| 353 | ++numDigit; |
---|
| 354 | } |
---|
[497] | 355 | |
---|
[1233] | 356 | if (CXios::usingOasis) |
---|
| 357 | { |
---|
| 358 | MPI_Comm_rank(CXios::globalComm,&rank); |
---|
| 359 | fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << rank << ext; |
---|
| 360 | } |
---|
| 361 | else |
---|
| 362 | fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << getRank() << ext; |
---|
| 363 | |
---|
| 364 | |
---|
[523] | 365 | fb->open(fileNameClient.str().c_str(), std::ios::out); |
---|
| 366 | if (!fb->is_open()) |
---|
| 367 | ERROR("void CClient::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)", |
---|
[1542] | 368 | << std::endl << "Can not open <" << fileNameClient.str() << "> file to write the client log(s)."); |
---|
[523] | 369 | } |
---|
[490] | 370 | |
---|
[523] | 371 | /*! |
---|
| 372 | * \brief Open a file stream to write the info logs |
---|
| 373 | * Open a file stream with a specific file name suffix+rank |
---|
| 374 | * to write the info logs. |
---|
| 375 | * \param fileName [in] protype file name |
---|
| 376 | */ |
---|
| 377 | void CClient::openInfoStream(const StdString& fileName) |
---|
| 378 | { |
---|
[1601] | 379 | info_FB[omp_get_thread_num()] = array_infoStream[omp_get_thread_num()].rdbuf(); |
---|
| 380 | |
---|
| 381 | openStream(fileName, ".out", info_FB[omp_get_thread_num()]); |
---|
[490] | 382 | |
---|
[1601] | 383 | info.write2File(info_FB[omp_get_thread_num()]); |
---|
| 384 | report.write2File(info_FB[omp_get_thread_num()]); |
---|
[523] | 385 | } |
---|
[490] | 386 | |
---|
[523] | 387 | //! Write the info logs to standard output |
---|
| 388 | void CClient::openInfoStream() |
---|
| 389 | { |
---|
| 390 | info.write2StdOut(); |
---|
| 391 | report.write2StdOut(); |
---|
| 392 | } |
---|
[490] | 393 | |
---|
[523] | 394 | //! Close the info logs file if it opens |
---|
| 395 | void CClient::closeInfoStream() |
---|
| 396 | { |
---|
| 397 | if (m_infoStream.is_open()) m_infoStream.close(); |
---|
| 398 | } |
---|
[490] | 399 | |
---|
[523] | 400 | /*! |
---|
| 401 | * \brief Open a file stream to write the error log |
---|
| 402 | * Open a file stream with a specific file name suffix+rank |
---|
| 403 | * to write the error log. |
---|
| 404 | * \param fileName [in] protype file name |
---|
| 405 | */ |
---|
| 406 | void CClient::openErrorStream(const StdString& fileName) |
---|
| 407 | { |
---|
| 408 | std::filebuf* fb = m_errorStream.rdbuf(); |
---|
| 409 | openStream(fileName, ".err", fb); |
---|
| 410 | |
---|
| 411 | error.write2File(fb); |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | //! Write the error log to standard error output |
---|
| 415 | void CClient::openErrorStream() |
---|
| 416 | { |
---|
| 417 | error.write2StdErr(); |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | //! Close the error log file if it opens |
---|
| 421 | void CClient::closeErrorStream() |
---|
| 422 | { |
---|
| 423 | if (m_errorStream.is_open()) m_errorStream.close(); |
---|
| 424 | } |
---|
[300] | 425 | } |
---|