source: XIOS/dev/dev_ym/XIOS_SERVICES/src/context_server.cpp @ 2142

Last change on this file since 2142 was 1765, checked in by ymipsl, 5 years ago

Some cleaning On XIOS services branch

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