[490] | 1 | #include "globalScopeData.hpp" |
---|
[591] | 2 | #include "xios_spl.hpp" |
---|
[300] | 3 | #include "cxios.hpp" |
---|
[342] | 4 | #include "server.hpp" |
---|
[983] | 5 | #include "client.hpp" |
---|
[300] | 6 | #include "type.hpp" |
---|
| 7 | #include "context.hpp" |
---|
[352] | 8 | #include "object_template.hpp" |
---|
[300] | 9 | #include "oasis_cinterface.hpp" |
---|
| 10 | #include <boost/functional/hash.hpp> |
---|
| 11 | #include <boost/algorithm/string.hpp> |
---|
[382] | 12 | #include "mpi.hpp" |
---|
[347] | 13 | #include "tracer.hpp" |
---|
| 14 | #include "timer.hpp" |
---|
[492] | 15 | #include "event_scheduler.hpp" |
---|
[300] | 16 | |
---|
[335] | 17 | namespace xios |
---|
[490] | 18 | { |
---|
[300] | 19 | MPI_Comm CServer::intraComm ; |
---|
[992] | 20 | list<MPI_Comm> CServer::interCommLeft ; |
---|
| 21 | list<MPI_Comm> CServer::interCommRight ; |
---|
[1054] | 22 | // list<MPI_Comm> CServer::interComm ; |
---|
[655] | 23 | std::list<MPI_Comm> CServer::contextInterComms; |
---|
[1071] | 24 | std::list<MPI_Comm> CServer::contextIntraComms; |
---|
[1021] | 25 | int CServer::serverLevel = 0 ; |
---|
[1071] | 26 | int CServer::serverLeader_ = 0; |
---|
| 27 | int CServer::serverSize_ = 0; |
---|
[1021] | 28 | int CServer::nbPools = 0; |
---|
| 29 | int CServer::poolId = 0; |
---|
[1071] | 30 | int CServer::nbContexts_ = 0; |
---|
[983] | 31 | bool CServer::isRoot = false ; |
---|
[1077] | 32 | int CServer::rank_ = INVALID_RANK; |
---|
[490] | 33 | StdOFStream CServer::m_infoStream; |
---|
[523] | 34 | StdOFStream CServer::m_errorStream; |
---|
[490] | 35 | map<string,CContext*> CServer::contextList ; |
---|
[300] | 36 | bool CServer::finished=false ; |
---|
| 37 | bool CServer::is_MPI_Initialized ; |
---|
[597] | 38 | CEventScheduler* CServer::eventScheduler = 0; |
---|
[983] | 39 | |
---|
| 40 | //--------------------------------------------------------------- |
---|
| 41 | /*! |
---|
| 42 | * \fn void CServer::initialize(void) |
---|
[1054] | 43 | * Creates intraComm for each possible type of servers (classical, primary or secondary). |
---|
| 44 | * In case of secondary servers intraComm is created for each secondary server pool. |
---|
| 45 | * (For now the assumption is that there is one proc per pool.) |
---|
| 46 | * Creates the following lists of interComms: |
---|
| 47 | * classical server -- interCommLeft |
---|
| 48 | * primary server -- interCommLeft and interCommRight |
---|
| 49 | * secondary server -- interComm for each pool. |
---|
[983] | 50 | */ |
---|
[300] | 51 | void CServer::initialize(void) |
---|
| 52 | { |
---|
| 53 | int initialized ; |
---|
| 54 | MPI_Initialized(&initialized) ; |
---|
| 55 | if (initialized) is_MPI_Initialized=true ; |
---|
| 56 | else is_MPI_Initialized=false ; |
---|
[490] | 57 | |
---|
[300] | 58 | // Not using OASIS |
---|
| 59 | if (!CXios::usingOasis) |
---|
| 60 | { |
---|
[490] | 61 | |
---|
| 62 | if (!is_MPI_Initialized) |
---|
[300] | 63 | { |
---|
[925] | 64 | MPI_Init(NULL, NULL); |
---|
[300] | 65 | } |
---|
[359] | 66 | CTimer::get("XIOS").resume() ; |
---|
[490] | 67 | |
---|
| 68 | boost::hash<string> hashString ; |
---|
[1021] | 69 | unsigned long hashServer = hashString(CXios::xiosCodeId); |
---|
[490] | 70 | |
---|
[300] | 71 | unsigned long* hashAll ; |
---|
[490] | 72 | |
---|
| 73 | // int rank ; |
---|
[300] | 74 | int size ; |
---|
| 75 | int myColor ; |
---|
| 76 | int i,c ; |
---|
[1021] | 77 | MPI_Comm newComm, serversComm; |
---|
[490] | 78 | |
---|
[983] | 79 | MPI_Comm_size(CXios::globalComm, &size) ; |
---|
[1077] | 80 | MPI_Comm_rank(CXios::globalComm, &rank_); |
---|
[1009] | 81 | |
---|
[300] | 82 | hashAll=new unsigned long[size] ; |
---|
[983] | 83 | MPI_Allgather(&hashServer, 1, MPI_LONG, hashAll, 1, MPI_LONG, CXios::globalComm) ; |
---|
[490] | 84 | |
---|
[1021] | 85 | map<unsigned long, int> colors ; |
---|
[300] | 86 | map<unsigned long, int> leaders ; |
---|
[1133] | 87 | map<unsigned long, int> lastProcesses ; // needed in case of two server levels |
---|
[300] | 88 | map<unsigned long, int>::iterator it ; |
---|
[490] | 89 | |
---|
[300] | 90 | for(i=0,c=0;i<size;i++) |
---|
| 91 | { |
---|
| 92 | if (colors.find(hashAll[i])==colors.end()) |
---|
| 93 | { |
---|
| 94 | colors[hashAll[i]]=c ; |
---|
| 95 | leaders[hashAll[i]]=i ; |
---|
| 96 | c++ ; |
---|
| 97 | } |
---|
[1133] | 98 | if (hashAll[i+1] != hashAll[i]) |
---|
| 99 | lastProcesses[hashAll[i]]=i ; |
---|
[300] | 100 | } |
---|
[490] | 101 | |
---|
[1021] | 102 | // Setting the number of secondary pools |
---|
| 103 | myColor = colors[hashServer]; |
---|
| 104 | if (CXios::usingServer2) |
---|
[1009] | 105 | { |
---|
[1077] | 106 | int serverRank = rank_ - leaders[hashServer]; // server proc rank starting 0 |
---|
[1133] | 107 | serverSize_ = lastProcesses[hashServer] - leaders[hashServer] + 1; |
---|
| 108 | // serverSize_ = lastProcesses - leaders[hashServer]; |
---|
[1071] | 109 | nbPools = serverSize_ * CXios::ratioServer2 / 100; |
---|
| 110 | if ( serverRank < (serverSize_ - nbPools) ) |
---|
[1009] | 111 | { |
---|
[1021] | 112 | serverLevel = 1; |
---|
[1009] | 113 | } |
---|
[1021] | 114 | else |
---|
[1009] | 115 | { |
---|
[1021] | 116 | serverLevel = 2; |
---|
[1071] | 117 | poolId = serverRank - serverSize_ + nbPools; |
---|
[1077] | 118 | myColor = rank_; |
---|
[1009] | 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
[1077] | 122 | MPI_Comm_split(CXios::globalComm, myColor, rank_, &intraComm) ; |
---|
[1009] | 123 | |
---|
[1021] | 124 | if (serverLevel == 0) |
---|
[983] | 125 | { |
---|
| 126 | int clientLeader; |
---|
| 127 | for(it=leaders.begin();it!=leaders.end();it++) |
---|
| 128 | { |
---|
| 129 | if (it->first!=hashServer) |
---|
| 130 | { |
---|
| 131 | clientLeader=it->second ; |
---|
| 132 | int intraCommSize, intraCommRank ; |
---|
| 133 | MPI_Comm_size(intraComm,&intraCommSize) ; |
---|
| 134 | MPI_Comm_rank(intraComm,&intraCommRank) ; |
---|
[1077] | 135 | info(50)<<"intercommCreate::server "<<rank_<<" intraCommSize : "<<intraCommSize |
---|
[983] | 136 | <<" intraCommRank :"<<intraCommRank<<" clientLeader "<< clientLeader<<endl ; |
---|
[490] | 137 | |
---|
[1009] | 138 | MPI_Intercomm_create(intraComm, 0, CXios::globalComm, clientLeader, 0, &newComm) ; |
---|
[992] | 139 | interCommLeft.push_back(newComm) ; |
---|
[983] | 140 | } |
---|
| 141 | } |
---|
| 142 | } |
---|
[1021] | 143 | else if (serverLevel == 1) |
---|
[983] | 144 | { |
---|
[1054] | 145 | int clientLeader, srvSndLeader; |
---|
| 146 | int srvPrmLeader ; |
---|
[1021] | 147 | for (it=leaders.begin();it!=leaders.end();it++) |
---|
[983] | 148 | { |
---|
[1021] | 149 | if (it->first != hashServer) |
---|
[983] | 150 | { |
---|
[1021] | 151 | clientLeader=it->second ; |
---|
| 152 | int intraCommSize, intraCommRank ; |
---|
| 153 | MPI_Comm_size(intraComm, &intraCommSize) ; |
---|
| 154 | MPI_Comm_rank(intraComm, &intraCommRank) ; |
---|
[1077] | 155 | info(50)<<"intercommCreate::server "<<rank_<<" intraCommSize : "<<intraCommSize |
---|
[1021] | 156 | <<" intraCommRank :"<<intraCommRank<<" clientLeader "<< clientLeader<<endl ; |
---|
| 157 | MPI_Intercomm_create(intraComm, 0, CXios::globalComm, clientLeader, 0, &newComm) ; |
---|
| 158 | interCommLeft.push_back(newComm) ; |
---|
[983] | 159 | } |
---|
| 160 | else |
---|
[1071] | 161 | serverLeader_ = it->second; |
---|
[1021] | 162 | } |
---|
[1009] | 163 | |
---|
[1021] | 164 | for (int i = 0; i < nbPools; ++i) |
---|
[983] | 165 | { |
---|
[1071] | 166 | srvSndLeader = serverLeader_ + serverSize_ - nbPools + i; |
---|
[1054] | 167 | int intraCommSize, intraCommRank ; |
---|
| 168 | MPI_Comm_size(intraComm, &intraCommSize) ; |
---|
| 169 | MPI_Comm_rank(intraComm, &intraCommRank) ; |
---|
[1077] | 170 | info(50)<<"intercommCreate::client "<<rank_<<" intraCommSize : "<<intraCommSize |
---|
[1054] | 171 | <<" intraCommRank :"<<intraCommRank<<" clientLeader "<< srvSndLeader<<endl ; |
---|
| 172 | MPI_Intercomm_create(intraComm, 0, CXios::globalComm, srvSndLeader, 0, &newComm) ; |
---|
| 173 | interCommRight.push_back(newComm) ; |
---|
[1021] | 174 | } |
---|
| 175 | } // primary server |
---|
| 176 | else |
---|
| 177 | { |
---|
| 178 | int clientLeader; |
---|
| 179 | clientLeader = leaders[hashString(CXios::xiosCodeId)]; |
---|
| 180 | int intraCommSize, intraCommRank ; |
---|
| 181 | MPI_Comm_size(intraComm, &intraCommSize) ; |
---|
| 182 | MPI_Comm_rank(intraComm, &intraCommRank) ; |
---|
[1077] | 183 | info(50)<<"intercommCreate::server "<<rank_<<" intraCommSize : "<<intraCommSize |
---|
[1021] | 184 | <<" intraCommRank :"<<intraCommRank<<" clientLeader "<< clientLeader<<endl ; |
---|
[983] | 185 | |
---|
[1021] | 186 | MPI_Intercomm_create(intraComm, 0, CXios::globalComm, clientLeader, 0, &newComm) ; |
---|
| 187 | interCommLeft.push_back(newComm) ; |
---|
| 188 | } // secondary server |
---|
[983] | 189 | |
---|
| 190 | delete [] hashAll ; |
---|
| 191 | |
---|
[300] | 192 | } |
---|
| 193 | // using OASIS |
---|
| 194 | else |
---|
| 195 | { |
---|
[1130] | 196 | int size, rank; |
---|
| 197 | int myColor; |
---|
[490] | 198 | if (!is_MPI_Initialized) oasis_init(CXios::xiosCodeId); |
---|
| 199 | |
---|
[359] | 200 | CTimer::get("XIOS").resume() ; |
---|
[655] | 201 | MPI_Comm localComm; |
---|
| 202 | oasis_get_localcomm(localComm); |
---|
| 203 | |
---|
[1130] | 204 | // Create server intraComm |
---|
| 205 | if (!CXios::usingServer2) |
---|
| 206 | MPI_Comm_dup(localComm, &intraComm); |
---|
| 207 | else |
---|
| 208 | { |
---|
| 209 | MPI_Comm_rank(localComm,&rank) ; |
---|
| 210 | MPI_Comm_size(localComm,&serverSize_) ; |
---|
| 211 | nbPools = serverSize_ * CXios::ratioServer2 / 100; |
---|
| 212 | if ( rank < (serverSize_ - nbPools) ) |
---|
| 213 | { |
---|
| 214 | serverLevel = 1; |
---|
| 215 | myColor = 0; |
---|
| 216 | } |
---|
| 217 | else |
---|
| 218 | { |
---|
| 219 | serverLevel = 2; |
---|
| 220 | poolId = rank - serverSize_ + nbPools; |
---|
| 221 | myColor = rank; |
---|
| 222 | } |
---|
| 223 | MPI_Comm_split(localComm, myColor, rank, &intraComm) ; |
---|
| 224 | |
---|
| 225 | } |
---|
[1077] | 226 | MPI_Comm_rank(intraComm,&rank_) ; |
---|
[300] | 227 | MPI_Comm_size(intraComm,&size) ; |
---|
[1130] | 228 | |
---|
[300] | 229 | string codesId=CXios::getin<string>("oasis_codes_id") ; |
---|
[490] | 230 | |
---|
[300] | 231 | vector<string> splitted ; |
---|
[483] | 232 | boost::split( splitted, codesId, boost::is_any_of(","), boost::token_compress_on ) ; |
---|
[300] | 233 | vector<string>::iterator it ; |
---|
| 234 | |
---|
| 235 | MPI_Comm newComm ; |
---|
| 236 | int globalRank ; |
---|
| 237 | MPI_Comm_rank(CXios::globalComm,&globalRank); |
---|
[490] | 238 | |
---|
[300] | 239 | for(it=splitted.begin();it!=splitted.end();it++) |
---|
| 240 | { |
---|
| 241 | oasis_get_intercomm(newComm,*it) ; |
---|
[1130] | 242 | // interComm.push_back(newComm) ; |
---|
| 243 | if ( !CXios::usingServer2) |
---|
| 244 | interCommLeft.push_back(newComm) ; |
---|
| 245 | else |
---|
| 246 | { |
---|
| 247 | if (serverLevel == 1) |
---|
| 248 | { |
---|
| 249 | info(50)<<"intercommCreate::server "<<rank_<<" intraCommSize : "<<size |
---|
| 250 | <<" intraCommRank :"<<rank_<<" clientLeader "<< rank<<endl ; |
---|
| 251 | MPI_Intercomm_create(intraComm, 0, localComm, rank, 0, &newComm) ; |
---|
| 252 | interCommRight.push_back(newComm) ; |
---|
| 253 | |
---|
| 254 | } |
---|
| 255 | else if (serverLevel == 2) |
---|
| 256 | { |
---|
| 257 | info(50)<<"intercommCreate::server "<<rank_<<" intraCommSize : "<<size |
---|
| 258 | <<" intraCommRank :"<<rank_<<" clientLeader "<< 0<<endl ; |
---|
| 259 | MPI_Intercomm_create(intraComm, 0, localComm, 0, 0, &newComm) ; |
---|
| 260 | interCommLeft.push_back(newComm) ; |
---|
| 261 | |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | } |
---|
| 265 | // if (rank_==0) MPI_Send(&globalRank,1,MPI_INT,0,0,newComm) ; |
---|
| 266 | // MPI_Comm_remote_size(newComm,&size); |
---|
| 267 | // Send serverLeader to client |
---|
| 268 | if (rank_==0) MPI_Send(&globalRank,1,MPI_INT,0,0,interCommLeft.back()) ; |
---|
[300] | 269 | } |
---|
[492] | 270 | oasis_enddef() ; |
---|
[300] | 271 | } |
---|
[490] | 272 | |
---|
[1077] | 273 | MPI_Comm_rank(intraComm, &rank_) ; |
---|
| 274 | if (rank_==0) isRoot=true; |
---|
[490] | 275 | else isRoot=false; |
---|
[492] | 276 | |
---|
| 277 | eventScheduler = new CEventScheduler(intraComm) ; |
---|
[300] | 278 | } |
---|
[490] | 279 | |
---|
[300] | 280 | void CServer::finalize(void) |
---|
| 281 | { |
---|
[983] | 282 | |
---|
[361] | 283 | CTimer::get("XIOS").suspend() ; |
---|
[697] | 284 | |
---|
[492] | 285 | delete eventScheduler ; |
---|
[655] | 286 | |
---|
| 287 | for (std::list<MPI_Comm>::iterator it = contextInterComms.begin(); it != contextInterComms.end(); it++) |
---|
| 288 | MPI_Comm_free(&(*it)); |
---|
[983] | 289 | |
---|
[1071] | 290 | for (std::list<MPI_Comm>::iterator it = contextIntraComms.begin(); it != contextIntraComms.end(); it++) |
---|
| 291 | MPI_Comm_free(&(*it)); |
---|
| 292 | |
---|
[992] | 293 | // for (std::list<MPI_Comm>::iterator it = interComm.begin(); it != interComm.end(); it++) |
---|
| 294 | // MPI_Comm_free(&(*it)); |
---|
| 295 | |
---|
[1077] | 296 | // for (std::list<MPI_Comm>::iterator it = interCommLeft.begin(); it != interCommLeft.end(); it++) |
---|
| 297 | // MPI_Comm_free(&(*it)); |
---|
[983] | 298 | |
---|
[1054] | 299 | for (std::list<MPI_Comm>::iterator it = interCommRight.begin(); it != interCommRight.end(); it++) |
---|
| 300 | MPI_Comm_free(&(*it)); |
---|
[992] | 301 | |
---|
[655] | 302 | MPI_Comm_free(&intraComm); |
---|
| 303 | |
---|
[300] | 304 | if (!is_MPI_Initialized) |
---|
[490] | 305 | { |
---|
[300] | 306 | if (CXios::usingOasis) oasis_finalize(); |
---|
| 307 | else MPI_Finalize() ; |
---|
| 308 | } |
---|
[347] | 309 | report(0)<<"Performance report : Time spent for XIOS : "<<CTimer::get("XIOS server").getCumulatedTime()<<endl ; |
---|
| 310 | report(0)<<"Performance report : Time spent in processing events : "<<CTimer::get("Process events").getCumulatedTime()<<endl ; |
---|
| 311 | report(0)<<"Performance report : Ratio : "<<CTimer::get("Process events").getCumulatedTime()/CTimer::get("XIOS server").getCumulatedTime()*100.<<"%"<<endl ; |
---|
[300] | 312 | } |
---|
[490] | 313 | |
---|
[300] | 314 | void CServer::eventLoop(void) |
---|
| 315 | { |
---|
| 316 | bool stop=false ; |
---|
[490] | 317 | |
---|
[347] | 318 | CTimer::get("XIOS server").resume() ; |
---|
[300] | 319 | while(!stop) |
---|
| 320 | { |
---|
| 321 | if (isRoot) |
---|
| 322 | { |
---|
| 323 | listenContext(); |
---|
| 324 | if (!finished) listenFinalize() ; |
---|
| 325 | } |
---|
| 326 | else |
---|
| 327 | { |
---|
| 328 | listenRootContext(); |
---|
| 329 | if (!finished) listenRootFinalize() ; |
---|
| 330 | } |
---|
[490] | 331 | |
---|
[300] | 332 | contextEventLoop() ; |
---|
| 333 | if (finished && contextList.empty()) stop=true ; |
---|
[956] | 334 | eventScheduler->checkEvent() ; |
---|
[983] | 335 | |
---|
[300] | 336 | } |
---|
[347] | 337 | CTimer::get("XIOS server").suspend() ; |
---|
[300] | 338 | } |
---|
[490] | 339 | |
---|
[300] | 340 | void CServer::listenFinalize(void) |
---|
| 341 | { |
---|
[992] | 342 | list<MPI_Comm>::iterator it, itr; |
---|
[300] | 343 | int msg ; |
---|
| 344 | int flag ; |
---|
[490] | 345 | |
---|
[992] | 346 | for(it=interCommLeft.begin();it!=interCommLeft.end();it++) |
---|
[300] | 347 | { |
---|
| 348 | MPI_Status status ; |
---|
[347] | 349 | traceOff() ; |
---|
[300] | 350 | MPI_Iprobe(0,0,*it,&flag,&status) ; |
---|
[347] | 351 | traceOn() ; |
---|
[300] | 352 | if (flag==true) |
---|
| 353 | { |
---|
[1054] | 354 | MPI_Recv(&msg,1,MPI_INT,0,0,*it,&status) ; |
---|
| 355 | info(20)<<" CServer : Receive client finalize"<<endl ; |
---|
| 356 | // Sending server finalize message to secondary servers (if any) |
---|
| 357 | for(itr=interCommRight.begin();itr!=interCommRight.end();itr++) |
---|
| 358 | { |
---|
| 359 | MPI_Send(&msg,1,MPI_INT,0,0,*itr) ; |
---|
| 360 | } |
---|
[655] | 361 | MPI_Comm_free(&(*it)); |
---|
[992] | 362 | interCommLeft.erase(it) ; |
---|
[300] | 363 | break ; |
---|
| 364 | } |
---|
| 365 | } |
---|
[490] | 366 | |
---|
[1054] | 367 | if (interCommLeft.empty()) |
---|
[300] | 368 | { |
---|
| 369 | int i,size ; |
---|
| 370 | MPI_Comm_size(intraComm,&size) ; |
---|
| 371 | MPI_Request* requests= new MPI_Request[size-1] ; |
---|
| 372 | MPI_Status* status= new MPI_Status[size-1] ; |
---|
[490] | 373 | |
---|
[300] | 374 | for(int i=1;i<size;i++) MPI_Isend(&msg,1,MPI_INT,i,4,intraComm,&requests[i-1]) ; |
---|
| 375 | MPI_Waitall(size-1,requests,status) ; |
---|
| 376 | |
---|
| 377 | finished=true ; |
---|
| 378 | delete [] requests ; |
---|
| 379 | delete [] status ; |
---|
| 380 | } |
---|
| 381 | } |
---|
[490] | 382 | |
---|
| 383 | |
---|
[300] | 384 | void CServer::listenRootFinalize() |
---|
| 385 | { |
---|
| 386 | int flag ; |
---|
| 387 | MPI_Status status ; |
---|
| 388 | int msg ; |
---|
[490] | 389 | |
---|
[347] | 390 | traceOff() ; |
---|
[300] | 391 | MPI_Iprobe(0,4,intraComm, &flag, &status) ; |
---|
[347] | 392 | traceOn() ; |
---|
[300] | 393 | if (flag==true) |
---|
| 394 | { |
---|
| 395 | MPI_Recv(&msg,1,MPI_INT,0,4,intraComm,&status) ; |
---|
| 396 | finished=true ; |
---|
| 397 | } |
---|
| 398 | } |
---|
[490] | 399 | |
---|
[300] | 400 | void CServer::listenContext(void) |
---|
| 401 | { |
---|
[490] | 402 | |
---|
[300] | 403 | MPI_Status status ; |
---|
| 404 | int flag ; |
---|
| 405 | static void* buffer ; |
---|
| 406 | static MPI_Request request ; |
---|
| 407 | static bool recept=false ; |
---|
| 408 | int rank ; |
---|
| 409 | int count ; |
---|
[490] | 410 | |
---|
[300] | 411 | if (recept==false) |
---|
| 412 | { |
---|
[347] | 413 | traceOff() ; |
---|
[300] | 414 | MPI_Iprobe(MPI_ANY_SOURCE,1,CXios::globalComm, &flag, &status) ; |
---|
[347] | 415 | traceOn() ; |
---|
[490] | 416 | if (flag==true) |
---|
[300] | 417 | { |
---|
| 418 | rank=status.MPI_SOURCE ; |
---|
| 419 | MPI_Get_count(&status,MPI_CHAR,&count) ; |
---|
| 420 | buffer=new char[count] ; |
---|
| 421 | MPI_Irecv(buffer,count,MPI_CHAR,rank,1,CXios::globalComm,&request) ; |
---|
[490] | 422 | recept=true ; |
---|
[300] | 423 | } |
---|
| 424 | } |
---|
| 425 | else |
---|
| 426 | { |
---|
[347] | 427 | traceOff() ; |
---|
[300] | 428 | MPI_Test(&request,&flag,&status) ; |
---|
[347] | 429 | traceOn() ; |
---|
[300] | 430 | if (flag==true) |
---|
| 431 | { |
---|
| 432 | rank=status.MPI_SOURCE ; |
---|
| 433 | MPI_Get_count(&status,MPI_CHAR,&count) ; |
---|
| 434 | recvContextMessage(buffer,count) ; |
---|
[1054] | 435 | delete [] buffer; |
---|
[490] | 436 | recept=false ; |
---|
[300] | 437 | } |
---|
| 438 | } |
---|
| 439 | } |
---|
[490] | 440 | |
---|
[300] | 441 | void CServer::recvContextMessage(void* buff,int count) |
---|
| 442 | { |
---|
[983] | 443 | static map<string,contextMessage> recvContextId; |
---|
[300] | 444 | map<string,contextMessage>::iterator it ; |
---|
| 445 | CBufferIn buffer(buff,count) ; |
---|
| 446 | string id ; |
---|
| 447 | int clientLeader ; |
---|
| 448 | int nbMessage ; |
---|
| 449 | |
---|
| 450 | buffer>>id>>nbMessage>>clientLeader ; |
---|
[490] | 451 | |
---|
[300] | 452 | it=recvContextId.find(id) ; |
---|
| 453 | if (it==recvContextId.end()) |
---|
[490] | 454 | { |
---|
[300] | 455 | contextMessage msg={0,0} ; |
---|
| 456 | pair<map<string,contextMessage>::iterator,bool> ret ; |
---|
| 457 | ret=recvContextId.insert(pair<string,contextMessage>(id,msg)) ; |
---|
| 458 | it=ret.first ; |
---|
[490] | 459 | } |
---|
[300] | 460 | it->second.nbRecv+=1 ; |
---|
| 461 | it->second.leaderRank+=clientLeader ; |
---|
[490] | 462 | |
---|
[300] | 463 | if (it->second.nbRecv==nbMessage) |
---|
[490] | 464 | { |
---|
[300] | 465 | int size ; |
---|
| 466 | MPI_Comm_size(intraComm,&size) ; |
---|
| 467 | MPI_Request* requests= new MPI_Request[size-1] ; |
---|
| 468 | MPI_Status* status= new MPI_Status[size-1] ; |
---|
[490] | 469 | |
---|
[300] | 470 | for(int i=1;i<size;i++) |
---|
| 471 | { |
---|
| 472 | MPI_Isend(buff,count,MPI_CHAR,i,2,intraComm,&requests[i-1]) ; |
---|
| 473 | } |
---|
| 474 | MPI_Waitall(size-1,requests,status) ; |
---|
| 475 | registerContext(buff,count,it->second.leaderRank) ; |
---|
| 476 | |
---|
| 477 | recvContextId.erase(it) ; |
---|
| 478 | delete [] requests ; |
---|
| 479 | delete [] status ; |
---|
| 480 | |
---|
| 481 | } |
---|
[490] | 482 | } |
---|
| 483 | |
---|
[300] | 484 | void CServer::listenRootContext(void) |
---|
| 485 | { |
---|
| 486 | MPI_Status status ; |
---|
| 487 | int flag ; |
---|
| 488 | static void* buffer ; |
---|
| 489 | static MPI_Request request ; |
---|
| 490 | static bool recept=false ; |
---|
| 491 | int rank ; |
---|
| 492 | int count ; |
---|
| 493 | const int root=0 ; |
---|
[490] | 494 | |
---|
[300] | 495 | if (recept==false) |
---|
| 496 | { |
---|
[347] | 497 | traceOff() ; |
---|
[300] | 498 | MPI_Iprobe(root,2,intraComm, &flag, &status) ; |
---|
[347] | 499 | traceOn() ; |
---|
[490] | 500 | if (flag==true) |
---|
[300] | 501 | { |
---|
| 502 | MPI_Get_count(&status,MPI_CHAR,&count) ; |
---|
| 503 | buffer=new char[count] ; |
---|
| 504 | MPI_Irecv(buffer,count,MPI_CHAR,root,2,intraComm,&request) ; |
---|
[490] | 505 | recept=true ; |
---|
[300] | 506 | } |
---|
| 507 | } |
---|
| 508 | else |
---|
| 509 | { |
---|
| 510 | MPI_Test(&request,&flag,&status) ; |
---|
| 511 | if (flag==true) |
---|
| 512 | { |
---|
| 513 | MPI_Get_count(&status,MPI_CHAR,&count) ; |
---|
| 514 | registerContext(buffer,count) ; |
---|
| 515 | delete [] buffer ; |
---|
[490] | 516 | recept=false ; |
---|
[300] | 517 | } |
---|
| 518 | } |
---|
[490] | 519 | } |
---|
| 520 | |
---|
[655] | 521 | void CServer::registerContext(void* buff, int count, int leaderRank) |
---|
[300] | 522 | { |
---|
| 523 | string contextId; |
---|
[655] | 524 | CBufferIn buffer(buff, count); |
---|
| 525 | buffer >> contextId; |
---|
[983] | 526 | CContext* context; |
---|
[300] | 527 | |
---|
[680] | 528 | info(20) << "CServer : Register new Context : " << contextId << endl; |
---|
[490] | 529 | |
---|
[680] | 530 | if (contextList.find(contextId) != contextList.end()) |
---|
| 531 | ERROR("void CServer::registerContext(void* buff, int count, int leaderRank)", |
---|
| 532 | << "Context '" << contextId << "' has already been registred"); |
---|
[490] | 533 | |
---|
[983] | 534 | context=CContext::create(contextId); |
---|
[655] | 535 | contextList[contextId]=context; |
---|
| 536 | |
---|
[1071] | 537 | // Primary or classical server: initialize its own server (CContextServer) |
---|
| 538 | MPI_Comm inter; |
---|
[1054] | 539 | if (serverLevel < 2) |
---|
| 540 | { |
---|
| 541 | MPI_Comm contextInterComm; |
---|
| 542 | MPI_Intercomm_create(intraComm, 0, CXios::globalComm, leaderRank, 10+leaderRank, &contextInterComm); |
---|
| 543 | MPI_Intercomm_merge(contextInterComm,1,&inter); |
---|
| 544 | MPI_Barrier(inter); |
---|
| 545 | MPI_Comm_free(&inter); |
---|
| 546 | context->initServer(intraComm,contextInterComm); |
---|
| 547 | contextInterComms.push_back(contextInterComm); |
---|
[1071] | 548 | |
---|
[1054] | 549 | } |
---|
[1071] | 550 | // Secondary server: initialize its own server (CContextServer) |
---|
[1054] | 551 | else if (serverLevel == 2) |
---|
| 552 | { |
---|
[1071] | 553 | MPI_Comm_dup(interCommLeft.front(), &inter); |
---|
| 554 | contextInterComms.push_back(inter); |
---|
| 555 | context->initServer(intraComm, contextInterComms.back()); |
---|
[1054] | 556 | } |
---|
| 557 | |
---|
| 558 | // Primary server: send create context message to secondary servers and initialize its own client (CContextClient) |
---|
[1021] | 559 | if (serverLevel == 1) |
---|
[983] | 560 | { |
---|
[1054] | 561 | int i = 0, size; |
---|
| 562 | CMessage msg; |
---|
| 563 | int messageSize; |
---|
| 564 | MPI_Comm_size(intraComm, &size) ; |
---|
| 565 | for (std::list<MPI_Comm>::iterator it = interCommRight.begin(); it != interCommRight.end(); it++, ++i) |
---|
| 566 | { |
---|
| 567 | StdString str = contextId +"_server_" + boost::lexical_cast<string>(i); |
---|
[1077] | 568 | msg<<str<<size<<rank_ ; |
---|
[1054] | 569 | messageSize = msg.size() ; |
---|
| 570 | buff = new char[messageSize] ; |
---|
| 571 | CBufferOut buffer(buff,messageSize) ; |
---|
| 572 | buffer<<msg ; |
---|
[1071] | 573 | int sndServerGloRanks = serverSize_-nbPools+serverLeader_ +i; // the assumption is that there is only one proc per secondary server pool |
---|
[1054] | 574 | MPI_Send(buff, buffer.count(), MPI_CHAR, sndServerGloRanks, 1, CXios::globalComm) ; |
---|
[1071] | 575 | MPI_Comm_dup(*it, &inter); |
---|
| 576 | contextInterComms.push_back(inter); |
---|
| 577 | MPI_Comm_dup(intraComm, &inter); |
---|
| 578 | contextIntraComms.push_back(inter); |
---|
| 579 | context->initClient(contextIntraComms.back(), contextInterComms.back()) ; |
---|
[1054] | 580 | delete [] buff ; |
---|
| 581 | } |
---|
[1071] | 582 | ++nbContexts_; |
---|
[983] | 583 | } |
---|
[490] | 584 | } |
---|
| 585 | |
---|
[300] | 586 | void CServer::contextEventLoop(void) |
---|
| 587 | { |
---|
[1130] | 588 | bool isFinalized ; |
---|
[300] | 589 | map<string,CContext*>::iterator it ; |
---|
[983] | 590 | |
---|
[490] | 591 | for(it=contextList.begin();it!=contextList.end();it++) |
---|
[300] | 592 | { |
---|
[1130] | 593 | isFinalized=it->second->isFinalized(); |
---|
| 594 | if (isFinalized) |
---|
[300] | 595 | { |
---|
[1139] | 596 | // it->second->postFinalize(); |
---|
[300] | 597 | contextList.erase(it) ; |
---|
| 598 | break ; |
---|
| 599 | } |
---|
[1054] | 600 | else |
---|
[1139] | 601 | it->second->checkBuffersAndListen(); |
---|
[300] | 602 | } |
---|
| 603 | } |
---|
[490] | 604 | |
---|
| 605 | //! Get rank of the current process |
---|
| 606 | int CServer::getRank() |
---|
| 607 | { |
---|
[1077] | 608 | return rank_; |
---|
[490] | 609 | } |
---|
| 610 | |
---|
[523] | 611 | /*! |
---|
| 612 | * Open a file specified by a suffix and an extension and use it for the given file buffer. |
---|
| 613 | * The file name will be suffix+rank+extension. |
---|
| 614 | * |
---|
| 615 | * \param fileName[in] protype file name |
---|
| 616 | * \param ext [in] extension of the file |
---|
| 617 | * \param fb [in/out] the file buffer |
---|
| 618 | */ |
---|
| 619 | void CServer::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb) |
---|
| 620 | { |
---|
| 621 | StdStringStream fileNameClient; |
---|
| 622 | int numDigit = 0; |
---|
| 623 | int size = 0; |
---|
[1021] | 624 | int id; |
---|
[523] | 625 | MPI_Comm_size(CXios::globalComm, &size); |
---|
| 626 | while (size) |
---|
| 627 | { |
---|
| 628 | size /= 10; |
---|
| 629 | ++numDigit; |
---|
| 630 | } |
---|
[497] | 631 | |
---|
[1021] | 632 | if (!CXios::usingServer2) |
---|
| 633 | id = getRank(); |
---|
[1009] | 634 | else |
---|
[1021] | 635 | { |
---|
| 636 | if (serverLevel == 1) |
---|
[1077] | 637 | id = rank_; |
---|
[1021] | 638 | else |
---|
| 639 | id = poolId; |
---|
| 640 | } |
---|
| 641 | fileNameClient << fileName << "_" << std::setfill('0') << std::setw(numDigit) << id << ext; |
---|
[523] | 642 | fb->open(fileNameClient.str().c_str(), std::ios::out); |
---|
| 643 | if (!fb->is_open()) |
---|
| 644 | ERROR("void CServer::openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb)", |
---|
| 645 | << std::endl << "Can not open <" << fileNameClient << "> file to write the server log(s)."); |
---|
| 646 | } |
---|
[490] | 647 | |
---|
[523] | 648 | /*! |
---|
| 649 | * \brief Open a file stream to write the info logs |
---|
| 650 | * Open a file stream with a specific file name suffix+rank |
---|
| 651 | * to write the info logs. |
---|
| 652 | * \param fileName [in] protype file name |
---|
| 653 | */ |
---|
| 654 | void CServer::openInfoStream(const StdString& fileName) |
---|
| 655 | { |
---|
| 656 | std::filebuf* fb = m_infoStream.rdbuf(); |
---|
| 657 | openStream(fileName, ".out", fb); |
---|
[490] | 658 | |
---|
[523] | 659 | info.write2File(fb); |
---|
| 660 | report.write2File(fb); |
---|
| 661 | } |
---|
[490] | 662 | |
---|
[523] | 663 | //! Write the info logs to standard output |
---|
| 664 | void CServer::openInfoStream() |
---|
| 665 | { |
---|
| 666 | info.write2StdOut(); |
---|
| 667 | report.write2StdOut(); |
---|
| 668 | } |
---|
[490] | 669 | |
---|
[523] | 670 | //! Close the info logs file if it opens |
---|
| 671 | void CServer::closeInfoStream() |
---|
| 672 | { |
---|
| 673 | if (m_infoStream.is_open()) m_infoStream.close(); |
---|
| 674 | } |
---|
| 675 | |
---|
| 676 | /*! |
---|
| 677 | * \brief Open a file stream to write the error log |
---|
| 678 | * Open a file stream with a specific file name suffix+rank |
---|
| 679 | * to write the error log. |
---|
| 680 | * \param fileName [in] protype file name |
---|
| 681 | */ |
---|
| 682 | void CServer::openErrorStream(const StdString& fileName) |
---|
| 683 | { |
---|
| 684 | std::filebuf* fb = m_errorStream.rdbuf(); |
---|
| 685 | openStream(fileName, ".err", fb); |
---|
| 686 | |
---|
| 687 | error.write2File(fb); |
---|
| 688 | } |
---|
| 689 | |
---|
| 690 | //! Write the error log to standard error output |
---|
| 691 | void CServer::openErrorStream() |
---|
| 692 | { |
---|
| 693 | error.write2StdErr(); |
---|
| 694 | } |
---|
| 695 | |
---|
| 696 | //! Close the error log file if it opens |
---|
| 697 | void CServer::closeErrorStream() |
---|
| 698 | { |
---|
| 699 | if (m_errorStream.is_open()) m_errorStream.close(); |
---|
| 700 | } |
---|
[300] | 701 | } |
---|