pub struct GpuContext { /* private fields */ }Expand description
GPU context for compute operations.
Manages GPU device, queue, and cached compute pipelines for FST operations. Create once and reuse for multiple operations to amortize initialization cost.
§Initialization
GPU context creation is an async operation that:
- Enumerates available GPU adapters
- Requests a device with required features
- Compiles and caches compute shader pipelines
Use pollster::block_on() for synchronous initialization:
use arcweight::gpu::GpuContext;
let ctx = pollster::block_on(GpuContext::new())?;
println!("Using GPU: {}", ctx.adapter_info().name);§Thread Safety
GpuContext is Send but not Sync. Each thread should have its own context,
or use synchronization primitives to share access.
§Resource Management
GPU resources are automatically cleaned up when the context is dropped. Buffers created from this context remain valid as long as the context lives.
Implementations§
Source§impl GpuContext
impl GpuContext
Sourcepub async fn new() -> Result<Self>
pub async fn new() -> Result<Self>
Create a new GPU context.
Initializes the GPU backend by requesting a high-performance adapter, creating a device, and compiling compute shader pipelines.
§Async
This is an async operation. Use pollster::block_on() for synchronous
initialization in non-async contexts.
§Errors
Returns an error if:
- No compatible GPU adapter is found
- Device creation fails
- Shader compilation fails
§Examples
use arcweight::gpu::GpuContext;
// Async context
async fn init_gpu() -> arcweight::Result<GpuContext> {
GpuContext::new().await
}
// Synchronous context
let ctx = pollster::block_on(GpuContext::new())?;Sourcepub fn adapter_info(&self) -> &AdapterInfo
pub fn adapter_info(&self) -> &AdapterInfo
Get information about the GPU adapter.
Returns details about the GPU including name, vendor, and backend.
Sourcepub fn device(&self) -> &Device
pub fn device(&self) -> &Device
Get the underlying wgpu device.
Use this for advanced GPU operations not provided by this module.
Sourcepub fn queue(&self) -> &Queue
pub fn queue(&self) -> &Queue
Get the wgpu command queue.
Use this for submitting custom command buffers.
Sourcepub fn create_weight_buffer(&self, weights: &[f32]) -> Result<GpuWeightBuffer>
pub fn create_weight_buffer(&self, weights: &[f32]) -> Result<GpuWeightBuffer>
Create a weight buffer on the GPU.
Uploads weight data to GPU memory for use in compute operations. The buffer is created with storage and copy usage flags.
§Arguments
weights- Slice of f32 weights to upload
§Complexity
- Time: O(n) for data transfer
- Space: O(n) GPU memory
§Examples
use arcweight::gpu::GpuContext;
let ctx = pollster::block_on(GpuContext::new())?;
let weights = vec![1.0f32, 2.0, 3.0, 4.0];
let gpu_buffer = ctx.create_weight_buffer(&weights)?;
assert_eq!(gpu_buffer.len(), 4);Sourcepub fn reduce_min_gpu(&self, weights: &GpuWeightBuffer) -> Result<f32>
pub fn reduce_min_gpu(&self, weights: &GpuWeightBuffer) -> Result<f32>
Compute minimum value using GPU parallel reduction.
Implements tropical semiring addition (minimum) using a multi-pass parallel reduction algorithm optimized for GPU execution.
§Algorithm
Uses a tree-based parallel reduction:
- Each workgroup (256 threads) reduces 256 elements to 1
- Results are written to intermediate buffer
- Repeat until single value remains
§Complexity
- Time: O(n / p + log p) where p = number of GPU cores
- Space: O(n / 256) for intermediate buffers
- Passes: ceil(log_256(n))
§Examples
use arcweight::gpu::GpuContext;
let ctx = pollster::block_on(GpuContext::new())?;
let weights: Vec<f32> = (0..10000).map(|i| i as f32).collect();
let buffer = ctx.create_weight_buffer(&weights)?;
let min = ctx.reduce_min_gpu(&buffer)?;
assert_eq!(min, 0.0);Sourcepub fn reduce_sum_gpu(&self, weights: &GpuWeightBuffer) -> Result<f32>
pub fn reduce_sum_gpu(&self, weights: &GpuWeightBuffer) -> Result<f32>
Compute sum using GPU parallel reduction.
Implements probability semiring addition (sum) using a multi-pass parallel reduction algorithm.
§Complexity
Same as reduce_min_gpu.
Sourcepub fn reduce_min(&self, weights: &GpuWeightBuffer) -> Result<f32>
pub fn reduce_min(&self, weights: &GpuWeightBuffer) -> Result<f32>
Compute minimum value in a weight buffer (CPU fallback)
Uses parallel reduction on the GPU for O(log n) depth.
Sourcepub fn reduce_sum(&self, weights: &GpuWeightBuffer) -> Result<f32>
pub fn reduce_sum(&self, weights: &GpuWeightBuffer) -> Result<f32>
Compute sum of values in a weight buffer (CPU fallback)
Sourcepub fn read_buffer(&self, buffer: &GpuWeightBuffer) -> Result<Vec<f32>>
pub fn read_buffer(&self, buffer: &GpuWeightBuffer) -> Result<Vec<f32>>
Read a buffer back to CPU memory
Sourcepub fn elementwise_min_gpu(
&self,
a: &GpuWeightBuffer,
b: &GpuWeightBuffer,
) -> Result<GpuWeightBuffer>
pub fn elementwise_min_gpu( &self, a: &GpuWeightBuffer, b: &GpuWeightBuffer, ) -> Result<GpuWeightBuffer>
Element-wise minimum of two weight buffers using GPU
Sourcepub fn elementwise_min(
&self,
a: &GpuWeightBuffer,
b: &GpuWeightBuffer,
) -> Result<GpuWeightBuffer>
pub fn elementwise_min( &self, a: &GpuWeightBuffer, b: &GpuWeightBuffer, ) -> Result<GpuWeightBuffer>
Element-wise minimum of two weight buffers
Sourcepub fn elementwise_add_gpu(
&self,
a: &GpuWeightBuffer,
b: &GpuWeightBuffer,
) -> Result<GpuWeightBuffer>
pub fn elementwise_add_gpu( &self, a: &GpuWeightBuffer, b: &GpuWeightBuffer, ) -> Result<GpuWeightBuffer>
Element-wise addition of two weight buffers using GPU
Sourcepub fn elementwise_add(
&self,
a: &GpuWeightBuffer,
b: &GpuWeightBuffer,
) -> Result<GpuWeightBuffer>
pub fn elementwise_add( &self, a: &GpuWeightBuffer, b: &GpuWeightBuffer, ) -> Result<GpuWeightBuffer>
Element-wise addition of two weight buffers (for tropical multiplication)
Sourcepub fn elementwise_mul_gpu(
&self,
a: &GpuWeightBuffer,
b: &GpuWeightBuffer,
) -> Result<GpuWeightBuffer>
pub fn elementwise_mul_gpu( &self, a: &GpuWeightBuffer, b: &GpuWeightBuffer, ) -> Result<GpuWeightBuffer>
Element-wise multiplication of two weight buffers using GPU
Sourcepub fn elementwise_mul(
&self,
a: &GpuWeightBuffer,
b: &GpuWeightBuffer,
) -> Result<GpuWeightBuffer>
pub fn elementwise_mul( &self, a: &GpuWeightBuffer, b: &GpuWeightBuffer, ) -> Result<GpuWeightBuffer>
Element-wise multiplication (for probability semiring)
Sourcepub fn log_sum_exp_gpu(&self, weights: &GpuWeightBuffer) -> Result<f32>
pub fn log_sum_exp_gpu(&self, weights: &GpuWeightBuffer) -> Result<f32>
Compute log-sum-exp for log semiring using GPU
This is numerically stable using the max-subtract trick.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for GpuContext
impl !UnwindSafe for GpuContext
impl Freeze for GpuContext
impl Send for GpuContext
impl Sync for GpuContext
impl Unpin for GpuContext
impl UnsafeUnpin for GpuContext
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.