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