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

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

branch merged with trunk @1645. arch file (ep&mpi) added for ADA

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