ug4
ug::GPUSparseMatrix< TValueType > Class Template Reference

sparse matrix for big, variable sparse matrices. More...

#include <gpusparsematrix.h>

Classes

class  const_row_iterator
 
class  row_iterator
 

Public Types

enum  { rows_sorted =true }
 
typedef AlgebraicConnection< TValueType > connection
 
typedef ConstMatrixRow< this_typeconst_row_type
 
typedef MatrixRow< this_typerow_type
 
typedef GPUSparseMatrix< value_typethis_type
 
typedef TValueType value_type
 

Public Member Functions

template<typename M >
void add (const M &mat)
 
void add_iterator () const
 
void add_matrix_row (size_t row, connection *c, size_t nr)
 
template<typename Vector_type >
bool apply (Vector_type &res, const Vector_type &x) const
 calculate res = A x More...
 
template<typename vector_t >
void apply_ignore_zero_rows (vector_t &dest, const number &beta1, const vector_t &w1) const
 calculated dest = beta1*A*w1 . For empty rows, dest will not be changed More...
 
template<typename Vector_type >
bool apply_transposed (Vector_type &res, const Vector_type &x) const
 calculate res = A.T x More...
 
template<typename vector_t >
void apply_transposed_ignore_zero_rows (vector_t &dest, const number &beta1, const vector_t &w1) const
 calculated dest = beta1*A*w1 . For empty cols of A (=empty rows of A^T), dest will not be changed More...
 
template<typename vector_t >
void axpy (double alpha, vector_t &x, double beta, const vector_t &y) const
 calculate x = alpha*x + beta*A*y (A = this matrix) More...
 
template<typename vector_t >
bool axpy (vector_t &dest, const number &alpha1, const vector_t &v1, const number &beta1, const vector_t &w1) const
 calculate dest = alpha1*v1 + beta1*A*w1 (A = this matrix) More...
 
template<typename vector_t >
bool axpy_transposed (vector_t &dest, const number &alpha1, const vector_t &v1, const number &beta1, const vector_t &w1) const
 calculate dest = alpha1*v1 + beta1*A^T*w1 (A = this matrix) More...
 
row_iterator begin_row (size_t r)
 
const_row_iterator begin_row (size_t r) const
 
void check_device () const
 
void check_rc (size_t r, size_t c) const
 
void check_row (size_t row, int i) const
 
void copy_to_device ()
 
void defragment ()
 
row_iterator end_row (size_t r)
 
const_row_iterator end_row (size_t r) const
 
void freeGPU ()
 
template<typename M >
void get (M &mat) const
 get local matrix More...
 
row_iterator get_connection (size_t r, size_t c)
 
const_row_iterator get_connection (size_t r, size_t c) const
 
row_iterator get_connection (size_t r, size_t c, bool &bFound)
 
const_row_iterator get_connection (size_t r, size_t c, bool &bFound) const
 
const int * get_device_cols () const
 
const int * get_device_rowStart () const
 
const double * get_device_value_ptr () const
 
row_iterator get_iterator_or_next (size_t r, size_t c)
 
cusparseMatDescr_t get_matrix_descr () const
 
row_type get_row (size_t r)
 
const_row_type get_row (size_t r) const
 
 GPUSparseMatrix ()
 constructor for empty GPUSparseMatrix More...
 
bool has_connection (size_t r, size_t c) const
 
void initGPU ()
 
bool is_isolated (size_t i) const
 check for isolated condition of an index More...
 
template<typename vector_t >
void mat_mult_add_row (size_t row, typename vector_t::value_type &dest, double alpha, const vector_t &v) const
 calculates dest += alpha * A[row, .] v; More...
 
template<typename Vector_type >
bool matmul_minus (Vector_type &res, const Vector_type &x) const
 calculate res -= A x More...
 
size_t num_cols () const
 returns the number of cols More...
 
size_t num_connections (size_t i) const
 returns number of connections of row row. More...
 
size_t num_rows () const
 returns number of rows More...
 
value_typeoperator() (size_t r, size_t c)
 
const value_typeoperator() (size_t r, size_t c) const
 
GPUSparseMatrix< value_type > & operator*= (double d)
 
GPUSparseMatrix< value_type > & operator= (const GPUSparseMatrix< value_type > &B)
 
void p () const
 
