Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
generated.hpp
Go to the documentation of this file.
1#pragma once
2
18
19#include <array>
20#include <concepts>
21#include <cstddef>
22#include <tuple>
23#include <type_traits>
24#include <utility>
25
26namespace uni20
27{
28namespace detail
29{
30
31template <class ElementType> struct FullGenerator
32{
33 ElementType value;
34
35 template <class Indices> [[nodiscard]] UNI20_HOST_DEVICE constexpr ElementType operator()(Indices const&) const
36 {
37 return value;
38 }
39};
40
41template <class ElementType> struct ZeroGenerator
42{
43 template <class Indices> [[nodiscard]] UNI20_HOST_DEVICE constexpr ElementType operator()(Indices const&) const
44 {
45 return ElementType{0};
46 }
47};
48
49template <class ElementType> struct OneGenerator
50{
51 template <class Indices> [[nodiscard]] UNI20_HOST_DEVICE constexpr ElementType operator()(Indices const&) const
52 {
53 return ElementType{1};
54 }
55};
56
57template <class ElementType> struct EyeGenerator
58{
59 template <class Indices>
60 [[nodiscard]] UNI20_HOST_DEVICE constexpr ElementType operator()(Indices const& indices) const
61 {
62 if constexpr (std::tuple_size_v<std::remove_cvref_t<Indices>> > 1)
63 {
64 for (std::size_t axis = 1; axis < indices.size(); ++axis)
65 if (indices[axis] != indices[0]) return ElementType{0};
66 }
67 return ElementType{1};
68 }
69};
70
71} // namespace detail
72
77template <Scalar ElementType, class Extents, class Generator> class GeneratedTensor {
78 public:
79 using element_type = ElementType;
81 using extents_type = Extents;
82 using generator_type = Generator;
85 using index_type = typename extents_type::index_type;
87 using mapping_type = typename layout_type::template mapping<extents_type>;
89 using mdspan_type = stdex::mdspan<element_type const, extents_type, layout_type, accessor_type>;
90
93 : extents_(std::move(extents)), generator_(std::move(generator))
94 {}
95
97 [[nodiscard]] static constexpr auto backend_selector() noexcept -> backend_selector_type
98 {
100 }
101
103 [[nodiscard]] constexpr auto mdspan() const -> mdspan_type
104 {
105 return mdspan_type{generated_data_handle{}, mapping_type{extents_}, accessor_type{extents_, generator_}};
106 }
107
109 [[nodiscard]] constexpr auto extents() const noexcept -> extents_type const& { return extents_; }
110
112 [[nodiscard]] constexpr auto extent(std::size_t axis) const noexcept -> index_type { return extents_.extent(axis); }
113
115 [[nodiscard]] static constexpr std::size_t rank() noexcept { return extents_type::rank(); }
116
118 template <class... Index>
119 requires(sizeof...(Index) == extents_type::rank())
120 [[nodiscard]] constexpr element_type operator[](Index... indices) const
121 {
122 return this->mdspan()[indices...];
123 }
124
125 private:
126 [[no_unique_address]] extents_type extents_{};
127 [[no_unique_address]] generator_type generator_{};
128};
129
131template <Scalar ElementType, std::integral... Extents> [[nodiscard]] auto full(ElementType value, Extents... extents)
132{
133 auto shape = detail::make_tensor_extents(extents...);
134 using shape_type = decltype(shape);
135 using generator_type = detail::FullGenerator<ElementType>;
136 return GeneratedTensor<ElementType, shape_type, generator_type>{std::move(shape), generator_type{std::move(value)}};
137}
138
140template <Scalar ElementType, std::integral... Extents> [[nodiscard]] auto zeros(Extents... extents)
141{
142 auto shape = detail::make_tensor_extents(extents...);
143 using shape_type = decltype(shape);
144 using generator_type = detail::ZeroGenerator<ElementType>;
145 return GeneratedTensor<ElementType, shape_type, generator_type>{std::move(shape), generator_type{}};
146}
147
149template <Scalar ElementType, std::integral... Extents> [[nodiscard]] auto ones(Extents... extents)
150{
151 auto shape = detail::make_tensor_extents(extents...);
152 using shape_type = decltype(shape);
153 using generator_type = detail::OneGenerator<ElementType>;
154 return GeneratedTensor<ElementType, shape_type, generator_type>{std::move(shape), generator_type{}};
155}
156
161template <Scalar ElementType, std::integral... Extents> [[nodiscard]] auto eye(Extents... extents)
162{
163 auto shape = detail::make_tensor_extents(extents...);
164 using shape_type = decltype(shape);
165 using generator_type = detail::EyeGenerator<ElementType>;
166 return GeneratedTensor<ElementType, shape_type, generator_type>{std::move(shape), generator_type{}};
167}
168
169} // namespace uni20
Compact read-only tensor evaluated from logical indices.
Definition generated.hpp:77
Extents extents_type
Definition generated.hpp:81
typename extents_type::index_type index_type
Definition generated.hpp:85
element_type value_type
Definition generated.hpp:80
constexpr auto mdspan() const -> mdspan_type
Resolve a self-contained generated mdspan descriptor.
Definition generated.hpp:103
static constexpr auto backend_selector() noexcept -> backend_selector_type
Return the host fallback selector used when no concrete storage operand is present.
Definition generated.hpp:97
constexpr auto extent(std::size_t axis) const noexcept -> index_type
Return one generated tensor extent.
Definition generated.hpp:112
constexpr GeneratedTensor(extents_type extents, generator_type generator)
Construct from extents and compact generator state.
Definition generated.hpp:92
constexpr auto extents() const noexcept -> extents_type const &
Return the generated tensor extents.
Definition generated.hpp:109
ElementType element_type
Definition generated.hpp:79
static constexpr std::size_t rank() noexcept
Return the tensor's static rank.
Definition generated.hpp:115
typename storage_policy::backend_selector_type backend_selector_type
Definition generated.hpp:84
Generator generator_type
Definition generated.hpp:82
stdex::mdspan< element_type const, extents_type, layout_type, accessor_type > mdspan_type
Definition generated.hpp:89
typename layout_type::template mapping< extents_type > mapping_type
Definition generated.hpp:87
Accessor that evaluates a generator at the logical index encoded by an offset.
Definition generated_accessor.hpp:33
Project compiler annotations shared by host and device code.
#define UNI20_HOST_DEVICE
Definition compiler_attributes.hpp:12
Concept for numeric scalar types (integer, real, or complex).
Definition scalar_concepts.hpp:81
locale_t value
Definition data_table.cpp:49
Read-only mdspan accessor for values generated from logical indices.
Synthetic mdspan layout for values generated from logical indices.
Storage policy marker for compact generated tensor values.
Configured gateway to Uni20's mdspan implementation.
Definition qnum.hpp:403
Definition async/async.hpp:26
auto zeros(Extents... extents)
Return a generated tensor filled with scalar zeros.
Definition generated.hpp:140
auto eye(Extents... extents)
Return a generated tensor equal to one exactly when all indices agree.
Definition generated.hpp:161
auto ones(Extents... extents)
Return a generated tensor filled with scalar ones.
Definition generated.hpp:149
auto full(ElementType value, Extents... extents)
Return a generated tensor filled with one scalar value.
Definition generated.hpp:131
Checked helpers for constructing runtime tensor extents.
Synthetic non-strided layout used by generated tensor accessors.
Definition generated_layout.hpp:23
Backend-neutral storage policy for tensors generated from compact state.
Definition generated_storage.hpp:19
linalg::backend_list< linalg::CpuReferenceBackend > backend_selector_type
Definition generated_storage.hpp:20
static constexpr auto backend_selector() noexcept -> backend_selector_type
Return the host reference fallback used when no concrete operand selects a backend.
Definition generated_storage.hpp:23
Compact mdspan handle identifying a base offset in a generated value sequence.
Definition generated_accessor.hpp:22
Concepts for tensor-level objects that resolve mdspan kernel operands.