Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
range_accessors.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_BASE_RANGE_ACCESSORS_HPP_
34#define GKO_PUBLIC_CORE_BASE_RANGE_ACCESSORS_HPP_
35
36
37#include <array>
38
39
40#include <ginkgo/core/base/range.hpp>
41#include <ginkgo/core/base/types.hpp>
42
43
44namespace gko {
50namespace accessor {
51
52
68template <typename ValueType, size_type Dimensionality>
69class row_major {
70public:
71 friend class range<row_major>;
72
73 static_assert(Dimensionality == 2,
74 "This accessor is only implemented for matrices");
75
79 using value_type = ValueType;
80
85
89 static constexpr size_type dimensionality = 2;
90
91protected:
102 constexpr GKO_ATTRIBUTES explicit row_major(data_type data,
107 {}
108
109public:
118 constexpr GKO_ATTRIBUTES value_type& operator()(size_type row,
119 size_type col) const
120 {
121 return GKO_ASSERT(row < lengths[0]), GKO_ASSERT(col < lengths[1]),
122 data[row * stride + col];
123 }
124
133 constexpr GKO_ATTRIBUTES range<row_major> operator()(const span& rows,
134 const span& cols) const
135 {
136 return GKO_ASSERT(rows.is_valid()), GKO_ASSERT(cols.is_valid()),
137 GKO_ASSERT(rows <= span{lengths[0]}),
138 GKO_ASSERT(cols <= span{lengths[1]}),
139 range<row_major>(data + rows.begin * stride + cols.begin,
140 rows.end - rows.begin, cols.end - cols.begin,
141 stride);
142 }
143
151 constexpr GKO_ATTRIBUTES size_type length(size_type dimension) const
152 {
153 return dimension < 2 ? lengths[dimension] : 1;
154 }
155
168 template <typename OtherAccessor>
169 GKO_ATTRIBUTES void copy_from(const OtherAccessor& other) const
170 {
171 for (size_type i = 0; i < lengths[0]; ++i) {
172 for (size_type j = 0; j < lengths[1]; ++j) {
173 (*this)(i, j) = other(i, j);
174 }
175 }
176 }
177
182
186 const std::array<const size_type, dimensionality> lengths;
187
192};
193
194
195} // namespace accessor
196} // namespace gko
197
198
199#endif // GKO_PUBLIC_CORE_BASE_RANGE_ACCESSORS_HPP_
A row_major accessor is a bridge between a range and the row-major memory layout.
Definition range_accessors.hpp:69
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:133
constexpr size_type length(size_type dimension) const
Returns the length in dimension dimension.
Definition range_accessors.hpp:151
const std::array< const size_type, dimensionality > lengths
An array of dimension sizes.
Definition range_accessors.hpp:186
const size_type stride
Distance between consecutive rows.
Definition range_accessors.hpp:191
ValueType value_type
Type of values returned by the accessor.
Definition range_accessors.hpp:79
void copy_from(const OtherAccessor &other) const
Copies data from another accessor.
Definition range_accessors.hpp:169
const data_type data
Reference to the underlying data.
Definition range_accessors.hpp:181
static constexpr size_type dimensionality
Number of dimensions of the accessor.
Definition range_accessors.hpp:89
constexpr value_type & operator()(size_type row, size_type col) const
Returns the data element at position (row, col)
Definition range_accessors.hpp:118
value_type * data_type
Type of underlying data storage.
Definition range_accessors.hpp:84
A range is a multidimensional view of the memory.
Definition range.hpp:326
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
A span is a lightweight structure used to create sub-ranges from other ranges.
Definition range.hpp:75