source: XIOS/dev/branch_openmp/src/context_server.cpp @ 1501

Last change on this file since 1501 was 1460, checked in by yushan, 6 years ago

branch_openmp merged with XIOS_DEV_CMIP6@1459

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