source: XIOS/dev/dev_ym/XIOS_COUPLING/src/context_server.cpp @ 2190

Last change on this file since 2190 was 2130, checked in by ymipsl, 3 years ago

New management of client-server buffers.

  • buffers can grow automatically in intialization phase
  • buffers is evaluated after the close context definition phase and fixed at optimal value.

YM

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
  • Property svn:eol-style set to native
File size: 14.4 KB
RevLine 
[300]1#include "context_server.hpp"
2#include "buffer_in.hpp"
3#include "type.hpp"
4#include "context.hpp"
[352]5#include "object_template.hpp"
6#include "group_template.hpp"
7#include "attribute_template.hpp"
[300]8#include "domain.hpp"
[352]9#include "field.hpp"
10#include "file.hpp"
11#include "grid.hpp"
[382]12#include "mpi.hpp"
[347]13#include "tracer.hpp"
14#include "timer.hpp"
[401]15#include "cxios.hpp"
[492]16#include "event_scheduler.hpp"
17#include "server.hpp"
[1761]18#include "servers_ressource.hpp"
19#include "pool_ressource.hpp"
20#include "services.hpp"
21#include "contexts_manager.hpp"
[2130]22#include "timeline_events.hpp"
[1761]23
[492]24#include <boost/functional/hash.hpp>
[1761]25#include <random>
26#include <chrono>
[300]27
28
[335]29namespace xios
[300]30{
[1761]31  using namespace std ;
[300]32
[1853]33  CContextServer::CContextServer(CContext* parent,MPI_Comm intraComm_,MPI_Comm interComm_) 
34    : eventScheduler_(nullptr), isProcessingEvent_(false), associatedClient_(nullptr)
[300]35  {
[549]36    context=parent;
37    intraComm=intraComm_;
[1639]38    MPI_Comm_size(intraComm,&intraCommSize);
39    MPI_Comm_rank(intraComm,&intraCommRank);
[1054]40
[549]41    interComm=interComm_;
42    int flag;
[1639]43    MPI_Comm_test_inter(interComm,&flag);
[1757]44
45    if (flag) attachedMode=false ;
46    else  attachedMode=true ;
47   
[1639]48    if (flag) MPI_Comm_remote_size(interComm,&commSize);
49    else  MPI_Comm_size(interComm,&commSize);
[983]50
[1761]51   
52    SRegisterContextInfo contextInfo ;
53    CXios::getContextsManager()->getContextInfo(context->getId(), contextInfo, intraComm) ;
54
[2022]55  //  if (contextInfo.serviceType != CServicesManager::CLIENT) // we must have an event scheduler => to be retrieve from the associated services
56  //  {
[2123]57      //if (!isAttachedModeEnabled()) eventScheduler_=CXios::getPoolRessource()->getService(contextInfo.serviceId,contextInfo.partitionId)->getEventScheduler() ;
58      eventScheduler_=CXios::getPoolRessource()->getService(contextInfo.serviceId,contextInfo.partitionId)->getEventScheduler() ;
59
[2022]60  //  }
[1761]61
62
[1757]63    currentTimeLine=1;
[549]64    scheduled=false;
65    finished=false;
[1761]66
67    // generate unique hash for server
68    auto time=chrono::system_clock::now().time_since_epoch().count() ;
69    std::default_random_engine rd(time); // not reproducible from a run to another
70    std::uniform_int_distribution<size_t> dist;
71    hashId=dist(rd) ;
72    MPI_Bcast(&hashId,1,MPI_SIZE_T,0,intraComm) ; // Bcast to all server of the context
73
74
[1757]75    if (!isAttachedModeEnabled())
76    {
77      MPI_Intercomm_merge(interComm_,true,&interCommMerged) ;
78// create windows for one sided comm
79      int interCommMergedRank;
80      MPI_Comm winComm ;
81      MPI_Comm_rank(intraComm, &interCommMergedRank);
82      windows.resize(2) ;
83      for(int rank=commSize; rank<commSize+intraCommSize; rank++)
84      {
85        if (rank==commSize+interCommMergedRank) 
86        {
87          MPI_Comm_split(interCommMerged, interCommMergedRank, rank, &winComm);
88          int myRank ;
89          MPI_Comm_rank(winComm,&myRank);
90          MPI_Win_create_dynamic(MPI_INFO_NULL, winComm, &windows[0]);
91          MPI_Win_create_dynamic(MPI_INFO_NULL, winComm, &windows[1]);     
92        }
93        else MPI_Comm_split(interCommMerged, interCommMergedRank, rank, &winComm);
94        MPI_Comm_free(&winComm) ;
95      }
96    }
97    else 
98    {
99      windows.resize(2) ;
100      windows[0]=MPI_WIN_NULL ;
101      windows[1]=MPI_WIN_NULL ;
102    }
103
104
105   
106    MPI_Comm_split(intraComm_,intraCommRank,intraCommRank, &commSelf) ;
107    itLastTimeLine=lastTimeLine.begin() ;
108
109    pureOneSided=CXios::getin<bool>("pure_one_sided",false); // pure one sided communication (for test)
110    if (isAttachedModeEnabled()) pureOneSided=false ; // no one sided in attach mode
111     
[300]112  }
[992]113
[1757]114//! Attached mode is used ?
115//! \return true if attached mode is used, false otherwise
116  bool CContextServer::isAttachedModeEnabled() const
117  {
118    return attachedMode ;
119  }
120 
[300]121  void CContextServer::setPendingEvent(void)
122  {
[549]123    pendingEvent=true;
[300]124  }
[489]125
[300]126  bool CContextServer::hasPendingEvent(void)
127  {
[549]128    return pendingEvent;
[300]129  }
[489]130
[597]131  bool CContextServer::hasFinished(void)
132  {
133    return finished;
134  }
135
[1054]136  bool CContextServer::eventLoop(bool enableEventsProcessing /*= true*/)
[300]137  {
[549]138    listen();
139    checkPendingRequest();
[1757]140    if (enableEventsProcessing)  processEvents();
[549]141    return finished;
[300]142  }
143
144  void CContextServer::listen(void)
145  {
146    int rank;
[549]147    int flag;
148    int count;
149    char * addr;
[1639]150    MPI_Status status;
[300]151    map<int,CServerBuffer*>::iterator it;
[1230]152    bool okLoop;
[489]153
[1225]154    traceOff();
155    MPI_Iprobe(MPI_ANY_SOURCE, 20,interComm,&flag,&status);
156    traceOn();
157
158    if (flag==true)
[300]159    {
[1225]160      rank=status.MPI_SOURCE ;
[1230]161      okLoop = true;
[1228]162      if (pendingRequest.find(rank)==pendingRequest.end())
163        okLoop = !listenPendingRequest(status) ;
164      if (okLoop)
[300]165      {
[1225]166        for(rank=0;rank<commSize;rank++)
[300]167        {
[1225]168          if (pendingRequest.find(rank)==pendingRequest.end())
[300]169          {
[1225]170
171            traceOff();
[1639]172            MPI_Iprobe(rank, 20,interComm,&flag,&status);
[1225]173            traceOn();
174            if (flag==true) listenPendingRequest(status) ;
[300]175          }
176        }
177      }
178    }
179  }
[489]180
[1639]181  bool CContextServer::listenPendingRequest(MPI_Status& status)
[1225]182  {
183    int count;
184    char * addr;
185    map<int,CServerBuffer*>::iterator it;
186    int rank=status.MPI_SOURCE ;
187
188    it=buffers.find(rank);
189    if (it==buffers.end()) // Receive the buffer size and allocate the buffer
190    {
[2130]191       MPI_Aint recvBuff[4] ;
192       MPI_Recv(recvBuff, 4, MPI_AINT, rank, 20, interComm, &status);
193       remoteHashId_ = recvBuff[0] ;
194       StdSize buffSize = recvBuff[1];
[1757]195       vector<MPI_Aint> winAdress(2) ;
[2130]196       winAdress[0]=recvBuff[2] ; winAdress[1]=recvBuff[3] ;
[1225]197       mapBufferSize_.insert(std::make_pair(rank, buffSize));
[1757]198       it=(buffers.insert(pair<int,CServerBuffer*>(rank,new CServerBuffer(windows, winAdress, rank, buffSize)))).first;
[1765]199     
[1757]200       lastTimeLine[rank]=0 ;
201       itLastTimeLine=lastTimeLine.begin() ;
202
[1228]203       return true;
[1225]204    }
205    else
206    {
[1639]207      MPI_Get_count(&status,MPI_CHAR,&count);
[1225]208      if (it->second->isBufferFree(count))
209      {
210         addr=(char*)it->second->getBuffer(count);
[1639]211         MPI_Irecv(addr,count,MPI_CHAR,rank,20,interComm,&pendingRequest[rank]);
[1225]212         bufferRequest[rank]=addr;
[1228]213         return true;
[1225]214       }
[1228]215      else
216        return false;
[1225]217    }
218  }
219
220
[300]221  void CContextServer::checkPendingRequest(void)
222  {
[1639]223    map<int,MPI_Request>::iterator it;
[549]224    list<int> recvRequest;
[300]225    list<int>::iterator itRecv;
[549]226    int rank;
227    int flag;
228    int count;
[1639]229    MPI_Status status;
[489]230
[300]231    for(it=pendingRequest.begin();it!=pendingRequest.end();it++)
232    {
[549]233      rank=it->first;
234      traceOff();
[1639]235      MPI_Test(& it->second, &flag, &status);
[549]236      traceOn();
[300]237      if (flag==true)
238      {
[1757]239        buffers[rank]->updateCurrentWindows() ;
[549]240        recvRequest.push_back(rank);
[1639]241        MPI_Get_count(&status,MPI_CHAR,&count);
[549]242        processRequest(rank,bufferRequest[rank],count);
[300]243      }
244    }
[489]245
246    for(itRecv=recvRequest.begin();itRecv!=recvRequest.end();itRecv++)
[300]247    {
[549]248      pendingRequest.erase(*itRecv);
249      bufferRequest.erase(*itRecv);
[300]250    }
251  }
[489]252
[1757]253  void CContextServer::getBufferFromClient(size_t timeLine)
254  {
255    if (!isAttachedModeEnabled()) // one sided desactivated in attached mode
256    { 
257      int rank ;
258      char *buffer ;
259      size_t count ; 
260
261      if (itLastTimeLine==lastTimeLine.end()) itLastTimeLine=lastTimeLine.begin() ;
262      for(;itLastTimeLine!=lastTimeLine.end();++itLastTimeLine)
263      {
264        rank=itLastTimeLine->first ;
265        if (itLastTimeLine->second < timeLine &&  pendingRequest.count(rank)==0)
266        {
267          if (buffers[rank]->getBufferFromClient(timeLine, buffer, count))
268          {
269            processRequest(rank, buffer, count);
270            break ;
271          }
272        }
273      }
274    }
275  }
276         
277       
[300]278  void CContextServer::processRequest(int rank, char* buff,int count)
279  {
[489]280
[549]281    CBufferIn buffer(buff,count);
282    char* startBuffer,endBuffer;
283    int size, offset;
[1757]284    size_t timeLine=0;
[549]285    map<size_t,CEventServer*>::iterator it;
[489]286
[1757]287   
[1225]288    CTimer::get("Process request").resume();
[300]289    while(count>0)
290    {
[549]291      char* startBuffer=(char*)buffer.ptr();
292      CBufferIn newBuffer(startBuffer,buffer.remain());
293      newBuffer>>size>>timeLine;
[300]294
[2130]295      if (timeLine==timelineEventNotifyChangeBufferSize)
296      {
297        buffers[rank]->notifyBufferResizing() ;
298        buffers[rank]->updateCurrentWindows() ;
299      } 
300      else if (timeLine==timelineEventChangeBufferSize)
301      {
302        size_t newSize ;
303        vector<MPI_Aint> winAdress(2) ;
304        newBuffer>>newSize>>winAdress[0]>>winAdress[1] ;
305        buffers.erase(rank) ;
306        buffers.insert(pair<int,CServerBuffer*>(rank,new CServerBuffer(windows, winAdress, rank, newSize)));
307      }
308      else
309      {
310        it=events.find(timeLine);
311        if (it==events.end()) it=events.insert(pair<int,CEventServer*>(timeLine,new CEventServer(this))).first;
312        it->second->push(rank,buffers[rank],startBuffer,size);
313        if (timeLine>0) lastTimeLine[rank]=timeLine ;
314      }
[549]315      buffer.advance(size);
316      count=buffer.remain();
[489]317    }
[1757]318   
[1225]319    CTimer::get("Process request").suspend();
[300]320  }
[489]321
[300]322  void CContextServer::processEvents(void)
323  {
[549]324    map<size_t,CEventServer*>::iterator it;
325    CEventServer* event;
[1761]326   
[1764]327//    if (context->isProcessingEvent()) return ;
328    if (isProcessingEvent_) return ;
[2130]329    if (isAttachedModeEnabled())
330      if (!CXios::getDaemonsManager()->isScheduledContext(remoteHashId_)) return ;
[489]331
[549]332    it=events.find(currentTimeLine);
[489]333    if (it!=events.end())
[300]334    {
[549]335      event=it->second;
[509]336
[300]337      if (event->isFull())
338      {
[2123]339        if (!scheduled && !isAttachedModeEnabled()) // Skip event scheduling for attached mode and reception on client side
[492]340        {
[1764]341          eventScheduler_->registerEvent(currentTimeLine,hashId);
[549]342          scheduled=true;
[492]343        }
[2123]344        else if (isAttachedModeEnabled() || eventScheduler_->queryEvent(currentTimeLine,hashId) )
[492]345        {
[2123]346          MPI_Request req ;
347          MPI_Status status ;
348
349          MPI_Ibarrier(intraComm,&req) ;
350          int flag=false ;
351          do 
352          {
353            eventScheduler_->checkEvent()  ;
354            MPI_Test(&req,&flag,&status) ;
355          } while (!flag) ;
356
357          //MPI_Barrier(intraComm) ;
[851]358         // When using attached mode, synchronise the processes to avoid that differents event be scheduled by differents processes
359         // The best way to properly solve this problem will be to use the event scheduler also in attached mode
360         // for now just set up a MPI barrier
[1875]361//ym to be check later
362//         if (!eventScheduler_ && CXios::isServer) MPI_Barrier(intraComm) ;
[851]363
[1764]364//         context->setProcessingEvent() ;
365         isProcessingEvent_=true ;
[549]366         CTimer::get("Process events").resume();
[2022]367         info(100)<<"Received Event "<<currentTimeLine<<" of class "<<event->classId<<" of type "<<event->type<<endl ;
[549]368         dispatchEvent(*event);
369         CTimer::get("Process events").suspend();
[1764]370         isProcessingEvent_=false ;
371//         context->unsetProcessingEvent() ;
[549]372         pendingEvent=false;
373         delete event;
374         events.erase(it);
375         currentTimeLine++;
376         scheduled = false;
[2130]377         if (isAttachedModeEnabled()) CXios::getDaemonsManager()->unscheduleContext() ;
[492]378        }
379      }
[1757]380      else getBufferFromClient(currentTimeLine) ;
[492]381    }
[1757]382    else if (pureOneSided) getBufferFromClient(currentTimeLine) ; // if pure one sided check buffer even if no event recorded at current time line
[492]383  }
[489]384
[300]385  CContextServer::~CContextServer()
386  {
[549]387    map<int,CServerBuffer*>::iterator it;
[1158]388    for(it=buffers.begin();it!=buffers.end();++it) delete it->second;
[489]389  }
[300]390
[1757]391  void CContextServer::releaseBuffers()
392  {
393    map<int,CServerBuffer*>::iterator it;
394    bool out ;
395    do
396    {
397      out=true ;
398      for(it=buffers.begin();it!=buffers.end();++it)
399      {
400//        out = out && it->second->freeWindows() ;
401
402      }
403    } while (! out) ; 
404  }
405
406  void CContextServer::notifyClientsFinalize(void)
407  {
408    for(auto it=buffers.begin();it!=buffers.end();++it)
409    {
410      it->second->notifyClientFinalize() ;
411    }
412  }
413
[300]414  void CContextServer::dispatchEvent(CEventServer& event)
415  {
[549]416    string contextName;
417    string buff;
418    int MsgSize;
419    int rank;
420    list<CEventServer::SSubEvent>::iterator it;
[1054]421    StdString ctxId = context->getId();
422    CContext::setCurrent(ctxId);
[1130]423    StdSize totalBuf = 0;
[489]424
[300]425    if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_FINALIZE)
426    {
[597]427      finished=true;
[1194]428      info(20)<<" CContextServer: Receive context <"<<context->getId()<<"> finalize."<<endl;
[1757]429//      releaseBuffers() ;
430      notifyClientsFinalize() ;
[1194]431      context->finalize();
[1757]432
433/* don't know where release windows
434      MPI_Win_free(&windows[0]) ;
435      MPI_Win_free(&windows[1]) ;
436*/     
[511]437      std::map<int, StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
[983]438                           iteMap = mapBufferSize_.end(), itMap;
[511]439      for (itMap = itbMap; itMap != iteMap; ++itMap)
440      {
[1054]441        rank = itMap->first;
[1130]442        report(10)<< " Memory report : Context <"<<ctxId<<"> : server side : memory used for buffer of each connection to client" << endl
443            << "  +) With client of rank " << rank << " : " << itMap->second << " bytes " << endl;
444        totalBuf += itMap->second;
[511]445      }
[1130]446      report(0)<< " Memory report : Context <"<<ctxId<<"> : server side : total memory used for buffer "<<totalBuf<<" bytes"<<endl;
[300]447    }
[549]448    else if (event.classId==CContext::GetType()) CContext::dispatchEvent(event);
449    else if (event.classId==CContextGroup::GetType()) CContextGroup::dispatchEvent(event);
450    else if (event.classId==CCalendarWrapper::GetType()) CCalendarWrapper::dispatchEvent(event);
451    else if (event.classId==CDomain::GetType()) CDomain::dispatchEvent(event);
452    else if (event.classId==CDomainGroup::GetType()) CDomainGroup::dispatchEvent(event);
453    else if (event.classId==CAxis::GetType()) CAxis::dispatchEvent(event);
454    else if (event.classId==CAxisGroup::GetType()) CAxisGroup::dispatchEvent(event);
[887]455    else if (event.classId==CScalar::GetType()) CScalar::dispatchEvent(event);
456    else if (event.classId==CScalarGroup::GetType()) CScalarGroup::dispatchEvent(event);
[549]457    else if (event.classId==CGrid::GetType()) CGrid::dispatchEvent(event);
458    else if (event.classId==CGridGroup::GetType()) CGridGroup::dispatchEvent(event);
459    else if (event.classId==CField::GetType()) CField::dispatchEvent(event);
460    else if (event.classId==CFieldGroup::GetType()) CFieldGroup::dispatchEvent(event);
461    else if (event.classId==CFile::GetType()) CFile::dispatchEvent(event);
462    else if (event.classId==CFileGroup::GetType()) CFileGroup::dispatchEvent(event);
463    else if (event.classId==CVariable::GetType()) CVariable::dispatchEvent(event);
[300]464    else
465    {
[549]466      ERROR("void CContextServer::dispatchEvent(CEventServer& event)",<<" Bad event class Id"<<endl);
[300]467    }
468  }
469}
Note: See TracBrowser for help on using the repository browser.