Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
debug_cuda_scheduler.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include "cuda_task.hpp"
9#include "debug_scheduler.hpp"
10
13
14namespace uni20::async
15{
16
22class DebugCudaScheduler final : public DebugScheduler, public ICudaScheduler {
23 public:
26 : DebugScheduler(options), default_device_(cuda::Device::get(0).ordinal()), device_count_(cuda::Device::count())
27 {}
28
33 : DebugScheduler(options), default_device_(default_device.ordinal()), device_count_(cuda::Device::count())
34 {}
35
37
40 void schedule(CudaTask&& task) override
41 {
42 TaskRegistry::record_task_scheduled(task.coroutine_handle());
43 if (task.set_scheduler(this)) this->enqueue_task(std::move(task));
44 }
45
49 void schedule(CudaTask&& task, int device) override
50 {
51 auto const validated_device = cuda::Device::get(device);
52 cuda_promise(task.handle()).bind_device(validated_device.ordinal());
53 TaskRegistry::record_task_scheduled(task.coroutine_handle());
54 if (task.set_scheduler(this)) this->enqueue_task(std::move(task));
55 }
56
57 private:
58 bool accepts_route(TaskRoute route) const noexcept override
59 {
60 if (route.domain == TaskDomain::host) return !route.cuda_device;
61 if (route.domain != TaskDomain::cuda) return false;
62 int const device = route.cuda_device.value_or(default_device_);
63 return device >= 0 && device < device_count_;
64 }
65
66 bool can_direct_transfer(TaskHandle from, TaskHandle to) const noexcept override
67 {
68 if (from.domain() != TaskDomain::cuda) return true;
69 return this->activation_device(from) == this->activation_device(to);
70 }
71
72 void resume_task(BasicTask& task) override
73 {
74 if (task.handle().domain() != TaskDomain::cuda)
75 {
77 return;
78 }
79
80 int const device = this->activation_device(task.handle());
81 cuda::ScopedDevice device_scope(device);
82#if UNI20_ASYNC_DEBUG
83 DEBUG_CHECK_EQUAL(cuda::Device::current().ordinal(), device);
84#endif
86 }
87
88 [[nodiscard]] int activation_device(TaskHandle task) const noexcept
89 {
90 return cuda_promise(task).device().value_or(default_device_);
91 }
92
93 int default_device_;
94 int device_count_;
95};
96
97} // namespace uni20::async
static void record_task_scheduled(std::coroutine_handle<> h)
Records the call stack where a coroutine was submitted to a scheduler.
std::optional< int > device() const noexcept
Return the task's established CUDA device affinity, if any.
Definition async_task_promise.hpp:659
void bind_device(int device)
Bind the CUDA device before the coroutine starts.
Definition async_task_promise.hpp:673
CUDA-constrained public task type used for typed initial admission.
Definition cuda_task.hpp:14
Configurable debug scheduler that admits ordinary and CUDA tasks.
Definition debug_cuda_scheduler.hpp:22
void schedule(CudaTask &&task) override
Enqueue a CUDA task without establishing device affinity.
Definition debug_cuda_scheduler.hpp:40
void schedule(CudaTask &&task, int device) override
Bind and enqueue a CUDA task for initial execution on a device.
Definition debug_cuda_scheduler.hpp:49
DebugCudaScheduler(cuda::Device default_device, DebugSchedulerOptions options={})
Construct with an explicit default CUDA activation device.
Definition debug_cuda_scheduler.hpp:32
DebugCudaScheduler(DebugSchedulerOptions options={})
Construct with device zero as the default CUDA activation device.
Definition debug_cuda_scheduler.hpp:25
Single-threaded scheduler with configurable runnable-batch ordering.
Definition debug_scheduler.hpp:63
void enqueue_task(BasicTask &&task)
Add one already-bound task to the runnable queue.
Definition debug_scheduler.hpp:192
DebugSchedulerOptions const & options() const noexcept
Return the scheduler's immutable ordering configuration.
Definition debug_scheduler.hpp:136
void schedule(AsyncTask &&task) override
Enqueue a task for later run.
Definition debug_scheduler.hpp:71
virtual void resume_task(BasicTask &task)
Resume one queued task in the execution context selected by this scheduler.
Definition debug_scheduler.hpp:195
Scheduler interface for initial CudaTask admission.
Definition cuda_task.hpp:49
Validated CUDA device identity with cached immutable capabilities.
Definition device.hpp:105
static Device get(int ordinal)
Validate and return one CUDA device.
Definition device.cpp:103
static Device current()
Return the CUDA device currently selected on the calling thread.
Definition device.cpp:123
Defines the CUDA task admission type and scheduler interface.
Coroutine scheduler for AsyncTask.
CUDA device discovery and immutable hardware capability snapshots.
This represents the reverse-mode adjoint for functions of complex variables, assuming a real-valued s...
Definition async/async.hpp:26
CudaTaskPromise & cuda_promise(TaskHandle handle) noexcept
Narrow an erased task promise after verifying that it is CUDA-specific.
Definition async_task_promise.hpp:707
CUDA device, stream, resource, and scoped runtime primitives.
Configuration for deterministic debug scheduling.
Definition debug_scheduler.hpp:55
Execution requirements used to validate a scheduler route.
Definition async_task.hpp:43
#define DEBUG_CHECK_EQUAL(a, b,...)
Definition trace.hpp:618