scry-gpu 0.1.0

Lightweight GPU compute — dispatch shaders without the graphics baggage
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// SPDX-License-Identifier: MIT OR Apache-2.0
//! CUDA compute backend via `cudarc`.
//!
//! Compute-only CUDA path:
//! - Context → stream → cuBLAS handle
//! - `CudaSlice<u8>` buffer management
//! - NVRTC kernel compilation from CUDA C strings
//! - cuBLAS SGEMM for matmul
//!
//! SPIR-V dispatch is not supported — use [`CudaBackend::compile_cuda`] for
//! native CUDA kernels, or [`CudaBackend::cublas_matmul`] for matrix multiply.

use std::sync::{Arc, Mutex};

use cudarc::cublas::sys::cublasOperation_t;
use cudarc::cublas::CudaBlas;
use cudarc::driver::{
    CudaContext, CudaEvent, CudaFunction, CudaSlice, CudaStream, DevicePtr, DevicePtrMut,
    LaunchConfig, PushKernelArg,
};
use cudarc::nvrtc::{compile_ptx_with_opts, CompileOptions};

use crate::backend::{Backend, BackendBufferOps};
use crate::error::{backend_err, BackendOp, GpuError, Result};

// ── Public types ──

/// CUDA compute backend state.
pub struct CudaBackend {
    ctx: Arc<CudaContext>,
    stream: Arc<CudaStream>,
    blas: Mutex<CudaBlas>,
    device_name: String,
    device_memory: u64,
}

/// A buffer allocated on the CUDA device.
pub struct CudaBuffer {
    pub(crate) inner: CudaSlice<u8>,
    size: u64,
    stream: Arc<CudaStream>,
}

/// A compiled CUDA kernel, ready for dispatch.
pub struct CudaKernel {
    pub(crate) function: CudaFunction,
    /// Thread block dimensions `(x, y, z)`, matching the CUDA kernel's
    /// expected `blockDim`.
    pub(crate) block_dim: (u32, u32, u32),
}

/// A batch of kernel dispatches on a dedicated CUDA stream.
///
/// Kernel launches on a single stream are automatically serialized by the
/// CUDA runtime, so barriers are no-ops. [`CudaBatch::submit`] synchronizes
/// the stream.
pub struct CudaBatch {
    stream: Arc<CudaStream>,
}

impl CudaBackend {
    /// Compile a CUDA C source string into a reusable kernel.
    ///
    /// Uses NVRTC to compile to PTX, then loads and extracts the named
    /// entry point function.
    pub fn compile_cuda(
        &self,
        source: &str,
        entry_point: &str,
        block_dim: (u32, u32, u32),
    ) -> Result<CudaKernel> {
        let opts = CompileOptions {
            use_fast_math: Some(true),
            ..Default::default()
        };
        let ptx = compile_ptx_with_opts(source, opts)
            .map_err(|e| backend_err(BackendOp::CompileKernel, e))?;
        let module = self
            .ctx
            .load_module(ptx)
            .map_err(|e| backend_err(BackendOp::LoadModule, e))?;
        let function = module
            .load_function(entry_point)
            .map_err(|e| backend_err(BackendOp::LoadFunction, e))?;

        Ok(CudaKernel {
            function,
            block_dim,
        })
    }

    /// Dispatch a compiled CUDA kernel.
    ///
    /// Buffer device pointers are passed as kernel arguments in order,
    /// followed by push constant bytes split into `u32` arguments.
    pub fn dispatch_cuda(
        &self,
        kernel: &CudaKernel,
        buffers: &[&CudaBuffer],
        workgroups: [u32; 3],
        push_constants: Option<&[u8]>,
    ) -> Result<()> {
        let config = LaunchConfig {
            grid_dim: (workgroups[0], workgroups[1], workgroups[2]),
            block_dim: kernel.block_dim,
            shared_mem_bytes: 0,
        };

        // Collect push constant u32 values so they outlive the builder.
        let pc_values: Vec<u32> = push_constants
            .map(|pc| {
                pc.chunks_exact(4)
                    .map(|c| u32::from_ne_bytes([c[0], c[1], c[2], c[3]]))
                    .collect()
            })
            .unwrap_or_default();

        unsafe {
            let mut builder = self.stream.launch_builder(&kernel.function);

            // Push buffer device pointers as kernel arguments.
            for buf in buffers {
                builder.arg(&buf.inner);
            }

            // Push constants as individual u32 kernel arguments.
            for val in &pc_values {
                builder.arg(val);
            }

            builder
                .launch(config)
                .map_err(|e| backend_err(BackendOp::LaunchKernel, e))?;
        }

        // Synchronize to match the Vulkan fence-wait semantics.
        self.stream
            .synchronize()
            .map_err(|e| backend_err(BackendOp::StreamSync, e))?;

        Ok(())
    }

