Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
ops/matrix_norm.hpp
Go to the documentation of this file.
1#pragma once
2
18
19#include <concepts>
20#include <type_traits>
21#include <utility>
22
23namespace uni20::linalg
24{
25
31template <KernelBackendSelector BackendSelector, uni20::MutableRankedMdspanLike<0> OutputMdspan,
32 uni20::RankedMdspanLike<2> MatrixMdspan>
34 std::same_as<typename std::remove_cvref_t<OutputMdspan>::value_type,
36void matrix_norm(BackendSelector&& selector, OutputMdspan&& output, MatrixMdspan&& matrix, MatrixNorm kind)
37{
38 auto output_descriptor = std::forward<OutputMdspan>(output);
39 auto matrix_descriptor = uni20::make_const_mdspan(matrix);
40 dispatch_kernel(std::forward<BackendSelector>(selector), matrix_norm_op{.kind = kind}, output_descriptor,
41 matrix_descriptor);
42}
43
45template <KernelBackendSelector BackendSelector, uni20::RankedMdspanLike<2> MatrixMdspan>
47[[nodiscard]] auto matrix_norm_host(BackendSelector&& selector, MatrixMdspan&& matrix, MatrixNorm kind)
49{
51 result_type result{};
52 auto matrix_descriptor = uni20::make_const_mdspan(matrix);
53 dispatch_kernel(std::forward<BackendSelector>(selector), matrix_norm_op{.kind = kind}, result, matrix_descriptor);
54 return result;
55}
56
61template <KernelBackendSelector BackendSelector, uni20::MutableScalarTensorView OutputTensor,
62 uni20::RankedTensorView<2> MatrixTensor>
64 std::same_as<uni20::tensor_element_t<OutputTensor>,
66void matrix_norm(BackendSelector&& selector, OutputTensor&& output, MatrixTensor const& matrix, MatrixNorm kind)
67{
68 auto output_descriptor = uni20::mdspec_of(output);
69 auto matrix_descriptor = uni20::mdspec_of(matrix);
70 dispatch_kernel(std::forward<BackendSelector>(selector), matrix_norm_op{.kind = kind}, output_descriptor,
71 matrix_descriptor);
72}
73
75template <uni20::MutableScalarTensorView OutputTensor, uni20::RankedTensorView<2> MatrixTensor>
77 std::same_as<uni20::tensor_element_t<OutputTensor>,
79void matrix_norm(OutputTensor&& output, MatrixTensor const& matrix, MatrixNorm kind)
80{
81 auto operation = matrix_norm_op{.kind = kind};
82 auto selector = select_backend(operation, output, matrix);
83 matrix_norm(selector, std::forward<OutputTensor>(output), matrix, kind);
84}
85
90template <KernelBackendSelector BackendSelector, uni20::RankedTensorView<2> MatrixTensor>
92 requires(MatrixTensor const& matrix) { uni20::norm(matrix); }
93[[nodiscard]] auto matrix_norm(BackendSelector&& selector, MatrixTensor const& matrix, MatrixNorm kind)
94{
95 // Reuse Tensor norm's established storage-preserving result type without evaluating it.
96 using result_type = decltype(uni20::norm(matrix));
97 result_type result(uni20::uninitialized);
98 matrix_norm(std::forward<BackendSelector>(selector), result, matrix, kind);
99 return result;
100}
101
103template <uni20::RankedTensorView<2> MatrixTensor>
105 requires(MatrixTensor const& matrix) { uni20::norm(matrix); }
106[[nodiscard]] auto matrix_norm(MatrixTensor const& matrix, MatrixNorm kind)
107{
108 auto selector = select_backend(matrix_norm_op{.kind = kind}, matrix);
109 return matrix_norm(selector, matrix, kind);
110}
111
117template <KernelBackendSelector BackendSelector, uni20::RankedTensorView<2> MatrixTensor>
119[[nodiscard]] auto matrix_norm_host(BackendSelector&& selector, MatrixTensor const& matrix,
121{
123 result_type result{};
124 auto matrix_descriptor = uni20::mdspec_of(matrix);
125 dispatch_kernel(std::forward<BackendSelector>(selector), matrix_norm_op{.kind = kind}, result, matrix_descriptor);
126 return result;
127}
128
130template <uni20::RankedTensorView<2> MatrixTensor>
132[[nodiscard]] auto matrix_norm_host(MatrixTensor const& matrix,
134{
135 auto selector = select_backend(matrix_norm_op{.kind = kind}, matrix);
136 return matrix_norm_host(selector, matrix, kind);
137}
138
139} // namespace uni20::linalg
Accessor-respecting reference CPU matrix norms.
LAPACK backend for dense matrix norms.
MutableTensorView whose writable mdspec has rank zero.
Definition tensor/concepts.hpp:175
TensorView whose readable mdspec has a specified static rank.
Definition tensor/concepts.hpp:149
Concept for types that are either real or complex.
Definition scalar_concepts.hpp:87
Operation-tag backend dispatch over tensor-view operands.
typename make_real< T >::type make_real_t
Alias for the underlying real-valued type of T.
Definition scalar_traits.hpp:168
Mdspan concept and accessor extensions for Uni20.
Definition linalg/async.hpp:28
auto matrix_norm_host(BackendSelector selector, async::Async< MatrixTensor > const &matrix, MatrixNorm kind)
Schedule a dense matrix norm returning a host C++ scalar through an explicit selector.
Definition async/matrix_norm.hpp:167
constexpr auto select_backend(Operation const &operation, FirstTensor const &, RestTensors const &...)
Select the default backend list for an operation on Tensor operands.
Definition backend_selector.hpp:252
void matrix_norm(BackendSelector selector, async::Async< OutputTensor > &output, async::Async< MatrixTensor > const &matrix, MatrixNorm kind)
Schedule a matrix norm into an explicit async scalar Tensor.
Definition async/matrix_norm.hpp:126
void dispatch_kernel(BackendSelector &&selector, Op op, Args &&... args)
Normalize a selector and dispatch or report that every eligible backend declined.
Definition dispatch.hpp:374
MatrixNorm
Matrix norm selected by matrix_norm_op.
Definition operation_tags.hpp:119
constexpr uninitialized_t uninitialized
Explicit allocation tag for overwrite outputs and scratch space.
Definition common/initialization.hpp:22
mpreal norm(mpcomplex const &z)
Definition mpcomplex.hpp:494
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
constexpr auto make_const_mdspan(Span const &span)
Build a read-only mdspan descriptor over the same storage and mapping.
Definition mdspan/concepts.hpp:372
Backend-independent operation values used with normalized kernel operands.
Dense rank-two matrix norm operation tag.
Definition operation_tags.hpp:135
MatrixNorm kind
Definition operation_tags.hpp:137
Concepts for tensor-level objects that resolve mdspan kernel operands.
Tensor sums, inner products, and Euclidean norms with explicit result residency.