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