15#ifndef dealii_petsc_matrix_base_h
16#define dealii_petsc_matrix_base_h
21#ifdef DEAL_II_WITH_PETSC
41template <
typename Matrix>
51 namespace MatrixIterators
123 <<
"You tried to access row " << arg1
124 <<
" of a distributed matrix, but only rows " << arg2
125 <<
" through " << arg3
126 <<
" are stored locally and can be accessed.");
239 <<
"Attempt to access element " << arg2 <<
" of row "
240 << arg1 <<
" which doesn't have that many elements.");
406 set(
const std::vector<size_type> &indices,
408 const bool elide_zero_values =
false);
416 set(
const std::vector<size_type> &row_indices,
417 const std::vector<size_type> &col_indices,
419 const bool elide_zero_values =
false);
437 const std::vector<size_type> &col_indices,
438 const std::vector<PetscScalar> &values,
439 const bool elide_zero_values =
false);
459 const PetscScalar *values,
460 const bool elide_zero_values =
false);
494 add(
const std::vector<size_type> &indices,
496 const bool elide_zero_values =
true);
504 add(
const std::vector<size_type> &row_indices,
505 const std::vector<size_type> &col_indices,
507 const bool elide_zero_values =
true);
525 const std::vector<size_type> &col_indices,
526 const std::vector<PetscScalar> &values,
527 const bool elide_zero_values =
true);
547 const PetscScalar *values,
548 const bool elide_zero_values =
true,
549 const bool col_indices_are_sorted =
false);
580 const PetscScalar new_diag_value = 0);
587 const PetscScalar new_diag_value = 0);
670 std::pair<size_type, size_type>
695 std::pair<size_type, size_type>
930 operator Mat()
const;
968 write_ascii(
const PetscViewerFormat format = PETSC_VIEWER_DEFAULT);
978 print(std::ostream &out,
const bool alternative_output =
false)
const;
990 "You are attempting an operation on two vectors that "
991 "are the same object, but the operation requires that the "
992 "two objects are in fact different.");
1000 <<
"You tried to do a "
1001 << (arg1 == 1 ?
"'set'" : (arg1 == 2 ?
"'add'" :
"???"))
1002 <<
" operation but the matrix is currently in "
1003 << (arg2 == 1 ?
"'set'" : (arg2 == 2 ?
"'add'" :
"???"))
1004 <<
" mode. You first have to call 'compress()'.");
1121 friend class ::BlockMatrixBase;
1130 namespace MatrixIterators
1133 const size_type row,
1134 const size_type index)
1139 visit_present_row();
1155 Assert(a_row < matrix->m(), ExcBeyondEndOfMatrix());
1156 return (*colnum_cache)[a_index];
1163 Assert(a_row < matrix->m(), ExcBeyondEndOfMatrix());
1171 Assert(a_row < matrix->m(), ExcBeyondEndOfMatrix());
1172 return (*value_cache)[a_index];
1177 const size_type row,
1178 const size_type index)
1184 inline const_iterator &
1185 const_iterator::operator++()
1193 if (accessor.a_index >= accessor.colnum_cache->size())
1195 accessor.a_index = 0;
1198 while ((accessor.a_row < accessor.matrix->m()) &&
1199 (accessor.a_row < accessor.matrix->local_range().second) &&
1200 (accessor.matrix->row_length(accessor.a_row) == 0))
1203 accessor.visit_present_row();
1209 inline const_iterator
1210 const_iterator::operator++(
int)
1212 const const_iterator old_state = *
this;
1218 inline const const_iterator::Accessor &
1219 const_iterator::operator*()
const
1225 inline const const_iterator::Accessor *
1226 const_iterator::operator->()
const
1233 const_iterator::operator==(
const const_iterator &other)
const
1235 return (accessor.a_row == other.accessor.a_row &&
1236 accessor.a_index == other.accessor.a_index);
1241 const_iterator::operator!=(
const const_iterator &other)
const
1243 return !(*
this == other);
1248 const_iterator::operator<(
const const_iterator &other)
const
1250 return (accessor.row() < other.accessor.row() ||
1251 (accessor.row() == other.accessor.row() &&
1252 accessor.index() < other.accessor.index()));
1267 MatrixBase::set(
const size_type i,
const size_type j,
const PetscScalar value)
1271 set(i, 1, &j, &value,
false);
1277 MatrixBase::set(
const std::vector<size_type> &indices,
1279 const bool elide_zero_values)
1281 Assert(indices.size() == values.m(),
1285 for (size_type i = 0; i < indices.size(); ++i)
1296 MatrixBase::set(
const std::vector<size_type> &row_indices,
1297 const std::vector<size_type> &col_indices,
1299 const bool elide_zero_values)
1301 Assert(row_indices.size() == values.m(),
1303 Assert(col_indices.size() == values.n(),
1306 for (size_type i = 0; i < row_indices.size(); ++i)
1317 MatrixBase::set(
const size_type row,
1318 const std::vector<size_type> &col_indices,
1319 const std::vector<PetscScalar> &values,
1320 const bool elide_zero_values)
1322 Assert(col_indices.size() == values.size(),
1335 MatrixBase::set(
const size_type row,
1336 const size_type n_cols,
1337 const size_type *col_indices,
1338 const PetscScalar *values,
1339 const bool elide_zero_values)
1343 const PetscInt petsc_i = row;
1344 const PetscInt *col_index_ptr;
1346 const PetscScalar *col_value_ptr;
1350 if (elide_zero_values ==
false)
1352 col_index_ptr =
reinterpret_cast<const PetscInt *
>(col_indices);
1353 col_value_ptr = values;
1360 if (column_indices.size() < n_cols)
1362 column_indices.resize(n_cols);
1363 column_values.resize(n_cols);
1367 for (size_type j = 0; j < n_cols; ++j)
1369 const PetscScalar
value = values[j];
1371 if (value != PetscScalar())
1373 column_indices[n_columns] = col_indices[j];
1374 column_values[n_columns] =
value;
1380 col_index_ptr = column_indices.data();
1381 col_value_ptr = column_values.data();
1384 const PetscErrorCode ierr = MatSetValues(matrix,
1397 MatrixBase::add(
const size_type i,
const size_type j,
const PetscScalar value)
1401 if (value == PetscScalar())
1412 add(i, 1, &j, &value,
false);
1418 MatrixBase::add(
const std::vector<size_type> &indices,
1420 const bool elide_zero_values)
1422 Assert(indices.size() == values.m(),
1426 for (size_type i = 0; i < indices.size(); ++i)
1437 MatrixBase::add(
const std::vector<size_type> &row_indices,
1438 const std::vector<size_type> &col_indices,
1440 const bool elide_zero_values)
1442 Assert(row_indices.size() == values.m(),
1444 Assert(col_indices.size() == values.n(),
1447 for (size_type i = 0; i < row_indices.size(); ++i)
1458 MatrixBase::add(
const size_type row,
1459 const std::vector<size_type> &col_indices,
1460 const std::vector<PetscScalar> &values,
1461 const bool elide_zero_values)
1463 Assert(col_indices.size() == values.size(),
1476 MatrixBase::add(
const size_type row,
1477 const size_type n_cols,
1478 const size_type *col_indices,
1479 const PetscScalar *values,
1480 const bool elide_zero_values,
1483 (void)elide_zero_values;
1487 const PetscInt petsc_i = row;
1488 const PetscInt *col_index_ptr;
1490 const PetscScalar *col_value_ptr;
1494 if (elide_zero_values ==
false)
1496 col_index_ptr =
reinterpret_cast<const PetscInt *
>(col_indices);
1497 col_value_ptr = values;
1504 if (column_indices.size() < n_cols)
1506 column_indices.resize(n_cols);
1507 column_values.resize(n_cols);
1511 for (size_type j = 0; j < n_cols; ++j)
1513 const PetscScalar
value = values[j];
1515 if (value != PetscScalar())
1517 column_indices[n_columns] = col_indices[j];
1518 column_values[n_columns] =
value;
1524 col_index_ptr = column_indices.data();
1525 col_value_ptr = column_values.data();
1528 const PetscErrorCode ierr = MatSetValues(
1529 matrix, 1, &petsc_i, n_columns, col_index_ptr, col_value_ptr, ADD_VALUES);
1536 MatrixBase::operator()(
const size_type i,
const size_type j)
const
1543 inline MatrixBase::const_iterator
1544 MatrixBase::begin()
const
1547 (in_local_range(0) && in_local_range(m() - 1)),
1549 "begin() and end() can only be called on a processor owning the entire matrix. If this is a distributed matrix, use begin(row) and end(row) instead."));
1553 size_type first_nonempty_row = 0;
1554 while ((first_nonempty_row < m()) && (row_length(first_nonempty_row) == 0))
1555 ++first_nonempty_row;
1557 return const_iterator(
this, first_nonempty_row, 0);
1561 inline MatrixBase::const_iterator
1562 MatrixBase::end()
const
1565 (in_local_range(0) && in_local_range(m() - 1)),
1567 "begin() and end() can only be called on a processor owning the entire matrix. If this is a distributed matrix, use begin(row) and end(row) instead."));
1569 return const_iterator(
this, m(), 0);
1573 inline MatrixBase::const_iterator
1574 MatrixBase::begin(
const size_type r)
const
1576 Assert(in_local_range(r),
1579 if (row_length(r) > 0)
1580 return const_iterator(
this, r, 0);
1586 inline MatrixBase::const_iterator
1587 MatrixBase::end(
const size_type r)
const
1589 Assert(in_local_range(r),
1602 for (size_type i = r + 1; i < m(); ++i)
1603 if (i == local_range().second || (row_length(i) > 0))
1604 return const_iterator(
this, i, 0);
1610 return {
this, m(), 0};
1616 MatrixBase::in_local_range(
const size_type index)
const
1620 const PetscErrorCode ierr =
1621 MatGetOwnershipRange(
static_cast<const Mat &
>(matrix), &begin, &end);
1624 return ((index >=
static_cast<size_type
>(begin)) &&
1625 (index <
static_cast<size_type
>(end)));
1634 last_action = new_action;
1636 Assert(last_action == new_action, ExcWrongMode(last_action, new_action));
1642 MatrixBase::assert_is_compressed()
1647 ExcMessage(
"Error: missing compress() call."));
1653 MatrixBase::prepare_add()
1661 MatrixBase::prepare_set()
1667 MatrixBase::get_mpi_communicator()
const
1669 return PetscObjectComm(
reinterpret_cast<PetscObject
>(matrix));
void add(const size_type i, const size_type j, const PetscScalar value)
void add(const std::vector< size_type > &row_indices, const std::vector< size_type > &col_indices, const FullMatrix< PetscScalar > &full_matrix, const bool elide_zero_values=true)
const_iterator end(const size_type r) const
size_type row_length(const size_type row) const
std::size_t memory_consumption() const
PetscReal l1_norm() const
VectorOperation::values last_action
void vmult(VectorBase &dst, const VectorBase &src) const
std::vector< PetscScalar > column_values
MPI_Comm get_mpi_communicator() const
PetscScalar diag_element(const size_type i) const
MatrixBase(const MatrixBase &)=delete
size_type local_domain_size() const
const_iterator begin() const
MatrixBase & operator/=(const PetscScalar factor)
void mmult(MatrixBase &C, const MatrixBase &B, const VectorBase &V) const
PetscBool is_symmetric(const double tolerance=1.e-12)
PetscReal frobenius_norm() const
virtual ~MatrixBase() override
std::pair< size_type, size_type > local_domain() const
const_iterator end() const
void Tvmult_add(VectorBase &dst, const VectorBase &src) const
void print(std::ostream &out, const bool alternative_output=false) const
const_iterator begin(const size_type r) const
void add(const size_type row, const std::vector< size_type > &col_indices, const std::vector< PetscScalar > &values, const bool elide_zero_values=true)
PetscScalar el(const size_type i, const size_type j) const
size_type local_size() const
PetscScalar operator()(const size_type i, const size_type j) const
MatrixBase & operator=(const MatrixBase &)=delete
void set(const std::vector< size_type > &row_indices, const std::vector< size_type > &col_indices, const FullMatrix< PetscScalar > &full_matrix, const bool elide_zero_values=false)
PetscScalar trace() const
void prepare_action(const VectorOperation::values new_action)
void add(const std::vector< size_type > &indices, const FullMatrix< PetscScalar > &full_matrix, const bool elide_zero_values=true)
bool in_local_range(const size_type index) const
PetscBool is_hermitian(const double tolerance=1.e-12)
PetscScalar matrix_scalar_product(const VectorBase &u, const VectorBase &v) const
void assert_is_compressed()
std::vector< PetscInt > column_indices
void Tvmult(VectorBase &dst, const VectorBase &src) const
void clear_rows_columns(const std::vector< size_type > &row_and_column_indices, const PetscScalar new_diag_value=0)
MatrixBase & operator*=(const PetscScalar factor)
PetscScalar residual(VectorBase &dst, const VectorBase &x, const VectorBase &b) const
void Tmmult(MatrixBase &C, const MatrixBase &B, const VectorBase &V) const
void set(const size_type row, const std::vector< size_type > &col_indices, const std::vector< PetscScalar > &values, const bool elide_zero_values=false)
void add(const size_type row, const size_type n_cols, const size_type *col_indices, const PetscScalar *values, const bool elide_zero_values=true, const bool col_indices_are_sorted=false)
void write_ascii(const PetscViewerFormat format=PETSC_VIEWER_DEFAULT)
void vmult_add(VectorBase &dst, const VectorBase &src) const
std::pair< size_type, size_type > local_range() const
void compress(const VectorOperation::values operation)
PetscScalar matrix_norm_square(const VectorBase &v) const
void set(const size_type row, const size_type n_cols, const size_type *col_indices, const PetscScalar *values, const bool elide_zero_values=false)
void clear_rows(const ArrayView< const size_type > &rows, const PetscScalar new_diag_value=0)
void set(const std::vector< size_type > &indices, const FullMatrix< PetscScalar > &full_matrix, const bool elide_zero_values=false)
void set(const size_type i, const size_type j, const PetscScalar value)
std::uint64_t n_nonzero_elements() const
void clear_row(const size_type row, const PetscScalar new_diag_value=0)
PetscReal linfty_norm() const
std::shared_ptr< const std::vector< PetscScalar > > value_cache
PetscScalar value() const
types::global_dof_index size_type
std::shared_ptr< const std::vector< size_type > > colnum_cache
Accessor(const MatrixBase *matrix, const size_type row, const size_type index)
const_iterator operator++(int)
bool operator!=(const const_iterator &) const
const_iterator(const MatrixBase *matrix, const size_type row, const size_type index)
bool operator==(const const_iterator &) const
bool operator<(const const_iterator &) const
const_iterator & operator++()
const Accessor * operator->() const
const Accessor & operator*() const
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
__global__ void set(Number *val, const Number s, const size_type N)
#define DeclException0(Exception0)
static ::ExceptionBase & ExcAccessToNonlocalRow(int arg1, int arg2, int arg3)
#define Assert(cond, exc)
static ::ExceptionBase & ExcIteratorPastEnd()
static ::ExceptionBase & ExcInvalidIndexWithinRow(int arg1, int arg2)
#define AssertIsFinite(number)
#define DeclException2(Exception2, type1, type2, outsequence)
static ::ExceptionBase & ExcBeyondEndOfMatrix()
#define AssertIndexRange(index, range)
#define DeclExceptionMsg(Exception, defaulttext)
static ::ExceptionBase & ExcSourceEqualsDestination()
static ::ExceptionBase & ExcWrongMode(int arg1, int arg2)
#define DeclException3(Exception3, type1, type2, type3, outsequence)
static ::ExceptionBase & ExcIndexRange(std::size_t arg1, std::size_t arg2, std::size_t arg3)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcNotQuadratic()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
@ matrix
Contents is actually a matrix.
VectorType::value_type * end(VectorType &V)
VectorType::value_type * begin(VectorType &V)
unsigned int global_dof_index