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

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

MARK: branch merged with trunk @1660. Add option --omp to enable multithreading.

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