[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; |
---|
[2399] | 95 | flag=true ; |
---|
| 96 | |
---|
| 97 | while(flag) |
---|
[2343] | 98 | { |
---|
[2399] | 99 | traceOff(); |
---|
[2524] | 100 | MPI_Iprobe(MPI_ANY_SOURCE, 20,interCommMerged_, &flag, &status); |
---|
[2399] | 101 | traceOn(); |
---|
| 102 | if (flag==true) |
---|
[2343] | 103 | { |
---|
[2525] | 104 | requests_.push_back(new CRequest(interCommMerged_, status)) ; |
---|
| 105 | if (requests_.back()->test()) |
---|
[2399] | 106 | { |
---|
[2525] | 107 | processRequest(*(requests_.back())) ; |
---|
| 108 | delete requests_.back(); |
---|
[2399] | 109 | requests_.pop_back() ; |
---|
| 110 | } |
---|
[2343] | 111 | } |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | void COneSidedContextServer::listenPendingRequest(void) |
---|
| 116 | { |
---|
| 117 | auto it = requests_.begin() ; |
---|
| 118 | while (it != requests_.end()) |
---|
| 119 | { |
---|
[2525] | 120 | if ((*it)->test()) |
---|
[2343] | 121 | { |
---|
[2525] | 122 | processRequest(*(*it)) ; |
---|
| 123 | delete (*it); |
---|
[2343] | 124 | auto it2=it ; |
---|
| 125 | ++it ; |
---|
| 126 | requests_.erase(it2) ; |
---|
| 127 | } |
---|
| 128 | else ++it ; |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | void COneSidedContextServer::processRequest(CRequest& request) |
---|
| 133 | { |
---|
| 134 | int rank = request.getRank() ; |
---|
| 135 | auto it=buffers_.find(rank); |
---|
| 136 | if (it==buffers_.end()) |
---|
| 137 | { |
---|
| 138 | buffers_[rank] = new COneSidedServerBuffer(rank, commSelf_, interCommMerged_, pendingEvents_, completedEvents_, request.getBuffer()) ; |
---|
| 139 | } |
---|
| 140 | else it->second->receivedRequest(request.getBuffer()) ; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | void COneSidedContextServer::checkBuffers(void) |
---|
| 144 | { |
---|
| 145 | if (!pendingEvents_.empty()) |
---|
| 146 | { |
---|
[2399] | 147 | /* |
---|
[2343] | 148 | SPendingEvent& nextEvent = pendingEvents_.begin()->second ; |
---|
| 149 | for(auto& buffer : nextEvent.buffers ) buffer->eventLoop() ; |
---|
| 150 | if (nextEvent.nbSenders==0) pendingEvents_.erase(pendingEvents_.begin()) ; |
---|
[2399] | 151 | */ |
---|
| 152 | for(auto it=pendingEvents_.begin() ; it!=pendingEvents_.end() ;) |
---|
| 153 | { |
---|
| 154 | SPendingEvent& nextEvent = it->second ; |
---|
| 155 | for(auto& buffer : nextEvent.buffers ) buffer->eventLoop() ; |
---|
| 156 | if (nextEvent.nbSenders==0) it=pendingEvents_.erase(it) ; |
---|
| 157 | else ++it ; |
---|
| 158 | } |
---|
[2343] | 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | void COneSidedContextServer::processEvents(bool enableEventsProcessing) |
---|
| 164 | { |
---|
| 165 | |
---|
| 166 | if (isProcessingEvent_) return ; |
---|
| 167 | if (isAttachedModeEnabled()) |
---|
| 168 | if (!CXios::getDaemonsManager()->isScheduledContext(remoteHashId_)) return ; |
---|
| 169 | |
---|
| 170 | auto it=completedEvents_.find(currentTimeLine); |
---|
| 171 | |
---|
| 172 | if (it!=completedEvents_.end()) |
---|
| 173 | { |
---|
| 174 | if (it->second.nbSenders == it->second.currentNbSenders) |
---|
| 175 | { |
---|
| 176 | if (!scheduled && !isAttachedModeEnabled()) // Skip event scheduling for attached mode and reception on client side |
---|
| 177 | { |
---|
| 178 | eventScheduler_->registerEvent(currentTimeLine,hashId); |
---|
| 179 | scheduled=true; |
---|
| 180 | } |
---|
| 181 | else if (isAttachedModeEnabled() || eventScheduler_->queryEvent(currentTimeLine,hashId) ) |
---|
| 182 | { |
---|
| 183 | //if (!enableEventsProcessing && isCollectiveEvent(event)) return ; |
---|
| 184 | |
---|
| 185 | if (!eventScheduled_) |
---|
| 186 | { |
---|
| 187 | MPI_Ibarrier(processEventBarrier_,&processEventRequest_) ; |
---|
| 188 | eventScheduled_=true ; |
---|
| 189 | return ; |
---|
| 190 | } |
---|
| 191 | else |
---|
| 192 | { |
---|
| 193 | MPI_Status status ; |
---|
| 194 | int flag ; |
---|
| 195 | MPI_Test(&processEventRequest_, &flag, &status) ; |
---|
| 196 | if (!flag) return ; |
---|
| 197 | eventScheduled_=false ; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | if (!isAttachedModeEnabled()) eventScheduler_->popEvent() ; |
---|
| 201 | |
---|
| 202 | isProcessingEvent_=true ; |
---|
| 203 | CEventServer event(this) ; |
---|
| 204 | for(auto& buffer : it->second.buffers) buffer->fillEventServer(currentTimeLine, event) ; |
---|
[2399] | 205 | MPI_Barrier(intraComm) ; |
---|
[2343] | 206 | CTimer::get("Process events").resume(); |
---|
| 207 | info(100)<<"Context id "<<context->getId()<<" : Process Event "<<currentTimeLine<<" of class "<<event.classId<<" of type "<<event.type<<endl ; |
---|
| 208 | dispatchEvent(event); |
---|
| 209 | CTimer::get("Process events").suspend(); |
---|
| 210 | isProcessingEvent_=false ; |
---|
| 211 | // context->unsetProcessingEvent() ; |
---|
| 212 | pendingEvent=false; |
---|
| 213 | completedEvents_.erase(it); |
---|
| 214 | currentTimeLine++; |
---|
| 215 | scheduled = false; |
---|
| 216 | if (isAttachedModeEnabled()) CXios::getDaemonsManager()->unscheduleContext() ; |
---|
| 217 | } |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | COneSidedContextServer::~COneSidedContextServer() |
---|
| 223 | { |
---|
| 224 | for(auto& buffer : buffers_) delete buffer.second; |
---|
| 225 | buffers_.clear() ; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | void COneSidedContextServer::releaseBuffers() |
---|
| 229 | { |
---|
| 230 | //for(auto it=buffers.begin();it!=buffers.end();++it) delete it->second ; |
---|
| 231 | //buffers.clear() ; |
---|
| 232 | freeWindows() ; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | void COneSidedContextServer::freeWindows() |
---|
| 236 | { |
---|
| 237 | //if (!isAttachedModeEnabled()) |
---|
| 238 | //{ |
---|
| 239 | // for(auto& it : winComm_) |
---|
| 240 | // { |
---|
| 241 | // int rank = it.first ; |
---|
| 242 | // MPI_Win_free(&windows_[rank][0]); |
---|
| 243 | // MPI_Win_free(&windows_[rank][1]); |
---|
| 244 | // MPI_Comm_free(&winComm_[rank]) ; |
---|
| 245 | // } |
---|
| 246 | //} |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | void COneSidedContextServer::notifyClientsFinalize(void) |
---|
| 250 | { |
---|
| 251 | for(auto it=buffers_.begin();it!=buffers_.end();++it) |
---|
| 252 | { |
---|
| 253 | it->second->notifyClientFinalize() ; |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | void COneSidedContextServer::dispatchEvent(CEventServer& event) |
---|
| 258 | { |
---|
| 259 | string contextName; |
---|
| 260 | string buff; |
---|
| 261 | int MsgSize; |
---|
| 262 | int rank; |
---|
| 263 | list<CEventServer::SSubEvent>::iterator it; |
---|
| 264 | StdString ctxId = context->getId(); |
---|
| 265 | CContext::setCurrent(ctxId); |
---|
| 266 | StdSize totalBuf = 0; |
---|
| 267 | |
---|
| 268 | if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_FINALIZE) |
---|
| 269 | { |
---|
| 270 | finished=true; |
---|
| 271 | info(20)<<" COneSidedContextServer: Receive context <"<<context->getId()<<"> finalize."<<endl; |
---|
| 272 | notifyClientsFinalize() ; |
---|
| 273 | CTimer::get("receiving requests").suspend(); |
---|
| 274 | context->finalize(); |
---|
| 275 | |
---|
| 276 | std::map<int, StdSize>::const_iterator itbMap = mapBufferSize_.begin(), |
---|
| 277 | iteMap = mapBufferSize_.end(), itMap; |
---|
| 278 | for (itMap = itbMap; itMap != iteMap; ++itMap) |
---|
| 279 | { |
---|
| 280 | rank = itMap->first; |
---|
| 281 | report(10)<< " Memory report : Context <"<<ctxId<<"> : server side : memory used for buffer of each connection to client" << endl |
---|
| 282 | << " +) With client of rank " << rank << " : " << itMap->second << " bytes " << endl; |
---|
| 283 | totalBuf += itMap->second; |
---|
| 284 | } |
---|
| 285 | report(0)<< " Memory report : Context <"<<ctxId<<"> : server side : total memory used for buffer "<<totalBuf<<" bytes"<<endl; |
---|
| 286 | } |
---|
| 287 | else if (event.classId==CContext::GetType()) CContext::dispatchEvent(event); |
---|
| 288 | else if (event.classId==CContextGroup::GetType()) CContextGroup::dispatchEvent(event); |
---|
| 289 | else if (event.classId==CCalendarWrapper::GetType()) CCalendarWrapper::dispatchEvent(event); |
---|
| 290 | else if (event.classId==CDomain::GetType()) CDomain::dispatchEvent(event); |
---|
| 291 | else if (event.classId==CDomainGroup::GetType()) CDomainGroup::dispatchEvent(event); |
---|
| 292 | else if (event.classId==CAxis::GetType()) CAxis::dispatchEvent(event); |
---|
| 293 | else if (event.classId==CAxisGroup::GetType()) CAxisGroup::dispatchEvent(event); |
---|
| 294 | else if (event.classId==CScalar::GetType()) CScalar::dispatchEvent(event); |
---|
| 295 | else if (event.classId==CScalarGroup::GetType()) CScalarGroup::dispatchEvent(event); |
---|
| 296 | else if (event.classId==CGrid::GetType()) CGrid::dispatchEvent(event); |
---|
| 297 | else if (event.classId==CGridGroup::GetType()) CGridGroup::dispatchEvent(event); |
---|
[2399] | 298 | else if (event.classId==CField::GetType()) |
---|
| 299 | { |
---|
| 300 | if (event.type==CField::EVENT_ID_UPDATE_DATA) CField::dispatchEvent(event); |
---|
| 301 | else CField::dispatchEvent(event); |
---|
| 302 | } |
---|
[2343] | 303 | else if (event.classId==CFieldGroup::GetType()) CFieldGroup::dispatchEvent(event); |
---|
| 304 | else if (event.classId==CFile::GetType()) CFile::dispatchEvent(event); |
---|
| 305 | else if (event.classId==CFileGroup::GetType()) CFileGroup::dispatchEvent(event); |
---|
| 306 | else if (event.classId==CVariable::GetType()) CVariable::dispatchEvent(event); |
---|
| 307 | else |
---|
| 308 | { |
---|
| 309 | ERROR("void COneSidedContextServer::dispatchEvent(CEventServer& event)",<<" Bad event class Id"<<endl); |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | bool COneSidedContextServer::isCollectiveEvent(CEventServer& event) |
---|
| 314 | { |
---|
| 315 | if (event.type>1000) return false ; |
---|
| 316 | else return true ; |
---|
| 317 | } |
---|
| 318 | } |
---|