[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 | |
---|
[2589] | 38 | xios::MPI_Comm_dup(intraComm, &processEventBarrier_) ; |
---|
[2592] | 39 | CXios::getMpiGarbageCollector().registerCommunicator(processEventBarrier_) ; |
---|
| 40 | |
---|
[2343] | 41 | currentTimeLine=1; |
---|
| 42 | scheduled=false; |
---|
| 43 | finished=false; |
---|
| 44 | |
---|
[2589] | 45 | xios::MPI_Intercomm_merge(interComm_,true,&interCommMerged_) ; |
---|
[2592] | 46 | CXios::getMpiGarbageCollector().registerCommunicator(interCommMerged_) ; |
---|
[2589] | 47 | xios::MPI_Comm_split(intraComm_, intraCommRank, intraCommRank, &commSelf_) ; // for windows |
---|
[2592] | 48 | CXios::getMpiGarbageCollector().registerCommunicator(commSelf_) ; |
---|
| 49 | |
---|
[2343] | 50 | itLastTimeLine=lastTimeLine.begin() ; |
---|
| 51 | |
---|
| 52 | pureOneSided=CXios::getin<bool>("pure_one_sided",false); // pure one sided communication (for test) |
---|
| 53 | |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | void COneSidedContextServer::setPendingEvent(void) |
---|
| 57 | { |
---|
| 58 | pendingEvent=true; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | bool COneSidedContextServer::hasPendingEvent(void) |
---|
| 62 | { |
---|
[2595] | 63 | return ((pendingEvents_.size()!=0)||(completedEvents_.size()!=0)); |
---|
[2343] | 64 | } |
---|
| 65 | |
---|
| 66 | bool COneSidedContextServer::hasFinished(void) |
---|
| 67 | { |
---|
| 68 | return finished; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | bool COneSidedContextServer::eventLoop(bool enableEventsProcessing /*= true*/) |
---|
| 72 | { |
---|
| 73 | CTimer::get("listen request").resume(); |
---|
| 74 | listen(); |
---|
| 75 | CTimer::get("listen request").suspend(); |
---|
| 76 | |
---|
| 77 | CTimer::get("listen pending request").resume(); |
---|
| 78 | listenPendingRequest() ; |
---|
| 79 | CTimer::get("listen pending request").suspend(); |
---|
| 80 | |
---|
| 81 | CTimer::get("check server Buffers").resume(); |
---|
| 82 | checkBuffers() ; |
---|
| 83 | CTimer::get("check server Buffers").suspend(); |
---|
| 84 | |
---|
| 85 | CTimer::get("check event process").resume(); |
---|
| 86 | processEvents(enableEventsProcessing); |
---|
| 87 | CTimer::get("check event process").suspend(); |
---|
| 88 | return finished; |
---|
| 89 | |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | void COneSidedContextServer::listen(void) |
---|
| 93 | { |
---|
| 94 | int rank; |
---|
| 95 | int flag; |
---|
| 96 | MPI_Status status; |
---|
[2399] | 97 | flag=true ; |
---|
| 98 | |
---|
| 99 | while(flag) |
---|
[2343] | 100 | { |
---|
[2399] | 101 | traceOff(); |
---|
[2526] | 102 | MPI_Iprobe(MPI_ANY_SOURCE, 20,interCommMerged_, &flag, &status); |
---|
[2399] | 103 | traceOn(); |
---|
| 104 | if (flag==true) |
---|
[2343] | 105 | { |
---|
[2561] | 106 | int rank=status.MPI_SOURCE ; |
---|
[2566] | 107 | auto& rankRequests = requests_[rank]; |
---|
| 108 | rankRequests.push_back(new CRequest(interCommMerged_, status)) ; |
---|
[2561] | 109 | // Test 1st request of the list, request treatment must be ordered |
---|
[2566] | 110 | if (rankRequests.front()->test()) |
---|
[2399] | 111 | { |
---|
[2566] | 112 | processRequest(*(rankRequests.front())) ; |
---|
| 113 | delete rankRequests.front(); |
---|
| 114 | rankRequests.pop_front() ; |
---|
[2399] | 115 | } |
---|
[2343] | 116 | } |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | void COneSidedContextServer::listenPendingRequest(void) |
---|
| 121 | { |
---|
[2561] | 122 | for(auto it_rank=requests_.begin() ; it_rank!=requests_.end() ; ++it_rank) |
---|
[2343] | 123 | { |
---|
[2561] | 124 | int rank = it_rank->first; |
---|
[2566] | 125 | auto& rankRequests = it_rank->second; |
---|
| 126 | while ( (!rankRequests.empty()) && (rankRequests.front()->test()) ) |
---|
[2343] | 127 | { |
---|
[2566] | 128 | processRequest( *(rankRequests.front()) ); |
---|
| 129 | delete rankRequests.front(); |
---|
| 130 | rankRequests.pop_front() ; |
---|
[2343] | 131 | } |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | void COneSidedContextServer::processRequest(CRequest& request) |
---|
| 136 | { |
---|
| 137 | int rank = request.getRank() ; |
---|
| 138 | auto it=buffers_.find(rank); |
---|
| 139 | if (it==buffers_.end()) |
---|
| 140 | { |
---|
| 141 | buffers_[rank] = new COneSidedServerBuffer(rank, commSelf_, interCommMerged_, pendingEvents_, completedEvents_, request.getBuffer()) ; |
---|
| 142 | } |
---|
| 143 | else it->second->receivedRequest(request.getBuffer()) ; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | void COneSidedContextServer::checkBuffers(void) |
---|
| 147 | { |
---|
| 148 | if (!pendingEvents_.empty()) |
---|
| 149 | { |
---|
[2399] | 150 | /* |
---|
[2343] | 151 | SPendingEvent& nextEvent = pendingEvents_.begin()->second ; |
---|
| 152 | for(auto& buffer : nextEvent.buffers ) buffer->eventLoop() ; |
---|
| 153 | if (nextEvent.nbSenders==0) pendingEvents_.erase(pendingEvents_.begin()) ; |
---|
[2399] | 154 | */ |
---|
| 155 | for(auto it=pendingEvents_.begin() ; it!=pendingEvents_.end() ;) |
---|
| 156 | { |
---|
| 157 | SPendingEvent& nextEvent = it->second ; |
---|
| 158 | for(auto& buffer : nextEvent.buffers ) buffer->eventLoop() ; |
---|
| 159 | if (nextEvent.nbSenders==0) it=pendingEvents_.erase(it) ; |
---|
| 160 | else ++it ; |
---|
| 161 | } |
---|
[2343] | 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | void COneSidedContextServer::processEvents(bool enableEventsProcessing) |
---|
| 167 | { |
---|
| 168 | |
---|
| 169 | if (isProcessingEvent_) return ; |
---|
| 170 | |
---|
| 171 | auto it=completedEvents_.find(currentTimeLine); |
---|
| 172 | |
---|
| 173 | if (it!=completedEvents_.end()) |
---|
| 174 | { |
---|
| 175 | if (it->second.nbSenders == it->second.currentNbSenders) |
---|
| 176 | { |
---|
[2547] | 177 | if (!scheduled) |
---|
[2343] | 178 | { |
---|
| 179 | eventScheduler_->registerEvent(currentTimeLine,hashId); |
---|
| 180 | scheduled=true; |
---|
| 181 | } |
---|
[2547] | 182 | else if (eventScheduler_->queryEvent(currentTimeLine,hashId) ) |
---|
[2343] | 183 | { |
---|
| 184 | //if (!enableEventsProcessing && isCollectiveEvent(event)) return ; |
---|
| 185 | |
---|
| 186 | if (!eventScheduled_) |
---|
| 187 | { |
---|
| 188 | MPI_Ibarrier(processEventBarrier_,&processEventRequest_) ; |
---|
| 189 | eventScheduled_=true ; |
---|
| 190 | return ; |
---|
| 191 | } |
---|
| 192 | else |
---|
| 193 | { |
---|
| 194 | MPI_Status status ; |
---|
| 195 | int flag ; |
---|
| 196 | MPI_Test(&processEventRequest_, &flag, &status) ; |
---|
| 197 | if (!flag) return ; |
---|
| 198 | eventScheduled_=false ; |
---|
| 199 | } |
---|
| 200 | |
---|
[2547] | 201 | eventScheduler_->popEvent() ; |
---|
[2343] | 202 | |
---|
| 203 | isProcessingEvent_=true ; |
---|
| 204 | CEventServer event(this) ; |
---|
| 205 | for(auto& buffer : it->second.buffers) buffer->fillEventServer(currentTimeLine, event) ; |
---|
[2407] | 206 | // MPI_Barrier(intraComm) ; |
---|
[2343] | 207 | CTimer::get("Process events").resume(); |
---|
| 208 | info(100)<<"Context id "<<context->getId()<<" : Process Event "<<currentTimeLine<<" of class "<<event.classId<<" of type "<<event.type<<endl ; |
---|
| 209 | dispatchEvent(event); |
---|
| 210 | CTimer::get("Process events").suspend(); |
---|
| 211 | isProcessingEvent_=false ; |
---|
| 212 | // context->unsetProcessingEvent() ; |
---|
| 213 | pendingEvent=false; |
---|
| 214 | completedEvents_.erase(it); |
---|
| 215 | currentTimeLine++; |
---|
| 216 | scheduled = false; |
---|
| 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 | // for(auto& it : winComm_) |
---|
| 238 | // { |
---|
| 239 | // int rank = it.first ; |
---|
| 240 | // MPI_Win_free(&windows_[rank][0]); |
---|
| 241 | // MPI_Win_free(&windows_[rank][1]); |
---|
[2589] | 242 | // xios::MPI_Comm_free(&winComm_[rank]) ; |
---|
[2343] | 243 | // } |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | void COneSidedContextServer::notifyClientsFinalize(void) |
---|
| 247 | { |
---|
| 248 | for(auto it=buffers_.begin();it!=buffers_.end();++it) |
---|
| 249 | { |
---|
| 250 | it->second->notifyClientFinalize() ; |
---|
| 251 | } |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | void COneSidedContextServer::dispatchEvent(CEventServer& event) |
---|
| 255 | { |
---|
| 256 | string contextName; |
---|
| 257 | string buff; |
---|
| 258 | int MsgSize; |
---|
| 259 | int rank; |
---|
| 260 | list<CEventServer::SSubEvent>::iterator it; |
---|
| 261 | StdString ctxId = context->getId(); |
---|
| 262 | CContext::setCurrent(ctxId); |
---|
| 263 | StdSize totalBuf = 0; |
---|
| 264 | |
---|
| 265 | if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_FINALIZE) |
---|
| 266 | { |
---|
| 267 | finished=true; |
---|
| 268 | info(20)<<" COneSidedContextServer: Receive context <"<<context->getId()<<"> finalize."<<endl; |
---|
| 269 | notifyClientsFinalize() ; |
---|
| 270 | CTimer::get("receiving requests").suspend(); |
---|
| 271 | context->finalize(); |
---|
| 272 | |
---|
| 273 | std::map<int, StdSize>::const_iterator itbMap = mapBufferSize_.begin(), |
---|
| 274 | iteMap = mapBufferSize_.end(), itMap; |
---|
| 275 | for (itMap = itbMap; itMap != iteMap; ++itMap) |
---|
| 276 | { |
---|
| 277 | rank = itMap->first; |
---|
| 278 | report(10)<< " Memory report : Context <"<<ctxId<<"> : server side : memory used for buffer of each connection to client" << endl |
---|
| 279 | << " +) With client of rank " << rank << " : " << itMap->second << " bytes " << endl; |
---|
| 280 | totalBuf += itMap->second; |
---|
| 281 | } |
---|
| 282 | report(0)<< " Memory report : Context <"<<ctxId<<"> : server side : total memory used for buffer "<<totalBuf<<" bytes"<<endl; |
---|
| 283 | } |
---|
| 284 | else if (event.classId==CContext::GetType()) CContext::dispatchEvent(event); |
---|
| 285 | else if (event.classId==CContextGroup::GetType()) CContextGroup::dispatchEvent(event); |
---|
| 286 | else if (event.classId==CCalendarWrapper::GetType()) CCalendarWrapper::dispatchEvent(event); |
---|
| 287 | else if (event.classId==CDomain::GetType()) CDomain::dispatchEvent(event); |
---|
| 288 | else if (event.classId==CDomainGroup::GetType()) CDomainGroup::dispatchEvent(event); |
---|
| 289 | else if (event.classId==CAxis::GetType()) CAxis::dispatchEvent(event); |
---|
| 290 | else if (event.classId==CAxisGroup::GetType()) CAxisGroup::dispatchEvent(event); |
---|
| 291 | else if (event.classId==CScalar::GetType()) CScalar::dispatchEvent(event); |
---|
| 292 | else if (event.classId==CScalarGroup::GetType()) CScalarGroup::dispatchEvent(event); |
---|
| 293 | else if (event.classId==CGrid::GetType()) CGrid::dispatchEvent(event); |
---|
| 294 | else if (event.classId==CGridGroup::GetType()) CGridGroup::dispatchEvent(event); |
---|
[2399] | 295 | else if (event.classId==CField::GetType()) |
---|
| 296 | { |
---|
| 297 | if (event.type==CField::EVENT_ID_UPDATE_DATA) CField::dispatchEvent(event); |
---|
| 298 | else CField::dispatchEvent(event); |
---|
| 299 | } |
---|
[2343] | 300 | else if (event.classId==CFieldGroup::GetType()) CFieldGroup::dispatchEvent(event); |
---|
| 301 | else if (event.classId==CFile::GetType()) CFile::dispatchEvent(event); |
---|
| 302 | else if (event.classId==CFileGroup::GetType()) CFileGroup::dispatchEvent(event); |
---|
| 303 | else if (event.classId==CVariable::GetType()) CVariable::dispatchEvent(event); |
---|
| 304 | else |
---|
| 305 | { |
---|
| 306 | ERROR("void COneSidedContextServer::dispatchEvent(CEventServer& event)",<<" Bad event class Id"<<endl); |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | bool COneSidedContextServer::isCollectiveEvent(CEventServer& event) |
---|
| 311 | { |
---|
| 312 | if (event.type>1000) return false ; |
---|
| 313 | else return true ; |
---|
| 314 | } |
---|
| 315 | } |
---|