Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
backends/cpu/lu.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "lu_common.hpp"
7
8namespace uni20::linalg
9{
10namespace detail::cpu_reference
11{
12template <class S, class... P>
13concept LuPrecisionArgs = (has_runtime_precision_v<S> && sizeof...(P) == 1 && (std::same_as<P, Precision> && ...)) ||
14 (!has_runtime_precision_v<S> && sizeof...(P) == 0);
15} // namespace detail::cpu_reference
16
18template <MutableRankedMdspecLike<2> A, class... P>
19 requires HostWritableMdspec<A> && RealOrComplex<typename A::value_type> &&
20 detail::cpu_reference::LuPrecisionArgs<typename A::value_type, P...>
27
29template <MutableRankedMdspecLike<2> A, class... P>
31 detail::cpu_reference::LuPrecisionArgs<typename A::value_type, P...>
32KernelAttempt try_kernel(CpuReferenceBackend, lu_factor_op const&, A& a, std::span<std::size_t> pivots,
34 SolveOptions<make_real_t<typename A::value_type>> const& options, P... precision)
35{
36 detail::require_lu_shape(a, pivots.size());
37 detail::require_solve_options(options);
38 using S = typename A::value_type;
39 using R = make_real_t<S>;
40 auto access = acquire_host_write_access_sync(a);
41 auto s = access.mdspan();
42 if constexpr (has_runtime_precision_v<S>)
43 {
44 Precision p(precision...);
45 (void)p.bit_count();
46 info = {};
47 if (!detail::solve_matrix_is_finite(s))
48 {
49 info.status = SolveStatus::nonfinite_input;
51 }
52 auto work = packed_detail::pack(s, p);
53 auto ws = work.mdspan();
54 if (!detail::solve_matrix_is_finite(ws))
55 {
58 }
59 SolveOptions<R> converted{.relative_pivot_tolerance = scalar_at_precision<R>(options.relative_pivot_tolerance, p)};
60 detail::cpu_reference::lu_factor(ws, pivots, norms, info, converted, scalar_at_precision<R>(0, p));
61 packed_detail::unpack(work, s);
63 }
64 else
65 return detail::cpu_reference::lu_factor(s, pivots, norms, info, options, R{});
66}
67
69template <RankedMdspecLike<2> A, MutableRankedMdspecLike<2> B, class... P>
71 std::same_as<typename A::value_type, typename B::value_type> &&
72 detail::cpu_reference::LuPrecisionArgs<typename A::value_type, P...>
73consteval auto kernel_accepts_types(CpuReferenceBackend, lu_solve_op const&, A&, std::span<std::size_t const>, B&,
74 SolveInfo&, P...)
75{
76 return kernel_types_yes;
77}
78
81template <RankedMdspecLike<2> A, MutableRankedMdspecLike<2> B, class... P>
83 std::same_as<typename A::value_type, typename B::value_type> &&
84 detail::cpu_reference::LuPrecisionArgs<typename A::value_type, P...>
85KernelAttempt try_kernel(CpuReferenceBackend, lu_solve_op const&, A& a, std::span<std::size_t const> pivots, B& b,
86 SolveInfo& info, P... precision)
87{
88 detail::require_lu_solve_shape(a, pivots, b);
89 using S = typename A::value_type;
90 if constexpr (has_runtime_precision_v<S>) (void)Precision(precision...).bit_count();
91 info = {};
92 if (a.extent(0) == 0 || b.extent(1) == 0) return KernelAttempt::success;
95 auto as = aa.mdspan();
96 auto bs = ba.mdspan();
97 if constexpr (has_runtime_precision_v<S>)
98 {
99 Precision p(precision...);
100 if (!detail::solve_matrix_is_finite(bs))
101 {
102 info.status = SolveStatus::nonfinite_input;
104 }
105 // The factor contract already establishes values at p; only RHS values
106 // need conversion. Reading through the accessor preserves view semantics.
107 auto work = packed_detail::pack(bs, p);
108 auto ws = work.mdspan();
109 if (!detail::solve_matrix_is_finite(ws))
110 {
111 info.status = SolveStatus::nonfinite_result;
113 }
114 detail::cpu_reference::lu_solve(as, pivots, ws, info);
115 packed_detail::unpack(work, bs);
117 }
118 else
119 return detail::cpu_reference::lu_solve(as, pivots, bs, info);
120}
121} // namespace uni20::linalg
RAII leases that resolve device descriptors and tensor views to mdspans.
Exact arithmetic or an explicit binary working precision.
Definition core/precision.hpp:13
constexpr bit_count_type bit_count() const
Number of binary significand bits.
Definition core/precision.hpp:43
Mdspec metadata supporting host read acquisition.
Definition access.hpp:688
Mutable mdspec metadata supporting host write acquisition.
Definition access.hpp:694
Concept for types that are either real or complex.
Definition scalar_concepts.hpp:87
typename make_real< T >::type make_real_t
Alias for the underlying real-valued type of T.
Definition scalar_traits.hpp:168
void unpack(Tensor const &source, Span &target)
Definition packed_matrix.hpp:33
auto pack(Span const &span, Precision p, bool read=true)
Definition packed_matrix.hpp:19
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
@ nonfinite_input
An input coefficient or RHS component was nonfinite; both workspaces are preserved.
@ nonfinite_result
A factor, solution, reduction, or required magnitude became nonfinite.
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::yes > kernel_types_yes
Report that a backend accepts every instance of the argument types.
Definition dispatch_error.hpp:40
Definition data_table.cpp:8
auto acquire_host_read_access_sync(Mdspan const &mdspan)
Acquire a directly host-accessible read-only mdspan.
Definition access.hpp:616
auto acquire_host_write_access_sync(Mdspan &mdspan)
Acquire a directly host-accessible writable mdspan.
Definition access.hpp:626
Backend-independent operation values used with normalized kernel operands.
Logical host packing for value-converting BLAS and LAPACK adapters.
Backend value for the reference host CPU dense linalg oracle.
Definition backend_selector.hpp:55
Original matrix norms after conversion to factor precision.
Definition lu_info.hpp:20
Diagnostics written by a completed numerical attempt, independent of dispatch.
Definition solve_info.hpp:41
Real-scalar pivot policy for a square linear solve.
Definition solve_info.hpp:62
Real relative_pivot_tolerance
Reject nonzero pivots at or below this fraction of the original maximum entry magnitude.
Definition solve_info.hpp:64
Fixed-output packed LU factorization with zero-based row-swap pivots.
Definition operation_tags.hpp:165
Solve using read-only packed LU factors, overwriting an existing RHS.
Definition operation_tags.hpp:171