NeoN
A framework for CFD software
Loading...
Searching...
No Matches
view.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 - 2026 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
15
16namespace NeoN
17{
18
19/* @class View
20 *
21 * @brief A wrapper class for std::span which allows to check whether the index access is in range
22 * The View can be initialized like a regular std::span or from an existing std::span
23 *
24 * @ingroup core
25 *
26 */
27template<typename ValueType>
28class View : public std::span<ValueType>
29{
30public:
31
32 using base = std::span<ValueType>;
33
34 /* A flag to control whether the program should terminate on invalid memory access or throw.
35 * Kokkos prefers to terminate, but for testing purpose the failureIndex is preferred
36 */
37 bool abortOnFail = true;
38
39 /* a member to store the first out of range data access. This assumes a span has
40 * at least a size of 1. A value of zero signals success. This is required we cannot
41 * throw from a device function.
42 */
44
45 using std::span<ValueType>::span; // Inherit constructors from std::span
46
47 /* Constructor from existing std::span
48 */
49 KOKKOS_INLINE_FUNCTION
50 View(std::span<ValueType> in) : View(in.begin(), in.end()) {}
51
52 // KOKKOS_INLINE_FUNCTION (i.e. __host__ __device__) so element access is valid inside device
53 // kernels, consistent with the other accessors below. NOTE: this forwards to
54 // std::span::operator[], itself a bare constexpr __host__ function, so on CUDA correctness
55 // still relies on --expt-relaxed-constexpr (enabled via Kokkos_ENABLE_CUDA_CONSTEXPR=ON in
56 // cmake/AutoEnableDevice.cmake). Without that flag nvcc miscompiles the device write silently
57 // (warning 20013); the build promotes that warning to an error to prevent shipping it.
58 KOKKOS_INLINE_FUNCTION
59 constexpr ValueType& operator[](localIdx index) const
60 {
61#ifdef NF_DEBUG
62 if (index < 0 || static_cast<sizeType>(index) >= this->size())
63 {
64 if (abortOnFail)
65 {
66 Kokkos::abort("Index is out of range.");
67 }
68 else
69 {
70 return std::span<ValueType>::operator[](static_cast<sizeType>(index));
71 }
72 }
73#endif
74 return std::span<ValueType>::operator[](static_cast<sizeType>(index));
75 }
76
77 KOKKOS_INLINE_FUNCTION
78 sizeType size() const { return base::size(); }
79
80 KOKKOS_INLINE_FUNCTION
82 {
83 return base::subspan(static_cast<sizeType>(start), static_cast<sizeType>(length));
84 }
85
86 KOKKOS_INLINE_FUNCTION
88 {
89 return base::subspan(static_cast<sizeType>(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:32
KOKKOS_INLINE_FUNCTION View< ValueType > subview(localIdx start, localIdx length) const
Definition view.hpp:81
KOKKOS_INLINE_FUNCTION sizeType size() const
Definition view.hpp:78
localIdx failureIndex
Definition view.hpp:43
KOKKOS_INLINE_FUNCTION View(std::span< ValueType > in)
Definition view.hpp:50
bool abortOnFail
Definition view.hpp:37
KOKKOS_INLINE_FUNCTION View< ValueType > subview(localIdx start) const
Definition view.hpp:87
KOKKOS_INLINE_FUNCTION constexpr ValueType & operator[](localIdx index) const
Definition view.hpp:59
Concept, for any type which has the 'view' method.
Definition view.hpp:98
Integer types used throughout NeoN.
Definition array.hpp:18
std::size_t sizeType
Definition label.hpp:56
int32_t localIdx
Definition label.hpp:50
auto views(Types &... args)
Unpacks all views of the passed classes.
Definition view.hpp:108