pub struct GpuContext { /* private fields */ }Expand description
GPU context for managing GPU resources and operations
Implementations§
Source§impl GpuContext
impl GpuContext
Sourcepub fn new(backend: GpuBackend) -> Result<GpuContext, GpuError>
pub fn new(backend: GpuBackend) -> Result<GpuContext, GpuError>
Create a new GPU context with the specified backend
Sourcepub fn backend(&self) -> GpuBackend
pub fn backend(&self) -> GpuBackend
Get the backend type
Sourcepub fn backend_name(&self) -> &str
pub fn backend_name(&self) -> &str
Get the backend name
Sourcepub fn create_buffer<T>(&self, size: usize) -> GpuBuffer<T>where
T: GpuDataType,
pub fn create_buffer<T>(&self, size: usize) -> GpuBuffer<T>where
T: GpuDataType,
Create a buffer with the given size
Sourcepub fn create_buffer_from_slice<T>(&self, data: &[T]) -> GpuBuffer<T>where
T: GpuDataType,
pub fn create_buffer_from_slice<T>(&self, data: &[T]) -> GpuBuffer<T>where
T: GpuDataType,
Create a buffer from a slice
Sourcepub fn execute<F, R>(&self, f: F) -> Rwhere
F: FnOnce(&GpuCompiler) -> R,
pub fn execute<F, R>(&self, f: F) -> Rwhere
F: FnOnce(&GpuCompiler) -> R,
Execute a function with a compiler
Sourcepub fn get_kernel(&self, name: &str) -> Result<GpuKernelHandle, GpuError>
pub fn get_kernel(&self, name: &str) -> Result<GpuKernelHandle, GpuError>
Get a kernel from the registry
Sourcepub fn get_specialized_kernel(
&self,
name: &str,
params: &KernelParams,
) -> Result<GpuKernelHandle, GpuError>
pub fn get_specialized_kernel( &self, name: &str, params: &KernelParams, ) -> Result<GpuKernelHandle, GpuError>
Get a specialized kernel from the registry
Sourcepub fn get_available_memory(&self) -> Option<usize>
pub fn get_available_memory(&self) -> Option<usize>
Get available memory on the device
Sourcepub fn get_total_memory(&self) -> Option<usize>
pub fn get_total_memory(&self) -> Option<usize>
Get total memory on the device
Sourcepub fn launch_kernel(
&self,
kernel_name: &str,
grid_size: (usize, usize, usize),
block_size: (usize, usize, usize),
args: &[DynamicKernelArg],
) -> Result<(), GpuError>
pub fn launch_kernel( &self, kernel_name: &str, grid_size: (usize, usize, usize), block_size: (usize, usize, usize), args: &[DynamicKernelArg], ) -> Result<(), GpuError>
Launch a kernel with the given parameters
Sourcepub fn transfer_async_host_to_device<T>(
&self,
ptr: &GpuPtr<T>,
data: &[T],
) -> Result<(), GpuError>where
T: GpuDataType,
pub fn transfer_async_host_to_device<T>(
&self,
ptr: &GpuPtr<T>,
data: &[T],
) -> Result<(), GpuError>where
T: GpuDataType,
Transfer data from host to device asynchronously
Sourcepub fn transfer_host_to_device<T>(
&self,
ptr: &GpuPtr<T>,
data: &[T],
) -> Result<(), GpuError>where
T: GpuDataType,
pub fn transfer_host_to_device<T>(
&self,
ptr: &GpuPtr<T>,
data: &[T],
) -> Result<(), GpuError>where
T: GpuDataType,
Transfer data from host to device synchronously
Sourcepub fn transfer_async_device_to_host<T>(
&self,
ptr: &GpuPtr<T>,
data: &mut [T],
) -> Result<(), GpuError>where
T: GpuDataType,
pub fn transfer_async_device_to_host<T>(
&self,
ptr: &GpuPtr<T>,
data: &mut [T],
) -> Result<(), GpuError>where
T: GpuDataType,
Transfer data from device to host asynchronously
Sourcepub fn transfer_device_to_host<T>(
&self,
ptr: &GpuPtr<T>,
data: &mut [T],
) -> Result<(), GpuError>where
T: GpuDataType,
pub fn transfer_device_to_host<T>(
&self,
ptr: &GpuPtr<T>,
data: &mut [T],
) -> Result<(), GpuError>where
T: GpuDataType,
Transfer data from device to host synchronously
Sourcepub fn execute_kernel(
&self,
source: &str,
buffers: &[GpuBuffer<f32>],
work_groups: (u32, u32, u32),
int_params: &[u32],
float_params: &[f32],
) -> Result<(), GpuError>
pub fn execute_kernel( &self, source: &str, buffers: &[GpuBuffer<f32>], work_groups: (u32, u32, u32), int_params: &[u32], float_params: &[f32], ) -> Result<(), GpuError>
Execute a kernel with dynamic compilation and parameter passing This method is expected by scirs2-vision for GPU operations
Sourcepub fn read_buffer<T>(&self, buffer: &GpuBuffer<T>) -> Result<Vec<T>, GpuError>where
T: GpuDataType,
pub fn read_buffer<T>(&self, buffer: &GpuBuffer<T>) -> Result<Vec<T>, GpuError>where
T: GpuDataType,
Read data from a GPU buffer This method is expected by scirs2-vision for reading GPU results
Sourcepub fn sum_all<T>(
&self,
buffer: &GpuBuffer<T>,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn sum_all<T>(
&self,
buffer: &GpuBuffer<T>,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
Global sum reduction
Sourcepub fn mean_all<T>(
&self,
buffer: &GpuBuffer<T>,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn mean_all<T>(
&self,
buffer: &GpuBuffer<T>,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
Global mean reduction
Sourcepub fn max_all<T>(
&self,
buffer: &GpuBuffer<T>,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn max_all<T>(
&self,
buffer: &GpuBuffer<T>,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
Global max reduction
Sourcepub fn min_all<T>(
&self,
buffer: &GpuBuffer<T>,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn min_all<T>(
&self,
buffer: &GpuBuffer<T>,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
Global min reduction
Sourcepub fn sum_axis<T>(
&self,
buffer: &GpuBuffer<T>,
shape: &[usize],
axis: usize,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn sum_axis<T>(
&self,
buffer: &GpuBuffer<T>,
shape: &[usize],
axis: usize,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
Sum reduction along an axis
Sourcepub fn mean_axis<T>(
&self,
buffer: &GpuBuffer<T>,
shape: &[usize],
axis: usize,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn mean_axis<T>(
&self,
buffer: &GpuBuffer<T>,
shape: &[usize],
axis: usize,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
Mean reduction along an axis
Sourcepub fn max_axis<T>(
&self,
buffer: &GpuBuffer<T>,
shape: &[usize],
axis: usize,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn max_axis<T>(
&self,
buffer: &GpuBuffer<T>,
shape: &[usize],
axis: usize,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
Max reduction along an axis
Sourcepub fn min_axis<T>(
&self,
buffer: &GpuBuffer<T>,
shape: &[usize],
axis: usize,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn min_axis<T>(
&self,
buffer: &GpuBuffer<T>,
shape: &[usize],
axis: usize,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
Min reduction along an axis
Sourcepub fn broadcast<T>(
&self,
buffer: &GpuBuffer<T>,
from_shape: &[usize],
to_shape: &[usize],
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn broadcast<T>(
&self,
buffer: &GpuBuffer<T>,
from_shape: &[usize],
to_shape: &[usize],
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
Broadcast a buffer to a different shape
Sourcepub fn scale<T>(
&self,
buffer: &GpuBuffer<T>,
scalar: T,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn scale<T>(
&self,
buffer: &GpuBuffer<T>,
scalar: T,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
Scale a buffer by a scalar value
Sourcepub fn gemm<T>(
&self,
a: &GpuBuffer<T>,
b: &GpuBuffer<T>,
m: usize,
k: usize,
n: usize,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn gemm<T>(
&self,
a: &GpuBuffer<T>,
b: &GpuBuffer<T>,
m: usize,
k: usize,
n: usize,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
General matrix multiplication: C = A @ B
Sourcepub fn gemm_transpose_b<T>(
&self,
a: &GpuBuffer<T>,
b: &GpuBuffer<T>,
m: usize,
k: usize,
n: usize,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn gemm_transpose_b<T>(
&self,
a: &GpuBuffer<T>,
b: &GpuBuffer<T>,
m: usize,
k: usize,
n: usize,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
GEMM with transposed B: C = A @ B^T
Sourcepub fn gemm_transpose_a<T>(
&self,
a: &GpuBuffer<T>,
b: &GpuBuffer<T>,
m: usize,
k: usize,
n: usize,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn gemm_transpose_a<T>(
&self,
a: &GpuBuffer<T>,
b: &GpuBuffer<T>,
m: usize,
k: usize,
n: usize,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
GEMM with transposed A: C = A^T @ B
Sourcepub fn relu<T>(&self, input: &GpuBuffer<T>) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn relu<T>(&self, input: &GpuBuffer<T>) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
ReLU activation forward pass
Sourcepub fn relu_backward<T>(
&self,
grad_output: &GpuBuffer<T>,
input: &GpuBuffer<T>,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn relu_backward<T>(
&self,
grad_output: &GpuBuffer<T>,
input: &GpuBuffer<T>,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
ReLU backward pass
Sourcepub fn sigmoid<T>(&self, input: &GpuBuffer<T>) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn sigmoid<T>(&self, input: &GpuBuffer<T>) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
Sigmoid activation forward pass
Sourcepub fn sigmoid_backward<T>(
&self,
grad_output: &GpuBuffer<T>,
input: &GpuBuffer<T>,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn sigmoid_backward<T>(
&self,
grad_output: &GpuBuffer<T>,
input: &GpuBuffer<T>,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
Sigmoid backward pass
Sourcepub fn tanh<T>(&self, input: &GpuBuffer<T>) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn tanh<T>(&self, input: &GpuBuffer<T>) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
Tanh activation forward pass
Sourcepub fn tanh_backward<T>(
&self,
grad_output: &GpuBuffer<T>,
input: &GpuBuffer<T>,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn tanh_backward<T>(
&self,
grad_output: &GpuBuffer<T>,
input: &GpuBuffer<T>,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
Tanh backward pass
Sourcepub fn gelu<T>(&self, input: &GpuBuffer<T>) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn gelu<T>(&self, input: &GpuBuffer<T>) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
GELU activation forward pass
Sourcepub fn gelu_backward<T>(
&self,
grad_output: &GpuBuffer<T>,
input: &GpuBuffer<T>,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
pub fn gelu_backward<T>(
&self,
grad_output: &GpuBuffer<T>,
input: &GpuBuffer<T>,
) -> Result<GpuBuffer<T>, GpuError>where
T: GpuDataType,
GELU backward pass
Trait Implementations§
Auto Trait Implementations§
impl Freeze for GpuContext
impl !RefUnwindSafe for GpuContext
impl Send for GpuContext
impl Sync for GpuContext
impl Unpin for GpuContext
impl UnsafeUnpin for GpuContext
impl !UnwindSafe for GpuContext
Blanket Implementations§
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 more