source: XIOS/dev/dev_trunk_omp/src/context_server.cpp @ 1745

Last change on this file since 1745 was 1745, checked in by yushan, 5 years ago

Generic_testcase: build flag --omp to enable EP with intelmpi. --omp2 to enable EP with openmpi. tested on Irene with compiler=intel17, mpi=intelmpi&openmpi, with and without EP

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