Ginkgo  Generated from pipelines/2779843039 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 csr {
99  dim<2> size;
100  size_type num_stored_elements;
101  ValueType* values;
102  IndexType* row_ptrs;
103  IndexType* col_idxs;
104 
107  constexpr csr(dim<2> size, size_type num_stored_elements, ValueType* values,
108  IndexType* row_ptrs, IndexType* col_idxs)
109  : size{size},
110  num_stored_elements{num_stored_elements},
111  values{values},
112  row_ptrs{row_ptrs},
113  col_idxs{col_idxs}
114  {}
115 
118  {
120  size, num_stored_elements, values, row_ptrs, col_idxs};
121  }
122 };
123 
124 
133 template <typename ValueType, typename IndexType>
134 struct ell {
135  dim<2> size;
136  size_type num_stored_elements_per_row;
137  size_type stride;
138  ValueType* values;
139  IndexType* col_idxs;
140 
142  constexpr ell(dim<2> size, size_type num_stored_elements_per_row,
143  size_type stride, ValueType* values, IndexType* col_idxs)
144  : size{size},
145  num_stored_elements_per_row{num_stored_elements_per_row},
146  stride{stride},
147  values{values},
148  col_idxs(col_idxs)
149  {
150  assert(stride >= size[0]);
151  }
152 
155  {
157  size, num_stored_elements_per_row, stride, values, col_idxs};
158  }
159 
161  constexpr ValueType& val_at(size_type row, size_type idx) const
162  {
163  return values[this->linearize_index(row, idx)];
164  }
165 
167  constexpr IndexType& col_at(size_type row, size_type idx) const
168  {
169  return col_idxs[this->linearize_index(row, idx)];
170  }
171 
172 private:
174  constexpr size_type linearize_index(size_type row,
175  size_type idx) const noexcept
176  {
177  assert(idx < num_stored_elements_per_row && row < size[0]);
178  return row + stride * idx;
179  }
180 };
181 
182 
191 template <typename ValueType, typename IndexType>
192 struct sellp {
193  dim<2> size;
194  size_type slice_size;
195  size_type stride_factor;
196  size_type total_cols;
197  ValueType* values;
198  IndexType* col_idxs;
199  static_assert(std::is_const_v<ValueType> == std::is_const_v<IndexType>,
200  "ValueType and IndexType must share the same constness");
201  using adapt_size_type = std::conditional_t<std::is_const_v<ValueType>,
202  const size_type, size_type>;
203  adapt_size_type* slice_lengths;
204  adapt_size_type* slice_sets;
205 
207  constexpr sellp(dim<2> size, size_type slice_size, size_type stride_factor,
208  size_type total_cols, ValueType* values,
209  IndexType* col_idxs, adapt_size_type* slice_lengths,
210  adapt_size_type* slice_sets)
211  : size{size},
212  slice_size{slice_size},
213  stride_factor{stride_factor},
214  total_cols{total_cols},
215  values{values},
216  col_idxs{col_idxs},
217  slice_lengths{slice_lengths},
218  slice_sets{slice_sets}
219  {}
220 
223  {
225  size, slice_size, stride_factor, total_cols,
226  values, col_idxs, slice_lengths, slice_sets};
227  }
228 
230  constexpr ValueType& val_at(size_type row, size_type slice_set,
231  size_type idx) const
232  {
233  return values[this->linearize_index(row, slice_set, idx)];
234  }
235 
237  constexpr IndexType& col_at(size_type row, size_type slice_set,
238  size_type idx) const
239  {
240  return col_idxs[this->linearize_index(row, slice_set, idx)];
241  }
242 
243 private:
245  constexpr size_type linearize_index(size_type row, size_type slice_set,
246  size_type idx) const noexcept
247  {
248  assert(row < slice_size);
249  // note the following does not catch all idx out of bound access.
250  assert(idx < total_cols);
251  return (slice_set + idx) * slice_size + row;
252  }
253 };
254 
255 
264 template <typename ValueType, typename IndexType>
265 struct hybrid {
266  static_assert(std::is_const_v<ValueType> == std::is_const_v<IndexType>,
267  "ValueType and IndexType must share the same constness");
268  dim<2> size;
269  ell<ValueType, IndexType> ell_part;
270  coo<ValueType, IndexType> coo_part;
271 
275  : size(ell_.size), ell_part(ell_), coo_part(coo_)
276  {
277  assert(ell_part.size == coo_part.size);
278  }
279 
282  {
283  return {ell_part.as_const(), coo_part.as_const()};
284  }
285 };
286 
287 
288 } // namespace view
289 } // namespace matrix
290 } // namespace gko
291 
292 
293 #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:154
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:167
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:142
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:101
gko::matrix::view::ell
Non-owning view of a matrix::Ell to be used inside device kernels.
Definition: device_views.hpp:134
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:230
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:281
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:222
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:161
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:237
gko::matrix::view::csr::csr
constexpr csr(dim< 2 > size, size_type num_stored_elements, ValueType *values, IndexType *row_ptrs, IndexType *col_idxs)
Constructs a coo view from size, nnz, values, row pointers, and column indices.
Definition: device_views.hpp:107
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:207
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::csr::as_const
constexpr csr< const ValueType, const IndexType > as_const() const
Returns a const view of the same data.
Definition: device_views.hpp:117
gko::matrix::view::hybrid
Non-owning view of a matrix::Hybrid to be used inside device kernels.
Definition: device_views.hpp:265
gko::matrix::view::sellp
Non-owning view of a matrix::Sellp to be used inside device kernels.
Definition: device_views.hpp:192
gko::matrix::view::csr
Non-owning view of a matrix::Csr to be used inside device kernels.
Definition: device_views.hpp:98
gko::matrix::view::hybrid::hybrid
constexpr hybrid(ell< ValueType, IndexType > ell_, coo< ValueType, IndexType > coo_)
Constructs a hybrid view.
Definition: device_views.hpp:273