Skip to content

Commit

Permalink
Simplify reinit() of PETSc/Trilinos BlockVector.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcfehling committed Nov 26, 2022
1 parent 8c44f0f commit 931b472
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 109 deletions.
68 changes: 38 additions & 30 deletions include/deal.II/lac/petsc_block_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,13 @@ namespace PETScWrappers
inline BlockVector::BlockVector(const BlockVector &v)
: BlockVectorBase<Vector>()
{
this->components.resize(v.n_blocks());
this->block_indices = v.block_indices;

this->components.resize(this->n_blocks());
for (unsigned int i = 0; i < this->n_blocks(); ++i)
this->components[i] = v.components[i];

this->collect_sizes();
}

inline BlockVector::BlockVector(
Expand Down Expand Up @@ -346,16 +348,17 @@ namespace PETScWrappers
{
// we only allow assignment to vectors with the same number of blocks
// or to an empty BlockVector
Assert(n_blocks() == 0 || n_blocks() == v.n_blocks(),
ExcDimensionMismatch(n_blocks(), v.n_blocks()));
Assert(this->n_blocks() == 0 || this->n_blocks() == v.n_blocks(),
ExcDimensionMismatch(this->n_blocks(), v.n_blocks()));

if (this->n_blocks() != v.n_blocks())
reinit(v.n_blocks());
this->block_indices = v.block_indices;

for (size_type i = 0; i < this->n_blocks(); ++i)
this->components[i] = v.block(i);
this->components.resize(this->n_blocks());
for (unsigned int i = 0; i < this->n_blocks(); ++i)
this->components[i] = v.components[i];

collect_sizes();
this->collect_sizes();

return *this;
}
Expand Down Expand Up @@ -384,61 +387,66 @@ namespace PETScWrappers
const bool omit_zeroing_entries)
{
this->block_indices.reinit(block_sizes);
if (this->components.size() != this->n_blocks())
this->components.resize(this->n_blocks());

this->components.resize(this->n_blocks());
for (unsigned int i = 0; i < this->n_blocks(); ++i)
this->components[i].reinit(communicator,
block_sizes[i],
locally_owned_sizes[i],
omit_zeroing_entries);

this->collect_sizes();
}


inline void
BlockVector::reinit(const BlockVector &v, const bool omit_zeroing_entries)
{
this->block_indices = v.get_block_indices();
if (this->components.size() != this->n_blocks())
this->components.resize(this->n_blocks());
if (this->n_blocks() != v.n_blocks())
this->block_indices = v.get_block_indices();

this->components.resize(this->n_blocks());
for (unsigned int i = 0; i < this->n_blocks(); ++i)
block(i).reinit(v.block(i), omit_zeroing_entries);
this->components[i].reinit(v.components[i], omit_zeroing_entries);

this->collect_sizes();
}

inline void
BlockVector::reinit(const std::vector<IndexSet> &parallel_partitioning,
const MPI_Comm & communicator)
{
std::vector<size_type> sizes(parallel_partitioning.size());
for (unsigned int i = 0; i < parallel_partitioning.size(); ++i)
sizes[i] = parallel_partitioning[i].size();

this->block_indices.reinit(sizes);
if (this->components.size() != this->n_blocks())
this->components.resize(this->n_blocks());
// update the number of blocks
this->block_indices.reinit(parallel_partitioning.size(), 0);

// initialize each block
this->components.resize(this->n_blocks());
for (unsigned int i = 0; i < this->n_blocks(); ++i)
block(i).reinit(parallel_partitioning[i], communicator);
this->components[i].reinit(parallel_partitioning[i], communicator);

// update block_indices content
this->collect_sizes();
}

