Skip to content

Commit 345b6d9

Browse files
authored
refactor(cuda): remove unused driver API (#10879)
## Summary Removes unused APIs from the CUDA driver wrapper and simplifies stream ownership. None of the removed APIs has remaining callers in the workspace; behavior for existing callers is unchanged. Removing the unused stream-priority bindings also reduces the symbols required when loading libcuda. The driver loader resolves every declared symbol up front and fails if any is missing. ## Changes - Remove unused context, stream, memory-view, and launch-config helpers. - Remove the cuStreamCreateWithPriority and cuCtxGetStreamPriorityRange bindings and their unused wrappers. - Keep stream creation on the existing non-blocking cuStreamCreate path. - Remove the unused borrowed-stream constructor. Every remaining CudaStream owns its handle, so remove the ownership flag and conditional destruction. - Retain the view slicing, device-pointer access tracking, and event-wait primitives used by existing callers. Commits: - refactor(cuda): remove driver API that nothing uses - refactor(cuda): remove unused borrowed stream constructor No builds, tests, or benchmarks were run for this change. --------- Signed-off-by: Alexander Droste <[email protected]>
1 parent 2c3c510 commit 345b6d9

5 files changed

Lines changed: 9 additions & 135 deletions

File tree

‎vortex-cuda/driver/src/context.rs‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -299,21 +299,7 @@ impl CudaContext {
299299

300300
/// A new non-blocking stream.
301301
pub fn new_stream(self: &Arc<Self>) -> Result<Arc<CudaStream>, DriverError> {
302-
CudaStream::create(self, None)
303-
}
304-
305-
/// A new non-blocking stream with `priority`; lower values are higher priorities.
306-
pub fn new_stream_with_priority(
307-
self: &Arc<Self>,
308-
priority: i32,
309-
) -> Result<Arc<CudaStream>, DriverError> {
310-
CudaStream::create(self, Some(priority))
311-
}
312-
313-
/// The legacy default stream.
314-
pub fn default_stream(self: &Arc<Self>) -> Arc<CudaStream> {
315-
// SAFETY: The null stream is valid for the context's lifetime.
316-
unsafe { CudaStream::borrowed(self, std::ptr::null_mut()) }
302+
CudaStream::create(self)
317303
}
318304

319305
/// A new event with `flags`, by default timing-disabled.

‎vortex-cuda/driver/src/launch.rs‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,6 @@ pub struct LaunchConfig {
2929
pub shared_mem_bytes: u32,
3030
}
3131

32-
impl LaunchConfig {
33-
/// One-dimensional blocks of 1024 threads covering `n` elements.
34-
pub fn for_num_elems(n: u32) -> Self {
35-
Self {
36-
grid_dim: (n.div_ceil(1024), 1, 1),
37-
block_dim: (1024, 1, 1),
38-
shared_mem_bytes: 0,
39-
}
40-
}
41-
}
42-
4332
/// A kernel launch being assembled on a stream; create with [`CudaStream::launch_builder`].
4433
///
4534
/// Device memory arguments are ordered after earlier conflicting accesses and recorded as

‎vortex-cuda/driver/src/memory.rs‎

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -561,11 +561,6 @@ impl<T> CudaSlice<T> {
561561
self.as_view().slice(bounds)
562562
}
563563

564-
/// A view of the elements in `bounds`, if in range.
565-
pub fn try_slice(&self, bounds: impl RangeBounds<usize>) -> Option<CudaView<'_, T>> {
566-
self.as_view().try_slice(bounds)
567-
}
568-
569564
/// A mutable view of the elements in `bounds`.
570565
///
571566
/// # Panics
@@ -711,15 +706,6 @@ impl<'a, T> CudaView<'a, T> {
711706
marker: PhantomData,
712707
})
713708
}
714-
715-
/// Like [`DevicePtr::device_ptr`], with the guard living as long as the view.
716-
pub fn view_ptr(
717-
self,
718-
stream: &'a CudaStream,
719-
) -> Result<(sys::CUdeviceptr, SyncOnDrop<'a>), DriverError> {
720-
let guard = access(self.access, self.stream, stream, AccessKind::Read)?;
721-
Ok((self.pointer, guard))
722-
}
723709
}
724710

