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