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