[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" |
---|
| 7 | |
---|
| 8 | namespace xios |
---|
| 9 | { |
---|
| 10 | CService::CService(MPI_Comm serviceComm, const std::string& poolId, const std::string& serviceId, const int& partitionId, |
---|
| 11 | int type, int nbPartitions) : finalizeSignal_(false), eventScheduler_(nullptr), poolId_(poolId), serviceId_(serviceId), |
---|
[1764] | 12 | partitionId_(partitionId), type_(type), nbPartitions_(nbPartitions), hasNotification_(false) |
---|
[1761] | 13 | |
---|
| 14 | |
---|
| 15 | { |
---|
| 16 | int localRank, globalRank, commSize ; |
---|
| 17 | |
---|
| 18 | MPI_Comm_dup(serviceComm, &serviceComm_) ; |
---|
| 19 | MPI_Comm globalComm_=CXios::getXiosComm() ; |
---|
| 20 | |
---|
| 21 | MPI_Comm_rank(globalComm_,&globalRank) ; |
---|
| 22 | MPI_Comm_rank(serviceComm_,&localRank) ; |
---|
| 23 | |
---|
| 24 | winNotify_ = new CWindowManager(serviceComm_, maxBufferSize_) ; |
---|
| 25 | winNotify_->lockWindow(localRank,0) ; |
---|
| 26 | winNotify_->updateToWindow(localRank, this, &CService::createContextDumpOut) ; |
---|
| 27 | winNotify_->unlockWindow(localRank,0) ; |
---|
[2220] | 28 | MPI_Barrier(serviceComm_) ; |
---|
[1761] | 29 | if (localRank==localLeader_) |
---|
| 30 | { |
---|
| 31 | globalLeader_=globalRank ; |
---|
| 32 | MPI_Comm_rank(serviceComm_,&commSize) ; |
---|
| 33 | CXios::getServicesManager()->registerService(poolId, serviceId, partitionId, type, commSize, nbPartitions, globalLeader_) ; |
---|
| 34 | } |
---|
| 35 | eventScheduler_ = new CEventScheduler(serviceComm_) ; |
---|
[1764] | 36 | |
---|
| 37 | ostringstream oss; |
---|
| 38 | oss<<partitionId; |
---|
| 39 | name_= poolId+"::"+serviceId+"_"+oss.str(); |
---|
[1761] | 40 | } |
---|
| 41 | |
---|
| 42 | void CService::createContext( const std::string& poolId, const std::string& serviceId, const int& partitionId, const std::string& contextId) |
---|
| 43 | { |
---|
| 44 | int commSize ; |
---|
| 45 | MPI_Comm_size(serviceComm_, &commSize) ; |
---|
| 46 | |
---|
[1764] | 47 | for(int rank=0; rank<commSize; rank++) |
---|
| 48 | { |
---|
| 49 | notifyOutType_=NOTIFY_CREATE_CONTEXT ; |
---|
| 50 | notifyOutCreateContext_ = make_tuple(poolId, serviceId, partitionId, contextId) ; |
---|
| 51 | sendNotification(rank) ; |
---|
| 52 | } |
---|
[1761] | 53 | } |
---|
| 54 | /* |
---|
| 55 | void CService::createContext(const std::string& contextId) |
---|
| 56 | { |
---|
| 57 | contexts_[contextId] = new CServerContext(this, serviceComm_, poolId_, serviceId_, partitionId_, contextId) ; |
---|
| 58 | } |
---|
| 59 | */ |
---|
| 60 | void CService::createContextNotify(int rank, const std::string& poolId, const std::string& serviceId, const int& partitionId, const std::string& contextId) |
---|
| 61 | { |
---|
| 62 | winNotify_->lockWindow(rank,0) ; |
---|
| 63 | winNotify_->updateFromWindow(rank, this, &CService::createContextDumpIn) ; |
---|
| 64 | notifications_.push_back(std::make_tuple(poolId, serviceId, partitionId, contextId)) ; |
---|
| 65 | winNotify_->updateToWindow(rank, this, &CService::createContextDumpOut) ; |
---|
| 66 | winNotify_->unlockWindow(rank,0) ; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | void CService::createContextDumpOut(CBufferOut& buffer) |
---|
| 71 | { |
---|
| 72 | buffer.realloc(maxBufferSize_) ; |
---|
| 73 | |
---|
| 74 | buffer << (int) (notifications_.size()); |
---|
| 75 | |
---|
| 76 | for(auto it=notifications_.begin();it!=notifications_.end(); ++it) |
---|
| 77 | buffer << std::get<0>(*it) << std::get<1>(*it) << std::get<2>(*it) << std::get<3>(*it) ; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | void CService::createContextDumpIn(CBufferIn& buffer) |
---|
| 82 | { |
---|
| 83 | std::string poolId ; |
---|
| 84 | std::string serviceId ; |
---|
| 85 | int partitionId ; |
---|
| 86 | std::string contextId ; |
---|
| 87 | |
---|
| 88 | notifications_.clear() ; |
---|
| 89 | int nbNotifications ; |
---|
| 90 | buffer>>nbNotifications ; |
---|
| 91 | for(int i=0;i<nbNotifications;i++) |
---|
| 92 | { |
---|
| 93 | buffer>>poolId>>serviceId>>partitionId>>contextId ; |
---|
| 94 | notifications_.push_back(std::make_tuple(poolId, serviceId, partitionId, contextId)) ; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | |
---|
[1764] | 98 | bool CService::eventLoop(bool serviceOnly) |
---|
[1761] | 99 | { |
---|
[1764] | 100 | //checkCreateContextNotification() ; |
---|
| 101 | checkNotifications() ; |
---|
| 102 | |
---|
[1761] | 103 | eventScheduler_->checkEvent() ; |
---|
| 104 | for(auto it=contexts_.begin();it!=contexts_.end();++it) |
---|
| 105 | { |
---|
[1764] | 106 | if (it->second->eventLoop(serviceOnly)) |
---|
[1761] | 107 | { |
---|
| 108 | contexts_.erase(it) ; |
---|
| 109 | // destroy server_context -> to do later |
---|
| 110 | break ; |
---|
| 111 | } ; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | if (contexts_.empty() && finalizeSignal_) return true ; |
---|
| 115 | else return false ; |
---|
| 116 | } |
---|
| 117 | |
---|
[1764] | 118 | void CService::sendNotification(int rank) |
---|
| 119 | { |
---|
| 120 | winNotify_->lockWindow(rank,0) ; |
---|
| 121 | winNotify_->pushToWindow(rank, this, &CService::notificationsDumpOut) ; |
---|
| 122 | winNotify_->unlockWindow(rank,0) ; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | void CService::notificationsDumpOut(CBufferOut& buffer) |
---|
| 127 | { |
---|
| 128 | |
---|
| 129 | buffer.realloc(maxBufferSize_) ; |
---|
| 130 | |
---|
| 131 | if (notifyOutType_==NOTIFY_CREATE_CONTEXT) |
---|
| 132 | { |
---|
| 133 | auto& arg=notifyOutCreateContext_ ; |
---|
| 134 | buffer << notifyOutType_ << std::get<0>(arg)<<std::get<1>(arg) << std::get<2>(arg)<<std::get<3>(arg) ; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | void CService::notificationsDumpIn(CBufferIn& buffer) |
---|
| 139 | { |
---|
| 140 | if (buffer.bufferSize() == 0) notifyInType_= NOTIFY_NOTHING ; |
---|
| 141 | else |
---|
| 142 | { |
---|
| 143 | buffer>>notifyInType_; |
---|
| 144 | if (notifyInType_==NOTIFY_CREATE_CONTEXT) |
---|
| 145 | { |
---|
| 146 | info(10)<<"NotifyDumpOut"<<endl ; |
---|
| 147 | auto& arg=notifyInCreateContext_ ; |
---|
| 148 | buffer >> std::get<0>(arg)>> std::get<1>(arg) >> std::get<2>(arg)>> std::get<3>(arg); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | |
---|
| 156 | void CService::checkNotifications(void) |
---|
| 157 | { |
---|
| 158 | if (!hasNotification_) |
---|
| 159 | { |
---|
| 160 | int commRank ; |
---|
| 161 | MPI_Comm_rank(serviceComm_, &commRank) ; |
---|
| 162 | winNotify_->lockWindow(commRank,0) ; |
---|
| 163 | winNotify_->popFromWindow(commRank, this, &CService::notificationsDumpIn) ; |
---|
| 164 | winNotify_->unlockWindow(commRank,0) ; |
---|
| 165 | |
---|
| 166 | if (notifyInType_!= NOTIFY_NOTHING) |
---|
| 167 | { |
---|
| 168 | hasNotification_=true ; |
---|
| 169 | std::hash<string> hashString ; |
---|
| 170 | size_t hashId = hashString(name_) ; |
---|
| 171 | size_t currentTimeLine=0 ; |
---|
| 172 | eventScheduler_->registerEvent(currentTimeLine,hashId); |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | if (hasNotification_) |
---|
| 177 | { |
---|
| 178 | std::hash<string> hashString ; |
---|
| 179 | size_t hashId = hashString(name_) ; |
---|
| 180 | size_t currentTimeLine=0 ; |
---|
| 181 | if (eventScheduler_->queryEvent(currentTimeLine,hashId)) |
---|
| 182 | { |
---|
| 183 | if (notifyInType_==NOTIFY_CREATE_CONTEXT) createContext() ; |
---|
| 184 | hasNotification_=false ; |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | |
---|
[1761] | 192 | void CService::checkCreateContextNotification(void) |
---|
| 193 | { |
---|
| 194 | int commRank ; |
---|
| 195 | MPI_Comm_rank(serviceComm_, &commRank) ; |
---|
| 196 | winNotify_->lockWindow(commRank,0) ; |
---|
| 197 | winNotify_->updateFromWindow(commRank, this, &CService::createContextDumpIn) ; |
---|
| 198 | |
---|
| 199 | if (!notifications_.empty()) |
---|
| 200 | { |
---|
| 201 | auto info = notifications_.front() ; |
---|
| 202 | createNewContext(get<0>(info), get<1>(info), get<2>(info), get<3>(info)) ; |
---|
| 203 | notifications_.pop_front() ; |
---|
| 204 | winNotify_->updateToWindow(commRank, this, &CService::createContextDumpOut) ; |
---|
| 205 | } |
---|
| 206 | winNotify_->unlockWindow(commRank,0) ; |
---|
| 207 | } |
---|
| 208 | |
---|
[1764] | 209 | void CService::createContext(void) |
---|
| 210 | { |
---|
| 211 | auto& arg=notifyInCreateContext_ ; |
---|
| 212 | string poolId = get<0>(arg) ; |
---|
| 213 | string& serviceId = get<1>(arg) ; |
---|
| 214 | int partitionId = get<2>(arg) ; |
---|
| 215 | string contextId = get<3>(arg) ; |
---|
| 216 | contexts_[contextId] = new CServerContext(this, serviceComm_, poolId, serviceId, partitionId, contextId) ; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | //to remove |
---|
[1761] | 220 | void CService::createNewContext(const std::string& poolId, const std::string& serviceId, const int& partitionId, const std::string& contextId) |
---|
| 221 | { |
---|
| 222 | contexts_[contextId] = new CServerContext(this, serviceComm_, poolId, serviceId, partitionId, contextId) ; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | void CService::finalizeSignal(void) |
---|
| 226 | { |
---|
| 227 | finalizeSignal_=true ; |
---|
| 228 | for(auto it=contexts_.begin();it!=contexts_.end();++it) it->second->finalizeSignal() ; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | CEventScheduler* CService::getEventScheduler(void) |
---|
| 232 | { |
---|
| 233 | return eventScheduler_ ; |
---|
| 234 | } |
---|
| 235 | } |
---|