source: XIOS/dev/branch_openmp/src/context_client.cpp @ 1328

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

dev_omp

  • 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: 13.7 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"
[1328]13using namespace ep_lib;
[300]14
[335]15namespace xios
[300]16{
[512]17    /*!
18    \param [in] parent Pointer to context on client side
19    \param [in] intraComm_ communicator of group client
20    \param [in] interComm_ communicator of group server
21    \cxtSer [in] cxtSer Pointer to context of server side. (It is only used on case of attached mode)
22    */
[1328]23    CContextClient::CContextClient(CContext* parent, MPI_Comm intraComm_, MPI_Comm interComm_, CContext* cxtSer)
[917]24     : mapBufferSize_(), parentServer(cxtSer), maxBufferedEvents(4)
[300]25    {
[595]26      context = parent;
27      intraComm = intraComm_;
28      interComm = interComm_;
29      MPI_Comm_rank(intraComm, &clientRank);
30      MPI_Comm_size(intraComm, &clientSize);
[509]31
[595]32      int flag;
33      MPI_Comm_test_inter(interComm, &flag);
34      if (flag) MPI_Comm_remote_size(interComm, &serverSize);
35      else  MPI_Comm_size(interComm, &serverSize);
[509]36
[595]37      if (clientSize < serverSize)
38      {
39        int serverByClient = serverSize / clientSize;
40        int remain = serverSize % clientSize;
41        int rankStart = serverByClient * clientRank;
[300]42
[595]43        if (clientRank < remain)
44        {
45          serverByClient++;
46          rankStart += clientRank;
47        }
48        else
49          rankStart += remain;
50
51        for (int i = 0; i < serverByClient; i++)
52          ranksServerLeader.push_back(rankStart + i);
[988]53
54        ranksServerNotLeader.resize(0);
[595]55      }
56      else
57      {
58        int clientByServer = clientSize / serverSize;
59        int remain = clientSize % serverSize;
60
61        if (clientRank < (clientByServer + 1) * remain)
62        {
63          if (clientRank % (clientByServer + 1) == 0)
64            ranksServerLeader.push_back(clientRank / (clientByServer + 1));
[988]65          else
66            ranksServerNotLeader.push_back(clientRank / (clientByServer + 1));
[595]67        }
68        else
69        {
70          int rank = clientRank - (clientByServer + 1) * remain;
71          if (rank % clientByServer == 0)
72            ranksServerLeader.push_back(remain + rank / clientByServer);
[988]73          else
74            ranksServerNotLeader.push_back(remain + rank / clientByServer);
75        }       
[595]76      }
77
78      timeLine = 0;
[300]79    }
80
[512]81    /*!
82    In case of attached mode, the current context must be reset to context for client
83    \param [in] event Event sent to server
84    */
[300]85    void CContextClient::sendEvent(CEventClient& event)
86    {
[731]87      list<int> ranks = event.getRanks();
[1033]88
[595]89      if (!event.isEmpty())
[300]90      {
[731]91        list<int> sizes = event.getSizes();
[300]92
[1033]93        // We force the getBuffers call to be non-blocking on the servers
94        list<CBufferOut*> buffList;
95        bool couldBuffer = getBuffers(ranks, sizes, buffList, !CXios::isClient);
[509]96
[1033]97        if (couldBuffer)
98        {
99          event.send(timeLine, sizes, buffList);
[731]100
[1033]101          checkBuffers(ranks);
102
103          if (isAttachedModeEnabled()) // couldBuffer is always true in attached mode
104          {
105            waitEvent(ranks);
106            CContext::setCurrent(context->getId());
107          }
108        }
109        else
110        {
111          tmpBufferedEvent.ranks = ranks;
112          tmpBufferedEvent.sizes = sizes;
113
114          for (list<int>::const_iterator it = sizes.begin(); it != sizes.end(); it++)
115            tmpBufferedEvent.buffers.push_back(new CBufferOut(*it));
116
117          event.send(timeLine, tmpBufferedEvent.sizes, tmpBufferedEvent.buffers);
118        }
[300]119      }
120
[1033]121      timeLine++;
122    }
123
124    /*!
125     * Send the temporarily buffered event (if any).
126     *
127     * \return true if a temporarily buffered event could be sent, false otherwise
128     */
129    bool CContextClient::sendTemporarilyBufferedEvent()
130    {
131      bool couldSendTmpBufferedEvent = false;
132
133      if (hasTemporarilyBufferedEvent())
[511]134      {
[1033]135        list<CBufferOut*> buffList;
136        if (getBuffers(tmpBufferedEvent.ranks, tmpBufferedEvent.sizes, buffList, true)) // Non-blocking call
137        {
138          list<CBufferOut*>::iterator it, itBuffer;
139
140          for (it = tmpBufferedEvent.buffers.begin(), itBuffer = buffList.begin(); it != tmpBufferedEvent.buffers.end(); it++, itBuffer++)
141            (*itBuffer)->put((char*)(*it)->start(), (*it)->count());
142
143          checkBuffers(tmpBufferedEvent.ranks);
144
145          tmpBufferedEvent.clear();
146
147          couldSendTmpBufferedEvent = true;
148        }
[511]149      }
150
[1033]151      return couldSendTmpBufferedEvent;
[300]152    }
[509]153
[512]154    /*!
155    If client is also server (attached mode), after sending event, it should process right away
156    the incoming event.
157    \param [in] ranks list rank of server connected this client
158    */
[300]159    void CContextClient::waitEvent(list<int>& ranks)
160    {
[595]161      parentServer->server->setPendingEvent();
162      while (checkBuffers(ranks))
[300]163      {
[595]164        parentServer->server->listen();
165        parentServer->server->checkPendingRequest();
[300]166      }
[386]167
[595]168      while (parentServer->server->hasPendingEvent())
[386]169      {
[595]170       parentServer->server->eventLoop();
[386]171      }
[300]172    }
173
[512]174    /*!
[1033]175     * Get buffers for each connection to the servers. This function blocks until there is enough room in the buffers unless
176     * it is explicitly requested to be non-blocking.
177     *
178     * \param [in] serverList list of rank of connected server
179     * \param [in] sizeList size of message corresponding to each connection
180     * \param [out] retBuffers list of buffers that can be used to store an event
181     * \param [in] nonBlocking whether this function should be non-blocking
182     * \return whether the already allocated buffers could be used
[512]183    */
[1033]184    bool CContextClient::getBuffers(const list<int>& serverList, const list<int>& sizeList, list<CBufferOut*>& retBuffers, bool nonBlocking /*= false*/)
[300]185    {
[1033]186      list<int>::const_iterator itServer, itSize;
[595]187      list<CClientBuffer*> bufferList;
[1033]188      map<int,CClientBuffer*>::const_iterator it;
[595]189      list<CClientBuffer*>::iterator itBuffer;
[884]190      bool areBuffersFree;
[300]191
[595]192      for (itServer = serverList.begin(); itServer != serverList.end(); itServer++)
[300]193      {
[595]194        it = buffers.find(*itServer);
195        if (it == buffers.end())
[300]196        {
[595]197          newBuffer(*itServer);
198          it = buffers.find(*itServer);
[509]199        }
[595]200        bufferList.push_back(it->second);
[300]201      }
[347]202
203      CTimer::get("Blocking time").resume();
[884]204      do
[300]205      {
[884]206        areBuffersFree = true;
[595]207        for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
[884]208          areBuffersFree &= (*itBuffer)->isBufferFree(*itSize);
209
210        if (!areBuffersFree)
[300]211        {
[884]212          checkBuffers();
213          context->server->listen();
[300]214        }
[1033]215      } while (!areBuffersFree && !nonBlocking);
[347]216      CTimer::get("Blocking time").suspend();
217
[1033]218      if (areBuffersFree)
[300]219      {
[1033]220        for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
221          retBuffers.push_back((*itBuffer)->getBuffer(*itSize));
[300]222      }
[1033]223
224      return areBuffersFree;
[300]225   }
[509]226
[512]227   /*!
228   Make a new buffer for a certain connection to server with specific rank
229   \param [in] rank rank of connected server
230   */
[300]231   void CContextClient::newBuffer(int rank)
232   {
[724]233      if (!mapBufferSize_.count(rank))
234      {
235        error(0) << "WARNING: Unexpected request for buffer to communicate with server " << rank << std::endl;
236        mapBufferSize_[rank] = CXios::minBufferSize;
[1205]237        maxEventSizes[rank] = CXios::minBufferSize;
[724]238      }
[1205]239      CClientBuffer* buffer = buffers[rank] = new CClientBuffer(interComm, rank, mapBufferSize_[rank], maxEventSizes[rank], maxBufferedEvents);
[725]240      // Notify the server
241      CBufferOut* bufOut = buffer->getBuffer(sizeof(StdSize));
242      bufOut->put(mapBufferSize_[rank]); // Stupid C++
243      buffer->checkBuffer();
[509]244   }
[300]245
[512]246   /*!
247   Verify state of buffers. Buffer is under pending state if there is no message on it
248   \return state of buffers, pending(true), ready(false)
249   */
[300]250   bool CContextClient::checkBuffers(void)
251   {
[595]252      map<int,CClientBuffer*>::iterator itBuff;
253      bool pending = false;
254      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) pending |= itBuff->second->checkBuffer();
255      return pending;
[509]256   }
[300]257
[512]258   //! Release all buffers
[300]259   void CContextClient::releaseBuffers(void)
260   {
[595]261      map<int,CClientBuffer*>::iterator itBuff;
262      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) delete itBuff->second;
[509]263   }
[300]264
[512]265   /*!
266   Verify state of buffers corresponding to a connection
267   \param [in] ranks list rank of server to which client connects to
268   \return state of buffers, pending(true), ready(false)
269   */
[300]270   bool CContextClient::checkBuffers(list<int>& ranks)
271   {
[595]272      list<int>::iterator it;
273      bool pending = false;
274      for (it = ranks.begin(); it != ranks.end(); it++) pending |= buffers[*it]->checkBuffer();
275      return pending;
[509]276   }
[300]277
[512]278   /*!
[917]279    * Set the buffer size for each connection. Warning: This function is collective.
280    *
281    * \param [in] mapSize maps the rank of the connected servers to the size of the correspoinding buffer
282    * \param [in] maxEventSize maps the rank of the connected servers to the size of the biggest event
[512]283   */
[917]284   void CContextClient::setBufferSize(const std::map<int,StdSize>& mapSize, const std::map<int,StdSize>& maxEventSize)
[509]285   {
286     mapBufferSize_ = mapSize;
[1205]287     maxEventSizes = maxEventSize;
[917]288
289     // Compute the maximum number of events that can be safely buffered.
290     double minBufferSizeEventSizeRatio = std::numeric_limits<double>::max();
291     for (std::map<int,StdSize>::const_iterator it = mapSize.begin(), ite = mapSize.end(); it != ite; ++it)
292     {
293       double ratio = double(it->second) / maxEventSize.at(it->first);
294       if (ratio < minBufferSizeEventSizeRatio) minBufferSizeEventSizeRatio = ratio;
295     }
[1328]296     //MPI_Allreduce(MPI_IN_PLACE, &minBufferSizeEventSizeRatio, 1, MPI_DOUBLE, MPI_MIN, intraComm);
[1134]297     MPI_Allreduce(&minBufferSizeEventSizeRatio, &minBufferSizeEventSizeRatio, 1, MPI_DOUBLE, MPI_MIN, intraComm);
[1328]298
[917]299     if (minBufferSizeEventSizeRatio < 1.0)
[1205]300     {
[917]301       ERROR("void CContextClient::setBufferSize(const std::map<int,StdSize>& mapSize, const std::map<int,StdSize>& maxEventSize)",
302             << "The buffer sizes and the maximum events sizes are incoherent.");
[1205]303     }
304     else if (minBufferSizeEventSizeRatio == std::numeric_limits<double>::max())
305       minBufferSizeEventSizeRatio = 1.0; // In this case, maxBufferedEvents will never be used but we want to avoid any floating point exception
[917]306
307     maxBufferedEvents = size_t(2 * minBufferSizeEventSizeRatio) // there is room for two local buffers on the server
308                          + size_t(minBufferSizeEventSizeRatio)  // one local buffer can always be fully used
309                          + 1;                                   // the other local buffer might contain only one event
[509]310   }
311
[595]312  /*!
313  Get leading server in the group of connected server
314  \return ranks of leading servers
315  */
[988]316  const std::list<int>& CContextClient::getRanksServerNotLeader(void) const
317  {
318    return ranksServerNotLeader;
319  }
320
321  /*!
322  Check if client connects to leading server
323  \return connected(true), not connected (false)
324  */
325  bool CContextClient::isServerNotLeader(void) const
326  {
327    return !ranksServerNotLeader.empty();
328  }
329
330  /*!
331  Get leading server in the group of connected server
332  \return ranks of leading servers
333  */
[595]334  const std::list<int>& CContextClient::getRanksServerLeader(void) const
335  {
336    return ranksServerLeader;
337  }
[509]338
[595]339  /*!
340  Check if client connects to leading server
341  \return connected(true), not connected (false)
342  */
343  bool CContextClient::isServerLeader(void) const
344  {
345    return !ranksServerLeader.empty();
346  }
[300]347
[704]348  /*!
349   * Check if the attached mode is used.
350   *
351   * \return true if and only if attached mode is used
352   */
353  bool CContextClient::isAttachedModeEnabled() const
354  {
355    return (parentServer != 0);
356  }
[697]357
[512]358   /*!
359   Finalize context client and do some reports
360   */
[300]361   void CContextClient::finalize(void)
362   {
[595]363     map<int,CClientBuffer*>::iterator itBuff;
[1033]364     bool stop = false;
[731]365
[1033]366     CTimer::get("Blocking time").resume();
367     while (hasTemporarilyBufferedEvent())
368     {
369       checkBuffers();
370       sendTemporarilyBufferedEvent();
371     }
372     CTimer::get("Blocking time").suspend();
373
[595]374     CEventClient event(CContext::GetType(), CContext::EVENT_ID_CONTEXT_FINALIZE);
[300]375     if (isServerLeader())
376     {
[595]377       CMessage msg;
378       const std::list<int>& ranks = getRanksServerLeader();
379       for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
380         event.push(*itRank, 1, msg);
381       sendEvent(event);
[300]382     }
[595]383     else sendEvent(event);
[509]384
[347]385     CTimer::get("Blocking time").resume();
[1033]386     while (!stop)
[300]387     {
[595]388       checkBuffers();
[1033]389       if (hasTemporarilyBufferedEvent())
390         sendTemporarilyBufferedEvent();
391
392       stop = true;
393       for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) stop &= !itBuff->second->hasPendingRequest();
[300]394     }
[347]395     CTimer::get("Blocking time").suspend();
[509]396
[595]397     std::map<int,StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
398                                           iteMap = mapBufferSize_.end(), itMap;
[511]399     StdSize totalBuf = 0;
400     for (itMap = itbMap; itMap != iteMap; ++itMap)
401     {
[1328]402       report(10) << " Memory report : Context <" << context->getId() << "> : client side : memory used for buffer of each connection to server" << endl
403                  << "  +) To server with rank " << itMap->first << " : " << itMap->second << " bytes " << endl;
[511]404       totalBuf += itMap->second;
405     }
[1328]406     report(0) << " Memory report : Context <" << context->getId() << "> : client side : total memory used for buffer " << totalBuf << " bytes" << endl;
[511]407
[595]408     releaseBuffers();
[300]409   }
[509]410}
Note: See TracBrowser for help on using the repository browser.