33 #ifndef __H__UG__CPU_ALGEBRA__GPUSparseMatrix__
34 #define __H__UG__CPU_ALGEBRA__GPUSparseMatrix__
40 #include "../algebra_common/sparsematrix_util.h"
45 #include "../algebra_common/connection.h"
46 #include "../algebra_common/matrixrow.h"
47 #include "../common/operations_mat/operations_mat.h"
52 #define PROFILE_GPUMATRIX(name) PROFILE_BEGIN_GROUP(name, "GPUSparseMatrix algebra")
100 template<
typename TValueType>
160 template<
typename vector_t>
161 bool axpy(vector_t &dest,
162 const number &alpha1,
const vector_t &v1,
163 const number &beta1,
const vector_t &w1)
const;
166 template<
typename vector_t>
167 void axpy(
double alpha, vector_t &x,
double beta,
const vector_t &y)
const;
171 template<
typename vector_t>
173 const number &alpha1,
const vector_t &v1,
174 const number &beta1,
const vector_t &w1)
const;
177 template<
typename vector_t>
179 const number &beta1,
const vector_t &w1)
const { assert(0); }
182 template<
typename vector_t>
184 const number &beta1,
const vector_t &w1)
const { assert(0); }
191 template<
typename Vector_type>
192 bool apply(Vector_type &res,
const Vector_type &x)
const
194 return axpy(res, 0.0, res, 1.0, x);
199 template<
typename Vector_type>
207 template<
typename Vector_type>
210 return axpy(res, 1.0, res, -1.0, x);
222 bool scale(
double d);
240 void add(
const M &mat);
243 void set(
const M &mat);
246 void get(M &mat)
const;
330 template<
typename vector_t>
534 void print(
const char *
const name = NULL)
const;
542 out <<
"GPUSparseMatrix "
618 cusparseStatus_t cusparseStatus = cusparseCreateMatDescr(&
descr);
620 if (checkCudaErrors(cusparseStatus))
625 cusparseSetMatType(
descr,CUSPARSE_MATRIX_TYPE_GENERAL);
626 cusparseSetMatIndexBase(
descr,CUSPARSE_INDEX_BASE_ZERO);
633 UG_LOG(
"cols.size = " <<
cols.size()*
sizeof(
int) <<
" values.size() == " <<
values.size()*
sizeof(
value_type) <<
" rowStart.size = " <<
rowStart.size()*
sizeof(
int) <<
"\n");
635 UG_LOG(
"gpusparsematrix.h:"<<__LINE__ <<
"\n")
637 UG_LOG(
"gpusparsematrix.h:"<<__LINE__ <<
"\n")
639 UG_LOG(
"gpusparsematrix.h:"<<__LINE__ <<
"\n")
672 template<
typename vector_t,
typename matrix_t>
674 const number &alpha1,
const vector_t &v1,
location name
Definition: checkpoint_util.lua:128
Definition: connection.h:40
static CUDAManager & get_instance()
Definition: cuda_manager.cpp:153
Definition: matrixrow.h:118
Definition: gpusparsematrix.h:400
const_row_iterator(const GPUSparseMatrix &_A, size_t _row, size_t _i)
Definition: gpusparsematrix.h:406
size_t row
Definition: gpusparsematrix.h:402
void check() const
Definition: gpusparsematrix.h:405
const GPUSparseMatrix & A
Definition: gpusparsematrix.h:401
bool operator!=(const const_row_iterator &o) const
Definition: gpusparsematrix.h:411
bool operator==(const const_row_iterator &other) const
Definition: gpusparsematrix.h:414
size_t i
Definition: gpusparsematrix.h:403
void operator++()
Definition: gpusparsematrix.h:412
~const_row_iterator()
Definition: gpusparsematrix.h:407
const value_type & value() const
Definition: gpusparsematrix.h:409
size_t index() const
Definition: gpusparsematrix.h:410
void operator+=(int nr)
Definition: gpusparsematrix.h:413
const_row_iterator * operator->()
Definition: gpusparsematrix.h:408
Definition: gpusparsematrix.h:383
~row_iterator()
Definition: gpusparsematrix.h:390
void operator+=(int nr)
Definition: gpusparsematrix.h:396
size_t i
Definition: gpusparsematrix.h:386
GPUSparseMatrix & A
Definition: gpusparsematrix.h:384
void operator++()
Definition: gpusparsematrix.h:395
size_t row
Definition: gpusparsematrix.h:385
row_iterator(GPUSparseMatrix &_A, size_t _row, size_t _i)
Definition: gpusparsematrix.h:389
bool operator==(const row_iterator &other) const
Definition: gpusparsematrix.h:397
size_t index() const
Definition: gpusparsematrix.h:393
void check() const
Definition: gpusparsematrix.h:388
row_iterator * operator->()
Definition: gpusparsematrix.h:391
value_type & value()
Definition: gpusparsematrix.h:392
bool operator!=(const row_iterator &o) const
Definition: gpusparsematrix.h:394
sparse matrix for big, variable sparse matrices.
Definition: gpusparsematrix.h:102
row_iterator get_iterator_or_next(size_t r, size_t c)
Definition: gpusparsematrix.h:445
void check_device() const
Definition: gpusparsematrix.h:645
const value_type & operator()(size_t r, size_t c) const
Definition: gpusparsematrix.h:268
const_row_iterator begin_row(size_t r) const
Definition: gpusparsematrix.h:422
GPUSparseMatrix< value_type > & operator=(const GPUSparseMatrix< value_type > &B)
Definition: gpusparsematrix.h:151
bool is_isolated(size_t i) const
check for isolated condition of an index
Definition: gpusparsematrix_impl.h:218
void check_rc(size_t r, size_t c) const
Definition: gpusparsematrix.h:253
bool resize_and_keep_values(size_t newRows, size_t newCols)
Definition: gpusparsematrix_impl.h:84
int * d_rowStart
Definition: gpusparsematrix.h:655
bool matmul_minus(Vector_type &res, const Vector_type &x) const
calculate res -= A x
Definition: gpusparsematrix.h:208
void remove_iterator() const
Definition: gpusparsematrix.h:364
row_iterator end_row(size_t r)
Definition: gpusparsematrix.h:421
cusparseMatDescr_t get_matrix_descr() const
Definition: gpusparsematrix.h:612
size_t get_nnz() const
Definition: gpusparsematrix.h:558
double * d_values
Definition: gpusparsematrix.h:656
void set(const M &mat)
set local matrix
Definition: gpusparsematrix_impl.h:320
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.
Definition: gpusparsematrix_impl.h:113
std::vector< int > rowStart
Definition: gpusparsematrix.h:574
void defragment()
Definition: gpusparsematrix.h:524
std::vector< int > rowMax
Definition: gpusparsematrix.h:576
const int * get_device_cols() const
Definition: gpusparsematrix.h:609
ConstMatrixRow< this_type > const_row_type
Definition: gpusparsematrix.h:112
void add_matrix_row(size_t row, connection *c, size_t nr)
Definition: gpusparsematrix_impl.h:264
row_iterator begin_row(size_t r)
Definition: gpusparsematrix.h:420
std::vector< int > cols
Definition: gpusparsematrix.h:577
std::vector< value_type > values
Definition: gpusparsematrix.h:582
size_t num_rows() const
returns number of rows
Definition: gpusparsematrix.h:337
size_t num_connections(size_t i) const
returns number of connections of row row.
Definition: gpusparsematrix.h:323
GPUSparseMatrix< value_type > & operator*=(double d)
Definition: gpusparsematrix.h:223
void assureValuesSize(size_t s)
Definition: gpusparsematrix_impl.h:555
int get_nnz_max_cols(size_t maxCols)
Definition: gpusparsematrix_impl.h:565
TValueType value_type
Definition: gpusparsematrix.h:104
int get_index_internal(size_t row, int col) const
Definition: gpusparsematrix_impl.h:350
virtual ~GPUSparseMatrix()
destructor
Definition: gpusparsematrix.h:121
bool apply_transposed(Vector_type &res, const Vector_type &x) const
calculate res = A.T x
Definition: gpusparsematrix.h:200
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
Definition: gpusparsematrix.h:183
bool set_as_copy_of(const GPUSparseMatrix< value_type > &B, double scale=1.0)
create/recreate this as a copy of GPUSparseMatrix B
Definition: gpusparsematrix_impl.h:273
void set_matrix_row(size_t row, connection *c, size_t nr)
Definition: gpusparsematrix_impl.h:230
const_row_iterator end_row(size_t r) const
Definition: gpusparsematrix.h:423
MatrixRow< this_type > row_type
Definition: gpusparsematrix.h:111
row_type get_row(size_t r)
Definition: gpusparsematrix.h:425
void pr(size_t row) const
Definition: gpusparsematrix.h:549
GPUSparseMatrix< value_type > this_type
Definition: gpusparsematrix.h:107
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)
Definition: gpusparsematrix_impl.h:170
std::vector< int > rowEnd
Definition: gpusparsematrix.h:575
bool scale(double d)
Definition: gpusparsematrix_impl.h:286
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
Definition: gpusparsematrix.h:178
int get_index_const(int r, int c) const
Definition: gpusparsematrix_impl.h:381
const_row_iterator get_connection(size_t r, size_t c, bool &bFound) const
Definition: gpusparsematrix.h:463
cusparseMatDescr_t descr
Definition: gpusparsematrix.h:657
const_row_iterator get_connection(size_t r, size_t c) const
Definition: gpusparsematrix.h:504
bool bNeedsValues
Definition: gpusparsematrix.h:580
void freeGPU()
Definition: gpusparsematrix.h:602
GPUSparseMatrix(GPUSparseMatrix &)
disallow copy operator
int iIterators
Definition: gpusparsematrix.h:585
void check_fragmentation() const
Definition: gpusparsematrix_impl.h:548
void add(const M &mat)
Definition: gpusparsematrix_impl.h:304
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;
Definition: gpusparsematrix_impl.h:149
bool has_connection(size_t r, size_t c) const
Definition: gpusparsematrix.h:432
size_t nnz
Definition: gpusparsematrix.h:579
void copyToNewSize(size_t newSize)
Definition: gpusparsematrix.h:564
void print_to_file(const char *filename) const
friend std::ostream & operator<<(std::ostream &out, const GPUSparseMatrix &m)
Definition: gpusparsematrix.h:540
void get(M &mat) const
get local matrix
Definition: gpusparsematrix_impl.h:335
size_t total_num_connections() const
returns the total number of connections
Definition: gpusparsematrix.h:343
@ rows_sorted
Definition: gpusparsematrix.h:105
size_t fragmented
Definition: gpusparsematrix.h:578
const int * get_device_rowStart() const
Definition: gpusparsematrix.h:610
int maxValues
Definition: gpusparsematrix.h:583
void add_iterator() const
Definition: gpusparsematrix.h:360
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)
Definition: gpusparsematrix_impl.h:190
void initGPU()
Definition: gpusparsematrix.h:595
row_iterator get_connection(size_t r, size_t c, bool &bFound)
Definition: gpusparsematrix.h:483
GPUSparseMatrix()
constructor for empty GPUSparseMatrix
Definition: gpusparsematrix_impl.h:52
row_iterator get_connection(size_t r, size_t c)
Definition: gpusparsematrix.h:515
bool apply(Vector_type &res, const Vector_type &x) const
calculate res = A x
Definition: gpusparsematrix.h:192
int * d_cols
Definition: gpusparsematrix.h:655
void p() const
Definition: gpusparsematrix.h:548
const_row_type get_row(size_t r) const
Definition: gpusparsematrix.h:426
int m_numCols
Definition: gpusparsematrix.h:584
void copy_to_device()
Definition: gpusparsematrix.h:614
void check_row(size_t row, int i) const
Definition: gpusparsematrix.h:373
bool resize_and_clear(size_t newRows, size_t newCols)
resizes the GPUSparseMatrix
Definition: gpusparsematrix_impl.h:66
AlgebraicConnection< TValueType > connection
Definition: gpusparsematrix.h:110
bool bOnDevice
Definition: gpusparsematrix.h:659
const double * get_device_value_ptr() const
Definition: gpusparsematrix.h:611
size_t num_cols() const
returns the number of cols
Definition: gpusparsematrix.h:340
int get_index(int r, int c)
Definition: gpusparsematrix_impl.h:393
Definition: matrixrow.h:65
void printtype() const
Definition: gpusparsematrix_print.h:72
void print(const char *const name=NULL) const
Definition: gpusparsematrix_print.h:47
void printrow(size_t row) const
Definition: gpusparsematrix_print.h:58
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
#define UG_LOG(msg)
Definition: log.h:367
double number
Definition: types.h:124
bool MatMultTransposedAdd(vector_t &dest, const number &alpha1, const vector_t &v1, const number &beta1, const matrix_t &A1, const vector_t &w1)
calculates dest = alpha1*v1 + beta1 * A1 *w1;
Definition: operations_mat.h:121
@ MATRIX_USE_ROW_FUNCTIONS
Definition: matrix_algebra_types.h:68
T::value_type * CudaCreateAndCopyToDevice(T &vec)
Definition: cuda_manager.h:163
T value_type
Definition: sparsematrix_interface.h:2
Definition: matrix_algebra_types.h:79
static const int type
Definition: matrix_algebra_types.h:80