Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
async/gemv.hpp
Go to the documentation of this file.
1#pragma once
2
17
18#include <memory>
19#include <tuple>
20#include <utility>
21
22namespace uni20::linalg
23{
24namespace detail
25{
26template <class BackendSelector, uni20::AsyncTensorOutput OutputTensor, uni20::RankedTensorView<2> MatrixTensor,
27 uni20::RankedTensorView<1> InputTensor, class AlphaAwaiter, class BetaAwaiter>
28async::AsyncTask co_gemv(BackendSelector const selector, async::WriteBuffer<OutputTensor> output,
30 AlphaAwaiter alpha, BetaAwaiter beta)
31{
32 auto output_awaiter = uni20::detail::mutable_async_tensor_awaiter(output);
33 auto awaited = co_await async::all(output_awaiter, matrix, input, alpha, beta);
34 decltype(auto) output_value = uni20::detail::mutable_async_tensor_value<OutputTensor>(std::get<0>(awaited));
35 auto const& matrix_value = std::get<1>(awaited);
36 auto const& input_value = std::get<2>(awaited);
37 using scalar_type = uni20::tensor_element_t<OutputTensor>;
38 scalar_type const alpha_value = static_cast<scalar_type>(std::get<3>(awaited));
39 scalar_type const beta_value = static_cast<scalar_type>(std::get<4>(awaited));
40
41 auto output_descriptor = uni20::mdspec_of(output_value);
42 auto matrix_descriptor = uni20::mdspec_of(matrix_value);
43 auto input_descriptor = uni20::mdspec_of(input_value);
44 co_await co_dispatch_kernel(selector, gemv_op{}, output_descriptor, alpha_value, matrix_descriptor, input_descriptor,
45 beta_value);
46 co_return;
47}
48
49template <class BackendSelector, uni20::AsyncTensorOutput OutputTensor, uni20::RankedTensorView<2> MatrixTensor,
50 uni20::RankedTensorView<1> InputTensor, class AlphaAwaiter, class BetaAwaiter>
51void schedule_async_gemv(BackendSelector selector, async::Async<OutputTensor>& output,
52 async::Async<MatrixTensor> const& matrix, async::Async<InputTensor> const& input,
53 AlphaAwaiter alpha, BetaAwaiter beta)
54{
55 auto const* const output_queue = std::addressof(output.queue());
56 ERROR_IF(output_queue == std::addressof(matrix.queue()) || output_queue == std::addressof(input.queue()),
57 "async gemv output must not share an epoch queue with an input");
58 auto task =
59 co_gemv(std::move(selector), output.write(), matrix.read(), input.read(), std::move(alpha), std::move(beta));
60 task.debug_name("gemv");
61 async::schedule(std::move(task));
62}
63} // namespace detail
64
68template <KernelBackendSelector BackendSelector, uni20::AsyncTensorOutput OutputTensor, class Alpha,
69 uni20::RankedTensorView<2> MatrixTensor, uni20::RankedTensorView<1> InputTensor, class Beta>
71 AsyncOperationScalar<Alpha, uni20::tensor_element_t<OutputTensor>> &&
72 AsyncOperationScalar<Beta, uni20::tensor_element_t<OutputTensor>>
73void gemv(BackendSelector selector, async::Async<OutputTensor>& output, Alpha&& alpha,
74 async::Async<MatrixTensor> const& matrix, async::Async<InputTensor> const& input, Beta&& beta)
75{
76 detail::schedule_async_gemv(std::move(selector), output, matrix, input, async::read(std::forward<Alpha>(alpha)),
77 async::read(std::forward<Beta>(beta)));
78}
79
81template <uni20::AsyncTensorOutput OutputTensor, class Alpha, uni20::RankedTensorView<2> MatrixTensor,
82 uni20::RankedTensorView<1> InputTensor, class Beta>
84 AsyncOperationScalar<Alpha, uni20::tensor_element_t<OutputTensor>> &&
85 AsyncOperationScalar<Beta, uni20::tensor_element_t<OutputTensor>>
86void gemv(async::Async<OutputTensor>& output, Alpha&& alpha, async::Async<MatrixTensor> const& matrix,
87 async::Async<InputTensor> const& input, Beta&& beta)
88{
89 auto selector = select_backend_for<OutputTensor, MatrixTensor, InputTensor>(gemv_op{});
90 gemv(std::move(selector), output, std::forward<Alpha>(alpha), matrix, input, std::forward<Beta>(beta));
91}
92
93} // namespace uni20::linalg
The Async<T> container: coroutine‐safe asynchronous read/write.
Coroutine-aware kernel dispatch with an ordinary blocking fallback.
Utilities for awaiting on Async<T>: co_await overload, all(), try_await().
Canonical host-oriented coroutine task type.
Definition async_task.hpp:196
Async<T> is a container for coroutine-safe asynchronous read/write.
Definition async/async.hpp:93
ReadBuffer< T > read() const
Begin an asynchronous read of the value.
Definition async/async.hpp:339
EpochQueue const & queue() const
Inspect the shared epoch queue.
Definition async/async.hpp:423
WriteBuffer< T > write()
Begin exclusive asynchronous access to the payload.
Definition async/async.hpp:346
RAII handle for reading the value from an Async container at a given epoch.
Definition buffers.hpp:53
Exclusive mutable-access buffer for one Async<T> epoch.
Definition buffers.hpp:859
Mutable Tensor output supported by async operation wrappers.
Definition linalg/async/concepts.hpp:25
MutableTensorView with a specified static rank.
Definition tensor/concepts.hpp:166
TensorView whose readable mdspec has a specified static rank.
Definition tensor/concepts.hpp:149
Coroutine scheduler for AsyncTask.
Capabilities shared by asynchronous Tensor operation wrappers.
Shared output capabilities for Async Tensor operation wrappers.
void schedule(AsyncTask &&task)
Schedule a task on the currently configured global scheduler.
Definition debug_scheduler.hpp:237
auto all(Aw &&... aw) noexcept
Build an awaitable that waits for all of the provided awaitables.
Definition awaiters.hpp:200
ReadBuffer< T > read(Async< T > const &a)
Convenience helper that forwards to Async<T>::read().
Definition async/async.hpp:540
Definition linalg/async.hpp:28
async::AsyncTask co_dispatch_kernel(BackendSelector selector, Op op, Args &... args)
Dispatch a kernel from a coroutine without requiring coroutine wrappers for blocking backends.
Definition async/dispatch.hpp:106
void gemv(BackendSelector selector, async::Async< OutputTensor > &output, Alpha &&alpha, async::Async< MatrixTensor > const &matrix, async::Async< InputTensor > const &input, Beta &&beta)
Schedule fixed-output GEMV through an explicit backend selector.
Definition async/gemv.hpp:73
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
std::remove_cv_t< typename tensor_mdspec_t< T >::element_type > tensor_element_t
Scalar value type exposed by a tensor-level object.
Definition tensor/concepts.hpp:126
mpreal beta(mpreal const &a, mpreal const &x)
Real Beta function; positive rational/integer pairs retain exact values.
Definition mpreal_special.hpp:164
Fixed-output Tensor GEMV front end.
Dense matrix-vector multiplication operation tag.
Definition operation_tags.hpp:90
#define ERROR_IF(cond,...)
Definition trace.hpp:389