source: XIOS/dev/branch_yushan/src/event_scheduler.cpp @ 1144

Last change on this file since 1144 was 1110, checked in by yushan, 7 years ago

redefinition of mpi_any_source and mpi_any_tag

  • 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: 7.0 KB
Line 
1#include "event_scheduler.hpp"
2#include "xios_spl.hpp"
3#include "mpi.hpp"
4
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
73  } 
74
75  void CEventScheduler::registerEvent(const size_t timeLine, const size_t contextHashId)
76  {
77    registerEvent(timeLine, contextHashId, level) ;
78  }
79 
80  void CEventScheduler::registerEvent(const size_t timeLine, const size_t contextHashId, const size_t lev)
81  {
82       
83    SPendingRequest* sentRequest=new SPendingRequest ;
84    sentRequest->buffer[0]=timeLine ;
85    sentRequest->buffer[1]=contextHashId ;
86    sentRequest->buffer[2]=lev-1 ;
87
88    pendingSentParentRequest.push(sentRequest) ;
89    MPI_Isend(sentRequest->buffer,3, MPI_UNSIGNED_LONG, parent[lev], 0, communicator, &sentRequest->request) ;
90  } 
91
92  bool CEventScheduler::queryEvent(const size_t timeLine, const size_t contextHashId)
93  {
94
95    if (! eventStack.empty() && eventStack.front().first==timeLine && eventStack.front().second==contextHashId)
96    {
97      eventStack.pop() ;
98      return true ;
99    }
100    else return false ; 
101  } 
102 
103  void CEventScheduler::checkEvent(void)
104  {
105    checkChildRequest() ;
106    checkParentRequest() ;
107   
108  }
109 
110 
111 
112  void CEventScheduler::checkParentRequest(void)
113  {
114    int completed ;
115    MPI_Status status ;
116    int received ;
117    SPendingRequest* recvRequest ;
118    completed=true ;
119   
120    // check sent request to parent
121    while (! pendingSentParentRequest.empty() && completed)
122    {
123      MPI_Test( & pendingSentParentRequest.front()->request, &completed, &status) ;
124      if (completed) 
125      {
126        delete pendingSentParentRequest.front() ;
127        pendingSentParentRequest.pop() ;
128      }
129    }
130   
131    // probe if a message is coming from parent
132    received=true ;
133    while(received)
134    {
135      #ifdef _usingEP
136      MPI_Iprobe(-1,1,communicator,&received, &status) ;
137      #else
138      MPI_Iprobe(MPI_ANY_SOURCE,1,communicator,&received, &status) ;
139      #endif
140      if (received)
141      {
142        recvRequest=new SPendingRequest ;
143        #ifdef _usingEP
144        MPI_Irecv(recvRequest->buffer, 3, MPI_UNSIGNED_LONG, -1, 1, communicator, &(recvRequest->request)) ;
145        #else
146        MPI_Irecv(recvRequest->buffer, 3, MPI_UNSIGNED_LONG, MPI_ANY_SOURCE, 1, communicator, &(recvRequest->request)) ;
147        #endif
148        pendingRecvParentRequest.push(recvRequest) ;
149      }
150    }
151   
152     // check sent request from parent
153    completed=true ;
154    while (! pendingRecvParentRequest.empty() && completed)
155    {
156      recvRequest=pendingRecvParentRequest.front() ;
157      MPI_Test( &(recvRequest->request), &completed, &status) ;
158      if (completed) 
159      {
160        size_t timeLine=recvRequest->buffer[0] ;
161        size_t hashId=recvRequest->buffer[1] ;
162        size_t lev=recvRequest->buffer[2] ;
163        delete recvRequest ;
164        pendingRecvParentRequest.pop() ;       
165 
166        if (lev==level) eventStack.push(pair<size_t,size_t>(timeLine,hashId)) ;
167        else  bcastEvent(timeLine, hashId, lev) ;
168      }
169    }   
170   
171  }
172
173  void CEventScheduler::checkChildRequest(void)
174  {
175// function call only by parent mpi process
176
177    MPI_Status status ; 
178    int received ;
179    received=true ;
180    SPendingRequest* recvRequest ;
181   
182    // check for posted requests and make the corresponding receive
183    while(received)
184    {
185      #ifdef _usingEP
186      MPI_Iprobe(-1,0,communicator,&received, &status) ;
187      #else
188      MPI_Iprobe(MPI_ANY_SOURCE,0,communicator,&received, &status) ;
189      #endif
190      if (received)
191      {
192        recvRequest=new SPendingRequest ;
193        #ifdef _usingEP
194        MPI_Irecv(recvRequest->buffer, 3, MPI_UNSIGNED_LONG, -1, 0, communicator, &recvRequest->request) ;
195        #else
196        MPI_Irecv(recvRequest->buffer, 3, MPI_UNSIGNED_LONG, MPI_ANY_SOURCE, 0, communicator, &recvRequest->request) ;
197        #endif
198        pendingRecvChildRequest.push_back(recvRequest) ;
199      }
200    }
201   
202    // check if receive request is achieved
203   
204    for(list<SPendingRequest*>::iterator it=pendingRecvChildRequest.begin(); it!=pendingRecvChildRequest.end() ; )
205    {
206      MPI_Test(&((*it)->request),&received,&status) ;
207      if (received)
208      {
209        size_t timeLine=(*it)->buffer[0] ;
210        size_t hashId=(*it)->buffer[1] ;
211        size_t lev=(*it)->buffer[2] ;
212       
213        SEvent event={timeLine,hashId,lev} ;
214        delete *it ; // free mem
215        it=pendingRecvChildRequest.erase(it) ; // get out of the list
216       
217        map< SEvent,int>::iterator itEvent=recvEvent.find(event) ;
218        if (itEvent==recvEvent.end()) 
219        {
220          itEvent=(recvEvent.insert(pair< SEvent ,int > (event,1))).first ;
221 
222        }
223        else (itEvent->second)++ ;
224        if (itEvent->second==nbChild[lev])
225        {
226          if (lev==0)
227          {
228            bcastEvent(timeLine,hashId,lev) ;
229            recvEvent.erase(itEvent) ;
230          }
231          else
232          {
233            registerEvent( timeLine,hashId,lev) ;
234          }
235        }
236      }
237      else ++it ;
238    }
239   
240    // check if bcast request is achieved
241
242    for(list<SPendingRequest*>::iterator it=pendingSentChildRequest.begin(); it!=pendingSentChildRequest.end() ; )
243    {
244      MPI_Test(&(*it)->request,&received,&status) ;
245      if (received)
246      {
247        delete *it ;    // free memory
248        it = pendingSentChildRequest.erase(it) ;          // get out of the list
249
250      }
251      else ++it ;
252       
253    }
254  }
255 
256  void CEventScheduler::bcastEvent(const size_t timeLine, const size_t contextHashId, const size_t lev)
257  {
258    SPendingRequest* sentRequest ;
259     
260   
261    for(int i=0; i<nbChild[lev];i++)
262    {
263      sentRequest=new SPendingRequest ;
264      sentRequest->buffer[0]=timeLine ;
265      sentRequest->buffer[1]=contextHashId ;
266      sentRequest->buffer[2]=lev+1 ;
267      MPI_Isend(sentRequest->buffer,3, MPI_UNSIGNED_LONG, child[lev][i], 1, communicator, & sentRequest->request) ;
268      pendingSentChildRequest.push_back(sentRequest) ;
269    }
270  }
271   
272
273}
Note: See TracBrowser for help on using the repository browser.