[1761] | 1 | #include "services.hpp" |
---|
| 2 | #include "services_manager.hpp" |
---|
| 3 | #include "mpi.hpp" |
---|
| 4 | #include "cxios.hpp" |
---|
| 5 | #include "server_context.hpp" |
---|
| 6 | #include "event_scheduler.hpp" |
---|
[2246] | 7 | #include "timer.hpp" |
---|
[1761] | 8 | |
---|
| 9 | namespace xios |
---|
| 10 | { |
---|
[2523] | 11 | CService::CService(MPI_Comm serviceComm, shared_ptr<CEventScheduler> eventScheduler, const std::string& poolId, const std::string& serviceId, const int& partitionId, |
---|
| 12 | int type, int nbPartitions) |
---|
[2404] | 13 | : finalizeSignal_(false), eventScheduler_(nullptr), poolId_(poolId), serviceId_(serviceId), |
---|
| 14 | partitionId_(partitionId), type_(type), nbPartitions_(nbPartitions), hasNotification_(false) |
---|
[1761] | 15 | |
---|
| 16 | |
---|
| 17 | { |
---|
[2246] | 18 | info(40)<<"CService::CService : new service created ; serviceId : "<<serviceId<<endl ; |
---|
| 19 | |
---|
[1761] | 20 | int localRank, globalRank, commSize ; |
---|
| 21 | |
---|
| 22 | MPI_Comm_dup(serviceComm, &serviceComm_) ; |
---|
| 23 | MPI_Comm globalComm_=CXios::getXiosComm() ; |
---|
| 24 | |
---|
| 25 | MPI_Comm_rank(globalComm_,&globalRank) ; |
---|
| 26 | MPI_Comm_rank(serviceComm_,&localRank) ; |
---|
| 27 | |
---|
| 28 | winNotify_ = new CWindowManager(serviceComm_, maxBufferSize_) ; |
---|
[2517] | 29 | winNotify_->updateToExclusiveWindow(localRank, this, &CService::createContextDumpOut) ; |
---|
[2220] | 30 | MPI_Barrier(serviceComm_) ; |
---|
[1761] | 31 | if (localRank==localLeader_) |
---|
| 32 | { |
---|
| 33 | globalLeader_=globalRank ; |
---|
| 34 | MPI_Comm_rank(serviceComm_,&commSize) ; |
---|
| 35 | CXios::getServicesManager()->registerService(poolId, serviceId, partitionId, type, commSize, nbPartitions, globalLeader_) ; |
---|
| 36 | } |
---|
[2404] | 37 | if (eventScheduler) eventScheduler_ = eventScheduler ; |
---|
[2486] | 38 | else eventScheduler_ = make_shared<CEventScheduler>(serviceComm_) ; |
---|
[1764] | 39 | |
---|
| 40 | ostringstream oss; |
---|
| 41 | oss<<partitionId; |
---|
[2287] | 42 | name_= poolId+"__"+serviceId+"_"+oss.str(); |
---|
[1761] | 43 | } |
---|
| 44 | |
---|
[2274] | 45 | CService::~CService() |
---|
| 46 | { |
---|
| 47 | delete winNotify_ ; |
---|
| 48 | for(auto& it : contexts_) delete it.second ; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | |
---|
[1761] | 52 | void CService::createContext( const std::string& poolId, const std::string& serviceId, const int& partitionId, const std::string& contextId) |
---|
| 53 | { |
---|
| 54 | int commSize ; |
---|
| 55 | MPI_Comm_size(serviceComm_, &commSize) ; |
---|
[2246] | 56 | info(40)<<"CService::createContext : notify CreateContext to all services members ; serviceId : "<<serviceId<<" ; contextId : "<<contextId<<endl ; |
---|
| 57 | |
---|
[1764] | 58 | for(int rank=0; rank<commSize; rank++) |
---|
| 59 | { |
---|
| 60 | notifyOutType_=NOTIFY_CREATE_CONTEXT ; |
---|
| 61 | notifyOutCreateContext_ = make_tuple(poolId, serviceId, partitionId, contextId) ; |
---|
| 62 | sendNotification(rank) ; |
---|
| 63 | } |
---|
[2246] | 64 | info(40)<<"CService::createContext : notify CreateContext to all services members : DONE "<<endl ; |
---|
[1761] | 65 | } |
---|
| 66 | /* |
---|
| 67 | void CService::createContext(const std::string& contextId) |
---|
| 68 | { |
---|
| 69 | contexts_[contextId] = new CServerContext(this, serviceComm_, poolId_, serviceId_, partitionId_, contextId) ; |
---|
| 70 | } |
---|
| 71 | */ |
---|
| 72 | void CService::createContextNotify(int rank, const std::string& poolId, const std::string& serviceId, const int& partitionId, const std::string& contextId) |
---|
| 73 | { |
---|
[2517] | 74 | winNotify_->lockWindowExclusive(rank) ; |
---|
| 75 | winNotify_->updateFromLockedWindow(rank, this, &CService::createContextDumpIn) ; |
---|
[1761] | 76 | notifications_.push_back(std::make_tuple(poolId, serviceId, partitionId, contextId)) ; |
---|
[2517] | 77 | winNotify_->updateToLockedWindow(rank, this, &CService::createContextDumpOut) ; |
---|
| 78 | winNotify_->unlockWindowExclusive(rank) ; |
---|
[1761] | 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | void CService::createContextDumpOut(CBufferOut& buffer) |
---|
| 83 | { |
---|
| 84 | buffer.realloc(maxBufferSize_) ; |
---|
| 85 | |
---|
| 86 | buffer << (int) (notifications_.size()); |
---|
| 87 | |
---|
| 88 | for(auto it=notifications_.begin();it!=notifications_.end(); ++it) |
---|
| 89 | buffer << std::get<0>(*it) << std::get<1>(*it) << std::get<2>(*it) << std::get<3>(*it) ; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | void CService::createContextDumpIn(CBufferIn& buffer) |
---|
| 94 | { |
---|
| 95 | std::string poolId ; |
---|
| 96 | std::string serviceId ; |
---|
| 97 | int partitionId ; |
---|
| 98 | std::string contextId ; |
---|
| 99 | |
---|
| 100 | notifications_.clear() ; |
---|
| 101 | int nbNotifications ; |
---|
| 102 | buffer>>nbNotifications ; |
---|
| 103 | for(int i=0;i<nbNotifications;i++) |
---|
| 104 | { |
---|
| 105 | buffer>>poolId>>serviceId>>partitionId>>contextId ; |
---|
| 106 | notifications_.push_back(std::make_tuple(poolId, serviceId, partitionId, contextId)) ; |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
[1764] | 110 | bool CService::eventLoop(bool serviceOnly) |
---|
[1761] | 111 | { |
---|
[1764] | 112 | //checkCreateContextNotification() ; |
---|
[2246] | 113 | CTimer::get("CService::eventLoop").resume(); |
---|
[2260] | 114 | int flag ; |
---|
| 115 | MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE); |
---|
| 116 | |
---|
[2246] | 117 | // double time=MPI_Wtime() ; |
---|
| 118 | // if (time-lastEventLoop_ > eventLoopLatency_) |
---|
| 119 | // { |
---|
| 120 | checkNotifications() ; |
---|
| 121 | // lastEventLoop_=time ; |
---|
| 122 | // } |
---|
[1764] | 123 | |
---|
[2246] | 124 | |
---|
[1761] | 125 | eventScheduler_->checkEvent() ; |
---|
| 126 | for(auto it=contexts_.begin();it!=contexts_.end();++it) |
---|
| 127 | { |
---|
[1764] | 128 | if (it->second->eventLoop(serviceOnly)) |
---|
[1761] | 129 | { |
---|
[2274] | 130 | delete it->second ; |
---|
[1761] | 131 | contexts_.erase(it) ; |
---|
| 132 | // destroy server_context -> to do later |
---|
| 133 | break ; |
---|
| 134 | } ; |
---|
| 135 | } |
---|
[2246] | 136 | CTimer::get("CService::eventLoop").suspend(); |
---|
[1761] | 137 | if (contexts_.empty() && finalizeSignal_) return true ; |
---|
| 138 | else return false ; |
---|
| 139 | } |
---|
| 140 | |
---|
[1764] | 141 | void CService::sendNotification(int rank) |
---|
| 142 | { |
---|
[2517] | 143 | winNotify_->pushToExclusiveWindow(rank, this, &CService::notificationsDumpOut) ; |
---|
[1764] | 144 | } |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | void CService::notificationsDumpOut(CBufferOut& buffer) |
---|
| 148 | { |
---|
| 149 | |
---|
| 150 | buffer.realloc(maxBufferSize_) ; |
---|
| 151 | |
---|
| 152 | if (notifyOutType_==NOTIFY_CREATE_CONTEXT) |
---|
| 153 | { |
---|
| 154 | auto& arg=notifyOutCreateContext_ ; |
---|
| 155 | buffer << notifyOutType_ << std::get<0>(arg)<<std::get<1>(arg) << std::get<2>(arg)<<std::get<3>(arg) ; |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | void CService::notificationsDumpIn(CBufferIn& buffer) |
---|
| 160 | { |
---|
| 161 | if (buffer.bufferSize() == 0) notifyInType_= NOTIFY_NOTHING ; |
---|
| 162 | else |
---|
| 163 | { |
---|
| 164 | buffer>>notifyInType_; |
---|
| 165 | if (notifyInType_==NOTIFY_CREATE_CONTEXT) |
---|
| 166 | { |
---|
| 167 | auto& arg=notifyInCreateContext_ ; |
---|
| 168 | buffer >> std::get<0>(arg)>> std::get<1>(arg) >> std::get<2>(arg)>> std::get<3>(arg); |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | |
---|
| 176 | void CService::checkNotifications(void) |
---|
| 177 | { |
---|
| 178 | if (!hasNotification_) |
---|
| 179 | { |
---|
[2246] | 180 | double time=MPI_Wtime() ; |
---|
| 181 | if (time-lastEventLoop_ > eventLoopLatency_) |
---|
| 182 | { |
---|
| 183 | int commRank ; |
---|
| 184 | MPI_Comm_rank(serviceComm_, &commRank) ; |
---|
[2517] | 185 | winNotify_->popFromExclusiveWindow(commRank, this, &CService::notificationsDumpIn) ; |
---|
| 186 | |
---|
[2246] | 187 | if (notifyInType_!= NOTIFY_NOTHING) |
---|
| 188 | { |
---|
| 189 | hasNotification_=true ; |
---|
| 190 | std::hash<string> hashString ; |
---|
| 191 | size_t hashId = hashString(name_) ; |
---|
| 192 | size_t currentTimeLine=0 ; |
---|
[2523] | 193 | info(40)<<"CService::checkNotifications(void) : receive notification => event scheduler : timeLine : "<<currentTimeLine<<" hashId : "<<hashId<<endl ; |
---|
[2246] | 194 | eventScheduler_->registerEvent(currentTimeLine,hashId); |
---|
| 195 | } |
---|
| 196 | lastEventLoop_=time ; |
---|
[1764] | 197 | } |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | if (hasNotification_) |
---|
| 201 | { |
---|
| 202 | std::hash<string> hashString ; |
---|
| 203 | size_t hashId = hashString(name_) ; |
---|
| 204 | size_t currentTimeLine=0 ; |
---|
[2523] | 205 | // info(40)<<"CService::checkNotifications(void) : receive notification => event scheduler : eventIsReceived ?"<<endl ; |
---|
[1764] | 206 | if (eventScheduler_->queryEvent(currentTimeLine,hashId)) |
---|
| 207 | { |
---|
[2230] | 208 | eventScheduler_->popEvent() ; |
---|
[2246] | 209 | info(40)<<"CService::checkNotifications(void) : receive notification => event scheduler : RECEIVED"<<endl ; |
---|
[1764] | 210 | if (notifyInType_==NOTIFY_CREATE_CONTEXT) createContext() ; |
---|
| 211 | hasNotification_=false ; |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | |
---|
[2246] | 218 | //ym not use any more |
---|
[1761] | 219 | void CService::checkCreateContextNotification(void) |
---|
| 220 | { |
---|
| 221 | int commRank ; |
---|
| 222 | MPI_Comm_rank(serviceComm_, &commRank) ; |
---|
[2517] | 223 | winNotify_->lockWindowExclusive(commRank) ; |
---|
| 224 | winNotify_->updateFromLockedWindow(commRank, this, &CService::createContextDumpIn) ; |
---|
[1761] | 225 | |
---|
| 226 | if (!notifications_.empty()) |
---|
| 227 | { |
---|
| 228 | auto info = notifications_.front() ; |
---|
| 229 | createNewContext(get<0>(info), get<1>(info), get<2>(info), get<3>(info)) ; |
---|
| 230 | notifications_.pop_front() ; |
---|
[2517] | 231 | winNotify_->updateToLockedWindow(commRank, this, &CService::createContextDumpOut) ; |
---|
[1761] | 232 | } |
---|
[2517] | 233 | winNotify_->unlockWindowExclusive(commRank) ; |
---|
[1761] | 234 | } |
---|
| 235 | |
---|
[1764] | 236 | void CService::createContext(void) |
---|
| 237 | { |
---|
[2246] | 238 | info(40)<<"CService::createContext(void) : receive createContext notification"<<endl ; |
---|
[1764] | 239 | auto& arg=notifyInCreateContext_ ; |
---|
| 240 | string poolId = get<0>(arg) ; |
---|
[2274] | 241 | string serviceId = get<1>(arg) ; |
---|
[1764] | 242 | int partitionId = get<2>(arg) ; |
---|
| 243 | string contextId = get<3>(arg) ; |
---|
[2274] | 244 | contexts_[contextId] = new CServerContext(this, serviceComm_, poolId, serviceId, partitionId, contextId) ; |
---|
[1764] | 245 | } |
---|
| 246 | |
---|
[2246] | 247 | //to remove, not used anymore |
---|
[1761] | 248 | void CService::createNewContext(const std::string& poolId, const std::string& serviceId, const int& partitionId, const std::string& contextId) |
---|
| 249 | { |
---|
| 250 | contexts_[contextId] = new CServerContext(this, serviceComm_, poolId, serviceId, partitionId, contextId) ; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | void CService::finalizeSignal(void) |
---|
| 254 | { |
---|
| 255 | finalizeSignal_=true ; |
---|
| 256 | for(auto it=contexts_.begin();it!=contexts_.end();++it) it->second->finalizeSignal() ; |
---|
| 257 | } |
---|
| 258 | |
---|
[2404] | 259 | shared_ptr<CEventScheduler> CService::getEventScheduler(void) |
---|
[1761] | 260 | { |
---|
| 261 | return eventScheduler_ ; |
---|
| 262 | } |
---|
[2287] | 263 | } |
---|