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

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

Forced the request processing, on servers, to be ordered in the one-sided protocol

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