source: XIOS/dev/dev_ym/XIOS_COUPLING/src/context_client.cpp @ 1853

Last change on this file since 1853 was 1853, checked in by ymipsl, 4 years ago

Coupling branch : replace hasServer and hasClient combination by the name of correct service : CLIENT, GATHERER or OUT_SERVER.

YM

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