DNDSR 0.1.0.dev1+gcd065ad
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
DeviceView.hpp
Go to the documentation of this file.
1#pragma once
2/// @file DeviceView.hpp
3/// @brief Non-owning device-side views of Array objects for host and CUDA backends.
4
5#include "ArrayBasic.hpp"
6#include "DNDS/Defines.hpp"
8#include "DNDS/Errors.hpp"
9
10namespace DNDS
11{
12 // template <class T, rowsize _row_size = 1, rowsize _row_max = _row_size, rowsize _align = NoAlign>
13 // class ArrayDeviceViewBase : public ArrayView<T, _row_size, _row_max, _align>
14 // {
15 // public:
16 // virtual DeviceBackend backend() = 0;
17 // virtual ~ArrayDeviceViewBase() = default;
18 // };
19
20 /**
21 * @brief Non-owning device-callable view of an @ref DNDS::Array "Array", specialised per @ref DNDS::DeviceBackend "DeviceBackend".
22 *
23 * @details The primary template is deleted; concrete specialisations are
24 * provided for each supported backend (Host always, CUDA when
25 * @ref DNDS_USE_CUDA is on). A device view captures host and/or device raw
26 * pointers; on the CUDA specialisation the element accessors are marked
27 * `__device__` so kernels can use them directly.
28 *
29 * Produced by `Array::deviceView<B>()`.
30 *
31 * @tparam B Device backend.
32 * @tparam T Element type (may be `const T` for read-only views).
33 * @tparam _row_size Mirrors @ref DNDS::Array "Array".
34 * @tparam _row_max Mirrors @ref DNDS::Array "Array".
35 * @tparam _align Mirrors @ref DNDS::Array "Array".
36 */
37 template <DeviceBackend B,
38 class T, rowsize _row_size = 1, rowsize _row_max = _row_size, rowsize _align = NoAlign>
39 class ArrayDeviceView : public ArrayView<T, _row_size, _row_max, _align>
40 {
41 public:
42 ArrayDeviceView() = delete;
43 };
44
45 /// @brief Host specialisation of @ref DNDS::ArrayDeviceView "ArrayDeviceView"; identical to the host-side @ref DNDS::ArrayView "ArrayView".
46 template <class T, rowsize _row_size, rowsize _row_max, rowsize _align>
48 DeviceBackend::Host, T, _row_size, _row_max, _align>
49 : public ArrayView<T, _row_size, _row_max, _align>
50 {
51 public:
53 using t_base::t_base;
54 using typename t_base::RowView;
55
57 // everything uses inherited
58 };
59
60#ifdef DNDS_USE_CUDA
61 /// @brief CUDA specialisation of @ref DNDS::ArrayDeviceView "ArrayDeviceView" with `__device__`
62 /// element accessors suitable for use inside kernels.
63 template <class T, rowsize _row_size, rowsize _row_max, rowsize _align>
64 class ArrayDeviceView<
65 DeviceBackend::CUDA, T, _row_size, _row_max, _align>
66 : public ArrayView<T, _row_size, _row_max, _align>
67 {
68 public:
69 using t_base = ArrayView<T, _row_size, _row_max, _align>;
70 using t_base::t_base;
71 using typename t_base::RowView;
72
74 DeviceBackend::CUDA, T, _row_size, _row_max, _align>;
75
76 static DeviceBackend backend() { return DeviceBackend::CUDA; }
77
78 /// @brief Device-side mutable element access. Uses the compressed-row path.
80 {
81 return const_cast<T &>(this->at_compressed(iRow, iCol));
82 }
83
84 /// @brief Device-side const element access.
85 DNDS_DEVICE_CALLABLE const T &operator()(index iRow, rowsize iCol = 0) const
86 {
87 return this->at_compressed(iRow, iCol);
88 }
89
90 /// @brief Device-side row pointer.
92 {
93 return this->get_rowstart_pointer_compressed(iRow);
94 }
95
96 DNDS_DEVICE_CALLABLE const T *operator[](index iRow) const
97 {
98 return static_cast<const T *>(const_cast<self_type *>(this)->operator[](iRow));
99 }
100 };
101#endif
102
103 /// @brief Factory for @ref DNDS::ArrayDeviceView "ArrayDeviceView": selects host or device pointers
104 /// based on `B` and forwards everything else.
105 /// @details Used by `Array::deviceView<B>()`; rarely called directly.
106 template <DeviceBackend B, class T, rowsize _row_size, rowsize _row_max, rowsize _align>
108 index n_size, T *n_data, index n_data_size,
109 const index *n_rowstart, index n_rowstart_size,
110 const rowsize *n_rowsizes, index n_rowsizes_size,
111 rowsize n_row_size_dynamic,
112 T *n_data_device,
113 const index *n_rowstart_device,
114 const rowsize *n_rowsizes_device)
115 {
117 DNDS_assert_info(B != DeviceBackend::Custom1, "unimplemented");
118 if constexpr (B == DeviceBackend::Host)
120 n_size, n_data, n_data_size,
121 n_rowstart, n_rowstart_size,
122 n_rowsizes, n_rowsizes_size,
123 n_row_size_dynamic,
124 true, nullptr);
125#ifdef DNDS_USE_CUDA
126 else if constexpr (B == DeviceBackend::CUDA)
128 n_size, n_data_device, n_data_size,
129 n_rowstart_device, n_rowstart_size,
130 n_rowsizes_device, n_rowsizes_size,
131 n_row_size_dynamic,
132 true, nullptr);
133#endif
134 else
136 n_size, n_data, n_data_size,
137 n_rowstart, n_rowstart_size,
138 n_rowsizes, n_rowsizes_size,
139 n_row_size_dynamic,
140 true, nullptr);
141 };
142}
Array layout descriptors, non-owning views, row views, and iterator base.
Core type aliases, constants, and metaprogramming utilities for the DNDS framework.
#define DNDS_DEVICE_CALLABLE
Definition Defines.hpp:76
Device memory abstraction layer with backend-specific storage and factory creation.
Assertion / error-handling macros and supporting helper functions.
#define DNDS_assert_info(expr, info)
Debug-only assertion with an extra std::string info message.
Definition Errors.hpp:113
#define DNDS_assert(expr)
Debug-only assertion (compiled out when DNDS_NDEBUG is defined). Prints the expression + file/line + ...
Definition Errors.hpp:108
Non-owning device-callable view of an Array, specialised per DeviceBackend.
Non-owning, device-callable view onto an Array.
ArrayView< T, _row_size, _row_max, _align > self_type
T * operator[](index iRow)
Raw row pointer. iRow == Size() is allowed for past-the-end queries (useful for computing buffer end ...
DNDS_DEVICE_CALLABLE T * get_rowstart_pointer_compressed(index iRow)
T & operator()(index iRow, rowsize iCol=0)
2D indexed access (writable). See at.
DNDS_DEVICE_CALLABLE const T & at_compressed(index iRow, rowsize iCol) const
the host side operators are provided as implemented
DeviceBackend
Enumerates the backends a DeviceStorage / Array can live on.
@ Unknown
Unset / sentinel.
@ Custom1
Reserved slot for a project-specific backend.
@ Host
Plain CPU memory.
auto ArrayDeviceView_build(index n_size, T *n_data, index n_data_size, const index *n_rowstart, index n_rowstart_size, const rowsize *n_rowsizes, index n_rowsizes_size, rowsize n_row_size_dynamic, T *n_data_device, const index *n_rowstart_device, const rowsize *n_rowsizes_device)
Factory for ArrayDeviceView: selects host or device pointers based on B and forwards everything else.
DNDS_CONSTANT const rowsize NoAlign
Alignment flag: no padding applied to rows (the only currently-supported value).
Definition Defines.hpp:282
int32_t rowsize
Row-width / per-row element-count type (signed 32-bit).
Definition Defines.hpp:109
int64_t index
Global row / DOF index type (signed 64-bit; handles multi-billion-cell meshes).
Definition Defines.hpp:107