725711
/// A mutable view of part of a [`CudaSlice`].
@@ -767,17 +753,6 @@ impl<'a, T> CudaViewMut<'a, T> {
767753
}
768754
}
769755

770-
/// Converts into a shared view with the original lifetime.
771-
pub fn into_view(self) -> CudaView<'a, T> {
772-
CudaView {
773-
pointer: self.pointer,
774-
len: self.len,
775-
access: self.access,
776-
stream: self.stream,
777-
marker: PhantomData,
778-
}
779-
}
780-
781756
/// A shared view of the elements in `bounds`.
782757
///
783758
/// # Panics
@@ -821,15 +796,6 @@ impl<'a, T> CudaViewMut<'a, T> {
821796
marker: PhantomData,
822797
})
823798
}
824-
825-
/// Like [`DevicePtrMut::device_ptr_mut`], with the guard living as long as the view.
826-
pub fn view_ptr_mut(
827-
self,
828-
stream: &'a CudaStream,
829-
) -> Result<(sys::CUdeviceptr, SyncOnDrop<'a>), DriverError> {
830-
let guard = access(self.access, self.stream, stream, AccessKind::Write)?;
831-
Ok((self.pointer, guard))
832-
}
833799
}
834800

835801
/// Orders `stream` after earlier conflicting accesses and returns the guard recording this one.

‎vortex-cuda/driver/src/stream.rs‎

Lines changed: 8 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ pub struct CudaStream {
2727
id: StreamId,
2828
stream: sys::CUstream,
2929
context: Arc<CudaContext>,
30-
owned: bool,
3130
}
3231

3332
// SAFETY: Stream handles are usable from any thread.
@@ -54,59 +53,29 @@ impl Eq for CudaStream {}
5453

