33 #ifndef GKO_CORE_MATRIX_DENSE_HPP_ 34 #define GKO_CORE_MATRIX_DENSE_HPP_ 37 #include <ginkgo/core/base/array.hpp> 38 #include <ginkgo/core/base/executor.hpp> 39 #include <ginkgo/core/base/lin_op.hpp> 40 #include <ginkgo/core/base/mtx_io.hpp> 41 #include <ginkgo/core/base/range_accessors.hpp> 42 #include <ginkgo/core/base/types.hpp> 43 #include <ginkgo/core/base/utils.hpp> 46 #include <initializer_list> 53 template <
typename ValueType,
typename IndexType>
56 template <
typename ValueType,
typename IndexType>
59 template <
typename ValueType,
typename IndexType>
62 template <
typename ValueType,
typename IndexType>
65 template <
typename ValueType,
typename IndexType>
84 template <
typename ValueType = default_precision>
119 using value_type = ValueType;
120 using index_type =
int64;
138 return Dense::create((*other).get_executor(), (*other).get_size(),
139 (*other).get_stride());
182 void read(
const mat_data &data)
override;
186 void write(
mat_data &data)
const override;
190 std::unique_ptr<LinOp>
transpose()
const override;
242 return values_.
get_data()[linearize_index(row, col)];
269 return values_.
get_data()[linearize_index(idx)];
357 auto range_result = range_this(rows, columns);
360 return Dense::create(
362 dim<2>{range_result.length(0), range_result.length(1)},
365 range_result.length(0) * range_this.length(1) - columns.
begin,
390 :
Dense(std::move(exec), size, size[1])
402 Dense(std::shared_ptr<const Executor> exec,
const dim<2> &size,
405 values_(exec, size[0] * stride),
425 template <
typename ValuesArray>
426 Dense(std::shared_ptr<const Executor> exec,
const dim<2> &size,
429 values_{exec, std::forward<ValuesArray>(values)},
432 GKO_ENSURE_IN_BOUNDS((size[0] - 1) * stride + size[1] - 1,
442 virtual void scale_impl(
const LinOp *alpha);
450 virtual void add_scaled_impl(
const LinOp *alpha,
const LinOp *b);
458 virtual void compute_dot_impl(
const LinOp *b,
LinOp *result)
const;
466 virtual void compute_norm2_impl(
LinOp *result)
const;
468 void apply_impl(
const LinOp *b,
LinOp *x)
const override;
471 LinOp *x)
const override;
475 return row * stride_ + col;
480 return linearize_index(idx / this->
get_size()[1],
514 template <
typename Matrix,
typename... TArgs>
516 size_type stride, std::initializer_list<typename Matrix::value_type> vals,
517 std::shared_ptr<const Executor> exec, TArgs &&... create_args)
521 auto tmp = dense::create(exec->get_master(),
dim<2>{num_rows, 1}, stride);
523 for (
const auto &elem : vals) {
527 auto mtx = Matrix::create(exec, std::forward<TArgs>(create_args)...);
528 tmp->move_to(mtx.get());
553 template <
typename Matrix,
typename... TArgs>
555 std::initializer_list<typename Matrix::value_type> vals,
556 std::shared_ptr<const Executor> exec, TArgs &&... create_args)
558 return initialize<Matrix>(1, vals, std::move(exec),
559 std::forward<TArgs>(create_args)...);
584 template <
typename Matrix,
typename... TArgs>
587 std::initializer_list<std::initializer_list<typename Matrix::value_type>>
589 std::shared_ptr<const Executor> exec, TArgs &&... create_args)
593 size_type num_cols = num_rows > 0 ? begin(vals)->size() : 1;
595 dense::create(exec->get_master(),
dim<2>{num_rows, num_cols}, stride);
597 for (
const auto &row : vals) {
599 for (
const auto &elem : row) {
600 tmp->
at(ridx, cidx) = elem;
605 auto mtx = Matrix::create(exec, std::forward<TArgs>(create_args)...);
606 tmp->move_to(mtx.get());
633 template <
typename Matrix,
typename... TArgs>
635 std::initializer_list<std::initializer_list<typename Matrix::value_type>>
637 std::shared_ptr<const Executor> exec, TArgs &&... create_args)
639 return initialize<Matrix>(vals.size() > 0 ? begin(vals)->size() : 0, vals,
641 std::forward<TArgs>(create_args)...);
648 #endif // GKO_CORE_MATRIX_DENSE_HPP_ value_type & at(size_type row, size_type col) noexcept
Returns a single element of the matrix.
Definition: dense.hpp:240
size_type get_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the matrix.
Definition: dense.hpp:225
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory...
Definition: polymorphic_object.hpp:576
size_type get_num_elems() const noexcept
Returns the number of elements in the Array.
Definition: array.hpp:388
ELL is a matrix format where stride with explicit zeros is used such that all rows have the same numb...
Definition: csr.hpp:53
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition: polymorphic_object.hpp:380
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:505
std::int32_t int32
32-bit signed integral type.
Definition: types.hpp:111
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:94
void compute_norm2(LinOp *result) const
Computes the Euclidian (L^2) norm of this matrix.
Definition: dense.hpp:335
const value_type * get_const_values() const noexcept
Returns a pointer to the array of values of the matrix.
Definition: dense.hpp:208
const value_type * get_const_data() const noexcept
Returns a constant pointer to the block of memory used to store the elements of the Array...
Definition: array.hpp:406
The Ginkgo namespace.
Definition: abstract_factory.hpp:45
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition: coo.hpp:51
ValueType & at(size_type idx) noexcept
Returns a single element of the matrix.
Definition: dense.hpp:267
void add_scaled(const LinOp *alpha, const LinOp *b)
Adds b scaled by alpha to the matrix (aka: BLAS axpy).
Definition: dense.hpp:305
SELL-P is a matrix format similar to ELL format.
Definition: csr.hpp:57
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: coo.hpp:55
Definition: lin_op.hpp:134
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition: lin_op.hpp:509
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition: lin_op.hpp:221
COO stores a matrix in the coordinate matrix format.
Definition: coo.hpp:73
std::int64_t int64
64-bit signed integral type.
Definition: types.hpp:117
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:201
static std::unique_ptr< Dense > create_with_config_of(const Dense *other)
Creates a Dense matrix with the configuration of another Dense matrix.
Definition: dense.hpp:131
void scale(const LinOp *alpha)
Scales the matrix with a scalar (aka: BLAS scal).
Definition: dense.hpp:289
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
void compute_dot(const LinOp *b, LinOp *result) const
Computes the column-wise dot product of this matrix and b.
Definition: dense.hpp:321
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition: lin_op.hpp:446
ValueType at(size_type idx) const noexcept
Returns a single element of the matrix.
Definition: dense.hpp:275
size_type get_stride() const noexcept
Returns the stride of the matrix.
Definition: dense.hpp:218
const size_type begin
Beginning of the span.
Definition: range.hpp:105
std::unique_ptr< Dense > create_submatrix(const span &rows, const span &columns, const size_type stride)
Create a submatrix from the original matrix.
Definition: dense.hpp:351
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the Array. ...
Definition: array.hpp:397
Linear operators which support transposition should implement the Transposable interface.
Definition: lin_op.hpp:398
This structure is used as an intermediate data type to store a sparse matrix.
Definition: matrix_data.hpp:102
A span is a lightweight structure used to create sub-ranges from other ranges.
Definition: range.hpp:73
A LinOp implementing this interface can read its data from a matrix_data structure.
Definition: lin_op.hpp:426
A range is a multidimensional view of the memory.
Definition: range.hpp:296
HYBRID is a matrix format which splits the matrix into ELLPACK and COO format.
Definition: dense.hpp:63
value_type at(size_type row, size_type col) const noexcept
Returns a single element of the matrix.
Definition: dense.hpp:248
temporary_clone< T > make_temporary_clone(std::shared_ptr< const Executor > exec, T *ptr)
Creates a temporary_clone.
Definition: utils.hpp:474
static Array view(std::shared_ptr< const Executor > exec, size_type num_elems, value_type *data)
Creates an Array from existing memory.
Definition: array.hpp:266
value_type * get_values() noexcept
Returns a pointer to the array of values of the matrix.
Definition: dense.hpp:199