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