Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
batch_dense.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_MATRIX_BATCH_DENSE_HPP_
6 #define GKO_PUBLIC_CORE_MATRIX_BATCH_DENSE_HPP_
7 
8 
9 #include <initializer_list>
10 #include <vector>
11 
12 
13 #include <ginkgo/core/base/array.hpp>
14 #include <ginkgo/core/base/batch_lin_op.hpp>
15 #include <ginkgo/core/base/batch_multi_vector.hpp>
16 #include <ginkgo/core/base/executor.hpp>
17 #include <ginkgo/core/base/mtx_io.hpp>
18 #include <ginkgo/core/base/range_accessors.hpp>
19 #include <ginkgo/core/base/types.hpp>
20 #include <ginkgo/core/base/utils.hpp>
21 #include <ginkgo/core/matrix/dense.hpp>
22 
23 
24 namespace gko {
25 namespace batch {
26 namespace matrix {
27 
28 
48 template <typename ValueType = default_precision>
49 class Dense final : public EnableBatchLinOp<Dense<ValueType>>,
50  public ConvertibleTo<Dense<next_precision<ValueType>>> {
52  friend class Dense<to_complex<ValueType>>;
53  friend class Dense<next_precision<ValueType>>;
54 
55 public:
58 
59  using value_type = ValueType;
60  using index_type = int32;
63  using absolute_type = remove_complex<Dense>;
64  using complex_type = to_complex<Dense>;
65 
66  void convert_to(Dense<next_precision<ValueType>>* result) const override;
67 
68  void move_to(Dense<next_precision<ValueType>>* result) override;
69 
80  std::unique_ptr<unbatch_type> create_view_for_item(size_type item_id);
81 
85  std::unique_ptr<const unbatch_type> create_const_view_for_item(
86  size_type item_id) const;
87 
96  {
97  GKO_ASSERT(batch_id < this->get_num_batch_items());
98  return batch_id * this->get_common_size()[0] *
99  this->get_common_size()[1];
100  }
101 
107  value_type* get_values() noexcept { return values_.get_data(); }
108 
116  const value_type* get_const_values() const noexcept
117  {
118  return values_.get_const_data();
119  }
120 
132  value_type& at(size_type batch_id, size_type row, size_type col)
133  {
134  GKO_ASSERT(batch_id < this->get_num_batch_items());
135  return values_.get_data()[linearize_index(batch_id, row, col)];
136  }
137 
141  value_type at(size_type batch_id, size_type row, size_type col) const
142  {
143  GKO_ASSERT(batch_id < this->get_num_batch_items());
144  return values_.get_const_data()[linearize_index(batch_id, row, col)];
145  }
146 
161  ValueType& at(size_type batch_id, size_type idx) noexcept
162  {
163  GKO_ASSERT(batch_id < this->get_num_batch_items());
164  return values_.get_data()[linearize_index(batch_id, idx)];
165  }
166 
170  ValueType at(size_type batch_id, size_type idx) const noexcept
171  {
172  GKO_ASSERT(batch_id < this->get_num_batch_items());
173  return values_.get_const_data()[linearize_index(batch_id, idx)];
174  }
175 
184  value_type* get_values_for_item(size_type batch_id) noexcept
185  {
186  GKO_ASSERT(batch_id < this->get_num_batch_items());
187  return values_.get_data() + this->get_cumulative_offset(batch_id);
188  }
189 
197  const value_type* get_const_values_for_item(
198  size_type batch_id) const noexcept
199  {
200  GKO_ASSERT(batch_id < this->get_num_batch_items());
201  return values_.get_const_data() + this->get_cumulative_offset(batch_id);
202  }
203 
212  {
213  return values_.get_size();
214  }
215 
222  {
223  return this->get_num_stored_elements() / this->get_num_batch_items();
224  }
225 
234  static std::unique_ptr<Dense> create(
235  std::shared_ptr<const Executor> exec,
236  const batch_dim<2>& size = batch_dim<2>{});
237 
252  static std::unique_ptr<Dense> create(std::shared_ptr<const Executor> exec,
253  const batch_dim<2>& size,
254  array<value_type> values);
255 
260  template <typename InputValueType>
261  GKO_DEPRECATED(
262  "explicitly construct the gko::array argument instead of passing an"
263  "initializer list")
264  static std::unique_ptr<Dense> create(
265  std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
266  std::initializer_list<InputValueType> values)
267  {
268  return create(exec, size, array<value_type>{exec, std::move(values)});
269  }
270 
285  static std::unique_ptr<const Dense> create_const(
286  std::shared_ptr<const Executor> exec, const batch_dim<2>& sizes,
287  gko::detail::const_array_view<ValueType>&& values);
288 
298 
313 
319 
325  const Dense* apply(ptr_param<const MultiVector<value_type>> alpha,
329 
336  void scale(const array<value_type>& row_scale,
337  const array<value_type>& col_scale);
338 
347  void scale_add(ptr_param<const MultiVector<value_type>> alpha,
349 
359  ptr_param<const MultiVector<value_type>> beta);
360 
361 private:
362  inline size_type compute_num_elems(const batch_dim<2>& size)
363  {
364  return size.get_num_batch_items() * size.get_common_size()[0] *
365  size.get_common_size()[1];
366  }
367 
368  Dense(std::shared_ptr<const Executor> exec,
369  const batch_dim<2>& size = batch_dim<2>{});
370 
371  Dense(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
372  array<value_type> values);
373 
374  void apply_impl(const MultiVector<value_type>* b,
375  MultiVector<value_type>* x) const;
376 
377  void apply_impl(const MultiVector<value_type>* alpha,
378  const MultiVector<value_type>* b,
379  const MultiVector<value_type>* beta,
380  MultiVector<value_type>* x) const;
381 
382  size_type linearize_index(size_type batch, size_type row,
383  size_type col) const noexcept
384  {
385  return this->get_cumulative_offset(batch) +
386  row * this->get_size().get_common_size()[1] + col;
387  }
388 
389  size_type linearize_index(size_type batch, size_type idx) const noexcept
390  {
391  return linearize_index(batch, idx / this->get_common_size()[1],
392  idx % this->get_common_size()[1]);
393  }
394 
395  array<value_type> values_;
396 };
397 
398 
399 } // namespace matrix
400 } // namespace batch
401 } // namespace gko
402 
403 
404 #endif // GKO_PUBLIC_CORE_MATRIX_BATCH_DENSE_HPP_
gko::batch::EnableBatchLinOp
The EnableBatchLinOp mixin can be used to provide sensible default implementations of the majority of...
Definition: batch_lin_op.hpp:251
gko::batch_dim::get_num_batch_items
size_type get_num_batch_items() const
Get the number of batch items stored.
Definition: batch_dim.hpp:37
gko::batch::matrix::Dense::get_values_for_item
value_type * get_values_for_item(size_type batch_id) noexcept
Returns a pointer to the array of values of the matrix for a specific batch item.
Definition: batch_dense.hpp:184
gko::batch::matrix::Dense::get_const_values_for_item
const value_type * get_const_values_for_item(size_type batch_id) const noexcept
Returns a pointer to the array of values of the matrix for a specific batch item.
Definition: batch_dense.hpp:197
gko::batch_dim::get_common_size
dim< dimensionality, dimension_type > get_common_size() const
Get the common size of the batch items.
Definition: batch_dim.hpp:44
gko::batch::matrix::Dense::scale
void scale(const array< value_type > &row_scale, const array< value_type > &col_scale)
Performs in-place row and column scaling for this matrix.
gko::batch::matrix::Dense::add_scaled_identity
void add_scaled_identity(ptr_param< const MultiVector< value_type >> alpha, ptr_param< const MultiVector< value_type >> beta)
Performs the operation this = alpha*I + beta*this.
gko::batch::matrix::Dense::get_num_elements_per_item
size_type get_num_elements_per_item() const noexcept
Returns the number of stored elements in each batch item.
Definition: batch_dense.hpp:221
gko::batch::matrix::Dense::at
ValueType & at(size_type batch_id, size_type idx) noexcept
Returns a single element for a particular batch item.
Definition: batch_dense.hpp:161
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: dense_cache.hpp:20
gko::batch::BatchLinOp
Definition: batch_lin_op.hpp:60
gko::batch::matrix::Dense::create_const
static std::unique_ptr< const Dense > create_const(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &sizes, gko::detail::const_array_view< ValueType > &&values)
Creates a constant (immutable) batch dense matrix from a constant array.
gko::batch::matrix::Dense::at
value_type at(size_type batch_id, size_type row, size_type col) const
Returns a single element for a particular batch item.
Definition: batch_dense.hpp:141
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:108
gko::batch::matrix::Dense::at
value_type & at(size_type batch_id, size_type row, size_type col)
Returns a single element for a particular batch item.
Definition: batch_dense.hpp:132
gko::batch::MultiVector
MultiVector stores multiple vectors in a batched fashion and is useful for batched operations.
Definition: batch_multi_vector.hpp:53
gko::batch::matrix::Dense::get_const_values
const value_type * get_const_values() const noexcept
Returns a pointer to the array of values of the multi-vector.
Definition: batch_dense.hpp:116
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::batch::matrix::Dense::apply
Dense * apply(ptr_param< const MultiVector< value_type >> b, ptr_param< MultiVector< value_type >> x)
Apply the matrix to a multi-vector.
gko::array< value_type >
gko::batch::matrix::Dense::at
ValueType at(size_type batch_id, size_type idx) const noexcept
Returns a single element for a particular batch item.
Definition: batch_dense.hpp:170
gko::batch::matrix::Dense::create_const_view_for_item
std::unique_ptr< const unbatch_type > create_const_view_for_item(size_type item_id) const
Creates a mutable view (of gko::matrix::Dense type) of one item of the batch::matrix::Dense<value_typ...
gko::batch_dim< 2 >
gko::batch::matrix::Dense::create_view_for_item
std::unique_ptr< unbatch_type > create_view_for_item(size_type item_id)
Creates a mutable view (of gko::matrix::Dense type) of one item of the batch::matrix::Dense<value_typ...
gko::next_precision
typename detail::next_precision_impl< T >::type next_precision
Obtains the next type in the singly-linked precision list.
Definition: math.hpp:462
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition: utils_helper.hpp:43
gko::array::get_data
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition: array.hpp:674
gko::batch::matrix::Dense::create
static std::unique_ptr< Dense > create(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &size=batch_dim< 2 >{})
Creates an uninitialized Dense matrix of the specified size.
gko::batch::matrix::Dense::get_num_stored_elements
size_type get_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the batch matrix, cumulative across all the batch...
Definition: batch_dense.hpp:211
gko::ConvertibleTo
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition: polymorphic_object.hpp:471
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition: types.hpp:125
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:616
gko::batch::matrix::Dense::get_values
value_type * get_values() noexcept
Returns a pointer to the array of values of the multi-vector.
Definition: batch_dense.hpp:107
gko::array::get_const_data
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:683
gko::batch::matrix::Dense
Dense is a batch matrix format which explicitly stores all values of the matrix in each of the batche...
Definition: batch_dense.hpp:49
gko::array::get_size
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition: array.hpp:657
gko::remove_complex
typename detail::remove_complex_s< T >::type remove_complex
Obtain the type which removed the complex of complex/scalar type or the template parameter of class b...
Definition: math.hpp:326
gko::batch::matrix::Dense::get_cumulative_offset
size_type get_cumulative_offset(size_type batch_id) const
Get the cumulative storage size offset.
Definition: batch_dense.hpp:95
gko::batch::matrix::Dense::scale_add
void scale_add(ptr_param< const MultiVector< value_type >> alpha, ptr_param< const batch::matrix::Dense< value_type >> b)
Performs the operation this = alpha*this + b.
gko::to_complex
typename detail::to_complex_s< T >::type to_complex
Obtain the type which adds the complex of complex/scalar type or the template parameter of class by a...
Definition: math.hpp:345
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:662