source: XIOS/dev/branch_yushan/src/context_server.cpp @ 1116

Last change on this file since 1116 was 1094, checked in by yushan, 7 years ago

modif for Curie, CurrContext?->CurrContext_ptr in object_factory and group_factory

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