source: XIOS/dev/dev_ym/XIOS_ONE_SIDED/src/context_server.cpp @ 2362

Last change on this file since 2362 was 1757, checked in by ymipsl, 5 years ago

Implement one sided communication in client/server protocol to avoid dead-lock when some buffer are full.

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: 12.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"
18#include <boost/functional/hash.hpp>
[300]19
20
21
[335]22namespace xios
[300]23{
24
[1639]25  CContextServer::CContextServer(CContext* parent,MPI_Comm intraComm_,MPI_Comm interComm_)
[300]26  {
[549]27    context=parent;
28    intraComm=intraComm_;
[1639]29    MPI_Comm_size(intraComm,&intraCommSize);
30    MPI_Comm_rank(intraComm,&intraCommRank);
[1054]31
[549]32    interComm=interComm_;
33    int flag;
[1639]34    MPI_Comm_test_inter(interComm,&flag);
[1757]35
36    if (flag) attachedMode=false ;
37    else  attachedMode=true ;
38   
[1639]39    if (flag) MPI_Comm_remote_size(interComm,&commSize);
40    else  MPI_Comm_size(interComm,&commSize);
[983]41
[1757]42     
43    currentTimeLine=1;
[549]44    scheduled=false;
45    finished=false;
46    boost::hash<string> hashString;
[1130]47    if (CServer::serverLevel == 1)
48      hashId=hashString(context->getId() + boost::lexical_cast<string>(context->clientPrimServer.size()));
49    else
50      hashId=hashString(context->getId());
[1757]51
52    if (!isAttachedModeEnabled())
53    {
54      MPI_Intercomm_merge(interComm_,true,&interCommMerged) ;
55// create windows for one sided comm
56      int interCommMergedRank;
57      MPI_Comm winComm ;
58      MPI_Comm_rank(intraComm, &interCommMergedRank);
59      windows.resize(2) ;
60      for(int rank=commSize; rank<commSize+intraCommSize; rank++)
61      {
62        if (rank==commSize+interCommMergedRank) 
63        {
64          MPI_Comm_split(interCommMerged, interCommMergedRank, rank, &winComm);
65          int myRank ;
66          MPI_Comm_rank(winComm,&myRank);
67          MPI_Win_create_dynamic(MPI_INFO_NULL, winComm, &windows[0]);
68          MPI_Win_create_dynamic(MPI_INFO_NULL, winComm, &windows[1]);     
69        }
70        else MPI_Comm_split(interCommMerged, interCommMergedRank, rank, &winComm);
71        MPI_Comm_free(&winComm) ;
72      }
73    }
74    else 
75    {
76      windows.resize(2) ;
77      windows[0]=MPI_WIN_NULL ;
78      windows[1]=MPI_WIN_NULL ;
79    }
80
81
82   
83    MPI_Comm_split(intraComm_,intraCommRank,intraCommRank, &commSelf) ;
84    itLastTimeLine=lastTimeLine.begin() ;
85
86    pureOneSided=CXios::getin<bool>("pure_one_sided",false); // pure one sided communication (for test)
87    if (isAttachedModeEnabled()) pureOneSided=false ; // no one sided in attach mode
88     
[300]89  }
[992]90
[1757]91//! Attached mode is used ?
92//! \return true if attached mode is used, false otherwise
93  bool CContextServer::isAttachedModeEnabled() const
94  {
95    return attachedMode ;
96  }
97 
[300]98  void CContextServer::setPendingEvent(void)
99  {
[549]100    pendingEvent=true;
[300]101  }
[489]102
[300]103  bool CContextServer::hasPendingEvent(void)
104  {
[549]105    return pendingEvent;
[300]106  }
[489]107
[597]108  bool CContextServer::hasFinished(void)
109  {
110    return finished;
111  }
112
[1054]113  bool CContextServer::eventLoop(bool enableEventsProcessing /*= true*/)
[300]114  {
[549]115    listen();
116    checkPendingRequest();
[1757]117    if (enableEventsProcessing)  processEvents();
[549]118    return finished;
[300]119  }
120
121  void CContextServer::listen(void)
122  {
123    int rank;
[549]124    int flag;
125    int count;
126    char * addr;
[1639]127    MPI_Status status;
[300]128    map<int,CServerBuffer*>::iterator it;
[1230]129    bool okLoop;
[489]130
[1225]131    traceOff();
132    MPI_Iprobe(MPI_ANY_SOURCE, 20,interComm,&flag,&status);
133    traceOn();
134
135    if (flag==true)
[300]136    {
[1225]137      rank=status.MPI_SOURCE ;
[1230]138      okLoop = true;
[1228]139      if (pendingRequest.find(rank)==pendingRequest.end())
140        okLoop = !listenPendingRequest(status) ;
141      if (okLoop)
[300]142      {
[1225]143        for(rank=0;rank<commSize;rank++)
[300]144        {
[1225]145          if (pendingRequest.find(rank)==pendingRequest.end())
[300]146          {
[1225]147
148            traceOff();
[1639]149            MPI_Iprobe(rank, 20,interComm,&flag,&status);
[1225]150            traceOn();
151            if (flag==true) listenPendingRequest(status) ;
[300]152          }
153        }
154      }
155    }
156  }
[489]157
[1639]158  bool CContextServer::listenPendingRequest(MPI_Status& status)
[1225]159  {
160    int count;
161    char * addr;
162    map<int,CServerBuffer*>::iterator it;
163    int rank=status.MPI_SOURCE ;
164
165    it=buffers.find(rank);
166    if (it==buffers.end()) // Receive the buffer size and allocate the buffer
167    {
[1757]168       MPI_Aint recvBuff[3] ;
169       MPI_Recv(recvBuff, 3, MPI_AINT, rank, 20, interComm, &status);
170       StdSize buffSize = recvBuff[0];
171       vector<MPI_Aint> winAdress(2) ;
172       winAdress[0]=recvBuff[1] ; winAdress[1]=recvBuff[2] ;
[1225]173       mapBufferSize_.insert(std::make_pair(rank, buffSize));
[1757]174       it=(buffers.insert(pair<int,CServerBuffer*>(rank,new CServerBuffer(windows, winAdress, rank, buffSize)))).first;
175      /*
176       if (!isAttachedModeEnabled())
177       {
178         MPI_Comm OneSidedInterComm, oneSidedComm ;
179         MPI_Intercomm_create(commSelf, 0, interCommMerged, rank, 0, &OneSidedInterComm );
180         MPI_Intercomm_merge(OneSidedInterComm,true,&oneSidedComm);
181         buffers[rank]->createWindows(oneSidedComm) ;
182       }
183       */
184       lastTimeLine[rank]=0 ;
185       itLastTimeLine=lastTimeLine.begin() ;
186
[1228]187       return true;
[1225]188    }
189    else
190    {
[1639]191      MPI_Get_count(&status,MPI_CHAR,&count);
[1225]192      if (it->second->isBufferFree(count))
193      {
194         addr=(char*)it->second->getBuffer(count);
[1639]195         MPI_Irecv(addr,count,MPI_CHAR,rank,20,interComm,&pendingRequest[rank]);
[1225]196         bufferRequest[rank]=addr;
[1228]197         return true;
[1225]198       }
[1228]199      else
200        return false;
[1225]201    }
202  }
203
204
[300]205  void CContextServer::checkPendingRequest(void)
206  {
[1639]207    map<int,MPI_Request>::iterator it;
[549]208    list<int> recvRequest;
[300]209    list<int>::iterator itRecv;
[549]210    int rank;
211    int flag;
212    int count;
[1639]213    MPI_Status status;
[489]214
[300]215    for(it=pendingRequest.begin();it!=pendingRequest.end();it++)
216    {
[549]217      rank=it->first;
218      traceOff();
[1639]219      MPI_Test(& it->second, &flag, &status);
[549]220      traceOn();
[300]221      if (flag==true)
222      {
[1757]223        buffers[rank]->updateCurrentWindows() ;
[549]224        recvRequest.push_back(rank);
[1639]225        MPI_Get_count(&status,MPI_CHAR,&count);
[549]226        processRequest(rank,bufferRequest[rank],count);
[300]227      }
228    }
[489]229
230    for(itRecv=recvRequest.begin();itRecv!=recvRequest.end();itRecv++)
[300]231    {
[549]232      pendingRequest.erase(*itRecv);
233      bufferRequest.erase(*itRecv);
[300]234    }
235  }
[489]236
[1757]237  void CContextServer::getBufferFromClient(size_t timeLine)
238  {
239    if (!isAttachedModeEnabled()) // one sided desactivated in attached mode
240    { 
241      int rank ;
242      char *buffer ;
243      size_t count ; 
244
245      if (itLastTimeLine==lastTimeLine.end()) itLastTimeLine=lastTimeLine.begin() ;
246      for(;itLastTimeLine!=lastTimeLine.end();++itLastTimeLine)
247      {
248        rank=itLastTimeLine->first ;
249        if (itLastTimeLine->second < timeLine &&  pendingRequest.count(rank)==0)
250        {
251          if (buffers[rank]->getBufferFromClient(timeLine, buffer, count))
252          {
253            processRequest(rank, buffer, count);
254            break ;
255          }
256        }
257      }
258    }
259  }
260         
261       
[300]262  void CContextServer::processRequest(int rank, char* buff,int count)
263  {
[489]264
[549]265    CBufferIn buffer(buff,count);
266    char* startBuffer,endBuffer;
267    int size, offset;
[1757]268    size_t timeLine=0;
[549]269    map<size_t,CEventServer*>::iterator it;
[489]270
[1757]271   
[1225]272    CTimer::get("Process request").resume();
[300]273    while(count>0)
274    {
[549]275      char* startBuffer=(char*)buffer.ptr();
276      CBufferIn newBuffer(startBuffer,buffer.remain());
277      newBuffer>>size>>timeLine;
278      it=events.find(timeLine);
279      if (it==events.end()) it=events.insert(pair<int,CEventServer*>(timeLine,new CEventServer)).first;
280      it->second->push(rank,buffers[rank],startBuffer,size);
[300]281
[549]282      buffer.advance(size);
283      count=buffer.remain();
[489]284    }
[1757]285
286    if (timeLine>0) lastTimeLine[rank]=timeLine ;
287   
[1225]288    CTimer::get("Process request").suspend();
[300]289  }
[489]290
[300]291  void CContextServer::processEvents(void)
292  {
[549]293    map<size_t,CEventServer*>::iterator it;
294    CEventServer* event;
[489]295
[549]296    it=events.find(currentTimeLine);
[489]297    if (it!=events.end())
[300]298    {
[549]299      event=it->second;
[509]300
[300]301      if (event->isFull())
302      {
[597]303        if (!scheduled && CServer::eventScheduler) // Skip event scheduling for attached mode and reception on client side
[492]304        {
[549]305          CServer::eventScheduler->registerEvent(currentTimeLine,hashId);
306          scheduled=true;
[492]307        }
[597]308        else if (!CServer::eventScheduler || CServer::eventScheduler->queryEvent(currentTimeLine,hashId) )
[492]309        {
[851]310         // When using attached mode, synchronise the processes to avoid that differents event be scheduled by differents processes
311         // The best way to properly solve this problem will be to use the event scheduler also in attached mode
312         // for now just set up a MPI barrier
[1639]313         if (!CServer::eventScheduler && CXios::isServer) MPI_Barrier(intraComm) ;
[851]314
[549]315         CTimer::get("Process events").resume();
316         dispatchEvent(*event);
317         CTimer::get("Process events").suspend();
318         pendingEvent=false;
319         delete event;
320         events.erase(it);
321         currentTimeLine++;
322         scheduled = false;
[492]323        }
324      }
[1757]325      else getBufferFromClient(currentTimeLine) ;
[492]326    }
[1757]327    else if (pureOneSided) getBufferFromClient(currentTimeLine) ; // if pure one sided check buffer even if no event recorded at current time line
[492]328  }
[489]329
[300]330  CContextServer::~CContextServer()
331  {
[549]332    map<int,CServerBuffer*>::iterator it;
[1158]333    for(it=buffers.begin();it!=buffers.end();++it) delete it->second;
[489]334  }
[300]335
[1757]336  void CContextServer::releaseBuffers()
337  {
338    map<int,CServerBuffer*>::iterator it;
339    bool out ;
340    do
341    {
342      out=true ;
343      for(it=buffers.begin();it!=buffers.end();++it)
344      {
345//        out = out && it->second->freeWindows() ;
346
347      }
348    } while (! out) ; 
349  }
350
351  void CContextServer::notifyClientsFinalize(void)
352  {
353    for(auto it=buffers.begin();it!=buffers.end();++it)
354    {
355      it->second->notifyClientFinalize() ;
356    }
357  }
358
[300]359  void CContextServer::dispatchEvent(CEventServer& event)
360  {
[549]361    string contextName;
362    string buff;
363    int MsgSize;
364    int rank;
365    list<CEventServer::SSubEvent>::iterator it;
[1054]366    StdString ctxId = context->getId();
367    CContext::setCurrent(ctxId);
[1130]368    StdSize totalBuf = 0;
[489]369
[300]370    if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_FINALIZE)
371    {
[597]372      finished=true;
[1194]373      info(20)<<" CContextServer: Receive context <"<<context->getId()<<"> finalize."<<endl;
[1757]374//      releaseBuffers() ;
375      notifyClientsFinalize() ;
[1194]376      context->finalize();
[1757]377
378/* don't know where release windows
379      MPI_Win_free(&windows[0]) ;
380      MPI_Win_free(&windows[1]) ;
381*/     
[511]382      std::map<int, StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
[983]383                           iteMap = mapBufferSize_.end(), itMap;
[511]384      for (itMap = itbMap; itMap != iteMap; ++itMap)
385      {
[1054]386        rank = itMap->first;
[1130]387        report(10)<< " Memory report : Context <"<<ctxId<<"> : server side : memory used for buffer of each connection to client" << endl
388            << "  +) With client of rank " << rank << " : " << itMap->second << " bytes " << endl;
389        totalBuf += itMap->second;
[511]390      }
[1130]391      report(0)<< " Memory report : Context <"<<ctxId<<"> : server side : total memory used for buffer "<<totalBuf<<" bytes"<<endl;
[300]392    }
[549]393    else if (event.classId==CContext::GetType()) CContext::dispatchEvent(event);
394    else if (event.classId==CContextGroup::GetType()) CContextGroup::dispatchEvent(event);
395    else if (event.classId==CCalendarWrapper::GetType()) CCalendarWrapper::dispatchEvent(event);
396    else if (event.classId==CDomain::GetType()) CDomain::dispatchEvent(event);
397    else if (event.classId==CDomainGroup::GetType()) CDomainGroup::dispatchEvent(event);
398    else if (event.classId==CAxis::GetType()) CAxis::dispatchEvent(event);
399    else if (event.classId==CAxisGroup::GetType()) CAxisGroup::dispatchEvent(event);
[887]400    else if (event.classId==CScalar::GetType()) CScalar::dispatchEvent(event);
401    else if (event.classId==CScalarGroup::GetType()) CScalarGroup::dispatchEvent(event);
[549]402    else if (event.classId==CGrid::GetType()) CGrid::dispatchEvent(event);
403    else if (event.classId==CGridGroup::GetType()) CGridGroup::dispatchEvent(event);
404    else if (event.classId==CField::GetType()) CField::dispatchEvent(event);
405    else if (event.classId==CFieldGroup::GetType()) CFieldGroup::dispatchEvent(event);
406    else if (event.classId==CFile::GetType()) CFile::dispatchEvent(event);
407    else if (event.classId==CFileGroup::GetType()) CFileGroup::dispatchEvent(event);
408    else if (event.classId==CVariable::GetType()) CVariable::dispatchEvent(event);
[300]409    else
410    {
[549]411      ERROR("void CContextServer::dispatchEvent(CEventServer& event)",<<" Bad event class Id"<<endl);
[300]412    }
413  }
414}
Note: See TracBrowser for help on using the repository browser.