Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
generated_layout.hpp
Go to the documentation of this file.
1#pragma once
2
10
11#include <array>
12#include <cstddef>
13#include <type_traits>
14
15namespace uni20
16{
17
23{
24 template <class Extents> class mapping {
25 public:
27 using extents_type = Extents;
28 using index_type = typename extents_type::index_type;
29 using size_type = typename extents_type::size_type;
30 using rank_type = typename extents_type::rank_type;
31
32 UNI20_HOST_DEVICE constexpr mapping() = default;
33
35 UNI20_HOST_DEVICE explicit constexpr mapping(extents_type const& extents) noexcept : extents_(extents) {}
36
38 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto extents() const noexcept -> extents_type const&
39 {
40 return extents_;
41 }
42
44 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto required_span_size() const noexcept -> index_type
45 {
46 index_type result = 1;
47 for (rank_type axis = 0; axis < extents_type::rank(); ++axis)
48 {
49 if (extents_.extent(axis) == 0) return 0;
50 result *= extents_.extent(axis);
51 }
52 return result;
53 }
54
56 template <class... Indices>
57 requires(sizeof...(Indices) == extents_type::rank() && (std::is_convertible_v<Indices, index_type> && ...))
58 [[nodiscard]] UNI20_HOST_DEVICE constexpr auto operator()(Indices... indices) const noexcept -> index_type
59 {
60 std::array<index_type, extents_type::rank()> logical{static_cast<index_type>(indices)...};
61 index_type offset = 0;
62 for (rank_type axis = 0; axis < extents_type::rank(); ++axis)
63 offset = offset * extents_.extent(axis) + logical[axis];
64 return offset;
65 }
66
67 [[nodiscard]] UNI20_HOST_DEVICE static constexpr bool is_always_unique() noexcept { return true; }
68 [[nodiscard]] UNI20_HOST_DEVICE static constexpr bool is_always_exhaustive() noexcept { return true; }
69 [[nodiscard]] UNI20_HOST_DEVICE static constexpr bool is_always_strided() noexcept { return false; }
70 [[nodiscard]] UNI20_HOST_DEVICE constexpr bool is_unique() const noexcept { return true; }
71 [[nodiscard]] UNI20_HOST_DEVICE constexpr bool is_exhaustive() const noexcept { return true; }
72 [[nodiscard]] UNI20_HOST_DEVICE constexpr bool is_strided() const noexcept { return false; }
73
74 friend UNI20_HOST_DEVICE constexpr bool operator==(mapping const&, mapping const&) = default;
75
76 private:
77 [[no_unique_address]] extents_type extents_{};
78 };
79};
80
81} // namespace uni20
Definition generated_layout.hpp:24
typename extents_type::size_type size_type
Definition generated_layout.hpp:29
typename extents_type::rank_type rank_type
Definition generated_layout.hpp:30
UNI20_HOST_DEVICE constexpr auto required_span_size() const noexcept -> index_type
Return the number of synthetic offsets in the logical index space.
Definition generated_layout.hpp:44
UNI20_HOST_DEVICE constexpr mapping(extents_type const &extents) noexcept
Construct a synthetic mapping over the supplied logical extents.
Definition generated_layout.hpp:35
UNI20_HOST_DEVICE constexpr bool is_strided() const noexcept
Definition generated_layout.hpp:72
UNI20_HOST_DEVICE constexpr bool is_unique() const noexcept
Definition generated_layout.hpp:70
static UNI20_HOST_DEVICE constexpr bool is_always_strided() noexcept
Definition generated_layout.hpp:69
UNI20_HOST_DEVICE constexpr mapping()=default
UNI20_HOST_DEVICE constexpr auto extents() const noexcept -> extents_type const &
Return the generated value's logical extents.
Definition generated_layout.hpp:38
Extents extents_type
Definition generated_layout.hpp:27
typename extents_type::index_type index_type
Definition generated_layout.hpp:28
UNI20_HOST_DEVICE constexpr bool is_exhaustive() const noexcept
Definition generated_layout.hpp:71
friend UNI20_HOST_DEVICE constexpr bool operator==(mapping const &, mapping const &)=default
static UNI20_HOST_DEVICE constexpr bool is_always_unique() noexcept
Definition generated_layout.hpp:67
static UNI20_HOST_DEVICE constexpr bool is_always_exhaustive() noexcept
Definition generated_layout.hpp:68
Project compiler annotations shared by host and device code.
#define UNI20_HOST_DEVICE
Definition compiler_attributes.hpp:12
Definition async/async.hpp:26
Synthetic non-strided layout used by generated tensor accessors.
Definition generated_layout.hpp:23