source: XIOS/dev/dev_trunk_omp/src/context_client.cpp @ 1771

Last change on this file since 1771 was 1665, checked in by yushan, 5 years ago

MARK: branch merged with trunk @1660. Add option --omp to enable multithreading.

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