Skip to content

Commit fd87b73

Browse files
committed
Feat: impl Send + Sync for most cust structs
1 parent c47e0fc commit fd87b73

File tree

14 files changed

+47
-0
lines changed

14 files changed

+47
-0
lines changed

crates/cust/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ any breaking changes, the API is the same.
2828
- Added `drop_async` to `DeviceBox`.
2929
- Added `new_async` to `DeviceBox`.
3030
- `Linker::complete` now only returns the built cubin, and not the cubin and a duration.
31+
- `Stream`, `Module`, `Linker`, `Function`, `Event`, `UnifiedBox`, `ArrayObject`, `LockedBuffer`, `LockedBox`, `DeviceSlice`, `DeviceBuffer`, and `DeviceBox` all now impl `Send` and `Sync`, this makes
32+
it much easier to write multigpu code. The CUDA API is fully thread-safe except for graph objects.
3133

3234
## 0.2.2 - 12/5/21
3335

crates/cust/src/context/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ pub struct Context {
166166
device: cuda::CUdevice,
167167
}
168168

169+
unsafe impl Send for Context {}
170+
unsafe impl Sync for Context {}
171+
169172
impl Clone for Context {
170173
fn clone(&self) -> Self {
171174
// because we already retained a context on this device successfully (self), it is

crates/cust/src/event.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ pub enum EventStatus {
6767
#[derive(Debug)]
6868
pub struct Event(CUevent);
6969

70+
unsafe impl Send for Event {}
71+
unsafe impl Sync for Event {}
72+
7073
impl Event {
7174
/// Create a new event with the specified flags.
7275
///

crates/cust/src/function.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ pub struct Function<'a> {
205205
inner: CUfunction,
206206
module: PhantomData<&'a Module>,
207207
}
208+
209+
unsafe impl Send for Function<'_> {}
210+
unsafe impl Sync for Function<'_> {}
211+
208212
impl<'a> Function<'a> {
209213
pub(crate) fn new(inner: CUfunction, _module: &Module) -> Function {
210214
Function {

crates/cust/src/link.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ pub struct Linker {
1515
raw: cuda::CUlinkState,
1616
}
1717

18+
unsafe impl Send for Linker {}
19+
unsafe impl Sync for Linker {}
20+
1821
impl Linker {
1922
/// Creates a new linker.
2023
pub fn new() -> CudaResult<Self> {

crates/cust/src/memory/array.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ pub struct ArrayObject {
305305
pub(crate) handle: CUarray,
306306
}
307307

308+
unsafe impl Send for ArrayObject {}
309+
unsafe impl Sync for ArrayObject {}
310+
308311
impl ArrayObject {
309312
pub(crate) fn into_raw(self) -> CUarray {
310313
ManuallyDrop::new(self).handle

crates/cust/src/memory/device/device_box.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ use std::os::raw::c_void;
1818
pub struct DeviceBox<T> {
1919
pub(crate) ptr: DevicePointer<T>,
2020
}
21+
22+
unsafe impl<T: Send> Send for DeviceBox<T> {}
23+
unsafe impl<T: Sync> Sync for DeviceBox<T> {}
24+
2125
impl<T: DeviceCopy> DeviceBox<T> {
2226
/// Allocate device memory and place val into it.
2327
///

crates/cust/src/memory/device/device_buffer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ pub struct DeviceBuffer<T> {
1616
buf: DevicePointer<T>,
1717
capacity: usize,
1818
}
19+
20+
unsafe impl<T: Send> Send for DeviceBuffer<T> {}
21+
unsafe impl<T: Sync> Sync for DeviceBuffer<T> {}
22+
1923
impl<T> DeviceBuffer<T> {
2024
/// Allocate a new device buffer large enough to hold `size` `T`'s, but without
2125
/// initializing the contents.

crates/cust/src/memory/device/device_slice.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ use std::slice::{self, Chunks, ChunksMut};
1919
#[repr(C)]
2020
pub struct DeviceSlice<T>([T]);
2121

22+
unsafe impl<T: Send> Send for DeviceSlice<T> {}
23+
unsafe impl<T: Sync> Sync for DeviceSlice<T> {}
24+
2225
impl<T: DeviceCopy + Default + Clone> DeviceSlice<T> {
2326
pub fn as_host_vec(&self) -> CudaResult<Vec<T>> {
2427
let mut vec = vec![T::default(); self.len()];

crates/cust/src/memory/locked/locked_box.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ pub struct LockedBox<T: DeviceCopy> {
3030
pub(crate) ptr: *mut T,
3131
}
3232

33+
unsafe impl<T: Send + DeviceCopy> Send for LockedBox<T> {}
34+
unsafe impl<T: Sync + DeviceCopy> Sync for LockedBox<T> {}
35+
3336
impl<T: DeviceCopy> LockedBox<T> {
3437
/// Creates an uninitialized [`LockedBox`]. The contents must
3538
/// not be read until the box is written to.

0 commit comments

Comments
 (0)