Skip to main content

CudaComputeContext

Struct CudaComputeContext 

Source
pub struct CudaComputeContext {
    pub ctx: Arc<CudaContext>,
    pub stream: Arc<CudaStream>,
    pub prefetch_stream: Option<Arc<CudaStream>>,
    pub module_cache: Mutex<HashMap<u64, (CudaFunction, Arc<CudaModule>, Arc<KernelSpec>)>>,
    pub adapter: AdapterIdentity,
    pub constraints: AdapterConstraints,
    pub allocator: QualiaSlabAllocator,
    pub slab: CudaSlice<u8>,
}

Fields§

§ctx: Arc<CudaContext>§stream: Arc<CudaStream>§prefetch_stream: Option<Arc<CudaStream>>

Secondary stream for overlapping H2D parameter writes with compute. Lazily created on first write_view_prefetch call to avoid overhead when double-buffering is not used.

§module_cache: Mutex<HashMap<u64, (CudaFunction, Arc<CudaModule>, Arc<KernelSpec>)>>

Cache of loaded CUDA functions keyed by (source_hash, entry_point). Avoids redundant load_module JIT on every compile_pipe! call — the PTX text is already cached in NVRTC_PTX_CACHE, but the driver module load is a separate JIT step that was repeated per token.

§adapter: AdapterIdentity§constraints: AdapterConstraints§allocator: QualiaSlabAllocator§slab: CudaSlice<u8>

Implementations§

Source§

impl CudaComputeContext

Source

pub fn new(capacity_bytes: usize) -> Result<Self, ForgeError>

Source

pub fn begin_graph_capture(&self) -> Result<(), ForgeError>

Begin thread-local capture on the prepared compute stream.

Source

pub fn end_graph_capture(&self) -> Result<CapturedCudaGraph, ForgeError>

Finish, instantiate and upload the current compute-stream capture.

Source

pub fn launch_graph(&self, graph: &CapturedCudaGraph) -> Result<(), ForgeError>

Enqueue one replay on the graph’s retained compute stream.

Source

pub fn allocate_and_write( &mut self, data: &[u8], binding: u32, group: u32, ) -> Result<BufferView, ForgeError>

Source

pub fn write_view_prefetch( &mut self, view: &BufferView, data: &[u8], ) -> Result<(), ForgeError>

Overwrite a device view with host bytes on the prefetch stream, overlapping with compute on the primary stream. Caller must invoke [join_prefetch] before launching a kernel that reads this data.

Source

pub fn join_prefetch(&self) -> Result<(), ForgeError>

Make the compute stream wait for all outstanding prefetch-stream work. Call this before launching a kernel that depends on prefetched data.

Source

pub fn write_view( &mut self, view: &BufferView, data: &[u8], ) -> Result<(), ForgeError>

Overwrite an existing device view with host bytes (no new allocation). data.len() must be ≤ view.length_bytes.

Source

pub fn allocate_transient( &mut self, size_bytes: usize, binding: u32, group: u32, ) -> Result<BufferView, ForgeError>

Source

pub fn advance_read_head(&mut self, offset: usize)

Source

pub fn clear_transient_allocations(&mut self)

Source

pub fn write_checkpoint(&self) -> u64

Source

pub fn restore_checkpoint(&mut self, write_count: u64)

Source

pub fn read_buffer_f32(&self, view: &BufferView) -> Result<Vec<f32>, ForgeError>

Source

pub fn read_buffer_u32_into( &self, view: &BufferView, output: &mut [u32], ) -> Result<(), ForgeError>

Copy a device view into a caller-owned u32 slice.

Unlike Self::read_buffer_f32, this performs no host allocation. It is the decode token-readback boundary: the four-byte copy also synchronizes all preceding stream work.

Source

pub fn read_buffer_f64(&self, view: &BufferView) -> Result<Vec<f64>, ForgeError>

Double-precision readback, the f64 mirror of Self::read_buffer_f32 (8 bytes/elem). Used by the native CUDA-f64 GEMM path — WGSL has no f64, so this is CUDA-only by construction.

Trait Implementations§

Source§

impl OracleContext for CudaComputeContext

Available on crate feature cuda only.
Source§

fn run_kernel( &mut self, kernel: &KernelSpec, schedule: &Schedule, buffers: &[BufferView], element_count: usize, warmups: usize, samples: usize, ) -> Result<Vec<u64>, ForgeError>

Compile the kernel’s CUDA-C (NVRTC → PTX, emitted internally by CudaPipeline::compile_cuda_c) and run the warmup + timed-sample dispatch loop. Mirrors the wgpu loop shape so the generic oracle is backend-agnostic; the cross-backend CUDA oracle uses warmups = 0, samples = 1, reproducing the single dispatch the previous evaluate_*_cuda functions performed.

Source§

fn allocate_and_write( &mut self, data: &[u8], binding: u32, group: u32, _usage: BindingUsage, ) -> Result<BufferView, ForgeError>

Allocate a transient slab sub-range and upload data into it. usage selects the backing slab on wgpu (read-only/uniform vs read-write); the CUDA backend addresses one slab and ignores it.
Source§

fn allocate_transient( &mut self, size_bytes: usize, binding: u32, group: u32, _usage: BindingUsage, ) -> Result<BufferView, ForgeError>

Allocate a transient (uninitialised) slab sub-range. usage is honoured by the wgpu backend and ignored by CUDA, as for Self::allocate_and_write.
Source§

fn read_buffer_f32(&self, view: &BufferView) -> Result<Vec<f32>, ForgeError>

Read a device buffer back as f32s.
Source§

fn clear_transient_allocations(&mut self)

Release every transient allocation (resets the ring’s read head to the write head). Only valid once all device work on those ranges has completed.
Source§

fn adapter(&self) -> &AdapterIdentity

The adapter identity (vendor/device/driver) backing this context.
Source§

fn constraints(&self) -> &AdapterConstraints

The adapter’s intrinsic capability/limit constraints.
Source§

fn timestamp_supported(&self) -> bool

Whether GPU-timestamp timing is available (wgpu only; CUDA times on the host wall clock, so this is always false there).

Auto Trait Implementations§

Blanket Implementations§

§

impl<S, A> Aggregate<Result<S, Error>> for A
where A: Aggregate<S>,

§

fn from_shares<T>(iter: T) -> Result<A, Error>
where T: IntoIterator<Item = Result<S, Error>>,

Aggregate shares in an MPC protocol.
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
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSendSync for T
where T: WasmNotSend + WasmNotSync,

§

impl<T> WasmNotSync for T
where T: Sync,