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