Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
generated_accessor.hpp
Go to the documentation of this file.
1#pragma once
2
11
12#include <array>
13#include <cstddef>
14#include <type_traits>
15#include <utility>
16
17namespace uni20
18{
19
22{
23 std::size_t base_offset = 0;
24
26 generated_data_handle const&) = default;
27};
28
33template <class ElementType, class Extents, class Generator> class generated_accessor {
34 public:
35 using value_type = std::remove_cv_t<ElementType>;
36 using element_type = value_type const;
40 using offset_type = std::size_t;
41 using extents_type = Extents;
42 using generator_type = Generator;
43 using index_type = typename extents_type::index_type;
44
46 requires(std::default_initializable<extents_type> && std::default_initializable<generator_type>)
47 = default;
48
51 : extents_(static_cast<extents_type&&>(extents)), generator_(static_cast<generator_type&&>(generator))
52 {}
53
56 {
57 return generator_(this->decode(handle.base_offset + offset));
58 }
59
62 offset_type offset) const noexcept
63 {
65 }
66
68 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto extents() const noexcept -> extents_type const& { return extents_; }
69
71 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto generator() const noexcept -> generator_type const&
72 {
73 return generator_;
74 }
75
76 private:
77 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto decode(std::size_t linear) const
78 -> std::array<index_type, extents_type::rank()>
79 {
80 std::array<index_type, extents_type::rank()> indices{};
81 for (std::size_t axis = extents_type::rank(); axis > 0; --axis)
82 {
83 auto const extent = static_cast<std::size_t>(extents_.extent(axis - 1));
84 indices[axis - 1] = static_cast<index_type>(linear % extent);
85 linear /= extent;
86 }
87 return indices;
88 }
89
90 [[no_unique_address]] extents_type extents_{};
91 [[no_unique_address]] generator_type generator_{};
92};
93
96template <class ElementType, class Extents, class Generator>
97inline constexpr bool
98 enable_accessor_in_domain<generated_accessor<ElementType, Extents, Generator>, host_access_domain> = true;
99
100template <class ElementType, class Extents, class Generator>
101inline constexpr bool
102 enable_accessor_in_domain<generated_accessor<ElementType, Extents, Generator>, cuda_access_domain> = true;
103
104} // namespace uni20
Accessor that evaluates a generator at the logical index encoded by an offset.
Definition generated_accessor.hpp:33
std::remove_cv_t< ElementType > value_type
Definition generated_accessor.hpp:35
UNI20_HOST_DEVICE constexpr auto generator() const noexcept -> generator_type const &
Return the stored generator state.
Definition generated_accessor.hpp:71
value_type const element_type
Definition generated_accessor.hpp:36
typename extents_type::index_type index_type
Definition generated_accessor.hpp:43
std::size_t offset_type
Definition generated_accessor.hpp:40
UNI20_HOST_DEVICE constexpr generated_accessor(extents_type extents, generator_type generator)
Construct an accessor from the original extents and generator state.
Definition generated_accessor.hpp:50
UNI20_HOST_DEVICE constexpr reference access(data_handle_type handle, offset_type offset) const
Generate the value at a handle-relative synthetic offset.
Definition generated_accessor.hpp:55
UNI20_HOST_DEVICE constexpr generated_accessor()=default
Extents extents_type
Definition generated_accessor.hpp:41
UNI20_HOST_DEVICE constexpr auto extents() const noexcept -> extents_type const &
Return the extents used to decode generated offsets.
Definition generated_accessor.hpp:68
UNI20_HOST_DEVICE constexpr data_handle_type offset(data_handle_type handle, offset_type offset) const noexcept
Advance a generated handle while preserving its original index space.
Definition generated_accessor.hpp:61
Generator generator_type
Definition generated_accessor.hpp:42
value_type reference
Definition generated_accessor.hpp:37
Project compiler annotations shared by host and device code.
#define UNI20_HOST_DEVICE
Definition compiler_attributes.hpp:12
cuda::ResourceLease< HandleSlot > handle
Definition cublas/execution.cpp:33
Mdspan concept and accessor extensions for Uni20.
Definition async/async.hpp:26
Execution domain in which CUDA device code may evaluate an accessor.
Definition mdspan/concepts.hpp:78
Compact mdspan handle identifying a base offset in a generated value sequence.
Definition generated_accessor.hpp:22
friend UNI20_HOST_DEVICE constexpr bool operator==(generated_data_handle const &, generated_data_handle const &)=default
std::size_t base_offset
Definition generated_accessor.hpp:23
Execution domain in which ordinary host code may evaluate an accessor.
Definition mdspan/concepts.hpp:74