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

Last change on this file since 1037 was 1037, checked in by yushan, 7 years ago

initialize the branch

  • 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.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"
[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
20    \cxtSer [in] cxtSer Pointer to context of server side. (It is only used on case of attached mode)
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);
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);
[1037]68        }
[595]69      }
70
71      timeLine = 0;
[300]72    }
73
[512]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    */
[300]78    void CContextClient::sendEvent(CEventClient& event)
79    {
[731]80      list<int> ranks = event.getRanks();
[595]81      if (!event.isEmpty())
[300]82      {
[731]83        list<int> sizes = event.getSizes();
[300]84
[1037]85        list<CBufferOut*> buffList = getBuffers(ranks, sizes);
[509]86
[1037]87        event.send(timeLine, sizes, buffList);
[731]88
[1037]89        checkBuffers(ranks);
[300]90      }
91
[1037]92      if (isAttachedModeEnabled())
[511]93      {
[1037]94        waitEvent(ranks);
95        CContext::setCurrent(context->getId());
[511]96      }
97
[1037]98      timeLine++;
[300]99    }
[509]100
[512]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    */
[300]106    void CContextClient::waitEvent(list<int>& ranks)
107    {
[595]108      parentServer->server->setPendingEvent();
[1037]109      size_t pendingmapSize;
[595]110      while (checkBuffers(ranks))
[300]111      {
[1037]112        parentServer->server->listen(); 
113        parentServer->server->checkPendingRequest(); 
[300]114      }
[386]115
[595]116      while (parentServer->server->hasPendingEvent())
[386]117      {
[1037]118       parentServer->server->eventLoop(); //printf("parentServer->server->eventLoop()\n");
[386]119      }
[300]120    }
121
[512]122    /*!
[1037]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
[512]127    */
[1037]128    list<CBufferOut*> CContextClient::getBuffers(list<int>& serverList, list<int>& sizeList)
[300]129    {
[1037]130      list<int>::iterator itServer, itSize;
[595]131      list<CClientBuffer*> bufferList;
[1037]132      map<int,CClientBuffer*>::iterator it;
[595]133      list<CClientBuffer*>::iterator itBuffer;
[1037]134      list<CBufferOut*>  retBuffer;
[884]135      bool areBuffersFree;
[300]136
[595]137      for (itServer = serverList.begin(); itServer != serverList.end(); itServer++)
[300]138      {
[595]139        it = buffers.find(*itServer);
140        if (it == buffers.end())
[300]141        {
[595]142          newBuffer(*itServer);
143          it = buffers.find(*itServer);
[509]144        }
[595]145        bufferList.push_back(it->second);
[300]146      }
[347]147
148      CTimer::get("Blocking time").resume();
[884]149      do
[300]150      {
[884]151        areBuffersFree = true;
[595]152        for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
[884]153          areBuffersFree &= (*itBuffer)->isBufferFree(*itSize);
154
155        if (!areBuffersFree)
[300]156        {
[884]157          checkBuffers();
158          context->server->listen();
[300]159        }
[1037]160      } while (!areBuffersFree);
[347]161      CTimer::get("Blocking time").suspend();
162
[1037]163      for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
[300]164      {
[1037]165        retBuffer.push_back((*itBuffer)->getBuffer(*itSize));
[300]166      }
[1037]167      return retBuffer;
[300]168   }
[509]169
[512]170   /*!
171   Make a new buffer for a certain connection to server with specific rank
172   \param [in] rank rank of connected server
173   */
[300]174   void CContextClient::newBuffer(int rank)
175   {
[724]176      if (!mapBufferSize_.count(rank))
177      {
178        error(0) << "WARNING: Unexpected request for buffer to communicate with server " << rank << std::endl;
179        mapBufferSize_[rank] = CXios::minBufferSize;
180      }
[917]181      CClientBuffer* buffer = buffers[rank] = new CClientBuffer(interComm, rank, mapBufferSize_[rank], maxBufferedEvents);
[725]182      // Notify the server
183      CBufferOut* bufOut = buffer->getBuffer(sizeof(StdSize));
184      bufOut->put(mapBufferSize_[rank]); // Stupid C++
185      buffer->checkBuffer();
[509]186   }
[300]187
[512]188   /*!
189   Verify state of buffers. Buffer is under pending state if there is no message on it
190   \return state of buffers, pending(true), ready(false)
191   */
[300]192   bool CContextClient::checkBuffers(void)
193   {
[595]194      map<int,CClientBuffer*>::iterator itBuff;
195      bool pending = false;
196      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) pending |= itBuff->second->checkBuffer();
197      return pending;
[509]198   }
[300]199
[512]200   //! Release all buffers
[300]201   void CContextClient::releaseBuffers(void)
202   {
[595]203      map<int,CClientBuffer*>::iterator itBuff;
204      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) delete itBuff->second;
[509]205   }
[300]206
[512]207   /*!
208   Verify state of buffers corresponding to a connection
209   \param [in] ranks list rank of server to which client connects to
210   \return state of buffers, pending(true), ready(false)
211   */
[300]212   bool CContextClient::checkBuffers(list<int>& ranks)
213   {
[595]214      list<int>::iterator it;
215      bool pending = false;
216      for (it = ranks.begin(); it != ranks.end(); it++) pending |= buffers[*it]->checkBuffer();
217      return pending;
[509]218   }
[300]219
[512]220   /*!
[917]221    * Set the buffer size for each connection. Warning: This function is collective.
222    *
223    * \param [in] mapSize maps the rank of the connected servers to the size of the correspoinding buffer
224    * \param [in] maxEventSize maps the rank of the connected servers to the size of the biggest event
[512]225   */
[917]226   void CContextClient::setBufferSize(const std::map<int,StdSize>& mapSize, const std::map<int,StdSize>& maxEventSize)
[509]227   {
228     mapBufferSize_ = mapSize;
[917]229
230     // Compute the maximum number of events that can be safely buffered.
231     double minBufferSizeEventSizeRatio = std::numeric_limits<double>::max();
232     for (std::map<int,StdSize>::const_iterator it = mapSize.begin(), ite = mapSize.end(); it != ite; ++it)
233     {
234       double ratio = double(it->second) / maxEventSize.at(it->first);
235       if (ratio < minBufferSizeEventSizeRatio) minBufferSizeEventSizeRatio = ratio;
236     }
[1037]237     #ifdef _usingMPI
[917]238     MPI_Allreduce(MPI_IN_PLACE, &minBufferSizeEventSizeRatio, 1, MPI_DOUBLE, MPI_MIN, intraComm);
[1037]239     #elif _usingEP
240     MPI_Allreduce(&minBufferSizeEventSizeRatio, &minBufferSizeEventSizeRatio, 1, MPI_DOUBLE, MPI_MIN, intraComm);
241     #endif
242     
[917]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
[595]252  /*!
253  Get leading server in the group of connected server
254  \return ranks of leading servers
255  */
256  const std::list<int>& CContextClient::getRanksServerLeader(void) const
257  {
258    return ranksServerLeader;
259  }
[509]260
[595]261  /*!
262  Check if client connects to leading server
263  \return connected(true), not connected (false)
264  */
265  bool CContextClient::isServerLeader(void) const
266  {
267    return !ranksServerLeader.empty();
268  }
[300]269
[704]270  /*!
271   * Check if the attached mode is used.
272   *
273   * \return true if and only if attached mode is used
274   */
275  bool CContextClient::isAttachedModeEnabled() const
276  {
277    return (parentServer != 0);
278  }
[697]279
[512]280   /*!
281   Finalize context client and do some reports
282   */
[300]283   void CContextClient::finalize(void)
284   {
[595]285     map<int,CClientBuffer*>::iterator itBuff;
[1037]286     bool stop = true;
[731]287
[595]288     CEventClient event(CContext::GetType(), CContext::EVENT_ID_CONTEXT_FINALIZE);
[300]289     if (isServerLeader())
290     {
[595]291       CMessage msg;
292       const std::list<int>& ranks = getRanksServerLeader();
293       for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
294         event.push(*itRank, 1, msg);
295       sendEvent(event);
[300]296     }
[595]297     else sendEvent(event);
[509]298
[347]299     CTimer::get("Blocking time").resume();
[1037]300     while (stop)
[300]301     {
[595]302       checkBuffers();
[1037]303       stop = false;
304       for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) stop |= itBuff->second->hasPendingRequest();
[300]305     }
[347]306     CTimer::get("Blocking time").suspend();
[509]307
[595]308     std::map<int,StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
309                                           iteMap = mapBufferSize_.end(), itMap;
[511]310     StdSize totalBuf = 0;
311     for (itMap = itbMap; itMap != iteMap; ++itMap)
312     {
[595]313       report(10) << " Memory report : Context <" << context->getId() << "> : client side : memory used for buffer of each connection to server" << endl
314                  << "  +) To server with rank " << itMap->first << " : " << itMap->second << " bytes " << endl;
[511]315       totalBuf += itMap->second;
316     }
[595]317     report(0) << " Memory report : Context <" << context->getId() << "> : client side : total memory used for buffer " << totalBuf << " bytes" << endl;
[511]318
[595]319     releaseBuffers();
[300]320   }
[509]321}
Note: See TracBrowser for help on using the repository browser.