17 #define MM_BANNER "%%MatrixMarket"
18 #define MM_MAT "matrix"
19 #define MM_VEC "vector"
20 #define MM_FMT "coordinate"
21 #define MM_TYPE_REAL "real"
22 #define MM_TYPE_GENERAL "general"
23 #define MM_TYPE_SYMM "symmetric"
26 template <
typename Float>
class vector;
27 template <
typename TYPE,
typename Float>
class view1D;
32 template <
typename Float>
class Dense;
33 template <
typename Float>
class CRS;
47 template <
typename Float>
class COO {
86 std::shared_ptr<Float>
val;
118 COO(
const size_t M,
const size_t N)
139 COO(
const size_t M,
const size_t N,
const size_t NNZ,
const int *
row,
140 const int *
col,
const Float *value);
157 COO(
const size_t M,
const size_t N,
const size_t NNZ,
158 const std::vector<int> &
row,
const std::vector<int> &
col,
159 const std::vector<Float> &value) {
160 this =
COO(M, N, NNZ,
row.data(),
col.data(), value.data());
180 COO(
const size_t M,
const size_t N,
const size_t NNZ,
181 const std::vector<int> &
row,
const std::vector<int> &
col,
204 COO(
const size_t M,
const size_t N,
const size_t NNZ,
const int *
row,
205 const int *
col,
const Float *value,
const size_t origin);
224 COO(
const size_t M,
const size_t N,
const size_t NNZ,
225 const std::vector<int> &
row,
const std::vector<int> &
col,
226 const std::vector<Float> &value,
const size_t origin) {
227 this =
COO(M, N, NNZ,
row.data(),
col.data(), value.data(), origin);
342 throw std::runtime_error(
"error, GPU util of COO format is not impl. ");
351 throw std::runtime_error(
"error, GPU util of COO format is not impl. ");
388 [[nodiscard]]
const Float *
data()
const {
return val.get(); }
396 [[nodiscard]] Float *
data() {
return val.get(); }
408 throw std::runtime_error(
"Error, GPU matrix cant use resize");
411 std::shared_ptr<Float> tmp(
new Float[N], std::default_delete<Float[]>());
413 for (
size_t i = 0; i < copy_size; ++i) {
414 tmp.get()[i] =
data()[i];
416 for (
size_t i = copy_size; i < N; ++i) {
426 throw std::runtime_error(
"Error, not create vector cant use resize");
490 [[nodiscard]] Float
at(
const size_t i,
const size_t j)
const;
499 [[nodiscard]] Float
at(
const size_t i,
const size_t j) {
500 return static_cast<const COO *
>(
this)->
at(i, j);
515 void set_ptr(
const size_t rN,
const size_t cN,
const std::vector<int> &r,
516 const std::vector<int> &c,
const std::vector<Float> &v);
530 void set_ptr(
const size_t rN,
const size_t cN,
const std::vector<int> &r,
531 const std::vector<int> &c,
const size_t vsize,
const Float *v);
545 void set_ptr(
const size_t rN,
const size_t cN,
const std::vector<int> &r,
546 const std::vector<int> &c,
const size_t vsize,
const Float v);
615 for (
size_t i = 0; i <
val_nnz; ++i) {
655 for (
size_t i = 0; i <
val_nnz; ++i) {
692 return 3 *
get_nnz() *
sizeof(Float) / 1.0e+9;
702 [[nodiscard]] std::string
type()
const {
return "COO"; }
710 [[nodiscard]]
const Float *
begin()
const {
return data(); }
804 throw std::runtime_error(
"Error, GPU vector cant use operator[]");
820 bool compare_cpu_and_device =
false)
const;
859 void insert(
const size_t m,
const size_t n,
const Float
val);
Coodinate (COO) format Matrix (need to sort)
void diag(view1D< vector< Float >, Float > &vec) const
bool operator!=(const COO< Float > &mat) const
Comparing matrices (A != mat)
void print_all(bool force_cpu=false) const
print all elements to standard I/O
void recv() const
recv data from GPU
Float at(const size_t i, const size_t j) const
Get matrix element (A(i,j))
Float * end()
returns a end iterator
void convert(const matrix::LinearOperator< Float > &linearoperator)
const Float * data() const
returns a direct pointer to the vector
COO(const size_t M, const size_t N, const size_t NNZ, const int *row, const int *col, const Float *value)
Create COO matrix from array.
bool equal(const COO< Float > &mat, bool compare_cpu_and_device=false) const
Comparing matrices (A == mat)
void col(const size_t c, view1D< tensor::tensor_Dense< Float >, Float > &vec) const
void operator=(const COO< Float > &mat)
matrix copy
void transpose(const COO &B)
create transposed matrix from COO matrix (B = A^T)
void fill(Float value)
fill matrix elements with a scalar value
COO(const std::string filename)
Create COO matrix from MatrixMarket format file (only real general) (MatrixMarket format: https://mat...
void col(const size_t c, view1D< vector< Float >, Float > &vec) const
const Float * begin() const
returns a begin iterator
void transpose()
get transposed matrix (A^T)
COO(const matrix::CRS< Float > &crs)
Create COO matrix from CRS matrix.
COO(const matrix::COO< Float > &coo)
Create COO matrix from COO matrix.
std::vector< int > row_index
Coodinate format row index, which stores row numbers of the non-zero elements (size nnz)
void diag(vector< Float > &vec) const
get diag. vector
void print_all(const std::string filename) const
print all elements to file
COO(const matrix::Dense< Float > &dense)
Create COO matrix from Dense matrix (drop zero)
bool operator==(const COO< Float > &mat) const
Comparing matrices (A == mat)
void set_col(const size_t N)
Set col number.
const std::vector< Float > get_val_ptr() const
get value
void col(const size_t c, vector< Float > &vec) const
get column vector
void set_ptr(const size_t rN, const size_t cN, const std::vector< int > &r, const std::vector< int > &c, const size_t vsize, const Float *v)
Set COO array from std::vector.
void sort(bool merge)
sort COO matrix elements (and merge elements)
COO(const matrix::COO< Float > &coo, Float value)
Initialize COO matrix of the same size as input matrix.
COO(const size_t M, const size_t N, const size_t NNZ, const std::vector< int > &row, const std::vector< int > &col, const vector< Float > &value)
Create COO matrix from monolish::vector.
std::vector< int > & get_col_ind()
get column index
void col(const size_t c, view1D< matrix::Dense< Float >, Float > &vec) const
COO(const size_t M, const size_t N)
Initialize M x N COO matrix.
void _q_sort(int lo, int hi)
void set_ptr(const size_t rN, const size_t cN, const std::vector< int > &r, const std::vector< int > &c, const std::vector< Float > &v)
Set COO array from std::vector.
void diag(view1D< tensor::tensor_Dense< Float >, Float > &vec) const
const std::vector< int > & get_col_ind() const
get column index
COO(const size_t M, const size_t N, const size_t NNZ, const std::vector< int > &row, const std::vector< int > &col, const std::vector< Float > &value)
Create COO matrix from std::vector.
std::string type() const
get format name "COO"
void insert(const size_t m, const size_t n, const Float val)
insert element to (m, n)
std::vector< int > & get_row_ptr()
get row index
Float at(const size_t i, const size_t j)
Get matrix element (A(i,j))
void device_free() const
free data on GPU
void input_mm(const std::string filename)
Create COO matrix from MatrixMarket format file (only real general) (MatrixMarket format: https://mat...
std::size_t alloc_nnz
alloced matrix size
COO(const matrix::LinearOperator< Float > &linearoperator)
Float & operator[](size_t i)
reference to the element at position (v[i])
const std::vector< int > & get_row_ptr() const
get row index
bool val_create_flag
matrix create flag;
bool gpu_status
# of non-zero element
size_t val_nnz
# of non-zero element
void resize(size_t N, Float Val=0)
resize matrix value
bool get_device_mem_stat() const
false; // true: sended, false: not send
void row(const size_t r, view1D< vector< Float >, Float > &vec) const
std::vector< Float > get_val_ptr()
get value
size_t get_nnz() const
get # of non-zeros
void row(const size_t r, view1D< tensor::tensor_Dense< Float >, Float > &vec) const
void send() const
Set # of non-zero elements.
void set_ptr(const size_t rN, const size_t cN, const std::vector< int > &r, const std::vector< int > &c, const size_t vsize, const Float v)
Set COO array from std::vector.
void convert(const matrix::Dense< Float > &dense)
Create COO matrix from Dense matrix (drop zero)
COO(const size_t M, const size_t N, const size_t NNZ, const std::vector< int > &row, const std::vector< int > &col, const std::vector< Float > &value, const size_t origin)
Create COO matrix from n-origin array.
size_t get_col() const
get # of col
std::shared_ptr< Float > val
Coodinate format value array (pointer), which stores values of the non-zero elements.
void diag(view1D< matrix::Dense< Float >, Float > &vec) const
std::vector< int > col_index
Coodinate format column index, which stores column numbers of the non-zero elements (size nnz)
const Float * end() const
returns a end iterator
size_t get_row() const
get # of row
void row(const size_t r, view1D< matrix::Dense< Float >, Float > &vec) const
void row(const size_t r, vector< Float > &vec) const
get row vector
Float * begin()
returns a begin iterator
void output_mm(const std::string filename) const
output matrix elements in MatrixMarket format (MatrixMarket format: https://math.nist....
COO(const size_t M, const size_t N, const size_t NNZ, const int *row, const int *col, const Float *value, const size_t origin)
Create COO matrix from n-origin array.
double get_data_size() const
Memory data space required by the matrix.
void convert(const matrix::CRS< Float > &crs)
Create COO matrix from CRS matrix.
Float * data()
returns a direct pointer to the vector
void set_row(const size_t M)
Set row number.
Compressed Row Storage (CRS) format Matrix.
Linear Operator imitating Matrix.
const Float * data() const
returns a direct pointer to the vector
bool get_device_mem_stat() const
true: sended, false: not send
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],...