opm-simulators
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 <iostream>
40 #include <algorithm>
41 
42 namespace Opm {
43 namespace Linear {
48 class BlackList
49 {
50 public:
52  Index nativeIndexOfPeer;
53  Index myOwnNativeIndex;
54  };
55  using PeerBlackList = std::vector<PeerBlackListedEntry>;
56  using PeerBlackLists = std::map<ProcessRank, PeerBlackList>;
57 
58  BlackList()
59  { }
60 
61  BlackList(const BlackList&) = default;
62 
63  bool hasIndex(Index nativeIdx) const
64  { return nativeBlackListedIndices_.count(nativeIdx) > 0; }
65 
66  void addIndex(Index nativeIdx)
67  { nativeBlackListedIndices_.insert(nativeIdx); }
68 
69  Index nativeToDomestic(Index nativeIdx) const
70  {
71  auto it = nativeToDomesticMap_.find(nativeIdx);
72  if (it == nativeToDomesticMap_.end())
73  return -1;
74  return it->second;
75  }
76 
77  void setPeerList(ProcessRank peerRank, const PeerBlackList& peerBlackList)
78  { peerBlackLists_[peerRank] = peerBlackList; }
79 
80  template <class DomesticOverlap>
81  void updateNativeToDomesticMap([[maybe_unused]] const DomesticOverlap& domesticOverlap)
82  {
83 #if HAVE_MPI
84  auto peerListIt = peerBlackLists_.begin();
85  const auto& peerListEndIt = peerBlackLists_.end();
86  for (; peerListIt != peerListEndIt; ++peerListIt) {
87  sendGlobalIndices_(peerListIt->first,
88  peerListIt->second,
89  domesticOverlap);
90  }
91 
92  peerListIt = peerBlackLists_.begin();
93  for (; peerListIt != peerListEndIt; ++peerListIt) {
94  receiveGlobalIndices_(peerListIt->first, domesticOverlap);
95  }
96 
97  peerListIt = peerBlackLists_.begin();
98  for (; peerListIt != peerListEndIt; ++peerListIt) {
99  numGlobalIdxSendBuff_.at(peerListIt->first).wait();
100  globalIdxSendBuff_.at(peerListIt->first).wait();
101  }
102 #endif // HAVE_MPI
103  }
104 
105  void print() const
106  {
107  std::cout << "my own blacklisted indices:\n";
108  auto idxIt = nativeBlackListedIndices_.begin();
109  const auto& idxEndIt = nativeBlackListedIndices_.end();
110  for (; idxIt != idxEndIt; ++idxIt)
111  std::cout << " (native index: " << *idxIt
112  << ", domestic index: " << nativeToDomestic(*idxIt) << ")\n";
113  std::cout << "blacklisted indices of the peers in my own domain:\n";
114  auto peerListIt = peerBlackLists_.begin();
115  const auto& peerListEndIt = peerBlackLists_.end();
116  for (; peerListIt != peerListEndIt; ++peerListIt) {
117  ProcessRank peerRank = peerListIt->first;
118  std::cout << " peer " << peerRank << ":\n";
119  auto idx2It = peerListIt->second.begin();
120  const auto& idx2EndIt = peerListIt->second.end();
121  for (; idx2It != idx2EndIt; ++ idx2It)
122  std::cout << " (native index: " << idx2It->myOwnNativeIndex
123  << ", native peer index: " << idx2It->nativeIndexOfPeer << ")\n";
124  }
125  }
126 
127 private:
128 #if HAVE_MPI
129  template <class DomesticOverlap>
130  void sendGlobalIndices_(ProcessRank peerRank,
131  const PeerBlackList& peerIndices,
132  const DomesticOverlap& domesticOverlap)
133  {
134  auto& numIdxBuff = numGlobalIdxSendBuff_[peerRank];
135  auto& idxBuff = globalIdxSendBuff_[peerRank];
136 
137  numIdxBuff.resize(1);
138  numIdxBuff[0] = static_cast<unsigned>(peerIndices.size());
139  numIdxBuff.send(peerRank);
140 
141  idxBuff.resize(2*peerIndices.size());
142  for (size_t i = 0; i < peerIndices.size(); ++i) {
143  // global index
144  Index myNativeIdx = peerIndices[i].myOwnNativeIndex;
145  Index myDomesticIdx = domesticOverlap.nativeToDomestic(myNativeIdx);
146  idxBuff[2*i + 0] = domesticOverlap.domesticToGlobal(myDomesticIdx);
147 
148  // native peer index
149  idxBuff[2*i + 1] = peerIndices[i].nativeIndexOfPeer;
150  }
151  idxBuff.send(peerRank);
152  }
153 
154  template <class DomesticOverlap>
155  void receiveGlobalIndices_(ProcessRank peerRank,
156  const DomesticOverlap& domesticOverlap)
157  {
158  MpiBuffer<unsigned> numGlobalIdxBuf(1);
159  numGlobalIdxBuf.receive(peerRank);
160  unsigned numIndices = numGlobalIdxBuf[0];
161 
162  MpiBuffer<Index> globalIdxBuf(2*numIndices);
163  globalIdxBuf.receive(peerRank);
164  for (unsigned i = 0; i < numIndices; ++i) {
165  Index globalIdx = globalIdxBuf[2*i + 0];
166  Index nativeIdx = globalIdxBuf[2*i + 1];
167 
168  nativeToDomesticMap_[nativeIdx] = domesticOverlap.globalToDomestic(globalIdx);
169  }
170  }
171 #endif // HAVE_MPI
172 
173  std::set<Index> nativeBlackListedIndices_;
174  std::map<Index, Index> nativeToDomesticMap_;
175 #if HAVE_MPI
176  std::map<ProcessRank, MpiBuffer<unsigned>> numGlobalIdxSendBuff_;
177  std::map<ProcessRank, MpiBuffer<Index>> globalIdxSendBuff_;
178 #endif // HAVE_MPI
179 
180  PeerBlackLists peerBlackLists_;
181 };
182 
183 } // namespace Linear
184 } // namespace Opm
185 
186 #endif
Simplifies handling of buffers to be used in conjunction with MPI.
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
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
Expresses which degrees of freedom are blacklisted for the parallel linear solvers and which domestic...
Definition: blacklist.hh:48
int Index
The type of an index of a degree of freedom.
Definition: overlaptypes.hh:44