| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2024 - 2025 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_tpetra_trilinos_block_sparse_matrix_templates_h |
| 16 |
#define dealii_tpetra_trilinos_block_sparse_matrix_templates_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#ifdef DEAL_II_TRILINOS_WITH_TPETRA |
| 21 |
|
| 22 |
# include <deal.II/lac/trilinos_tpetra_block_sparse_matrix.h> |
| 23 |
|
| 24 |
DEAL_II_NAMESPACE_OPEN |
| 25 |
|
| 26 |
namespace LinearAlgebra |
| 27 |
{ |
| 28 |
|
| 29 |
namespace TpetraWrappers |
| 30 |
{ |
| 31 |
|
| 32 |
template <typename Number, typename MemorySpace> |
| 33 |
BlockSparseMatrix<Number, MemorySpace>::~BlockSparseMatrix() |
| 34 |
{ |
| 35 |
// delete previous content of |
| 36 |
// the subobjects array |
| 37 |
try |
| 38 |
{ |
| 39 |
clear(); |
| 40 |
} |
| 41 |
catch (...) |
| 42 |
{} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
template <typename Number, typename MemorySpace> |
| 48 |
BlockSparseMatrix<Number, MemorySpace> & |
| 49 |
BlockSparseMatrix<Number, MemorySpace>::operator=(const Number d) |
| 50 |
{ |
| 51 |
Assert(d == 0, ExcScalarAssignmentOnlyForZeroValue()); |
| 52 |
|
| 53 |
for (size_type r = 0; r < this->n_block_rows(); ++r) |
| 54 |
for (size_type c = 0; c < this->n_block_cols(); ++c) |
| 55 |
this->block(r, c) = d; |
| 56 |
|
| 57 |
return *this; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
template <typename Number, typename MemorySpace> |
| 63 |
void |
| 64 |
BlockSparseMatrix<Number, MemorySpace>::reinit( |
| 65 |
const size_type n_block_rows, |
| 66 |
const size_type n_block_columns) |
| 67 |
{ |
| 68 |
// first delete previous content of |
| 69 |
// the subobjects array |
| 70 |
clear(); |
| 71 |
|
| 72 |
// then resize. set sizes of blocks to |
| 73 |
// zero. user will later have to call |
| 74 |
// collect_sizes for this |
| 75 |
this->sub_objects.reinit(n_block_rows, n_block_columns); |
| 76 |
this->row_block_indices.reinit(n_block_rows, 0); |
| 77 |
this->column_block_indices.reinit(n_block_columns, 0); |
| 78 |
|
| 79 |
// and reinitialize the blocks |
| 80 |
for (size_type r = 0; r < this->n_block_rows(); ++r) |
| 81 |
for (size_type c = 0; c < this->n_block_cols(); ++c) |
| 82 |
{ |
| 83 |
BlockType *p = new BlockType(); |
| 84 |
|
| 85 |
Assert(this->sub_objects[r][c] == nullptr, ExcInternalError()); |
| 86 |
this->sub_objects[r][c] = p; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
template <typename Number, typename MemorySpace> |
| 93 |
template <typename BlockSparsityPatternType> |
| 94 |
void |
| 95 |
BlockSparseMatrix<Number, MemorySpace>::reinit( |
| 96 |
const std::vector<IndexSet> ¶llel_partitioning, |
| 97 |
const BlockSparsityPatternType &block_sparsity_pattern, |
| 98 |
const MPI_Comm communicator, |
| 99 |
const bool exchange_data) |
| 100 |
{ |
| 101 |
if constexpr (running_in_debug_mode()) |
| 102 |
{ |
| 103 |
std::vector<typename TpetraTypes::MapType<MemorySpace>> tpetra_maps; |
| 104 |
for (size_type i = 0; i < block_sparsity_pattern.n_block_rows(); ++i) |
| 105 |
tpetra_maps.push_back( |
| 106 |
parallel_partitioning[i] |
| 107 |
.template make_tpetra_map< |
| 108 |
typename TpetraTypes::NodeType<MemorySpace>>(communicator, |
| 109 |
false)); |
| 110 |
|
| 111 |
Assert(tpetra_maps.size() == block_sparsity_pattern.n_block_rows(), |
| 112 |
ExcDimensionMismatch(tpetra_maps.size(), |
| 113 |
block_sparsity_pattern.n_block_rows())); |
| 114 |
Assert(tpetra_maps.size() == block_sparsity_pattern.n_block_cols(), |
| 115 |
ExcDimensionMismatch(tpetra_maps.size(), |
| 116 |
block_sparsity_pattern.n_block_cols())); |
| 117 |
|
| 118 |
const size_type n_block_rows = tpetra_maps.size(); |
| 119 |
Assert(n_block_rows == block_sparsity_pattern.n_block_rows(), |
| 120 |
ExcDimensionMismatch(n_block_rows, |
| 121 |
block_sparsity_pattern.n_block_rows())); |
| 122 |
Assert(n_block_rows == block_sparsity_pattern.n_block_cols(), |
| 123 |
ExcDimensionMismatch(n_block_rows, |
| 124 |
block_sparsity_pattern.n_block_cols())); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
// Call the other basic reinit function, ... |
| 129 |
reinit(block_sparsity_pattern.n_block_rows(), |
| 130 |
block_sparsity_pattern.n_block_cols()); |
| 131 |
|
| 132 |
// ... set the correct sizes, ... |
| 133 |
this->row_block_indices = block_sparsity_pattern.get_row_indices(); |
| 134 |
this->column_block_indices = block_sparsity_pattern.get_column_indices(); |
| 135 |
|
| 136 |
// ... and then assign the correct |
| 137 |
// data to the blocks. |
| 138 |
for (size_type r = 0; r < this->n_block_rows(); ++r) |
| 139 |
for (size_type c = 0; c < this->n_block_cols(); ++c) |
| 140 |
{ |
| 141 |
this->sub_objects[r][c]->reinit(parallel_partitioning[r], |
| 142 |
parallel_partitioning[c], |
| 143 |
block_sparsity_pattern.block(r, c), |
| 144 |
communicator, |
| 145 |
exchange_data); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
template <typename Number, typename MemorySpace> |
| 152 |
template <typename BlockSparsityPatternType> |
| 153 |
void |
| 154 |
BlockSparseMatrix<Number, MemorySpace>::reinit( |
| 155 |
const BlockSparsityPatternType &block_sparsity_pattern) |
| 156 |
{ |
| 157 |
std::vector<IndexSet> parallel_partitioning; |
| 158 |
for (size_type i = 0; i < block_sparsity_pattern.n_block_rows(); ++i) |
| 159 |
parallel_partitioning.emplace_back( |
| 160 |
complete_index_set(block_sparsity_pattern.block(i, 0).n_rows())); |
| 161 |
|
| 162 |
reinit(parallel_partitioning, block_sparsity_pattern); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
template <typename Number, typename MemorySpace> |
| 168 |
void |
| 169 |
BlockSparseMatrix<Number, MemorySpace>::reinit( |
| 170 |
const std::vector<IndexSet> ¶llel_partitioning, |
| 171 |
const ::dealii::BlockSparseMatrix<double> &dealii_block_sparse_matrix, |
| 172 |
const MPI_Comm communicator, |
| 173 |
const double drop_tolerance) |
| 174 |
{ |
| 175 |
const size_type n_block_rows = parallel_partitioning.size(); |
| 176 |
|
| 177 |
Assert(n_block_rows == dealii_block_sparse_matrix.n_block_rows(), |
| 178 |
ExcDimensionMismatch(n_block_rows, |
| 179 |
dealii_block_sparse_matrix.n_block_rows())); |
| 180 |
Assert(n_block_rows == dealii_block_sparse_matrix.n_block_cols(), |
| 181 |
ExcDimensionMismatch(n_block_rows, |
| 182 |
dealii_block_sparse_matrix.n_block_cols())); |
| 183 |
|
| 184 |
// Call the other basic reinit function ... |
| 185 |
reinit(n_block_rows, n_block_rows); |
| 186 |
|
| 187 |
// ... and then assign the correct |
| 188 |
// data to the blocks. |
| 189 |
for (size_type r = 0; r < this->n_block_rows(); ++r) |
| 190 |
for (size_type c = 0; c < this->n_block_cols(); ++c) |
| 191 |
{ |
| 192 |
this->sub_objects[r][c]->reinit(parallel_partitioning[r], |
| 193 |
parallel_partitioning[c], |
| 194 |
dealii_block_sparse_matrix.block(r, |
| 195 |
c), |
| 196 |
communicator, |
| 197 |
drop_tolerance); |
| 198 |
} |
| 199 |
|
| 200 |
collect_sizes(); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
template <typename Number, typename MemorySpace> |
| 206 |
void |
| 207 |
BlockSparseMatrix<Number, MemorySpace>::reinit( |
| 208 |
const ::dealii::BlockSparseMatrix<double> &dealii_block_sparse_matrix, |
| 209 |
const double drop_tolerance) |
| 210 |
{ |
| 211 |
Assert(dealii_block_sparse_matrix.n_block_rows() == |
| 212 |
dealii_block_sparse_matrix.n_block_cols(), |
| 213 |
ExcDimensionMismatch(dealii_block_sparse_matrix.n_block_rows(), |
| 214 |
dealii_block_sparse_matrix.n_block_cols())); |
| 215 |
Assert(dealii_block_sparse_matrix.m() == dealii_block_sparse_matrix.n(), |
| 216 |
ExcDimensionMismatch(dealii_block_sparse_matrix.m(), |
| 217 |
dealii_block_sparse_matrix.n())); |
| 218 |
|
| 219 |
std::vector<IndexSet> parallel_partitioning; |
| 220 |
for (size_type i = 0; i < dealii_block_sparse_matrix.n_block_rows(); ++i) |
| 221 |
parallel_partitioning.emplace_back( |
| 222 |
complete_index_set(dealii_block_sparse_matrix.block(i, 0).m())); |
| 223 |
|
| 224 |
reinit(parallel_partitioning, |
| 225 |
dealii_block_sparse_matrix, |
| 226 |
MPI_COMM_SELF, |
| 227 |
drop_tolerance); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
template <typename Number, typename MemorySpace> |
| 233 |
inline bool |
| 234 |
BlockSparseMatrix<Number, MemorySpace>::is_compressed() const |
| 235 |
{ |
| 236 |
bool compressed = true; |
| 237 |
for (size_type row = 0; row < this->n_block_rows(); ++row) |
| 238 |
for (size_type col = 0; col < this->n_block_cols(); ++col) |
| 239 |
if (this->block(row, col).is_compressed() == false) |
| 240 |
{ |
| 241 |
compressed = false; |
| 242 |
break; |
| 243 |
} |
| 244 |
|
| 245 |
return compressed; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
template <typename Number, typename MemorySpace> |
| 251 |
void |
| 252 |
BlockSparseMatrix<Number, MemorySpace>::collect_sizes() |
| 253 |
{ |
| 254 |
// simply forward to the (non-public) function of the base class |
| 255 |
BaseClass::collect_sizes(); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
template <typename Number, typename MemorySpace> |
| 261 |
std::uint64_t |
| 262 |
BlockSparseMatrix<Number, MemorySpace>::n_nonzero_elements() const |
| 263 |
{ |
| 264 |
std::uint64_t n_nonzero = 0; |
| 265 |
for (size_type rows = 0; rows < this->n_block_rows(); ++rows) |
| 266 |
for (size_type cols = 0; cols < this->n_block_cols(); ++cols) |
| 267 |
n_nonzero += this->block(rows, cols).n_nonzero_elements(); |
| 268 |
|
| 269 |
return n_nonzero; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
template <typename Number, typename MemorySpace> |
| 275 |
MPI_Comm |
| 276 |
BlockSparseMatrix<Number, MemorySpace>::get_mpi_communicator() const |
| 277 |
{ |
| 278 |
Assert(this->n_block_cols() != 0, ExcNotInitialized()); |
| 279 |
Assert(this->n_block_rows() != 0, ExcNotInitialized()); |
| 280 |
return this->sub_objects[0][0]->get_mpi_communicator(); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
template <typename Number, typename MemorySpace> |
| 286 |
inline std::vector<IndexSet> |
| 287 |
BlockSparseMatrix<Number, MemorySpace>::locally_owned_domain_indices() const |
| 288 |
{ |
| 289 |
Assert(this->n_block_cols() != 0, ExcNotInitialized()); |
| 290 |
Assert(this->n_block_rows() != 0, ExcNotInitialized()); |
| 291 |
|
| 292 |
std::vector<IndexSet> domain_indices; |
| 293 |
for (size_type c = 0; c < this->n_block_cols(); ++c) |
| 294 |
domain_indices.push_back( |
| 295 |
this->sub_objects[0][c]->locally_owned_domain_indices()); |
| 296 |
|
| 297 |
return domain_indices; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
template <typename Number, typename MemorySpace> |
| 303 |
inline std::vector<IndexSet> |
| 304 |
BlockSparseMatrix<Number, MemorySpace>::locally_owned_range_indices() const |
| 305 |
{ |
| 306 |
Assert(this->n_block_cols() != 0, ExcNotInitialized()); |
| 307 |
Assert(this->n_block_rows() != 0, ExcNotInitialized()); |
| 308 |
|
| 309 |
std::vector<IndexSet> range_indices; |
| 310 |
for (size_type r = 0; r < this->n_block_rows(); ++r) |
| 311 |
range_indices.push_back( |
| 312 |
this->sub_objects[r][0]->locally_owned_range_indices()); |
| 313 |
|
| 314 |
return range_indices; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
template <typename Number, typename MemorySpace> |
| 320 |
template <typename VectorType1, typename VectorType2, typename VectorType3> |
| 321 |
Number |
| 322 |
BlockSparseMatrix<Number, MemorySpace>::residual(VectorType1 &dst, |
| 323 |
const VectorType2 &x, |
| 324 |
const VectorType3 &b) const |
| 325 |
{ |
| 326 |
vmult(dst, x); |
| 327 |
dst -= b; |
| 328 |
dst *= -1.; |
| 329 |
|
| 330 |
return dst.l2_norm(); |
| 331 |
} |
| 332 |
|
| 333 |
} // namespace TpetraWrappers |
| 334 |
|
| 335 |
} // namespace LinearAlgebra |
| 336 |
|
| 337 |
DEAL_II_NAMESPACE_CLOSE |
| 338 |
|
| 339 |
#else |
| 340 |
|
| 341 |
// Make sure the scripts that create the C++20 module input files have |
| 342 |
// something to latch on if the preprocessor #ifdef above would |
| 343 |
// otherwise lead to an empty content of the file. |
| 344 |
DEAL_II_NAMESPACE_OPEN |
| 345 |
DEAL_II_NAMESPACE_CLOSE |
| 346 |
|
| 347 |
#endif // DEAL_II_TRILINOS_WITH_TPETRA |
| 348 |
|
| 349 |
#endif // dealii_tpetra_trilinos_block_sparse_matrix_templates_h |