source: XIOS/dev/branch_openmp/src/context_server.cpp @ 1342

Last change on this file since 1342 was 1342, checked in by yushan, 6 years ago

log files

  • 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: 8.7 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"
[1328]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
[1328]20using namespace ep_lib;
[300]21
[335]22namespace xios
[300]23{
24
[1328]25  CContextServer::CContextServer(CContext* parent,MPI_Comm intraComm_,MPI_Comm interComm_)
[300]26  {
[549]27    context=parent;
28    intraComm=intraComm_;
29    MPI_Comm_size(intraComm,&intraCommSize);
30    MPI_Comm_rank(intraComm,&intraCommRank);
31    interComm=interComm_;
32    int flag;
33    MPI_Comm_test_inter(interComm,&flag);
[300]34    if (flag) MPI_Comm_remote_size(interComm,&commSize);
[549]35    else  MPI_Comm_size(interComm,&commSize);
36    currentTimeLine=0;
37    scheduled=false;
38    finished=false;
[509]39
[549]40    boost::hash<string> hashString;
41    hashId=hashString(context->getId());
[492]42
[300]43  }
44  void CContextServer::setPendingEvent(void)
45  {
[549]46    pendingEvent=true;
[300]47  }
[489]48
[300]49  bool CContextServer::hasPendingEvent(void)
50  {
[549]51    return pendingEvent;
[300]52  }
[489]53
[597]54  bool CContextServer::hasFinished(void)
55  {
56    return finished;
57  }
58
[1033]59  bool CContextServer::eventLoop(bool enableEventsProcessing /*= true*/)
[300]60  {
[549]61    listen();
62    checkPendingRequest();
[1033]63    if (enableEventsProcessing)
64      processEvents();
[549]65    return finished;
[300]66  }
67
68  void CContextServer::listen(void)
69  {
70    int rank;
[549]71    int flag;
72    int count;
73    char * addr;
[1328]74    MPI_Status status;
[300]75    map<int,CServerBuffer*>::iterator it;
[1328]76    bool okLoop;
[489]77
[1328]78    traceOff();
79    MPI_Iprobe(-2, 20,interComm,&flag,&status);
80    traceOn();
81
82    if (flag==true)
[300]83    {
[1328]84      #ifdef _usingMPI
85      rank=status.MPI_SOURCE ;
86      #elif _usingEP
87      rank=status.ep_src ;
88      #endif
89      okLoop = true;
[300]90      if (pendingRequest.find(rank)==pendingRequest.end())
[1328]91        okLoop = !listenPendingRequest(status) ;
92      if (okLoop)
[300]93      {
[1328]94        for(rank=0;rank<commSize;rank++)
[300]95        {
[1328]96          if (pendingRequest.find(rank)==pendingRequest.end())
[300]97          {
[1328]98
99            traceOff();
100            MPI_Iprobe(rank, 20,interComm,&flag,&status);
101            traceOn();
102            if (flag==true) listenPendingRequest(status) ;
[300]103          }
104        }
105      }
106    }
107  }
[489]108
[1328]109  bool CContextServer::listenPendingRequest(MPI_Status& status)
110  {
111    int count;
112    char * addr;
113    map<int,CServerBuffer*>::iterator it;
114    #ifdef _usingMPI
115    int rank=status.MPI_SOURCE ;
116    #elif _usingEP
117    int rank=status.ep_src;
118    #endif
119
120    it=buffers.find(rank);
121    if (it==buffers.end()) // Receive the buffer size and allocate the buffer
122    {
123       StdSize buffSize = 0;
124       MPI_Recv(&buffSize, 1, MPI_LONG, rank, 20, interComm, &status);
125       mapBufferSize_.insert(std::make_pair(rank, buffSize));
126       it=(buffers.insert(pair<int,CServerBuffer*>(rank,new CServerBuffer(buffSize)))).first;
127       return true;
128    }
129    else
130    {
131      MPI_Get_count(&status,MPI_CHAR,&count);
132      if (it->second->isBufferFree(count))
133      {
134         addr=(char*)it->second->getBuffer(count);
135         MPI_Irecv(addr,count,MPI_CHAR,rank,20,interComm,&pendingRequest[rank]);
136         bufferRequest[rank]=addr;
137         return true;
138      }
139      else
140        return false;
141    }
142  }
143
[300]144  void CContextServer::checkPendingRequest(void)
145  {
[1328]146    map<int,MPI_Request>::iterator it;
[549]147    list<int> recvRequest;
[300]148    list<int>::iterator itRecv;
[549]149    int rank;
150    int flag;
151    int count;
[1328]152    MPI_Status status;
[489]153
[1328]154    for(it=pendingRequest.begin();it!=pendingRequest.end();it++)
[300]155    {
[549]156      rank=it->first;
157      traceOff();
[1328]158      MPI_Test(& it->second, &flag, &status);
[549]159      traceOn();
[300]160      if (flag==true)
161      {
[549]162        recvRequest.push_back(rank);
[1328]163        MPI_Get_count(&status,MPI_CHAR,&count);
[549]164        processRequest(rank,bufferRequest[rank],count);
[300]165      }
166    }
[489]167
168    for(itRecv=recvRequest.begin();itRecv!=recvRequest.end();itRecv++)
[300]169    {
[549]170      pendingRequest.erase(*itRecv);
171      bufferRequest.erase(*itRecv);
[300]172    }
173  }
[489]174
[300]175  void CContextServer::processRequest(int rank, char* buff,int count)
176  {
[489]177
[549]178    CBufferIn buffer(buff,count);
179    char* startBuffer,endBuffer;
180    int size, offset;
181    size_t timeLine;
182    map<size_t,CEventServer*>::iterator it;
[489]183
[300]184    while(count>0)
185    {
[549]186      char* startBuffer=(char*)buffer.ptr();
187      CBufferIn newBuffer(startBuffer,buffer.remain());
188      newBuffer>>size>>timeLine;
[300]189
[549]190      it=events.find(timeLine);
191      if (it==events.end()) it=events.insert(pair<int,CEventServer*>(timeLine,new CEventServer)).first;
192      it->second->push(rank,buffers[rank],startBuffer,size);
[300]193
[549]194      buffer.advance(size);
195      count=buffer.remain();
[489]196    }
197
[300]198  }
[489]199
[300]200  void CContextServer::processEvents(void)
201  {
[549]202    map<size_t,CEventServer*>::iterator it;
203    CEventServer* event;
[489]204
[549]205    it=events.find(currentTimeLine);
[489]206    if (it!=events.end())
[300]207    {
[549]208      event=it->second;
[509]209
[300]210      if (event->isFull())
211      {
[597]212        if (!scheduled && CServer::eventScheduler) // Skip event scheduling for attached mode and reception on client side
[492]213        {
[549]214          CServer::eventScheduler->registerEvent(currentTimeLine,hashId);
215          scheduled=true;
[492]216        }
[597]217        else if (!CServer::eventScheduler || CServer::eventScheduler->queryEvent(currentTimeLine,hashId) )
[492]218        {
[851]219         // When using attached mode, synchronise the processes to avoid that differents event be scheduled by differents processes
220         // The best way to properly solve this problem will be to use the event scheduler also in attached mode
221         // for now just set up a MPI barrier
[998]222         if (!CServer::eventScheduler && CXios::isServer) MPI_Barrier(intraComm) ;
[851]223
[549]224         CTimer::get("Process events").resume();
225         dispatchEvent(*event);
226         CTimer::get("Process events").suspend();
227         pendingEvent=false;
228         delete event;
229         events.erase(it);
230         currentTimeLine++;
231         scheduled = false;
[492]232        }
233      }
234    }
235  }
[489]236
[300]237  CContextServer::~CContextServer()
238  {
[549]239    map<int,CServerBuffer*>::iterator it;
240    for(it=buffers.begin();it!=buffers.end();++it) delete it->second;
[489]241  }
[300]242
243
244  void CContextServer::dispatchEvent(CEventServer& event)
245  {
[549]246    string contextName;
247    string buff;
248    int MsgSize;
249    int rank;
250    list<CEventServer::SSubEvent>::iterator it;
251    CContext::setCurrent(context->getId());
[489]252
[300]253    if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_FINALIZE)
254    {
[597]255      finished=true;
[1342]256      #pragma omp critical (_output)
257      info(20)<<"Server Side context <"<<context->getId()<<"> finalized"<<endl;
[511]258      std::map<int, StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
259                                             iteMap = mapBufferSize_.end(), itMap;
260      StdSize totalBuf = 0;
261      for (itMap = itbMap; itMap != iteMap; ++itMap)
262      {
[1338]263        //report(10)<< " Memory report : Context <"<<context->getId()<<"> : server side : memory used for buffer of each connection to client" << endl
264        //          << "  +) With client of rank " << itMap->first << " : " << itMap->second << " bytes " << endl;
[511]265        totalBuf += itMap->second;
266      }
[549]267      context->finalize();
[1338]268      //report(0)<< " Memory report : Context <"<<context->getId()<<"> : server side : total memory used for buffer "<<totalBuf<<" bytes"<<endl;
[300]269    }
[549]270    else if (event.classId==CContext::GetType()) CContext::dispatchEvent(event);
271    else if (event.classId==CContextGroup::GetType()) CContextGroup::dispatchEvent(event);
272    else if (event.classId==CCalendarWrapper::GetType()) CCalendarWrapper::dispatchEvent(event);
273    else if (event.classId==CDomain::GetType()) CDomain::dispatchEvent(event);
274    else if (event.classId==CDomainGroup::GetType()) CDomainGroup::dispatchEvent(event);
275    else if (event.classId==CAxis::GetType()) CAxis::dispatchEvent(event);
276    else if (event.classId==CAxisGroup::GetType()) CAxisGroup::dispatchEvent(event);
[887]277    else if (event.classId==CScalar::GetType()) CScalar::dispatchEvent(event);
278    else if (event.classId==CScalarGroup::GetType()) CScalarGroup::dispatchEvent(event);
[549]279    else if (event.classId==CGrid::GetType()) CGrid::dispatchEvent(event);
280    else if (event.classId==CGridGroup::GetType()) CGridGroup::dispatchEvent(event);
281    else if (event.classId==CField::GetType()) CField::dispatchEvent(event);
282    else if (event.classId==CFieldGroup::GetType()) CFieldGroup::dispatchEvent(event);
283    else if (event.classId==CFile::GetType()) CFile::dispatchEvent(event);
284    else if (event.classId==CFileGroup::GetType()) CFileGroup::dispatchEvent(event);
285    else if (event.classId==CVariable::GetType()) CVariable::dispatchEvent(event);
[300]286    else
287    {
[549]288      ERROR("void CContextServer::dispatchEvent(CEventServer& event)",<<" Bad event class Id"<<endl);
[300]289    }
290  }
291}
Note: See TracBrowser for help on using the repository browser.