blacklist.hh
Go to the documentation of this file.
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 /*
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 2 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 
19  Consult the COPYING file in the top-level source directory of this
20  module for the precise wording of the license and the list of
21  copyright holders.
22 */
27 #ifndef EWOMS_BLACK_LIST_HH
28 #define EWOMS_BLACK_LIST_HH
29 
30 #include "overlaptypes.hh"
31 
32 #if HAVE_MPI
34 
35 #include <dune/grid/common/datahandleif.hh>
36 #include <dune/grid/common/gridenums.hh>
37 #endif // HAVE_MPI
38 
39 #include <algorithm>
40 
41 namespace Ewoms {
42 namespace Linear {
47 class BlackList
48 {
49 public:
51  Index nativeIndexOfPeer;
52  Index myOwnNativeIndex;
53  };
54  typedef std::vector<PeerBlackListedEntry> PeerBlackList;
55  typedef std::map<ProcessRank, PeerBlackList> PeerBlackLists;
56 
57  BlackList()
58  { }
59 
60  BlackList(const BlackList&) = default;
61 
62  bool hasIndex(Index nativeIdx) const
63  { return nativeBlackListedIndices_.count(nativeIdx) > 0; }
64 
65  void addIndex(Index nativeIdx)
66  { nativeBlackListedIndices_.insert(nativeIdx); }
67 
68  Index nativeToDomestic(Index nativeIdx) const
69  {
70  auto it = nativeToDomesticMap_.find(nativeIdx);
71  if (it == nativeToDomesticMap_.end())
72  return -1;
73  return it->second;
74  }
75 
76  void setPeerList(ProcessRank peerRank, const PeerBlackList& peerBlackList)
77  { peerBlackLists_[peerRank] = peerBlackList; }
78 
79  template <class DomesticOverlap>
80  void updateNativeToDomesticMap(const DomesticOverlap& domesticOverlap)
81  {
82 #if HAVE_MPI
83  auto peerListIt = peerBlackLists_.begin();
84  const auto& peerListEndIt = peerBlackLists_.end();
85  for (; peerListIt != peerListEndIt; ++peerListIt) {
86  sendGlobalIndices_(peerListIt->first,
87  peerListIt->second,
88  domesticOverlap);
89  }
90 
91  peerListIt = peerBlackLists_.begin();
92  for (; peerListIt != peerListEndIt; ++peerListIt) {
93  receiveGlobalIndices_(peerListIt->first, domesticOverlap);
94  }
95 
96  peerListIt = peerBlackLists_.begin();
97  for (; peerListIt != peerListEndIt; ++peerListIt) {
98  numGlobalIdxSendBuff_.at(peerListIt->first).wait();
99  globalIdxSendBuff_.at(peerListIt->first).wait();
100  }
101 #endif // HAVE_MPI
102  }
103 
104  void print() const
105  {
106  std::cout << "my own blacklisted indices:\n";
107  auto idxIt = nativeBlackListedIndices_.begin();
108  const auto& idxEndIt = nativeBlackListedIndices_.end();
109  for (; idxIt != idxEndIt; ++idxIt)
110  std::cout << " (native index: " << *idxIt
111  << ", domestic index: " << nativeToDomestic(*idxIt) << ")\n";
112  std::cout << "blacklisted indices of the peers in my own domain:\n";
113  auto peerListIt = peerBlackLists_.begin();
114  const auto& peerListEndIt = peerBlackLists_.end();
115  for (; peerListIt != peerListEndIt; ++peerListIt) {
116  ProcessRank peerRank = peerListIt->first;
117  std::cout << " peer " << peerRank << ":\n";
118  auto idx2It = peerListIt->second.begin();
119  const auto& idx2EndIt = peerListIt->second.end();
120  for (; idx2It != idx2EndIt; ++ idx2It)
121  std::cout << " (native index: " << idx2It->myOwnNativeIndex
122  << ", native peer index: " << idx2It->nativeIndexOfPeer << ")\n";
123  }
124  }
125 
126 private:
127 #if HAVE_MPI
128  template <class DomesticOverlap>
129  void sendGlobalIndices_(ProcessRank peerRank,
130  const PeerBlackList& peerIndices,
131  const DomesticOverlap& domesticOverlap)
132  {
133  auto& numIdxBuff = numGlobalIdxSendBuff_[peerRank];
134  auto& idxBuff = globalIdxSendBuff_[peerRank];
135 
136  numIdxBuff.resize(1);
137  numIdxBuff[0] = static_cast<unsigned>(peerIndices.size());
138  numIdxBuff.send(peerRank);
139 
140  idxBuff.resize(2*peerIndices.size());
141  for (size_t i = 0; i < peerIndices.size(); ++i) {
142  // global index
143  Index myNativeIdx = peerIndices[i].myOwnNativeIndex;
144  Index myDomesticIdx = domesticOverlap.nativeToDomestic(myNativeIdx);
145  idxBuff[2*i + 0] = domesticOverlap.domesticToGlobal(myDomesticIdx);
146 
147  // native peer index
148  idxBuff[2*i + 1] = peerIndices[i].nativeIndexOfPeer;
149  }
150  idxBuff.send(peerRank);
151  }
152 
153  template <class DomesticOverlap>
154  void receiveGlobalIndices_(ProcessRank peerRank,
155  const DomesticOverlap& domesticOverlap)
156  {
157  MpiBuffer<unsigned> numGlobalIdxBuf(1);
158  numGlobalIdxBuf.receive(peerRank);
159  unsigned numIndices = numGlobalIdxBuf[0];
160 
161  MpiBuffer<Index> globalIdxBuf(2*numIndices);
162  globalIdxBuf.receive(peerRank);
163  for (unsigned i = 0; i < numIndices; ++i) {
164  Index globalIdx = globalIdxBuf[2*i + 0];
165  Index nativeIdx = globalIdxBuf[2*i + 1];
166 
167  nativeToDomesticMap_[nativeIdx] = domesticOverlap.globalToDomestic(globalIdx);
168  }
169  }
170 #endif // HAVE_MPI
171 
172  std::set<Index> nativeBlackListedIndices_;
173  std::map<Index, Index> nativeToDomesticMap_;
174 #if HAVE_MPI
175  std::map<ProcessRank, MpiBuffer<unsigned>> numGlobalIdxSendBuff_;
176  std::map<ProcessRank, MpiBuffer<Index>> globalIdxSendBuff_;
177 #endif // HAVE_MPI
178 
179  PeerBlackLists peerBlackLists_;
180 };
181 
182 } // namespace Linear
183 } // namespace Ewoms
184 
185 #endif
Definition: baseauxiliarymodule.hh:37
Simplifies handling of buffers to be used in conjunction with MPI.
int Index
The type of an index of a degree of freedom.
Definition: overlaptypes.hh:44
Expresses which degrees of freedom are blacklisted for the parallel linear solvers and which domestic...
Definition: blacklist.hh:47
This files provides several data structures for storing tuples of indices of remote and/or local proc...
unsigned ProcessRank
The type of the rank of a process.
Definition: overlaptypes.hh:49