[2343] | 1 | #include "one_sided_context_server.hpp" |
---|
| 2 | #include "buffer_in.hpp" |
---|
| 3 | #include "type.hpp" |
---|
| 4 | #include "context.hpp" |
---|
| 5 | #include "object_template.hpp" |
---|
| 6 | #include "group_template.hpp" |
---|
| 7 | #include "attribute_template.hpp" |
---|
| 8 | #include "domain.hpp" |
---|
| 9 | #include "field.hpp" |
---|
| 10 | #include "file.hpp" |
---|
| 11 | #include "grid.hpp" |
---|
| 12 | #include "mpi.hpp" |
---|
| 13 | #include "tracer.hpp" |
---|
| 14 | #include "timer.hpp" |
---|
| 15 | #include "cxios.hpp" |
---|
| 16 | #include "event_scheduler.hpp" |
---|
| 17 | #include "server.hpp" |
---|
| 18 | #include "servers_ressource.hpp" |
---|
| 19 | #include "pool_ressource.hpp" |
---|
| 20 | #include "services.hpp" |
---|
| 21 | #include "contexts_manager.hpp" |
---|
| 22 | #include "timeline_events.hpp" |
---|
| 23 | |
---|
| 24 | #include <boost/functional/hash.hpp> |
---|
| 25 | #include <random> |
---|
| 26 | #include <chrono> |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | namespace xios |
---|
| 30 | { |
---|
| 31 | using namespace std ; |
---|
| 32 | |
---|
| 33 | COneSidedContextServer::COneSidedContextServer(CContext* parent,MPI_Comm intraComm_,MPI_Comm interComm_) |
---|
| 34 | : CContextServer(parent, intraComm_, interComm_), |
---|
| 35 | isProcessingEvent_(false) |
---|
| 36 | { |
---|
| 37 | |
---|
| 38 | MPI_Comm_dup(intraComm, &processEventBarrier_) ; |
---|
| 39 | |
---|
| 40 | currentTimeLine=1; |
---|
| 41 | scheduled=false; |
---|
| 42 | finished=false; |
---|
| 43 | |
---|
| 44 | if (!isAttachedModeEnabled()) MPI_Intercomm_merge(interComm_,true,&interCommMerged_) ; |
---|
| 45 | MPI_Comm_split(intraComm_, intraCommRank, intraCommRank, &commSelf_) ; // for windows |
---|
| 46 | |
---|
| 47 | itLastTimeLine=lastTimeLine.begin() ; |
---|
| 48 | |
---|
| 49 | pureOneSided=CXios::getin<bool>("pure_one_sided",false); // pure one sided communication (for test) |
---|
| 50 | if (isAttachedModeEnabled()) pureOneSided=false ; // no one sided in attach mode |
---|
| 51 | |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | void COneSidedContextServer::setPendingEvent(void) |
---|
| 55 | { |
---|
| 56 | pendingEvent=true; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | bool COneSidedContextServer::hasPendingEvent(void) |
---|
| 60 | { |
---|
| 61 | return pendingEvent; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | bool COneSidedContextServer::hasFinished(void) |
---|
| 65 | { |
---|
| 66 | return finished; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | bool COneSidedContextServer::eventLoop(bool enableEventsProcessing /*= true*/) |
---|
| 70 | { |
---|
| 71 | CTimer::get("listen request").resume(); |
---|
| 72 | listen(); |
---|
| 73 | CTimer::get("listen request").suspend(); |
---|
| 74 | |
---|
| 75 | CTimer::get("listen pending request").resume(); |
---|
| 76 | listenPendingRequest() ; |
---|
| 77 | CTimer::get("listen pending request").suspend(); |
---|
| 78 | |
---|
| 79 | CTimer::get("check server Buffers").resume(); |
---|
| 80 | checkBuffers() ; |
---|
| 81 | CTimer::get("check server Buffers").suspend(); |
---|
| 82 | |
---|
| 83 | CTimer::get("check event process").resume(); |
---|
| 84 | processEvents(enableEventsProcessing); |
---|
| 85 | CTimer::get("check event process").suspend(); |
---|
| 86 | return finished; |
---|
| 87 | |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | void COneSidedContextServer::listen(void) |
---|
| 91 | { |
---|
| 92 | int rank; |
---|
| 93 | int flag; |
---|
| 94 | MPI_Status status; |
---|
| 95 | |
---|
| 96 | traceOff(); |
---|
| 97 | MPI_Iprobe(MPI_ANY_SOURCE, 20,interComm, &flag, &status); |
---|
| 98 | traceOn(); |
---|
| 99 | if (flag==true) |
---|
| 100 | { |
---|
| 101 | requests_.push_back(CRequest(interComm, status)) ; |
---|
| 102 | if (requests_.back().test()) |
---|
| 103 | { |
---|
| 104 | processRequest(requests_.back()) ; |
---|
| 105 | requests_.pop_back() ; |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void COneSidedContextServer::listenPendingRequest(void) |
---|
| 111 | { |
---|
| 112 | auto it = requests_.begin() ; |
---|
| 113 | while (it != requests_.end()) |
---|
| 114 | { |
---|
| 115 | if (it->test()) |
---|
| 116 | { |
---|
| 117 | processRequest(*it) ; |
---|
| 118 | auto it2=it ; |
---|
| 119 | ++it ; |
---|
| 120 | requests_.erase(it2) ; |
---|
| 121 | } |
---|
| 122 | else ++it ; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | void COneSidedContextServer::processRequest(CRequest& request) |
---|
| 127 | { |
---|
| 128 | int rank = request.getRank() ; |
---|
| 129 | auto it=buffers_.find(rank); |
---|
| 130 | if (it==buffers_.end()) |
---|
| 131 | { |
---|
| 132 | buffers_[rank] = new COneSidedServerBuffer(rank, commSelf_, interCommMerged_, pendingEvents_, completedEvents_, request.getBuffer()) ; |
---|
| 133 | } |
---|
| 134 | else it->second->receivedRequest(request.getBuffer()) ; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | void COneSidedContextServer::checkBuffers(void) |
---|
| 138 | { |
---|
| 139 | if (!pendingEvents_.empty()) |
---|
| 140 | { |
---|
| 141 | SPendingEvent& nextEvent = pendingEvents_.begin()->second ; |
---|
| 142 | for(auto& buffer : nextEvent.buffers ) buffer->eventLoop() ; |
---|
| 143 | if (nextEvent.nbSenders==0) pendingEvents_.erase(pendingEvents_.begin()) ; |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | void COneSidedContextServer::processEvents(bool enableEventsProcessing) |
---|
| 149 | { |
---|
| 150 | |
---|
| 151 | if (isProcessingEvent_) return ; |
---|
| 152 | if (isAttachedModeEnabled()) |
---|
| 153 | if (!CXios::getDaemonsManager()->isScheduledContext(remoteHashId_)) return ; |
---|
| 154 | |
---|
| 155 | auto it=completedEvents_.find(currentTimeLine); |
---|
| 156 | |
---|
| 157 | if (it!=completedEvents_.end()) |
---|
| 158 | { |
---|
| 159 | if (it->second.nbSenders == it->second.currentNbSenders) |
---|
| 160 | { |
---|
| 161 | if (!scheduled && !isAttachedModeEnabled()) // Skip event scheduling for attached mode and reception on client side |
---|
| 162 | { |
---|
| 163 | eventScheduler_->registerEvent(currentTimeLine,hashId); |
---|
| 164 | scheduled=true; |
---|
| 165 | } |
---|
| 166 | else if (isAttachedModeEnabled() || eventScheduler_->queryEvent(currentTimeLine,hashId) ) |
---|
| 167 | { |
---|
| 168 | //if (!enableEventsProcessing && isCollectiveEvent(event)) return ; |
---|
| 169 | |
---|
| 170 | if (!eventScheduled_) |
---|
| 171 | { |
---|
| 172 | MPI_Ibarrier(processEventBarrier_,&processEventRequest_) ; |
---|
| 173 | eventScheduled_=true ; |
---|
| 174 | return ; |
---|
| 175 | } |
---|
| 176 | else |
---|
| 177 | { |
---|
| 178 | MPI_Status status ; |
---|
| 179 | int flag ; |
---|
| 180 | MPI_Test(&processEventRequest_, &flag, &status) ; |
---|
| 181 | if (!flag) return ; |
---|
| 182 | eventScheduled_=false ; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | if (!isAttachedModeEnabled()) eventScheduler_->popEvent() ; |
---|
| 186 | |
---|
| 187 | isProcessingEvent_=true ; |
---|
| 188 | CEventServer event(this) ; |
---|
| 189 | for(auto& buffer : it->second.buffers) buffer->fillEventServer(currentTimeLine, event) ; |
---|
| 190 | |
---|
| 191 | CTimer::get("Process events").resume(); |
---|
| 192 | info(100)<<"Context id "<<context->getId()<<" : Process Event "<<currentTimeLine<<" of class "<<event.classId<<" of type "<<event.type<<endl ; |
---|
| 193 | dispatchEvent(event); |
---|
| 194 | CTimer::get("Process events").suspend(); |
---|
| 195 | isProcessingEvent_=false ; |
---|
| 196 | // context->unsetProcessingEvent() ; |
---|
| 197 | pendingEvent=false; |
---|
| 198 | completedEvents_.erase(it); |
---|
| 199 | currentTimeLine++; |
---|
| 200 | scheduled = false; |
---|
| 201 | if (isAttachedModeEnabled()) CXios::getDaemonsManager()->unscheduleContext() ; |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | COneSidedContextServer::~COneSidedContextServer() |
---|
| 208 | { |
---|
| 209 | for(auto& buffer : buffers_) delete buffer.second; |
---|
| 210 | buffers_.clear() ; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | void COneSidedContextServer::releaseBuffers() |
---|
| 214 | { |
---|
| 215 | //for(auto it=buffers.begin();it!=buffers.end();++it) delete it->second ; |
---|
| 216 | //buffers.clear() ; |
---|
| 217 | freeWindows() ; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | void COneSidedContextServer::freeWindows() |
---|
| 221 | { |
---|
| 222 | //if (!isAttachedModeEnabled()) |
---|
| 223 | //{ |
---|
| 224 | // for(auto& it : winComm_) |
---|
| 225 | // { |
---|
| 226 | // int rank = it.first ; |
---|
| 227 | // MPI_Win_free(&windows_[rank][0]); |
---|
| 228 | // MPI_Win_free(&windows_[rank][1]); |
---|
| 229 | // MPI_Comm_free(&winComm_[rank]) ; |
---|
| 230 | // } |
---|
| 231 | //} |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | void COneSidedContextServer::notifyClientsFinalize(void) |
---|
| 235 | { |
---|
| 236 | for(auto it=buffers_.begin();it!=buffers_.end();++it) |
---|
| 237 | { |
---|
| 238 | it->second->notifyClientFinalize() ; |
---|
| 239 | } |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | void COneSidedContextServer::dispatchEvent(CEventServer& event) |
---|
| 243 | { |
---|
| 244 | string contextName; |
---|
| 245 | string buff; |
---|
| 246 | int MsgSize; |
---|
| 247 | int rank; |
---|
| 248 | list<CEventServer::SSubEvent>::iterator it; |
---|
| 249 | StdString ctxId = context->getId(); |
---|
| 250 | CContext::setCurrent(ctxId); |
---|
| 251 | StdSize totalBuf = 0; |
---|
| 252 | |
---|
| 253 | if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_FINALIZE) |
---|
| 254 | { |
---|
| 255 | finished=true; |
---|
| 256 | info(20)<<" COneSidedContextServer: Receive context <"<<context->getId()<<"> finalize."<<endl; |
---|
| 257 | notifyClientsFinalize() ; |
---|
| 258 | CTimer::get("receiving requests").suspend(); |
---|
| 259 | context->finalize(); |
---|
| 260 | |
---|
| 261 | std::map<int, StdSize>::const_iterator itbMap = mapBufferSize_.begin(), |
---|
| 262 | iteMap = mapBufferSize_.end(), itMap; |
---|
| 263 | for (itMap = itbMap; itMap != iteMap; ++itMap) |
---|
| 264 | { |
---|
| 265 | rank = itMap->first; |
---|
| 266 | report(10)<< " Memory report : Context <"<<ctxId<<"> : server side : memory used for buffer of each connection to client" << endl |
---|
| 267 | << " +) With client of rank " << rank << " : " << itMap->second << " bytes " << endl; |
---|
| 268 | totalBuf += itMap->second; |
---|
| 269 | } |
---|
| 270 | report(0)<< " Memory report : Context <"<<ctxId<<"> : server side : total memory used for buffer "<<totalBuf<<" bytes"<<endl; |
---|
| 271 | } |
---|
| 272 | else if (event.classId==CContext::GetType()) CContext::dispatchEvent(event); |
---|
| 273 | else if (event.classId==CContextGroup::GetType()) CContextGroup::dispatchEvent(event); |
---|
| 274 | else if (event.classId==CCalendarWrapper::GetType()) CCalendarWrapper::dispatchEvent(event); |
---|
| 275 | else if (event.classId==CDomain::GetType()) CDomain::dispatchEvent(event); |
---|
| 276 | else if (event.classId==CDomainGroup::GetType()) CDomainGroup::dispatchEvent(event); |
---|
| 277 | else if (event.classId==CAxis::GetType()) CAxis::dispatchEvent(event); |
---|
| 278 | else if (event.classId==CAxisGroup::GetType()) CAxisGroup::dispatchEvent(event); |
---|
| 279 | else if (event.classId==CScalar::GetType()) CScalar::dispatchEvent(event); |
---|
| 280 | else if (event.classId==CScalarGroup::GetType()) CScalarGroup::dispatchEvent(event); |
---|
| 281 | else if (event.classId==CGrid::GetType()) CGrid::dispatchEvent(event); |
---|
| 282 | else if (event.classId==CGridGroup::GetType()) CGridGroup::dispatchEvent(event); |
---|
| 283 | else if (event.classId==CField::GetType()) CField::dispatchEvent(event); |
---|
| 284 | else if (event.classId==CFieldGroup::GetType()) CFieldGroup::dispatchEvent(event); |
---|
| 285 | else if (event.classId==CFile::GetType()) CFile::dispatchEvent(event); |
---|
| 286 | else if (event.classId==CFileGroup::GetType()) CFileGroup::dispatchEvent(event); |
---|
| 287 | else if (event.classId==CVariable::GetType()) CVariable::dispatchEvent(event); |
---|
| 288 | else |
---|
| 289 | { |
---|
| 290 | ERROR("void COneSidedContextServer::dispatchEvent(CEventServer& event)",<<" Bad event class Id"<<endl); |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | bool COneSidedContextServer::isCollectiveEvent(CEventServer& event) |
---|
| 295 | { |
---|
| 296 | if (event.type>1000) return false ; |
---|
| 297 | else return true ; |
---|
| 298 | } |
---|
| 299 | } |
---|