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

Last change on this file since 1169 was 1033, checked in by rlacroix, 7 years ago

Make the XIOS server(s) completely non-blocking.

This fixes some deadlocks caused by bugs in the communication protocol when using inputs and multiple contexts.

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