Skip to main content

vyre_driver_cuda/
device_work_queue.rs

1//! CUDA adapter for backend-neutral device-side work queue planning.
2
3use vyre_driver::device_work_queue::{
4    plan_device_work_queue, plan_device_work_queue_backpressure, DeviceWorkQueueBackpressurePlan,
5    DeviceWorkQueueDrainStrategy, DeviceWorkQueueError, DeviceWorkQueuePlan,
6    DeviceWorkQueueProfile, WorkQueueHostSync,
7};
8
9/// Host synchronization policy for a CUDA device-side work queue.
10pub type CudaWorkQueueHostSync = WorkQueueHostSync;
11/// Work queue workload profile.
12pub type CudaDeviceWorkQueueProfile = DeviceWorkQueueProfile;
13/// Device-side work queue execution plan.
14pub type CudaDeviceWorkQueuePlan = DeviceWorkQueuePlan;
15/// Device-side work queue drain strategy.
16pub type CudaDeviceWorkQueueDrainStrategy = DeviceWorkQueueDrainStrategy;
17/// Device-side work queue plan with bounded resident drain windows.
18pub type CudaDeviceWorkQueueBackpressurePlan = DeviceWorkQueueBackpressurePlan;
19/// Device work queue planning errors.
20pub type CudaDeviceWorkQueueError = DeviceWorkQueueError;
21
22/// Plan a CUDA-resident work queue for dependent dataflow execution.
23pub fn plan_cuda_device_work_queue(
24    profile: CudaDeviceWorkQueueProfile,
25) -> Result<CudaDeviceWorkQueuePlan, CudaDeviceWorkQueueError> {
26    plan_device_work_queue(profile)
27}
28
29/// Plan a CUDA-resident work queue plus bounded device-side drain windows.
30pub fn plan_cuda_device_work_queue_backpressure(
31    profile: CudaDeviceWorkQueueProfile,
32    max_items_per_drain_launch: u64,
33) -> Result<CudaDeviceWorkQueueBackpressurePlan, CudaDeviceWorkQueueError> {
34    plan_device_work_queue_backpressure(profile, max_items_per_drain_launch)
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn cuda_device_work_queue_is_adapter_not_policy_fork() {
43        let source = include_str!("device_work_queue.rs");
44        let production = source
45            .split("#[cfg(test)]")
46            .next()
47            .expect("Fix: CUDA device work-queue adapter source must contain production section");
48
49        assert!(source.contains("use vyre_driver::device_work_queue::{"));
50        assert!(source.contains("pub type CudaDeviceWorkQueueProfile = DeviceWorkQueueProfile;"));
51        assert!(source.contains("plan_device_work_queue(profile)"));
52        assert!(source
53            .contains("plan_device_work_queue_backpressure(profile, max_items_per_drain_launch)"));
54        assert!(!production.contains("CudaArithmeticOverflow"));
55        assert!(!production.contains("checked_add_u64_count"));
56        assert!(!production.contains("checked_mul_u64_count"));
57        assert!(!production.contains("CUDA_NUMERIC"));
58        assert!(!production.contains("fn div_ceil_u64"));
59    }
60
61    #[test]
62    fn cuda_device_work_queue_adapter_preserves_final_only_contract() {
63        let plan = plan_cuda_device_work_queue(CudaDeviceWorkQueueProfile {
64            initial_items: 256,
65            queue_capacity: 1_024,
66            entry_bytes: 16,
67            control_bytes: 128,
68            budget_bytes: 32_768,
69            host_sync: CudaWorkQueueHostSync::FinalOnly,
70        })
71        .expect("Fix: valid CUDA device work queue should plan through shared owner");
72
73        assert_eq!(plan.queue_bytes, 16_384);
74        assert_eq!(plan.control_bytes, 128);
75        assert_eq!(plan.resident_bytes, 16_512);
76        assert_eq!(plan.initial_occupancy_bps, 2_500);
77        assert!(plan.final_only_host_sync);
78    }
79
80    #[test]
81    fn cuda_device_work_queue_adapter_preserves_backpressure_contract() {
82        let plan = plan_cuda_device_work_queue_backpressure(
83            CudaDeviceWorkQueueProfile {
84                initial_items: 4_096,
85                queue_capacity: 65_536,
86                entry_bytes: 16,
87                control_bytes: 128,
88                budget_bytes: 2 << 20,
89                host_sync: CudaWorkQueueHostSync::FinalOnly,
90            },
91            8_192,
92        )
93        .expect("Fix: CUDA device work queue backpressure should plan through shared owner");
94
95        assert_eq!(
96            plan.strategy,
97            CudaDeviceWorkQueueDrainStrategy::ChunkedResidentDrain
98        );
99        assert_eq!(plan.items_per_chunk, 8_192);
100        assert_eq!(plan.chunks, 8);
101        assert!(plan.final_only_host_sync);
102    }
103}