5554
impl Drop for CudaStream {
5655
fn drop(&mut self) {
57-
if self.owned {
58-
if let Err(error) = self.bind() {
59-
log_error("binding context to destroy stream", error);
60-
}
61-
// SAFETY: Owned streams are destroyed exactly once; queued work still completes.
62-
if let Err(error) = unsafe { sys::cuStreamDestroy_v2(self.stream) }.result() {
63-
log_error("destroying CUDA stream", error);
64-
}
56+
if let Err(error) = self.bind() {
57+
log_error("binding context to destroy stream", error);
58+
}
59+
// SAFETY: Owned streams are destroyed exactly once; queued work still completes.
60+
if let Err(error) = unsafe { sys::cuStreamDestroy_v2(self.stream) }.result() {
61+
log_error("destroying CUDA stream", error);
6562
}
6663
}
6764
}
6865

6966
impl CudaStream {
70-
pub(crate) fn create(
71-
context: &Arc<CudaContext>,
72-
priority: Option<i32>,
73-
) -> Result<Arc<Self>, DriverError> {
67+
pub(crate) fn create(context: &Arc<CudaContext>) -> Result<Arc<Self>, DriverError> {
7468
context.bind_to_thread()?;
7569
let mut stream = std::ptr::null_mut();
7670
// SAFETY: The context is current; the driver writes the new stream.
77-
unsafe {
78-
match priority {
79-
Some(priority) => sys::cuStreamCreateWithPriority(
80-
&raw mut stream,
81-
sys::CU_STREAM_NON_BLOCKING,
82-
priority,
83-
),
84-
None => sys::cuStreamCreate(&raw mut stream, sys::CU_STREAM_NON_BLOCKING),
85-
}
86-
}
87-
.result()?;
71+
unsafe { sys::cuStreamCreate(&raw mut stream, sys::CU_STREAM_NON_BLOCKING) }.result()?;
8872
Ok(Arc::new(Self {
8973
id: NEXT_STREAM_ID.fetch_add(1, Ordering::Relaxed),
9074
stream,
9175
context: Arc::clone(context),
92-
owned: true,
9376
}))
9477
}
9578

96-
/// Wraps a stream owned by someone else, which is not destroyed on drop.
97-
///
98-
/// # Safety
99-
///
100-
/// `stream` must belong to `context` and outlive the returned wrapper.
101-
pub unsafe fn borrowed(context: &Arc<CudaContext>, stream: sys::CUstream) -> Arc<Self> {
102-
Arc::new(Self {
103-
id: NEXT_STREAM_ID.fetch_add(1, Ordering::Relaxed),
104-
stream,
105-
context: Arc::clone(context),
106-
owned: false,
107-
})
108-
}
109-
11079
pub(crate) fn id(&self) -> StreamId {
11180
self.id
11281
}
@@ -148,18 +117,6 @@ impl CudaStream {
148117
self.wait_raw(event.cu_event())
149118
}
150119

151-
/// Makes later work wait for the work queued on `other` so far.
152-
pub fn join(&self, other: &CudaStream) -> Result<(), DriverError> {
153-
self.wait(&other.record_event(None)?)
154-
}
155-
156-
/// A new stream whose work follows the work queued here so far.
157-
pub fn fork(&self) -> Result<Arc<Self>, DriverError> {
158-
let stream = Self::create(&self.context, None)?;
159-
stream.join(self)?;
160-
Ok(stream)
161-
}
162-
163120
pub(crate) fn wait_raw(&self, event: sys::CUevent) -> Result<(), DriverError> {
164121
// SAFETY: Both handles belong to this stream's context and are valid.
165122
unsafe {
@@ -232,28 +189,6 @@ impl CudaStream {
232189
}
233190
}
234191

235-
/// The (least, greatest) stream priorities of the current context.
236-
pub fn get_priority_range() -> Result<(i32, i32), DriverError> {
237-
let (mut least, mut greatest) = (0, 0);
238-
// SAFETY: The driver writes both values.
239-
unsafe { sys::cuCtxGetStreamPriorityRange(&raw mut least, &raw mut greatest) }.result()?;
240-
Ok((least, greatest))
241-
}
242-
243-
/// Makes `stream` wait for the work recorded in `event`.
244-
///
245-
/// # Safety
246-
///
247-
/// Both handles must be valid and belong to the current context.
248-
pub unsafe fn wait_event(
249-
stream: sys::CUstream,
250-
event: sys::CUevent,
251-
flags: sys::CUevent_wait_flags,
252-
) -> Result<(), DriverError> {
253-
// SAFETY: Under the caller's guarantees.
254-
unsafe { sys::cuStreamWaitEvent(stream, event, flags.0) }.result()
255-
}
256-
257192
/// Enqueues a host function on `stream`.
258193
///
259194
/// # Safety

‎vortex-cuda/driver/src/sys.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,8 @@ driver_api! {
244244
fn cuCtxGetCurrent(context: *mut CUcontext);
245245
fn cuCtxSetCurrent(context: CUcontext);
246246
fn cuCtxSynchronize();
247-
fn cuCtxGetStreamPriorityRange(least: *mut c_int, greatest: *mut c_int);
248247
fn cuMemGetInfo_v2(free: *mut usize, total: *mut usize);
249248
fn cuStreamCreate(stream: *mut CUstream, flags: c_uint);
250-
fn cuStreamCreateWithPriority(stream: *mut CUstream, flags: c_uint, priority: c_int);
251249
fn cuStreamDestroy_v2(stream: CUstream);
252250
fn cuStreamSynchronize(stream: CUstream);
253251
fn cuStreamWaitEvent(stream: CUstream, event: CUevent, flags: c_uint);

0 commit comments

Comments
 (0)