source: XIOS/dev/XIOS_DEV_CMIP6/src/context_server.cpp @ 1228

Last change on this file since 1228 was 1228, checked in by oabramkina, 7 years ago

Resolving a deadlock introduced in r1225.

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