Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
layout.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <array>
6#include <concepts>
7#include <cstddef>
8#include <type_traits>
9#include <utility>
10
12{
13
14template <typename LayoutPolicy, typename Extents>
15using mapping_for_t = typename LayoutPolicy::template mapping<Extents>;
16
17template <typename Builder, typename LayoutPolicy, typename Extents>
18concept mapping_builder_for = requires(Builder&& builder, Extents const& exts) {
19 { std::forward<Builder>(builder)(exts) } -> std::same_as<mapping_for_t<LayoutPolicy, Extents>>;
20};
21namespace detail
22{
23
24template <typename Extents>
25constexpr auto layout_right_strides(Extents const& exts) -> std::array<typename Extents::index_type, Extents::rank()>
26{
27 using index_type = typename Extents::index_type;
28 std::array<index_type, Extents::rank()> strides{};
29 index_type run = 1;
30 for (int d = static_cast<int>(Extents::rank()) - 1; d >= 0; --d)
31 {
32 strides[d] = run;
33 run *= exts.extent(static_cast<std::size_t>(d));
34 }
35 return strides;
36}
37
38template <typename Extents>
39constexpr auto layout_left_strides(Extents const& exts) -> std::array<typename Extents::index_type, Extents::rank()>
40{
41 using index_type = typename Extents::index_type;
42 std::array<index_type, Extents::rank()> strides{};
43 index_type run = 1;
44 for (std::size_t d = 0; d < Extents::rank(); ++d)
45 {
46 strides[d] = run;
47 run *= exts.extent(d);
48 }
49 return strides;
50}
51
52} // namespace detail
53
55{
56 template <typename Extents>
57 constexpr auto operator()(Extents const& exts) const -> mapping_for_t<stdex::layout_stride, Extents>
58 {
59 return {exts, detail::layout_right_strides(exts)};
60 }
61};
62
64{
65 template <typename Extents>
66 constexpr auto operator()(Extents const& exts) const -> mapping_for_t<stdex::layout_stride, Extents>
67 {
68 return {exts, detail::layout_left_strides(exts)};
69 }
70};
71
72template <typename LayoutPolicy, typename Extents>
73constexpr auto make_mapping(Extents const& exts) -> mapping_for_t<LayoutPolicy, Extents>
74{
75 if constexpr (std::is_same_v<LayoutPolicy, stdex::layout_stride>)
76 {
77 return LayoutRight{}(exts);
78 }
79 else
80 {
82 }
83}
84
85} // namespace uni20::layout
Definition layout.hpp:18
auto strides(S const &s)
Retrieve the strides associated with a strided mdspec-like type.
Definition mdspan/concepts.hpp:558
Configured gateway to Uni20's mdspan implementation.
Definition layout.hpp:12
constexpr auto make_mapping(Extents const &exts) -> mapping_for_t< LayoutPolicy, Extents >
Definition layout.hpp:73
typename LayoutPolicy::template mapping< Extents > mapping_for_t
Definition layout.hpp:15
KernelAttempt run(X &x, A &a, B &b, std::span< make_real_t< typename A::value_type > > residuals, LeastSquaresInfo &info, LeastSquaresOptions< make_real_t< typename A::value_type > > const &options, Precision p, Factor factor)
Definition least_squares_common.hpp:92
std::ptrdiff_t index_type
Definition types.hpp:19
Definition layout.hpp:64
constexpr auto operator()(Extents const &exts) const -> mapping_for_t< stdex::layout_stride, Extents >
Definition layout.hpp:66
Definition layout.hpp:55
constexpr auto operator()(Extents const &exts) const -> mapping_for_t< stdex::layout_stride, Extents >
Definition layout.hpp:57