void pr (size_t row) const
 
void print (const char *const name=NULL) const
 
void print_to_file (const char *filename) const
 
void printrow (size_t row) const
 
void printtype () const
 
void remove_iterator () const
 
bool resize_and_clear (size_t newRows, size_t newCols)
 resizes the GPUSparseMatrix More...
 
bool resize_and_keep_values (size_t newRows, size_t newCols)
 
bool scale (double d)
 
template<typename M >
void set (const M &mat)
 set local matrix More...
 
bool set (double a)
 set matrix to Id*a More...
 
bool set_as_copy_of (const GPUSparseMatrix< value_type > &B, double scale=1.0)
 create/recreate this as a copy of GPUSparseMatrix B More...
 
bool set_as_transpose_of (const GPUSparseMatrix< value_type > &B, double scale=1.0)
 write in a empty GPUSparseMatrix (this) the transpose GPUSparseMatrix of B. More...
 
void set_matrix_row (size_t row, connection *c, size_t nr)
 
size_t total_num_connections () const
 returns the total number of connections More...
 
virtual ~GPUSparseMatrix ()
 destructor More...
 

Protected Member Functions

void check_fragmentation () const
 
void copyToNewSize (size_t newSize)
 
void copyToNewSize (size_t newSize, size_t maxCols)
 
int get_index (int r, int c)
 
int get_index_const (int r, int c) const
 
int get_index_internal (size_t row, int col) const
 
int get_nnz_max_cols (size_t maxCols)
 

Protected Attributes

bool bNeedsValues
 
std::vector< int > cols
 
size_t fragmented
 
int iIterators
 
int m_numCols
 
int maxValues
 
size_t nnz
 
std::vector< int > rowEnd
 
std::vector< int > rowMax
 
std::vector< int > rowStart
 
std::vector< value_typevalues
 

Private Member Functions

void assureValuesSize (size_t s)
 
size_t get_nnz () const
 
 GPUSparseMatrix (GPUSparseMatrix &)
 disallow copy operator More...
 

Private Attributes

bool bOnDevice
 
int * d_cols
 
int * d_rowStart
 
double * d_values
 
cusparseMatDescr_t descr
 

Friends

std::ostream & operator<< (std::ostream &out, const GPUSparseMatrix &m)
 

Detailed Description

template<typename TValueType>
class ug::GPUSparseMatrix< TValueType >

sparse matrix for big, variable sparse matrices.

GPUSparseMatrix

matrix is stored independent row-wise When doing discretisation, use the add set and get methods for dealing with submatrices of A. For other things you can use the row iterators or operator()-methods.

See also
matrixrow, CreateAsMultiplyOf
Parameters
Tblocktype
Tblocktype

Member Typedef Documentation

◆ connection

template<typename TValueType >
typedef AlgebraicConnection<TValueType> ug::GPUSparseMatrix< TValueType >::connection

◆ const_row_type

template<typename TValueType >
typedef ConstMatrixRow<this_type> ug::GPUSparseMatrix< TValueType >::const_row_type

◆ row_type

template<typename TValueType >
typedef MatrixRow<this_type> ug::GPUSparseMatrix< TValueType >::row_type

◆ this_type

template<typename TValueType >
typedef GPUSparseMatrix<value_type> ug::GPUSparseMatrix< TValueType >::this_type

◆ value_type

template<typename TValueType >
typedef TValueType ug::GPUSparseMatrix< TValueType >::value_type

Member Enumeration Documentation

◆ anonymous enum

template<typename TValueType >
anonymous enum
Enumerator
rows_sorted 

Constructor & Destructor Documentation

◆ GPUSparseMatrix() [1/2]

template<typename T >
ug::GPUSparseMatrix< T >::GPUSparseMatrix

constructor for empty GPUSparseMatrix

References PROFILE_GPUMATRIX.

◆ ~GPUSparseMatrix()

template<typename TValueType >
virtual ug::GPUSparseMatrix< TValueType >::~GPUSparseMatrix ( )
inlinevirtual

◆ GPUSparseMatrix() [2/2]

template<typename TValueType >
ug::GPUSparseMatrix< TValueType >::GPUSparseMatrix ( GPUSparseMatrix< TValueType > &  )
private

disallow copy operator

Member Function Documentation

◆ add()