inline void
BlockVector::reinit(const std::vector<IndexSet> &parallel_partitioning,
const std::vector<IndexSet> &ghost_entries,
const MPI_Comm & communicator)
{
std::vector<types::global_dof_index> sizes(parallel_partitioning.size());
for (unsigned int i = 0; i < parallel_partitioning.size(); ++i)
sizes[i] = parallel_partitioning[i].size();
AssertDimension(parallel_partitioning.size(), ghost_entries.size());

this->block_indices.reinit(sizes);
if (this->components.size() != this->n_blocks())
this->components.resize(this->n_blocks());
// update the number of blocks
this->block_indices.reinit(parallel_partitioning.size(), 0);

// initialize each block
this->components.resize(this->n_blocks());
for (unsigned int i = 0; i < this->n_blocks(); ++i)
block(i).reinit(parallel_partitioning[i],
ghost_entries[i],
communicator);
this->components[i].reinit(parallel_partitioning[i],
ghost_entries[i],
communicator);

// update block_indices content
this->collect_sizes();
}


Expand Down
27 changes: 15 additions & 12 deletions include/deal.II/lac/trilinos_parallel_block_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,13 @@ namespace TrilinosWrappers
inline BlockVector::BlockVector(const BlockVector &v)
: dealii::BlockVectorBase<MPI::Vector>()
{
this->components.resize(v.n_blocks());
this->block_indices = v.block_indices;

for (size_type i = 0; i < this->n_blocks(); ++i)
this->components.resize(this->n_blocks());
for (unsigned int i = 0; i < this->n_blocks(); ++i)
this->components[i] = v.components[i];

this->collect_sizes();
}


Expand All @@ -370,18 +372,19 @@ namespace TrilinosWrappers
BlockVector &
BlockVector::operator=(const ::dealii::BlockVector<Number> &v)
{
if (n_blocks() != v.n_blocks())
{
std::vector<size_type> block_sizes(v.n_blocks(), 0);
block_indices.reinit(block_sizes);
if (components.size() != n_blocks())
components.resize(n_blocks());
}

for (size_type i = 0; i < this->n_blocks(); ++i)
// we only allow assignment to vectors with the same number of blocks
// or to an empty BlockVector
Assert(this->n_blocks() == 0 || this->n_blocks() == v.n_blocks(),
ExcDimensionMismatch(this->n_blocks(), v.n_blocks()));

if (this->n_blocks() != v.n_blocks())
this->block_indices = v.get_block_indices();

this->components.resize(this->n_blocks());
for (unsigned int i = 0; i < this->n_blocks(); ++i)
this->components[i] = v.block(i);

collect_sizes();
this->collect_sizes();

return *this;
}
Expand Down
120 changes: 53 additions & 67 deletions source/lac/trilinos_block_vector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ namespace TrilinosWrappers
{
// we only allow assignment to vectors with the same number of blocks
// or to an empty BlockVector
Assert(n_blocks() == 0 || n_blocks() == v.n_blocks(),
ExcDimensionMismatch(n_blocks(), v.n_blocks()));
Assert(this->n_blocks() == 0 || this->n_blocks() == v.n_blocks(),
ExcDimensionMismatch(this->n_blocks(), v.n_blocks()));

if (this->n_blocks() != v.n_blocks())
reinit(v.n_blocks());
this->block_indices = v.block_indices;

for (size_type i = 0; i < this->n_blocks(); ++i)
this->components[i] = v.block(i);
this->components.resize(this->n_blocks());
for (unsigned int i = 0; i < this->n_blocks(); ++i)
this->components[i] = v.components[i];

collect_sizes();
this->collect_sizes();

return *this;
}
Expand All @@ -71,24 +72,18 @@ namespace TrilinosWrappers
const MPI_Comm & communicator,
const bool omit_zeroing_entries)
{
const size_type no_blocks = parallel_partitioning.size();
std::vector<size_type> block_sizes(no_blocks);

for (size_type i = 0; i < no_blocks; ++i)
{
block_sizes[i] = parallel_partitioning[i].size();
}

this->block_indices.reinit(block_sizes);
if (components.size() != n_blocks())
components.resize(n_blocks());

for (size_type i = 0; i < n_blocks(); ++i)
components[i].reinit(parallel_partitioning[i],
communicator,
omit_zeroing_entries);

collect_sizes();
// update the number of blocks
this->block_indices.reinit(parallel_partitioning.size(), 0);

// initialize each block
this->components.resize(this->n_blocks());
for (unsigned int i = 0; i < this->n_blocks(); ++i)
this->components[i].reinit(parallel_partitioning[i],
communicator,
omit_zeroing_entries);

// update block_indices content
this->collect_sizes();
}


