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