template<typename T >
template<typename M >
void ug::GPUSparseMatrix< T >::add ( const M &  mat)

Add a local matrix

The local matrix type must declare the following members:

◆ add_iterator()

◆ add_matrix_row()

template<typename T >
void ug::GPUSparseMatrix< T >::add_matrix_row ( size_t  row,
connection c,
size_t  nr 
)

adds the connections c to the matrixrow row. if c has a connection con with con.iIndex=i, and the matrix already has a connection (row, i), the function will set A(row,i) += con.dValue. otherwise the connection A(row, i) is created and set to con.dValue.

Parameters
rowrow to add to
cconnections ("row") to be added the row.
nrnumber of connections in array c.
Returns
true on success.
Note
you may use double connections in c.

References ug::AlgebraicConnection< T >::iIndex.

◆ apply()

template<typename TValueType >
template<typename Vector_type >
bool ug::GPUSparseMatrix< TValueType >::apply ( Vector_type &  res,
const Vector_type &  x 
) const
inline

calculate res = A x

References ug::GPUSparseMatrix< TValueType >::axpy().

◆ apply_ignore_zero_rows()

template<typename TValueType >
template<typename vector_t >
void ug::GPUSparseMatrix< TValueType >::apply_ignore_zero_rows ( vector_t &  dest,
const number beta1,
const vector_t &  w1 
) const
inline

calculated dest = beta1*A*w1 . For empty rows, dest will not be changed

◆ apply_transposed()

template<typename TValueType >
template<typename Vector_type >
bool ug::GPUSparseMatrix< TValueType >::apply_transposed ( Vector_type &  res,
const Vector_type &  x 
) const
inline

calculate res = A.T x

References ug::GPUSparseMatrix< TValueType >::axpy_transposed().

◆ apply_transposed_ignore_zero_rows()

template<typename TValueType >
template<typename vector_t >
void ug::GPUSparseMatrix< TValueType >::apply_transposed_ignore_zero_rows ( vector_t &  dest,
const number beta1,
const vector_t &  w1 
) const
inline

calculated dest = beta1*A*w1 . For empty cols of A (=empty rows of A^T), dest will not be changed

◆ assureValuesSize()

template<typename T >
void ug::GPUSparseMatrix< T >::assureValuesSize ( size_t  s)
private

References s.

◆ axpy() [1/2]

template<typename T >
template<typename vector_t >
void ug::GPUSparseMatrix< T >::axpy ( double  alpha,
vector_t &  x,
double  beta,
const vector_t &  y 
) const

calculate x = alpha*x + beta*A*y (A = this matrix)

References alpha, ug::CUDAManager::get_cusparseHandle(), num_cols(), and num_rows().

◆ axpy() [2/2]

template<typename T >
template<typename vector_t >
bool ug::GPUSparseMatrix< T >::axpy ( vector_t &  dest,
const number alpha1,
const vector_t &  v1,
const number beta1,
const vector_t &  w1 
) const

calculate dest = alpha1*v1 + beta1*A*w1 (A = this matrix)

References PROFILE_GPUMATRIX.

Referenced by ug::GPUSparseMatrix< TValueType >::apply(), and ug::GPUSparseMatrix< TValueType >::matmul_minus().

◆ axpy_transposed()

template<typename T >
template<typename vector_t >
bool ug::GPUSparseMatrix< T >::axpy_transposed ( vector_t &  dest,
const number alpha1,
const vector_t &  v1,
const number beta1,
const vector_t &  w1 
) const

calculate dest = alpha1*v1 + beta1*A^T*w1 (A = this matrix)

References PROFILE_GPUMATRIX, and UG_ASSERT.

Referenced by ug::GPUSparseMatrix< TValueType >::apply_transposed(), and ug::MatMultTransposedAdd().

◆ begin_row() [1/2]

◆ begin_row() [2/2]

template<typename TValueType >
const_row_iterator ug::GPUSparseMatrix< TValueType >::begin_row ( size_t  r) const
inline

◆ check_device()

◆ check_fragmentation()

template<typename T >
void ug::GPUSparseMatrix< T >::check_fragmentation
protected

◆ check_rc()

◆ check_row()

◆ copy_to_device()

◆ copyToNewSize() [1/2]

template<typename TValueType >
void ug::GPUSparseMatrix< TValueType >::copyToNewSize ( size_t  newSize)
inlineprotected

