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