source: XIOS/dev/dev_trunk_omp/src/event_scheduler.cpp @ 1680

Last change on this file since 1680 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: 6.6 KB
Line 
1#include "event_scheduler.hpp"
2#include "xios_spl.hpp"
3#include "mpi.hpp"
4#include "tracer.hpp"
5
6#ifdef _usingEP
7using namespace ep_lib;
8#endif
9
10namespace xios
11{
12 
13 
14  CEventScheduler::CEventScheduler(const MPI_Comm& comm) 
15  {
16    MPI_Comm_dup(comm, &communicator) ;
17    MPI_Comm_size(communicator,&mpiSize) ;
18    MPI_Comm_rank(communicator,&mpiRank);
19
20
21    int maxChild=1 ;
22
23    int m ;
24    do
25    {
26      m=1 ;
27      maxChild=maxChild+1 ;
28      for(int i=0;i<maxChild;i++) m=m*maxChild ;
29     } while(m<mpiSize) ;
30   
31   
32    int maxLevel=0 ;
33    for(int size=1; size<=mpiSize; size*=maxChild) maxLevel++ ; 
34
35    int begin, end, nb ;
36    int pos, n ;
37 
38    parent=vector<int>(maxLevel+1) ;
39    child=vector<vector<int> >(maxLevel+1,vector<int>(maxChild)) ;
40    nbChild=vector<int> (maxLevel+1) ;
41   
42    level=0 ;
43    begin=0 ;
44    end=mpiSize-1 ;     
45    nb=end-begin+1 ;
46     
47    do
48    {
49      n=0 ;
50      pos=begin ;
51      nbChild[level]=0 ;
52      parent[level+1]=begin ;
53      for(int i=0;i<maxChild && i<nb ;i++)
54      {
55        if (i<nb%maxChild) n = nb/maxChild + 1 ;
56        else n = nb/maxChild ;
57     
58        if (mpiRank>=pos && mpiRank<pos+n)
59        {
60          begin=pos ;
61          end=pos+n-1 ;
62        }
63        child[level][i]=pos ;
64        pos=pos+n ;
65        nbChild[level]++ ;
66      } 
67      nb=end-begin+1 ;
68      level=level+1 ;
69    } while (nb>1) ;
70
71   
72  }
73
74  CEventScheduler::~CEventScheduler()
75  {
76
77  } 
78
79  void CEventScheduler::registerEvent(const size_t timeLine, const size_t contextHashId)
80  {
81    registerEvent(timeLine, contextHashId, level) ;
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
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(-2,1,communicator,&received, &status) ;
142      if (received)
143      {
144        recvRequest=new SPendingRequest ;
145        MPI_Irecv(recvRequest->buffer, 3, MPI_UNSIGNED_LONG, -2, 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(-2,0,communicator,&received, &status) ;
184      if (received)
185      {
186        recvRequest=new SPendingRequest ;
187        MPI_Irecv(recvRequest->buffer, 3, MPI_UNSIGNED_LONG, -2, 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          }
225        }
226      }
227      else ++it ;
228    }
229   
230    // check if bcast request is achieved
231
232    for(list<SPendingRequest*>::iterator it=pendingSentChildRequest.begin(); it!=pendingSentChildRequest.end() ; )
233    {
234      MPI_Test(&(*it)->request,&received,&status) ;
235      if (received)
236      {
237        delete *it ;    // free memory
238        it = pendingSentChildRequest.erase(it) ;          // get out of the list
239
240      }
241      else ++it ;
242       
243    }
244  }
245 
246  void CEventScheduler::bcastEvent(const size_t timeLine, const size_t contextHashId, const size_t lev)
247  {
248    SPendingRequest* sentRequest ;
249     
250   
251    for(int i=0; i<nbChild[lev];i++)
252    {
253      sentRequest=new SPendingRequest ;
254      sentRequest->buffer[0]=timeLine ;
255      sentRequest->buffer[1]=contextHashId ;
256      sentRequest->buffer[2]=lev+1 ;
257      MPI_Isend(sentRequest->buffer,3, MPI_UNSIGNED_LONG, child[lev][i], 1, communicator, & sentRequest->request) ;
258      pendingSentChildRequest.push_back(sentRequest) ;
259    }
260  }
261   
262
263}
Note: See TracBrowser for help on using the repository browser.