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

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

branch merged with trunk @1645. arch file (ep&mpi) added for 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.8 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#ifdef _usingEP
15using namespace ep_lib;
16#endif
17
18namespace xios
19{
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
24    \cxtSer [in] cxtSer Pointer to context of server side. (It is only used in case of attached mode).
25    */
26    CContextClient::CContextClient(CContext* parent, MPI_Comm intraComm_, MPI_Comm interComm_, CContext* cxtSer)
27     : mapBufferSize_(), parentServer(cxtSer), maxBufferedEvents(4)
28    {
29      context = parent;
30      intraComm = intraComm_;
31      interComm = interComm_;
32      MPI_Comm_rank(intraComm, &clientRank);
33      MPI_Comm_size(intraComm, &clientSize);
34
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);
39
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
51      if (clientSize < serverSize)
52      {
53        int serverByClient = serverSize / clientSize;
54        int remain = serverSize % clientSize;
55        int rankStart = serverByClient * clientRank;
56
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++)
66          rankRecvLeader.push_back(rankStart + i);
67
68        rankRecvNotLeader.resize(0);
69      }
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)
78            rankRecvLeader.push_back(clientRank / (clientByServer + 1));
79          else
80            rankRecvNotLeader.push_back(clientRank / (clientByServer + 1));
81        }
82        else
83        {
84          int rank = clientRank - (clientByServer + 1) * remain;
85          if (rank % clientByServer == 0)
86            rankRecvLeader.push_back(remain + rank / clientByServer);
87          else
88            rankRecvNotLeader.push_back(remain + rank / clientByServer);
89        }
90      }
91    }
92
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    */
97    void CContextClient::sendEvent(CEventClient& event)
98    {
99      list<int> ranks = event.getRanks();
100      #pragma omp critical (_output)
101      {
102        info(100)<<"Event "<<timeLine<<" of context "<<context->getId()<<endl ;
103      }
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() ;
109        MPI_Allreduce(&timeLine,&timeLine_out, 1, MPI_LONG_LONG_INT, MPI_SUM, intraComm) ;
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
119      if (!event.isEmpty())
120      {
121        list<int> sizes = event.getSizes();
122
123        // We force the getBuffers call to be non-blocking on classical servers
124        list<CBufferOut*> buffList;
125        bool couldBuffer = getBuffers(ranks, sizes, buffList, (!CXios::isClient && (CServer::serverLevel == 0) ));
126//        bool couldBuffer = getBuffers(ranks, sizes, buffList, CXios::isServer );
127
128        if (couldBuffer)
129        {
130          event.send(timeLine, sizes, buffList);
131          #pragma omp critical (_output)
132          {
133            info(100)<<"Event "<<timeLine<<" of context "<<context->getId()<<"  sent"<<endl ;
134          }
135
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));
151          #pragma omp critical (_output)
152          {
153            info(100)<<"DEBUG : temporaly event created : timeline "<<timeLine<<endl ;
154          }
155          event.send(timeLine, tmpBufferedEvent.sizes, tmpBufferedEvent.buffers);
156          #pragma omp critical (_output)
157          {
158            info(100)<<"Event "<<timeLine<<" of context "<<context->getId()<<"  sent"<<endl ;
159          }
160        }
161      }
162
163      timeLine++;
164    }
165
166    /*!
167     * Send the temporarily buffered event (if any).
168     *
169     * \return true if a temporarily buffered event could be sent, false otherwise
170     */
171    bool CContextClient::sendTemporarilyBufferedEvent()
172    {
173      bool couldSendTmpBufferedEvent = false;
174
175      if (hasTemporarilyBufferedEvent())
176      {
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
185          #pragma omp critical (_output)
186          {
187            info(100)<<"DEBUG : temporaly event sent "<<endl ;
188          }
189          checkBuffers(tmpBufferedEvent.ranks);
190
191          tmpBufferedEvent.clear();
192
193          couldSendTmpBufferedEvent = true;
194        }
195      }
196
197      return couldSendTmpBufferedEvent;
198    }
199
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    */
205    void CContextClient::waitEvent(list<int>& ranks)
206    {
207      parentServer->server->setPendingEvent();
208      while (checkBuffers(ranks))
209      {
210        parentServer->server->listen();
211        parentServer->server->checkPendingRequest();
212      }
213
214      while (parentServer->server->hasPendingEvent())
215      {
216       parentServer->server->eventLoop();
217      }
218    }
219
220    /*!
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
229    */
230    bool CContextClient::getBuffers(const list<int>& serverList, const list<int>& sizeList, list<CBufferOut*>& retBuffers,
231                                    bool nonBlocking /*= false*/)
232    {
233      list<int>::const_iterator itServer, itSize;
234      list<CClientBuffer*> bufferList;
235      map<int,CClientBuffer*>::const_iterator it;
236      list<CClientBuffer*>::iterator itBuffer;
237      bool areBuffersFree;
238
239      for (itServer = serverList.begin(); itServer != serverList.end(); itServer++)
240      {
241        it = buffers.find(*itServer);
242        if (it == buffers.end())
243        {
244          newBuffer(*itServer);
245          it = buffers.find(*itServer);
246        }
247        bufferList.push_back(it->second);
248      }
249
250      CTimer::get("Blocking time").resume();
251      do
252      {
253        areBuffersFree = true;
254        for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
255          areBuffersFree &= (*itBuffer)->isBufferFree(*itSize);
256
257        if (!areBuffersFree)
258        {
259          checkBuffers();
260          if (CServer::serverLevel == 0)
261            context->server->listen();
262
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();
268            CServer::contextEventLoop(false) ; // avoid dead-lock at finalize...
269          }
270
271          else if (CServer::serverLevel == 2)
272            context->server->listen();
273
274        }
275      } while (!areBuffersFree && !nonBlocking);
276
277      CTimer::get("Blocking time").suspend();
278
279      if (areBuffersFree)
280      {
281        for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
282          retBuffers.push_back((*itBuffer)->getBuffer(*itSize));
283      }
284
285      return areBuffersFree;
286   }
287
288   /*!
289   Make a new buffer for a certain connection to server with specific rank
290   \param [in] rank rank of connected server
291   */
292   void CContextClient::newBuffer(int rank)
293   {
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();
305   }
306
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   */
311   bool CContextClient::checkBuffers(void)
312   {
313      map<int,CClientBuffer*>::iterator itBuff;
314      bool pending = false;
315      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++)
316        pending |= itBuff->second->checkBuffer();
317      return pending;
318   }
319
320   //! Release all buffers
321   void CContextClient::releaseBuffers()
322   {
323      map<int,CClientBuffer*>::iterator itBuff;
324      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++)
325      {
326          delete itBuff->second;
327      }
328      buffers.clear();
329   }
330
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   */
336   bool CContextClient::checkBuffers(list<int>& ranks)
337   {
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;
342   }
343
344   /*!
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
349   */
350   void CContextClient::setBufferSize(const std::map<int,StdSize>& mapSize, const std::map<int,StdSize>& maxEventSize)
351   {
352     mapBufferSize_ = mapSize;
353     maxEventSizes = maxEventSize;
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     {
359       double ratio = double(it->second) / maxEventSizes[it->first];
360       if (ratio < minBufferSizeEventSizeRatio) minBufferSizeEventSizeRatio = ratio;
361     }
362     
363     #ifdef _usingEP
364     MPI_Allreduce(&minBufferSizeEventSizeRatio, &minBufferSizeEventSizeRatio, 1, MPI_DOUBLE, MPI_MIN, intraComm);
365     #elif _usingMPI
366     MPI_Allreduce(MPI_IN_PLACE, &minBufferSizeEventSizeRatio, 1, MPI_DOUBLE, MPI_MIN, intraComm);
367     #endif
368
369     if (minBufferSizeEventSizeRatio < 1.0)
370     {
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.");
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
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
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::getRanksServerNotLeader(void) const
387  {
388    return ranksServerNotLeader;
389  }
390
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  }
399
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  }
408
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  }
417
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  }
427
428   /*!
429   * Finalize context client and do some reports. Function is non-blocking.
430   */
431  void CContextClient::finalize(void)
432  {
433    map<int,CClientBuffer*>::iterator itBuff;
434    bool stop = false;
435
436    CTimer::get("Blocking time").resume();
437    while (hasTemporarilyBufferedEvent())
438    {
439      checkBuffers();
440      sendTemporarilyBufferedEvent();
441    }
442    CTimer::get("Blocking time").suspend();
443
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)
450      {
451        #pragma omp critical (_output)
452        {
453          info(100)<<"DEBUG : Sent context Finalize event to rank "<<*itRank<<endl ;
454        }
455        event.push(*itRank, 1, msg);
456      }
457      sendEvent(event);
458    }
459    else sendEvent(event);
460
461    CTimer::get("Blocking time").resume();
462//    while (!stop)
463    {
464      checkBuffers();
465      if (hasTemporarilyBufferedEvent())
466        sendTemporarilyBufferedEvent();
467
468      stop = true;
469//      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) stop &= !itBuff->second->hasPendingRequest();
470    }
471    CTimer::get("Blocking time").suspend();
472
473    std::map<int,StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
474                                          iteMap = mapBufferSize_.end(), itMap;
475
476    StdSize totalBuf = 0;
477    for (itMap = itbMap; itMap != iteMap; ++itMap)
478    {
479      #pragma omp critical (_output)
480      {
481        report(10) << " Memory report : Context <" << context->getId() << "> : client side : memory used for buffer of each connection to server" << endl
482                 << "  +) To server with rank " << itMap->first << " : " << itMap->second << " bytes " << endl;
483      }
484      totalBuf += itMap->second;
485    }
486    #pragma omp critical (_output)
487    {
488      report(0) << " Memory report : Context <" << context->getId() << "> : client side : total memory used for buffer " << totalBuf << " bytes" << endl;
489    }
490
491    //releaseBuffers(); // moved to CContext::finalize()
492  }
493
494
495  /*!
496  */
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
507}
Note: See TracBrowser for help on using the repository browser.