source: XIOS/dev/dev_ym/XIOS_COUPLING/src/manager/services.cpp @ 2230

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

Fix some Dead-lock issue...
YM

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 7.2 KB
Line 
1#include "services.hpp"
2#include "services_manager.hpp"
3#include "mpi.hpp"
4#include "cxios.hpp"
5#include "server_context.hpp"
6#include "event_scheduler.hpp"
7
8namespace xios
9{
10  CService::CService(MPI_Comm serviceComm, const std::string& poolId, const std::string& serviceId, const int& partitionId, 
11                     int type, int nbPartitions) : finalizeSignal_(false), eventScheduler_(nullptr), poolId_(poolId), serviceId_(serviceId),
12                                                   partitionId_(partitionId), type_(type), nbPartitions_(nbPartitions), hasNotification_(false)
13
14
15  {
16    int localRank, globalRank, commSize ;
17
18    MPI_Comm_dup(serviceComm, &serviceComm_) ;
19    MPI_Comm globalComm_=CXios::getXiosComm() ;
20 
21    MPI_Comm_rank(globalComm_,&globalRank) ;
22    MPI_Comm_rank(serviceComm_,&localRank) ;
23   
24    winNotify_ = new CWindowManager(serviceComm_, maxBufferSize_) ;
25    winNotify_->lockWindow(localRank,0) ;
26    winNotify_->updateToWindow(localRank, this, &CService::createContextDumpOut) ;
27    winNotify_->unlockWindow(localRank,0) ;
28    MPI_Barrier(serviceComm_) ;
29    if (localRank==localLeader_) 
30    {
31      globalLeader_=globalRank ;
32      MPI_Comm_rank(serviceComm_,&commSize) ;
33      CXios::getServicesManager()->registerService(poolId, serviceId, partitionId, type, commSize, nbPartitions, globalLeader_) ;
34    }
35    eventScheduler_ = new CEventScheduler(serviceComm_) ;
36
37    ostringstream oss;
38    oss<<partitionId;
39    name_= poolId+"::"+serviceId+"_"+oss.str();
40  }
41
42  void CService::createContext( const std::string& poolId, const std::string& serviceId, const int& partitionId, const std::string& contextId)
43  {
44    int commSize ;
45    MPI_Comm_size(serviceComm_, &commSize) ;
46   
47    for(int rank=0; rank<commSize; rank++) 
48    {
49      notifyOutType_=NOTIFY_CREATE_CONTEXT ;
50      notifyOutCreateContext_ = make_tuple(poolId, serviceId, partitionId, contextId) ;
51      sendNotification(rank) ;
52    }
53  }
54/*
55  void CService::createContext(const std::string& contextId)
56  {
57    contexts_[contextId] = new CServerContext(this, serviceComm_, poolId_, serviceId_, partitionId_, contextId) ;
58  }
59*/
60  void CService::createContextNotify(int rank, const std::string& poolId, const std::string& serviceId, const int& partitionId, const std::string& contextId)
61  {
62    winNotify_->lockWindow(rank,0) ;
63    winNotify_->updateFromWindow(rank, this, &CService::createContextDumpIn) ;
64    notifications_.push_back(std::make_tuple(poolId, serviceId, partitionId, contextId)) ;
65    winNotify_->updateToWindow(rank, this, &CService::createContextDumpOut) ; 
66    winNotify_->unlockWindow(rank,0) ;   
67  }
68
69
70  void CService::createContextDumpOut(CBufferOut& buffer)
71  {
72    buffer.realloc(maxBufferSize_) ;
73   
74    buffer << (int) (notifications_.size());
75   
76    for(auto it=notifications_.begin();it!=notifications_.end(); ++it) 
77      buffer << std::get<0>(*it) << std::get<1>(*it) << std::get<2>(*it) << std::get<3>(*it)  ;
78  }
79
80
81  void CService::createContextDumpIn(CBufferIn& buffer)
82  {
83    std::string poolId ;
84    std::string serviceId ;
85    int partitionId ;
86    std::string contextId ;
87   
88    notifications_.clear() ;
89    int nbNotifications ;
90    buffer>>nbNotifications ;
91    for(int i=0;i<nbNotifications;i++) 
92    {
93      buffer>>poolId>>serviceId>>partitionId>>contextId ;
94      notifications_.push_back(std::make_tuple(poolId, serviceId, partitionId, contextId)) ;
95    }
96  }
97
98  bool CService::eventLoop(bool serviceOnly)
99  {
100    //checkCreateContextNotification() ;
101    checkNotifications() ;
102
103    eventScheduler_->checkEvent() ;
104    for(auto it=contexts_.begin();it!=contexts_.end();++it) 
105    {
106      if (it->second->eventLoop(serviceOnly))
107      {
108        contexts_.erase(it) ;
109        // destroy server_context -> to do later
110        break ;
111      } ;
112    }
113
114    if (contexts_.empty() && finalizeSignal_) return true ;
115    else return false ;
116  }
117
118  void CService::sendNotification(int rank)
119  {
120    winNotify_->lockWindow(rank,0) ;
121    winNotify_->pushToWindow(rank, this, &CService::notificationsDumpOut) ;
122    winNotify_->unlockWindow(rank,0) ;
123  }
124
125 
126  void CService::notificationsDumpOut(CBufferOut& buffer)
127  {
128   
129    buffer.realloc(maxBufferSize_) ;
130   
131    if (notifyOutType_==NOTIFY_CREATE_CONTEXT)
132    {
133      auto& arg=notifyOutCreateContext_ ;
134      buffer << notifyOutType_ << std::get<0>(arg)<<std::get<1>(arg) << std::get<2>(arg)<<std::get<3>(arg) ;
135    }
136  }
137
138  void CService::notificationsDumpIn(CBufferIn& buffer)
139  {
140    if (buffer.bufferSize() == 0) notifyInType_= NOTIFY_NOTHING ;
141    else
142    {
143      buffer>>notifyInType_;
144      if (notifyInType_==NOTIFY_CREATE_CONTEXT)
145      {
146        info(10)<<"NotifyDumpOut"<<endl ;
147        auto& arg=notifyInCreateContext_ ;
148        buffer >> std::get<0>(arg)>> std::get<1>(arg) >> std::get<2>(arg)>> std::get<3>(arg);
149      }
150    }
151  }
152
153
154
155
156  void CService::checkNotifications(void)
157  {
158    if (!hasNotification_)
159    {
160      int commRank ;
161      MPI_Comm_rank(serviceComm_, &commRank) ;
162      winNotify_->lockWindow(commRank,0) ;
163      winNotify_->popFromWindow(commRank, this, &CService::notificationsDumpIn) ;
164      winNotify_->unlockWindow(commRank,0) ;
165     
166      if (notifyInType_!= NOTIFY_NOTHING)
167      {
168        hasNotification_=true ;
169        std::hash<string> hashString ;
170        size_t hashId = hashString(name_) ;
171        size_t currentTimeLine=0 ;
172        eventScheduler_->registerEvent(currentTimeLine,hashId); 
173      }
174    }
175   
176    if (hasNotification_)
177    {
178      std::hash<string> hashString ;
179      size_t hashId = hashString(name_) ;
180      size_t currentTimeLine=0 ;
181      if (eventScheduler_->queryEvent(currentTimeLine,hashId))
182      {
183        eventScheduler_->popEvent() ;
184        if (notifyInType_==NOTIFY_CREATE_CONTEXT) createContext() ;
185        hasNotification_=false ;
186      }
187    }
188  }
189
190
191
192
193  void CService::checkCreateContextNotification(void)
194  {
195    int commRank ;
196    MPI_Comm_rank(serviceComm_, &commRank) ;
197    winNotify_->lockWindow(commRank,0) ;
198    winNotify_->updateFromWindow(commRank, this, &CService::createContextDumpIn) ;
199   
200    if (!notifications_.empty())
201    {
202      auto info = notifications_.front() ;
203      createNewContext(get<0>(info), get<1>(info), get<2>(info), get<3>(info)) ;
204      notifications_.pop_front() ;
205      winNotify_->updateToWindow(commRank, this, &CService::createContextDumpOut) ;     
206    }
207    winNotify_->unlockWindow(commRank,0) ;
208  }
209
210  void CService::createContext(void)
211   {
212     auto& arg=notifyInCreateContext_ ;
213     string poolId = get<0>(arg) ;
214     string& serviceId = get<1>(arg) ;
215     int partitionId = get<2>(arg) ;
216     string contextId = get<3>(arg) ;
217     contexts_[contextId] = new CServerContext(this, serviceComm_, poolId, serviceId, partitionId, contextId) ; 
218   }
219
220   //to remove
221   void CService::createNewContext(const std::string& poolId, const std::string& serviceId, const int& partitionId, const std::string& contextId)
222   {
223     contexts_[contextId] = new CServerContext(this, serviceComm_, poolId, serviceId, partitionId, contextId) ; 
224   }
225
226  void CService::finalizeSignal(void)
227  {
228    finalizeSignal_=true ;
229    for(auto it=contexts_.begin();it!=contexts_.end();++it) it->second->finalizeSignal() ;
230  }
231
232  CEventScheduler* CService::getEventScheduler(void)
233  {
234    return eventScheduler_ ;
235  }
236}
Note: See TracBrowser for help on using the repository browser.