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