source: XIOS/dev/branch_yushan/src/context_client.cpp @ 1070

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

Preperation for merge from trunk

  • 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: 10.9 KB
Line 
1#include "xios_spl.hpp"
2#include "context_client.hpp"
3#include "context_server.hpp"
4#include "event_client.hpp"
5#include "buffer_out.hpp"
6#include "buffer_client.hpp"
7#include "type.hpp"
8#include "event_client.hpp"
9#include "context.hpp"
10#include "mpi.hpp"
11#include "timer.hpp"
12#include "cxios.hpp"
13
14namespace xios
15{
16    /*!
17    \param [in] parent Pointer to context on client side
18    \param [in] intraComm_ communicator of group client
19    \param [in] interComm_ communicator of group server
20    \cxtSer [in] cxtSer Pointer to context of server side. (It is only used on case of attached mode)
21    */
22    CContextClient::CContextClient(CContext* parent, ep_lib::MPI_Comm intraComm_, ep_lib::MPI_Comm interComm_, CContext* cxtSer)
23     : mapBufferSize_(), parentServer(cxtSer), maxBufferedEvents(4)
24    {
25      context = parent;
26      intraComm = intraComm_;
27      interComm = interComm_;
28      MPI_Comm_rank(intraComm, &clientRank);
29      MPI_Comm_size(intraComm, &clientSize);
30
31      int flag;
32      MPI_Comm_test_inter(interComm, &flag);
33      if (flag) MPI_Comm_remote_size(interComm, &serverSize);
34      else  MPI_Comm_size(interComm, &serverSize);
35
36      if (clientSize < serverSize)
37      {
38        int serverByClient = serverSize / clientSize;
39        int remain = serverSize % clientSize;
40        int rankStart = serverByClient * clientRank;
41
42        if (clientRank < remain)
43        {
44          serverByClient++;
45          rankStart += clientRank;
46        }
47        else
48          rankStart += remain;
49
50        for (int i = 0; i < serverByClient; i++)
51          ranksServerLeader.push_back(rankStart + i);
52      }
53      else
54      {
55        int clientByServer = clientSize / serverSize;
56        int remain = clientSize % serverSize;
57
58        if (clientRank < (clientByServer + 1) * remain)
59        {
60          if (clientRank % (clientByServer + 1) == 0)
61            ranksServerLeader.push_back(clientRank / (clientByServer + 1));
62        }
63        else
64        {
65          int rank = clientRank - (clientByServer + 1) * remain;
66          if (rank % clientByServer == 0)
67            ranksServerLeader.push_back(remain + rank / clientByServer);
68        }
69      }
70
71      timeLine = 0;
72    }
73
74    /*!
75    In case of attached mode, the current context must be reset to context for client
76    \param [in] event Event sent to server
77    */
78    void CContextClient::sendEvent(CEventClient& event)
79    {
80      list<int> ranks = event.getRanks();
81      if (!event.isEmpty())
82      {
83        list<int> sizes = event.getSizes();
84
85        list<CBufferOut*> buffList = getBuffers(ranks, sizes);
86
87        event.send(timeLine, sizes, buffList);
88
89        checkBuffers(ranks);
90      }
91
92      if (isAttachedModeEnabled())
93      {
94        waitEvent(ranks);
95        CContext::setCurrent(context->getId());
96      }
97
98      timeLine++;
99    }
100
101    /*!
102    If client is also server (attached mode), after sending event, it should process right away
103    the incoming event.
104    \param [in] ranks list rank of server connected this client
105    */
106    void CContextClient::waitEvent(list<int>& ranks)
107    {
108      parentServer->server->setPendingEvent();
109      size_t pendingmapSize;
110      while (checkBuffers(ranks))
111      {
112        parentServer->server->listen(); 
113        parentServer->server->checkPendingRequest(); 
114      }
115
116      while (parentServer->server->hasPendingEvent())
117      {
118       parentServer->server->eventLoop();
119      }
120    }
121
122    /*!
123    Setup buffer for each connection to server and verify their state to put content into them
124    \param [in] serverList list of rank of connected server
125    \param [in] sizeList size of message corresponding to each connection
126    \return List of buffer input which event can be placed
127    */
128    list<CBufferOut*> CContextClient::getBuffers(list<int>& serverList, list<int>& sizeList)
129    {
130      list<int>::iterator itServer, itSize;
131      list<CClientBuffer*> bufferList;
132      map<int,CClientBuffer*>::iterator it;
133      list<CClientBuffer*>::iterator itBuffer;
134      list<CBufferOut*>  retBuffer;
135      bool areBuffersFree;
136
137      for (itServer = serverList.begin(); itServer != serverList.end(); itServer++)
138      {
139        it = buffers.find(*itServer);
140        if (it == buffers.end())
141        {
142          newBuffer(*itServer);
143          it = buffers.find(*itServer);
144        }
145        bufferList.push_back(it->second);
146      }
147
148      CTimer::get("Blocking time").resume();
149      do
150      {
151        areBuffersFree = true;
152        for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
153          areBuffersFree &= (*itBuffer)->isBufferFree(*itSize);
154
155        if (!areBuffersFree)
156        {
157          checkBuffers();
158          context->server->listen();
159        }
160      } while (!areBuffersFree);
161      CTimer::get("Blocking time").suspend();
162
163      for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
164      {
165        CBufferOut* m_buf = (*itBuffer)->getBuffer(*itSize);
166        //retBuffer.push_back((*itBuffer)->getBuffer(*itSize));
167        //int m_size = retBuffer.size();
168        //retBuffer.resize(m_size+1);
169        //m_size = retBuffer.size();
170        retBuffer.push_back(m_buf);
171      }
172      return retBuffer;
173   }
174
175   /*!
176   Make a new buffer for a certain connection to server with specific rank
177   \param [in] rank rank of connected server
178   */
179   void CContextClient::newBuffer(int rank)
180   {
181      if (!mapBufferSize_.count(rank))
182      {
183        error(0) << "WARNING: Unexpected request for buffer to communicate with server " << rank << std::endl;
184        mapBufferSize_[rank] = CXios::minBufferSize;
185      }
186      CClientBuffer* buffer = buffers[rank] = new CClientBuffer(interComm, rank, mapBufferSize_[rank], maxBufferedEvents);
187      // Notify the server
188      CBufferOut* bufOut = buffer->getBuffer(sizeof(StdSize));
189      bufOut->put(mapBufferSize_[rank]); // Stupid C++
190      buffer->checkBuffer();
191   }
192
193   /*!
194   Verify state of buffers. Buffer is under pending state if there is no message on it
195   \return state of buffers, pending(true), ready(false)
196   */
197   bool CContextClient::checkBuffers(void)
198   {
199      map<int,CClientBuffer*>::iterator itBuff;
200      bool pending = false;
201      if(! buffers.empty())
202      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) pending |= itBuff->second->checkBuffer();
203      return pending;
204   }
205
206   //! Release all buffers
207   void CContextClient::releaseBuffers(void)
208   {
209      map<int,CClientBuffer*>::iterator itBuff;
210      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) delete itBuff->second;
211   }
212
213   /*!
214   Verify state of buffers corresponding to a connection
215   \param [in] ranks list rank of server to which client connects to
216   \return state of buffers, pending(true), ready(false)
217   */
218   bool CContextClient::checkBuffers(list<int>& ranks)
219   {
220      list<int>::iterator it;
221      bool pending = false;
222      for (it = ranks.begin(); it != ranks.end(); it++) pending |= buffers[*it]->checkBuffer();
223      return pending;
224   }
225
226   /*!
227    * Set the buffer size for each connection. Warning: This function is collective.
228    *
229    * \param [in] mapSize maps the rank of the connected servers to the size of the correspoinding buffer
230    * \param [in] maxEventSize maps the rank of the connected servers to the size of the biggest event
231   */
232   void CContextClient::setBufferSize(const std::map<int,StdSize>& mapSize, const std::map<int,StdSize>& maxEventSize)
233   {
234     mapBufferSize_ = mapSize;
235
236     // Compute the maximum number of events that can be safely buffered.
237     double minBufferSizeEventSizeRatio = std::numeric_limits<double>::max();
238     for (std::map<int,StdSize>::const_iterator it = mapSize.begin(), ite = mapSize.end(); it != ite; ++it)
239     {
240       double ratio = double(it->second) / maxEventSize.at(it->first);
241       if (ratio < minBufferSizeEventSizeRatio) minBufferSizeEventSizeRatio = ratio;
242     }
243     #ifdef _usingMPI
244     MPI_Allreduce(MPI_IN_PLACE, &minBufferSizeEventSizeRatio, 1, MPI_DOUBLE, MPI_MIN, intraComm);
245     #elif _usingEP
246     MPI_Allreduce(&minBufferSizeEventSizeRatio, &minBufferSizeEventSizeRatio, 1, MPI_DOUBLE, MPI_MIN, intraComm);
247     #endif
248     
249     if (minBufferSizeEventSizeRatio < 1.0)
250       ERROR("void CContextClient::setBufferSize(const std::map<int,StdSize>& mapSize, const std::map<int,StdSize>& maxEventSize)",
251             << "The buffer sizes and the maximum events sizes are incoherent.");
252
253     maxBufferedEvents = size_t(2 * minBufferSizeEventSizeRatio) // there is room for two local buffers on the server
254                          + size_t(minBufferSizeEventSizeRatio)  // one local buffer can always be fully used
255                          + 1;                                   // the other local buffer might contain only one event
256   }
257
258  /*!
259  Get leading server in the group of connected server
260  \return ranks of leading servers
261  */
262  const std::list<int>& CContextClient::getRanksServerLeader(void) const
263  {
264    return ranksServerLeader;
265  }
266
267  /*!
268  Check if client connects to leading server
269  \return connected(true), not connected (false)
270  */
271  bool CContextClient::isServerLeader(void) const
272  {
273    return !ranksServerLeader.empty();
274  }
275
276  /*!
277   * Check if the attached mode is used.
278   *
279   * \return true if and only if attached mode is used
280   */
281  bool CContextClient::isAttachedModeEnabled() const
282  {
283    return (parentServer != 0);
284  }
285
286   /*!
287   Finalize context client and do some reports
288   */
289   void CContextClient::finalize(void)
290   {
291     map<int,CClientBuffer*>::iterator itBuff;
292     bool stop = true;
293
294     CEventClient event(CContext::GetType(), CContext::EVENT_ID_CONTEXT_FINALIZE);
295     if (isServerLeader())
296     {
297       CMessage msg;
298       const std::list<int>& ranks = getRanksServerLeader();
299       for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
300         event.push(*itRank, 1, msg);
301       sendEvent(event);
302     }
303     else sendEvent(event);
304
305     CTimer::get("Blocking time").resume();
306     while (stop)
307     {
308       checkBuffers();
309       stop = false;
310       for (itBuff = buffers.begin(); itBuff != buffers.end(); ++itBuff) stop |= itBuff->second->hasPendingRequest();
311     }
312     CTimer::get("Blocking time").suspend();
313
314     std::map<int,StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
315                                           iteMap = mapBufferSize_.end(), itMap;
316     StdSize totalBuf = 0;
317     for (itMap = itbMap; itMap != iteMap; ++itMap)
318     {
319       report(10) << " Memory report : Context <" << context->getId() << "> : client side : memory used for buffer of each connection to server" << endl
320                  << "  +) To server with rank " << itMap->first << " : " << itMap->second << " bytes " << endl;
321       totalBuf += itMap->second;
322     }
323     report(0) << " Memory report : Context <" << context->getId() << "> : client side : total memory used for buffer " << totalBuf << " bytes" << endl;
324
325     releaseBuffers();
326   }
327}
Note: See TracBrowser for help on using the repository browser.