Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
linalg/backends/cpu/conjugate_inplace.hpp
Go to the documentation of this file.
1#pragma once
2
9#include <uni20/core/math.hpp>
16
17#include <array>
18#include <cstddef>
19#include <type_traits>
20#include <utility>
21
22namespace uni20::linalg
23{
24namespace detail::cpu_reference
25{
26template <class Mdspan, std::size_t... Axis>
27consteval bool conjugate_element_is_assignable(std::index_sequence<Axis...>)
28{
29 using span_type = std::remove_cvref_t<Mdspan>;
30 using index_type = typename span_type::index_type;
31 return requires(span_type& span, index_type index) {
32 span.operator[](((void)Axis, index)...) =
33 uni20::conj(static_cast<typename span_type::value_type>(span.operator[](((void)Axis, index)...)));
34 };
35}
36
37template <class Mdspan, std::size_t... Axis>
38void conjugate_element(Mdspan& span, std::array<typename Mdspan::index_type, Mdspan::rank()> const& index,
39 std::index_sequence<Axis...>)
40{
41 using value_type = typename Mdspan::value_type;
42 auto&& element = span.operator[](index[Axis]...);
43 element = uni20::conj(static_cast<value_type>(element));
44}
45
46template <std::size_t Axis, class Mdspan>
47void conjugate_elements(Mdspan& span, std::array<typename Mdspan::index_type, Mdspan::rank()>& index)
48{
49 if constexpr (Axis == Mdspan::rank())
50 {
51 conjugate_element(span, index, std::make_index_sequence<Mdspan::rank()>{});
52 }
53 else
54 {
55 using index_type = typename Mdspan::index_type;
56 for (index_type i = 0; i < static_cast<index_type>(span.extent(Axis)); ++i)
57 {
58 index[Axis] = i;
59 conjugate_elements<Axis + 1>(span, index);
60 }
61 }
62}
63
65template <uni20::MutableMdspanLike Mdspan> consteval auto conjugate_acceptance()
66{
67 using span_type = std::remove_cvref_t<Mdspan>;
69 conjugate_element_is_assignable<span_type>(std::make_index_sequence<span_type::rank()>{}))
70 {
71 return kernel_types_yes;
72 }
73 else
74 {
75 return kernel_types_no;
76 }
77}
78
80template <class Mdspan> KernelAttempt conjugate(Mdspan& span)
81{
82 using span_type = std::remove_cvref_t<Mdspan>;
84 {
85 using value_type = typename span_type::value_type;
86 cpu::detail::transform_strided<true>(span,
87 [](auto const& value) { return uni20::conj(static_cast<value_type>(value)); });
88 }
89 else
90 {
91 std::array<typename span_type::index_type, span_type::rank()> index{};
92 conjugate_elements<0>(span, index);
93 }
95}
96} // namespace detail::cpu_reference
97
99template <uni20::MutableMdspecLike Mdspan>
101consteval auto kernel_accepts_types(CpuReferenceBackend const&, conjugate_inplace_op const&, Mdspan&)
102{
103 using span_type = uni20::host_write_mdspan_t<Mdspan>;
104 constexpr auto acceptance = detail::cpu_reference::conjugate_acceptance<span_type>();
105 if constexpr (acceptance == KernelTypeAcceptance::yes)
106 return kernel_types_yes;
107 else
108 return kernel_types_no;
109}
110
112template <uni20::MutableMdspecLike Mdspan>
115{
116 auto access = acquire_host_write_access_sync(mdspan);
117 auto span = access.mdspan();
118 return detail::cpu_reference::conjugate(span);
119}
120
121} // 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
Mutable mdspan-like types whose layout reports they are always strided.
Definition mdspan/concepts.hpp:421
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.
uni20::complex< T > conj(detail::standard_complex< T > x)
Returns the complex conjugate for complex-valued scalars.
Definition math.hpp:70
Scalar math helper utilities.
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
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
typename optional_traits< T >::value_type value_type
Definition data_table.hpp:42
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.
std::size_t span
Definition presentation.cpp:2017
CPU executor for compact strided elementwise iteration plans.
Backend value for the reference host CPU dense linalg oracle.
Definition backend_selector.hpp:55
Accessor-respecting in-place scalar conjugation operation tag.
Definition operation_tags.hpp:39
Concepts for tensor-level objects that resolve mdspan kernel operands.