Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
mdspan_vector.hpp
Go to the documentation of this file.
1#pragma once
2
13
14#include <optional>
15#include <type_traits>
16
17namespace uni20::linalg::blas
18{
19
21template <class Scalar, class Handle> struct MdspanVectorStage
22{
23 Handle data{};
24 blas_int extent = 0;
25 blas_int increment = 1;
26 bool needs_conjugation = false;
27};
28
29namespace detail
30{
31template <class Mdspan>
32concept lapack_writable_vector_mdspan =
36} // namespace detail
37
39template <uni20::RankedStridedMdspanLike<1> Mdspan>
40auto try_mdspan_vector_stage(Mdspan const& span)
41 -> std::optional<
43{
44 using scalar_type = std::remove_cv_t<typename Mdspan::element_type>;
45 using handle_type = typename Mdspan::data_handle_type;
46
47 auto const& mapping = span.mapping();
48 if (!mapping.is_unique())
49 {
50 return std::nullopt;
51 }
52
53 blas_int const extent = uni20::blas::try_blas_int(span.extent(0));
55 {
56 return std::nullopt;
57 }
58
59 // Empty and singleton mappings do not observe a step between elements, so
60 // choose the canonical BLAS increment regardless of the mapping's stride.
61 blas_int increment = 1;
62 if (extent > 1)
63 {
64 increment = uni20::blas::try_blas_int(mapping.stride(0));
65 if (!uni20::blas::is_valid_blas_int(increment) || increment == 0)
66 {
67 return std::nullopt;
68 }
69 }
70
72 .extent = extent,
73 .increment = increment,
74 .needs_conjugation = mdspan_needs_conjugation_v<Mdspan>};
75}
76
79template <class Scalar, class Handle>
80constexpr auto
82{
83 CHECK(!stage.needs_conjugation, "writable BLAS vectors cannot discard accessor conjugation");
84 return {.data = stage.data, .size = stage.extent, .increment = stage.increment};
85}
86
90template <class Scalar, class Handle>
91constexpr auto
93{
94 CHECK(!stage.needs_conjugation, "readable BLAS vectors cannot discard accessor conjugation");
95 return {.data = stage.data, .size = stage.extent, .increment = stage.increment};
96}
97
99template <class Mdspan>
100 requires detail::blas_writable_mdspan<Mdspan, 1>
102 -> std::optional<
104{
105 auto stage = try_mdspan_vector_stage(span);
106 if (!stage || stage->needs_conjugation)
107 {
108 return std::nullopt;
109 }
110 return blas_writable_vector(*stage);
111}
112
114template <class Mdspan>
115 requires detail::blas_readable_mdspan<Mdspan, 1>
117 -> std::optional<
119{
120 auto stage = try_mdspan_vector_stage(span);
121 if (!stage || stage->needs_conjugation)
122 {
123 return std::nullopt;
124 }
125 return blas_readable_vector(*stage);
126}
127
129template <detail::lapack_writable_vector_mdspan Mdspan>
131 -> std::optional<
133{
134 auto stage = try_mdspan_vector_stage(span);
135 if (!stage || stage->increment != 1 || stage->needs_conjugation)
136 {
137 return std::nullopt;
138 }
139 return blas_writable_vector(*stage);
140}
141
142} // namespace uni20::linalg::blas
Safe conversion helpers for the configured signed BLAS integer ABI.
Provider-ready BLAS vector operands.
An mdspan-like type whose accessor is stdex::default_accessor.
Definition mdspan/concepts.hpp:501
Concept for scalar types with a configured dense LAPACK backend.
Definition scalar_concepts.hpp:147
A mutable strided mdspan-like type with a specified static rank.
Definition mdspan/concepts.hpp:532
Direct BLAS accessor eligibility for ranked mdspan operands.
constexpr blas_int try_blas_int(Value value) noexcept
Convert a non-negative integer to blas_int, or return the negative invalid sentinel.
Definition blas_int.hpp:28
constexpr bool is_valid_blas_int(blas_int value) noexcept
Return whether a non-throwing BLAS integer conversion succeeded.
Definition blas_int.hpp:25
Definition blas.hpp:18
auto try_blas_writable_vector(Mdspan const &span) -> std::optional< BlasWritableVector< std::remove_cv_t< typename Mdspan::element_type >, typename Mdspan::data_handle_type > >
Try to lower a mutable rank-one mdspan directly to a writable provider operand.
Definition mdspan_vector.hpp:101
constexpr auto blas_writable_vector(MdspanVectorStage< Scalar, Handle > const &stage) -> BlasWritableVector< Scalar, Handle >
Return the provider-ready writable operand for a staged mdspan vector.
Definition mdspan_vector.hpp:81
auto try_blas_readable_vector(Mdspan const &span) -> std::optional< BlasReadableVector< std::remove_cv_t< typename Mdspan::element_type >, typename Mdspan::data_handle_type > >
Try to lower a readable rank-one mdspan directly to a provider operand.
Definition mdspan_vector.hpp:116
auto try_mdspan_vector_stage(Mdspan const &span) -> std::optional< MdspanVectorStage< std::remove_cv_t< typename Mdspan::element_type >, typename Mdspan::data_handle_type > >
Build an mdspan vector staging descriptor when direct BLAS lowering is possible.
Definition mdspan_vector.hpp:40
constexpr auto blas_readable_vector(MdspanVectorStage< Scalar, Handle > const &stage) -> BlasReadableVector< Scalar, Handle >
Return the provider-ready readable storage operand for a staged mdspan vector.
Definition mdspan_vector.hpp:92
auto try_lapack_writable_vector(Mdspan const &span) -> std::optional< BlasWritableVector< std::remove_cv_t< typename Mdspan::element_type >, typename Mdspan::data_handle_type > >
Try to lower an mdspan to the contiguous writable vector shape expected by LAPACK.
Definition mdspan_vector.hpp:130
std::size_t span
Definition presentation.cpp:2017
Readable vector shape as seen by a BLAS provider.
Definition blas_vector.hpp:24
Writable vector shape as seen by a BLAS provider.
Definition blas_vector.hpp:16
Logical rank-one mdspan layout before provider-specific BLAS lowering.
Definition mdspan_vector.hpp:22
blas_int extent
Definition mdspan_vector.hpp:24
blas_int increment
Definition mdspan_vector.hpp:25
Handle data
Definition mdspan_vector.hpp:23
bool needs_conjugation
Definition mdspan_vector.hpp:26
Trait to extract the scalar type from a type T.
Definition scalar_traits.hpp:93
#define CHECK(cond,...)
Definition trace.hpp:299