11#include <dune/common/typeutilities.hh>
12#include <dune/common/parallel/communication.hh>
15#include <dune/common/parallel/indexset.hh>
16#include <dune/common/parallel/localindex.hh>
17#include <dune/common/parallel/plocalindex.hh>
18#include <dune/common/parallel/remoteindices.hh>
19#include <dune/common/parallel/remoteindices.hh>
21#include <amdis/common/parallel/Communicator.hpp>
22#include <amdis/linearalgebra/ParallelIndexSet.hpp>
25#include <amdis/functions/GlobalIdSet.hpp>
26#include <amdis/linearalgebra/AttributeSet.hpp>
38 using DofIndex = size_type;
39 using LocalIndex = size_type;
40 using GlobalIndex = PetscInt;
43 template <
class Basis,
44 Dune::disableCopyMove<Self, Basis> = 0>
52 return PETSC_COMM_SELF;
100 assert(
false &&
"There are no ghost indices in sequential dofmappings");
107 assert(
false &&
"There are no ghost indices in sequential dofmappings");
112 GlobalIndex
global(LocalIndex
const& n)
const
152 template <
class Basis>
155 localSize_ = basis.dimension();
156 globalSize_ = basis.dimension();
157 indices_.resize(globalSize_);
158 std::iota(indices_.begin(), indices_.end(), size_type(0));
161 void debug()
const {}
164 size_type localSize_ = 0;
165 size_type globalSize_ = 0;
166 std::vector<GlobalIndex> indices_;
170 template <
class GlobalId,
class LI,
class Comm>
178 template <
class GlobalId,
class LI>
179 class PetscParallelIndexDistribution
183 using Self = PetscParallelIndexDistribution;
184 using PLocalIndex = Dune::ParallelLocalIndex<DefaultAttributeSet::Type>;
185 using IndexSet = Dune::ParallelIndexSet<GlobalId, PLocalIndex, 512>;
186 using Attribute =
typename AttributeSet<IndexSet>::type;
187 using RemoteIndices = Dune::RemoteIndices<IndexSet>;
190 using size_type = LI;
191 using DofIndex = size_type;
192 using LocalIndex = size_type;
193 using GlobalIndex = PetscInt;
196 template <
class Basis,
197 Dune::disableCopyMove<Self, Basis> = 0>
198 PetscParallelIndexDistribution(Basis
const& basis)
199 : world_(basis.gridView().comm())
204 MPI_Comm comm()
const
210 size_type localSize()
const
216 std::vector<size_type>
const& localSizes()
const
222 size_type globalSize()
const
228 std::vector<GlobalIndex>
const& globalStarts()
const
234 std::vector<GlobalIndex>
const& globalIndices()
const
236 return globalIndices_;
240 size_type ghostSize()
const
246 std::vector<GlobalIndex>
const& ghostIndices()
const
248 return ghostIndices_;
252 LocalIndex globalToGhost(GlobalIndex
const& n)
const
254 auto it = std::find(ghostIndices_.begin(), ghostIndices_.end(), n);
255 assert(it != ghostIndices_.end());
256 return std::distance(ghostIndices_.begin(), it);
260 LocalIndex dofToGhost(DofIndex
const& n)
const
263 assert(n < ghostLocalIndices_.size());
264 assert(ghostLocalIndices_[n] < ghostSize_);
266 return ghostLocalIndices_[n];
270 GlobalIndex global(DofIndex
const& n)
const
272 assert(n < globalIndices_.size());
273 return globalIndices_[n];
278 LocalIndex globalToLocal(GlobalIndex
const& n)
const
280 return n - starts_[world_.rank()];
284 LocalIndex dofToLocal(DofIndex
const& n)
const
286 assert(n < globalIndices_.size());
287 return globalToLocal(globalIndices_[n]);
291 bool owner(DofIndex
const& n)
const
293 assert(n < owner_.size());
298 bool globalOwner(GlobalIndex
const& n)
const
300 return globalOwner(world_.rank(), n);
304 bool globalOwner(
int p, GlobalIndex
const& n)
const
306 assert(p < world_.size());
307 return n >= starts_[p] && n < starts_[p+1];
310 RemoteIndices
const& remoteIndices()
const
312 return *remoteIndices_;
316 template <
class Basis>
317 void update(Basis
const& basis);
330 globalIndices_.clear();
331 ghostIndices_.clear();
332 ghostLocalIndices_.clear();
334 indexSet_ = std::make_unique<IndexSet>();
335 remoteIndices_ = std::make_unique<RemoteIndices>();
339 Mpi::Communicator world_;
341 std::vector<size_type> sizes_;
342 std::vector<GlobalIndex> starts_;
343 size_type localSize_;
344 size_type globalSize_;
345 size_type ghostSize_;
347 std::vector<GlobalIndex> globalIndices_;
348 std::vector<GlobalIndex> ghostIndices_;
349 std::vector<LocalIndex> ghostLocalIndices_;
350 std::vector<bool> owner_;
352 std::unique_ptr<IndexSet> indexSet_ =
nullptr;
353 std::unique_ptr<RemoteIndices> remoteIndices_ =
nullptr;
355 const Mpi::Tag tag_{7513};
358 template <
class GlobalId,
class LI>
359 struct PetscIndexDistributionType<GlobalId, LI, Dune::Communication<MPI_Comm>>
361 using type = PetscParallelIndexDistribution<GlobalId, LI>;
366 using PetscIndexDistribution_t
367 =
typename PetscIndexDistributionType<GlobalIdType_t<B>,
typename B::size_type,
typename B::GridView::CollectiveCommunication>::type;
371#include <amdis/linearalgebra/petsc/IndexDistribution.inc.hpp>
Sparsity pattern used to create PETSc matrices.
Definition: MatrixNnzStructure.hpp:18
Fallback for PetscParallelIndexDistribution in case there is only one mpi core.
Definition: IndexDistribution.hpp:33
size_type globalSize() const
The total number of global DOFs.
Definition: IndexDistribution.hpp:68
void update(Basis const &basis)
Update the local to global mapping. Must be called before mapping local to global.
Definition: IndexDistribution.hpp:153
bool owner(DofIndex const &n) const
DOF index n is owned by this processor.
Definition: IndexDistribution.hpp:131
size_type localSize() const
How many DOFs are owned by my processor?
Definition: IndexDistribution.hpp:56
LocalIndex dofToLocal(DofIndex const &n) const
Map DOF index to consecutive local owner index.
Definition: IndexDistribution.hpp:124
bool globalOwner(GlobalIndex const &n) const
Global index n is owned by this processor.
Definition: IndexDistribution.hpp:138
std::array< size_type, 1 > localSizes() const
Return the sequence of number of local indices for all processors.
Definition: IndexDistribution.hpp:62
LocalIndex globalToLocal(GlobalIndex const &n) const
Map global index to consecutive local owner index.
Definition: IndexDistribution.hpp:118
GlobalIndex ghostSize() const
Return number of ghost indices.
Definition: IndexDistribution.hpp:86
bool globalOwner(int p, GlobalIndex const &n) const
Global index n is owned by processor p.
Definition: IndexDistribution.hpp:144
GlobalIndex global(LocalIndex const &n) const
Global index of local index n.
Definition: IndexDistribution.hpp:112
LocalIndex globalToGhost(GlobalIndex const &n) const
Map global index to local ghost index.
Definition: IndexDistribution.hpp:98
std::vector< GlobalIndex > const & globalIndices() const
Return the vector of global indices.
Definition: IndexDistribution.hpp:80
LocalIndex dofToGhost(DofIndex const &n) const
Map DOF index to local ghost index.
Definition: IndexDistribution.hpp:105
std::array< GlobalIndex, 0 > ghostIndices() const
Return the vector of ghost indices.
Definition: IndexDistribution.hpp:92
std::array< GlobalIndex, 1 > globalStarts() const
Return the sequence of starting points of the global indices for all processors.
Definition: IndexDistribution.hpp:74
By default the PetscParallelIndexDistribution is just a sequential distribution.
Definition: IndexDistribution.hpp:172