source: XIOS3/trunk/src/transport/legacy_context_client.cpp @ 2628

Last change on this file since 2628 was 2628, checked in by jderouillat, 7 weeks ago

New timers integration/reporting

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 16.5 KB
Line 
1#include "xios_spl.hpp"
2#include "legacy_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#include "services.hpp"
15#include "ressources_manager.hpp"
16#include <boost/functional/hash.hpp>
17#include <random>
18#include <chrono>
19
20namespace xios
21{
22    extern CLogType logTimers ;
23 
24    /*!
25    \param [in] parent Pointer to context on client side
26    \param [in] intraComm_ communicator of group client
27    \param [in] interComm_ communicator of group server
28    \cxtSer [in] cxtSer Pointer to context of server side. (It is only used in case of attached mode --> obsolete).
29    */
30    CLegacyContextClient::CLegacyContextClient(CContext* parent, MPI_Comm intraComm_, MPI_Comm interComm_, CContext* cxtSer)
31                         : CContextClient(parent, intraComm_, interComm_, cxtSer),
32                           mapBufferSize_(),  maxBufferedEvents(4)
33    {
34      pureOneSided=CXios::getin<bool>("pure_one_sided",false); // pure one sided communication (for test)
35      xios::MPI_Intercomm_merge(interComm_,false, &interCommMerged_) ;
36      CXios::getMpiGarbageCollector().registerCommunicator(interCommMerged_) ;
37      xios::MPI_Comm_split(intraComm_,clientRank,clientRank, &commSelf_) ; // for windows
38      CXios::getMpiGarbageCollector().registerCommunicator(commSelf_) ;
39      eventScheduler_ = parent->getEventScheduler() ; 
40      timeLine = 1;
41    }
42
43    CContextClient::ETransport getType(void) {return CContextClient::legacy ;}
44
45    /*!
46    \param [in] event Event sent to server
47    */
48    void CLegacyContextClient::sendEvent(CEventClient& event)
49    {
50      list<int> ranks = event.getRanks();
51 
52//      ostringstream str ;
53//      for(auto& rank : ranks) str<<rank<<" ; " ;
54//      info(100)<<"Event "<<timeLine<<" of context "<<context_->getId()<<"  for ranks : "<<str.str()<<endl ;
55
56      if (CXios::checkEventSync)
57      {
58        int typeId, classId, typeId_in, classId_in;
59        long long timeLine_out;
60        long long timeLine_in( timeLine );
61        typeId_in=event.getTypeId() ;
62        classId_in=event.getClassId() ;
63//        MPI_Allreduce(&timeLine,&timeLine_out, 1, MPI_UINT64_T, MPI_SUM, intraComm) ; // MPI_UINT64_T standardized by MPI 3
64        MPI_Allreduce(&timeLine_in,&timeLine_out, 1, MPI_LONG_LONG_INT, MPI_SUM, intraComm) ; 
65        MPI_Allreduce(&typeId_in,&typeId, 1, MPI_INT, MPI_SUM, intraComm) ;
66        MPI_Allreduce(&classId_in,&classId, 1, MPI_INT, MPI_SUM, intraComm) ;
67        if (typeId/clientSize!=event.getTypeId() || classId/clientSize!=event.getClassId() || timeLine_out/clientSize!=timeLine)
68        {
69           ERROR("void CLegacyContextClient::sendEvent(CEventClient& event)",
70               << "Event are not coherent between client for timeline = "<<timeLine);
71        }
72       
73        vector<int> servers(serverSize,0) ;
74        auto ranks=event.getRanks() ;
75        for(auto& rank : ranks) servers[rank]=1 ;
76        MPI_Allreduce(MPI_IN_PLACE, servers.data(), serverSize,MPI_INT,MPI_SUM,intraComm) ;
77        ostringstream osstr ;
78        for(int i=0;i<serverSize;i++)  if (servers[i]==0) osstr<<i<<" , " ;
79        if (!osstr.str().empty())
80        {
81          ERROR("void CLegacyContextClient::sendEvent(CEventClient& event)",
82                 <<" Some servers will not receive the message for timeline = "<<timeLine<<endl
83                 <<"Servers are : "<<osstr.str()) ;
84        }
85
86
87      }
88
89      if (!event.isEmpty())
90      {
91        list<int> sizes = event.getSizes();
92
93         // We force the getBuffers call to be non-blocking on classical servers
94        list<CBufferOut*> buffList;
95        getBuffers(timeLine, ranks, sizes, buffList) ;
96
97        event.send(timeLine, sizes, buffList);
98       
99        //for (auto itRank = ranks.begin(); itRank != ranks.end(); itRank++) buffers[*itRank]->infoBuffer() ;
100
101        unlockBuffers(ranks) ;
102        checkBuffers(ranks);
103       
104      }
105     
106      synchronize() ;
107      timeLine++;
108    }
109
110
111    /*!
112     * Get buffers for each connection to the servers. This function blocks until there is enough room in the buffers unless
113     * it is explicitly requested to be non-blocking.
114     *
115     *
116     * \param [in] timeLine time line of the event which will be sent to servers
117     * \param [in] serverList list of rank of connected server
118     * \param [in] sizeList size of message corresponding to each connection
119     * \param [out] retBuffers list of buffers that can be used to store an event
120     * \param [in] nonBlocking whether this function should be non-blocking
121     * \return whether the already allocated buffers could be used
122    */
123    void CLegacyContextClient::getBuffers(const size_t timeLine, const list<int>& serverList, const list<int>& sizeList, list<CBufferOut*>& retBuffers)
124    {
125      list<int>::const_iterator itServer, itSize;
126      list<CClientBuffer*> bufferList;
127      map<int,CClientBuffer*>::const_iterator it;
128      list<CClientBuffer*>::iterator itBuffer;
129      bool areBuffersFree;
130/*     
131      for (itServer = serverList.begin(); itServer != serverList.end(); itServer++)
132      {
133        it = buffers.find(*itServer);
134        if (it == buffers.end())
135        {
136          CTokenManager* tokenManager = CXios::getRessourcesManager()->getTokenManager() ;
137          size_t token = tokenManager->getToken() ;
138          while (!tokenManager->checkToken(token)) callGlobalEventLoop() ;
139          newBuffer(*itServer);
140          it = buffers.find(*itServer);
141          checkAttachWindows(it->second,it->first) ;
142          tokenManager->updateToken(token) ;
143        }
144        bufferList.push_back(it->second);
145      }
146*/
147      map<int,MPI_Request> attachList ;
148     
149      for (itServer = serverList.begin(); itServer != serverList.end(); itServer++)
150      {
151        it = buffers.find(*itServer);
152        if (it == buffers.end())
153        {
154          newBuffer(*itServer);
155          it = buffers.find(*itServer);
156          checkAttachWindows(it->second, it->first, attachList) ;
157        }
158        bufferList.push_back(it->second);
159      }
160     
161      while(!attachList.empty())
162      {
163        auto it = attachList.begin() ;
164        while(it!=attachList.end())
165        {
166          if (checkAttachWindows(buffers[it->first], it->first, attachList)) it=attachList.erase(it) ;
167          else ++it ;
168        }
169
170        yield() ;
171      }
172
173
174      double lastTimeBuffersNotFree=0. ;
175      double time ;
176      bool doUnlockBuffers ;
177      CTimer::get("Blocking time").resume();
178      do
179      {
180        areBuffersFree = true;
181        doUnlockBuffers=false ;
182        time=MPI_Wtime() ;
183        if (time-lastTimeBuffersNotFree > latency_)
184        {
185          for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
186          {
187            areBuffersFree &= (*itBuffer)->isBufferFree(*itSize);
188          }
189          if (!areBuffersFree)
190          {
191            lastTimeBuffersNotFree = time ;
192            doUnlockBuffers=true ;
193          }         
194        }
195        else areBuffersFree = false ;
196
197        if (!areBuffersFree)
198        {
199          if (doUnlockBuffers) for (itBuffer = bufferList.begin(); itBuffer != bufferList.end(); itBuffer++) (*itBuffer)->unlockBuffer();
200          checkBuffers();
201
202          yield() ;
203        }
204
205      } while (!areBuffersFree);
206      CTimer::get("Blocking time").suspend();
207
208      for (itBuffer = bufferList.begin(), itSize = sizeList.begin(); itBuffer != bufferList.end(); itBuffer++, itSize++)
209        retBuffers.push_back((*itBuffer)->getBuffer(timeLine, *itSize));
210   }
211
212
213   bool CLegacyContextClient::checkAttachWindows(CClientBuffer* buffer, int rank, map<int, MPI_Request>& attachList)
214   {
215      int dummy;
216      bool ret=true; 
217
218      if (!buffer->isAttachedWindows())
219      {
220           // create windows dynamically for one-sided
221          /*
222          if (info.isActive(logTimers)) CTimer::get("create Windows").resume() ;
223          MPI_Comm interComm ;
224          int tag = 0 ;
225          xios::MPI_Intercomm_create(commSelf_, 0, interCommMerged_, clientSize+rank, tag, &interComm) ;
226          xios::MPI_Intercomm_merge(interComm, false, &winComm_[rank]) ;
227          xios::MPI_Comm_free(&interComm) ;
228               
229          buffer->attachWindows(winComm_[rank]) ;
230          CXios::getMpiGarbageCollector().registerCommunicator(winComm_[rank]) ;
231          MPI_Barrier(winComm_[rank]) ;
232        */
233        if (attachList.count(rank)==0) 
234        {
235          MPI_Irecv(&dummy,0,MPI_INT,clientSize+rank, 21, interCommMerged_, &attachList[rank]) ;
236          ret = false ;
237        }
238        else
239        {
240          MPI_Status status ;
241          int flag ;
242          MPI_Test(&attachList[rank],&flag, &status) ;
243          if (flag)
244          {
245            if (info.isActive(logTimers)) CTimer::get("create Windows").resume() ;
246            MPI_Comm interComm ;
247            int tag = 0 ;
248            xios::MPI_Intercomm_create(commSelf_, 0, interCommMerged_, clientSize+rank, tag, &interComm) ;
249            xios::MPI_Intercomm_merge(interComm, false, &winComm_[rank]) ;
250            xios::MPI_Comm_free(&interComm) ;
251             
252            buffer->attachWindows(winComm_[rank]) ;
253            CXios::getMpiGarbageCollector().registerCommunicator(winComm_[rank]) ;
254            MPI_Barrier(winComm_[rank]) ;
255            ret = true ;
256          }
257          else ret=false ;
258        }
259      }
260      return ret ;
261    }
262
263
264   void CLegacyContextClient::eventLoop(void)
265   {
266      if (!locked_) checkBuffers() ;
267   }
268
269   void CLegacyContextClient::callGlobalEventLoop(void)
270   {
271     locked_=true ;
272     context_->yield() ;
273     locked_=false ;
274   }
275
276   void CLegacyContextClient::yield(void)
277   {
278     locked_=true ;
279     context_->yield() ;
280     locked_=false ;
281   }
282
283   void CLegacyContextClient::synchronize(void)
284   {
285     if (context_->getServiceType()!=CServicesManager::CLIENT)
286     {
287       locked_=true ;
288       context_->synchronize() ;
289       locked_=false ;
290     }   
291   }
292   /*!
293   Make a new buffer for a certain connection to server with specific rank
294   \param [in] rank rank of connected server
295   */
296   void CLegacyContextClient::newBuffer(int rank)
297   {
298      if (!mapBufferSize_.count(rank))
299      {
300        error(0) << "WARNING: Unexpected request for buffer to communicate with server " << rank << std::endl;
301        mapBufferSize_[rank] = CXios::minBufferSize;
302        maxEventSizes[rank] = CXios::minBufferSize;
303      }
304      bool hasWindows = true ;
305      CClientBuffer* buffer = buffers[rank] = new CClientBuffer(interCommMerged_, clientSize+rank, mapBufferSize_[rank], hasWindows);
306      if (isGrowableBuffer_) buffer->setGrowableBuffer(1.2) ;
307      else buffer->fixBuffer() ;
308      // Notify the server
309     
310      CBufferOut* bufOut = buffer->getBuffer(0, 4*sizeof(MPI_Aint));
311      MPI_Aint sendBuff[4] ;
312      sendBuff[0]=hashId_;
313      sendBuff[1]=mapBufferSize_[rank];
314      sendBuff[2]=buffers[rank]->getWinBufferAddress(0); 
315      sendBuff[3]=buffers[rank]->getWinBufferAddress(1); 
316      info(100)<<"CLegacyContextClient::newBuffer : rank "<<rank<<" winAdress[0] "<<buffers[rank]->getWinBufferAddress(0)<<" winAdress[1] "<<buffers[rank]->getWinBufferAddress(1)<<endl;
317      bufOut->put(sendBuff,4); 
318      buffer->checkBuffer(true);
319
320   }
321
322 
323 
324   /*!
325   Verify state of buffers. Buffer is under pending state if there is no message on it
326   \return state of buffers, pending(true), ready(false)
327   */
328   bool CLegacyContextClient::checkBuffers(void)
329   {
330      map<int,CClientBuffer*>::iterator itBuff;
331      bool pending = false;
332      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++)
333        pending |= itBuff->second->checkBuffer(!pureOneSided);
334      return pending;
335   }
336
337   //! Release all buffers
338   void CLegacyContextClient::releaseBuffers()
339   {
340      map<int,CClientBuffer*>::iterator itBuff;
341      for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++)
342      {
343         delete itBuff->second;
344      }
345      buffers.clear();
346
347      for(auto& it : winComm_)
348      {
349        int rank = it.first ;
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 CLegacyContextClient::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 CLegacyContextClient::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 CLegacyContextClient::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 CLegacyContextClient::setBufferSize(const std::map<int,StdSize>& mapSize)
394   {
395     setFixedBuffer() ;
396     for(auto& it : mapSize)
397     {
398      size_t size=std::max(CXios::minBufferSize*1.0,std::min(it.second*CXios::bufferSizeFactor*1.01,CXios::maxBufferSize*1.0)) ;
399      mapBufferSize_[it.first]=size ;
400      if (buffers.count(it.first)>0) buffers[it.first]->fixBufferSize(size);
401     }
402   }
403
404   /*!
405   * Finalize context client and do some reports. Function is non-blocking.
406   */
407  void CLegacyContextClient::finalize(void)
408  {
409    map<int,CClientBuffer*>::iterator itBuff;
410    std::list<int>::iterator ItServerLeader; 
411   
412    bool stop = false;
413
414    int* nbServerConnectionLocal  = new int[serverSize] ;
415    int* nbServerConnectionGlobal  = new int[serverSize] ;
416    for(int i=0;i<serverSize;++i) nbServerConnectionLocal[i]=0 ;
417    for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++)  nbServerConnectionLocal[itBuff->first]=1 ;
418    for (ItServerLeader = ranksServerLeader.begin(); ItServerLeader != ranksServerLeader.end(); ItServerLeader++)  nbServerConnectionLocal[*ItServerLeader]=1 ;
419   
420    MPI_Allreduce(nbServerConnectionLocal, nbServerConnectionGlobal, serverSize, MPI_INT, MPI_SUM, intraComm);
421   
422    CEventClient event(CContext::GetType(), CContext::EVENT_ID_CONTEXT_FINALIZE);
423    CMessage msg;
424
425    for (int i=0;i<serverSize;++i) if (nbServerConnectionLocal[i]==1) event.push(i, nbServerConnectionGlobal[i], msg) ;
426    sendEvent(event);
427
428    delete[] nbServerConnectionLocal ;
429    delete[] nbServerConnectionGlobal ;
430
431
432    CTimer::get("Blocking time").resume();
433    checkBuffers();
434    CTimer::get("Blocking time").suspend();
435
436    std::map<int,StdSize>::const_iterator itbMap = mapBufferSize_.begin(),
437                                          iteMap = mapBufferSize_.end(), itMap;
438
439    StdSize totalBuf = 0;
440    for (itMap = itbMap; itMap != iteMap; ++itMap)
441    {
442      report(10) << " Memory report : Context <" << context_->getId() << "> : client side : memory used for buffer of each connection to server" << endl
443                 << "  +) To server with rank " << itMap->first << " : " << itMap->second << " bytes " << endl;
444      totalBuf += itMap->second;
445    }
446    report(0) << " Memory report : Context <" << context_->getId() << "> : client side : total memory used for buffer " << totalBuf << " bytes" << endl;
447
448  }
449
450
451  /*!
452  */
453  bool CLegacyContextClient::havePendingRequests(void)
454  {
455    bool pending = false;
456    map<int,CClientBuffer*>::iterator itBuff;
457    for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++)
458      pending |= itBuff->second->hasPendingRequest();
459    return pending;
460  }
461 
462  bool CLegacyContextClient::havePendingRequests(list<int>& ranks)
463  {
464      list<int>::iterator it;
465      bool pending = false;
466      for (it = ranks.begin(); it != ranks.end(); it++) pending |= buffers[*it]->hasPendingRequest();
467      return pending;
468  }
469
470  bool CLegacyContextClient::isNotifiedFinalized(void)
471  {
472    bool finalized = true;
473    map<int,CClientBuffer*>::iterator itBuff;
474    for (itBuff = buffers.begin(); itBuff != buffers.end(); itBuff++)
475      finalized &= itBuff->second->isNotifiedFinalized();
476    return finalized;
477  }
478
479}
Note: See TracBrowser for help on using the repository browser.