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

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

Two server levels: merging trunk r1200 (except for non-contiguous zoom) into dev. Tested on Curie. Todo: non-contiguous zoom.

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