Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
linalg/async/detail/output.hpp
Go to the documentation of this file.
1#pragma once
2
13
14#include <concepts>
15#include <memory>
16#include <type_traits>
17#include <utility>
18
19namespace uni20
20{
21namespace detail
22{
23
28template <class Alias> class AsyncAliasWriteDescriptorAwaiter {
29 public:
30 explicit AsyncAliasWriteDescriptorAwaiter(async::WriteBuffer<Alias>& output) : output_(std::addressof(output)) {}
31
32 [[nodiscard]] bool await_ready() const noexcept { return output_->await_ready(); }
33
34 void await_suspend(async::BasicTask task) noexcept { output_->await_suspend(std::move(task)); }
35
36 [[nodiscard]] Alias const& await_resume() { return output_->await_resume().get(); }
37
38#if UNI20_DEBUG_DAG
39 [[nodiscard]] auto node() const { return output_->node(); }
40 static constexpr auto debug_task_role() noexcept { return TaskRegistry::EpochTaskRole::Writer; }
41#endif
42
43 private:
45};
46
51template <AsyncTensorOutput Tensor> [[nodiscard]] auto mutable_async_tensor_awaiter(async::WriteBuffer<Tensor>& output)
52{
53 if constexpr (async::is_async_alias_v<Tensor>)
54 return AsyncAliasWriteDescriptorAwaiter<Tensor>{output};
55 else
56 return output.storage();
57}
58
63template <AsyncTensorOutput Tensor, class Awaited>
64[[nodiscard]] decltype(auto) mutable_async_tensor_value(Awaited&& awaited)
65{
66 if constexpr (async::is_async_alias_v<Tensor>)
67 {
68 return Tensor(std::forward<Awaited>(awaited));
69 }
70 else
71 {
72 if (!awaited.constructed()) throw async::buffer_write_uninitialized{};
73 return *awaited;
74 }
75}
76
78template <AsyncTensorOutput Tensor>
79[[nodiscard]] Tensor& prepare_async_scalar_output(async::shared_storage<Tensor>& storage)
80{
81 if (storage.constructed()) return *storage;
82 if constexpr (std::constructible_from<Tensor, uninitialized_t>)
83 return storage.emplace(uninitialized);
84 else if constexpr (std::default_initializable<Tensor>)
85 return storage.emplace();
86 else
88}
89
90} // namespace detail
91} // namespace uni20
The Async<T> container: coroutine‐safe asynchronous read/write.
Move-only promise-neutral ownership claim for a Uni20 coroutine.
Definition async_task.hpp:103
Exclusive mutable-access buffer for one Async<T> epoch.
Definition buffers.hpp:859
auto storage() &
Awaiter yielding direct access to writer shared storage.
Definition buffers.hpp:1029
Raised when a writer requests a mutable reference before construction.
Definition async_errors.hpp:74
A lightweight, thread-safe, reference-counted storage for a single object.
Definition shared_storage.hpp:94
bool constructed() const noexcept
Reports whether a value is currently constructed.
Definition shared_storage.hpp:234
T & emplace(Args &&... args)
Destroy any existing value and construct a new one in place.
Definition shared_storage.hpp:270
Coroutine scheduler for AsyncTask.
Capabilities shared by asynchronous Tensor operation wrappers.
Definition qnum.hpp:403
Definition async/async.hpp:26
constexpr uninitialized_t uninitialized
Explicit allocation tag for overwrite outputs and scratch space.
Definition common/initialization.hpp:22
Tensor(InputTensor const &) -> Tensor< tensor_element_t< InputTensor >, tensor_mdspec_t< InputTensor >::rank(), HostStorage, detail::materialized_layout_t< void, tensor_mdspec_t< InputTensor > >, DefaultAccessorFactory >
Deduce a runtime-extents host tensor that materializes a tensor view.