Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
reduction_output.hpp
Go to the documentation of this file.
1#pragma once
2
13
14#include <concepts>
15#include <cstddef>
16#include <functional>
17#include <type_traits>
18#include <utility>
19
21{
22
24template <class Output, class Scalar, std::size_t Rank> consteval bool output_is_supported()
25{
26 using output_type = std::remove_cvref_t<Output>;
28 {
29 return std::same_as<typename output_type::value_type, Scalar>;
30 }
31 else if constexpr (Rank == 0)
32 {
33 return std::same_as<output_type, Scalar> && requires(Output& output, Scalar value) { output = value; };
34 }
35 else
36 {
37 return false;
38 }
39}
40
42template <class Output>
44
45template <class Output, bool = uni20::MdspecLike<Output>> struct HostOutputType
46{
47 using type = std::remove_cvref_t<Output>;
48};
49
50template <class Output> struct HostOutputType<Output, true>
51{
53};
54
56template <class Output> using host_output_t = typename HostOutputType<Output>::type;
57
59template <class Output, class Scalar> void write_host_output(Output& output, Scalar value)
60{
62 output[] = value;
63 else
64 output = value;
65}
66
68template <class Output, class Function> KernelAttempt with_host_output(Output& output, Function&& function)
69{
70 if constexpr (uni20::MdspecLike<Output>)
71 {
72 auto output_access = acquire_host_write_access_sync(output);
73 auto output_span = output_access.mdspan();
74 return std::invoke(std::forward<Function>(function), output_span);
75 }
76 else
77 {
78 return std::invoke(std::forward<Function>(function), output);
79 }
80}
81
82} // namespace uni20::linalg::reduction_detail
RAII leases that resolve device descriptors and tensor views to mdspans.
Mutable mdspec metadata supporting host write acquisition.
Definition access.hpp:694
Mdspan metadata whose data handle is immediate or available through a data descriptor.
Definition mdspan/concepts.hpp:304
A mutable mdspan-like type with a specified static rank.
Definition mdspan/concepts.hpp:494
Concept for numeric scalar types (integer, real, or complex).
Definition scalar_concepts.hpp:81
Reduction output that is either a host scalar or host-writable mdspec.
Definition reduction_output.hpp:43
locale_t value
Definition data_table.cpp:49
Allocation-free runtime results for kernel backend attempts.
Mdspan concept and accessor extensions for Uni20.
Definition reduction_output.hpp:21
typename HostOutputType< Output >::type host_output_t
Resolved output type used by a host reduction backend probe.
Definition reduction_output.hpp:56
void write_host_output(Output &output, Scalar value)
Store a scalar in either a resolved rank-zero mdspan or C++ scalar.
Definition reduction_output.hpp:59
consteval bool output_is_supported()
Report whether an output can receive a reduction value of fixed rank.
Definition reduction_output.hpp:24
KernelAttempt with_host_output(Output &output, Function &&function)
Invoke a synchronous reduction with a resolved host output.
Definition reduction_output.hpp:68
KernelAttempt
Result of one runtime kernel backend attempt.
Definition kernel_attempt.hpp:27
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
auto acquire_host_write_access_sync(Mdspan &mdspan)
Acquire a directly host-accessible writable mdspan.
Definition access.hpp:626
uni20::host_write_mdspan_t< Output > type
Definition reduction_output.hpp:52
Definition reduction_output.hpp:46
std::remove_cvref_t< Output > type
Definition reduction_output.hpp:47
Concepts for tensor-level objects that resolve mdspan kernel operands.