Skip to main content

baracuda_runtime/
ipc.rs

1//! Runtime-API IPC — share events and device allocations between
2//! processes. Linux-primary; Windows returns `NOT_SUPPORTED` or
3//! similar on most paths.
4
5use core::ffi::c_void;
6
7use baracuda_cuda_sys::runtime::{cudaEvent_t, runtime};
8use baracuda_cuda_sys::types::{CUipcEventHandle, CUipcMemHandle};
9
10use crate::error::{check, Result};
11use crate::event::Event;
12
13/// Export a CUDA event for sharing with another process.
14pub fn event_get_handle(event: &Event) -> Result<CUipcEventHandle> {
15    let r = runtime()?;
16    let cu = r.cuda_ipc_get_event_handle()?;
17    let mut h = CUipcEventHandle::default();
18    check(unsafe { cu(&mut h, event.as_raw()) })?;
19    Ok(h)
20}
21
22/// Open a peer-exported event handle into a raw `cudaEvent_t`.
23pub fn event_open_handle(handle: CUipcEventHandle) -> Result<cudaEvent_t> {
24    let r = runtime()?;
25    let cu = r.cuda_ipc_open_event_handle()?;
26    let mut event: cudaEvent_t = core::ptr::null_mut();
27    check(unsafe { cu(&mut event, handle) })?;
28    Ok(event)
29}
30
31/// Export a device allocation's pointer for peer-process import.
32#[allow(clippy::not_unsafe_ptr_arg_deref)]
33pub fn mem_get_handle(dev_ptr: *mut c_void) -> Result<CUipcMemHandle> {
34    let r = runtime()?;
35    let cu = r.cuda_ipc_get_mem_handle()?;
36    let mut h = CUipcMemHandle::default();
37    check(unsafe { cu(&mut h, dev_ptr) })?;
38    Ok(h)
39}
40
41/// Open a peer-exported memory handle. The returned pointer is valid
42/// in the *current* process's default device context.
43pub fn mem_open_handle(handle: CUipcMemHandle, flags: u32) -> Result<*mut c_void> {
44    let r = runtime()?;
45    let cu = r.cuda_ipc_open_mem_handle()?;
46    let mut ptr: *mut c_void = core::ptr::null_mut();
47    check(unsafe { cu(&mut ptr, handle, flags) })?;
48    Ok(ptr)
49}
50
51/// Release a peer-imported memory handle.
52#[allow(clippy::not_unsafe_ptr_arg_deref)]
53pub fn mem_close_handle(ptr: *mut c_void) -> Result<()> {
54    let r = runtime()?;
55    let cu = r.cuda_ipc_close_mem_handle()?;
56    check(unsafe { cu(ptr) })
57}