source: XIOS3/trunk/src/transport/one_sided_context_server.cpp

Last change on this file was 2629, checked in by jderouillat, 6 weeks ago

Delete boost dependencies, the few features used are replaced by functions stored in extern/boost_extraction

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 10.4 KB
Line 
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 <random>
25#include <chrono>
26
27
28namespace xios
29{
30  using namespace std ;
31  extern CLogType logTimers ;
32  extern CLogType logProfile ;
33
34  COneSidedContextServer::COneSidedContextServer(CContext* parent,MPI_Comm intraComm_,MPI_Comm interComm_)
35                         : CContextServer(parent, intraComm_, interComm_), 
36                           isProcessingEvent_(false)
37  {
38   
39    xios::MPI_Comm_dup(intraComm, &processEventBarrier_) ;
40    CXios::getMpiGarbageCollector().registerCommunicator(processEventBarrier_) ;
41   
42    currentTimeLine=1;
43    scheduled=false;
44    finished=false;
45
46    xios::MPI_Intercomm_merge(interComm_,true,&interCommMerged_) ;
47    CXios::getMpiGarbageCollector().registerCommunicator(interCommMerged_) ;
48    xios::MPI_Comm_split(intraComm_, intraCommRank, intraCommRank, &commSelf_) ; // for windows
49    CXios::getMpiGarbageCollector().registerCommunicator(commSelf_) ;
50
51    itLastTimeLine=lastTimeLine.begin() ;
52
53    pureOneSided=CXios::getin<bool>("pure_one_sided",false); // pure one sided communication (for test)
54     
55  }
56
57  void COneSidedContextServer::setPendingEvent(void)
58  {
59    pendingEvent=true;
60  }
61
62  bool COneSidedContextServer::hasPendingEvent(void)
63  {
64    return ((pendingEvents_.size()!=0)||(completedEvents_.size()!=0));
65  }
66
67  bool COneSidedContextServer::hasFinished(void)
68  {
69    return finished;
70  }
71
72  bool COneSidedContextServer::eventLoop(bool enableEventsProcessing /*= true*/)
73  {
74    if (info.isActive(logTimers)) CTimer::get("listen request").resume();
75    listen();
76    if (info.isActive(logTimers)) CTimer::get("listen request").suspend();
77
78    if (info.isActive(logTimers)) CTimer::get("listen pending request").resume();
79    listenPendingRequest() ;
80    if (info.isActive(logTimers)) CTimer::get("listen pending request").suspend();
81
82    if (info.isActive(logTimers)) CTimer::get("check server Buffers").resume();
83    checkBuffers() ;
84    if (info.isActive(logTimers)) CTimer::get("check server Buffers").suspend();
85
86    if (info.isActive(logTimers)) CTimer::get("check event process").resume();
87    processEvents(enableEventsProcessing);
88    if (info.isActive(logTimers)) CTimer::get("check event process").suspend();
89    return finished;
90
91  }
92
93 void COneSidedContextServer::listen(void)
94  {
95    int rank;
96    int flag;
97    MPI_Status status;
98    flag=true ;
99
100    while(flag)
101    {
102      traceOff();
103      MPI_Iprobe(MPI_ANY_SOURCE, 20,interCommMerged_, &flag, &status);
104      traceOn();
105      if (flag==true)
106      {
107        int rank=status.MPI_SOURCE ;
108        auto& rankRequests = requests_[rank];
109        rankRequests.push_back(new CRequest(interCommMerged_, status)) ;
110        // Test 1st request of the list, request treatment must be ordered
111        if (rankRequests.front()->test()) 
112        {
113          processRequest(*(rankRequests.front())) ;
114          delete rankRequests.front();
115          rankRequests.pop_front() ;
116        }
117      }
118    }
119  }
120
121  void COneSidedContextServer::listenPendingRequest(void)
122  {
123    for(auto it_rank=requests_.begin() ; it_rank!=requests_.end() ; ++it_rank)
124    {
125      int rank = it_rank->first;
126      auto& rankRequests = it_rank->second;
127      while ( (!rankRequests.empty()) && (rankRequests.front()->test()) )
128      {
129        processRequest( *(rankRequests.front()) );
130        delete rankRequests.front();
131        rankRequests.pop_front() ;
132      }
133    }
134  }
135
136  void COneSidedContextServer::processRequest(CRequest& request)
137  {
138    int rank = request.getRank() ;
139    auto it=buffers_.find(rank);
140    if (it==buffers_.end())
141    {
142      buffers_[rank] = new COneSidedServerBuffer(rank, commSelf_, interCommMerged_, pendingEvents_, completedEvents_, request.getBuffer()) ;
143    }
144    else it->second->receivedRequest(request.getBuffer()) ;
145  }
146
147  void COneSidedContextServer::checkBuffers(void)
148  {
149    if (!pendingEvents_.empty())
150    {
151/*
152      SPendingEvent& nextEvent = pendingEvents_.begin()->second ;
153      for(auto& buffer : nextEvent.buffers ) buffer->eventLoop() ;
154      if (nextEvent.nbSenders==0) pendingEvents_.erase(pendingEvents_.begin()) ;
155*/
156      for(auto it=pendingEvents_.begin() ;  it!=pendingEvents_.end() ;)
157      {
158        SPendingEvent& nextEvent = it->second ;
159        for(auto& buffer : nextEvent.buffers ) buffer->eventLoop() ;
160        if (nextEvent.nbSenders==0) it=pendingEvents_.erase(it) ;
161        else ++it ;
162      }
163    }
164  }
165
166
167  void COneSidedContextServer::processEvents(bool enableEventsProcessing)
168  {
169 
170    if (isProcessingEvent_) return ;
171
172    auto it=completedEvents_.find(currentTimeLine);
173
174    if (it!=completedEvents_.end())
175    {
176      if (it->second.nbSenders == it->second.currentNbSenders)
177      {
178        if (!scheduled) 
179        {
180          eventScheduler_->registerEvent(currentTimeLine,hashId);
181          scheduled=true;
182        }
183        else if (eventScheduler_->queryEvent(currentTimeLine,hashId) )
184        {
185          //if (!enableEventsProcessing && isCollectiveEvent(event)) return ;
186
187          if (!eventScheduled_) 
188          {
189            MPI_Ibarrier(processEventBarrier_,&processEventRequest_) ;
190            eventScheduled_=true ;
191            return ;
192          }
193          else 
194          {
195            MPI_Status status ;
196            int flag ;
197            MPI_Test(&processEventRequest_, &flag, &status) ;
198            if (!flag) return ;
199            eventScheduled_=false ;
200          }
201
202          eventScheduler_->popEvent() ;
203
204          isProcessingEvent_=true ;
205          CEventServer event(this) ;
206          for(auto& buffer : it->second.buffers) buffer->fillEventServer(currentTimeLine, event) ;
207//          MPI_Barrier(intraComm) ;
208          CTimer::get("Process events").resume();
209          info(100)<<"Context id "<<context->getId()<<" : Process Event "<<currentTimeLine<<" of class "<<event.classId<<" of type "<<event.type<<endl ;
210          dispatchEvent(event);
211          CTimer::get("Process events").suspend();
212          isProcessingEvent_=false ;
213//         context->unsetProcessingEvent() ;
214          pendingEvent=false;
215          completedEvents_.erase(it);
216          currentTimeLine++;
217          scheduled = false;
218        }
219      }
220    }
221  }
222
223  COneSidedContextServer::~COneSidedContextServer()
224  {
225    for(auto& buffer : buffers_) delete buffer.second;
226    buffers_.clear() ;
227  }
228
229  void COneSidedContextServer::releaseBuffers()
230  {
231    //for(auto it=buffers.begin();it!=buffers.end();++it) delete it->second ;
232    //buffers.clear() ;
233    freeWindows() ;
234  }
235
236  void COneSidedContextServer::freeWindows()
237  {
238    //  for(auto& it : winComm_)
239    //  {
240    //    int rank = it.first ;
241    //    MPI_Win_free(&windows_[rank][0]);
242    //    MPI_Win_free(&windows_[rank][1]);
243    //    xios::MPI_Comm_free(&winComm_[rank]) ;
244    //  }
245  }
246
247  void COneSidedContextServer::notifyClientsFinalize(void)
248  {
249    for(auto it=buffers_.begin();it!=buffers_.end();++it)
250    {
251      it->second->notifyClientFinalize() ;
252    }
253  }
254
255  void COneSidedContextServer::dispatchEvent(CEventServer& event)
256  {
257    string contextName;
258    string buff;
259    int MsgSize;
260    int rank;
261    list<CEventServer::SSubEvent>::iterator it;
262    StdString ctxId = context->getId();
263    CContext::setCurrent(ctxId);
264    StdSize totalBuf = 0;
265
266    if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_FINALIZE)
267    {
268      if (info.isActive(logProfile)) CTimer::get("Context finalize").resume();
269      finished=true;
270      info(20)<<" COneSidedContextServer: Receive context <"<<context->getId()<<"> finalize."<<endl;
271      notifyClientsFinalize() ;
272      if (info.isActive(logTimers)) CTimer::get("receiving requests").suspend();
273      context->finalize();
274     
275      std::map<int, StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
276                           iteMap = mapBufferSize_.end(), itMap;
277      for (itMap = itbMap; itMap != iteMap; ++itMap)
278      {
279        rank = itMap->first;
280        report(10)<< " Memory report : Context <"<<ctxId<<"> : server side : memory used for buffer of each connection to client" << endl
281            << "  +) With client of rank " << rank << " : " << itMap->second << " bytes " << endl;
282        totalBuf += itMap->second;
283      }
284      report(0)<< " Memory report : Context <"<<ctxId<<"> : server side : total memory used for buffer "<<totalBuf<<" bytes"<<endl;
285      if (info.isActive(logProfile)) CTimer::get("Context finalize").suspend();
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);
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    }
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}
Note: See TracBrowser for help on using the repository browser.