    /// Run cuBLAS SGEMM: C = alpha * A * B + beta * C.
    ///
    /// Buffers must contain `f32` data. This is the recommended path for
    /// matrix multiplication on CUDA — it reaches 80%+ peak throughput
    /// without any custom kernels.
    ///
    /// Matrix layout is row-major. Dimensions: A is `m×k`, B is `k×n`,
    /// C is `m×n`.
    #[allow(clippy::many_single_char_names)]
    pub fn cublas_matmul(
        &self,
        a: &CudaBuffer,
        b: &CudaBuffer,
        c: &mut CudaBuffer,
        m: u32,
        n: u32,
        k: u32,
    ) -> Result<()> {
        // cuBLAS uses column-major layout. For row-major C = A * B, we
        // compute C^T = B^T * A^T in cuBLAS terms, which gives us:
        //   sgemm(N, N, n, m, k, 1.0, B, n, A, k, 0.0, C, n)
        #[allow(clippy::cast_possible_wrap)]
        unsafe {
            let blas = self
                .blas
                .lock()
                .map_err(|_| backend_err(BackendOp::MutexPoisoned, "cublas"))?;
            let (a_ptr, _a_guard) = a.inner.device_ptr(&self.stream);
            let (b_ptr, _b_guard) = b.inner.device_ptr(&self.stream);
            let (c_ptr, _c_guard) = c.inner.device_ptr_mut(&self.stream);

            cudarc::cublas::result::sgemm(
                *blas.handle(),
                cublasOperation_t::CUBLAS_OP_N,
                cublasOperation_t::CUBLAS_OP_N,
                n as i32,
                m as i32,
                k as i32,
                &1.0f32,
                b_ptr as *const f32,
                n as i32,
                a_ptr as *const f32,
                k as i32,
                &0.0f32,
                c_ptr as *mut f32,
                n as i32,
            )
            .map_err(|e| backend_err(BackendOp::CuBlas, e))?;
        }

        // Sync after cuBLAS call.
        self.stream
            .synchronize()
            .map_err(|e| backend_err(BackendOp::StreamSync, e))?;

        Ok(())
    }

    /// Begin a batch dispatch session.
    ///
    /// On CUDA, kernel launches on the same stream are inherently batched
    /// (queued without GPU idle time), so this uses the default stream.
    /// [`CudaBatch::submit`] synchronizes once at the end.
    #[allow(clippy::unnecessary_wraps)]
    pub fn begin_batch(&self) -> Result<CudaBatch> {
        Ok(CudaBatch {
            stream: Arc::clone(&self.stream),
        })
    }
}

// ── Backend trait implementation ──

impl Backend for CudaBackend {
    type Buffer = CudaBuffer;
    type Pipeline = CudaKernel;

    fn create() -> Result<Self> {
        let ctx = CudaContext::new(0).map_err(|e| backend_err(BackendOp::CreateDevice, e))?;

        let device_name = ctx
            .name()
            .map_err(|e| backend_err(BackendOp::DeviceQuery, e))?;
        let device_memory =
            ctx.total_mem()
                .map_err(|e| backend_err(BackendOp::DeviceQuery, e))? as u64;

        let stream = ctx.default_stream();
        let blas = CudaBlas::new(stream.clone()).map_err(|e| backend_err(BackendOp::CuBlas, e))?;

        Ok(Self {
            ctx,
            stream,
            blas: Mutex::new(blas),
            device_name,
            device_memory,
        })
    }

    fn upload(&self, data: &[u8]) -> Result<Self::Buffer> {
        let size = data.len() as u64;
        let inner = self
            .stream
            .clone_htod(data)
            .map_err(|e| backend_err(BackendOp::CopyBuffer, e))?;
        Ok(CudaBuffer {
            inner,
            size,
            stream: Arc::clone(&self.stream),
        })
    }

    fn alloc(&self, size: u64) -> Result<Self::Buffer> {
        let inner = self
            .stream
            .alloc_zeros::<u8>(size as usize)
            .map_err(|e| backend_err(BackendOp::CreateBuffer, e))?;
        Ok(CudaBuffer {
            inner,
            size,
            stream: Arc::clone(&self.stream),
        })
    }

    fn dispatch(
        &self,
        _spirv: &[u32],
        _entry_point: &str,
        _buffers: &[&Self::Buffer],
        _workgroups: [u32; 3],
        _push_constants: Option<&[u8]>,
    ) -> Result<()> {
        Err(GpuError::BackendUnavailable(
            "CUDA cannot execute SPIR-V shaders — use compile_cuda() instead".into(),
        ))
    }

