Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
mdspec_tensor_view.hpp
Go to the documentation of this file.
1#pragma once
2
11
12#include <concepts>
13#include <cstddef>
14#include <type_traits>
15#include <utility>
16
17namespace uni20
18{
19
28template <MdspecLike MutableMdspec, MdspecLike ConstMdspec, class StoragePolicy>
29 requires(std::same_as<typename MutableMdspec::extents_type, typename ConstMdspec::extents_type> &&
30 std::same_as<std::remove_const_t<typename MutableMdspec::element_type>,
31 std::remove_const_t<typename ConstMdspec::element_type>> &&
32 std::is_const_v<typename ConstMdspec::element_type> && requires { StoragePolicy::backend_selector(); })
33class MdspecTensorView : public detail::precision_default<typename MutableMdspec::value_type> {
34 public:
35 using mutable_mdspec_type = MutableMdspec;
36 using const_mdspec_type = ConstMdspec;
37 using storage_policy = StoragePolicy;
38 using backend_selector_type = std::remove_cvref_t<decltype(storage_policy::backend_selector())>;
39 using element_type = typename mutable_mdspec_type::element_type;
40 using value_type = std::remove_cv_t<element_type>;
41 using extents_type = typename mutable_mdspec_type::extents_type;
42 using index_type = typename extents_type::index_type;
43
45 constexpr MdspecTensorView(mutable_mdspec_type mutable_mdspec, const_mdspec_type const_mdspec)
46 : mutable_mdspec_(std::move(mutable_mdspec)), const_mdspec_(std::move(const_mdspec))
47 {}
48
50 [[nodiscard]] static constexpr auto backend_selector() noexcept -> backend_selector_type
51 {
52 return storage_policy::backend_selector();
53 }
54
56 [[nodiscard]] constexpr auto mdspec() & -> mutable_mdspec_type { return mutable_mdspec_; }
57
59 [[nodiscard]] constexpr auto mdspec() const& -> const_mdspec_type { return const_mdspec_; }
60
62 [[nodiscard]] constexpr auto mdspan() & -> mutable_mdspec_type
64 {
65 return mutable_mdspec_;
66 }
67
69 [[nodiscard]] constexpr auto mdspan() const& -> const_mdspec_type
71 {
72 return const_mdspec_;
73 }
74
76 [[nodiscard]] static constexpr std::size_t rank() noexcept { return extents_type::rank(); }
77
79 [[nodiscard]] constexpr auto extents() const noexcept -> extents_type const& { return const_mdspec_.extents(); }
80
82 [[nodiscard]] constexpr auto extent(std::size_t axis) const noexcept -> index_type
83 {
84 return const_mdspec_.extent(axis);
85 }
86
88 [[nodiscard]] constexpr auto stride(std::size_t axis) const -> index_type
90 {
91 return const_mdspec_.stride(axis);
92 }
93
95 template <class... Index>
96 requires MdspanLike<mutable_mdspec_type> && (sizeof...(Index) == extents_type::rank())
97 constexpr decltype(auto) operator[](Index... indices)
98 {
99 return mutable_mdspec_[indices...];
100 }
101
103 template <class... Index>
104 requires MdspanLike<const_mdspec_type> && (sizeof...(Index) == extents_type::rank())
105 constexpr decltype(auto) operator[](Index... indices) const
106 {
107 return const_mdspec_[indices...];
108 }
109
110 private:
111 [[no_unique_address]] mutable_mdspec_type mutable_mdspec_;
112 [[no_unique_address]] const_mdspec_type const_mdspec_;
113};
114
115} // namespace uni20
TensorView materialization over writable and read-only mdspec values.
Definition mdspec_tensor_view.hpp:33
typename mutable_mdspec_type::extents_type extents_type
Definition mdspec_tensor_view.hpp:41
static constexpr std::size_t rank() noexcept
Return the static dense-block rank.
Definition mdspec_tensor_view.hpp:76
StoragePolicy storage_policy
Definition mdspec_tensor_view.hpp:37
typename mutable_mdspec_type::element_type element_type
Definition mdspec_tensor_view.hpp:39
static constexpr auto backend_selector() noexcept -> backend_selector_type
Return the dense storage policy's default backend selector.
Definition mdspec_tensor_view.hpp:50
constexpr decltype(auto) operator[](Index... indices) const
Evaluate one element when read-only metadata is immediate.
Definition mdspec_tensor_view.hpp:105
constexpr auto extent(std::size_t axis) const noexcept -> index_type
Return one dense-block extent.
Definition mdspec_tensor_view.hpp:82
constexpr auto mdspan() &-> mutable_mdspec_type
Return the writable mdspan when the metadata is immediate.
Definition mdspec_tensor_view.hpp:62
constexpr decltype(auto) operator[](Index... indices)
Evaluate or assign one element when writable metadata is immediate.
Definition mdspec_tensor_view.hpp:97
constexpr MdspecTensorView(mutable_mdspec_type mutable_mdspec, const_mdspec_type const_mdspec)
Construct a tensor view from writable and read-only metadata.
Definition mdspec_tensor_view.hpp:45
constexpr auto mdspec() const &-> const_mdspec_type
Return read-only immediate or descriptor-backed metadata.
Definition mdspec_tensor_view.hpp:59
constexpr auto mdspec() &-> mutable_mdspec_type
Return writable immediate or descriptor-backed metadata.
Definition mdspec_tensor_view.hpp:56
std::remove_cv_t< element_type > value_type
Definition mdspec_tensor_view.hpp:40
constexpr auto extents() const noexcept -> extents_type const &
Return the dense-block extents.
Definition mdspec_tensor_view.hpp:79
constexpr auto stride(std::size_t axis) const -> index_type
Return one dense-block stride when the mapping is strided.
Definition mdspec_tensor_view.hpp:88
MutableMdspec mutable_mdspec_type
Definition mdspec_tensor_view.hpp:35
constexpr auto mdspan() const &-> const_mdspec_type
Return the read-only mdspan when the metadata is immediate.
Definition mdspec_tensor_view.hpp:69
typename extents_type::index_type index_type
Definition mdspec_tensor_view.hpp:42
ConstMdspec const_mdspec_type
Definition mdspec_tensor_view.hpp:36
std::remove_cvref_t< decltype(storage_policy::backend_selector())> backend_selector_type
Definition mdspec_tensor_view.hpp:38
A readable type implementing the mdspan observers and multidimensional subscript contract.
Definition mdspan/concepts.hpp:291
An mdspec-like type whose mapping exposes multidimensional strides.
Definition mdspan/concepts.hpp:427
Definition qnum.hpp:403
Definition async/async.hpp:26
Concepts for tensor-level objects that resolve mdspan kernel operands.