source: XIOS/dev/dev_olga/src/context_server.cpp @ 1130

Last change on this file since 1130 was 1130, checked in by oabramkina, 3 years ago

Two-level server: merging new grid functionalities and changes in the communication protocol (e.g. non-blocking context finalize, registries, oasis).

Tests on curie: test_client, test_complete, nemo (test_xios2_cmip6.exe).

To do: non-structured grid, check reading, possible bug in client/server initialization (?).

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