Discontinuous Galerkin Library
#include "dg/algorithm.h"
|
A CSR formatted sparse matrix. More...
Public Types | |
using | policy = dg::get_execution_policy<Vector<Value>> |
Execution policy of the matrix. | |
using | index_type = Index |
The index type (on GPU must be either int or long ) | |
using | value_type = Value |
The value type (on GPU must be either float or double ) | |
template<class OtherMatrix > | |
using | enable_if_serial = std::enable_if_t<std::is_same_v<typename OtherMatrix::policy, SerialTag>, OtherMatrix> |
A static guard to allow certain members only on host (for serial execution) | |
Public Member Functions | |
SparseMatrix ()=default | |
Empty matrix. | |
SparseMatrix (size_t num_rows, size_t num_cols, const Vector< Index > &row_offsets, const Vector< Index > &column_indices, const Vector< Value > &values) | |
Directly construct from given vectors. | |
template<class I , class V , template< class > class Vec> | |
SparseMatrix (const SparseMatrix< I, V, Vec > &src) | |
Copy construct from differen type. | |
void | setFromCoo (size_t num_rows, size_t num_cols, const Vector< Index > &row_indices, const Vector< Index > &column_indices, const Vector< Value > &values, bool sort=false) |
Set csr values from coo formatted sparse matrix. | |
void | set (size_t num_rows, size_t num_cols, const Vector< Index > &row_offsets, const Vector< Index > column_indices, const Vector< Value > &values, bool sort=false) |
Set csr values directly. | |
void | sort_indices () |
Sort by row and column. | |
size_t | total_num_rows () const |
Alias for num_rows . | |
size_t | total_num_cols () const |
Alias for num_cols . | |
size_t | num_rows () const |
Number of rows in matrix. | |
size_t | num_cols () const |
Number of columns in matrix. | |
size_t | num_vals () const |
Alias for num_nnz . | |
size_t | num_nnz () const |
Number of nonzero elements in matrix. | |
size_t | num_entries () const |
Alias for num_nnz . | |
const Vector< Index > & | row_offsets () const |
Read row_offsets vector. | |
const Vector< Index > & | column_indices () const |
Read column indices vector. | |
const Vector< Value > & | values () const |
Read values vector. | |
Vector< Value > & | values () |
Change values vector. | |
SparseMatrix | transpose () const |
Transposition. | |
template<class OtherMatrix = SparseMatrix> | |
std::enable_if_t< dg::has_policy_v< OtherMatrix, SerialTag >, bool > | operator!= (const SparseMatrix &rhs) const |
Two Matrices are considered equal if elements and sparsity pattern are equal. | |
template<class OtherMatrix = SparseMatrix> | |
std::enable_if_t< dg::has_policy_v< OtherMatrix, SerialTag >, bool > | operator== (const SparseMatrix &rhs) const |
Two Matrices are considered equal if elements and sparsity pattern are equal. | |
SparseMatrix | operator- () const |
Negate. | |
template<class OtherMatrix = SparseMatrix> | |
enable_if_serial< OtherMatrix > & | operator+= (const SparseMatrix &op) |
Add. | |
template<class OtherMatrix = SparseMatrix> | |
enable_if_serial< OtherMatrix > & | operator-= (const SparseMatrix &op) |
subtract | |
SparseMatrix & | operator*= (const Value &value) |
scalar multiply | |
Friends | |
template<class I , class V , template< class > class Vec> | |
class | SparseMatrix |
Enable copy constructor from foreign types. | |
template<class OtherMatrix = SparseMatrix> | |
enable_if_serial< OtherMatrix > | operator+ (const SparseMatrix &lhs, const SparseMatrix &rhs) |
add | |
template<class OtherMatrix = SparseMatrix> | |
enable_if_serial< OtherMatrix > | operator- (const SparseMatrix &lhs, const SparseMatrix &rhs) |
subtract | |
template<class OtherMatrix = SparseMatrix> | |
enable_if_serial< OtherMatrix > | operator* (const Value &value, const SparseMatrix &rhs) |
scalar multiplication | |
template<class OtherMatrix = SparseMatrix> | |
enable_if_serial< OtherMatrix > | operator* (const SparseMatrix &lhs, const Value &value) |
scalar multiplication | |
template<class OtherMatrix = SparseMatrix> | |
enable_if_serial< OtherMatrix > | operator* (const SparseMatrix &lhs, const SparseMatrix &rhs) |
matrix-matrix multiplication \( C = A*B\) | |
template<class ContainerType , class = std::enable_if_t < dg::has_policy_v<ContainerType, policy> and dg::is_vector_v<ContainerType> >> | |
ContainerType | operator* (const SparseMatrix &S, const ContainerType &x) |
matrix-vector multiplication \( y = S x\) | |
template<class Ostream , class OtherMatrix = SparseMatrix> | |
std::enable_if_t< dg::has_policy_v< OtherMatrix, SerialTag >, Ostream > & | operator<< (Ostream &os, const SparseMatrix &mat) |
puts a matrix linewise in output stream | |
A CSR formatted sparse matrix.
This class was designed to replace our dependency on cusp::csr_matrix
. On the host arithmetic operators like + and * are overloaded allowing for expressive code to concisely assemble the matrix:
On the device like OpenMP or GPU the only currently allowed operations are transpose
and the matrix-vector multiplications through dg::blas2::symv
(and its aliases dg::blas2::gemv
and dg::apply
). We use the cusparse
library to dispatch matrix-vector multiplication on the GPU.
Index | The index type (on GPU must be either int or long ) |
Value | The value type (on GPU must be either float or double ) |
Vector | The vector class to store indices and values (typically thrust::host_vector or thrust::device_vector ) |
using dg::SparseMatrix< Index, Value, Vector >::enable_if_serial = std::enable_if_t<std::is_same_v<typename OtherMatrix::policy, SerialTag>, OtherMatrix> |
A static guard to allow certain members only on host (for serial execution)
using dg::SparseMatrix< Index, Value, Vector >::index_type = Index |
The index type (on GPU must be either int
or long
)
using dg::SparseMatrix< Index, Value, Vector >::policy = dg::get_execution_policy<Vector<Value>> |
Execution policy of the matrix.
using dg::SparseMatrix< Index, Value, Vector >::value_type = Value |
The value type (on GPU must be either float
or double
)
|
default |
Empty matrix.
Set values using either setFromCoo
or set
members
|
inline |
Directly construct from given vectors.
num_rows | Number of rows in the matrix |
num_cols | Number of columns in the matrix |
row_offsets | Vector of size num_rows+1 representing the starting position of each row in the column_indices and values arrays. row_offsets.back() equals the number of non-zero elements in the matrix |
column_indices | Vector of size row_offsets.back() containing column indices. The column indices may be unsorted within each row and should be unique. If duplicates exist symv may not be correct (from cusparse). |
values | Vector of size row_offsets.back() containing nonzero values |
|
inline |
Copy construct from differen type.
Typically used to transfer a matrix from host to device
src | Copy the matrix |
|
inline |
Read column indices vector.
|
inline |
Number of columns in matrix.
|
inline |
Alias for num_nnz
.
|
inline |
Number of nonzero elements in matrix.
|
inline |
Number of rows in matrix.
|
inline |
Alias for num_nnz
.
|
inline |
Two Matrices are considered equal if elements and sparsity pattern are equal.
rhs | Matrix to be compared to this |
|
inline |
scalar multiply
value |
|
inline |
Add.
op |
|
inline |
Negate.
|
inline |
subtract
op |
|
inline |
Two Matrices are considered equal if elements and sparsity pattern are equal.
rhs | Matrix to be compared to this |
|
inline |
Read row_offsets vector.
|
inline |
Set csr values directly.
num_rows | Number of rows in the matrix |
num_cols | Number of columns in the matrix |
row_offsets | Vector of size num_rows+1 representing the starting position of each row in the column_indices and values arrays. row_offsets.back() equals the number of non-zero elements in the matrix |
column_indices | Vector of size row_offsets.back() containing column indices. The column indices may be unsorted within each row and should be unique. If duplicates exist symv may not be correct (from cusparse). |
values | Vector of size row_offsets.back() containing nonzero values |
sort | If true the given vectors are sorted by column_indices in each row. It is allowed to have unsorted column indices in each row so sorting is not strictly necessary. It may (or may not) have an effect on performance. |
|
inline |
Set csr values from coo formatted sparse matrix.
sort
parameter should be set to true
; if duplicates exist symv may not be correct (from cusparse)num_rows | Number of rows in the matrix |
num_cols | Number of columns in the matrix |
row_indices | Contains row indices of the corresponding elements in the values vector |
column_indices | Vector of size row_indices.size()() containing column indices. |
values | Vector of size row_indices.size() containing nonzero values |
sort | If true the given vectors are sorted by row and column before converted to the csr format. Per default we assume that the values are sorted by row (not necessarily by column). In this case there is no need to sort the indices, which may be somewhat expensive. If the row indices are unsorted however, sort must be set to true otherwise the coo2csr conversion will fail. |
|
inline |
Sort by row and column.
Sort is somewhat expensive and it is allowed to have unsorted column indices (at least in cusparse). Sorting may or may not have an influence on performance. Never sort a stencil for dg::blas2::stencil
.
|
inline |
Alias for num_cols
.
|
inline |
Alias for num_rows
.
|
inline |
Transposition.
The transpose is sorted even if the original is not
|
inline |
Change values vector.
values
can be changed directly while row_offsets
and column_indices
cannot, is that changing the values does not influence the performance cache, while changing the sparsity pattern through row_offsets
and column_indices
does
|
inline |
Read values vector.
|
friend |
matrix-matrix multiplication \( C = A*B\)
lhs | |
rhs |
|
friend |
scalar multiplication
Simply multiply values array by given value
lhs | |
value |
|
friend |
matrix-vector multiplication \( y = S x\)
Works if ContainerType has the same execution policy as the SparseMatrix
S | Matrix |
x | Vector |
|
friend |
scalar multiplication
Simply multiply values array by given value
value | |
rhs |
|
friend |
add
lhs | |
rhs |
|
friend |
subtract
lhs | |
rhs |
|
friend |
puts a matrix linewise in output stream
Ostream | The stream e.g. std::cout |
os | the outstream |
mat | the matrix to output |
|
friend |
Enable copy constructor from foreign types.