source: XIOS/dev/branch_openmp/src/event_scheduler.cpp @ 1328

Last change on this file since 1328 was 1328, checked in by yushan, 6 years ago

dev_omp

  • 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.5 KB
Line 
1#include "event_scheduler.hpp"
2#include "xios_spl.hpp"
3#include "mpi.hpp"
4using namespace ep_lib;
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      MPI_Iprobe(-2,1,communicator,&received, &status) ;
136      if (received)
137      {
138        recvRequest=new SPendingRequest ;
139        MPI_Irecv(recvRequest->buffer, 3, MPI_UNSIGNED_LONG, -2, 1, communicator, &(recvRequest->request)) ;
140        pendingRecvParentRequest.push(recvRequest) ;
141      }
142    }
143   
144     // check sent request from parent
145    completed=true ;
146    while (! pendingRecvParentRequest.empty() && completed)
147    {
148      recvRequest=pendingRecvParentRequest.front() ;
149      MPI_Test( &(recvRequest->request), &completed, &status) ;
150      if (completed) 
151      {
152        size_t timeLine=recvRequest->buffer[0] ;
153        size_t hashId=recvRequest->buffer[1] ;
154        size_t lev=recvRequest->buffer[2] ;
155        delete recvRequest ;
156        pendingRecvParentRequest.pop() ;       
157 
158        if (lev==level) eventStack.push(pair<size_t,size_t>(timeLine,hashId)) ;
159        else  bcastEvent(timeLine, hashId, lev) ;
160      }
161    }   
162   
163  }
164
165  void CEventScheduler::checkChildRequest(void)
166  {
167// function call only by parent mpi process
168
169    MPI_Status status ; 
170    int received ;
171    received=true ;
172    SPendingRequest* recvRequest ;
173   
174    // check for posted requests and make the corresponding receive
175    while(received)
176    {
177      MPI_Iprobe(-2,0,communicator,&received, &status) ;
178      if (received)
179      {
180        recvRequest=new SPendingRequest ;
181        MPI_Irecv(recvRequest->buffer, 3, MPI_UNSIGNED_LONG, -2, 0, communicator, &recvRequest->request) ;
182        pendingRecvChildRequest.push_back(recvRequest) ;
183      }
184    }
185   
186    // check if receive request is achieved
187   
188    for(list<SPendingRequest*>::iterator it=pendingRecvChildRequest.begin(); it!=pendingRecvChildRequest.end() ; )
189    {
190      MPI_Test(&((*it)->request),&received,&status) ;
191      if (received)
192      {
193        size_t timeLine=(*it)->buffer[0] ;
194        size_t hashId=(*it)->buffer[1] ;
195        size_t lev=(*it)->buffer[2] ;
196       
197        SEvent event={timeLine,hashId,lev} ;
198        delete *it ; // free mem
199        it=pendingRecvChildRequest.erase(it) ; // get out of the list
200       
201        map< SEvent,int>::iterator itEvent=recvEvent.find(event) ;
202        if (itEvent==recvEvent.end()) 
203        {
204          itEvent=(recvEvent.insert(pair< SEvent ,int > (event,1))).first ;
205 
206        }
207        else (itEvent->second)++ ;
208        if (itEvent->second==nbChild[lev])
209        {
210          if (lev==0)
211          {
212            bcastEvent(timeLine,hashId,lev) ;
213            recvEvent.erase(itEvent) ;
214          }
215          else
216          {
217            registerEvent( timeLine,hashId,lev) ;
218          }
219        }
220      }
221      else ++it ;
222    }
223   
224    // check if bcast request is achieved
225
226    for(list<SPendingRequest*>::iterator it=pendingSentChildRequest.begin(); it!=pendingSentChildRequest.end() ; )
227    {
228      MPI_Test(&(*it)->request,&received,&status) ;
229      if (received)
230      {
231        delete *it ;    // free memory
232        it = pendingSentChildRequest.erase(it) ;          // get out of the list
233
234      }
235      else ++it ;
236       
237    }
238  }
239 
240  void CEventScheduler::bcastEvent(const size_t timeLine, const size_t contextHashId, const size_t lev)
241  {
242    SPendingRequest* sentRequest ;
243     
244   
245    for(int i=0; i<nbChild[lev];i++)
246    {
247      sentRequest=new SPendingRequest ;
248      sentRequest->buffer[0]=timeLine ;
249      sentRequest->buffer[1]=contextHashId ;
250      sentRequest->buffer[2]=lev+1 ;
251      MPI_Isend(sentRequest->buffer,3, MPI_UNSIGNED_LONG, child[lev][i], 1, communicator, & sentRequest->request) ;
252      pendingSentChildRequest.push_back(sentRequest) ;
253    }
254  }
255   
256
257}
Note: See TracBrowser for help on using the repository browser.