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