source: XIOS/trunk/src/context_server.cpp @ 779

Last change on this file since 779 was 597, checked in by rlacroix, 9 years ago

Add basic infrastructure for servers to clients communication.

  • 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: 7.3 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
[345]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
[300]59  bool CContextServer::eventLoop(void)
60  {
[549]61    listen();
62    checkPendingRequest();
63    processEvents();
64    return finished;
[300]65  }
66
67  void CContextServer::listen(void)
68  {
69    int rank;
[549]70    int flag;
71    int count;
72    char * addr;
[489]73    MPI_Status status;
[300]74    map<int,CServerBuffer*>::iterator it;
[489]75
[300]76    for(rank=0;rank<commSize;rank++)
77    {
78      if (pendingRequest.find(rank)==pendingRequest.end())
79      {
[549]80        traceOff();
[489]81        MPI_Iprobe(rank,20,interComm,&flag,&status);
[549]82        traceOn();
[300]83        if (flag==true)
84        {
[549]85          it=buffers.find(rank);
[597]86          if (it==buffers.end()) // Receive the buffer size and allocate the buffer
[300]87          {
[509]88            StdSize buffSize = 0;
89            MPI_Recv(&buffSize, 1, MPI_LONG, rank, 20, interComm, &status);
[511]90            mapBufferSize_.insert(std::make_pair(rank, buffSize));
[549]91            it=(buffers.insert(pair<int,CServerBuffer*>(rank,new CServerBuffer(buffSize)))).first;
[300]92          }
[509]93          else
94          {
[549]95            MPI_Get_count(&status,MPI_CHAR,&count);
[509]96            if (it->second->isBufferFree(count))
97            {
[549]98              addr=(char*)it->second->getBuffer(count);
99              MPI_Irecv(addr,count,MPI_CHAR,rank,20,interComm,&pendingRequest[rank]);
100              bufferRequest[rank]=addr;
[509]101            }
102          }
[300]103        }
104      }
105    }
106  }
[489]107
[300]108  void CContextServer::checkPendingRequest(void)
109  {
110    map<int,MPI_Request>::iterator it;
[549]111    list<int> recvRequest;
[300]112    list<int>::iterator itRecv;
[549]113    int rank;
114    int flag;
115    int count;
116    MPI_Status status;
[489]117
[300]118    for(it=pendingRequest.begin();it!=pendingRequest.end();it++)
119    {
[549]120      rank=it->first;
121      traceOff();
122      MPI_Test(& it->second, &flag, &status);
123      traceOn();
[300]124      if (flag==true)
125      {
[549]126        recvRequest.push_back(rank);
127        MPI_Get_count(&status,MPI_CHAR,&count);
128        processRequest(rank,bufferRequest[rank],count);
[300]129      }
130    }
[489]131
132    for(itRecv=recvRequest.begin();itRecv!=recvRequest.end();itRecv++)
[300]133    {
[549]134      pendingRequest.erase(*itRecv);
135      bufferRequest.erase(*itRecv);
[300]136    }
137  }
[489]138
[300]139  void CContextServer::processRequest(int rank, char* buff,int count)
140  {
[489]141
[549]142    CBufferIn buffer(buff,count);
143    char* startBuffer,endBuffer;
144    int size, offset;
145    size_t timeLine;
146    map<size_t,CEventServer*>::iterator it;
[489]147
[300]148    while(count>0)
149    {
[549]150      char* startBuffer=(char*)buffer.ptr();
151      CBufferIn newBuffer(startBuffer,buffer.remain());
152      newBuffer>>size>>timeLine;
[300]153
[549]154      it=events.find(timeLine);
155      if (it==events.end()) it=events.insert(pair<int,CEventServer*>(timeLine,new CEventServer)).first;
156      it->second->push(rank,buffers[rank],startBuffer,size);
[300]157
[549]158      buffer.advance(size);
159      count=buffer.remain();
[489]160    }
161
[300]162  }
[489]163
[300]164  void CContextServer::processEvents(void)
165  {
[549]166    map<size_t,CEventServer*>::iterator it;
167    CEventServer* event;
[489]168
[549]169    it=events.find(currentTimeLine);
[489]170    if (it!=events.end())
[300]171    {
[549]172      event=it->second;
[509]173
[300]174      if (event->isFull())
175      {
[597]176        if (!scheduled && CServer::eventScheduler) // Skip event scheduling for attached mode and reception on client side
[492]177        {
[549]178          CServer::eventScheduler->registerEvent(currentTimeLine,hashId);
179          scheduled=true;
[492]180        }
[597]181        else if (!CServer::eventScheduler || CServer::eventScheduler->queryEvent(currentTimeLine,hashId) )
[492]182        {
[549]183         CTimer::get("Process events").resume();
184         dispatchEvent(*event);
185         CTimer::get("Process events").suspend();
186         pendingEvent=false;
187         delete event;
188         events.erase(it);
189         currentTimeLine++;
190         scheduled = false;
[492]191        }
192      }
193    }
194  }
[489]195
[300]196  CContextServer::~CContextServer()
197  {
[549]198    map<int,CServerBuffer*>::iterator it;
199    for(it=buffers.begin();it!=buffers.end();++it) delete it->second;
[489]200  }
[300]201
202
203  void CContextServer::dispatchEvent(CEventServer& event)
204  {
[549]205    string contextName;
206    string buff;
207    int MsgSize;
208    int rank;
209    list<CEventServer::SSubEvent>::iterator it;
210    CContext::setCurrent(context->getId());
[489]211
[300]212    if (event.classId==CContext::GetType() && event.type==CContext::EVENT_ID_CONTEXT_FINALIZE)
213    {
[597]214      finished=true;
[549]215      info(20)<<"Server Side context <"<<context->getId()<<"> finalized"<<endl;
[511]216      std::map<int, StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
217                                             iteMap = mapBufferSize_.end(), itMap;
218      StdSize totalBuf = 0;
219      for (itMap = itbMap; itMap != iteMap; ++itMap)
220      {
221        report(10)<< " Memory report : Context <"<<context->getId()<<"> : server side : memory used for buffer of each connection to client" << endl
222                  << "  +) With client of rank " << itMap->first << " : " << itMap->second << " bytes " << endl;
223        totalBuf += itMap->second;
224      }
[549]225      context->finalize();
226      report(0)<< " Memory report : Context <"<<context->getId()<<"> : server side : total memory used for buffer "<<totalBuf<<" bytes"<<endl;
[300]227    }
[549]228    else if (event.classId==CContext::GetType()) CContext::dispatchEvent(event);
229    else if (event.classId==CContextGroup::GetType()) CContextGroup::dispatchEvent(event);
230    else if (event.classId==CCalendarWrapper::GetType()) CCalendarWrapper::dispatchEvent(event);
231    else if (event.classId==CDomain::GetType()) CDomain::dispatchEvent(event);
232    else if (event.classId==CDomainGroup::GetType()) CDomainGroup::dispatchEvent(event);
233    else if (event.classId==CAxis::GetType()) CAxis::dispatchEvent(event);
234    else if (event.classId==CAxisGroup::GetType()) CAxisGroup::dispatchEvent(event);
235    else if (event.classId==CGrid::GetType()) CGrid::dispatchEvent(event);
236    else if (event.classId==CGridGroup::GetType()) CGridGroup::dispatchEvent(event);
237    else if (event.classId==CField::GetType()) CField::dispatchEvent(event);
238    else if (event.classId==CFieldGroup::GetType()) CFieldGroup::dispatchEvent(event);
239    else if (event.classId==CFile::GetType()) CFile::dispatchEvent(event);
240    else if (event.classId==CFileGroup::GetType()) CFileGroup::dispatchEvent(event);
241    else if (event.classId==CVariable::GetType()) CVariable::dispatchEvent(event);
[300]242    else
243    {
[549]244      ERROR("void CContextServer::dispatchEvent(CEventServer& event)",<<" Bad event class Id"<<endl);
[300]245    }
246  }
247}
Note: See TracBrowser for help on using the repository browser.