Loading...
Searching...
No Matches
TpetraMatrix.h
// Copyright (C) 2014
//
// 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/>.
//
// First added: 2014
#ifndef __TPETRA_MATRIX_H
#define __TPETRA_MATRIX_H
#ifdef HAS_TRILINOS
#include <array>
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <Teuchos_GlobalMPISession.hpp>
#include <Teuchos_oblackholestream.hpp>
#include <Tpetra_CrsMatrix.hpp>
#include <Tpetra_DefaultPlatform.hpp>
#include <Tpetra_Vector.hpp>
#include <Tpetra_Version.hpp>
#include <dolfin/common/types.h>
#include "GenericMatrix.h"
namespace dolfin
{
class GenericLinearAlgebraFactory;
class IndexMap;
class TensorLayout;
class TpetraVector;
class TpetraMatrix : public GenericMatrix
{
public:
typedef Tpetra::CrsMatrix<double, int, dolfin::la_index> matrix_type;
typedef Tpetra::CrsGraph<int, dolfin::la_index> graph_type;
typedef Tpetra::Map<int, dolfin::la_index> map_type;
TpetraMatrix();
explicit TpetraMatrix(Teuchos::RCP<matrix_type> A);
TpetraMatrix(const TpetraMatrix& A);
virtual ~TpetraMatrix();
//--- Implementation of the GenericTensor interface ---
void init(const TensorLayout& tensor_layout);
bool empty() const;
std::size_t size(std::size_t dim) const;
std::pair<std::int64_t, std::int64_t> local_range(std::size_t dim) const;
// Number of non-zero entries
std::size_t nnz() const;
virtual void zero();
virtual void apply(std::string mode);
MPI_Comm mpi_comm() const;
virtual std::string str(bool verbose) const;
//--- Implementation of the GenericMatrix interface --
virtual std::shared_ptr<GenericMatrix> copy() const;
virtual void init_vector(GenericVector& z, std::size_t dim) const;
virtual void get(double* block,
std::size_t m, const dolfin::la_index* rows,
std::size_t n, const dolfin::la_index* cols) const;
virtual void set(const double* block,
std::size_t m, const dolfin::la_index* rows,
std::size_t n, const dolfin::la_index* 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);
virtual void add(const double* block,
std::size_t m, const dolfin::la_index* rows,
std::size_t n, const dolfin::la_index* 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);
virtual void axpy(double a, const GenericMatrix& A,
bool same_nonzero_pattern);
virtual void getrow(std::size_t row,
std::vector<std::size_t>& columns,
std::vector<double>& values) const;
virtual void setrow(std::size_t row,
const std::vector<std::size_t>& columns,
const std::vector<double>& values);
virtual void zero(std::size_t m, const dolfin::la_index* rows);
virtual void zero_local(std::size_t m, const dolfin::la_index* rows);
virtual void ident(std::size_t m, const dolfin::la_index* rows);
virtual void ident_local(std::size_t m, const dolfin::la_index* rows);
// Matrix-vector product, y = Ax
virtual void mult(const GenericVector& x, GenericVector& y) const;
// Matrix-vector product, y = A^T x
virtual void transpmult(const GenericVector& x, GenericVector& y) const;
virtual void get_diagonal(GenericVector& x) const;
virtual void set_diagonal(const GenericVector& x);
virtual const TpetraMatrix& operator*= (double a);
virtual const TpetraMatrix& operator/= (double a);
virtual const GenericMatrix& operator= (const GenericMatrix& A);
virtual bool is_symmetric(double tol) const;
//--- Special functions ---
virtual GenericLinearAlgebraFactory& factory() const;
//--- Special TpetraFunctions ---
double norm(std::string norm_type) const;
const TpetraMatrix& operator= (const TpetraMatrix& A);
Teuchos::RCP<matrix_type> mat()
{ return _matA; }
Teuchos::RCP<const matrix_type> mat() const
{ return _matA; }
static void graphdump(const Teuchos::RCP<const graph_type> graph);
private:
// The matrix
Teuchos::RCP<matrix_type> _matA;
// Row and Column maps to allow local indexing of off-process
// entries needed in add_local() and set_local()
std::array<std::shared_ptr<const IndexMap>, 2> index_map;
};
}
#endif
#endif
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::GenericVector
This class defines a common interface for vectors.
Definition GenericVector.h:48
dolfin::IndexMap
Definition IndexMap.h:36
dolfin::TensorLayout
Definition TensorLayout.h:42
dolfin::TpetraMatrix::empty
bool empty() const
Return true if empty.
Definition TpetraMatrix.cpp:244
dolfin::TpetraMatrix::mult
virtual void mult(const GenericVector &x, GenericVector &y) const
Compute matrix-vector product y = Ax.
Definition TpetraMatrix.cpp:572
dolfin::TpetraMatrix::operator*=
virtual const TpetraMatrix & operator*=(double a)
Multiply matrix by given number.
Definition TpetraMatrix.cpp:746
dolfin::TpetraMatrix::TpetraMatrix
TpetraMatrix()
Create empty matrix.
Definition TpetraMatrix.cpp:43
dolfin::TpetraMatrix::size
std::size_t size(std::size_t dim) const
Return size of given dimension.
Definition TpetraMatrix.cpp:184
dolfin::TpetraMatrix::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 TpetraMatrix.cpp:365
dolfin::TpetraMatrix::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 TpetraMatrix.cpp:538
dolfin::TpetraMatrix::ident
virtual void ident(std::size_t m, const dolfin::la_index *rows)
Set given rows (global row indices) to identity matrix.
Definition TpetraMatrix.cpp:510
dolfin::TpetraMatrix::transpmult
virtual void transpmult(const GenericVector &x, GenericVector &y) const
Definition TpetraMatrix.cpp:601
dolfin::TpetraMatrix::zero_local
virtual void zero_local(std::size_t m, const dolfin::la_index *rows)
Set given rows (local row indices) to zero.
Definition TpetraMatrix.cpp:492
dolfin::TpetraMatrix::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 TpetraMatrix.cpp:414
dolfin::TpetraMatrix::mat
Teuchos::RCP< matrix_type > mat()
Return Teuchos reference counted pointer to raw matrix.
Definition TpetraMatrix.h:215
dolfin::TpetraMatrix::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 TpetraMatrix.cpp:283
dolfin::TpetraMatrix::copy
virtual std::shared_ptr< GenericMatrix > copy() const
Return copy of matrix.
Definition TpetraMatrix.cpp:71
dolfin::TpetraMatrix::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 TpetraMatrix.cpp:349
dolfin::TpetraMatrix::set_diagonal
virtual void set_diagonal(const GenericVector &x)
Set diagonal of a matrix.
Definition TpetraMatrix.cpp:659
dolfin::TpetraMatrix::factory
virtual GenericLinearAlgebraFactory & factory() const
Return linear algebra backend factory.
Definition TpetraMatrix.cpp:778
dolfin::TpetraMatrix::graph_type
Tpetra::CrsGraph< int, dolfin::la_index > graph_type
Graph type (local index, global index).
Definition TpetraMatrix.h:66
dolfin::TpetraMatrix::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 TpetraMatrix.cpp:442
dolfin::TpetraMatrix::get_diagonal
virtual void get_diagonal(GenericVector &x) const
Get diagonal of a matrix.
Definition TpetraMatrix.cpp:629
dolfin::TpetraMatrix::local_range
std::pair< std::int64_t, std::int64_t > local_range(std::size_t dim) const
Return local ownership range.
Definition TpetraMatrix.cpp:205
dolfin::TpetraMatrix::apply
virtual void apply(std::string mode)
Finalize assembly of tensor. The mode parameter is ignored.
Definition TpetraMatrix.cpp:711
dolfin::TpetraMatrix::graphdump
static void graphdump(const Teuchos::RCP< const graph_type > graph)
Print out a graph, for debugging.
Definition TpetraMatrix.cpp:802
dolfin::TpetraMatrix::operator/=
virtual const TpetraMatrix & operator/=(double a)
Divide matrix by given number.
Definition TpetraMatrix.cpp:756
dolfin::TpetraMatrix::axpy
virtual void axpy(double a, const GenericMatrix &A, bool same_nonzero_pattern)
Add multiple of given matrix (AXPY operation).
Definition TpetraMatrix.cpp:397
dolfin::TpetraMatrix::nnz
std::size_t nnz() const
Return number of non-zero entries in matrix (collective).
Definition TpetraMatrix.cpp:236
dolfin::TpetraMatrix::mat
Teuchos::RCP< const matrix_type > mat() const
Return Teuchos reference counted pointer to raw matrix (const).
Definition TpetraMatrix.h:219
dolfin::TpetraMatrix::~TpetraMatrix
virtual ~TpetraMatrix()
Destructor.
Definition TpetraMatrix.cpp:66
dolfin::TpetraMatrix::init_vector
virtual void init_vector(GenericVector &z, std::size_t dim) const
Definition TpetraMatrix.cpp:249
dolfin::TpetraMatrix::mpi_comm
MPI_Comm mpi_comm() const
Return MPI communicator.
Definition TpetraMatrix.cpp:726
dolfin::TpetraMatrix::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 TpetraMatrix.cpp:302
dolfin::TpetraMatrix::operator=
virtual const GenericMatrix & operator=(const GenericMatrix &A)
Assignment operator.
Definition TpetraMatrix.cpp:766
dolfin::TpetraMatrix::zero
virtual void zero()
Set all entries to zero and keep any sparse structure.
Definition TpetraMatrix.cpp:737
dolfin::TpetraMatrix::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 TpetraMatrix.cpp:318
dolfin::TpetraMatrix::str
virtual std::string str(bool verbose) const
Return informal string representation (pretty-print).
Definition TpetraMatrix.cpp:788
dolfin::TpetraMatrix::matrix_type
Tpetra::CrsMatrix< double, int, dolfin::la_index > matrix_type
Matrix type (scalar, local index, global index).
Definition TpetraMatrix.h:64
dolfin::TpetraMatrix::norm
double norm(std::string norm_type) const
Return norm of matrix.
Definition TpetraMatrix.cpp:695
dolfin::TpetraMatrix::init
void init(const TensorLayout &tensor_layout)
Initialize zero tensor using tensor layout.
Definition TpetraMatrix.cpp:76
dolfin::TpetraMatrix::map_type
Tpetra::Map< int, dolfin::la_index > map_type
Map type (local index, global index).
Definition TpetraMatrix.h:68
dolfin::TpetraMatrix::is_symmetric
virtual bool is_symmetric(double tol) const
Test if matrix is symmetric.
Definition TpetraMatrix.cpp:772
dolfin::TpetraVector
Definition TpetraVector.h:54
dolfin
Definition adapt.h:30
dolfin::la_index
PetscInt la_index
Index type for compatibility with linear algebra backend(s).
Definition types.h:32