16#ifndef dealii_petsc_matrix_base_h
17# define dealii_petsc_matrix_base_h
22# ifdef DEAL_II_WITH_PETSC
42template <
typename Matrix>
52 namespace MatrixIterators
124 <<
"You tried to access row " << arg1
125 <<
" of a distributed matrix, but only rows " << arg2
126 <<
" through " << arg3
127 <<
" are stored locally and can be accessed.");
238 <<
"Attempt to access element " << arg2 <<
" of row "
239 << arg1 <<
" which doesn't have that many elements.");
375 set(
const std::vector<size_type> & indices,
377 const bool elide_zero_values =
false);
385 set(
const std::vector<size_type> & row_indices,
386 const std::vector<size_type> & col_indices,
388 const bool elide_zero_values =
false);
406 const std::vector<size_type> & col_indices,
407 const std::vector<PetscScalar> &
values,
408 const bool elide_zero_values =
false);
428 const PetscScalar *
values,
429 const bool elide_zero_values =
false);
463 add(
const std::vector<size_type> & indices,
465 const bool elide_zero_values =
true);
473 add(
const std::vector<size_type> & row_indices,
474 const std::vector<size_type> & col_indices,
476 const bool elide_zero_values =
true);
494 const std::vector<size_type> & col_indices,
495 const std::vector<PetscScalar> &
values,
496 const bool elide_zero_values =
true);
516 const PetscScalar *
values,
517 const bool elide_zero_values =
true,
518 const bool col_indices_are_sorted =
false);
548 clear_rows(
const std::vector<size_type> &rows,
549 const PetscScalar new_diag_value = 0);
632 std::pair<size_type, size_type>
885 operator Mat()
const;
923 write_ascii(
const PetscViewerFormat format = PETSC_VIEWER_DEFAULT);
933 print(std::ostream &out,
const bool alternative_output =
false)
const;
945 "You are attempting an operation on two matrices that "
946 "are the same object, but the operation requires that the "
947 "two objects are in fact different.");
955 <<
"You tried to do a "
956 << (arg1 == 1 ?
"'set'" : (arg1 == 2 ?
"'add'" :
"???"))
957 <<
" operation but the matrix is currently in "
958 << (arg2 == 1 ?
"'set'" : (arg2 == 2 ?
"'add'" :
"???"))
959 <<
" mode. You first have to call 'compress()'.");
1076 friend class ::BlockMatrixBase;
1085 namespace MatrixIterators
1094 visit_present_row();
1111 return (*colnum_cache)[a_index];
1127 return (*value_cache)[a_index];
1139 inline const_iterator &
1148 if (accessor.a_index >= accessor.colnum_cache->size())
1150 accessor.a_index = 0;
1153 while ((accessor.a_row < accessor.matrix->m()) &&
1154 (accessor.a_row < accessor.matrix->local_range().second) &&
1155 (accessor.matrix->row_length(accessor.a_row) == 0))
1158 accessor.visit_present_row();
1164 inline const_iterator
1167 const const_iterator old_state = *
this;
1179 inline const const_iterator::Accessor *const_iterator::operator->()
const
1188 return (accessor.a_row == other.accessor.a_row &&
1189 accessor.a_index == other.accessor.a_index);
1196 return !(*
this == other);
1203 return (accessor.row() < other.accessor.row() ||
1204 (accessor.row() == other.accessor.row() &&
1205 accessor.index() < other.accessor.index()));
1224 set(i, 1, &j, &value,
false);
1232 const bool elide_zero_values)
1238 for (
size_type i = 0; i < indices.size(); ++i)
1250 const std::vector<size_type> & col_indices,
1252 const bool elide_zero_values)
1259 for (
size_type i = 0; i < row_indices.size(); ++i)
1271 const std::vector<size_type> & col_indices,
1272 const std::vector<PetscScalar> &
values,
1273 const bool elide_zero_values)
1291 const PetscScalar *
values,
1292 const bool elide_zero_values)
1296 const PetscInt petsc_i = row;
1297 PetscInt
const *col_index_ptr;
1299 PetscScalar
const *col_value_ptr;
1303 if (elide_zero_values ==
false)
1305 col_index_ptr =
reinterpret_cast<const PetscInt *
>(col_indices);
1313 if (column_indices.size() < n_cols)
1315 column_indices.resize(n_cols);
1316 column_values.resize(n_cols);
1324 if (value != PetscScalar())
1326 column_indices[n_columns] = col_indices[j];
1327 column_values[n_columns] =
value;
1333 col_index_ptr = column_indices.data();
1334 col_value_ptr = column_values.data();
1337 const PetscErrorCode ierr = MatSetValues(
matrix,
1354 if (value == PetscScalar())
1365 add(i, 1, &j, &value,
false);
1371 MatrixBase::add(
const std::vector<size_type> & indices,
1373 const bool elide_zero_values)
1379 for (
size_type i = 0; i < indices.size(); ++i)
1390 MatrixBase::add(
const std::vector<size_type> & row_indices,
1391 const std::vector<size_type> & col_indices,
1393 const bool elide_zero_values)
1400 for (
size_type i = 0; i < row_indices.size(); ++i)
1412 const std::vector<size_type> & col_indices,
1413 const std::vector<PetscScalar> &
values,
1414 const bool elide_zero_values)
1432 const PetscScalar *
values,
1433 const bool elide_zero_values,
1436 (void)elide_zero_values;
1440 const PetscInt petsc_i = row;
1441 PetscInt
const *col_index_ptr;
1443 PetscScalar
const *col_value_ptr;
1447 if (elide_zero_values ==
false)
1449 col_index_ptr =
reinterpret_cast<const PetscInt *
>(col_indices);
1457 if (column_indices.size() < n_cols)
1459 column_indices.resize(n_cols);
1460 column_values.resize(n_cols);
1468 if (value != PetscScalar())
1470 column_indices[n_columns] = col_indices[j];
1471 column_values[n_columns] =
value;
1477 col_index_ptr = column_indices.data();
1478 col_value_ptr = column_values.data();
1481 const PetscErrorCode ierr = MatSetValues(
1482 matrix, 1, &petsc_i, n_columns, col_index_ptr, col_value_ptr, ADD_VALUES);
1496 inline MatrixBase::const_iterator
1500 (in_local_range(0) && in_local_range(m() - 1)),
1502 "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."));
1507 while ((first_nonempty_row < m()) && (row_length(first_nonempty_row) == 0))
1508 ++first_nonempty_row;
1510 return const_iterator(
this, first_nonempty_row, 0);
1514 inline MatrixBase::const_iterator
1518 (in_local_range(0) && in_local_range(m() - 1)),
1520 "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."));
1522 return const_iterator(
this, m(), 0);
1526 inline MatrixBase::const_iterator
1529 Assert(in_local_range(r),
1532 if (row_length(r) > 0)
1533 return const_iterator(
this, r, 0);
1539 inline MatrixBase::const_iterator
1542 Assert(in_local_range(r),
1556 if (i == local_range().second || (row_length(i) > 0))
1557 return const_iterator(
this, i, 0);
1563 return {
this, m(), 0};
1569 MatrixBase::in_local_range(
const size_type index)
const
1573 const PetscErrorCode ierr =
1574 MatGetOwnershipRange(
static_cast<const Mat &
>(
matrix), &
begin, &
end);
1587 last_action = new_action;
1589 Assert(last_action == new_action, ExcWrongMode(last_action, new_action));
1595 MatrixBase::assert_is_compressed()
1600 ExcMessage(
"Error: missing compress() call."));
1606 MatrixBase::prepare_add()
1614 MatrixBase::prepare_set()
bool operator!=(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
bool operator==(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
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
void clear_rows(const std::vector< size_type > &rows, const PetscScalar new_diag_value=0)
std::vector< PetscScalar > column_values
PetscScalar diag_element(const size_type i) const
MatrixBase(const MatrixBase &)=delete
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
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
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
size_type n_nonzero_elements() 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)
virtual const MPI_Comm & get_mpi_communicator() const =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)
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)
types::global_dof_index size_type
bool operator==(const const_iterator &) const
bool operator<(const const_iterator &) const
const_iterator & operator++()
const Accessor * operator->() const
const Accessor & operator*() const
std::enable_if< std::is_floating_point< T >::value &&std::is_floating_point< U >::value, typenameProductType< std::complex< T >, std::complex< U > >::type >::type operator*(const std::complex< T > &left, const std::complex< U > &right)
#define DEAL_II_DEPRECATED
#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)
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
#define Assert(cond, exc)
static ::ExceptionBase & ExcIteratorPastEnd()
static ::ExceptionBase & ExcInvalidIndexWithinRow(int arg1, int arg2)
#define AssertIsFinite(number)
static ::ExceptionBase & ExcBeyondEndOfMatrix()
#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 & 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.
types::global_dof_index size_type
SymmetricTensor< 2, dim, Number > C(const Tensor< 2, dim, Number > &F)
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
VectorType::value_type * end(VectorType &V)
VectorType::value_type * begin(VectorType &V)
unsigned int global_dof_index
SynchronousIterators< Iterators > operator++(SynchronousIterators< Iterators > &a)
bool operator<(const SynchronousIterators< Iterators > &a, const SynchronousIterators< Iterators > &b)