Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
backends/cpu/matrix_set.hpp
Go to the documentation of this file.
1#pragma once
2
16
17#include <concepts>
18#include <type_traits>
19#include <utility>
20
21namespace uni20::linalg
22{
23namespace detail::cpu_reference
24{
25
27template <uni20::MutableRankedMdspanLike<2> MatrixMdspan, uni20::Scalar Scalar> consteval auto matrix_set_acceptance()
28{
29 using matrix_scalar = std::remove_cv_t<typename MatrixMdspan::element_type>;
30 if constexpr (std::same_as<matrix_scalar, Scalar> &&
31 requires(MatrixMdspan& matrix, typename MatrixMdspan::index_type index, Scalar value) {
32 matrix.operator[](index, index) = value;
33 })
34 {
35 return kernel_types_yes;
36 }
37 else
38 {
39 return kernel_types_no;
40 }
41}
42
44template <class MatrixMdspan, class Scalar>
45KernelAttempt set_matrix(matrix_set_op const& op, MatrixMdspan& matrix, Scalar diagonal, Scalar off_diagonal)
46{
47 using matrix_type = std::remove_cvref_t<MatrixMdspan>;
48 using index_type = typename matrix_type::index_type;
49
50 auto selected = [&](index_type row, index_type col) {
51 switch (op.region)
52 {
54 return true;
56 return row <= col;
58 return row >= col;
59 }
60 PANIC("invalid MatrixRegion", std::to_underlying(op.region));
61 };
62
63 for (index_type row = 0; row < matrix.extent(0); ++row)
64 {
65 for (index_type col = 0; col < matrix.extent(1); ++col)
66 {
67 if (selected(row, col))
68 {
69 matrix[row, col] = row == col ? diagonal : off_diagonal;
70 }
71 }
72 }
74}
75} // namespace detail::cpu_reference
76
78template <uni20::MutableRankedMdspecLike<2> MatrixMdspan, uni20::Scalar Scalar>
80consteval auto kernel_accepts_types(CpuReferenceBackend const&, matrix_set_op const&, MatrixMdspan&, Scalar const&,
81 Scalar const&)
82{
84 constexpr auto acceptance = detail::cpu_reference::matrix_set_acceptance<matrix_span, Scalar>();
85 if constexpr (acceptance == KernelTypeAcceptance::yes)
86 return kernel_types_yes;
87 else
88 return kernel_types_no;
89}
90
92template <uni20::MutableRankedMdspecLike<2> MatrixMdspan, uni20::Scalar Scalar>
94KernelAttempt try_kernel(CpuReferenceBackend, matrix_set_op const& op, MatrixMdspan& matrix, Scalar diagonal,
95 Scalar off_diagonal)
96{
97 auto matrix_access = acquire_host_write_access_sync(matrix);
98 auto matrix_span = matrix_access.mdspan();
99 return detail::cpu_reference::set_matrix(op, matrix_span, diagonal, off_diagonal);
100}
101
102} // namespace uni20::linalg
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
locale_t value
Definition data_table.cpp:49
Operation-tag backend dispatch over tensor-view operands.
Mdspan concept and accessor extensions for Uni20.
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
void set_matrix(BackendSelector selector, async::Async< MatrixTensor > &matrix, Diagonal &&diagonal, OffDiagonal &&off_diagonal, MatrixRegion region=MatrixRegion::All)
Schedule structured matrix initialization through an explicit selector.
Definition async/matrix_set.hpp:58
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
@ Upper
Entries with row <= column, including the diagonal.
@ All
All entries, including rectangular portions of the matrix.
@ Lower
Entries with row >= column, including the diagonal.
constexpr KernelAcceptance< KernelTypeAcceptance::yes > kernel_types_yes
Report that a backend accepts every instance of the argument types.
Definition dispatch_error.hpp:40
std::remove_cvref_t< decltype(std::declval< detail::host_write_mdspan_lease_t< Mdspec > & >().mdspan())> host_write_mdspan_t
Writable mdspan type resolved by host descriptor acquisition.
Definition access.hpp:684
std::ptrdiff_t index_type
Definition types.hpp:19
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 value for the reference host CPU dense linalg oracle.
Definition backend_selector.hpp:55
Structured dense matrix initialization operation tag.
Definition operation_tags.hpp:219
Concepts for tensor-level objects that resolve mdspan kernel operands.
#define PANIC(...)
Definition trace.hpp:382