[1918] | 1 | #include "element.hpp" |
---|
| 2 | #include "distributed_view.hpp" |
---|
| 3 | #include "local_view.hpp" |
---|
| 4 | #include "local_connector.hpp" |
---|
| 5 | #include "context_client.hpp" |
---|
| 6 | #include "context_server.hpp" |
---|
| 7 | #include "mpi.hpp" |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | namespace xios |
---|
| 11 | { |
---|
| 12 | |
---|
| 13 | CDistributedElement::CDistributedElement(int globalSize, const map<int, CArray<size_t,1>>& globalIndex) |
---|
| 14 | { |
---|
| 15 | globalSize_= globalSize ; |
---|
| 16 | for(auto index : globalIndex) |
---|
| 17 | { |
---|
| 18 | globalIndex_[index.first].reference(index.second.copy()) ; |
---|
| 19 | localSize_[index.first] = index.second.numElements() ; |
---|
| 20 | } |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | CDistributedElement::CDistributedElement(CEventServer& event) |
---|
| 24 | { |
---|
| 25 | recvFromClient(event) ; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | void CDistributedElement::addView(CElementView::type type, std::map<int, CArray<int,1>>& indexView) |
---|
| 29 | { |
---|
[2267] | 30 | views_[type] = make_shared<CDistributedView>(shared_from_this(), type, indexView) ; |
---|
[1918] | 31 | } |
---|
| 32 | |
---|
[1930] | 33 | void CDistributedElement::addView(CElementView::type type, std::map<int, CArray<bool,1>>& maskView) |
---|
| 34 | { |
---|
[2267] | 35 | views_[type] = make_shared<CDistributedView>(shared_from_this(), type, maskView) ; |
---|
[1930] | 36 | } |
---|
| 37 | |
---|
[1918] | 38 | void CDistributedElement::addFullView(void) |
---|
| 39 | { |
---|
| 40 | if (views_[CElementView::FULL]!=nullptr) return ; |
---|
| 41 | |
---|
| 42 | std::map<int, CArray<int,1>> indexView ; |
---|
| 43 | for(auto rank : globalIndex_) |
---|
| 44 | { |
---|
| 45 | auto& index = indexView[rank.first] ; |
---|
| 46 | int size=rank.second.numElements() ; |
---|
| 47 | index.resize(size) ; |
---|
| 48 | for(int i=0;i<size;i++) index(i)=i ; |
---|
| 49 | } |
---|
| 50 | addView(CElementView::FULL, indexView) ; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | void CDistributedElement::sendToServer(CContextClient* client, CEventClient& event, const CMessage& messageHeader) |
---|
| 54 | { |
---|
| 55 | int remoteSize = client->getRemoteSize() ; |
---|
| 56 | vector<int> nbSenders(remoteSize,0) ; |
---|
| 57 | for(auto rank : globalIndex_) nbSenders[rank.first]=1 ; |
---|
| 58 | MPI_Allreduce(MPI_IN_PLACE, nbSenders.data(), remoteSize, MPI_INT, MPI_SUM, client->getIntraComm()) ; |
---|
| 59 | |
---|
| 60 | list<CMessage> messages; |
---|
| 61 | for(auto ranksData : globalIndex_) |
---|
| 62 | { |
---|
| 63 | int rank = ranksData.first ; |
---|
| 64 | auto& data = ranksData.second ; |
---|
| 65 | |
---|
| 66 | messages.push_back(CMessage(messageHeader)); |
---|
| 67 | messages.back()<<globalSize_<<data; |
---|
| 68 | event.push(rank, nbSenders[rank], messages.back()); |
---|
| 69 | } |
---|
| 70 | client->sendEvent(event) ; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | void CDistributedElement::recvFromClient(CEventServer& event) |
---|
| 74 | { |
---|
| 75 | globalIndex_.clear(); |
---|
| 76 | for (auto& subEvent : event.subEvents) |
---|
| 77 | { |
---|
| 78 | CBufferIn* buffer = subEvent.buffer; |
---|
| 79 | int rank=subEvent.rank ; |
---|
| 80 | *buffer>>globalSize_ ; |
---|
| 81 | *buffer >> globalIndex_[rank]; |
---|
| 82 | } |
---|
| 83 | localSize_.clear() ; |
---|
| 84 | for(auto& globalIndex : globalIndex_) localSize_[globalIndex.first] = globalIndex.second.numElements() ; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | |
---|
[2396] | 89 | CLocalElement::CLocalElement(int localRank, size_t globalSize, const CArray<size_t,1>& globalIndex) |
---|
[1918] | 90 | : CDistributedElement(globalSize, {{localRank, globalIndex}}), |
---|
| 91 | globalIndex_(CDistributedElement::globalIndex_[localRank]), localSize_(CDistributedElement::localSize_[localRank]), localRank_(localRank) |
---|
| 92 | { |
---|
| 93 | |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | CLocalElement::CLocalElement(int localRank, CEventServer& event) : globalIndex_(CDistributedElement::globalIndex_[localRank]), localSize_(CDistributedElement::localSize_[localRank]), localRank_(localRank) |
---|
| 97 | { |
---|
| 98 | recvFromClient(localRank, event) ; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | void CLocalElement::recvFromClient(int localRank, CEventServer& event) |
---|
| 102 | { |
---|
| 103 | set<size_t> globalIndex ; |
---|
| 104 | |
---|
| 105 | for (auto& subEvent : event.subEvents) |
---|
| 106 | { |
---|
| 107 | CBufferIn* buffer = subEvent.buffer; |
---|
| 108 | int rank=subEvent.rank ; |
---|
| 109 | CArray<size_t,1> indGlo ; |
---|
| 110 | *buffer >> globalSize_>> indGlo; |
---|
| 111 | globalIndex.insert(indGlo.dataFirst(), indGlo.dataFirst()+indGlo.numElements()) ; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | localSize_ = globalIndex.size() ; |
---|
| 115 | globalIndex_.resize(localSize_) ; |
---|
| 116 | int i=0 ; |
---|
| 117 | for(auto& ind : globalIndex) { globalIndex_(i)=ind ; i++; } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | void CLocalElement::addView(CElementView::type type, CArray<int,1>& indexView) |
---|
| 121 | { |
---|
[2267] | 122 | views_[type] = make_shared<CLocalView>(static_pointer_cast<CLocalElement>(shared_from_this()), type, indexView) ; |
---|
[1918] | 123 | } |
---|
| 124 | |
---|
[1930] | 125 | void CLocalElement::addView(CElementView::type type, CArray<bool,1>& maskView) |
---|
| 126 | { |
---|
[2267] | 127 | views_[type] = make_shared<CLocalView>(static_pointer_cast<CLocalElement>(shared_from_this()), type, maskView) ; |
---|
[1930] | 128 | } |
---|
| 129 | |
---|
[1918] | 130 | void CLocalElement::addFullView(void) |
---|
| 131 | { |
---|
| 132 | if (views_[CElementView::FULL]!=nullptr) return ; |
---|
| 133 | |
---|
| 134 | CArray<int,1> indexView(localSize_) ; |
---|
| 135 | for(int i=0;i<localSize_;i++) indexView(i)=i ; |
---|
| 136 | addView(CElementView::FULL, indexView) ; |
---|
| 137 | } |
---|
| 138 | |
---|
[2267] | 139 | shared_ptr<CLocalView> CLocalElement::getView(CElementView::type type) |
---|
[1918] | 140 | { |
---|
[2267] | 141 | if (views_[(size_t)type]==nullptr) { ERROR("CLocalElement::getView(CElementView::type type)",<<"View is not initialized");} |
---|
| 142 | else return static_pointer_cast<CLocalView>(views_[(size_t)type]) ; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | shared_ptr<CLocalConnector> CLocalElement::getConnector(CElementView::type srcType, CElementView::type dstType) |
---|
| 146 | { |
---|
[1918] | 147 | auto newPair = pair<CElementView::type,CElementView::type>(srcType,dstType); |
---|
| 148 | auto it = connectors_.find(newPair) ; |
---|
| 149 | if (it==connectors_.end()) |
---|
| 150 | { |
---|
[2267] | 151 | auto insertPair=pair<pair<CElementView::type,CElementView::type>, shared_ptr<CLocalConnector>>(newPair,make_shared<CLocalConnector>(getView(srcType),getView(dstType))) ; |
---|
[1918] | 152 | it=connectors_.insert(insertPair).first ; |
---|
| 153 | it->second->computeConnector() ; |
---|
| 154 | } |
---|
| 155 | return it->second ; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | } |
---|