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

Last change on this file since 1027 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
RevLine 
[591]1#include "xios_spl.hpp"
[300]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"
[382]10#include "mpi.hpp"
[347]11#include "timer.hpp"
[401]12#include "cxios.hpp"
[300]13
[335]14namespace xios
[300]15{
[512]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
[983]20    \cxtSer [in] cxtSer Pointer to context of server side. (It is only used in case of attached mode).
[512]21    */
[595]22    CContextClient::CContextClient(CContext* parent, MPI_Comm intraComm_, MPI_Comm interComm_, CContext* cxtSer)
[917]23     : mapBufferSize_(), parentServer(cxtSer), maxBufferedEvents(4)
[300]24    {
[595]25      context = parent;
26      intraComm = intraComm_;
27      interComm = interComm_;
28      MPI_Comm_rank(intraComm, &clientRank);
29      MPI_Comm_size(intraComm, &clientSize);
[509]30
[595]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);
[509]35
[595]36      if (clientSize < serverSize)
37      {
38        int serverByClient = serverSize / clientSize;
39        int remain = serverSize % clientSize;
40        int rankStart = serverByClient * clientRank;
[300]41
[595]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);
[1021]52
53        ranksServerNotLeader.resize(0);      }
[595]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));
[1021]63          else
64            ranksServerNotLeader.push_back(clientRank / (clientByServer + 1));
[595]65        }
66        else
67        {
68          int rank = clientRank - (clientByServer + 1) * remain;
69          if (rank % clientByServer == 0)
70            ranksServerLeader.push_back(remain + rank / clientByServer);
[1021]71          else
72            ranksServerNotLeader.push_back(remain + rank / clientByServer);
[595]73        }
74      }
75
76      timeLine = 0;
[300]77    }
78
[512]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    */
[300]83    void CContextClient::sendEvent(CEventClient& event)
84    {
[731]85      list<int> ranks = event.getRanks();
[595]86      if (!event.isEmpty())
[300]87      {
[731]88        list<int> sizes = event.getSizes();
[300]89
[595]90        list<CBufferOut*> buffList = getBuffers(ranks, sizes);
[509]91
[731]92        event.send(timeLine, sizes, buffList);
93
[595]94        checkBuffers(ranks);
[300]95      }
96
[704]97      if (isAttachedModeEnabled())
[511]98      {
99        waitEvent(ranks);
100        CContext::setCurrent(context->getId());
101      }
102
[595]103      timeLine++;
[300]104    }
[509]105
[512]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    */
[300]111    void CContextClient::waitEvent(list<int>& ranks)
112    {
[595]113      parentServer->server->setPendingEvent();
114      while (checkBuffers(ranks))
[300]115      {
[595]116        parentServer->server->listen();
117        parentServer->server->checkPendingRequest();
[300]118      }
[386]119
[595]120      while (parentServer->server->hasPendingEvent())
[386]121      {
[595]122       parentServer->server->eventLoop();
[386]123      }
[300]124    }
125
[512]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    */
[300]132    list<CBufferOut*> CContextClient::getBuffers(list<int>& serverList, list<int>& sizeList)
133    {
[595]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;
[884]139      bool areBuffersFree;
[300]140
[595]141      for (itServer = serverList.begin(); itServer != serverList.end(); itServer++)
[300]142      {
[595]143        it = buffers.find(*itServer);
144        if (it == buffers.end())
[300]145        {
[595]146          newBuffer(*itServer);
147          it = buffers.find(*itServer);
[509]148        }
[595]149        bufferList.push_back(it->second);
[300]150      }
[347]151
152      CTimer::get("Blocking time").resume();
[884]153      do
[300]154      {
[884]155        areBuffersFree = true;
[595]156        for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
[884]157          areBuffersFree &= (*itBuffer)->isBufferFree(*itSize);
158
159        if (!areBuffersFree)
[300]160        {
[884]161          checkBuffers();
162          context->server->listen();
[300]163        }
[884]164      } while (!areBuffersFree);
[347]165      CTimer::get("Blocking time").suspend();
166
[595]167      for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
[300]168      {
[595]169        retBuffer.push_back((*itBuffer)->getBuffer(*itSize));
[300]170      }
[595]171      return retBuffer;
[300]172   }
[509]173
[512]174   /*!
175   Make a new buffer for a certain connection to server with specific rank
176   \param [in] rank rank of connected server
177   */
[300]178   void CContextClient::newBuffer(int rank)
179   {
[724]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      }
[917]185      CClientBuffer* buffer = buffers[rank] = new CClientBuffer(interComm, rank, mapBufferSize_[rank], maxBufferedEvents);
[725]186      // Notify the server
187      CBufferOut* bufOut = buffer->getBuffer(sizeof(StdSize));
188      bufOut->put(mapBufferSize_[rank]); // Stupid C++
189      buffer->checkBuffer();
[509]190   }
[300]191
[512]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   */
[300]196   bool CContextClient::checkBuffers(void)
197   {
[595]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;
[509]202   }
[300]203
[512]204   //! Release all buffers
[300]205   void CContextClient::releaseBuffers(void)
206   {
[595]207      map<int,CClientBuffer*>::iterator itBuff;
208      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) delete itBuff->second;
[509]209   }
[300]210
[512]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   */
[300]216   bool CContextClient::checkBuffers(list<int>& ranks)
217   {
[595]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;
[509]222   }
[300]223
[512]224   /*!
[917]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
[512]229   */
[917]230   void CContextClient::setBufferSize(const std::map<int,StdSize>& mapSize, const std::map<int,StdSize>& maxEventSize)
[509]231   {
232     mapBufferSize_ = mapSize;
[917]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
[509]250   }
251
[1021]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
[595]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  }
[509]278
[595]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  }
[300]287
[704]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  }
[697]297
[512]298   /*!
299   Finalize context client and do some reports
300   */
[300]301   void CContextClient::finalize(void)
302   {
[595]303     map<int,CClientBuffer*>::iterator itBuff;
304     bool stop = true;
[731]305
[595]306     CEventClient event(CContext::GetType(), CContext::EVENT_ID_CONTEXT_FINALIZE);
[300]307     if (isServerLeader())
308     {
[595]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);
[300]314     }
[595]315     else sendEvent(event);
[509]316
[347]317     CTimer::get("Blocking time").resume();
[595]318     while (stop)
[300]319     {
[595]320       checkBuffers();
321       stop = false;
322       for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) stop |= itBuff->second->hasPendingRequest();
[300]323     }
[347]324     CTimer::get("Blocking time").suspend();
[509]325
[595]326     std::map<int,StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
327                                           iteMap = mapBufferSize_.end(), itMap;
[511]328     StdSize totalBuf = 0;
329     for (itMap = itbMap; itMap != iteMap; ++itMap)
330     {
[595]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;
[511]333       totalBuf += itMap->second;
334     }
[595]335     report(0) << " Memory report : Context <" << context->getId() << "> : client side : total memory used for buffer " << totalBuf << " bytes" << endl;
[511]336
[595]337     releaseBuffers();
[300]338   }
[509]339}
Note: See TracBrowser for help on using the repository browser.