source: XIOS/dev/dev_ym/XIOS_COUPLING/src/manager/ressources_manager.cpp @ 2260

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

Improvment of one sided protocol

  • removed latency
  • solve dead-lock

YM

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 8.6 KB
Line 
1#include "ressources_manager.hpp"
2#include "server.hpp"
3#include "servers_ressource.hpp"
4#include "timer.hpp"
5
6
7
8
9
10namespace xios
11{
12  using namespace std;
13
14  CRessourcesManager::CRessourcesManager(bool isXiosServer) 
15  {
16   
17    xiosComm_ = CXios::getXiosComm()  ;
18   
19    int commRank ; 
20    MPI_Comm_rank(xiosComm_, &commRank) ;
21    if (commRank==0 && isXiosServer) MPI_Comm_rank(xiosComm_, &commRank) ; 
22    else commRank=0 ;
23    MPI_Allreduce(&commRank, &managerGlobalLeader_, 1, MPI_INT, MPI_SUM, xiosComm_) ;
24
25    MPI_Comm_rank(xiosComm_, &commRank) ;
26    winNotify_ = new CWindowManager(xiosComm_, maxBufferSize_) ;
27   
28
29    winRessources_ = new CWindowManager(xiosComm_, maxBufferSize_) ;
30    winRessources_->lockWindow(commRank,0) ;
31    serverLeader_=-1 ;
32    winRessources_->updateToWindow(commRank, this, &CRessourcesManager::ressourcesDumpOut) ;
33    winRessources_->unlockWindow(commRank,0) ;
34
35    MPI_Barrier(xiosComm_)  ;   
36  }
37 
38  CRessourcesManager::~CRessourcesManager()
39  {
40    delete winNotify_ ;
41    delete winRessources_ ;
42  } 
43
44  void CRessourcesManager::createPool(const string& poolId, int size)
45  {
46    info(40)<<"CRessourcesManager::createPool : calling createPool : "<<poolId<<"  of size"<<size<<endl ;
47    info(40)<<"send notification to leader : "<<serverLeader_<<endl ;
48    winRessources_->lockWindow(managerGlobalLeader_,0) ;
49    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
50    winRessources_->unlockWindow(managerGlobalLeader_,0) ;   
51   
52    notifyType_=NOTIFY_CREATE_POOL ;
53    notifyCreatePool_=make_tuple(poolId, size) ;
54    info(40)<<"CRessourcesManager::createPool : send notification creating pool to server leader "<<serverLeader_<<endl ;
55    sendNotification(serverLeader_) ; 
56  }
57 
58  void CRessourcesManager::finalize(void)
59  {
60    winRessources_->lockWindow(managerGlobalLeader_,0) ;
61    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
62    winRessources_->unlockWindow(managerGlobalLeader_,0) ;   
63   
64    if (serverLeader_!=-1)
65    {
66      notifyType_=NOTIFY_FINALIZE ;
67      info(40)<<"CRessourcesManager::finalize : send notification finalize to server leader "<<serverLeader_<<endl ;
68      sendNotification(serverLeader_) ;
69    } 
70  }
71
72  void CRessourcesManager::sendNotification(int rank)
73  {
74    winNotify_->lockWindowExclusive(rank) ;
75    winNotify_->pushToLockedWindow(rank, this, &CRessourcesManager::notificationsDumpOut) ;
76    winNotify_->unlockWindow(rank) ;
77  }
78
79 
80  void CRessourcesManager::notificationsDumpOut(CBufferOut& buffer)
81  {
82   
83    buffer.realloc(maxBufferSize_) ;
84   
85    if (notifyType_==NOTIFY_CREATE_POOL)
86    {
87      auto& arg=notifyCreatePool_ ;
88      buffer << notifyType_<< get<0>(arg) << get<1>(arg) ;
89    }
90    else if (notifyType_==NOTIFY_FINALIZE)
91    {
92      buffer << notifyType_ ;
93    }
94  }
95
96  void CRessourcesManager::notificationsDumpIn(CBufferIn& buffer)
97  {
98    if (buffer.bufferSize() == 0) notifyType_= NOTIFY_NOTHING ;
99    else
100    {
101      buffer>>notifyType_;
102      if (notifyType_==NOTIFY_CREATE_POOL)
103      {
104        auto& arg=notifyCreatePool_ ;
105        buffer >> get<0>(arg) >> get<1>(arg)  ;
106      }
107      else if (notifyType_==NOTIFY_FINALIZE) { /*nothing to do*/ }
108    }
109
110  }
111
112  void CRessourcesManager::eventLoop(void)
113  {
114    CTimer::get("CRessourcesManager::eventLoop").resume();
115    int flag ;
116    MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE);
117    double time=MPI_Wtime() ;
118    if (time-lastEventLoop_ > eventLoopLatency_) 
119    {
120      checkNotifications() ;
121      lastEventLoop_=time ;
122    }
123
124    CTimer::get("CRessourcesManager::eventLoop").suspend();
125  }
126 
127  void CRessourcesManager::checkNotifications(void)
128  {
129    int commRank ;
130    MPI_Comm_rank(xiosComm_, &commRank) ;
131    CTimer::get("CRessourcesManager::checkNotifications lock").resume();
132    winNotify_->lockWindowExclusive(commRank) ;
133    CTimer::get("CRessourcesManager::checkNotifications lock").suspend();
134    CTimer::get("CRessourcesManager::checkNotifications pop").resume();
135    winNotify_->popFromLockedWindow(commRank, this, &CRessourcesManager::notificationsDumpIn) ;
136    CTimer::get("CRessourcesManager::checkNotifications pop").suspend();
137    CTimer::get("CRessourcesManager::checkNotifications unlock").resume();
138    winNotify_->unlockWindow(commRank) ;
139    CTimer::get("CRessourcesManager::checkNotifications unlock").suspend();
140    if (notifyType_==NOTIFY_CREATE_POOL) createPool() ;
141    else if (notifyType_==NOTIFY_FINALIZE) finalizeSignal() ;
142  }
143
144  void CRessourcesManager::createPool(void)
145  {
146   
147    auto& arg=notifyCreatePool_ ;
148    string poolId=get<0>(arg) ;
149    int size=get<1>(arg) ;
150    info(40)<<"CRessourcesManager::createPool : receive create pool notification : "<< poolId<<"  of size "<<size<<endl ;
151    CServer::getServersRessource()->createPool(poolId,size) ;
152  } 
153
154  void CRessourcesManager::finalizeSignal(void)
155  {
156    info(40)<<"CRessourcesManager::createPool : receive finalize notification"<<endl ;
157    CServer::getServersRessource()->finalize() ;
158  }
159
160  void CRessourcesManager::ressourcesDumpOut(CBufferOut& buffer)
161  {
162   
163    buffer.realloc(maxBufferSize_) ;
164   
165    buffer<<serverLeader_ ; 
166    buffer<<(int) pools_.size();
167    for(auto it=pools_.begin();it!=pools_.end(); ++it)
168    { 
169      auto key = it->first ;
170      auto val = it->second ; 
171      buffer << key<<std::get<0>(val) << std::get<1>(val)  ;
172    }
173  }
174
175  void CRessourcesManager::ressourcesDumpIn(CBufferIn& buffer)
176  {
177    std::string poolId ;
178    int size ;
179    int leader ;
180   
181    buffer>>serverLeader_ ;
182    pools_.clear() ;
183    int nbPools ;
184    buffer>>nbPools ;
185    for(int i=0;i<nbPools;i++) 
186    {
187      buffer>>poolId>>size>>leader ;
188      pools_[poolId]=std::make_tuple(size,leader) ;
189    }
190  }
191 
192  void CRessourcesManager::registerServerLeader(int serverLeaderRank)
193  {
194    winRessources_->lockWindow(managerGlobalLeader_,0) ;
195    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
196    serverLeader_ = serverLeaderRank ;
197    winRessources_->updateToWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpOut) ;
198    winRessources_->unlockWindow(managerGlobalLeader_,0) ;   
199  }
200 
201  void CRessourcesManager::registerRessourcesSize(int size)
202  {
203    winRessources_->lockWindow(managerGlobalLeader_,0) ;
204    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
205    ressourcesSize_ = size ;
206    freeRessourcesSize_ = size ;
207    winRessources_->updateToWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpOut) ;
208    winRessources_->unlockWindow(managerGlobalLeader_,0) ;   
209  }
210
211 
212  void CRessourcesManager::registerPool(const string& poolId, int size, int leader)
213  {
214    winRessources_->lockWindow(managerGlobalLeader_,0) ;
215    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
216    pools_[poolId] = make_tuple(size,leader) ;
217    freeRessourcesSize_-=size ;
218    winRessources_->updateToWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpOut) ;
219    winRessources_->unlockWindow(managerGlobalLeader_,0) ;   
220  }
221
222
223  bool CRessourcesManager::getPoolInfo(const string& poolId, int& size, int& leader)
224  {
225    winRessources_->lockWindow(managerGlobalLeader_,0) ;
226    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
227    winRessources_->unlockWindow(managerGlobalLeader_,0) ;
228
229    auto it=pools_.find(poolId) ;
230    if ( it == pools_.end()) return false ;
231    else
232    {
233      size=get<0>(it->second) ;
234      leader=get<1>(it->second) ;
235      return true ;
236    }
237  }
238
239  int CRessourcesManager::getRessourcesSize(void)
240  {
241    winRessources_->lockWindow(managerGlobalLeader_,0) ;
242    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
243    winRessources_->unlockWindow(managerGlobalLeader_,0) ;
244
245    return ressourcesSize_ ;
246  }
247
248  int CRessourcesManager::getFreeRessourcesSize(void)
249  {
250    winRessources_->lockWindow(managerGlobalLeader_,0) ;
251    winRessources_->updateFromWindow(managerGlobalLeader_, this, &CRessourcesManager::ressourcesDumpIn) ;
252    winRessources_->unlockWindow(managerGlobalLeader_,0) ;
253
254    return freeRessourcesSize_ ;
255  } 
256
257  bool CRessourcesManager::getPoolLeader(const string& poolId, int& leader)
258  {
259    int size ;
260    return getPoolInfo(poolId, size, leader) ;
261  }
262
263  bool CRessourcesManager::getPoolSize(const string& poolId, int& size)
264  {
265    int leader ;
266    return getPoolInfo(poolId, size, leader) ;
267  }
268
269  bool CRessourcesManager::hasPool(const string& poolId)
270  {
271    int leader,size ;
272    return getPoolInfo(poolId, size, leader) ;
273  }
274}
Note: See TracBrowser for help on using the repository browser.