Skip to content

Commit f016d39

Browse files
fix: add SAFETY message to kernel and remove unnecessary imports
1 parent 00135f5 commit f016d39

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

samples/introduction/async_api/kernels/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use cuda_std::prelude::*;
22

33
#[kernel]
4+
/// # Safety
5+
///
6+
/// The user must ensure that the number of (threads * blocks * grids)
7+
/// must not be greater than the number of elements in `g_data`.
48
pub unsafe fn increment(g_data: *mut u32, inc_value: u32) {
59
// This can also be obtained directly as
610
//

samples/introduction/async_api/src/main.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use cust::context::Context;
21
use cust::device::Device;
32
use cust::event::{Event, EventFlags};
43
use cust::function::{BlockSize, GridSize};
4+
use cust::launch;
55
use cust::memory::{AsyncCopyDestination, DeviceBuffer, LockedBuffer};
66
use cust::module::Module;
77
use cust::prelude::EventStatus;
88
use cust::stream::{Stream, StreamFlags};
9-
use cust::{CudaFlags, launch};
109
use std::time::Instant;
1110

1211
static PTX: &str = include_str!(concat!(env!("OUT_DIR"), "/kernels.ptx"));
@@ -54,7 +53,9 @@ fn main() -> Result<(), cust::error::CudaError> {
5453
.expect("Failed to record start_event in the CUDA stream!");
5554
let start = Instant::now();
5655

57-
// SAFETY: until the stop_event is triggered:
56+
// # Safety
57+
//
58+
// Until the stop_event is triggered:
5859
// 1. `host_a` is not being modified
5960
// 2. Both `device_a` and `host_a` are not deallocated
6061
// 3. Until `stop_query` yields `EventStatus::Ready`, `device_a` is not involved in any other operation
@@ -65,7 +66,9 @@ fn main() -> Result<(), cust::error::CudaError> {
6566
.expect("Could not copy from host to device!");
6667
}
6768

68-
// SAFETY: number of threads * number of blocks = total number of elements.
69+
// # Safety
70+
//
71+
// Number of threads * number of blocks = total number of elements.
6972
// Hence there will not be any out-of-bounds issues.
7073
unsafe {
7174
let result = launch!(increment<<<grids, blocks, 0, stream>>>(
@@ -75,7 +78,9 @@ fn main() -> Result<(), cust::error::CudaError> {
7578
result.expect("Result of `increment` kernel did not process!");
7679
}
7780

78-
// SAFETY: until the stop_event is triggered:
81+
// # Safety
82+
//
83+
// Until the stop_event is triggered:
7984
// 1. `device_a` is not being modified
8085
// 2. Both `device_a` and `host_a` are not deallocated
8186
// 3. At this point, until `stop_query` yields `EventStatus::Ready`,

0 commit comments

Comments
 (0)