Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
linalg/backends/lapack/common.hpp
Go to the documentation of this file.
1#pragma once
2
11#include <uni20/core/math.hpp>
14
15#include <concepts>
16#include <cstddef>
17#include <functional>
18#include <optional>
19#include <tuple>
20#include <type_traits>
21#include <utility>
22
24{
25
27template <class Function, uni20::HostWritableMdspec... Mdspecs>
28 requires std::same_as<std::invoke_result_t<Function&&, uni20::host_write_mdspan_t<Mdspecs>&...>, KernelAttempt>
29KernelAttempt with_host_write_mdspans(Function&& function, Mdspecs&... mdspecs)
30{
31 auto accesses = std::tuple{acquire_host_write_access_sync(mdspecs)...};
32 return std::apply(
33 [&](auto&... access) -> KernelAttempt {
34 auto mdspans = std::tuple{access.mdspan()...};
35 return std::apply(
36 [&](auto&... mdspan) -> KernelAttempt { return std::invoke(std::forward<Function>(function), mdspan...); },
37 mdspans);
38 },
39 accesses);
40}
41
43template <class Scalar> blas_int workspace_size(Scalar const& query)
44{
45 auto const recommended = uni20::real(query);
46 using real_type = std::remove_cv_t<decltype(recommended)>;
47 CHECK(uni20::isfinite(recommended), recommended);
48 CHECK(recommended >= real_type{1}, recommended);
49
50 auto const maximum = static_cast<real_type>(uni20::numeric_limits<blas_int>::max());
52 {
53 // A low-precision real may round max(blas_int) up to the first
54 // unrepresentable integer, so equality is not a safe conversion bound.
55 CHECK(recommended < maximum, recommended);
56 }
57 else
58 {
59 CHECK(recommended <= maximum, recommended);
60 }
61
62 blas_int const result = static_cast<blas_int>(recommended);
63 CHECK(result >= 1, result);
64 CHECK(std::in_range<std::size_t>(result), result);
65 return result;
66}
67
69constexpr std::optional<std::size_t> try_size_product(std::size_t left, std::size_t right) noexcept
70{
71 if (left != 0 && right > uni20::numeric_limits<std::size_t>::max() / left)
72 {
73 return std::nullopt;
74 }
75 return left * right;
76}
77
78} // namespace uni20::linalg::lapack_detail
RAII leases that resolve device descriptors and tensor views to mdspans.
Mutable mdspec metadata supporting host write acquisition.
Definition access.hpp:694
Concept for numeric scalar types (integer, real, or complex).
Definition scalar_concepts.hpp:81
constexpr T & real(detail::standard_complex< T > &z) noexcept
Provides mutable access to the real component of a uni20::complex value.
Definition math.hpp:173
constexpr bool isfinite(I const &x) noexcept
Returns whether an integer scalar is finite.
Definition math.hpp:121
Allocation-free runtime results for kernel backend attempts.
Scalar math helper utilities.
Definition linalg/backends/lapack/common.hpp:24
blas_int workspace_size(Scalar const &query)
Convert a provider workspace query result to the configured LAPACK integer type.
Definition linalg/backends/lapack/common.hpp:43
KernelAttempt with_host_write_mdspans(Function &&function, Mdspecs &... mdspecs)
Invoke a LAPACK attempt with host-writable mdspans under simultaneous leases.
Definition linalg/backends/lapack/common.hpp:29
constexpr std::optional< std::size_t > try_size_product(std::size_t left, std::size_t right) noexcept
Multiply allocation extents without wrapping std::size_t.
Definition linalg/backends/lapack/common.hpp:69
KernelAttempt
Result of one runtime kernel backend attempt.
Definition kernel_attempt.hpp:27
auto acquire_host_write_access_sync(Mdspan &mdspan)
Acquire a directly host-accessible writable mdspan.
Definition access.hpp:626
Project-level numeric limits customization point.
Definition numeric_limits.hpp:16
#define CHECK(cond,...)
Definition trace.hpp:299