    fn create_pipeline(
        &self,
        _spirv: &[u32],
        _entry_point: &str,
        _binding_count: usize,
        _push_constant_size: u32,
    ) -> Result<Self::Pipeline> {
        Err(GpuError::BackendUnavailable(
            "CUDA cannot compile SPIR-V pipelines — use compile_cuda() instead".into(),
        ))
    }

    fn dispatch_pipeline(
        &self,
        pipeline: &Self::Pipeline,
        buffers: &[&Self::Buffer],
        workgroups: [u32; 3],
        push_constants: Option<&[u8]>,
    ) -> Result<()> {
        self.dispatch_cuda(pipeline, buffers, workgroups, push_constants)
    }

    fn device_name(&self) -> &str {
        &self.device_name
    }

    fn device_memory(&self) -> u64 {
        self.device_memory
    }

    fn subgroup_size(&self) -> u32 {
        // NVIDIA warp size is always 32.
        32
    }

    fn copy_buffer(&self, src: &Self::Buffer, size: u64) -> Result<Self::Buffer> {
        let mut dst = self
            .stream
            .alloc_zeros::<u8>(size as usize)
            .map_err(|e| backend_err(BackendOp::CreateBuffer, e))?;
        self.stream
            .memcpy_dtod(&mut dst, &src.inner, size as usize)
            .map_err(|e| backend_err(BackendOp::CopyBuffer, e))?;
        Ok(CudaBuffer {
            inner: dst,
            size,
            stream: Arc::clone(&self.stream),
        })
    }
}

// ── Buffer operations ──

impl BackendBufferOps for CudaBuffer {
    fn read_back(&self) -> Result<Vec<u8>> {
        self.stream
            .clone_dtoh(&self.inner)
            .map_err(|e| backend_err(BackendOp::CopyBuffer, e))
    }

    fn byte_size(&self) -> u64 {
        self.size
    }
}

// ── Batch dispatch ──

impl CudaBatch {
    /// Record a kernel dispatch into the batch stream.
    pub fn record_dispatch(
        &mut self,
        kernel: &CudaKernel,
        buffers: &[&CudaBuffer],
        workgroups: [u32; 3],
        push_constants: Option<&[u8]>,
    ) -> Result<()> {
        let config = LaunchConfig {
            grid_dim: (workgroups[0], workgroups[1], workgroups[2]),
            block_dim: kernel.block_dim,
            shared_mem_bytes: 0,
        };

        let pc_values: Vec<u32> = push_constants
            .map(|pc| {
                pc.chunks_exact(4)
                    .map(|c| u32::from_ne_bytes([c[0], c[1], c[2], c[3]]))
                    .collect()
            })
            .unwrap_or_default();

        unsafe {
            let mut builder = self.stream.launch_builder(&kernel.function);

            for buf in buffers {
                builder.arg(&buf.inner);
            }

            for val in &pc_values {
                builder.arg(val);
            }

            builder
                .launch(config)
                .map_err(|e| backend_err(BackendOp::LaunchKernel, e))?;
        }

        Ok(())
    }

    /// No-op on CUDA — kernel launches on the same stream are serialized.
    #[allow(
        clippy::unused_self,
        clippy::needless_pass_by_ref_mut,
        clippy::missing_const_for_fn
    )]
    pub fn record_barrier(&mut self) {
        // CUDA streams serialize operations automatically.
    }

    /// Submit all recorded dispatches and return a [`CudaTicket`] for
    /// non-blocking completion tracking.
    pub fn submit_async(self) -> Result<CudaTicket> {
        let event = self
            .stream
            .record_event(None)
            .map_err(|e| backend_err(BackendOp::RecordEvent, e))?;
        Ok(CudaTicket {
            stream: self.stream,
            event,
        })
    }
}

/// In-flight GPU submission handle for the CUDA backend.
///
/// Records a [`CudaEvent`] at submit time so completion can be polled
/// without synchronizing the entire stream.
pub struct CudaTicket {
    stream: Arc<CudaStream>,
    event: CudaEvent,
}

impl CudaTicket {
    /// Block until all work preceding the recorded event completes.
    pub(crate) fn wait(self) -> Result<()> {
        self.stream
            .synchronize()
            .map_err(|e| backend_err(BackendOp::StreamSync, e))
    }

    /// Check whether the recorded event has been reached without blocking.
    pub(crate) fn is_ready(&self) -> bool {
        self.event.is_complete()
    }
}