[490] | 1 | #include "globalScopeData.hpp" |
---|
[300] | 2 | #include "xmlioserver_spl.hpp" |
---|
| 3 | #include "cxios.hpp" |
---|
[342] | 4 | #include "server.hpp" |
---|
[300] | 5 | #include "type.hpp" |
---|
| 6 | #include "context.hpp" |
---|
[352] | 7 | #include "object_template.hpp" |
---|
[300] | 8 | #include "oasis_cinterface.hpp" |
---|
| 9 | #include <boost/functional/hash.hpp> |
---|
| 10 | #include <boost/algorithm/string.hpp> |
---|
[382] | 11 | #include "mpi.hpp" |
---|
[347] | 12 | #include "tracer.hpp" |
---|
| 13 | #include "timer.hpp" |
---|
[492] | 14 | #include "event_scheduler.hpp" |
---|
[300] | 15 | |
---|
[335] | 16 | namespace xios |
---|
[490] | 17 | { |
---|
[300] | 18 | MPI_Comm CServer::intraComm ; |
---|
| 19 | list<MPI_Comm> CServer::interComm ; |
---|
| 20 | bool CServer::isRoot ; |
---|
[490] | 21 | int CServer::rank = INVALID_RANK; |
---|
| 22 | StdOFStream CServer::m_infoStream; |
---|
| 23 | map<string,CContext*> CServer::contextList ; |
---|
[300] | 24 | bool CServer::finished=false ; |
---|
| 25 | bool CServer::is_MPI_Initialized ; |
---|
[492] | 26 | CEventScheduler* CServer::eventScheduler ; |
---|
[490] | 27 | |
---|
[300] | 28 | void CServer::initialize(void) |
---|
| 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 | // Not using OASIS |
---|
| 36 | if (!CXios::usingOasis) |
---|
| 37 | { |
---|
[490] | 38 | |
---|
| 39 | if (!is_MPI_Initialized) |
---|
[300] | 40 | { |
---|
| 41 | int argc=0; |
---|
| 42 | char** argv=NULL; |
---|
| 43 | MPI_Init(&argc,&argv) ; |
---|
| 44 | } |
---|
[359] | 45 | CTimer::get("XIOS").resume() ; |
---|
[490] | 46 | |
---|
| 47 | boost::hash<string> hashString ; |
---|
| 48 | |
---|
[300] | 49 | unsigned long hashServer=hashString(CXios::xiosCodeId) ; |
---|
| 50 | unsigned long* hashAll ; |
---|
[490] | 51 | |
---|
| 52 | // int rank ; |
---|
[300] | 53 | int size ; |
---|
| 54 | int myColor ; |
---|
| 55 | int i,c ; |
---|
| 56 | MPI_Comm newComm ; |
---|
[490] | 57 | |
---|
[300] | 58 | MPI_Comm_size(CXios::globalComm,&size) ; |
---|
| 59 | MPI_Comm_rank(CXios::globalComm,&rank); |
---|
| 60 | hashAll=new unsigned long[size] ; |
---|
[490] | 61 | |
---|
[300] | 62 | MPI_Allgather(&hashServer,1,MPI_LONG,hashAll,1,MPI_LONG,CXios::globalComm) ; |
---|
| 63 | |
---|
| 64 | map<unsigned long, int> colors ; |
---|
| 65 | map<unsigned long, int> leaders ; |
---|
| 66 | map<unsigned long, int>::iterator it ; |
---|
[490] | 67 | |
---|
[300] | 68 | for(i=0,c=0;i<size;i++) |
---|
| 69 | { |
---|
| 70 | if (colors.find(hashAll[i])==colors.end()) |
---|
| 71 | { |
---|
| 72 | colors[hashAll[i]]=c ; |
---|
| 73 | leaders[hashAll[i]]=i ; |
---|
| 74 | c++ ; |
---|
| 75 | } |
---|
| 76 | } |
---|
[490] | 77 | |
---|
[300] | 78 | myColor=colors[hashServer] ; |
---|
| 79 | MPI_Comm_split(MPI_COMM_WORLD,myColor,rank,&intraComm) ; |
---|
| 80 | |
---|
| 81 | int serverLeader=leaders[hashServer] ; |
---|
| 82 | int clientLeader; |
---|
[490] | 83 | |
---|
[300] | 84 | serverLeader=leaders[hashServer] ; |
---|
| 85 | for(it=leaders.begin();it!=leaders.end();it++) |
---|
| 86 | { |
---|
| 87 | if (it->first!=hashServer) |
---|
| 88 | { |
---|
| 89 | clientLeader=it->second ; |
---|
[492] | 90 | int intraCommSize, intraCommRank ; |
---|
| 91 | MPI_Comm_size(intraComm,&intraCommSize) ; |
---|
| 92 | MPI_Comm_rank(intraComm,&intraCommRank) ; |
---|
[493] | 93 | info(50)<<"intercommCreate::server "<<rank<<" intraCommSize : "<<intraCommSize |
---|
| 94 | <<" intraCommRank :"<<intraCommRank<<" clientLeader "<< clientLeader<<endl ; |
---|
[490] | 95 | |
---|
[300] | 96 | MPI_Intercomm_create(intraComm,0,CXios::globalComm,clientLeader,0,&newComm) ; |
---|
| 97 | interComm.push_back(newComm) ; |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | delete [] hashAll ; |
---|
| 102 | } |
---|
| 103 | // using OASIS |
---|
| 104 | else |
---|
| 105 | { |
---|
[490] | 106 | // int rank ,size; |
---|
| 107 | int size; |
---|
| 108 | if (!is_MPI_Initialized) oasis_init(CXios::xiosCodeId); |
---|
| 109 | |
---|
[359] | 110 | CTimer::get("XIOS").resume() ; |
---|
[300] | 111 | oasis_get_localcomm(intraComm) ; |
---|
| 112 | MPI_Comm_rank(intraComm,&rank) ; |
---|
| 113 | MPI_Comm_size(intraComm,&size) ; |
---|
| 114 | string codesId=CXios::getin<string>("oasis_codes_id") ; |
---|
[490] | 115 | |
---|
[300] | 116 | vector<string> splitted ; |
---|
[483] | 117 | boost::split( splitted, codesId, boost::is_any_of(","), boost::token_compress_on ) ; |
---|
[300] | 118 | vector<string>::iterator it ; |
---|
| 119 | |
---|
| 120 | MPI_Comm newComm ; |
---|
| 121 | int globalRank ; |
---|
| 122 | MPI_Comm_rank(CXios::globalComm,&globalRank); |
---|
[490] | 123 | |
---|
[300] | 124 | for(it=splitted.begin();it!=splitted.end();it++) |
---|
| 125 | { |
---|
| 126 | oasis_get_intercomm(newComm,*it) ; |
---|
| 127 | if (rank==0) MPI_Send(&globalRank,1,MPI_INT,0,0,newComm) ; |
---|
| 128 | MPI_Comm_remote_size(newComm,&size); |
---|
| 129 | interComm.push_back(newComm) ; |
---|
| 130 | } |
---|
[492] | 131 | oasis_enddef() ; |
---|
[300] | 132 | } |
---|
[490] | 133 | |
---|
| 134 | // int rank; |
---|
[300] | 135 | MPI_Comm_rank(intraComm,&rank) ; |
---|
| 136 | if (rank==0) isRoot=true; |
---|
[490] | 137 | else isRoot=false; |
---|
[492] | 138 | |
---|
| 139 | eventScheduler = new CEventScheduler(intraComm) ; |
---|
[300] | 140 | } |
---|
[490] | 141 | |
---|
[300] | 142 | void CServer::finalize(void) |
---|
| 143 | { |
---|
[361] | 144 | CTimer::get("XIOS").suspend() ; |
---|
[492] | 145 | |
---|
| 146 | delete eventScheduler ; |
---|
| 147 | |
---|
[300] | 148 | if (!is_MPI_Initialized) |
---|
[490] | 149 | { |
---|
[300] | 150 | if (CXios::usingOasis) oasis_finalize(); |
---|
| 151 | else MPI_Finalize() ; |
---|
| 152 | } |
---|
[347] | 153 | report(0)<<"Performance report : Time spent for XIOS : "<<CTimer::get("XIOS server").getCumulatedTime()<<endl ; |
---|
| 154 | report(0)<<"Performance report : Time spent in processing events : "<<CTimer::get("Process events").getCumulatedTime()<<endl ; |
---|
| 155 | report(0)<<"Performance report : Ratio : "<<CTimer::get("Process events").getCumulatedTime()/CTimer::get("XIOS server").getCumulatedTime()*100.<<"%"<<endl ; |
---|
[300] | 156 | } |
---|
[490] | 157 | |
---|
[300] | 158 | void CServer::eventLoop(void) |
---|
| 159 | { |
---|
| 160 | bool stop=false ; |
---|
[490] | 161 | |
---|
[347] | 162 | CTimer::get("XIOS server").resume() ; |
---|
[300] | 163 | while(!stop) |
---|
| 164 | { |
---|
| 165 | if (isRoot) |
---|
| 166 | { |
---|
| 167 | listenContext(); |
---|
| 168 | if (!finished) listenFinalize() ; |
---|
| 169 | } |
---|
| 170 | else |
---|
| 171 | { |
---|
| 172 | listenRootContext(); |
---|
| 173 | if (!finished) listenRootFinalize() ; |
---|
| 174 | } |
---|
[490] | 175 | |
---|
[300] | 176 | contextEventLoop() ; |
---|
| 177 | if (finished && contextList.empty()) stop=true ; |
---|
[492] | 178 | if (! CXios::isServer) eventScheduler->checkEvent() ; |
---|
[300] | 179 | } |
---|
[347] | 180 | CTimer::get("XIOS server").suspend() ; |
---|
[300] | 181 | } |
---|
[490] | 182 | |
---|
[300] | 183 | void CServer::listenFinalize(void) |
---|
| 184 | { |
---|
| 185 | list<MPI_Comm>::iterator it; |
---|
| 186 | int msg ; |
---|
| 187 | int flag ; |
---|
[490] | 188 | |
---|
[300] | 189 | for(it=interComm.begin();it!=interComm.end();it++) |
---|
| 190 | { |
---|
| 191 | MPI_Status status ; |
---|
[347] | 192 | traceOff() ; |
---|
[300] | 193 | MPI_Iprobe(0,0,*it,&flag,&status) ; |
---|
[347] | 194 | traceOn() ; |
---|
[300] | 195 | if (flag==true) |
---|
| 196 | { |
---|
| 197 | MPI_Recv(&msg,1,MPI_INT,0,0,*it,&status) ; |
---|
| 198 | info(20)<<" CServer : Receive client finalize"<<endl ; |
---|
| 199 | interComm.erase(it) ; |
---|
| 200 | break ; |
---|
| 201 | } |
---|
| 202 | } |
---|
[490] | 203 | |
---|
[300] | 204 | if (interComm.empty()) |
---|
| 205 | { |
---|
| 206 | int i,size ; |
---|
| 207 | MPI_Comm_size(intraComm,&size) ; |
---|
| 208 | MPI_Request* requests= new MPI_Request[size-1] ; |
---|
| 209 | MPI_Status* status= new MPI_Status[size-1] ; |
---|
[490] | 210 | |
---|
[300] | 211 | for(int i=1;i<size;i++) MPI_Isend(&msg,1,MPI_INT,i,4,intraComm,&requests[i-1]) ; |
---|
| 212 | MPI_Waitall(size-1,requests,status) ; |
---|
| 213 | |
---|
| 214 | finished=true ; |
---|
| 215 | delete [] requests ; |
---|
| 216 | delete [] status ; |
---|
| 217 | } |
---|
| 218 | } |
---|
[490] | 219 | |
---|
| 220 | |
---|
[300] | 221 | void CServer::listenRootFinalize() |
---|
| 222 | { |
---|
| 223 | int flag ; |
---|
| 224 | MPI_Status status ; |
---|
| 225 | int msg ; |
---|
[490] | 226 | |
---|
[347] | 227 | traceOff() ; |
---|
[300] | 228 | MPI_Iprobe(0,4,intraComm, &flag, &status) ; |
---|
[347] | 229 | traceOn() ; |
---|
[300] | 230 | if (flag==true) |
---|
| 231 | { |
---|
| 232 | MPI_Recv(&msg,1,MPI_INT,0,4,intraComm,&status) ; |
---|
| 233 | finished=true ; |
---|
| 234 | } |
---|
| 235 | } |
---|
[490] | 236 | |
---|
[300] | 237 | void CServer::listenContext(void) |
---|
| 238 | { |
---|
[490] | 239 | |
---|
[300] | 240 | MPI_Status status ; |
---|
| 241 | int flag ; |
---|
| 242 | static void* buffer ; |
---|
| 243 | static MPI_Request request ; |
---|
| 244 | static bool recept=false ; |
---|
| 245 | int rank ; |
---|
| 246 | int count ; |
---|
[490] | 247 | |
---|
[300] | 248 | if (recept==false) |
---|
| 249 | { |
---|
[347] | 250 | traceOff() ; |
---|
[300] | 251 | MPI_Iprobe(MPI_ANY_SOURCE,1,CXios::globalComm, &flag, &status) ; |
---|
[347] | 252 | traceOn() ; |
---|
[490] | 253 | if (flag==true) |
---|
[300] | 254 | { |
---|
| 255 | rank=status.MPI_SOURCE ; |
---|
| 256 | MPI_Get_count(&status,MPI_CHAR,&count) ; |
---|
| 257 | buffer=new char[count] ; |
---|
| 258 | MPI_Irecv(buffer,count,MPI_CHAR,rank,1,CXios::globalComm,&request) ; |
---|
[490] | 259 | recept=true ; |
---|
[300] | 260 | } |
---|
| 261 | } |
---|
| 262 | else |
---|
| 263 | { |
---|
[347] | 264 | traceOff() ; |
---|
[300] | 265 | MPI_Test(&request,&flag,&status) ; |
---|
[347] | 266 | traceOn() ; |
---|
[300] | 267 | if (flag==true) |
---|
| 268 | { |
---|
| 269 | rank=status.MPI_SOURCE ; |
---|
| 270 | MPI_Get_count(&status,MPI_CHAR,&count) ; |
---|
| 271 | recvContextMessage(buffer,count) ; |
---|
| 272 | delete [] buffer ; |
---|
[490] | 273 | recept=false ; |
---|
[300] | 274 | } |
---|
| 275 | } |
---|
| 276 | } |
---|
[490] | 277 | |
---|
[300] | 278 | void CServer::recvContextMessage(void* buff,int count) |
---|
| 279 | { |
---|
| 280 | static map<string,contextMessage> recvContextId ; |
---|
| 281 | map<string,contextMessage>::iterator it ; |
---|
[490] | 282 | |
---|
[300] | 283 | CBufferIn buffer(buff,count) ; |
---|
| 284 | string id ; |
---|
| 285 | int clientLeader ; |
---|
| 286 | int nbMessage ; |
---|
| 287 | |
---|
| 288 | buffer>>id>>nbMessage>>clientLeader ; |
---|
[490] | 289 | |
---|
[300] | 290 | it=recvContextId.find(id) ; |
---|
| 291 | if (it==recvContextId.end()) |
---|
[490] | 292 | { |
---|
[300] | 293 | contextMessage msg={0,0} ; |
---|
| 294 | pair<map<string,contextMessage>::iterator,bool> ret ; |
---|
| 295 | ret=recvContextId.insert(pair<string,contextMessage>(id,msg)) ; |
---|
| 296 | it=ret.first ; |
---|
[490] | 297 | } |
---|
[300] | 298 | it->second.nbRecv+=1 ; |
---|
| 299 | it->second.leaderRank+=clientLeader ; |
---|
[490] | 300 | |
---|
[300] | 301 | if (it->second.nbRecv==nbMessage) |
---|
[490] | 302 | { |
---|
[300] | 303 | int size ; |
---|
| 304 | MPI_Comm_size(intraComm,&size) ; |
---|
| 305 | MPI_Request* requests= new MPI_Request[size-1] ; |
---|
| 306 | MPI_Status* status= new MPI_Status[size-1] ; |
---|
[490] | 307 | |
---|
[300] | 308 | for(int i=1;i<size;i++) |
---|
| 309 | { |
---|
| 310 | MPI_Isend(buff,count,MPI_CHAR,i,2,intraComm,&requests[i-1]) ; |
---|
| 311 | } |
---|
| 312 | MPI_Waitall(size-1,requests,status) ; |
---|
| 313 | registerContext(buff,count,it->second.leaderRank) ; |
---|
| 314 | |
---|
| 315 | recvContextId.erase(it) ; |
---|
| 316 | delete [] requests ; |
---|
| 317 | delete [] status ; |
---|
| 318 | |
---|
| 319 | } |
---|
[490] | 320 | } |
---|
| 321 | |
---|
[300] | 322 | void CServer::listenRootContext(void) |
---|
| 323 | { |
---|
[490] | 324 | |
---|
[300] | 325 | MPI_Status status ; |
---|
| 326 | int flag ; |
---|
| 327 | static void* buffer ; |
---|
| 328 | static MPI_Request request ; |
---|
| 329 | static bool recept=false ; |
---|
| 330 | int rank ; |
---|
| 331 | int count ; |
---|
| 332 | const int root=0 ; |
---|
[490] | 333 | |
---|
[300] | 334 | if (recept==false) |
---|
| 335 | { |
---|
[347] | 336 | traceOff() ; |
---|
[300] | 337 | MPI_Iprobe(root,2,intraComm, &flag, &status) ; |
---|
[347] | 338 | traceOn() ; |
---|
[490] | 339 | if (flag==true) |
---|
[300] | 340 | { |
---|
| 341 | MPI_Get_count(&status,MPI_CHAR,&count) ; |
---|
| 342 | buffer=new char[count] ; |
---|
| 343 | MPI_Irecv(buffer,count,MPI_CHAR,root,2,intraComm,&request) ; |
---|
[490] | 344 | recept=true ; |
---|
[300] | 345 | } |
---|
| 346 | } |
---|
| 347 | else |
---|
| 348 | { |
---|
| 349 | MPI_Test(&request,&flag,&status) ; |
---|
| 350 | if (flag==true) |
---|
| 351 | { |
---|
| 352 | MPI_Get_count(&status,MPI_CHAR,&count) ; |
---|
| 353 | registerContext(buffer,count) ; |
---|
| 354 | delete [] buffer ; |
---|
[490] | 355 | recept=false ; |
---|
[300] | 356 | } |
---|
| 357 | } |
---|
[490] | 358 | } |
---|
| 359 | |
---|
| 360 | |
---|
| 361 | |
---|
[300] | 362 | void CServer::registerContext(void* buff,int count, int leaderRank) |
---|
| 363 | { |
---|
[490] | 364 | |
---|
[300] | 365 | string contextId; |
---|
| 366 | CBufferIn buffer(buff,count) ; |
---|
| 367 | |
---|
| 368 | buffer>>contextId ; |
---|
| 369 | MPI_Comm contextIntercomm ; |
---|
| 370 | MPI_Intercomm_create(intraComm,0,CXios::globalComm,leaderRank,10+leaderRank,&contextIntercomm) ; |
---|
[490] | 371 | |
---|
[300] | 372 | info(20)<<"CServer : Register new Context : "<<contextId<<endl ; |
---|
| 373 | MPI_Comm inter ; |
---|
| 374 | MPI_Intercomm_merge(contextIntercomm,1,&inter) ; |
---|
| 375 | MPI_Barrier(inter) ; |
---|
[490] | 376 | if (contextList.find(contextId)!=contextList.end()) |
---|
[300] | 377 | ERROR("void CServer::registerContext(void* buff,int count, int leaderRank)", |
---|
| 378 | <<"Context has already been registred") ; |
---|
[490] | 379 | |
---|
[347] | 380 | CContext* context=CContext::create(contextId) ; |
---|
| 381 | contextList[contextId]=context ; |
---|
[300] | 382 | context->initServer(intraComm,contextIntercomm) ; |
---|
[490] | 383 | |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | |
---|
[300] | 387 | void CServer::contextEventLoop(void) |
---|
| 388 | { |
---|
| 389 | bool finished ; |
---|
| 390 | map<string,CContext*>::iterator it ; |
---|
[490] | 391 | for(it=contextList.begin();it!=contextList.end();it++) |
---|
[300] | 392 | { |
---|
| 393 | finished=it->second->eventLoop() ; |
---|
| 394 | if (finished) |
---|
| 395 | { |
---|
| 396 | contextList.erase(it) ; |
---|
| 397 | break ; |
---|
| 398 | } |
---|
| 399 | } |
---|
[490] | 400 | |
---|
[300] | 401 | } |
---|
[490] | 402 | |
---|
| 403 | //! Get rank of the current process |
---|
| 404 | int CServer::getRank() |
---|
| 405 | { |
---|
| 406 | return rank; |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | /*! |
---|
| 410 | * \brief Open file stream to write in |
---|
| 411 | * Opening a file stream with a specific file name suffix-server+rank |
---|
| 412 | * \param [in] protype file name |
---|
| 413 | */ |
---|
| 414 | void CServer::openInfoStream(const StdString& fileName) |
---|
| 415 | { |
---|
| 416 | std::filebuf* fb = m_infoStream.rdbuf(); |
---|
| 417 | StdStringStream fileNameServer; |
---|
[497] | 418 | int numDigit = 0; |
---|
| 419 | int size = 0; |
---|
| 420 | MPI_Comm_size(CXios::globalComm, &size); |
---|
| 421 | while (size) |
---|
| 422 | { |
---|
| 423 | size /= 10; |
---|
| 424 | ++numDigit; |
---|
| 425 | } |
---|
| 426 | |
---|
[499] | 427 | fileNameServer << fileName <<"_" << std::setfill('0') << std::setw(numDigit) << getRank() << ".out"; |
---|
[490] | 428 | fb->open(fileNameServer.str().c_str(), std::ios::out); |
---|
| 429 | if (!fb->is_open()) |
---|
| 430 | ERROR("void CServer::openInfoStream(const StdString& fileName)", |
---|
| 431 | <<endl<< "Can not open <"<<fileNameServer<<"> file to write" ); |
---|
| 432 | |
---|
| 433 | info.write2File(fb); |
---|
[499] | 434 | report.write2File(fb); |
---|
[490] | 435 | } |
---|
| 436 | |
---|
| 437 | //! Open stream for standard output |
---|
| 438 | void CServer::openInfoStream() |
---|
| 439 | { |
---|
| 440 | info.write2StdOut(); |
---|
[499] | 441 | report.write2StdOut(); |
---|
[490] | 442 | } |
---|
| 443 | |
---|
| 444 | //! Close opening stream |
---|
| 445 | void CServer::closeInfoStream() |
---|
| 446 | { |
---|
| 447 | if (m_infoStream.is_open()) m_infoStream.close(); |
---|
| 448 | } |
---|
| 449 | |
---|
[300] | 450 | } |
---|