Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
tensor/concepts.hpp
Go to the documentation of this file.
1
7#pragma once
8
10
11#include <concepts>
12#include <cstddef>
13#include <type_traits>
14#include <utility>
15
16namespace uni20
17{
18
19namespace detail
20{
21template <class T> using tensor_type_t = std::remove_reference_t<T>;
22
23template <class Tensor, class = void> struct TensorStoragePolicy
24{
25 using type = void;
26};
27
28template <class Tensor> struct TensorStoragePolicy<Tensor, std::void_t<typename Tensor::storage_policy>>
29{
30 using type = typename Tensor::storage_policy;
31};
32
33template <class T> using tensor_const_mdspan_t = decltype(std::declval<tensor_type_t<T> const&>().mdspan());
34
35template <class T> using tensor_mutable_mdspan_t = decltype(std::declval<tensor_type_t<T>&>().mdspan());
36} // namespace detail
37
39template <class Tensor>
40using tensor_storage_policy_t = typename detail::TensorStoragePolicy<std::remove_cvref_t<Tensor>>::type;
41
48template <class Tensor>
49 requires requires(Tensor& tensor) { tensor.mdspec(); }
50[[nodiscard]] constexpr decltype(auto) mdspec_of(Tensor& tensor) noexcept(noexcept(tensor.mdspec()))
51{
52 return tensor.mdspec();
53}
54
61template <class Tensor>
62 requires(
63 !requires(Tensor& tensor) { tensor.mdspec(); } && requires(Tensor& tensor) { tensor.mdspan(); })
64[[nodiscard]] constexpr decltype(auto) mdspec_of(Tensor& tensor) noexcept(noexcept(tensor.mdspan()))
65{
66 return tensor.mdspan();
67}
68
69namespace detail
70{
71template <class T> using normalized_const_mdspec_t = decltype(mdspec_of(std::declval<tensor_type_t<T> const&>()));
72
73template <class T> using normalized_mutable_mdspec_t = decltype(mdspec_of(std::declval<tensor_type_t<T>&>()));
74} // namespace detail
75
81template <class T>
82concept TensorView = (!MdspecLike<T>) && requires(std::remove_reference_t<T> const& tensor) {
83 typename std::remove_cvref_t<detail::normalized_const_mdspec_t<T>>::extents_type;
84 tensor.backend_selector();
85 mdspec_of(tensor);
86 {
87 tensor.extents()
88 } -> std::convertible_to<typename std::remove_cvref_t<detail::normalized_const_mdspec_t<T>>::extents_type>;
89 {
90 tensor.extent(std::size_t{})
91 } -> std::convertible_to<typename std::remove_cvref_t<detail::normalized_const_mdspec_t<T>>::index_type>;
93
99template <class T>
100concept ImmediateTensorView = TensorView<T> && requires(std::remove_reference_t<T> const& tensor) {
101 tensor.mdspan();
103
108template <class T> inline constexpr bool enable_owning_tensor = false;
109
115template <class T>
116concept OwningTensor = TensorView<T> && enable_owning_tensor<std::remove_cvref_t<T>>;
117
119template <ImmediateTensorView T>
120using immediate_tensor_mdspan_t = std::remove_cvref_t<detail::tensor_const_mdspan_t<T>>;
121
123template <TensorView T> using tensor_mdspec_t = std::remove_cvref_t<detail::normalized_const_mdspec_t<T>>;
124
126template <TensorView T> using tensor_element_t = std::remove_cv_t<typename tensor_mdspec_t<T>::element_type>;
127
129template <TensorView T> using tensor_extents_t = typename tensor_mdspec_t<T>::extents_type;
130
132template <class T>
133concept MutableTensorView = TensorView<T> && requires(std::remove_reference_t<T>& tensor) { mdspec_of(tensor); } &&
135
137template <class T>
140 requires(std::remove_reference_t<T>& tensor) { tensor.mdspan(); } &&
142
144template <MutableTensorView T>
145using mutable_tensor_mdspec_t = std::remove_cvref_t<detail::normalized_mutable_mdspec_t<T>>;
146
148template <class T, std::size_t Rank>
150
152template <class T>
154
156template <class T>
159
161template <class T, std::size_t Rank>
163
165template <class T, std::size_t Rank>
168
170template <class T>
172
174template <class T>
176
178template <class T, std::size_t Rank>
180
182template <MutableImmediateTensorView T>
183using mutable_immediate_tensor_mdspan_t = std::remove_cvref_t<detail::tensor_mutable_mdspan_t<T>>;
184
186template <class T>
189 StridedMdspanLike<decltype(std::declval<std::remove_reference_t<T> const&>().mdspan())>;
190
192template <class T>
195 MutableStridedMdspanLike<decltype(std::declval<std::remove_reference_t<T>&>().mdspan())>;
196
200template <class T, std::size_t Rank>
203 RankedMdspanLike<decltype(std::declval<std::remove_reference_t<T> const&>().mdspan()), Rank>;
204
208template <class T, std::size_t Rank>
212
214template <class T>
216
218template <class T>
220
222template <class T, std::size_t Rank>
225
227template <class T, std::size_t Rank>
231
236template <StridedTensorView T> [[nodiscard]] auto strides(T const& tensor) { return strides(mdspec_of(tensor)); }
237
241template <MutableTensorView Output, TensorView Input>
242 requires(tensor_mdspec_t<Output>::rank() == tensor_mdspec_t<Input>::rank())
243void assign_through(Output& output, Input const& input);
244
245} // namespace uni20
General-purpose owning tensor with immediate or deferred mdspan metadata.
Definition basic_tensor.hpp:109
auto mdspec() noexcept
Return mutable mdspan metadata for explicit data-handle acquisition.
Definition basic_tensor.hpp:626
StoragePolicy storage_policy
Definition basic_tensor.hpp:115
auto mdspan() noexcept -> mdspan_type
Resolve a writable mdspan over the owned storage.
Definition basic_tensor.hpp:609
TensorView whose mdspec is already an mdspan.
Definition tensor/concepts.hpp:100
A readable type implementing the mdspan observers and multidimensional subscript contract.
Definition mdspan/concepts.hpp:291
Mdspan metadata whose data handle is immediate or available through a data descriptor.
Definition mdspan/concepts.hpp:304
MutableTensorView whose writable mdspec is already an mdspan.
Definition tensor/concepts.hpp:138
MdspanLike types whose accessor supports indexed assignment.
Definition mdspan/concepts.hpp:387
MdspecLike types whose eventual storage supports writes.
Definition mdspan/concepts.hpp:398
MutableImmediateTensorView whose writable mdspan has a specified static rank.
Definition tensor/concepts.hpp:209
A mutable mdspan-like type with a specified static rank.
Definition mdspan/concepts.hpp:494
Mutable mdspec-like type with a specified static rank.
Definition mdspan/concepts.hpp:487
Mutable strided tensor-level object whose resolved mdspan has a specified static rank.
Definition tensor/concepts.hpp:228
Mutable strided TensorView with a specified static rank.
Definition tensor/concepts.hpp:179
MutableTensorView with a specified static rank.
Definition tensor/concepts.hpp:166
Mutable tensor-level object whose writable resolved mdspan has rank zero.
Definition tensor/concepts.hpp:219
MutableTensorView whose writable mdspec has rank zero.
Definition tensor/concepts.hpp:175
MutableImmediateTensorView whose readable and writable mdspans are strided.
Definition tensor/concepts.hpp:193
Mutable mdspan-like types whose layout reports they are always strided.
Definition mdspan/concepts.hpp:421
Mutable mdspec-like type whose mapping is always strided.
Definition mdspan/concepts.hpp:440
MutableTensorView whose readable and writable mdspecs are strided.
Definition tensor/concepts.hpp:157
TensorView whose writable mdspec supports eventual element assignment.
Definition tensor/concepts.hpp:133
Tensor-level object that owns the storage described by its multidimensional view.
Definition tensor/concepts.hpp:116
ImmediateTensorView whose readable mdspan has a specified static rank.
Definition tensor/concepts.hpp:201
An mdspan-like type with a specified static rank.
Definition mdspan/concepts.hpp:447
An mdspec-like type with a specified static rank.
Definition mdspan/concepts.hpp:454
Strided tensor-level object whose resolved mdspan has a specified static rank.
Definition tensor/concepts.hpp:223
StridedTensorView with a specified static rank.
Definition tensor/concepts.hpp:162
TensorView whose readable mdspec has a specified static rank.
Definition tensor/concepts.hpp:149
Tensor-level object whose readable resolved mdspan has rank zero.
Definition tensor/concepts.hpp:215
TensorView whose readable mdspec has rank zero.
Definition tensor/concepts.hpp:171
ImmediateTensorView whose readable mdspan is strided.
Definition tensor/concepts.hpp:187
An mdspan-like type whose mapping is always strided and exposes stride observers.
Definition mdspan/concepts.hpp:406
An mdspec-like type whose mapping exposes multidimensional strides.
Definition mdspan/concepts.hpp:427
TensorView whose readable mdspec is strided.
Definition tensor/concepts.hpp:153
Tensor-level object exposing readable multidimensional metadata and a backend selector.
Definition tensor/concepts.hpp:82
auto strides(S const &s)
Retrieve the strides associated with a strided mdspec-like type.
Definition mdspan/concepts.hpp:558
Mdspan concept and accessor extensions for Uni20.
Definition qnum.hpp:403
Definition async/async.hpp:26
typename detail::TensorStoragePolicy< std::remove_cvref_t< Tensor > >::type tensor_storage_policy_t
Storage-policy type exposed by a tensor-level object, or void.
Definition tensor/concepts.hpp:40
std::remove_cvref_t< detail::normalized_mutable_mdspec_t< T > > mutable_tensor_mdspec_t
Normalized writable MdspecLike type exposed by a mutable tensor-level object.
Definition tensor/concepts.hpp:145
constexpr bool enable_owning_tensor
Opt-in marker for tensor types that own their storage and lifetime.
Definition tensor/concepts.hpp:108
typename tensor_mdspec_t< T >::extents_type tensor_extents_t
Extents type exposed by a tensor-level object.
Definition tensor/concepts.hpp:129
void assign_through(Output &output, Input const &input)
Assign tensor values through a mutable tensor alias descriptor.
Definition copy_into.hpp:121
constexpr decltype(auto) mdspec_of(Tensor &tensor) noexcept(noexcept(tensor.mdspec()))
Return a tensor's immediate or descriptor-backed multidimensional metadata.
Definition tensor/concepts.hpp:50
std::remove_cvref_t< detail::tensor_mutable_mdspan_t< T > > mutable_immediate_tensor_mdspan_t
Resolved writable mdspan type exposed by a mutable tensor-level object.
Definition tensor/concepts.hpp:183
std::remove_cv_t< typename tensor_mdspec_t< T >::element_type > tensor_element_t
Scalar value type exposed by a tensor-level object.
Definition tensor/concepts.hpp:126
std::ptrdiff_t index_type
Definition types.hpp:19
Tensor(InputTensor const &) -> Tensor< tensor_element_t< InputTensor >, tensor_mdspec_t< InputTensor >::rank(), HostStorage, detail::materialized_layout_t< void, tensor_mdspec_t< InputTensor > >, DefaultAccessorFactory >
Deduce a runtime-extents host tensor that materializes a tensor view.
std::remove_cvref_t< detail::tensor_const_mdspan_t< T > > immediate_tensor_mdspan_t
Resolved readable mdspan type exposed by a tensor-level object.
Definition tensor/concepts.hpp:120
std::remove_cvref_t< detail::normalized_const_mdspec_t< T > > tensor_mdspec_t
Normalized readable MdspecLike type exposed by a tensor-level object.
Definition tensor/concepts.hpp:123