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
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 // KOKKOS_INLINE_FUNCTION (i.e. __host__ __device__) so element access is valid inside device
52 // kernels, consistent with the other accessors below. NOTE: this forwards to
53 // std::span::operator[], itself a bare constexpr __host__ function, so on CUDA correctness
54 // still relies on --expt-relaxed-constexpr (enabled via Kokkos_ENABLE_CUDA_CONSTEXPR=ON in
55 // cmake/AutoEnableDevice.cmake). Without that flag nvcc miscompiles the device write silently
56 // (warning 20013); the build promotes that warning to an error to prevent shipping it.
57 KOKKOS_INLINE_FUNCTION
58 constexpr ValueType& operator[](localIdx index) const
59 {
60#ifdef NF_DEBUG
61 if (index < 0 || index >= this->size())
62 {
63 if (abortOnFail)
64 {
65 Kokkos::abort("Index is out of range.");
66 }
67 else
68 {
69 return std::span<ValueType>::operator[](static_cast<size_t>(index));
70 }
71 }
72#endif
73 return std::span<ValueType>::operator[](static_cast<size_t>(index));
74 }
75
76 KOKKOS_INLINE_FUNCTION
77 localIdx size() const { return static_cast<localIdx>(base::size()); }
78
79 KOKKOS_INLINE_FUNCTION
81 {
82 return base::subspan(static_cast<size_t>(start), static_cast<size_t>(length));
83 }
84
85 KOKKOS_INLINE_FUNCTION
87 {
88 return base::subspan(static_cast<size_t>(start));
89 }
90};
91
96template<class Type>
97concept hasView =
98 requires(Type& inst) { inst.view(); } || requires(const Type& inst) { inst.view(); };
99
105template<typename... Types>
106 requires(hasView<std::remove_reference_t<Types>> && ...)
107auto views(Types&... args)
108{
109 return std::tuple(args.view()...);
110}
111
112} // namespace NeoN
std::span< ValueType > base
Definition view.hpp:31
KOKKOS_INLINE_FUNCTION View< ValueType > subview(localIdx start, localIdx length) const
Definition view.hpp:80
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:86
KOKKOS_INLINE_FUNCTION localIdx size() const
Definition view.hpp:77
KOKKOS_INLINE_FUNCTION constexpr ValueType & operator[](localIdx index) const
Definition view.hpp:58
Concept, for any type which has the 'view' method.
Definition view.hpp:97
Integer types used throughout NeoN.
Definition array.hpp:18
int32_t localIdx
Definition label.hpp:50
auto views(Types &... args)
Unpacks all views of the passed classes.
Definition view.hpp:107