18 template <
typename Float>
class vector;
19 template <
typename TYPE,
typename Float>
class view1D;
21 template <
typename Float>
class tensor_Dense;
22 template <
typename Float>
class tensor_COO;
25 template <
typename Float>
class Dense;
26 template <
typename Float>
class COO;
39 template <
typename Float>
class CRS {
59 mutable std::shared_ptr<bool>
gpu_status = std::make_shared<bool>(
false);
76 std::shared_ptr<Float>
val;
117 CRS(
const size_t M,
const size_t N,
const size_t NNZ);
134 CRS(
const size_t M,
const size_t N,
const size_t NNZ,
const int *rowptr,
135 const int *colind,
const Float *value);
154 CRS(
const size_t M,
const size_t N,
const size_t NNZ,
const int *rowptr,
155 const int *colind,
const Float *value,
const size_t origin);
171 CRS(
const size_t M,
const size_t N,
const std::vector<int> &rowptr,
172 const std::vector<int> &colind,
const std::vector<Float> &value);
188 CRS(
const size_t M,
const size_t N,
const std::vector<int> &rowptr,
265 void set_ptr(
const size_t M,
const size_t N,
const std::vector<int> &rowptr,
266 const std::vector<int> &colind,
const std::vector<Float> &value);
282 void set_ptr(
const size_t M,
const size_t N,
const std::vector<int> &rowptr,
283 const std::vector<int> &colind,
const size_t vsize,
300 void set_ptr(
const size_t M,
const size_t N,
const std::vector<int> &rowptr,
301 const std::vector<int> &colind,
const size_t vsize,
391 [[nodiscard]] std::string
type()
const {
return "CRS"; }
482 [[nodiscard]]
const Float *
data()
const {
return val.get(); }
490 [[nodiscard]] Float *
data() {
return val.get(); }
502 throw std::runtime_error(
"Error, GPU matrix cant use resize");
505 std::shared_ptr<Float> tmp(
new Float[N], std::default_delete<Float[]>());
507 for (
size_t i = 0; i < copy_size; ++i) {
508 tmp.get()[i] =
data()[i];
510 for (
size_t i = copy_size; i < N; ++i) {
517 throw std::runtime_error(
"Error, not create vector cant use resize");
527 [[nodiscard]]
const Float *
begin()
const {
return data(); }
752 return (
get_nnz() *
sizeof(Float) + (
get_row() + 1) *
sizeof(
int) +
790 throw std::runtime_error(
"Error, GPU vector cant use operator[]");
806 bool compare_cpu_and_device =
false)
const;
Coodinate (COO) format Matrix (need to sort)
Compressed Row Storage (CRS) format Matrix.
void diag_mul(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
std::shared_ptr< bool > gpu_status
# of non-zero element
bool equal(const CRS< Float > &mat, bool compare_cpu_and_device=false) const
Comparing matrices (A == mat)
std::shared_ptr< bool > get_gpu_status() const
gpu status shared pointer
void set_col(const size_t M)
Set column number.
CRS(const CRS< Float > &mat)
Create CRS matrix from CRS matrix.
const Float * data() const
returns a direct pointer to the matrix
Float * data()
returns a direct pointer to the matrix
void diag_mul(const Float alpha)
Scalar and diag. vector of CRS format matrix mul.
size_t get_col() const
get # of col
CRS(const size_t M, const size_t N, const size_t NNZ)
declare CRS matrix
void diag(vector< Float > &vec) const
get diag. vector
void diag_mul(const view1D< matrix::Dense< Float >, Float > &vec)
CRS(const size_t M, const size_t N, const size_t NNZ, const int *rowptr, const int *colind, const Float *value, const size_t origin)
Create CRS matrix from array, also compute the hash.
Float * end()
returns a end iterator
size_t first
first position of data array
size_t val_nnz
# of non-zero element (M * N)
CRS(COO< Float > &coo)
Create CRS matrix from COO matrix, also compute the hash.
void diag_mul(const vector< Float > &vec)
Vector and diag. vector of CRS format matrix mul.
void set_ptr(const size_t M, const size_t N, const std::vector< int > &rowptr, const std::vector< int > &colind, const size_t vsize, const Float *value)
Set CRS array from std::vector.
double get_data_size() const
void diag_sub(const Float alpha)
Scalar and diag. vector of CRS format matrix sub.
void compute_hash()
compute index array hash (to compare structure)
void diag_sub(const view1D< vector< Float >, Float > &vec)
CRS(const size_t M, const size_t N, const size_t NNZ, const int *rowptr, const int *colind, const Float *value)
Create CRS matrix from array, also compute the hash.
void row(const size_t r, view1D< matrix::Dense< Float >, Float > &vec) const
size_t alloc_nnz
alloced matrix size
bool operator!=(const CRS< Float > &mat) const
Comparing matrices (A != mat)
void diag_add(const view1D< vector< Float >, Float > &vec)
void fill(Float value)
fill matrix elements with a scalar value
size_t get_alloc_nnz() const
get # of alloced non-zeros
void row(const size_t r, view1D< tensor::tensor_Dense< Float >, Float > &vec) const
bool val_create_flag
matrix create flag;
size_t get_hash() const
get index array hash (to compare structure)
void diag_add(const view1D< matrix::Dense< Float >, Float > &vec)
bool operator==(const CRS< Float > &mat) const
Comparing matrices (A == mat)
void set_row(const size_t N)
Set row number.
void send() const
send data to GPU
size_t get_first() const
get first position
void convert(CRS< Float > &crs)
Convert CRS matrix from COO matrix.
void print_all(bool force_cpu=false) const
print all elements to standard I/O
void resize(size_t N, Float Val=0)
resize matrix value
void col(const size_t c, vector< Float > &vec) const
get column vector
void diag_sub(const vector< Float > &vec)
Vector and diag. vector of CRS format matrix sub.
void operator=(const CRS< Float > &mat)
matrix copy
void transpose(const CRS &B)
create transposed matrix from CRS format matrix (B = A^T)
CRS(const CRS< Float > &mat, Float value)
Create CRS matrix of the same size as input matrix.
std::shared_ptr< Float > val
CRS format value (pointer), which stores values of the non-zero elements.
size_t get_offset() const
get first position (same as get_first())
size_t get_nnz() const
get # of non-zeros
size_t structure_hash
hash, created from row_ptr and col_ind
void col(const size_t c, view1D< vector< Float >, Float > &vec) const
void diag_div(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
const Float * end() const
returns a end iterator
void diag(view1D< vector< Float >, Float > &vec) const
void diag(view1D< tensor::tensor_Dense< Float >, Float > &vec) const
void diag(view1D< matrix::Dense< Float >, Float > &vec) const
size_t get_row() const
get # of row
std::vector< int > col_ind
CRS format column index, which stores column numbers of the non-zero elements (size nnz)
void diag_sub(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
~CRS()
destructor of CRS matrix, free GPU memory
const Float * begin() const
returns a begin iterator
std::string type() const
get format name "CRS"
void diag_add(const view1D< tensor::tensor_Dense< Float >, Float > &vec)
void nonfree_recv()
recv. data to GPU (w/o free)
void convert(COO< Float > &coo)
Convert CRS matrix from COO matrix, also compute the hash.
void diag_add(const Float alpha)
Scalar and diag. vector of CRS format matrix add.
void col(const size_t c, view1D< tensor::tensor_Dense< Float >, Float > &vec) const
void transpose()
get transposed matrix (A^T)
void device_free() const
free data on GPU
void row(const size_t r, view1D< vector< Float >, Float > &vec) const
void diag_add(const vector< Float > &vec)
Vector and diag. vector of CRS format matrix add.
void diag_div(const view1D< matrix::Dense< Float >, Float > &vec)
void diag_div(const Float alpha)
Scalar and diag. vector of CRS format matrix div.
void diag_mul(const view1D< vector< Float >, Float > &vec)
Float * begin()
returns a begin iterator
void recv()
recv. data to GPU, and free data on GPU
Float & operator[](size_t i)
reference to the element at position (v[i])
void row(const size_t r, vector< Float > &vec) const
get row vector
void diag_div(const vector< Float > &vec)
Vector and diag. vector of CRS format matrix div.
void set_ptr(const size_t M, const size_t N, const std::vector< int > &rowptr, const std::vector< int > &colind, const std::vector< Float > &value)
Set CRS array from std::vector.
void col(const size_t c, view1D< matrix::Dense< Float >, Float > &vec) const
std::vector< int > row_ptr
CRS format row pointer, which stores the starting points of the rows of the arrays value and col_ind ...
CRS(const size_t M, const size_t N, const std::vector< int > &rowptr, const std::vector< int > &colind, const std::vector< Float > &value)
Create CRS matrix from std::vector, also compute the hash.
bool get_device_mem_stat() const
true: sended, false: not send
void diag_sub(const view1D< matrix::Dense< Float >, Float > &vec)
CRS(const size_t M, const size_t N, const std::vector< int > &rowptr, const std::vector< int > &colind, const vector< Float > &value)
Create CRS matrix from std::vector, also compute the hash.
void diag_div(const view1D< vector< Float >, Float > &vec)
void set_ptr(const size_t M, const size_t N, const std::vector< int > &rowptr, const std::vector< int > &colind, const size_t vsize, const Float value)
Set CRS array from std::vector.
void min(const matrix::CRS< double > &A, const matrix::CRS< double > &B, matrix::CRS< double > &C)
Create a new CRS matrix with smallest elements of two matrices (C[0:nnz] = min(A[0:nnz],...