MiniVector.hpp
Go to the documentation of this file.
1/*
2 Copyright 2025 Equinor ASA
3 This file is part of the Open Porous Media project (OPM).
4 OPM is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8 OPM is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with OPM. If not, see <http://www.gnu.org/licenses/>.
14*/
15
16#ifndef OPM_GPUISTL_MINIVECTOR_HPP
17#define OPM_GPUISTL_MINIVECTOR_HPP
18
19#include <algorithm>
20#include <array>
21#include <cstddef>
22#include <initializer_list>
23#include <stdexcept>
24#include <type_traits>
25
26#include <opm/common/ErrorMacros.hpp>
27#include <opm/common/utility/gpuDecorators.hpp>
28
29#if HAVE_DUNE_COMMON
30#include <dune/common/fvector.hh>
31#endif
32
43namespace Opm::gpuistl
44{
45
50template <class T, int Dimension>
52{
53 static_assert(Dimension > 0, "Dimension must be positive");
54
55public:
57 using value_type = T;
59 using size_type = std::size_t;
65 using iterator = typename std::array<T, Dimension>::iterator;
67 using const_iterator = typename std::array<T, Dimension>::const_iterator;
68
72 OPM_HOST_DEVICE constexpr MiniVector() noexcept(std::is_nothrow_default_constructible<value_type>::value) = default;
73
78 OPM_HOST_DEVICE constexpr explicit MiniVector(const value_type& value)
79 {
80 fill(value);
81 }
82
87 explicit MiniVector(const Dune::FieldVector<T, Dimension>& fv)
88 {
89 for (size_type i = 0; i < Dimension; ++i) {
90 data_[i] = fv[i];
91 }
92 }
93
106 OPM_HOST_DEVICE MiniVector(std::initializer_list<value_type> init)
107 {
108 if (init.size() != Dimension) {
109 OPM_THROW(std::runtime_error, "Opm::MiniVector – initializer‑list size mismatch");
110 }
111 std::copy_n(init.begin(), Dimension, data_.begin());
112 }
113
117 OPM_HOST_DEVICE constexpr reference operator[](size_type idx) noexcept
118 {
119 return data_[idx];
120 }
121
123 OPM_HOST_DEVICE constexpr const_reference operator[](size_type idx) const noexcept
124 {
125 return data_[idx];
126 }
127
132 OPM_HOST_DEVICE reference at(size_type idx)
133 {
134 if (idx >= Dimension) {
135 OPM_THROW(std::out_of_range, "Opm::MiniVector::at – index out of range");
136 }
137 return data_[idx];
138 }
140 OPM_HOST_DEVICE const_reference at(size_type idx) const
141 {
142 if (idx >= Dimension) {
143 OPM_THROW(std::out_of_range, "Opm::MiniVector::at – index out of range");
144 }
145 return data_[idx];
146 }
147
149 OPM_HOST_DEVICE constexpr iterator begin() noexcept
150 {
151 return data_.begin();
152 }
154 OPM_HOST_DEVICE constexpr const_iterator begin() const noexcept
155 {
156 return data_.begin();
157 }
159 OPM_HOST_DEVICE constexpr const_iterator cbegin() const noexcept
160 {
161 return data_.cbegin();
162 }
163
165 OPM_HOST_DEVICE constexpr iterator end() noexcept
166 {
167 return data_.end();
168 }
170 OPM_HOST_DEVICE constexpr const_iterator end() const noexcept
171 {
172 return data_.end();
173 }
175 OPM_HOST_DEVICE constexpr const_iterator cend() const noexcept
176 {
177 return data_.cend();
178 }
179
181 OPM_HOST_DEVICE static constexpr size_type size() noexcept
182 {
183 return Dimension;
184 }
185
186
191 OPM_HOST_DEVICE constexpr void fill(const value_type& value)
192 {
193 for (auto& x : data_) {
194 x = value;
195 }
196 }
197
199 OPM_HOST_DEVICE bool operator==(const MiniVector& other) const noexcept
200 {
201 for (size_type i = 0; i < Dimension; ++i) {
202 if (data_[i] != other.data_[i]) {
203 return false;
204 }
205 }
206 return true;
207 }
208
210 OPM_HOST_DEVICE bool operator!=(const MiniVector& other) const noexcept
211 {
212 return !(*this == other);
213 }
214
216 OPM_HOST_DEVICE MiniVector& operator=(const value_type& value)
217 {
218 fill(value);
219 return *this;
220 }
221
222 OPM_HOST_DEVICE MiniVector operator+(const value_type& value) const
223 {
224 MiniVector result = *this;
225 for (auto& x : result.data_) {
226 x += value;
227 }
228 return result;
229 }
230
231 OPM_HOST_DEVICE MiniVector& operator+=(const MiniVector& other)
232 {
233 for (size_type i = 0; i < Dimension; ++i) {
234 data_[i] += other.data_[i];
235 }
236 return *this;
237 }
238
239 OPM_HOST_DEVICE MiniVector operator+(const MiniVector& other) const
240 {
241 MiniVector result = *this;
242 result += other;
243 return result;
244 }
245
246 OPM_HOST_DEVICE MiniVector operator-(const MiniVector& other) const
247 {
248 MiniVector result = *this;
249 for (size_type i = 0; i < Dimension; ++i) {
250 result.data_[i] -= other.data_[i];
251 }
252 return result;
253 }
254
255 OPM_HOST_DEVICE MiniVector& operator-=(const MiniVector& other)
256 {
257 for (size_type i = 0; i < Dimension; ++i) {
258 data_[i] -= other.data_[i];
259 }
260 return *this;
261 }
262
264 OPM_HOST_DEVICE MiniVector& operator*=(const value_type& value)
265 {
266 for (auto& x : data_) {
267 x *= value;
268 }
269 return *this;
270 }
271
272private:
274 std::array<value_type, Dimension> data_ {};
275};
276
277} // namespace Opm::gpuistl
278
279#endif // OPM_GPUISTL_MINIVECTOR_HPP
Definition: MiniVector.hpp:52
MiniVector(const Dune::FieldVector< T, Dimension > &fv)
Conversion constructor from Dune::FieldVector.
Definition: MiniVector.hpp:87
T value_type
Element type.
Definition: MiniVector.hpp:57
OPM_HOST_DEVICE MiniVector operator+(const value_type &value) const
Definition: MiniVector.hpp:222
OPM_HOST_DEVICE reference at(size_type idx)
Safe element access with bounds checking (throws on host).
Definition: MiniVector.hpp:132
typename std::array< T, Dimension >::const_iterator const_iterator
Immutable iterator.
Definition: MiniVector.hpp:67
std::size_t size_type
Index/size type.
Definition: MiniVector.hpp:59
OPM_HOST_DEVICE constexpr const_iterator begin() const noexcept
Definition: MiniVector.hpp:154
OPM_HOST_DEVICE MiniVector & operator=(const value_type &value)
Definition: MiniVector.hpp:216
OPM_HOST_DEVICE MiniVector(std::initializer_list< value_type > init)
Initializer‑list constructor.
Definition: MiniVector.hpp:106
OPM_HOST_DEVICE constexpr void fill(const value_type &value)
Fill every component with the supplied value.
Definition: MiniVector.hpp:191
OPM_HOST_DEVICE bool operator!=(const MiniVector &other) const noexcept
Definition: MiniVector.hpp:210
OPM_HOST_DEVICE MiniVector operator+(const MiniVector &other) const
Definition: MiniVector.hpp:239
OPM_HOST_DEVICE constexpr const_iterator end() const noexcept
Definition: MiniVector.hpp:170
OPM_HOST_DEVICE constexpr iterator begin() noexcept
Definition: MiniVector.hpp:149
OPM_HOST_DEVICE bool operator==(const MiniVector &other) const noexcept
Definition: MiniVector.hpp:199
OPM_HOST_DEVICE MiniVector & operator+=(const MiniVector &other)
Definition: MiniVector.hpp:231
const value_type & const_reference
Immutable element reference.
Definition: MiniVector.hpp:63
static OPM_HOST_DEVICE constexpr size_type size() noexcept
Definition: MiniVector.hpp:181
OPM_HOST_DEVICE constexpr reference operator[](size_type idx) noexcept
Definition: MiniVector.hpp:117
OPM_HOST_DEVICE MiniVector operator-(const MiniVector &other) const
Definition: MiniVector.hpp:246
typename std::array< T, Dimension >::iterator iterator
Mutable iterator.
Definition: MiniVector.hpp:65
OPM_HOST_DEVICE constexpr const_reference operator[](size_type idx) const noexcept
Definition: MiniVector.hpp:123
OPM_HOST_DEVICE constexpr const_iterator cend() const noexcept
Definition: MiniVector.hpp:175
OPM_HOST_DEVICE constexpr MiniVector() noexcept(std::is_nothrow_default_constructible< value_type >::value)=default
Default‑constructs the MiniVector; elements are value‑initialized.
OPM_HOST_DEVICE constexpr iterator end() noexcept
Definition: MiniVector.hpp:165
OPM_HOST_DEVICE const_reference at(size_type idx) const
Safe element access with bounds checking (throws on host).
Definition: MiniVector.hpp:140
OPM_HOST_DEVICE MiniVector & operator*=(const value_type &value)
Definition: MiniVector.hpp:264
OPM_HOST_DEVICE constexpr const_iterator cbegin() const noexcept
Definition: MiniVector.hpp:159
value_type & reference
Mutable element reference.
Definition: MiniVector.hpp:61
OPM_HOST_DEVICE MiniVector & operator-=(const MiniVector &other)
Definition: MiniVector.hpp:255
A small, fixed‑dimension MiniVector class backed by std::array that can be used in both host and CUDA...
Definition: AmgxInterface.hpp:38