47template <
class Span,
class =
void>
struct SpanData
49 using type =
typename std::remove_cvref_t<Span>::data_handle_type;
51 [[nodiscard]]
static constexpr auto get(Span
const&
span) -> type {
return span.data_handle(); }
54template <
class Span>
struct SpanData<Span,
std::void_t<typename std::remove_cvref_t<Span>::data_descriptor_type>>
56 using type =
typename std::remove_cvref_t<Span>::data_descriptor_type;
58 [[nodiscard]]
static constexpr auto get(Span
const&
span) -> type {
return span.data_descriptor(); }
61template <
class Span>
using span_data_t =
typename SpanData<Span>::type;
63template <
class Span> [[nodiscard]]
constexpr auto span_data(Span
const&
span) -> span_data_t<Span>
65 return SpanData<Span>::get(
span);
68inline blas_int minimum_leading_dimension(blas_int rows) {
return rows > 1 ?
rows : 1; }
70inline std::optional<blas_int> normalized_nonunit_stride(blas_int nonunit_stride, blas_int provider_rows,
71 blas_int provider_cols)
73 blas_int
const minimum = minimum_leading_dimension(provider_rows);
74 if (nonunit_stride >= minimum)
76 return nonunit_stride;
82 if (provider_rows == 0 || provider_cols <= 1)
90template <
class Mdspan>
91concept lapack_writable_mdspan =
98template <uni20::RankedStr
idedMdspecLike<2> Mdspan>
101 auto const& mapping =
span.mapping();
102 if (!mapping.is_unique())
116 blas_int stride0 = 1;
122 blas_int stride1 = 1;
129 int unit_stride_axis = -1;
130 blas_int nonunit_stride = 0;
131 blas_int provider_rows = 0;
132 blas_int provider_cols = 0;
136 if (extent0 > 1 && stride0 == 1)
138 unit_stride_axis = 0;
139 nonunit_stride = stride1;
140 provider_rows = extent0;
141 provider_cols = extent1;
143 else if (extent1 > 1 && stride1 == 1)
145 unit_stride_axis = 1;
146 nonunit_stride = stride0;
147 provider_rows = extent1;
148 provider_cols = extent0;
150 else if (extent0 <= 1)
152 unit_stride_axis = 0;
153 nonunit_stride = stride1;
154 provider_rows = extent0;
155 provider_cols = extent1;
157 else if (extent1 <= 1)
159 unit_stride_axis = 1;
160 nonunit_stride = stride0;
161 provider_rows = extent1;
162 provider_cols = extent0;
169 auto const normalized_stride = detail::normalized_nonunit_stride(nonunit_stride, provider_rows, provider_cols);
170 if (!normalized_stride)
177 .nonunit_stride = *normalized_stride,
178 .unit_stride_axis = unit_stride_axis,
179 .needs_conjugation = mdspan_needs_conjugation_v<Mdspan>};
185template <uni20::RankedStr
idedMdspecLike<2> Mdspan>
186auto make_mdspan_matrix_stage(Mdspan
const&
span, MdspanMatrixMetadata
const& metadata)
187 -> MdspanMatrixStage<std::remove_cv_t<typename Mdspan::element_type>, detail::span_data_t<Mdspan>>
189 using scalar_type = std::remove_cv_t<typename Mdspan::element_type>;
190 using handle_type = detail::span_data_t<Mdspan>;
191 return MdspanMatrixStage<scalar_type, handle_type>{.data = detail::span_data(
span),
192 .extent0 = metadata.extent0,
193 .extent1 = metadata.extent1,
194 .nonunit_stride = metadata.nonunit_stride,
195 .unit_stride_axis = metadata.unit_stride_axis,
196 .needs_conjugation = metadata.needs_conjugation};
201template <uni20::RankedStr
idedMdspecLike<2> Mdspan>
203 -> std::optional<MdspanMatrixStage<std::remove_cv_t<typename Mdspan::element_type>, detail::span_data_t<Mdspan>>>
206 if (!metadata)
return std::nullopt;
207 return detail::make_mdspan_matrix_stage(
span, *metadata);
217template <
class Scalar,
class Handle>
224template <
class Scalar,
class Handle>
228 if (stage.unit_stride_axis == 0)
231 .
data = stage.data, .rows = stage.extent0, .cols = stage.extent1, .leading_dimension = stage.nonunit_stride};
233 return {.data = stage.data, .rows = stage.extent1, .cols = stage.extent0, .leading_dimension = stage.nonunit_stride};
237template <
class Scalar,
class Handle>
242 if (stage.needs_conjugation)
248 return {.data = writable.data,
249 .rows = writable.rows,
250 .cols = writable.cols,
251 .leading_dimension = writable.leading_dimension,
252 .transform = transform};
256template <
class Mdspan>
257 requires detail::blas_writable_mdspan<Mdspan, 2>
270template <
class Mdspan>
271 requires detail::blas_readable_mdspan<Mdspan, 2>
284template <detail::lapack_writable_mdspan Mdspan>
289 if (!stage || stage->needs_conjugation)
296 if (stage->unit_stride_axis != 0)
298 if (stage->extent0 > 1)
return std::nullopt;
299 stage->unit_stride_axis = 0;
300 stage->nonunit_stride = detail::minimum_leading_dimension(stage->extent0);
Safe conversion helpers for the configured signed BLAS integer ABI.
Provider-ready BLAS matrix operands and transform helpers.
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.
int rows(std::FILE *stream)
Return the terminal row count associated with a C stream.
Definition terminal.cpp:482
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
constexpr MatrixTransform storage_transform(MdspanMatrixMetadata const &metadata)
Transform from the provider matrix shape to the logical mdspan shape.
Definition mdspan_matrix.hpp:211
MatrixTransform
Logical matrix transform independent of a provider-specific opcode, although it happens to coincide w...
Definition blas_matrix.hpp:23
constexpr auto blas_writable_matrix(MdspanMatrixStage< Scalar, Handle > const &stage) -> BlasWritableMatrix< Scalar, Handle >
Return the provider-ready writable operand for a staged mdspan matrix.
Definition mdspan_matrix.hpp:225
auto try_mdspan_matrix_metadata(Mdspan const &span) -> std::optional< MdspanMatrixMetadata >
Inspect matrix layout and accessor semantics without resolving a data handle.
Definition mdspan_matrix.hpp:99
auto try_blas_writable_matrix(Mdspan const &span) -> std::optional< BlasWritableMatrix< std::remove_cv_t< typename Mdspan::element_type >, typename Mdspan::data_handle_type > >
Try to lower a mutable mdspan directly to a writable provider operand.
Definition mdspan_matrix.hpp:258
auto try_mdspan_matrix_stage(Mdspan const &span) -> std::optional< MdspanMatrixStage< std::remove_cv_t< typename Mdspan::element_type >, detail::span_data_t< Mdspan > > >
Build a matrix staging descriptor when direct provider lowering is possible.
Definition mdspan_matrix.hpp:202
constexpr auto blas_readable_matrix(MdspanMatrixStage< Scalar, Handle > const &stage) -> BlasReadableMatrix< Scalar, Handle >
Return the provider-ready readable operand for a staged mdspan matrix.
Definition mdspan_matrix.hpp:238
auto try_blas_readable_matrix(Mdspan const &span) -> std::optional< BlasReadableMatrix< std::remove_cv_t< typename Mdspan::element_type >, typename Mdspan::data_handle_type > >
Try to lower an mdspan directly to a readable provider operand.
Definition mdspan_matrix.hpp:272
auto try_lapack_writable_matrix(Mdspan const &span) -> std::optional< BlasWritableMatrix< std::remove_cv_t< typename Mdspan::element_type >, typename Mdspan::data_handle_type > >
Try to lower an mdspan to the strict column-major shape expected by LAPACK.
Definition mdspan_matrix.hpp:285
constexpr MatrixTransform compose(MatrixTransform outer, MatrixTransform inner)
Compose two matrix transforms as outer(inner(A)).
Definition blas_matrix.hpp:58
std::size_t span
Definition presentation.cpp:2017
Readable matrix shape plus transform as seen by a BLAS provider.
Definition blas_matrix.hpp:130
Writable matrix shape as seen by a BLAS/LAPACK provider.
Definition blas_matrix.hpp:119
Handle data
Definition blas_matrix.hpp:120
Logical mdspan matrix layout before provider-specific BLAS lowering.
Definition mdspan_matrix.hpp:35
bool needs_conjugation
Definition mdspan_matrix.hpp:42
blas_int nonunit_stride
Mdspan stride orthogonal to the unit-stride axis.
Definition mdspan_matrix.hpp:40
blas_int extent1
Definition mdspan_matrix.hpp:38
int unit_stride_axis
Definition mdspan_matrix.hpp:41
blas_int extent0
Definition mdspan_matrix.hpp:37
Handle data
Definition mdspan_matrix.hpp:36
Trait to extract the scalar type from a type T.
Definition scalar_traits.hpp:93