Loading...
Searching...
No Matches
Matrix.h
// Copyright (C) 2006-2008 Anders Logg and Garth N. Wells
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
#ifndef __MATRIX_H
#define __MATRIX_H
#include <memory>
#include <tuple>
#include "DefaultFactory.h"
#include "GenericMatrix.h"
namespace dolfin
{
class GenericVector;
class TensorLayout;
class Matrix : public GenericMatrix
{
public:
Matrix() : Matrix(MPI_COMM_WORLD) {}
explicit Matrix(MPI_Comm comm)
{
DefaultFactory factory;
matrix = factory.create_matrix(comm);
}
Matrix(const Matrix& A) : matrix(A.matrix->copy()) {}
Matrix(const GenericMatrix& A) : matrix(A.copy()) {}
virtual ~Matrix() {}
//--- Implementation of the GenericTensor interface ---
virtual void init(const TensorLayout& tensor_layout)
{ matrix->init(tensor_layout); }
virtual bool empty() const
{ return matrix->empty(); }
virtual std::size_t size(std::size_t dim) const
{ return matrix->size(dim); }
virtual std::pair<std::int64_t, std::int64_t>
local_range(std::size_t dim) const
{ return matrix->local_range(dim); }
virtual std::size_t nnz() const
{ return matrix->nnz(); }
virtual void zero()
{ matrix->zero(); }
virtual void apply(std::string mode)
{ matrix->apply(mode); }
MPI_Comm mpi_comm() const
{ return matrix->mpi_comm(); }
virtual std::string str(bool verbose) const
{ return "<Matrix wrapper of " + matrix->str(verbose) + ">"; }
//--- Implementation of the GenericMatrix interface ---
virtual std::shared_ptr<GenericMatrix> copy() const
{
std::shared_ptr<Matrix> A(new Matrix(*this));
return A;
}
virtual void init_vector(GenericVector& y, std::size_t dim) const
{ matrix->init_vector(y, dim); }
virtual void get(double* block,
std::size_t m, const dolfin::la_index* rows,
std::size_t n, const dolfin::la_index* cols) const
{ matrix->get(block, m, rows, n, cols); }
virtual void set(const double* block,
std::size_t m, const dolfin::la_index* rows,
std::size_t n, const dolfin::la_index* cols)
{ matrix->set(block, m, rows, n, cols); }
virtual void set_local(const double* block,
std::size_t m, const dolfin::la_index* rows,
std::size_t n, const dolfin::la_index* cols)
{ matrix->set_local(block, m, rows, n, cols); }
virtual void add(const double* block,
std::size_t m, const dolfin::la_index* rows,
std::size_t n, const dolfin::la_index* cols)
{ matrix->add(block, m, rows, n, cols); }
virtual void add_local(const double* block,
std::size_t m, const dolfin::la_index* rows,
std::size_t n, const dolfin::la_index* cols)
{ matrix->add_local(block, m, rows, n, cols); }
virtual void axpy(double a, const GenericMatrix& A,
bool same_nonzero_pattern)
{ matrix->axpy(a, A, same_nonzero_pattern); }
virtual double norm(std::string norm_type) const
{ return matrix->norm(norm_type); }
virtual void getrow(std::size_t row, std::vector<std::size_t>& columns,
std::vector<double>& values) const
{ matrix->getrow(row, columns, values); }
virtual void setrow(std::size_t row,
const std::vector<std::size_t>& columns,
const std::vector<double>& values)
{ matrix->setrow(row, columns, values); }
virtual void zero(std::size_t m, const dolfin::la_index* rows)
{ matrix->zero(m, rows); }
virtual void zero_local(std::size_t m, const dolfin::la_index* rows)
{ matrix->zero_local(m, rows); }
virtual void ident(std::size_t m, const dolfin::la_index* rows)
{ matrix->ident(m, rows); }
virtual void ident_local(std::size_t m, const dolfin::la_index* rows)
{ matrix->ident_local(m, rows); }
// Matrix-vector product, y = Ax
virtual void mult(const GenericVector& x, GenericVector& y) const
{ matrix->mult(x, y); }
// Matrix-vector product, y = Ax
virtual void transpmult(const GenericVector& x, GenericVector& y) const
{ matrix->transpmult(x, y); }
virtual void get_diagonal(GenericVector& x) const
{ matrix->get_diagonal(x); }
virtual void set_diagonal(const GenericVector& x)
{ matrix->set_diagonal(x); }
virtual const Matrix& operator*= (double a)
{ *matrix *= a; return *this; }
virtual const Matrix& operator/= (double a)
{ *matrix /= a; return *this; }
virtual const GenericMatrix& operator= (const GenericMatrix& A)
{ *matrix = A; return *this; }
virtual bool is_symmetric(double tol) const
{ return matrix->is_symmetric(tol); }
//--- Special functions ---
virtual GenericLinearAlgebraFactory& factory() const
{ return matrix->factory(); }
//--- Special functions, intended for library use only ---
virtual const GenericMatrix* instance() const
{ return matrix.get() ; }
virtual GenericMatrix* instance()
{ return matrix.get(); }
virtual std::shared_ptr<const LinearAlgebraObject> shared_instance() const
{ return matrix; }
virtual std::shared_ptr<LinearAlgebraObject> shared_instance()
{ return matrix; }
//--- Special Matrix functions ---
const Matrix& operator= (const Matrix& A)
{ *matrix = *A.matrix; return *this; }
private:
// Pointer to concrete implementation
std::shared_ptr<GenericMatrix> matrix;
};
}
#endif
dolfin::DefaultFactory
Default linear algebra factory based on global parameter "linear_algebra_backend".
Definition DefaultFactory.h:36
dolfin::GenericLinearAlgebraFactory
Base class for LinearAlgebra factories.
Definition GenericLinearAlgebraFactory.h:47
dolfin::GenericMatrix
This class defines a common interface for matrices.
Definition GenericMatrix.h:47
dolfin::GenericMatrix::get
virtual void get(double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows) const
Get block of values.
Definition GenericMatrix.h:70
dolfin::GenericVector
This class defines a common interface for vectors.
Definition GenericVector.h:48
dolfin::Matrix::set_local
virtual void set_local(const double *block, std::size_t m, const dolfin::la_index *rows, std::size_t n, const dolfin::la_index *cols)
Set block of values using local indices.
Definition Matrix.h:125
dolfin::Matrix::shared_instance
virtual std::shared_ptr< const LinearAlgebraObject > shared_instance() const
Return concrete shared ptr instance / unwrap (const version).
Definition Matrix.h:226
dolfin::Matrix::instance
virtual const GenericMatrix * instance() const
Return concrete instance / unwrap (const version).
Definition Matrix.h:219
dolfin::Matrix::empty
virtual bool empty() const
Return true if matrix is empty.
Definition Matrix.h:65
dolfin::Matrix::instance
virtual GenericMatrix * instance()
Return concrete instance / unwrap (non-const version).
Definition Matrix.h:223
dolfin::Matrix::norm
virtual double norm(std::string norm_type) const
Return norm of matrix.
Definition Matrix.h:148
dolfin::Matrix::set_diagonal
virtual void set_diagonal(const GenericVector &x)
Set diagonal of a matrix.
Definition Matrix.h:191
dolfin::Matrix::copy
virtual std::shared_ptr< GenericMatrix > copy() const
Return copy of matrix.
Definition Matrix.h:100
dolfin::Matrix::init_vector
virtual void init_vector(GenericVector &y, std::size_t dim) const
Definition Matrix.h:109
dolfin::Matrix::nnz
virtual std::size_t nnz() const
Return number of non-zero entries in matrix (collective).
Definition Matrix.h:78
dolfin::Matrix::init
virtual void init(const TensorLayout &tensor_layout)
Initialize zero tensor using tensor layout.
Definition Matrix.h:61
dolfin::Matrix::operator=
virtual const GenericMatrix & operator=(const GenericMatrix &A)
Assignment operator.
Definition Matrix.h:203
dolfin::Matrix::get_diagonal
virtual void get_diagonal(GenericVector &x) const
Get diagonal of a matrix.
Definition Matrix.h:187
dolfin::Matrix::Matrix
Matrix()
Create empty matrix.
Definition Matrix.h:40
dolfin::Matrix::get
virtual void get(double *block, std::size_t m, const dolfin::la_index *rows, std::size_t n, const dolfin::la_index *cols) const
Get block of values.
Definition Matrix.h:113
dolfin::Matrix::Matrix
Matrix(const GenericMatrix &A)
Create a Vector from a GenericVector.
Definition Matrix.h:53
dolfin::Matrix::is_symmetric
virtual bool is_symmetric(double tol) const
Test if matrix is symmetric.
Definition Matrix.h:207
dolfin::Matrix::add_local
virtual void add_local(const double *block, std::size_t m, const dolfin::la_index *rows, std::size_t n, const dolfin::la_index *cols)
Add block of values using local indices.
Definition Matrix.h:137
dolfin::Matrix::mpi_comm
MPI_Comm mpi_comm() const
Return MPI communicator.
Definition Matrix.h:90
dolfin::Matrix::apply
virtual void apply(std::string mode)
Finalize assembly of tensor.
Definition Matrix.h:86
dolfin::Matrix::factory
virtual GenericLinearAlgebraFactory & factory() const
Return linear algebra backend factory.
Definition Matrix.h:213
dolfin::Matrix::operator*=
virtual const Matrix & operator*=(double a)
Multiply matrix by given number.
Definition Matrix.h:195
dolfin::Matrix::str
virtual std::string str(bool verbose) const
Return informal string representation (pretty-print).
Definition Matrix.h:94
dolfin::Matrix::operator/=
virtual const Matrix & operator/=(double a)
Divide matrix by given number.
Definition Matrix.h:199
dolfin::Matrix::zero
virtual void zero()
Set all entries to zero and keep any sparse structure.
Definition Matrix.h:82
dolfin::Matrix::set
virtual void set(const double *block, std::size_t m, const dolfin::la_index *rows, std::size_t n, const dolfin::la_index *cols)
Set block of values using global indices.
Definition Matrix.h:119
dolfin::Matrix::zero
virtual void zero(std::size_t m, const dolfin::la_index *rows)
Set given rows to zero.
Definition Matrix.h:163
dolfin::Matrix::setrow
virtual void setrow(std::size_t row, const std::vector< std::size_t > &columns, const std::vector< double > &values)
Set values for given row.
Definition Matrix.h:157
dolfin::Matrix::Matrix
Matrix(MPI_Comm comm)
Create empty matrix.
Definition Matrix.h:43
dolfin::Matrix::mult
virtual void mult(const GenericVector &x, GenericVector &y) const
Compute matrix-vector product y = Ax.
Definition Matrix.h:179
dolfin::Matrix::Matrix
Matrix(const Matrix &A)
Copy constructor.
Definition Matrix.h:50
dolfin::Matrix::zero_local
virtual void zero_local(std::size_t m, const dolfin::la_index *rows)
Set given rows (local row indices) to zero.
Definition Matrix.h:167
dolfin::Matrix::add
virtual void add(const double *block, std::size_t m, const dolfin::la_index *rows, std::size_t n, const dolfin::la_index *cols)
Add block of values using global indices.
Definition Matrix.h:131
dolfin::Matrix::axpy
virtual void axpy(double a, const GenericMatrix &A, bool same_nonzero_pattern)
Add multiple of given matrix (AXPY operation).
Definition Matrix.h:143
dolfin::Matrix::shared_instance
virtual std::shared_ptr< LinearAlgebraObject > shared_instance()
Return concrete shared ptr instance / unwrap (non-const version).
Definition Matrix.h:229
dolfin::Matrix::transpmult
virtual void transpmult(const GenericVector &x, GenericVector &y) const
Definition Matrix.h:183
dolfin::Matrix::ident
virtual void ident(std::size_t m, const dolfin::la_index *rows)
Set given rows (global row indices) to identity matrix.
Definition Matrix.h:171
dolfin::Matrix::getrow
virtual void getrow(std::size_t row, std::vector< std::size_t > &columns, std::vector< double > &values) const
Get non-zero values of given row.
Definition Matrix.h:152
dolfin::Matrix::~Matrix
virtual ~Matrix()
Destructor.
Definition Matrix.h:56
dolfin::Matrix::size
virtual std::size_t size(std::size_t dim) const
Return size of given dimension.
Definition Matrix.h:69
dolfin::Matrix::local_range
virtual std::pair< std::int64_t, std::int64_t > local_range(std::size_t dim) const
Return local ownership range.
Definition Matrix.h:74
dolfin::Matrix::ident_local
virtual void ident_local(std::size_t m, const dolfin::la_index *rows)
Set given rows (local row indices) to identity matrix.
Definition Matrix.h:175
dolfin::TensorLayout
Definition TensorLayout.h:42
dolfin
Definition adapt.h:30
dolfin::la_index
PetscInt la_index
Index type for compatibility with linear algebra backend(s).
Definition types.h:32