Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
range_accessors.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_BASE_RANGE_ACCESSORS_HPP_
6 #define GKO_PUBLIC_CORE_BASE_RANGE_ACCESSORS_HPP_
7 
8 
9 #include <array>
10 
11 
12 #include <ginkgo/core/base/range.hpp>
13 #include <ginkgo/core/base/types.hpp>
14 
15 
16 namespace gko {
22 namespace accessor {
23 
24 
40 template <typename ValueType, size_type Dimensionality>
41 class row_major {
42 public:
43  friend class range<row_major>;
44 
45  static_assert(Dimensionality == 2,
46  "This accessor is only implemented for matrices");
47 
51  using value_type = ValueType;
52 
57 
61  static constexpr size_type dimensionality = 2;
62 
63 protected:
74  constexpr GKO_ATTRIBUTES explicit row_major(data_type data,
75  size_type num_rows,
76  size_type num_cols,
78  : data{data}, lengths{num_rows, num_cols}, stride{stride}
79  {}
80 
81 public:
90  constexpr GKO_ATTRIBUTES value_type& operator()(size_type row,
91  size_type col) const
92  {
93  return GKO_ASSERT(row < lengths[0]), GKO_ASSERT(col < lengths[1]),
94  data[row * stride + col];
95  }
96 
105  constexpr GKO_ATTRIBUTES range<row_major> operator()(const span& rows,
106  const span& cols) const
107  {
108  return GKO_ASSERT(rows.is_valid()), GKO_ASSERT(cols.is_valid()),
109  GKO_ASSERT(rows <= span{lengths[0]}),
110  GKO_ASSERT(cols <= span{lengths[1]}),
111  range<row_major>(data + rows.begin * stride + cols.begin,
112  rows.end - rows.begin, cols.end - cols.begin,
113  stride);
114  }
115 
123  constexpr GKO_ATTRIBUTES size_type length(size_type dimension) const
124  {
125  return dimension < 2 ? lengths[dimension] : 1;
126  }
127 
140  template <typename OtherAccessor>
141  GKO_ATTRIBUTES void copy_from(const OtherAccessor& other) const
142  {
143  for (size_type i = 0; i < lengths[0]; ++i) {
144  for (size_type j = 0; j < lengths[1]; ++j) {
145  (*this)(i, j) = other(i, j);
146  }
147  }
148  }
149 
154 
158  const std::array<const size_type, dimensionality> lengths;
159 
164 };
165 
166 
167 } // namespace accessor
168 } // namespace gko
169 
170 
171 #endif // GKO_PUBLIC_CORE_BASE_RANGE_ACCESSORS_HPP_
gko::accessor::row_major::operator()
constexpr value_type & operator()(size_type row, size_type col) const
Returns the data element at position (row, col)
Definition: range_accessors.hpp:90
gko::span::end
const size_type end
End of the span.
Definition: range.hpp:91
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:108
gko::accessor::row_major::operator()
constexpr range< row_major > operator()(const span &rows, const span &cols) const
Returns the sub-range spanning the range (rows, cols)
Definition: range_accessors.hpp:105
gko::accessor::row_major
A row_major accessor is a bridge between a range and the row-major memory layout.
Definition: range_accessors.hpp:41
gko::span::is_valid
constexpr bool is_valid() const
Checks if a span is valid.
Definition: range.hpp:74
gko::span::begin
const size_type begin
Beginning of the span.
Definition: range.hpp:86
gko::accessor::row_major::data
const data_type data
Reference to the underlying data.
Definition: range_accessors.hpp:153
gko::accessor::row_major::lengths
const std::array< const size_type, dimensionality > lengths
An array of dimension sizes.
Definition: range_accessors.hpp:158
gko::range
A range is a multidimensional view of the memory.
Definition: range.hpp:298
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::span
A span is a lightweight structure used to create sub-ranges from other ranges.
Definition: range.hpp:47
gko::accessor::row_major::stride
const size_type stride
Distance between consecutive rows.
Definition: range_accessors.hpp:163
gko::accessor::row_major::value_type
ValueType value_type
Type of values returned by the accessor.
Definition: range_accessors.hpp:51
gko::accessor::row_major::copy_from
void copy_from(const OtherAccessor &other) const
Copies data from another accessor.
Definition: range_accessors.hpp:141
gko::accessor::row_major::dimensionality
static constexpr size_type dimensionality
Number of dimensions of the accessor.
Definition: range_accessors.hpp:61
gko::accessor::row_major::length
constexpr size_type length(size_type dimension) const
Returns the length in dimension dimension.
Definition: range_accessors.hpp:123
gko::accessor::row_major::data_type
value_type * data_type
Type of underlying data storage.
Definition: range_accessors.hpp:56