Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
backends/lapack/least_squares.hpp
Go to the documentation of this file.
1#pragma once
2
6
7namespace uni20::linalg
8{
9namespace least_squares_detail
10{
11template <class S, class... P> consteval auto qr_acceptance()
12{
13 using D = decltype(mdspec_of(std::declval<DenseMatrix<S>&>()));
14 return probe_dispatch_kernel_types<LapackBackend, qr_op, D&, D&, D&, P...>();
15}
16
17template <class Matrix> void provider_qr(Matrix& a, Matrix& b, Precision p)
18{
19 using S = typename Matrix::value_type;
20 std::size_t m = a.extent(0), n = a.extent(1);
21 DenseMatrix<S> q(uninitialized, m, n, p), r(uninitialized, n, n, p);
22 auto ad = mdspec_of(a), qd = mdspec_of(q), rd = mdspec_of(r);
23 detail::dispatch_at_precision<S>(LapackBackend{}, qr_op{}, p, qd, rd, ad);
24 std::size_t nrhs = b.extent(1);
25 DenseMatrix<S> projected_rhs(uninitialized, n, nrhs, p);
26 for (std::size_t j = 0; j < nrhs; ++j)
27 for (std::size_t k = 0; k < n; ++k)
28 {
29 S value = scalar_at_precision<S>(0, p);
30 for (std::size_t i = 0; i < m; ++i)
31 value += uni20::conj(q[i, k]) * b[i, j];
32 projected_rhs[k, j] = value;
33 }
34 // Only the n-by-n triangular system is used after this point.
35 a = std::move(r);
36 b = std::move(projected_rhs);
37}
38} // namespace least_squares_detail
39
41template <class X, class A, class B, class... P>
42 requires least_squares_detail::Operands<X, A, B> &&
43 least_squares_detail::PrecisionArgs<typename A::value_type, P...> &&
44 (least_squares_detail::qr_acceptance<typename A::value_type, P...>() != KernelTypeAcceptance::no)
51
53template <class X, class A, class B, class... P>
54 requires least_squares_detail::Operands<X, A, B> &&
55 least_squares_detail::PrecisionArgs<typename A::value_type, P...> &&
56 (least_squares_detail::qr_acceptance<typename A::value_type, P...>() != KernelTypeAcceptance::no)
58 std::span<make_real_t<typename A::value_type>> residuals, LeastSquaresInfo& info,
59 LeastSquaresOptions<make_real_t<typename A::value_type>> const& options, P... precision)
60{
65 x, a, b, residuals, info, options, least_squares_detail::working_precision<typename A::value_type>(precision...),
66 [](auto& work, auto& rhs, Precision p) { least_squares_detail::provider_qr(work, rhs, p); });
67}
68} // namespace uni20::linalg
LAPACK backend for reduced dense real or complex QR factorization.
Exact arithmetic or an explicit binary working precision.
Definition core/precision.hpp:13
General-purpose owning tensor with immediate or deferred mdspan metadata.
Definition basic_tensor.hpp:109
locale_t value
Definition data_table.cpp:49
uni20::complex< T > conj(detail::standard_complex< T > x)
Returns the complex conjugate for complex-valued scalars.
Definition math.hpp:70
typename make_real< T >::type make_real_t
Alias for the underlying real-valued type of T.
Definition scalar_traits.hpp:168
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
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
void provider_qr(Matrix &a, Matrix &b, Precision p)
Definition backends/lapack/least_squares.hpp:17
consteval auto qr_acceptance()
Definition backends/lapack/least_squares.hpp:11
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
consteval KernelTypeAcceptance probe_dispatch_kernel_types()
Probe whether a backend selector type can dispatch operation argument types.
Definition dispatch.hpp:194
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
Definition data_table.cpp:8
constexpr uninitialized_t uninitialized
Explicit allocation tag for overwrite outputs and scratch space.
Definition common/initialization.hpp:22
constexpr decltype(auto) mdspec_of(Tensor &tensor) noexcept(noexcept(tensor.mdspec()))
Return a tensor's immediate or descriptor-backed multidimensional metadata.
Definition tensor/concepts.hpp:50
Backend family for configured dense LAPACK kernels, including MPLAPACK scalar adapters.
Definition backend_selector.hpp:20
Completed numerical attempt; success does not certify rank or conditioning.
Definition least_squares_info.hpp:26
Full-rank QR rejection policy, independent of any physical convergence criterion.
Definition least_squares_info.hpp:37
Preserving full-column-rank least squares with fixed solution and diagnostic outputs.
Definition operation_tags.hpp:21
Reduced dense real QR factorization operation tag.
Definition operation_tags.hpp:179