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