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