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

Last change on this file since 1071 was 1071, checked in by oabramkina, 4 years ago

dev: test for secondary servers added.

  • 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.2 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  StdSize CContextServer::totalBuf_ = 0;
25
26  CContextServer::CContextServer(CContext* parent, MPI_Comm intraComm_,MPI_Comm interComm_)
27  {
28    context=parent;
29    intraComm=intraComm_;
30    MPI_Comm_size(intraComm,&intraCommSize);
31    MPI_Comm_rank(intraComm,&intraCommRank);
32
33    interComm=interComm_;
34    int flag;
35    MPI_Comm_test_inter(interComm,&flag);
36    if (flag) MPI_Comm_remote_size(interComm,&commSize);
37    else  MPI_Comm_size(interComm,&commSize);
38
39    currentTimeLine=0;
40    scheduled=false;
41    finished=false;
42    boost::hash<string> hashString;
43    hashId=hashString(context->getId());
44  }
45
46  void CContextServer::setPendingEvent(void)
47  {
48    pendingEvent=true;
49  }
50
51
52  bool CContextServer::hasPendingEvent(void)
53  {
54    return pendingEvent;
55  }
56
57  bool CContextServer::hasFinished(void)
58  {
59    return finished;
60  }
61
62  bool CContextServer::eventLoop(bool enableEventsProcessing /*= true*/)
63  {
64    listen();
65    checkPendingRequest();
66    if (enableEventsProcessing)
67      processEvents();
68    return finished;
69  }
70
71  void CContextServer::listen(void)
72  {
73    int rank;
74    int flag;
75    int count;
76    char * addr;
77    MPI_Status status;
78    map<int,CServerBuffer*>::iterator it;
79
80    for(rank=0;rank<commSize;rank++)
81    {
82      if (pendingRequest.find(rank)==pendingRequest.end())
83      {
84        traceOff();
85        MPI_Iprobe(rank,20,interComm,&flag,&status);
86        traceOn();
87        if (flag==true)
88        {
89          it=buffers.find(rank);
90          if (it==buffers.end()) // Receive the buffer size and allocate the buffer
91          {
92            StdSize buffSize = 0;
93            MPI_Recv(&buffSize, 1, MPI_LONG, rank, 20, interComm, &status);
94            mapBufferSize_.insert(std::make_pair(rank, buffSize));
95            it=(buffers.insert(pair<int,CServerBuffer*>(rank,new CServerBuffer(buffSize)))).first;
96          }
97          else
98          {
99            MPI_Get_count(&status,MPI_CHAR,&count);
100            if (it->second->isBufferFree(count))
101            {
102              addr=(char*)it->second->getBuffer(count);
103              MPI_Irecv(addr,count,MPI_CHAR,rank,20,interComm,&pendingRequest[rank]);
104              bufferRequest[rank]=addr;
105            }
106          }
107        }
108      }
109    }
110  }
111
112  void CContextServer::checkPendingRequest(void)
113  {
114    map<int,MPI_Request>::iterator it;
115    list<int> recvRequest;
116    list<int>::iterator itRecv;
117    int rank;
118    int flag;
119    int count;
120    MPI_Status status;
121
122    for(it=pendingRequest.begin();it!=pendingRequest.end();it++)
123    {
124      rank=it->first;
125      traceOff();
126      MPI_Test(& it->second, &flag, &status);
127      traceOn();
128      if (flag==true)
129      {
130        recvRequest.push_back(rank);
131        MPI_Get_count(&status,MPI_CHAR,&count);
132        processRequest(rank,bufferRequest[rank],count);
133      }
134    }
135
136    for(itRecv=recvRequest.begin();itRecv!=recvRequest.end();itRecv++)
137    {
138      pendingRequest.erase(*itRecv);
139      bufferRequest.erase(*itRecv);
140    }
141  }
142
143  void CContextServer::processRequest(int rank, char* buff,int count)
144  {
145
146    CBufferIn buffer(buff,count);
147    char* startBuffer,endBuffer;
148    int size, offset;
149    size_t timeLine;
150    map<size_t,CEventServer*>::iterator it;
151
152    while(count>0)
153    {
154      char* startBuffer=(char*)buffer.ptr();
155      CBufferIn newBuffer(startBuffer,buffer.remain());
156      newBuffer>>size>>timeLine;
157
158      it=events.find(timeLine);
159      if (it==events.end()) it=events.insert(pair<int,CEventServer*>(timeLine,new CEventServer)).first;
160      it->second->push(rank,buffers[rank],startBuffer,size);
161
162      buffer.advance(size);
163      count=buffer.remain();
164    }
165  }
166
167  void CContextServer::processEvents(void)
168  {
169    map<size_t,CEventServer*>::iterator it;
170    CEventServer* event;
171    boost::hash<string> hashString;
172    size_t hashId=hashString(context->getId());
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) 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//    CContext::setCurrent(context->getId());
221    StdString ctxId = context->getId();
222    CContext::setCurrent(ctxId);
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
239//      report(0)<< " Memory report : Context <"<<ctxId<<"> : server side : total memory used for buffer "<<totalBuf<<" bytes"<<endl; // moved to CContext::finalize()
240    }
241    else if (event.classId==CContext::GetType()) CContext::dispatchEvent(event);
242    else if (event.classId==CContextGroup::GetType()) CContextGroup::dispatchEvent(event);
243    else if (event.classId==CCalendarWrapper::GetType()) CCalendarWrapper::dispatchEvent(event);
244    else if (event.classId==CDomain::GetType()) CDomain::dispatchEvent(event);
245    else if (event.classId==CDomainGroup::GetType()) CDomainGroup::dispatchEvent(event);
246    else if (event.classId==CAxis::GetType()) CAxis::dispatchEvent(event);
247    else if (event.classId==CAxisGroup::GetType()) CAxisGroup::dispatchEvent(event);
248    else if (event.classId==CScalar::GetType()) CScalar::dispatchEvent(event);
249    else if (event.classId==CScalarGroup::GetType()) CScalarGroup::dispatchEvent(event);
250    else if (event.classId==CGrid::GetType()) CGrid::dispatchEvent(event);
251    else if (event.classId==CGridGroup::GetType()) CGridGroup::dispatchEvent(event);
252    else if (event.classId==CField::GetType()) CField::dispatchEvent(event);
253    else if (event.classId==CFieldGroup::GetType()) CFieldGroup::dispatchEvent(event);
254    else if (event.classId==CFile::GetType()) CFile::dispatchEvent(event);
255    else if (event.classId==CFileGroup::GetType()) CFileGroup::dispatchEvent(event);
256    else if (event.classId==CVariable::GetType()) CVariable::dispatchEvent(event);
257    else
258    {
259      ERROR("void CContextServer::dispatchEvent(CEventServer& event)",<<" Bad event class Id"<<endl);
260    }
261  }
262
263  size_t CContextServer::getTotalBuf(void)
264  {
265    return totalBuf_;
266  }
267
268}
Note: See TracBrowser for help on using the repository browser.