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