Expand All @@ -99,56 +94,50 @@ namespace TrilinosWrappers
const MPI_Comm & communicator,
const bool vector_writable)
{
const size_type no_blocks = parallel_partitioning.size();
std::vector<size_type> block_sizes(no_blocks);

for (size_type i = 0; i < no_blocks; ++i)
{
block_sizes[i] = parallel_partitioning[i].size();
}
AssertDimension(parallel_partitioning.size(), ghost_values.size());

this->block_indices.reinit(block_sizes);
if (components.size() != n_blocks())
components.resize(n_blocks());
// update the number of blocks
this->block_indices.reinit(parallel_partitioning.size(), 0);

for (size_type i = 0; i < n_blocks(); ++i)
components[i].reinit(parallel_partitioning[i],
ghost_values[i],
communicator,
vector_writable);
// initialize each block
this->components.resize(this->n_blocks());
for (unsigned int i = 0; i < this->n_blocks(); ++i)
this->components[i].reinit(parallel_partitioning[i],
ghost_values[i],
communicator,
vector_writable);

collect_sizes();
// update block_indices content
this->collect_sizes();
}



void
BlockVector::reinit(const BlockVector &v, const bool omit_zeroing_entries)
{
block_indices = v.get_block_indices();
if (components.size() != n_blocks())
components.resize(n_blocks());
if (this->n_blocks() != v.n_blocks())
this->block_indices = v.get_block_indices();

for (size_type i = 0; i < n_blocks(); ++i)
components[i].reinit(v.block(i), omit_zeroing_entries, false);
this->components.resize(this->n_blocks());
for (unsigned int i = 0; i < this->n_blocks(); ++i)
this->components[i].reinit(v.components[i],
omit_zeroing_entries,
false);

collect_sizes();
this->collect_sizes();
}



void
BlockVector::reinit(const size_type num_blocks)
{
std::vector<size_type> block_sizes(num_blocks, 0);
this->block_indices.reinit(block_sizes);
if (this->components.size() != this->n_blocks())
this->components.resize(this->n_blocks());

for (size_type i = 0; i < this->n_blocks(); ++i)
components[i].clear();
this->block_indices.reinit(num_blocks, 0);

collect_sizes();
this->components.resize(this->n_blocks());
for (auto &c : this->components)
c.clear();
}


Expand All @@ -158,21 +147,18 @@ namespace TrilinosWrappers
const TrilinosWrappers::BlockSparseMatrix &m,
const BlockVector & v)
{
Assert(m.n_block_rows() == v.n_blocks(),
ExcDimensionMismatch(m.n_block_rows(), v.n_blocks()));
Assert(m.n_block_cols() == v.n_blocks(),
ExcDimensionMismatch(m.n_block_cols(), v.n_blocks()));
AssertDimension(m.n_block_rows(), v.n_blocks());
AssertDimension(m.n_block_cols(), v.n_blocks());

if (v.n_blocks() != n_blocks())
{
block_indices = v.get_block_indices();
components.resize(v.n_blocks());
}
if (this->n_blocks() != v.n_blocks())
this->block_indices = v.get_block_indices();

for (size_type i = 0; i < this->n_blocks(); ++i)
components[i].import_nonlocal_data_for_fe(m.block(i, i), v.block(i));
this->components.resize(this->n_blocks());
for (unsigned int i = 0; i < this->n_blocks(); ++i)
this->components[i].import_nonlocal_data_for_fe(m.block(i, i),
v.block(i));

collect_sizes();
this->collect_sizes();
}


Expand All @@ -183,7 +169,7 @@ namespace TrilinosWrappers
const bool scientific,
const bool across) const
{
for (size_type i = 0; i < this->n_blocks(); ++i)
for (unsigned int i = 0; i < this->n_blocks(); ++i)
{
if (across)
out << 'C' << i << ':';
Expand Down

0 comments on commit 931b472

Please sign in to comment.