Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
backends/lapack/matrix_norm.hpp
Go to the documentation of this file.
1#pragma once
2
9#include "common.hpp"
10
21
22#include <algorithm>
23#include <cstddef>
24#include <type_traits>
25#include <utility>
26#include <vector>
27
28namespace uni20::linalg
29{
30namespace lapack_detail
31{
32
33template <class Output, class MatrixMdspan> consteval auto matrix_norm_acceptance()
34{
35 using scalar_type = std::remove_cv_t<typename MatrixMdspan::element_type>;
36 using result_type = uni20::make_real_t<scalar_type>;
38 reduction_detail::output_is_supported<Output, result_type, 0>())
39 return kernel_types_maybe;
40 else
41 return kernel_types_no;
42}
43
44inline char lapack_matrix_norm(MatrixNorm kind, bool transposed)
45{
46 switch (kind)
47 {
49 return 'M';
50 case MatrixNorm::One:
51 return transposed ? 'I' : '1';
53 return transposed ? '1' : 'I';
55 return 'F';
56 }
57 PANIC("invalid MatrixNorm", std::to_underlying(kind));
58}
59
60template <class Output, uni20::RankedStridedMdspanLike<2> MatrixMdspan>
61KernelAttempt try_matrix_norm(matrix_norm_op const& operation, Output& output, MatrixMdspan& matrix)
62{
63 using scalar_type = std::remove_cv_t<typename MatrixMdspan::element_type>;
64 using result_type = uni20::make_real_t<scalar_type>;
65
66 if constexpr (uni20::Complex<scalar_type>)
67 {
68 // xLANGE uses |Re(z)| + |Im(z)| for these complex norms. Uni20's
69 // public contract uses the mathematical complex magnitude instead.
71 }
72
74 if (!stage) return KernelAttempt::unsupported_layout;
75
76 auto provider = uni20::linalg::blas::blas_writable_matrix(*stage);
77 if (provider.rows == 0 || provider.cols == 0)
78 {
79 reduction_detail::write_host_output(output, result_type{});
81 }
82
83 char const norm = lapack_matrix_norm(operation.kind, stage->unit_stride_axis == 1);
84 result_type work_placeholder{};
85 std::vector<result_type> work;
86 if (norm == 'I')
87 {
88 work.resize(static_cast<std::size_t>(provider.rows));
89 }
90 result_type* work_data = work.empty() ? &work_placeholder : work.data();
91
92 // LANGE is read-only although its Fortran ABI predates const-correct C
93 // wrappers. Keep the cast at this provider boundary.
94 auto* data = const_cast<scalar_type*>(provider.data);
95 result_type const result =
96 uni20::lapack::lange(norm, provider.rows, provider.cols, data, provider.leading_dimension, work_data);
99}
100
101} // namespace lapack_detail
102
104template <class Output, uni20::RankedStridedMdspecLike<2> MatrixMdspec>
105 requires reduction_detail::HostOutput<Output> && uni20::HostReadableMdspec<MatrixMdspec>
106consteval auto kernel_accepts_types(LapackBackend const&, matrix_norm_op const&, Output&, MatrixMdspec&)
107{
108 using output_type = reduction_detail::host_output_t<Output>;
109 using matrix_span = uni20::host_read_mdspan_t<MatrixMdspec>;
110 constexpr auto acceptance = lapack_detail::matrix_norm_acceptance<output_type, matrix_span>();
111 if constexpr (acceptance == KernelTypeAcceptance::no)
112 return kernel_types_no;
113 else
114 return kernel_types_maybe;
115}
116
118template <class Output, uni20::RankedStridedMdspecLike<2> MatrixMdspec>
119 requires reduction_detail::HostOutput<Output> && uni20::HostReadableMdspec<MatrixMdspec>
120KernelAttempt try_kernel(LapackBackend, matrix_norm_op const& operation, Output& output, MatrixMdspec& matrix)
121{
122 auto matrix_access = acquire_host_read_access_sync(matrix);
123 auto matrix_span = matrix_access.mdspan();
124 return reduction_detail::with_host_output(output, [&](auto& resolved_output) {
125 return lapack_detail::try_matrix_norm(operation, resolved_output, matrix_span);
126 });
127}
128
129} // namespace uni20::linalg
RAII leases that resolve device descriptors and tensor views to mdspans.
Concept for complex scalar types.
Definition scalar_concepts.hpp:75
An mdspan-like type whose accessor is stdex::default_accessor.
Definition mdspan/concepts.hpp:501
Mdspec metadata supporting host read acquisition.
Definition access.hpp:688
Concept for scalar types with a configured dense LAPACK backend.
Definition scalar_concepts.hpp:147
Operation-tag backend dispatch over tensor-view operands.
typename make_real< T >::type make_real_t
Alias for the underlying real-valued type of T.
Definition scalar_traits.hpp:168
Checked LAPACK wrappers over provider-specific unchecked overloads.
Shared helpers for LAPACK operation-tag backend adapters.
Mdspan concept and accessor extensions for Uni20.
Lower mdspan matrix views to provider-ready BLAS operands.
Scalar lange(char norm, blas_int m, blas_int n, Scalar *a, blas_int lda, Scalar *work)
Compute a dense real matrix norm through the configured LAPACK backend.
Definition lapack.hpp:185
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_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
char lapack_matrix_norm(MatrixNorm kind, bool transposed)
Definition backends/lapack/matrix_norm.hpp:44
KernelAttempt try_matrix_norm(matrix_norm_op const &operation, Output &output, MatrixMdspan &matrix)
Definition backends/lapack/matrix_norm.hpp:61
consteval auto matrix_norm_acceptance()
Definition backends/lapack/matrix_norm.hpp:33
typename HostOutputType< Output >::type host_output_t
Resolved output type used by a host reduction backend probe.
Definition reduction_output.hpp:56
void write_host_output(Output &output, Scalar value)
Store a scalar in either a resolved rank-zero mdspan or C++ scalar.
Definition reduction_output.hpp:59
KernelAttempt with_host_output(Output &output, Function &&function)
Invoke a synchronous reduction with a resolved host output.
Definition reduction_output.hpp:68
Definition linalg/async.hpp:28
KernelAttempt try_kernel(BlasBackend, gemm_op const &, C &c, S const &alpha, A &a, B &b, S const &beta)
Pack logical host values and execute GEMM through value-preserving provider conversion.
Definition linalg/backends/blas/binary80.hpp:30
KernelAttempt
Result of one runtime kernel backend attempt.
Definition kernel_attempt.hpp:27
constexpr KernelAcceptance< KernelTypeAcceptance::no > kernel_types_no
Report that a backend rejects the argument types.
Definition dispatch_error.hpp:34
MatrixNorm
Matrix norm selected by matrix_norm_op.
Definition operation_tags.hpp:119
@ One
Induced one-norm: largest column sum of entry magnitudes.
@ MaxAbs
Largest entry magnitude, max_ij abs(A[i,j]).
@ Frobenius
Frobenius norm: square root of the sum of squared entry magnitudes.
@ Infinity
Induced infinity-norm: largest row sum of entry magnitudes.
consteval auto kernel_accepts_types(BlasBackend, gemm_op const &, C &, S const &, A &, B &, S const &)
Accept host-readable native binary80 operands, including transforming accessors.
Definition linalg/backends/blas/binary80.hpp:19
constexpr KernelAcceptance< KernelTypeAcceptance::maybe > kernel_types_maybe
Report that runtime operand values determine backend acceptance.
Definition dispatch_error.hpp:37
std::remove_cvref_t< decltype(std::declval< detail::host_read_mdspan_lease_t< Mdspec > & >().mdspan())> host_read_mdspan_t
Read-only mdspan type resolved by host descriptor acquisition.
Definition access.hpp:679
mpreal norm(mpcomplex const &z)
Definition mpcomplex.hpp:494
auto acquire_host_read_access_sync(Mdspan const &mdspan)
Acquire a directly host-accessible read-only mdspan.
Definition access.hpp:616
Backend-independent operation values used with normalized kernel operands.
Shared host-output handling for synchronous scalar reductions.
Backend family for configured dense LAPACK kernels, including MPLAPACK scalar adapters.
Definition backend_selector.hpp:20
Dense rank-two matrix norm operation tag.
Definition operation_tags.hpp:135
Trait to extract the scalar type from a type T.
Definition scalar_traits.hpp:93
Concepts for tensor-level objects that resolve mdspan kernel operands.
#define PANIC(...)
Definition trace.hpp:382