|
Uni20
Asynchronous tensor and runtime library.
|
This guide is for contributors who are new to Uni20 async code.
Goal: after reading this, you should be able to write a small coroutine kernel, understand why it suspends/resumes, and avoid the most common correctness traps.
Uni20 async code expresses dependency order directly in dataflow form. You do not manually lock data structures or coordinate condition variables.
Instead:
Async<T>ReadBuffer<T> and WriteBuffer<T>co_await until the epoch is readyThink of each Async<T> as a timeline of versions (epochs): one writer phase, then reader phase, then next epoch.
For first experiments and most unit tests, use DebugScheduler. Normal application code can use the default global scheduler without ScopedScheduler.
Many operators on Async<T> (and mixes of Async<T> with scalars) build a DAG by scheduling coroutines. They do not compute immediately.
Operator kernels schedule work onto the global scheduler. If you need a non-default scheduler, use ScopedScheduler or set_global_scheduler(...). If you only need completion and do not need the value, use b.wait() instead.
What happened:
value starts with unconstructed storage.co_await writer = 42 and constructs the value.sched.run_all() drains all scheduled work.Default Async<T> does not default-construct T.
First write is safe through proxy assignment when the source can both construct and later assign T:
If the value already exists, this evaluates stored_value = source. Use explicit emplace(...) when reconstruction rather than assignment is intended.
What is unsafe is forcing mutable-reference style access before construction:
This can throw buffer_write_uninitialized.
When input and output are different timelines, use a reader for the input and a writer for the output:
Why owned.release() is often useful:
transfer() is the explicit owning form for named buffers. Direct co_await on a temporary buffer still uses the owning rvalue path.
WriteBuffer<T> provides exclusive mutable access; it is not write-only. Use the existing value through the write proxy:
Do not model ordinary mutation by taking both value.read() and value.write(). Those buffers select different epochs and cannot be held as simultaneous read/write access. A deliberate read-then-write transition on one queue is an advanced pattern that must copy the required data and release the reader before awaiting the later writer.
Main buffer access failures:
buffer_read_uninitializedbuffer_write_uninitializedtask_cancelledUnhandled exceptions thrown inside a coroutine are captured in the promise and forwarded to registered sink epochs (usually output write buffers).
To explicitly route exceptions to additional buffers, use:
For async coroutine lambdas (AsyncTask return type):
static lambda call operatorCorrect pattern:
This avoids closure lifetime issues when coroutine lifetime exceeds lambda object lifetime.
coroutines_primer.mdruntime_model.mdbuffers_and_awaiters.mdcookbook.mdexceptions_and_cancellation.mdschedulers.mdReverseValue, Var): reverse_mode_ad.mdquick_reference.mdThe examples/ folder contains runnable async demos that match current semantics:
examples/async/async_example.cpp: basic reads/writes and try_awaitexamples/async/async_example2.cpp: async toy IO + expression compositionexamples/async/async_ops_example.cpp: expression DAG composition + all(...)examples/async/async_buffer_semantics_example.cpp: read/write ownership, cancellation paths, and exception routingexamples/async/async_tbb_reduction_example.cpp: parallel map-reduce with TbbSchedulerexamples/async/async_dag_debug_example.cpp: Graphviz snapshots of a blocked async DAGexamples/async/async_diagnostics_guide_example.cpp: guided task-registry, stacktrace, xdot, and presentation setupexamples/async/async_coroutine_failure_example.cpp: exception propagation from a failed coroutine to get_wait(), including opt-in live registry and DOT diagnosticsexamples/async/async_dag_deadlock_tbb_example.cpp: deliberate TBB dataflow deadlock with a best-effort DOT dumpexamples/async/async_fib_example.cpp: recursive async composition