Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
linear_solve_mpfr.hpp
Go to the documentation of this file.
1#pragma once
2
9
10namespace uni20::linalg
11{
13template <MutableRankedMdspecLike<2> A, MutableRankedMdspecLike<2> B>
14 requires HostWritableMdspec<A> && HostWritableMdspec<B> && mpfr_detail::RuntimeScalar<typename A::value_type> &&
15 std::same_as<typename A::value_type, typename B::value_type>
18{
19 return kernel_types_yes;
20}
21
23template <MutableRankedMdspecLike<2> A, MutableRankedMdspecLike<2> B>
24 requires HostWritableMdspec<A> && HostWritableMdspec<B> && mpfr_detail::RuntimeScalar<typename A::value_type> &&
25 std::same_as<typename A::value_type, typename B::value_type>
27 SolveOptions<mpreal> const& options, Precision p)
28{
29 (void)p.bit_count(); // Numerical provider kernels require finite working precision.
30 ERROR_IF(a.extent(0) != a.extent(1) || a.extent(0) != b.extent(0), "solve operand shapes do not agree");
31 detail::require_solve_options(options);
32 info = {};
33 if (a.extent(0) == 0 || b.extent(1) == 0) return KernelAttempt::success;
36 auto as = aa.mdspan();
37 auto bs = ba.mdspan();
38 if (!detail::solve_matrix_is_finite(as) || !detail::solve_matrix_is_finite(bs))
39 {
40 info.status = SolveStatus::nonfinite_input;
42 }
43 auto av = mpfr_detail::pack(as, p), bv = mpfr_detail::pack(bs, p);
44 mpreal scale(0, p);
45 if (!detail::prepare_linear_solve(av.mdspan(), bv.mdspan(), scale, info))
46 {
47 // The original inputs were finite; failure arose during boundary conversion.
50 }
51 auto tolerance = options.relative_pivot_tolerance.at(p);
52 std::vector<std::size_t> pivots;
53 auto singular = mplapack::getrf(a.extent(0), mpfr_detail::values(av), pivots, p);
54 mpfr_detail::unpack(av, as);
55 if (!detail::solve_matrix_is_finite(av.mdspan()))
57 else if (singular)
58 info = {.status = SolveStatus::singular, .pivot = singular - 1};
59 else
60 {
61 for (std::size_t i = 0; i < std::size_t(a.extent(0)); ++i)
62 {
63 auto magnitude = uni20::abs(av[i, i]);
64 if (!uni20::isfinite(magnitude))
65 {
67 break;
68 }
69 if (tolerance > 0 && magnitude / scale <= tolerance)
70 {
71 info = {.status = SolveStatus::small_pivot, .pivot = i};
72 break;
73 }
74 }
75 if (info.succeeded())
76 {
77 mplapack::getrs(a.extent(0), b.extent(1), mpfr_detail::values(av), pivots, mpfr_detail::values(bv), p);
78 mpfr_detail::unpack(bv, bs);
79 if (!detail::solve_matrix_is_finite(bv.mdspan())) info.status = SolveStatus::nonfinite_result;
80 }
81 }
83}
84} // namespace uni20::linalg
RAII leases that resolve device descriptors and tensor views to mdspans.
Synchronous packed-array boundary to the serial MPFR/MPC provider.
Exact arithmetic or an explicit binary working precision.
Definition core/precision.hpp:13
Owning exact rational or finite-precision MPFR real, with eager arithmetic.
Definition mpreal.hpp:74
Mutable mdspec metadata supporting host write acquisition.
Definition access.hpp:694
Operation-tag backend dispatch over tensor-view operands.
constexpr bool isfinite(I const &x) noexcept
Returns whether an integer scalar is finite.
Definition math.hpp:121
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.
@ singular
An exactly zero elimination pivot was found.
@ nonfinite_result
A factor, solution, reduction, or required magnitude became nonfinite.
@ small_pivot
A nonzero pivot failed the requested relative threshold.
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
std::size_t getrf(std::size_t n, std::span< float80 > a, std::vector< std::size_t > &pivots)
Factor a square binary80 matrix, returning zero or a one-based singular pivot.
Definition binary80.cpp:203
void getrs(std::size_t n, std::size_t nrhs, std::span< float80 const > a, std::span< std::size_t const > pivots, std::span< float80 > b)
Solve from read-only binary80 LU factors and zero-based pivots.
Definition binary80.cpp:213
Definition data_table.cpp:8
constexpr auto abs(basic_half_int< T > value) -> basic_half_int< T >
Return the absolute value of a half-integer.
Definition half_int.hpp:431
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.
Backend family for configured dense LAPACK kernels, including MPLAPACK scalar adapters.
Definition backend_selector.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
Destructive dense general linear-system solve operation tag.
Definition operation_tags.hpp:155
Multiply one element value by a retained scalar factor.
Definition elementwise_functions.hpp:68
#define ERROR_IF(cond,...)
Definition trace.hpp:389