source: XIOS/dev/dev_olga/src/context_client.cpp @ 1025

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

Intermeadiate version for merging with new server functionalities.

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