ginkgo/core/matrix/batch_dense.hpp Source File

ginkgo/core/matrix/batch_dense.hpp Source File#

Reference API: ginkgo/core/matrix/batch_dense.hpp Source File
Reference API
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#include <ginkgo/core/base/array.hpp>
13#include <ginkgo/core/base/batch_lin_op.hpp>
14#include <ginkgo/core/base/batch_multi_vector.hpp>
15#include <ginkgo/core/base/executor.hpp>
16#include <ginkgo/core/base/mtx_io.hpp>
17#include <ginkgo/core/base/range_accessors.hpp>
18#include <ginkgo/core/base/types.hpp>
19#include <ginkgo/core/base/utils.hpp>
20#include <ginkgo/core/matrix/dense.hpp>
21
22
23namespace gko {
24namespace batch {
25namespace matrix {
26
27
47template <typename ValueType = default_precision>
48class Dense final
49 : public EnableBatchLinOp<Dense<ValueType>>,
50#if GINKGO_ENABLE_HALF
51 public ConvertibleTo<Dense<next_precision<next_precision<ValueType>>>>,
52#endif
53 public ConvertibleTo<Dense<next_precision<ValueType>>> {
55 friend class Dense<to_complex<ValueType>>;
56 friend class Dense<previous_precision<ValueType>>;
57
58public:
59 using EnableBatchLinOp<Dense>::convert_to;
60 using EnableBatchLinOp<Dense>::move_to;
61
62 using value_type = ValueType;
63 using index_type = int32;
66 using absolute_type = remove_complex<Dense>;
67 using complex_type = to_complex<Dense>;
68
69 void convert_to(Dense<next_precision<ValueType>>* result) const override;
70
71 void move_to(Dense<next_precision<ValueType>>* result) override;
72
73#if GINKGO_ENABLE_HALF
74 friend class Dense<previous_precision<previous_precision<ValueType>>>;
75 using ConvertibleTo<
77 using ConvertibleTo<
79
80 void convert_to(Dense<next_precision<next_precision<ValueType>>>* result)
81 const override;
82
83 void move_to(
85#endif
86
97 std::unique_ptr<unbatch_type> create_view_for_item(size_type item_id);
98
102 std::unique_ptr<const unbatch_type> create_const_view_for_item(
103 size_type item_id) const;
104
113 {
114 GKO_ASSERT(batch_id < this->get_num_batch_items());
115 return batch_id * this->get_common_size()[0] *
116 this->get_common_size()[1];
117 }
118
124 value_type* get_values() noexcept { return values_.get_data(); }
125
133 const value_type* get_const_values() const noexcept
134 {
135 return values_.get_const_data();
136 }
137
149 value_type& at(size_type batch_id, size_type row, size_type col)
150 {
151 GKO_ASSERT(batch_id < this->get_num_batch_items());
152 return values_.get_data()[linearize_index(batch_id, row, col)];
153 }
154
158 value_type at(size_type batch_id, size_type row, size_type col) const
159 {
160 GKO_ASSERT(batch_id < this->get_num_batch_items());
161 return values_.get_const_data()[linearize_index(batch_id, row, col)];
162 }
163
178 ValueType& at(size_type batch_id, size_type idx) noexcept
179 {
180 GKO_ASSERT(batch_id < this->get_num_batch_items());
181 return values_.get_data()[linearize_index(batch_id, idx)];
182 }
183
187 ValueType at(size_type batch_id, size_type idx) const noexcept
188 {
189 GKO_ASSERT(batch_id < this->get_num_batch_items());
190 return values_.get_const_data()[linearize_index(batch_id, idx)];
191 }
192
201 value_type* get_values_for_item(size_type batch_id) noexcept
202 {
203 GKO_ASSERT(batch_id < this->get_num_batch_items());
204 return values_.get_data() + this->get_cumulative_offset(batch_id);
205 }
206
214 const value_type* get_const_values_for_item(
215 size_type batch_id) const noexcept
216 {
217 GKO_ASSERT(batch_id < this->get_num_batch_items());
218 return values_.get_const_data() + this->get_cumulative_offset(batch_id);
219 }
220
229 {
230 return values_.get_size();
231 }
232
239 {
240 return this->get_num_stored_elements() / this->get_num_batch_items();
241 }
242
251 static std::unique_ptr<Dense> create(
252 std::shared_ptr<const Executor> exec,
253 const batch_dim<2>& size = batch_dim<2>{});
254
269 static std::unique_ptr<Dense> create(std::shared_ptr<const Executor> exec,
270 const batch_dim<2>& size,
271 array<value_type> values);
272
277 template <typename InputValueType>
278 GKO_DEPRECATED(
279 "explicitly construct the gko::array argument instead of passing an"
280 "initializer list")
281 static std::unique_ptr<Dense> create(
282 std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
283 std::initializer_list<InputValueType> values)
284 {
285 return create(exec, size, array<value_type>{exec, std::move(values)});
286 }
287
302 static std::unique_ptr<const Dense> create_const(
303 std::shared_ptr<const Executor> exec, const batch_dim<2>& sizes,
304 gko::detail::const_array_view<ValueType>&& values);
305
315
330
336
346
353 void scale(const array<value_type>& row_scale,
354 const array<value_type>& col_scale);
355
366
377
378private:
379 inline size_type compute_num_elems(const batch_dim<2>& size)
380 {
381 return size.get_num_batch_items() * size.get_common_size()[0] *
382 size.get_common_size()[1];
383 }
384
385 Dense(std::shared_ptr<const Executor> exec,
386 const batch_dim<2>& size = batch_dim<2>{});
387
388 Dense(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
389 array<value_type> values);
390
391 void apply_impl(const MultiVector<value_type>* b,
392 MultiVector<value_type>* x) const;
393
394 void apply_impl(const MultiVector<value_type>* alpha,
396 const MultiVector<value_type>* beta,
397 MultiVector<value_type>* x) const;
398
399 size_type linearize_index(size_type batch, size_type row,
400 size_type col) const noexcept
401 {
402 return this->get_cumulative_offset(batch) +
403 row * this->get_size().get_common_size()[1] + col;
404 }
405
406 size_type linearize_index(size_type batch, size_type idx) const noexcept
407 {
408 return linearize_index(batch, idx / this->get_common_size()[1],
409 idx % this->get_common_size()[1]);
410 }
411
412 array<value_type> values_;
413};
414
415
416} // namespace matrix
417} // namespace batch
418} // namespace gko
419
420
421#endif // GKO_PUBLIC_CORE_MATRIX_BATCH_DENSE_HPP_
Definition polymorphic_object.hpp:479
Definition polymorphic_object.hpp:668
Definition executor.hpp:615
Definition array.hpp:166
value_type * get_data() noexcept
Definition array.hpp:673
const value_type * get_const_data() const noexcept
Definition array.hpp:682
size_type get_size() const noexcept
Definition array.hpp:656
Definition batch_lin_op.hpp:59
Definition batch_lin_op.hpp:252
Definition batch_multi_vector.hpp:59
Definition batch_dense.hpp:53
static std::unique_ptr< Dense > create(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &size, array< value_type > values)
size_type get_num_elements_per_item() const noexcept
Definition batch_dense.hpp:238
std::unique_ptr< unbatch_type > create_view_for_item(size_type item_id)
const Dense * apply(ptr_param< const MultiVector< value_type > > b, ptr_param< MultiVector< value_type > > x) const
value_type * get_values() noexcept
Definition batch_dense.hpp:124
Dense * apply(ptr_param< const MultiVector< value_type > > b, ptr_param< MultiVector< value_type > > x)
value_type at(size_type batch_id, size_type row, size_type col) const
Definition batch_dense.hpp:158
void scale(const array< value_type > &row_scale, const array< value_type > &col_scale)
size_type get_cumulative_offset(size_type batch_id) const
Definition batch_dense.hpp:112
ValueType & at(size_type batch_id, size_type idx) noexcept
Definition batch_dense.hpp:178
static std::unique_ptr< Dense > create(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &size=batch_dim< 2 >{})
std::unique_ptr< const unbatch_type > create_const_view_for_item(size_type item_id) 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)
value_type * get_values_for_item(size_type batch_id) noexcept
Definition batch_dense.hpp:201
value_type & at(size_type batch_id, size_type row, size_type col)
Definition batch_dense.hpp:149
void add_scaled_identity(ptr_param< const MultiVector< value_type > > alpha, ptr_param< const MultiVector< value_type > > beta)
void scale_add(ptr_param< const MultiVector< value_type > > alpha, ptr_param< const batch::matrix::Dense< value_type > > b)
size_type get_num_stored_elements() const noexcept
Definition batch_dense.hpp:228
const Dense * apply(ptr_param< const MultiVector< value_type > > alpha, ptr_param< const MultiVector< value_type > > b, ptr_param< const MultiVector< value_type > > beta, ptr_param< MultiVector< value_type > > x) const
Dense * apply(ptr_param< const MultiVector< value_type > > alpha, ptr_param< const MultiVector< value_type > > b, ptr_param< const MultiVector< value_type > > beta, ptr_param< MultiVector< value_type > > x)
ValueType at(size_type batch_id, size_type idx) const noexcept
Definition batch_dense.hpp:187
const value_type * get_const_values() const noexcept
Definition batch_dense.hpp:133
const value_type * get_const_values_for_item(size_type batch_id) const noexcept
Definition batch_dense.hpp:214
Definition utils_helper.hpp:41
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::int32_t int32
Definition types.hpp:106
typename detail::next_precision_impl< T >::type next_precision
Definition math.hpp:438
std::size_t size_type
Definition types.hpp:89
typename detail::to_complex_s< T >::type to_complex
Definition math.hpp:279
typename detail::remove_complex_s< T >::type remove_complex
Definition math.hpp:260
STL namespace.
Definition batch_dim.hpp:27
dim< dimensionality, dimension_type > get_common_size() const
Definition batch_dim.hpp:43
size_type get_num_batch_items() const
Definition batch_dim.hpp:36