pub struct GpuSession { /* private fields */ }Expand description
Persistent GPU session backed by a lease’s CUDA context.
GpuSession provides persistent device memory and loaded modules for
multi-kernel workloads. The session is tied to the GpuLease and
all resources are freed when the lease expires.
Implementations§
Source§impl GpuSession
impl GpuSession
Sourcepub fn new(lease: &GpuLease) -> Self
pub fn new(lease: &GpuLease) -> Self
Create a session handle from a GPU lease.
§Canonical lifecycle example
This doctest exercises the full session lifecycle against the
host mock so it runs on every cargo test build, with no GPU
hardware required.
use grafos_std::gpu::{GpuBuilder, GpuSession, KernelArgs};
// 1. Acquire a GPU lease.
let lease = GpuBuilder::new().min_vram(1024).acquire()?;
// 2. Open a persistent session on the lease.
let mut sess = GpuSession::new(&lease);
// 3. Allocate device memory and write input data.
let buf = sess.mem_alloc(1024)?;
sess.mem_write(&buf, 0, &[1u8, 2, 3, 4])?;
// 4. Load a module (PTX/cubin bytes — placeholder under the mock).
let module = sess.module_load(&[0x7f, 0x45, 0x4c, 0x46])?;
// 5. Launch a kernel using the typed argument builder.
let args = KernelArgs::new()
.push_u32(42)
.push_buffer(&buf);
sess.launch_with_args(&module, "vector_add", [256, 1, 1], [64, 1, 1], args)?;
// 6. Synchronize and read results back.
sess.sync()?;
let _out = sess.mem_read(&buf, 0, 4)?;
// 7. `buf` and `module` drop here. Both are RAII-freed (the
// daemon calls `cuMemFree` / `cuModuleUnload` on real
// hardware). The lease itself drops at end of scope.Sourcepub fn mem_alloc(&mut self, size: u64) -> Result<GpuMemHandle>
pub fn mem_alloc(&mut self, size: u64) -> Result<GpuMemHandle>
Allocate device memory.
Sourcepub fn mem_write(
&mut self,
handle: &GpuMemHandle,
offset: u64,
data: &[u8],
) -> Result<()>
pub fn mem_write( &mut self, handle: &GpuMemHandle, offset: u64, data: &[u8], ) -> Result<()>
Write data to device memory.
Sourcepub fn mem_read(
&mut self,
handle: &GpuMemHandle,
offset: u64,
size: u32,
) -> Result<Vec<u8>>
pub fn mem_read( &mut self, handle: &GpuMemHandle, offset: u64, size: u32, ) -> Result<Vec<u8>>
Read data from device memory.
Sourcepub fn mem_free(&mut self, handle: GpuMemHandle) -> Result<()>
pub fn mem_free(&mut self, handle: GpuMemHandle) -> Result<()>
Free a device memory allocation explicitly.
Consumes the handle. After this returns successfully (or even
on error), the handle’s Drop will be a no-op — explicit free
and Drop are both safe; the latter never double-frees.
Sourcepub fn module_load(&mut self, binary: &[u8]) -> Result<GpuModule>
pub fn module_load(&mut self, binary: &[u8]) -> Result<GpuModule>
Load a GPU module (PTX/cubin).
Sourcepub fn module_unload(&mut self, module: GpuModule) -> Result<()>
pub fn module_unload(&mut self, module: GpuModule) -> Result<()>
Unload a GPU module explicitly (Phase 48.16 SDK polish).
Consumes the handle. After this returns successfully (or even
on error), the handle’s Drop will be a no-op — explicit
unload and Drop are both safe; the latter never double-unloads.
Sourcepub fn launch(
&mut self,
module: &GpuModule,
kernel: &str,
grid: [u32; 3],
block: [u32; 3],
args: &[u8],
arg_sizes: &[u32],
) -> Result<()>
pub fn launch( &mut self, module: &GpuModule, kernel: &str, grid: [u32; 3], block: [u32; 3], args: &[u8], arg_sizes: &[u32], ) -> Result<()>
Launch a kernel from a loaded module (async — does not wait).
Sourcepub fn launch_with_args(
&mut self,
module: &GpuModule,
kernel: &str,
grid: [u32; 3],
block: [u32; 3],
args: KernelArgs,
) -> Result<()>
pub fn launch_with_args( &mut self, module: &GpuModule, kernel: &str, grid: [u32; 3], block: [u32; 3], args: KernelArgs, ) -> Result<()>
Launch a kernel using a KernelArgs builder.
Convenience over GpuSession::launch: builds the
(args, arg_sizes) byte/size pair from the typed builder and
forwards to the same underlying hostcall.