NeoN
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
view.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025 NeoN authors
3#pragma once
4
5#include <limits>
6#include <span>
7
9
10namespace NeoN
11{
12
13/* @class Span
14 *
15 * @brief A wrapper class for std::span which allows to check whether the index access is in range
16 * The Span can be initialized like a regular std::span or from an existing std::span
17 *
18 * @ingroup core
19 *
20 */
21template<typename ValueType>
22class View : public std::span<ValueType>
23{
24public:
25
26 using base = std::span<ValueType>;
27
28 /* A flag to control whether the program should terminate on invalid memory access or throw.
29 * Kokkos prefers to terminate, but for testing purpose the failureIndex is preferred
30 */
31 bool abort = true;
32
33 /* a member to store the first out of range data access. This assumes a span has
34 * at least a size of 1. A value of zero signals success. This is required we cannot
35 * throw from a device function.
36 */
38
39 using std::span<ValueType>::span; // Inherit constructors from std::span
40
41 /* Constructor from existing std::span
42 */
43 View(std::span<ValueType> in) : View(in.begin(), in.end()) {}
44
45 constexpr ValueType& operator[](localIdx index) const
46 {
47#ifdef NF_DEBUG
48 if (index >= this->size())
49 {
50 // TODO: currently this is failing on our AWS workflow, once we have clang>16 there
51 // this should work again.
52 // const std::string msg {"Index is out of range. Index: "} + to_string(index);
53 if (abort)
54 {
55 Kokkos::abort("Index is out of range");
56 }
57 else
58 {
59 // NOTE: throwing from a device function does not work
60 // throw std::invalid_argument("Index is out of range");
61 if (failureIndex == 0)
62 {
63 failureIndex = index;
64 }
65 return std::span<ValueType>::operator[](static_cast<size_t>(index));
66 }
67 }
68#endif
69 return std::span<ValueType>::operator[](static_cast<size_t>(index));
70 }
71
72 localIdx size() const { return static_cast<localIdx>(base::size()); }
73
75 {
76 return base::subspan(static_cast<size_t>(start), static_cast<size_t>(length));
77 }
78
80 {
81 return base::subspan(static_cast<size_t>(start));
82 }
83};
84
85
86} // namespace NeoN
std::span< ValueType > base
Definition view.hpp:26
constexpr ValueType & operator[](localIdx index) const
Definition view.hpp:45
localIdx size() const
Definition view.hpp:72
localIdx failureIndex
Definition view.hpp:37
View< ValueType > subspan(localIdx start, localIdx length) const
Definition view.hpp:74
bool abort
Definition view.hpp:31
View< ValueType > subspan(localIdx start) const
Definition view.hpp:79
View(std::span< ValueType > in)
Definition view.hpp:43
int32_t localIdx
Definition label.hpp:30