NeoN
A framework for CFD software
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#include <type_traits>
8
10
11namespace NeoN
12{
13
14/* @class View
15 *
16 * @brief A wrapper class for std::span which allows to check whether the index access is in range
17 * The View can be initialized like a regular std::span or from an existing std::span
18 *
19 * @ingroup core
20 *
21 */
22template<typename ValueType>
23class View : public std::span<ValueType>
24{
25public:
26
27 using base = std::span<ValueType>;
28
29 /* A flag to control whether the program should terminate on invalid memory access or throw.
30 * Kokkos prefers to terminate, but for testing purpose the failureIndex is preferred
31 */
32 bool abort = true;
33
34 /* a member to store the first out of range data access. This assumes a span has
35 * at least a size of 1. A value of zero signals success. This is required we cannot
36 * throw from a device function.
37 */
39
40 using std::span<ValueType>::span; // Inherit constructors from std::span
41
42 /* Constructor from existing std::span
43 */
44 KOKKOS_INLINE_FUNCTION
45 View(std::span<ValueType> in) : View(in.begin(), in.end()) {}
46
47 constexpr ValueType& operator[](localIdx index) const
48 {
49#ifdef NF_DEBUG
50 if (index >= this->size())
51 {
52 // TODO: currently this is failing on our AWS workflow, once we have clang>16 there
53 // this should work again.
54 // const std::string msg {"Index is out of range. Index: "} + to_string(index);
55 if (abort)
56 {
57 Kokkos::abort("Index is out of range");
58 }
59 else
60 {
61 // NOTE: throwing from a device function does not work
62 // throw std::invalid_argument("Index is out of range");
63 if (failureIndex == 0)
64 {
65 failureIndex = index;
66 }
67 return std::span<ValueType>::operator[](static_cast<size_t>(index));
68 }
69 }
70#endif
71 return std::span<ValueType>::operator[](static_cast<size_t>(index));
72 }
73
74 KOKKOS_INLINE_FUNCTION
75 localIdx size() const { return static_cast<localIdx>(base::size()); }
76
77 KOKKOS_INLINE_FUNCTION
79 {
80 return base::subspan(static_cast<size_t>(start), static_cast<size_t>(length));
81 }
82
83 KOKKOS_INLINE_FUNCTION
85 {
86 return base::subspan(static_cast<size_t>(start));
87 }
88};
89
94template<class Type>
95concept hasView =
96 requires(Type& inst) { inst.view(); } || requires(const Type& inst) { inst.view(); };
97
103template<typename... Types>
104 requires(hasView<std::remove_reference_t<Types>> && ...)
105auto views(Types&... args)
106{
107 return std::tuple(args.view()...);
108}
109
110} // namespace NeoN
std::span< ValueType > base
Definition view.hpp:27
KOKKOS_INLINE_FUNCTION View< ValueType > subview(localIdx start, localIdx length) const
Definition view.hpp:78
constexpr ValueType & operator[](localIdx index) const
Definition view.hpp:47
localIdx failureIndex
Definition view.hpp:38
KOKKOS_INLINE_FUNCTION View(std::span< ValueType > in)
Definition view.hpp:45
bool abort
Definition view.hpp:32
KOKKOS_INLINE_FUNCTION View< ValueType > subview(localIdx start) const
Definition view.hpp:84
KOKKOS_INLINE_FUNCTION localIdx size() const
Definition view.hpp:75
Concept, for any type which has the 'view' method.
Definition view.hpp:95
int32_t localIdx
Definition label.hpp:30
auto views(Types &... args)
Unpacks all views of the passed classes.
Definition view.hpp:105