source: XIOS/dev/dev_olga/src/context_server.cpp @ 992

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

First rebond on the secondary server pool. XIOS finalizes correctly.

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