Ginkgo  Generated from pipelines/2567305205 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
device_views.hpp
1 // SPDX-FileCopyrightText: 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_MATRIX_DEVICE_DENSE_HPP_
6 #define GKO_PUBLIC_CORE_MATRIX_DEVICE_DENSE_HPP_
7 
8 #include <cassert>
9 
10 #include <ginkgo/core/base/dim.hpp>
11 
12 
13 namespace gko {
14 namespace matrix {
15 namespace view {
16 
17 
26 template <typename ValueType>
27 struct dense {
28  dim<2> size;
29  size_type stride;
30  ValueType* values;
31 
33  constexpr dense(dim<2> size, size_type stride, ValueType* values)
34  : size{size}, stride{stride}, values{values}
35  {
36  assert(stride >= size[1]);
37  }
38 
40  constexpr dense<const ValueType> as_const() const
41  {
42  return dense<const ValueType>{size, stride, values};
43  }
44 
46  constexpr ValueType& operator()(size_type row, size_type col) const
47  {
48  assert(row < size[0] && col < size[1]);
49  return values[row * stride + col];
50  }
51 };
52 
53 
62 template <typename ValueType, typename IndexType>
63 struct coo {
64  dim<2> size;
65  size_type num_stored_elements;
66  ValueType* values;
67  IndexType* row_idxs;
68  IndexType* col_idxs;
69 
71  constexpr coo(dim<2> size, size_type num_stored_elements, ValueType* values,
72  IndexType* row_idxs, IndexType* col_idxs)
73  : size{size},
74  num_stored_elements{num_stored_elements},
75  values{values},
76  row_idxs{row_idxs},
77  col_idxs{col_idxs}
78  {}
79 
82  {
84  size, num_stored_elements, values, row_idxs, col_idxs};
85  }
86 };
87 
88 
97 template <typename ValueType, typename IndexType>
98 struct ell {
99  dim<2> size;
100  size_type num_stored_elements_per_row;
101  size_type stride;
102  ValueType* values;
103  IndexType* col_idxs;
104 
106  constexpr ell(dim<2> size, size_type num_stored_elements_per_row,
107  size_type stride, ValueType* values, IndexType* col_idxs)
108  : size{size},
109  num_stored_elements_per_row{num_stored_elements_per_row},
110  stride{stride},
111  values{values},
112  col_idxs(col_idxs)
113  {
114  assert(stride >= size[0]);
115  }
116 
119  {
121  size, num_stored_elements_per_row, stride, values, col_idxs};
122  }
123 
125  constexpr ValueType& val_at(size_type row, size_type idx) const
126  {
127  return values[this->linearize_index(row, idx)];
128  }
129 
131  constexpr IndexType& col_at(size_type row, size_type idx) const
132  {
133  return col_idxs[this->linearize_index(row, idx)];
134  }
135 
136 private:
138  constexpr size_type linearize_index(size_type row,
139  size_type idx) const noexcept
140  {
141  assert(idx < num_stored_elements_per_row && row < size[0]);
142  return row + stride * idx;
143  }
144 };
145 
146 
155 template <typename ValueType, typename IndexType>
156 struct sellp {
157  dim<2> size;
158  size_type slice_size;
159  size_type stride_factor;
160  size_type total_cols;
161  ValueType* values;
162  IndexType* col_idxs;
163  static_assert(std::is_const_v<ValueType> == std::is_const_v<IndexType>,
164  "ValueType and IndexType must share the same constness");
165  using adapt_size_type = std::conditional_t<std::is_const_v<ValueType>,
166  const size_type, size_type>;
167  adapt_size_type* slice_lengths;
168  adapt_size_type* slice_sets;
169 
171  constexpr sellp(dim<2> size, size_type slice_size, size_type stride_factor,
172  size_type total_cols, ValueType* values,
173  IndexType* col_idxs, adapt_size_type* slice_lengths,
174  adapt_size_type* slice_sets)
175  : size{size},
176  slice_size{slice_size},
177  stride_factor{stride_factor},
178  total_cols{total_cols},
179  values{values},
180  col_idxs{col_idxs},
181  slice_lengths{slice_lengths},
182  slice_sets{slice_sets}
183  {}
184 
187  {
189  size, slice_size, stride_factor, total_cols,
190  values, col_idxs, slice_lengths, slice_sets};
191  }
192 
194  constexpr ValueType& val_at(size_type row, size_type slice_set,
195  size_type idx) const
196  {
197  return values[this->linearize_index(row, slice_set, idx)];
198  }
199 
201  constexpr IndexType& col_at(size_type row, size_type slice_set,
202  size_type idx) const
203  {
204  return col_idxs[this->linearize_index(row, slice_set, idx)];
205  }
206 
207 private:
209  constexpr size_type linearize_index(size_type row, size_type slice_set,
210  size_type idx) const noexcept
211  {
212  assert(row < slice_size);
213  // note the following does not catch all idx out of bound access.
214  assert(idx < total_cols);
215  return (slice_set + idx) * slice_size + row;
216  }
217 };
218 
219 
228 template <typename ValueType, typename IndexType>
229 struct hybrid {
230  static_assert(std::is_const_v<ValueType> == std::is_const_v<IndexType>,
231  "ValueType and IndexType must share the same constness");
232  dim<2> size;
233  ell<ValueType, IndexType> ell_part;
234  coo<ValueType, IndexType> coo_part;
235 
239  : size(ell_.size), ell_part(ell_), coo_part(coo_)
240  {
241  assert(ell_part.size == coo_part.size);
242  }
243 
246  {
247  return {ell_part.as_const(), coo_part.as_const()};
248  }
249 };
250 
251 
252 } // namespace view
253 } // namespace matrix
254 } // namespace gko
255 
256 
257 #endif // GKO_PUBLIC_CORE_MATRIX_DEVICE_DENSE_HPP_
gko::matrix::view::dense
Non-owning view of a matrix::Dense to be used inside device kernels.
Definition: device_views.hpp:27
gko::matrix::view::ell::as_const
constexpr ell< const ValueType, const IndexType > as_const() const
Returns a const view of the same values.
Definition: device_views.hpp:118
gko::matrix::view::coo
Non-owning view of a matrix::Coo to be used inside device kernels.
Definition: device_views.hpp:63
gko::matrix::view::ell::col_at
constexpr IndexType & col_at(size_type row, size_type idx) const
accessing the column index of the idx-th element within the given row
Definition: device_views.hpp:131
gko::matrix::view::ell::ell
constexpr ell(dim< 2 > size, size_type num_stored_elements_per_row, size_type stride, ValueType *values, IndexType *col_idxs)
Constructs a ell view.
Definition: device_views.hpp:106
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:90
gko::matrix::view::ell
Non-owning view of a matrix::Ell to be used inside device kernels.
Definition: device_views.hpp:98
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::matrix::view::sellp::val_at
constexpr ValueType & val_at(size_type row, size_type slice_set, size_type idx) const
accessing the value of the idx-th element within the given row
Definition: device_views.hpp:194
gko::dim< 2 >
gko::matrix::view::hybrid::as_const
constexpr hybrid< const ValueType, const IndexType > as_const() const
Returns a const view of the same values.
Definition: device_views.hpp:245
gko::matrix::view::sellp::as_const
constexpr sellp< const ValueType, const IndexType > as_const() const
Returns a const view of the same values.
Definition: device_views.hpp:186
gko::matrix::view::coo::as_const
constexpr coo< const ValueType, const IndexType > as_const() const
Returns a const view of the same data.
Definition: device_views.hpp:81
gko::matrix::view::dense::dense
constexpr dense(dim< 2 > size, size_type stride, ValueType *values)
Constructs a dense view from size, stride and values.
Definition: device_views.hpp:33
gko::matrix::view::ell::val_at
constexpr ValueType & val_at(size_type row, size_type idx) const
accessing the value of the idx-th element within the given row
Definition: device_views.hpp:125
gko::matrix::view::dense::operator()
constexpr ValueType & operator()(size_type row, size_type col) const
Subscript operator accessing the given row and column.
Definition: device_views.hpp:46
gko::matrix::view::dense::as_const
constexpr dense< const ValueType > as_const() const
Returns a const view of the same values.
Definition: device_views.hpp:40
gko::matrix::view::sellp::col_at
constexpr IndexType & col_at(size_type row, size_type slice_set, size_type idx) const
accessing the column index of the idx-th element within the given row
Definition: device_views.hpp:201
gko::matrix::view::sellp::sellp
constexpr sellp(dim< 2 > size, size_type slice_size, size_type stride_factor, size_type total_cols, ValueType *values, IndexType *col_idxs, adapt_size_type *slice_lengths, adapt_size_type *slice_sets)
Constructs a sellp view.
Definition: device_views.hpp:171
gko::matrix::view::coo::coo
constexpr coo(dim< 2 > size, size_type num_stored_elements, ValueType *values, IndexType *row_idxs, IndexType *col_idxs)
Constructs a coo view from size, nnz, values, row and column indices.
Definition: device_views.hpp:71
gko::matrix::view::hybrid
Non-owning view of a matrix::Hybrid to be used inside device kernels.
Definition: device_views.hpp:229
gko::matrix::view::sellp
Non-owning view of a matrix::Sellp to be used inside device kernels.
Definition: device_views.hpp:156
gko::matrix::view::hybrid::hybrid
constexpr hybrid(ell< ValueType, IndexType > ell_, coo< ValueType, IndexType > coo_)
Constructs a hybrid view.
Definition: device_views.hpp:237