source: XIOS/dev/dev_ym/XIOS_SERVICES/src/context_client.cpp @ 1761

Last change on this file since 1761 was 1761, checked in by ymipsl, 5 years ago

implementing first guess for service functionnalities.

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: 17.0 KB
Line 
1#include "xios_spl.hpp"
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"
10#include "mpi.hpp"
11#include "timer.hpp"
12#include "cxios.hpp"
13#include "server.hpp"
14
15namespace xios
16{
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
21    \cxtSer [in] cxtSer Pointer to context of server side. (It is only used in case of attached mode).
22    */
23    CContextClient::CContextClient(CContext* parent, MPI_Comm intraComm_, MPI_Comm interComm_, CContext* cxtSer)
24     : mapBufferSize_(), parentServer(cxtSer), maxBufferedEvents(4)
25    {
26     
27      context = parent;
28      intraComm = intraComm_;
29      interComm = interComm_;
30      MPI_Comm_rank(intraComm, &clientRank);
31      MPI_Comm_size(intraComm, &clientSize);
32
33      int flag;
34      MPI_Comm_test_inter(interComm, &flag);
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
43      if (flag) MPI_Comm_remote_size(interComm, &serverSize);
44      else  MPI_Comm_size(interComm, &serverSize);
45
46      computeLeader(clientRank, clientSize, serverSize, ranksServerLeader, ranksServerNotLeader);
47
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;
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
77      if (clientSize < serverSize)
78      {
79        int serverByClient = serverSize / clientSize;
80        int remain = serverSize % clientSize;
81        int rankStart = serverByClient * clientRank;
82
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++)
92          rankRecvLeader.push_back(rankStart + i);
93
94        rankRecvNotLeader.resize(0);
95      }
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)
104            rankRecvLeader.push_back(clientRank / (clientByServer + 1));
105          else
106            rankRecvNotLeader.push_back(clientRank / (clientByServer + 1));
107        }
108        else
109        {
110          int rank = clientRank - (clientByServer + 1) * remain;
111          if (rank % clientByServer == 0)
112            rankRecvLeader.push_back(remain + rank / clientByServer);
113          else
114            rankRecvNotLeader.push_back(remain + rank / clientByServer);
115        }
116      }
117    }
118
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    */
123    void CContextClient::sendEvent(CEventClient& event)
124    {
125      list<int> ranks = event.getRanks();
126      info(100)<<"Event "<<timeLine<<" of context "<<context->getId()<<endl ;
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() ;
132//        MPI_Allreduce(&timeLine,&timeLine_out, 1, MPI_UINT64_T, MPI_SUM, intraComm) ; // MPI_UINT64_T standardized by MPI 3
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) ;
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
143      if (!event.isEmpty())
144      {
145        list<int> sizes = event.getSizes();
146
147         // We force the getBuffers call to be non-blocking on classical servers
148        list<CBufferOut*> buffList;
149        getBuffers(timeLine, ranks, sizes, buffList) ;
150
151        event.send(timeLine, sizes, buffList);
152       
153        //for (auto itRank = ranks.begin(); itRank != ranks.end(); itRank++) buffers[*itRank]->infoBuffer() ;
154
155        unlockBuffers(ranks) ;
156        info(100)<<"Event "<<timeLine<<" of context "<<context->getId()<<"  sent"<<endl ;
157         
158        checkBuffers(ranks);
159      }
160     
161      if (isAttachedModeEnabled()) // couldBuffer is always true in attached mode
162      {
163        waitEvent(ranks);
164        CContext::setCurrent(context->getId());
165      }
166     
167      timeLine++;
168    }
169
170    /*!
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    */
175    void CContextClient::waitEvent(list<int>& ranks)
176    {
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    {
200      parentServer->server->setPendingEvent();
201      while (checkBuffers(ranks))
202      {
203        parentServer->server->listen();
204        parentServer->server->checkPendingRequest();
205      }
206
207      while (parentServer->server->hasPendingEvent())
208      {
209       parentServer->server->eventLoop();
210      }
211    }
212
213    /*!
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     *
217     *
218     * \param [in] timeLine time line of the event which will be sent to servers
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
224    */
225    bool CContextClient::getBuffers(const size_t timeLine, const list<int>& serverList, const list<int>& sizeList, list<CBufferOut*>& retBuffers,
226                                    bool nonBlocking /*= false*/)
227    {
228      list<int>::const_iterator itServer, itSize;
229      list<CClientBuffer*> bufferList;
230      map<int,CClientBuffer*>::const_iterator it;
231      list<CClientBuffer*>::iterator itBuffer;
232      bool areBuffersFree;
233
234      for (itServer = serverList.begin(); itServer != serverList.end(); itServer++)
235      {
236        it = buffers.find(*itServer);
237        if (it == buffers.end())
238        {
239          newBuffer(*itServer);
240          it = buffers.find(*itServer);
241        }
242        bufferList.push_back(it->second);
243      }
244
245      CTimer::get("Blocking time").resume();
246      do
247      {
248        areBuffersFree = true;
249        for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
250        {
251          areBuffersFree &= (*itBuffer)->isBufferFree(*itSize);
252        }
253
254        if (!areBuffersFree)
255        {
256          for (itBuffer = bufferList.begin(); itBuffer != bufferList.end(); itBuffer++) (*itBuffer)->unlockBuffer();
257          checkBuffers();
258         
259          context->server->listen();
260
261          if (context->serverPrimServer.size()>0)
262          {
263            for (int i = 0; i < context->serverPrimServer.size(); ++i)  context->serverPrimServer[i]->listen();
264            CServer::contextEventLoop(false) ; // avoid dead-lock at finalize...
265          }
266
267        }
268      } while (!areBuffersFree && !nonBlocking);
269      CTimer::get("Blocking time").suspend();
270
271      if (areBuffersFree)
272      {
273        for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
274          retBuffers.push_back((*itBuffer)->getBuffer(timeLine, *itSize));
275      }
276      return areBuffersFree;
277   }
278
279   /*!
280   Make a new buffer for a certain connection to server with specific rank
281   \param [in] rank rank of connected server
282   */
283   void CContextClient::newBuffer(int rank)
284   {
285      if (!mapBufferSize_.count(rank))
286      {
287        error(0) << "WARNING: Unexpected request for buffer to communicate with server " << rank << std::endl;
288        mapBufferSize_[rank] = CXios::minBufferSize;
289        maxEventSizes[rank] = CXios::minBufferSize;
290      }
291     
292      vector<MPI_Win> Wins(2,MPI_WIN_NULL) ;
293      if (!isAttachedModeEnabled()) Wins=windows[rank] ;
294 
295      CClientBuffer* buffer = buffers[rank] = new CClientBuffer(interComm, Wins, clientRank, rank, mapBufferSize_[rank], maxEventSizes[rank]);
296      // Notify the server
297      CBufferOut* bufOut = buffer->getBuffer(0, 3*sizeof(MPI_Aint));
298      MPI_Aint sendBuff[3] ;
299      sendBuff[0]=mapBufferSize_[rank]; // Stupid C++
300      sendBuff[1]=buffers[rank]->getWinAddress(0); 
301      sendBuff[2]=buffers[rank]->getWinAddress(1); 
302      info(100)<<"CContextClient::newBuffer : rank "<<rank<<" winAdress[0] "<<buffers[rank]->getWinAddress(0)<<" winAdress[1] "<<buffers[rank]->getWinAddress(1)<<endl;
303      bufOut->put(sendBuff, 3); // Stupid C++
304      buffer->checkBuffer(true);
305
306/*
307      if (!isAttachedModeEnabled()) // create windows only in server mode
308      {
309        MPI_Comm OneSidedInterComm, oneSidedComm ;
310        MPI_Intercomm_create(commSelf, 0, interCommMerged, clientSize+rank, 0, &OneSidedInterComm );
311        MPI_Intercomm_merge(OneSidedInterComm,false,&oneSidedComm);
312        buffer->createWindows(oneSidedComm) ;
313      }
314 */     
315   }
316
317   /*!
318   Verify state of buffers. Buffer is under pending state if there is no message on it
319   \return state of buffers, pending(true), ready(false)
320   */
321   bool CContextClient::checkBuffers(void)
322   {
323      map<int,CClientBuffer*>::iterator itBuff;
324      bool pending = false;
325      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++)
326        pending |= itBuff->second->checkBuffer(!pureOneSided);
327      return pending;
328   }
329
330   //! Release all buffers
331   void CContextClient::releaseBuffers()
332   {
333      map<int,CClientBuffer*>::iterator itBuff;
334      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++)
335      {
336         delete itBuff->second;
337      }
338      buffers.clear();
339
340/* don't know when release windows
341
342      if (!isAttachedModeEnabled())
343      { 
344        for(int rank=0; rank<serverSize; rank++)
345        {
346          MPI_Win_free(&windows[rank][0]);
347          MPI_Win_free(&windows[rank][1]);
348        }
349      }
350*/
351   }
352
353     
354  /*!
355   Lock the buffers for one sided communications
356   \param [in] ranks list rank of server to which client connects to
357   */
358   void CContextClient::lockBuffers(list<int>& ranks)
359   {
360      list<int>::iterator it;
361      for (it = ranks.begin(); it != ranks.end(); it++) buffers[*it]->lockBuffer();
362   }
363
364  /*!
365   Unlock the buffers for one sided communications
366   \param [in] ranks list rank of server to which client connects to
367   */
368   void CContextClient::unlockBuffers(list<int>& ranks)
369   {
370      list<int>::iterator it;
371      for (it = ranks.begin(); it != ranks.end(); it++) buffers[*it]->unlockBuffer();
372   }
373     
374   /*!
375   Verify state of buffers corresponding to a connection
376   \param [in] ranks list rank of server to which client connects to
377   \return state of buffers, pending(true), ready(false)
378   */
379   bool CContextClient::checkBuffers(list<int>& ranks)
380   {
381      list<int>::iterator it;
382      bool pending = false;
383      for (it = ranks.begin(); it != ranks.end(); it++) pending |= buffers[*it]->checkBuffer(!pureOneSided);
384      return pending;
385   }
386
387   /*!
388    * Set the buffer size for each connection. Warning: This function is collective.
389    *
390    * \param [in] mapSize maps the rank of the connected servers to the size of the correspoinding buffer
391    * \param [in] maxEventSize maps the rank of the connected servers to the size of the biggest event
392   */
393   void CContextClient::setBufferSize(const std::map<int,StdSize>& mapSize, const std::map<int,StdSize>& maxEventSize)
394   {
395     mapBufferSize_ = mapSize;
396     maxEventSizes = maxEventSize;
397   }
398
399  /*!
400  Get leading server in the group of connected server
401  \return ranks of leading servers
402  */
403  const std::list<int>& CContextClient::getRanksServerNotLeader(void) const
404  {
405    return ranksServerNotLeader;
406  }
407
408  /*!
409  Check if client connects to leading server
410  \return connected(true), not connected (false)
411  */
412  bool CContextClient::isServerNotLeader(void) const
413  {
414    return !ranksServerNotLeader.empty();
415  }
416
417  /*!
418  Get leading server in the group of connected server
419  \return ranks of leading servers
420  */
421  const std::list<int>& CContextClient::getRanksServerLeader(void) const
422  {
423    return ranksServerLeader;
424  }
425
426  /*!
427  Check if client connects to leading server
428  \return connected(true), not connected (false)
429  */
430  bool CContextClient::isServerLeader(void) const
431  {
432    return !ranksServerLeader.empty();
433  }
434
435   /*!
436   * Finalize context client and do some reports. Function is non-blocking.
437   */
438  void CContextClient::finalize(void)
439  {
440    map<int,CClientBuffer*>::iterator itBuff;
441    std::list<int>::iterator ItServerLeader; 
442   
443    bool stop = false;
444
445    int* nbServerConnectionLocal  = new int[serverSize] ;
446    int* nbServerConnectionGlobal  = new int[serverSize] ;
447    for(int i=0;i<serverSize;++i) nbServerConnectionLocal[i]=0 ;
448    for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++)  nbServerConnectionLocal[itBuff->first]=1 ;
449    for (ItServerLeader = ranksServerLeader.begin(); ItServerLeader != ranksServerLeader.end(); ItServerLeader++)  nbServerConnectionLocal[*ItServerLeader]=1 ;
450   
451    MPI_Allreduce(nbServerConnectionLocal, nbServerConnectionGlobal, serverSize, MPI_INT, MPI_SUM, intraComm);
452   
453    CEventClient event(CContext::GetType(), CContext::EVENT_ID_CONTEXT_FINALIZE);
454    CMessage msg;
455
456    for (int i=0;i<serverSize;++i) if (nbServerConnectionLocal[i]==1) event.push(i, nbServerConnectionGlobal[i], msg) ;
457    sendEvent(event);
458
459    delete[] nbServerConnectionLocal ;
460    delete[] nbServerConnectionGlobal ;
461/*   
462    if (isServerLeader())
463    {
464      CMessage msg;
465      const std::list<int>& ranks = getRanksServerLeader();
466      for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
467      {
468        info(100)<<"DEBUG : Sent context Finalize event to rank "<<*itRank<<endl ;
469        event.push(*itRank, 1, msg);
470      }
471      sendEvent(event);
472    }
473    else sendEvent(event);
474*/
475
476    CTimer::get("Blocking time").resume();
477    checkBuffers();
478    CTimer::get("Blocking time").suspend();
479
480    std::map<int,StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
481                                          iteMap = mapBufferSize_.end(), itMap;
482
483    StdSize totalBuf = 0;
484    for (itMap = itbMap; itMap != iteMap; ++itMap)
485    {
486      report(10) << " Memory report : Context <" << context->getId() << "> : client side : memory used for buffer of each connection to server" << endl
487                 << "  +) To server with rank " << itMap->first << " : " << itMap->second << " bytes " << endl;
488      totalBuf += itMap->second;
489    }
490    report(0) << " Memory report : Context <" << context->getId() << "> : client side : total memory used for buffer " << totalBuf << " bytes" << endl;
491
492    //releaseBuffers(); // moved to CContext::finalize()
493  }
494
495
496  /*!
497  */
498  bool CContextClient::havePendingRequests(void)
499  {
500    bool pending = false;
501    map<int,CClientBuffer*>::iterator itBuff;
502    for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++)
503      pending |= itBuff->second->hasPendingRequest();
504    return pending;
505  }
506 
507  bool CContextClient::isNotifiedFinalized(void)
508  {
509    if (isAttachedModeEnabled()) return true ;
510
511    bool finalized = true;
512    map<int,CClientBuffer*>::iterator itBuff;
513    for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++)
514      finalized &= itBuff->second->isNotifiedFinalized();
515    return finalized;
516  }
517
518}
Note: See TracBrowser for help on using the repository browser.