Skip to content

Commit

Permalink
Add Stream::wait_event
Browse files Browse the repository at this point in the history
  • Loading branch information
LutzCle committed May 9, 2019
1 parent 33ce296 commit e97f87b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,14 @@ impl Event {
}
}

// Get the inner `CUevent` from the `Event`.
//
// Necessary for certain CUDA functions outside of this
// module that expect a bare `CUevent`.
pub(crate) fn as_inner(&self) -> CUevent {
self.0
}

/// Destroy an `Event` returning an error.
///
/// Destroying an event can return errors from previous asynchronous work.
Expand Down
45 changes: 45 additions & 0 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! a stream to be completed.
use crate::error::{CudaResult, DropResult, ToResult};
use crate::event::Event;
use crate::function::{BlockSize, Function, GridSize};
use cuda_sys::cuda::{self, cudaError_t, CUstream};
use std::ffi::c_void;
Expand Down Expand Up @@ -39,6 +40,16 @@ bitflags! {
}
}

bitflags! {
/// Bit flags for configuring a CUDA Stream waiting on an CUDA Event.
///
/// Current versions of CUDA support only the default flag.
pub struct StreamWaitEventFlags: u32 {
/// No flags set.
const DEFAULT = 0x0;
}
}

/// A stream of work for the device to perform.
///
/// See the module-level documentation for more information.
Expand Down Expand Up @@ -211,6 +222,40 @@ impl Stream {
unsafe { cuda::cuStreamSynchronize(self.inner).to_result() }
}

/// Make the stream wait on an event.
///
/// All future work submitted to the stream will wait for the event to
/// complete. Synchronization is performed on the device, if possible. The
/// event may originate from different context or device than the stream.
///
/// # Example:
///
/// ```
/// # use rustacuda::quick_init;
/// # use std::error::Error;
/// # fn main() -> Result<(), Box<dyn Error>> {
/// # let _context = quick_init()?;
/// use rustacuda::stream::{Stream, StreamFlags, StreamWaitEventFlags};
/// use rustacuda::event::{Event, EventFlags};
///
/// let stream_0 = Stream::new(StreamFlags::NON_BLOCKING, None)?;
/// let stream_1 = Stream::new(StreamFlags::NON_BLOCKING, None)?;
/// let event = Event::new(EventFlags::DEFAULT)?;
///
/// // do some work on stream_0 ...
///
/// // record an event
/// event.record(&stream_0)?;
///
/// // wait until the work on stream_0 is finished before continuing stream_1
/// stream_1.wait_event(event)?;
/// # Ok(())
/// }
/// ```
pub fn wait_event(&self, event: Event, flags: StreamWaitEventFlags) -> CudaResult<()> {
unsafe { cuda::cuStreamWaitEvent(self.inner, event.as_inner(), flags.bits()).to_result() }
}

// Hidden implementation detail function. Highly unsafe. Use the `launch!` macro instead.
#[doc(hidden)]
pub unsafe fn launch<G, B>(
Expand Down

0 comments on commit e97f87b

Please sign in to comment.