source: XIOS/trunk/src/context_client.cpp @ 1638

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

dev on ADA

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