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