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

Last change on this file since 2121 was 2022, checked in by ymipsl, 3 years ago

Reimplement coupling in the new infrastructure.
Tested for 2-way coupling toy model.

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