source: XIOS/dev/dev_ym/XIOS_SERVICES/src/manager/ressources_manager.cpp @ 1761

Last change on this file since 1761 was 1761, checked in by ymipsl, 5 years ago

implementing first guess for service functionnalities.

YM

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