[1761] | 1 | #include "pool_ressource.hpp" |
---|
| 2 | #include "services.hpp" |
---|
| 3 | #include "buffer_in.hpp" |
---|
| 4 | #include "buffer_out.hpp" |
---|
| 5 | #include "message.hpp" |
---|
| 6 | #include "type.hpp" |
---|
| 7 | #include "cxios.hpp" |
---|
[2246] | 8 | #include "timer.hpp" |
---|
[1761] | 9 | |
---|
| 10 | namespace xios |
---|
| 11 | { |
---|
| 12 | CPoolRessource::CPoolRessource(MPI_Comm poolComm, const std::string& Id) : Id_(Id), finalizeSignal_(false) |
---|
| 13 | { |
---|
| 14 | int commRank, commSize ; |
---|
| 15 | MPI_Comm_dup(poolComm, &poolComm_) ; |
---|
| 16 | winNotify_ = new CWindowManager(poolComm_, maxBufferSize_) ; |
---|
| 17 | MPI_Comm_rank(poolComm, &commRank) ; |
---|
| 18 | MPI_Comm_size(poolComm, &commSize) ; |
---|
[2246] | 19 | info(40)<<"CPoolRessource::CPoolRessource : creating new pool : "<<Id<<endl ; |
---|
[1761] | 20 | if (commRank==localLeader_) |
---|
| 21 | { |
---|
| 22 | for(int i=0; i<commSize;i++) occupancy_.insert(std::pair<char,int>(0,i)) ; |
---|
| 23 | int globalLeaderRank ; |
---|
| 24 | MPI_Comm_rank(CXios::getXiosComm(),&globalLeaderRank) ; |
---|
| 25 | CXios::getRessourcesManager()->registerPool(Id, commSize, globalLeaderRank) ; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | winNotify_->lockWindow(commRank,0) ; |
---|
| 29 | winNotify_->updateToWindow(commRank, this, &CPoolRessource::createServiceDumpOut) ; |
---|
| 30 | winNotify_->unlockWindow(commRank,0) ; |
---|
[2208] | 31 | MPI_Barrier(poolComm_) ; |
---|
[1761] | 32 | } |
---|
| 33 | |
---|
| 34 | void CPoolRessource::createService(const std::string& serviceId, int type, int size, int nbPartitions) |
---|
| 35 | { |
---|
| 36 | // for now suppose nbPartitions=1 |
---|
| 37 | |
---|
| 38 | auto it=occupancy_.begin() ; |
---|
| 39 | int commSize ; |
---|
| 40 | MPI_Comm_size(poolComm_, &commSize) ; |
---|
| 41 | vector<bool> procs_in(commSize,false) ; |
---|
| 42 | vector<pair<int,int>> procs_update ; |
---|
| 43 | |
---|
| 44 | for(int i=0; i<size; i++) |
---|
| 45 | { |
---|
| 46 | procs_in[it->second]=true ; |
---|
| 47 | procs_update.push_back(std::pair<int,int>(it->first+1,it->second)) ; |
---|
| 48 | ++it ; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | occupancy_.erase(occupancy_.begin(),it) ; |
---|
| 52 | occupancy_.insert(procs_update.begin(),procs_update.end()) ; |
---|
[2246] | 53 | |
---|
| 54 | info(40)<<"CPoolRessource::createService : notify createService to all pool members ; serviceId : "<<serviceId<<endl ; |
---|
[1761] | 55 | for(int rank=0; rank<commSize; rank++) |
---|
| 56 | { |
---|
| 57 | if (procs_in[rank]) createServiceNotify(rank, serviceId, type, size, nbPartitions, true) ; |
---|
| 58 | else createServiceNotify(rank, serviceId, type, size, nbPartitions, false) ; |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | void CPoolRessource::createServiceNotify(int rank, const std::string& serviceId, int type, int size, int nbPartitions, |
---|
| 64 | bool in) |
---|
| 65 | { |
---|
| 66 | winNotify_->lockWindow(rank,0) ; |
---|
| 67 | winNotify_->updateFromWindow(rank, this, &CPoolRessource::createServiceDumpIn) ; |
---|
| 68 | notifications_.push_back(std::make_tuple(serviceId,type,size,nbPartitions,in)) ; |
---|
| 69 | winNotify_->updateToWindow(rank, this, &CPoolRessource::createServiceDumpOut) ; |
---|
| 70 | winNotify_->unlockWindow(rank,0) ; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | void CPoolRessource::createServiceDumpOut(CBufferOut& buffer) |
---|
| 75 | { |
---|
| 76 | buffer.realloc(maxBufferSize_) ; |
---|
| 77 | |
---|
| 78 | buffer << (int) (notifications_.size()); |
---|
| 79 | |
---|
| 80 | for(auto it=notifications_.begin();it!=notifications_.end(); ++it) |
---|
| 81 | buffer << std::get<0>(*it) << static_cast<int>(std::get<1>(*it))<< std::get<2>(*it)<< std::get<3>(*it) << std::get<4>(*it) ; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | void CPoolRessource::createServiceDumpIn(CBufferIn& buffer) |
---|
| 86 | { |
---|
| 87 | std::string serviceId ; |
---|
| 88 | int type ; |
---|
| 89 | int size; |
---|
| 90 | int nbPartitions; |
---|
| 91 | bool in ; |
---|
| 92 | |
---|
| 93 | notifications_.clear() ; |
---|
| 94 | int nbNotifications ; |
---|
| 95 | buffer>>nbNotifications ; |
---|
| 96 | for(int i=0;i<nbNotifications;i++) |
---|
| 97 | { |
---|
| 98 | buffer>>serviceId>>type>>size>>nbPartitions>>in ; |
---|
| 99 | notifications_.push_back(std::make_tuple(serviceId,type,size,nbPartitions,in)) ; |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
[1764] | 103 | bool CPoolRessource::eventLoop(bool serviceOnly) |
---|
[1761] | 104 | { |
---|
[2246] | 105 | CTimer::get("CPoolRessource::eventLoop").resume(); |
---|
| 106 | |
---|
| 107 | double time=MPI_Wtime() ; |
---|
[2260] | 108 | int flag ; |
---|
| 109 | MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE); |
---|
[2246] | 110 | if (time-lastEventLoop_ > eventLoopLatency_) |
---|
| 111 | { |
---|
| 112 | checkCreateServiceNotification() ; |
---|
| 113 | lastEventLoop_=time ; |
---|
| 114 | } |
---|
| 115 | |
---|
[1761] | 116 | for (auto it=services_.begin(); it!=services_.end() ; ++it) |
---|
| 117 | { |
---|
[1764] | 118 | if (it->second->eventLoop(serviceOnly)) |
---|
[1761] | 119 | { |
---|
| 120 | services_.erase(it) ; |
---|
| 121 | // don't forget to free service later |
---|
| 122 | break ; |
---|
| 123 | } |
---|
| 124 | } |
---|
[2246] | 125 | CTimer::get("CPoolRessource::eventLoop").suspend(); |
---|
[1761] | 126 | if (services_.empty() && finalizeSignal_) return true ; |
---|
| 127 | else return false ; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | void CPoolRessource::checkCreateServiceNotification(void) |
---|
| 131 | { |
---|
| 132 | int commRank ; |
---|
| 133 | MPI_Comm_rank(poolComm_, &commRank) ; |
---|
| 134 | winNotify_->lockWindow(commRank,0) ; |
---|
| 135 | winNotify_->updateFromWindow(commRank, this, &CPoolRessource::createServiceDumpIn) ; |
---|
| 136 | |
---|
| 137 | if (!notifications_.empty()) |
---|
| 138 | { |
---|
| 139 | auto info = notifications_.front() ; |
---|
| 140 | createNewService(get<0>(info), get<1>(info), get<2>(info), get<3>(info), get<4>(info)) ; |
---|
| 141 | notifications_.pop_front() ; |
---|
| 142 | winNotify_->updateToWindow(commRank, this, &CPoolRessource::createServiceDumpOut) ; |
---|
| 143 | } |
---|
| 144 | winNotify_->unlockWindow(commRank,0) ; |
---|
| 145 | |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | void CPoolRessource::createNewService(const std::string& serviceId, int type, int size, int nbPartitions, bool in) |
---|
| 149 | { |
---|
[2246] | 150 | |
---|
| 151 | info(40)<<"CPoolRessource::createNewService : receive createService notification ; serviceId : "<<serviceId<<endl ; |
---|
[1761] | 152 | MPI_Comm serviceComm, newServiceComm ; |
---|
| 153 | int commRank ; |
---|
| 154 | MPI_Comm_rank(poolComm_,&commRank) ; |
---|
| 155 | MPI_Comm_split(poolComm_, in, commRank, &serviceComm) ; |
---|
| 156 | if (in) |
---|
| 157 | { |
---|
| 158 | int serviceCommSize ; |
---|
| 159 | int serviceCommRank ; |
---|
| 160 | MPI_Comm_size(serviceComm,&serviceCommSize) ; |
---|
| 161 | MPI_Comm_rank(serviceComm,&serviceCommRank) ; |
---|
| 162 | |
---|
| 163 | info(10)<<"Service "<<serviceId<<" created "<<" service size : "<<serviceCommSize<< " service rank : "<<serviceCommRank |
---|
| 164 | <<" on rank pool "<<commRank<<endl ; |
---|
| 165 | |
---|
| 166 | int partitionId ; |
---|
| 167 | if ( serviceCommRank >= (serviceCommSize/nbPartitions+1)*(serviceCommSize%nbPartitions) ) |
---|
| 168 | { |
---|
| 169 | int rank = serviceCommRank - (serviceCommSize/nbPartitions+1)*(serviceCommSize%nbPartitions) ; |
---|
| 170 | partitionId = serviceCommSize%nbPartitions + rank / (serviceCommSize/nbPartitions) ; |
---|
| 171 | } |
---|
| 172 | else partitionId = serviceCommRank / (serviceCommSize/nbPartitions + 1) ; |
---|
| 173 | |
---|
| 174 | MPI_Comm_split(serviceComm, partitionId, commRank, &newServiceComm) ; |
---|
| 175 | |
---|
| 176 | MPI_Comm_size(newServiceComm,&serviceCommSize) ; |
---|
| 177 | MPI_Comm_rank(newServiceComm,&serviceCommRank) ; |
---|
| 178 | info(10)<<"Service "<<serviceId<<" created "<<" partition : " <<partitionId<<" service size : "<<serviceCommSize |
---|
| 179 | << " service rank : "<<serviceCommRank <<" on rank pool "<<commRank<<endl ; |
---|
| 180 | |
---|
| 181 | services_[std::make_tuple(serviceId,partitionId)] = new CService(newServiceComm, Id_, serviceId, partitionId, type, nbPartitions) ; |
---|
| 182 | |
---|
| 183 | MPI_Comm_free(&newServiceComm) ; |
---|
| 184 | } |
---|
| 185 | MPI_Comm_free(&serviceComm) ; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | void CPoolRessource::createService(MPI_Comm serviceComm, const std::string& serviceId, int partitionId, int type, int nbPartitions) // for clients & attached |
---|
| 189 | { |
---|
| 190 | services_[std::make_tuple(serviceId,partitionId)] = new CService(serviceComm, Id_, serviceId, partitionId, type, nbPartitions) ; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | void CPoolRessource::finalizeSignal(void) |
---|
| 195 | { |
---|
| 196 | finalizeSignal_=true ; |
---|
| 197 | for (auto it=services_.begin(); it!=services_.end() ; ++it) it->second->finalizeSignal() ; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | } |
---|