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

Last change on this file since 2566 was 2566, checked in by jderouillat, 10 months ago

Clean implementation of the request ordering management

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