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

Last change on this file since 595 was 595, checked in by rlacroix, 7 years ago

Allow using more servers than clients.

This will be useful later when implementing server to client communications.

  • 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: 10.1 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 "message.hpp"
9#include "event_client.hpp"
10#include "context.hpp"
11#include "mpi.hpp"
12#include "timer.hpp"
13#include "cxios.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 on case of attached mode)
22    */
23    CContextClient::CContextClient(CContext* parent, MPI_Comm intraComm_, MPI_Comm interComm_, CContext* cxtSer)
24     : mapBufferSize_(), parentServer(cxtSer)
25    {
26      context = parent;
27      intraComm = intraComm_;
28      interComm = interComm_;
29      MPI_Comm_rank(intraComm, &clientRank);
30      MPI_Comm_size(intraComm, &clientSize);
31
32      int flag;
33      MPI_Comm_test_inter(interComm, &flag);
34      if (flag) MPI_Comm_remote_size(interComm, &serverSize);
35      else  MPI_Comm_size(interComm, &serverSize);
36
37      if (clientSize < serverSize)
38      {
39        int serverByClient = serverSize / clientSize;
40        int remain = serverSize % clientSize;
41        int rankStart = serverByClient * clientRank;
42
43        if (clientRank < remain)
44        {
45          serverByClient++;
46          rankStart += clientRank;
47        }
48        else
49          rankStart += remain;
50
51        for (int i = 0; i < serverByClient; i++)
52          ranksServerLeader.push_back(rankStart + i);
53      }
54      else
55      {
56        int clientByServer = clientSize / serverSize;
57        int remain = clientSize % serverSize;
58
59        if (clientRank < (clientByServer + 1) * remain)
60        {
61          if (clientRank % (clientByServer + 1) == 0)
62            ranksServerLeader.push_back(clientRank / (clientByServer + 1));
63        }
64        else
65        {
66          int rank = clientRank - (clientByServer + 1) * remain;
67          if (rank % clientByServer == 0)
68            ranksServerLeader.push_back(remain + rank / clientByServer);
69        }
70      }
71
72      timeLine = 0;
73    }
74
75    /*!
76    In case of attached mode, the current context must be reset to context for client
77    \param [in] event Event sent to server
78    */
79    void CContextClient::sendEvent(CEventClient& event)
80    {
81      list<int>::iterator itServer;
82      list<int> ranks;
83      list<int> sizes;
84      list<int>::iterator itSize;
85
86      ranks = event.getRanks();
87      if (!event.isEmpty())
88      {
89        sizes = event.getSizes();
90        CMessage msg;
91
92        msg << *(sizes.begin()) << timeLine;
93        for (list<int>::iterator it = sizes.begin(); it != sizes.end(); it++) *it += msg.size();
94        list<CBufferOut*> buffList = getBuffers(ranks, sizes);
95
96        list<CBufferOut*>::iterator it;
97        for (it = buffList.begin(), itSize = sizes.begin(); it != buffList.end(); ++it, ++itSize)
98        {
99          **it << *itSize << timeLine;
100        }
101        event.send(buffList);
102        checkBuffers(ranks);
103      }
104
105      if (0 != parentServer) // context->hasServer
106      {
107        waitEvent(ranks);
108        CContext::setCurrent(context->getId());
109      }
110
111      timeLine++;
112    }
113
114    /*!
115    Special function to setup size of buffer not only on client side but also on server side
116    corresponding to the connection
117    */
118    void CContextClient::sendBufferSizeEvent()
119    {
120      std::map<int,CClientBuffer*>::iterator it, itE;
121      std::map<int,StdSize>::const_iterator itMap = mapBufferSize_.begin(), iteMap = mapBufferSize_.end();
122
123      if (itMap == iteMap)
124         ERROR("void CContextClient::sendBufferSizeEvent()",
125              <<"No information about server buffer, that should not happen...");
126
127      for (; itMap != iteMap; ++itMap)
128      {
129        if (buffers.end() == buffers.find(itMap->first))
130          newBuffer(itMap->first);
131      }
132
133      CBufferOut* bufOut(NULL);
134      itE = buffers.end();
135      for (it = buffers.begin(); it != itE; ++it)
136      {
137        bufOut = (it->second)->getBuffer(sizeof(StdSize));
138        bufOut->put(mapBufferSize_[it->first]);  // Stupid C++
139        (it->second)->checkBuffer();
140      }
141    }
142
143    /*!
144    If client is also server (attached mode), after sending event, it should process right away
145    the incoming event.
146    \param [in] ranks list rank of server connected this client
147    */
148    void CContextClient::waitEvent(list<int>& ranks)
149    {
150//      context->server->setPendingEvent();
151//      while (checkBuffers(ranks))
152//      {
153//        context->server->listen();
154//        context->server->checkPendingRequest();
155//      }
156//
157//      while (context->server->hasPendingEvent())
158//      {
159//       context->server->eventLoop();
160//      }
161
162      parentServer->server->setPendingEvent();
163      while (checkBuffers(ranks))
164      {
165        parentServer->server->listen();
166        parentServer->server->checkPendingRequest();
167      }
168
169      while (parentServer->server->hasPendingEvent())
170      {
171       parentServer->server->eventLoop();
172      }
173    }
174
175    /*!
176    Setup buffer for each connection to server and verify their state to put content into them
177    \param [in] serverList list of rank of connected server
178    \param [in] sizeList size of message corresponding to each connection
179    \return List of buffer input which event can be placed
180    */
181    list<CBufferOut*> CContextClient::getBuffers(list<int>& serverList, list<int>& sizeList)
182    {
183      list<int>::iterator itServer, itSize;
184      list<CClientBuffer*> bufferList;
185      map<int,CClientBuffer*>::iterator it;
186      list<CClientBuffer*>::iterator itBuffer;
187      list<CBufferOut*>  retBuffer;
188      bool free;
189
190      for (itServer = serverList.begin(); itServer != serverList.end(); itServer++)
191      {
192        it = buffers.find(*itServer);
193        if (it == buffers.end())
194        {
195          newBuffer(*itServer);
196          it = buffers.find(*itServer);
197        }
198        bufferList.push_back(it->second);
199      }
200      free = false;
201
202      CTimer::get("Blocking time").resume();
203      while (!free)
204      {
205        free = true;
206        for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
207        {
208          (*itBuffer)->checkBuffer();
209         free &= (*itBuffer)->isBufferFree(*itSize);
210        }
211      }
212      CTimer::get("Blocking time").suspend();
213
214      for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
215      {
216        retBuffer.push_back((*itBuffer)->getBuffer(*itSize));
217      }
218      return retBuffer;
219   }
220
221   /*!
222   Make a new buffer for a certain connection to server with specific rank
223   \param [in] rank rank of connected server
224   */
225   void CContextClient::newBuffer(int rank)
226   {
227      buffers[rank] = new CClientBuffer(interComm, rank, mapBufferSize_[rank]);
228   }
229
230   /*!
231   Verify state of buffers. Buffer is under pending state if there is no message on it
232   \return state of buffers, pending(true), ready(false)
233   */
234   bool CContextClient::checkBuffers(void)
235   {
236      map<int,CClientBuffer*>::iterator itBuff;
237      bool pending = false;
238      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) pending |= itBuff->second->checkBuffer();
239      return pending;
240   }
241
242   //! Release all buffers
243   void CContextClient::releaseBuffers(void)
244   {
245      map<int,CClientBuffer*>::iterator itBuff;
246      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) delete itBuff->second;
247   }
248
249   /*!
250   Verify state of buffers corresponding to a connection
251   \param [in] ranks list rank of server to which client connects to
252   \return state of buffers, pending(true), ready(false)
253   */
254   bool CContextClient::checkBuffers(list<int>& ranks)
255   {
256      list<int>::iterator it;
257      bool pending = false;
258      for (it = ranks.begin(); it != ranks.end(); it++) pending |= buffers[*it]->checkBuffer();
259      return pending;
260   }
261
262   /*!
263   Set buffer size for each connection
264   \param [in] mapSize mapping rank of connected server to size of allocated buffer
265   */
266   void CContextClient::setBufferSize(const std::map<int,StdSize>& mapSize)
267   {
268     mapBufferSize_ = mapSize;
269     sendBufferSizeEvent();
270   }
271
272  /*!
273  Get leading server in the group of connected server
274  \return ranks of leading servers
275  */
276  const std::list<int>& CContextClient::getRanksServerLeader(void) const
277  {
278    return ranksServerLeader;
279  }
280
281  /*!
282  Check if client connects to leading server
283  \return connected(true), not connected (false)
284  */
285  bool CContextClient::isServerLeader(void) const
286  {
287    return !ranksServerLeader.empty();
288  }
289
290   /*!
291   Finalize context client and do some reports
292   */
293   void CContextClient::finalize(void)
294   {
295     map<int,CClientBuffer*>::iterator itBuff;
296     bool stop = true;
297
298     CEventClient event(CContext::GetType(), CContext::EVENT_ID_CONTEXT_FINALIZE);
299     if (isServerLeader())
300     {
301       CMessage msg;
302       const std::list<int>& ranks = getRanksServerLeader();
303       for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
304         event.push(*itRank, 1, msg);
305       sendEvent(event);
306     }
307     else sendEvent(event);
308
309     CTimer::get("Blocking time").resume();
310     while (stop)
311     {
312       checkBuffers();
313       stop = false;
314       for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++) stop |= itBuff->second->hasPendingRequest();
315     }
316     CTimer::get("Blocking time").suspend();
317
318     std::map<int,StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
319                                           iteMap = mapBufferSize_.end(), itMap;
320     StdSize totalBuf = 0;
321     for (itMap = itbMap; itMap != iteMap; ++itMap)
322     {
323       report(10) << " Memory report : Context <" << context->getId() << "> : client side : memory used for buffer of each connection to server" << endl
324                  << "  +) To server with rank " << itMap->first << " : " << itMap->second << " bytes " << endl;
325       totalBuf += itMap->second;
326     }
327     report(0) << " Memory report : Context <" << context->getId() << "> : client side : total memory used for buffer " << totalBuf << " bytes" << endl;
328
329     releaseBuffers();
330   }
331}
Note: See TracBrowser for help on using the repository browser.