◆ copyToNewSize() [2/2]

template<typename T >
void ug::GPUSparseMatrix< T >::copyToNewSize ( size_t  newSize,
size_t  maxCols 
)
protected

◆ defragment()

◆ end_row() [1/2]

◆ end_row() [2/2]

template<typename TValueType >
const_row_iterator ug::GPUSparseMatrix< TValueType >::end_row ( size_t  r) const
inline

◆ freeGPU()

◆ get()

template<typename T >
template<typename M >
void ug::GPUSparseMatrix< T >::get ( M &  mat) const

get local matrix

See also
add

◆ get_connection() [1/4]

template<typename TValueType >
row_iterator ug::GPUSparseMatrix< TValueType >::get_connection ( size_t  r,
size_t  c 
)
inline
Parameters
rindex of the row
cindex of the column
Returns
a row_iterator to the connection A(r,c)
Remarks
creates connection if necessary.

References ug::GPUSparseMatrix< TValueType >::bNeedsValues, ug::GPUSparseMatrix< TValueType >::check_rc(), and ug::GPUSparseMatrix< TValueType >::get_index().

◆ get_connection() [2/4]

template<typename TValueType >
const_row_iterator ug::GPUSparseMatrix< TValueType >::get_connection ( size_t  r,
size_t  c 
) const
inline
Parameters
rindex of the row
cindex of the column
Returns
a const_row_iterator to the connection A(r,c) if existing, otherwise end_row(row)

References ug::GPUSparseMatrix< TValueType >::get_connection().

◆ get_connection() [3/4]

template<typename TValueType >
row_iterator ug::GPUSparseMatrix< TValueType >::get_connection ( size_t  r,
size_t  c,
bool &  bFound 
)
inline
Parameters
rindex of the row
cindex of the column
Returns
a row_iterator to the connection A(r,c) if existing, otherwise end_row(row)

References ug::GPUSparseMatrix< TValueType >::check_rc(), ug::GPUSparseMatrix< TValueType >::end_row(), and ug::GPUSparseMatrix< TValueType >::get_index_const().

◆ get_connection() [4/4]

template<typename TValueType >
const_row_iterator ug::GPUSparseMatrix< TValueType >::get_connection ( size_t  r,
size_t  c,
bool &  bFound 
) const
inline

◆ get_device_cols()

template<typename TValueType >
const int* ug::GPUSparseMatrix< TValueType >::get_device_cols ( ) const
inline

◆ get_device_rowStart()

template<typename TValueType >
const int* ug::GPUSparseMatrix< TValueType >::get_device_rowStart ( ) const
inline

◆ get_device_value_ptr()

template<typename TValueType >
const double* ug::GPUSparseMatrix< TValueType >::get_device_value_ptr ( ) const
inline

◆ get_index()

template<typename T >
int ug::GPUSparseMatrix< T >::get_index ( int  r,
int  c 
)
protected

◆ get_index_const()

template<typename T >
int ug::GPUSparseMatrix< T >::get_index_const ( int  r,
int  c 
) const
protected

◆ get_index_internal()

template<typename T >
int ug::GPUSparseMatrix< T >::get_index_internal ( size_t  row,
int  col 
) const
protected

◆ get_iterator_or_next()

template<typename TValueType >
row_iterator ug::GPUSparseMatrix< TValueType >::get_iterator_or_next ( size_t  r,
size_t  c 
)
inline

◆ get_matrix_descr()

template<typename TValueType >
cusparseMatDescr_t ug::GPUSparseMatrix< TValueType >::get_matrix_descr ( ) const
inline

◆ get_nnz()

template<typename TValueType >
size_t ug::GPUSparseMatrix< TValueType >::get_nnz ( ) const
inlineprivate

◆ get_nnz_max_cols()

template<typename T >
int ug::GPUSparseMatrix< T >::get_nnz_max_cols ( size_t  maxCols)
protected

References num_rows().

◆ get_row() [1/2]

template<typename TValueType >
row_type ug::GPUSparseMatrix< TValueType >::get_row ( size_t  r)
inline

◆ get_row() [2/2]

template<typename TValueType >
const_row_type ug::GPUSparseMatrix< TValueType >::get_row ( size_t  r) const
inline

