source: XIOS/dev/dev_ym/XIOS_COUPLING/src/event_scheduler.cpp @ 2274

Last change on this file since 2274 was 2274, checked in by ymipsl, 2 years ago

Tracking memory leak : release memory statically alocated

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: 6.8 KB
Line 
1#include "event_scheduler.hpp"
2#include "xios_spl.hpp"
3#include "mpi.hpp"
4#include "tracer.hpp"
5
6namespace xios
7{
8 
9 
10  CEventScheduler::CEventScheduler(const MPI_Comm& comm) 
11  {
12    MPI_Comm_dup(comm, &communicator) ;
13    MPI_Comm_size(communicator,&mpiSize) ;
14    MPI_Comm_rank(communicator,&mpiRank);
15
16
17    int maxChild=1 ;
18
19    int m ;
20    do
21    {
22      m=1 ;
23      maxChild=maxChild+1 ;
24      for(int i=0;i<maxChild;i++) m=m*maxChild ;
25     } while(m<mpiSize) ;
26   
27   
28    int maxLevel=0 ;
29    for(int size=1; size<=mpiSize; size*=maxChild) maxLevel++ ; 
30
31    int begin, end, nb ;
32    int pos, n ;
33 
34    parent=vector<int>(maxLevel+1) ;
35    child=vector<vector<int> >(maxLevel+1,vector<int>(maxChild)) ;
36    nbChild=vector<int> (maxLevel+1) ;
37   
38    level=0 ;
39    begin=0 ;
40    end=mpiSize-1 ;     
41    nb=end-begin+1 ;
42     
43    do
44    {
45      n=0 ;
46      pos=begin ;
47      nbChild[level]=0 ;
48      parent[level+1]=begin ;
49      for(int i=0;i<maxChild && i<nb ;i++)
50      {
51        if (i<nb%maxChild) n = nb/maxChild + 1 ;
52        else n = nb/maxChild ;
53     
54        if (mpiRank>=pos && mpiRank<pos+n)
55        {
56          begin=pos ;
57          end=pos+n-1 ;
58        }
59        child[level][i]=pos ;
60        pos=pos+n ;
61        nbChild[level]++ ;
62      } 
63      nb=end-begin+1 ;
64      level=level+1 ;
65    } while (nb>1) ;
66
67   
68  }
69
70  CEventScheduler::~CEventScheduler()
71  {
72    while (!pendingSentParentRequest.empty() || !pendingRecvParentRequest.empty() || !pendingRecvChildRequest.empty() ||  !pendingSentChildRequest.empty())
73    {
74      checkEvent() ;
75    } 
76  } 
77
78  void CEventScheduler::registerEvent(const size_t timeLine, const size_t contextHashId)
79  {
80    registerEvent(timeLine, contextHashId, level) ;
81    checkEvent() ;
82  }
83 
84  void CEventScheduler::registerEvent(const size_t timeLine, const size_t contextHashId, const size_t lev)
85  {
86       
87    traceOff() ;
88    SPendingRequest* sentRequest=new SPendingRequest ;
89    sentRequest->buffer[0]=timeLine ;
90    sentRequest->buffer[1]=contextHashId ;
91    sentRequest->buffer[2]=lev-1 ;
92
93    pendingSentParentRequest.push(sentRequest) ;
94    MPI_Isend(sentRequest->buffer,3, MPI_UNSIGNED_LONG, parent[lev], 0, communicator, &sentRequest->request) ;
95    traceOn() ;
96  } 
97
98  bool CEventScheduler::queryEvent(const size_t timeLine, const size_t contextHashId)
99  {
100    checkEvent() ;
101    if (! eventStack.empty() && eventStack.front().first==timeLine && eventStack.front().second==contextHashId)
102    {
103      //eventStack.pop() ;
104      return true ;
105    }
106    else return false ; 
107  } 
108 
109  void CEventScheduler::checkEvent(void)
110  {
111    traceOff() ;
112    checkChildRequest() ;
113    checkParentRequest() ;
114    traceOn() ;
115   
116  }
117 
118  void CEventScheduler::checkParentRequest(void)
119  {
120    int completed ;
121    MPI_Status status ;
122    int received ;
123    SPendingRequest* recvRequest ;
124    completed=true ;
125   
126    // check sent request to parent
127    while (! pendingSentParentRequest.empty() && completed)
128    {
129      MPI_Test( & pendingSentParentRequest.front()->request, &completed, &status) ;
130      if (completed) 
131      {
132        delete pendingSentParentRequest.front() ;
133        pendingSentParentRequest.pop() ;
134      }
135    }
136   
137    // probe if a message is coming from parent
138    received=true ;
139    while(received)
140    {
141      MPI_Iprobe(MPI_ANY_SOURCE,1,communicator,&received, &status) ;
142      if (received)
143      {
144        recvRequest=new SPendingRequest ;
145        MPI_Irecv(recvRequest->buffer, 3, MPI_UNSIGNED_LONG, MPI_ANY_SOURCE, 1, communicator, &(recvRequest->request)) ;
146        pendingRecvParentRequest.push(recvRequest) ;
147      }
148    }
149   
150     // check sent request from parent
151    completed=true ;
152    while (! pendingRecvParentRequest.empty() && completed)
153    {
154      recvRequest=pendingRecvParentRequest.front() ;
155      MPI_Test( &(recvRequest->request), &completed, &status) ;
156      if (completed) 
157      {
158        size_t timeLine=recvRequest->buffer[0] ;
159        size_t hashId=recvRequest->buffer[1] ;
160        size_t lev=recvRequest->buffer[2] ;
161        delete recvRequest ;
162        pendingRecvParentRequest.pop() ;       
163 
164        if (lev==level) eventStack.push(pair<size_t,size_t>(timeLine,hashId)) ;
165        else  bcastEvent(timeLine, hashId, lev) ;
166      }
167    }   
168   
169  }
170
171  void CEventScheduler::checkChildRequest(void)
172  {
173// function call only by parent mpi process
174
175    MPI_Status status ; 
176    int received ;
177    received=true ;
178    SPendingRequest* recvRequest ;
179   
180    // check for posted requests and make the corresponding receive
181    while(received)
182    {
183      MPI_Iprobe(MPI_ANY_SOURCE,0,communicator,&received, &status) ;
184      if (received)
185      {
186        recvRequest=new SPendingRequest ;
187        MPI_Irecv(recvRequest->buffer, 3, MPI_UNSIGNED_LONG, MPI_ANY_SOURCE, 0, communicator, &recvRequest->request) ;
188        pendingRecvChildRequest.push_back(recvRequest) ;
189      }
190    }
191   
192    // check if receive request is achieved
193   
194    for(list<SPendingRequest*>::iterator it=pendingRecvChildRequest.begin(); it!=pendingRecvChildRequest.end() ; )
195    {
196      MPI_Test(&((*it)->request),&received,&status) ;
197      if (received)
198      {
199        size_t timeLine=(*it)->buffer[0] ;
200        size_t hashId=(*it)->buffer[1] ;
201        size_t lev=(*it)->buffer[2] ;
202       
203        SEvent event={timeLine,hashId,lev} ;
204        delete *it ; // free mem
205        it=pendingRecvChildRequest.erase(it) ; // get out of the list
206       
207        map< SEvent,int>::iterator itEvent=recvEvent.find(event) ;
208        if (itEvent==recvEvent.end()) 
209        {
210          itEvent=(recvEvent.insert(pair< SEvent ,int > (event,1))).first ;
211 
212        }
213        else (itEvent->second)++ ;
214        if (itEvent->second==nbChild[lev])
215        {
216          if (lev==0)
217          {
218            bcastEvent(timeLine,hashId,lev) ;
219            recvEvent.erase(itEvent) ;
220          }
221          else
222          {
223            registerEvent( timeLine,hashId,lev) ;
224            recvEvent.erase(itEvent) ;
225          }
226        }
227      }
228      else ++it ;
229    }
230   
231    // check if bcast request is achieved
232
233    for(list<SPendingRequest*>::iterator it=pendingSentChildRequest.begin(); it!=pendingSentChildRequest.end() ; )
234    {
235      MPI_Test(&(*it)->request,&received,&status) ;
236      if (received)
237      {
238        delete *it ;    // free memory
239        it = pendingSentChildRequest.erase(it) ;          // get out of the list
240
241      }
242      else ++it ;
243       
244    }
245  }
246 
247  void CEventScheduler::bcastEvent(const size_t timeLine, const size_t contextHashId, const size_t lev)
248  {
249    SPendingRequest* sentRequest ;
250     
251   
252    for(int i=0; i<nbChild[lev];i++)
253    {
254      sentRequest=new SPendingRequest ;
255      sentRequest->buffer[0]=timeLine ;
256      sentRequest->buffer[1]=contextHashId ;
257      sentRequest->buffer[2]=lev+1 ;
258      MPI_Isend(sentRequest->buffer,3, MPI_UNSIGNED_LONG, child[lev][i], 1, communicator, & sentRequest->request) ;
259      pendingSentChildRequest.push_back(sentRequest) ;
260    }
261  }
262   
263
264}
Note: See TracBrowser for help on using the repository browser.