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

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

save dev. TO DO : test with xios

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