◆ has_connection()

template<typename TValueType >
bool ug::GPUSparseMatrix< TValueType >::has_connection ( size_t  r,
size_t  c 
) const
inline

◆ initGPU()

◆ is_isolated()

template<typename T >
bool ug::GPUSparseMatrix< T >::is_isolated ( size_t  i) const
inline

check for isolated condition of an index

Parameters
i
Returns
true if only A[i,i] != 0.0

References begin_row(), end_row(), num_rows(), and UG_ASSERT.

◆ mat_mult_add_row()

template<typename T >
template<typename vector_t >
void ug::GPUSparseMatrix< T >::mat_mult_add_row ( size_t  row,
typename vector_t::value_type dest,
double  alpha,
const vector_t &  v 
) const
inline

calculates dest += alpha * A[row, .] v;

References alpha, begin_row(), end_row(), and ug::MatMultAdd().

◆ matmul_minus()

template<typename TValueType >
template<typename Vector_type >
bool ug::GPUSparseMatrix< TValueType >::matmul_minus ( Vector_type &  res,
const Vector_type &  x 
) const
inline

calculate res -= A x

References ug::GPUSparseMatrix< TValueType >::axpy().

◆ num_cols()

◆ num_connections()

template<typename TValueType >
size_t ug::GPUSparseMatrix< TValueType >::num_connections ( size_t  i) const
inline

returns number of connections of row row.

References ug::GPUSparseMatrix< TValueType >::rowEnd, and ug::GPUSparseMatrix< TValueType >::rowStart.

◆ num_rows()

◆ operator()() [1/2]

template<typename TValueType >
value_type& ug::GPUSparseMatrix< TValueType >::operator() ( size_t  r,
size_t  c 
)
inline

operator() (size_t r, size_t c) const access or create connection (r, c)

Parameters
rrow
ccolumn
Note
(r,c) is added to sparsity pattern if not already there use operator()(r,c,bConnectionFound) to prevent
Returns
SparseMat(r, c)=0.0 if connection created, otherwise SparseMat(r, c)

References ug::GPUSparseMatrix< TValueType >::check_rc(), ug::GPUSparseMatrix< TValueType >::cols, ug::GPUSparseMatrix< TValueType >::get_index(), ug::GPUSparseMatrix< TValueType >::rowEnd, ug::GPUSparseMatrix< TValueType >::rowStart, UG_ASSERT, and ug::GPUSparseMatrix< TValueType >::values.

◆ operator()() [2/2]

template<typename TValueType >
const value_type& ug::GPUSparseMatrix< TValueType >::operator() ( size_t  r,
size_t  c 
) const
inline

operator() (size_t r, size_t c) const access connection (r, c)

Parameters
rrow
ccolumn
Note
if connection (r, c) is not there, returns 0.0
Returns
SparseMat(r, c)

References ug::GPUSparseMatrix< TValueType >::check_rc(), ug::GPUSparseMatrix< TValueType >::cols, ug::GPUSparseMatrix< TValueType >::get_index_const(), ug::GPUSparseMatrix< TValueType >::rowEnd, ug::GPUSparseMatrix< TValueType >::rowStart, UG_ASSERT, and ug::GPUSparseMatrix< TValueType >::values.

◆ operator*=()

template<typename TValueType >
GPUSparseMatrix<value_type>& ug::GPUSparseMatrix< TValueType >::operator*= ( double  d)
inline

◆ operator=()

template<typename TValueType >
GPUSparseMatrix<value_type>& ug::GPUSparseMatrix< TValueType >::operator= ( const GPUSparseMatrix< value_type > &  B)
inline

◆ p()

template<typename TValueType >
void ug::GPUSparseMatrix< TValueType >::p ( ) const
inline

◆ pr()

template<typename TValueType >
void ug::GPUSparseMatrix< TValueType >::pr ( size_t  row) const
inline

◆ print_to_file()

template<typename TValueType >
void ug::GPUSparseMatrix< TValueType >::print_to_file ( const char *  filename) const

◆ remove_iterator()

◆ resize_and_clear()

template<typename T >
bool ug::GPUSparseMatrix< T >::resize_and_clear ( size_t  newRows,
size_t  newCols 
)

resizes the GPUSparseMatrix

Parameters
newRowsnew nr of rows
newColsnew nr of cols
Returns

