Skip to main content

Device

Struct Device 

Source
pub struct Device { /* private fields */ }
Expand description

A GPU compute device.

This is the main entry point for scry-gpu. A Device wraps a single GPU and provides methods to upload data, dispatch shaders, and read results back.

§Example

let gpu = Device::auto()?;

let input = gpu.upload(&[1.0f32, 2.0, 3.0, 4.0])?;
let output = gpu.alloc::<f32>(4)?;

gpu.dispatch(SHADER_SRC, &[&input, &output], 4)?;

let result: Vec<f32> = output.download()?;

Implementations§

Source§

impl Device

Source

pub fn auto() -> Result<Self>

Auto-select the best available GPU.

Tries backends in order of preference: CUDA → Vulkan → (Metal in future). CUDA is preferred when available because it enables cuBLAS matmul and native CUDA kernel dispatch.

Source

pub fn with_backend(kind: BackendKind) -> Result<Self>

Create a device with a specific backend.

Source

pub fn upload<T: Pod>(&self, data: &[T]) -> Result<Buffer<T>>

Upload a slice to GPU memory, returning a typed buffer.

Source

pub fn alloc<T: Pod>(&self, count: usize) -> Result<Buffer<T>>

Allocate an uninitialized GPU buffer for count elements of type T.

Source

pub fn dispatch( &self, shader_src: &str, buffers: &[&dyn GpuBuf], invocations: u32, ) -> Result<()>

Dispatch a WGSL compute shader.

Buffers are bound in order to @binding(0), @binding(1), etc. Workgroup dispatch dimensions are auto-calculated from invocations and the shader’s @workgroup_size.

Source

pub fn dispatch_configured( &self, config: &DispatchConfig<'_>, buffers: &[&dyn GpuBuf], ) -> Result<()>

Dispatch with full configuration.

Source

pub fn compile(&self, shader_src: &str) -> Result<Kernel>

Compile a WGSL compute shader into a reusable Kernel.

The returned kernel holds all GPU objects (pipeline, layouts, shader module) and can be dispatched many times via Device::run.

Uses "main" as the entry point. See Device::compile_named for a custom entry point.

Source

pub fn compile_named( &self, shader_src: &str, entry_point: &str, ) -> Result<Kernel>

Compile a WGSL shader with a specific entry point name.

Source

pub fn run( &self, kernel: &Kernel, buffers: &[&dyn GpuBuf], invocations: u32, ) -> Result<()>

Dispatch a precompiled kernel.

Buffers are bound in order to @binding(0), @binding(1), etc. Workgroup dispatch dimensions are auto-calculated from invocations and the kernel’s compiled @workgroup_size.

Source

pub fn run_with_push_constants( &self, kernel: &Kernel, buffers: &[&dyn GpuBuf], invocations: u32, push_constants: &[u8], ) -> Result<()>

Dispatch a precompiled kernel with push constants.

Source

pub fn run_configured( &self, kernel: &Kernel, buffers: &[&dyn GpuBuf], workgroups: [u32; 3], push_constants: Option<&[u8]>, ) -> Result<()>

Dispatch a precompiled kernel with explicit workgroup dimensions.

Use this for 2D/3D dispatches or when you need precise control over workgroup counts. For simple 1D dispatches, prefer Device::run.

Source

pub fn copy_buffer<T: Pod>(&self, src: &Buffer<T>) -> Result<Buffer<T>>

Create a GPU-to-GPU copy of a buffer.

Allocates a new buffer on the same device and copies the contents of src into it. The copy is synchronous (blocks until complete).

Source

pub fn batch(&self) -> Result<Batch>

Begin a batched dispatch session.

Records multiple dispatches into a single command buffer, submitted with one fence wait via [Batch::submit].

Source

pub fn name(&self) -> &str

Device name (for diagnostics / logging).

Source

pub fn memory(&self) -> u64

Total device memory in bytes.

Source

pub fn subgroup_size(&self) -> u32

Subgroup (warp/wavefront) size.

Typically 32 on NVIDIA, 64 on AMD, 32 on Intel. Useful for sizing subgroup-aware shaders.

Source

pub const fn backend_kind(&self) -> BackendKind

Which backend this device is using.

Trait Implementations§

Source§

impl Debug for Device

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.