References PROFILE_GPUMATRIX, and UG_LOG.

◆ resize_and_keep_values()

template<typename T >
bool ug::GPUSparseMatrix< T >::resize_and_keep_values ( size_t  newRows,
size_t  newCols 
)

References num_rows(), PROFILE_GPUMATRIX, and UG_LOG.

◆ scale()

template<typename T >
bool ug::GPUSparseMatrix< T >::scale ( double  d)

◆ set() [1/2]

template<typename T >
template<typename M >
void ug::GPUSparseMatrix< T >::set ( const M &  mat)

set local matrix

See also
add

◆ set() [2/2]

template<typename T >
bool ug::GPUSparseMatrix< T >::set ( double  a)

set matrix to Id*a

References begin_row(), end_row(), num_rows(), and PROFILE_GPUMATRIX.

◆ set_as_copy_of()

template<typename TValueType >
bool ug::GPUSparseMatrix< T >::set_as_copy_of ( const GPUSparseMatrix< value_type > &  B,
double  scale = 1.0 
)

create/recreate this as a copy of GPUSparseMatrix B

Parameters
Bthe matrix of which to create a copy of
scalean optional scaling
Returns
true on success

References ug::GPUSparseMatrix< TValueType >::begin_row(), ug::GPUSparseMatrix< TValueType >::end_row(), ug::GPUSparseMatrix< TValueType >::num_cols(), ug::GPUSparseMatrix< TValueType >::num_rows(), and operator()().

Referenced by ug::GPUSparseMatrix< TValueType >::operator=().

◆ set_as_transpose_of()

template<typename T >
bool ug::GPUSparseMatrix< T >::set_as_transpose_of ( const GPUSparseMatrix< value_type > &  B,
double  scale = 1.0 
)

write in a empty GPUSparseMatrix (this) the transpose GPUSparseMatrix of B.

Parameters
Bthe matrix of which to create the transpose of
scalean optional scaling
Returns
true on success

References ug::GPUSparseMatrix< TValueType >::begin_row(), ug::GPUSparseMatrix< TValueType >::end_row(), ug::GPUSparseMatrix< TValueType >::num_cols(), ug::GPUSparseMatrix< TValueType >::num_rows(), operator()(), and PROFILE_GPUMATRIX.

◆ set_matrix_row()

template<typename T >
void ug::GPUSparseMatrix< T >::set_matrix_row ( size_t  row,
connection c,
size_t  nr 
)

set a row of the matrix. all previous content in this row is destroyed (

See also
add_matrix_row).
Parameters
rowindex of the row to set
cpointer to a array of sorted connections of size nr
nrnumber of connections in c
Remarks
will sort array c

References ug::AlgebraicConnection< T >::iIndex.

◆ total_num_connections()

template<typename TValueType >
size_t ug::GPUSparseMatrix< TValueType >::total_num_connections ( ) const
inline

returns the total number of connections

References ug::GPUSparseMatrix< TValueType >::nnz.

Friends And Related Function Documentation

◆ operator<<

template<typename TValueType >
std::ostream& operator<< ( std::ostream &  out,
const GPUSparseMatrix< TValueType > &  m 
)
friend

Member Data Documentation

◆ bNeedsValues

template<typename TValueType >
bool ug::GPUSparseMatrix< TValueType >::bNeedsValues
protected

◆ bOnDevice

template<typename TValueType >
bool ug::GPUSparseMatrix< TValueType >::bOnDevice
private

◆ cols

◆ d_cols

◆ d_rowStart

◆ d_values

◆ descr

◆ fragmented

template<typename TValueType >
size_t ug::GPUSparseMatrix< TValueType >::fragmented
protected

◆ iIterators

template<typename TValueType >
int ug::GPUSparseMatrix< TValueType >::iIterators
mutableprotected

◆ m_numCols

template<typename TValueType >
int ug::GPUSparseMatrix< TValueType >::m_numCols
protected

◆ maxValues

template<typename TValueType >
int ug::GPUSparseMatrix< TValueType >::maxValues
protected

◆ nnz

◆ rowEnd

◆ rowMax

template<typename TValueType >
std::vector<int> ug::GPUSparseMatrix< TValueType >::rowMax
protected

◆ rowStart

◆ values


The documentation for this class was generated from the following files: