MCPcopy Index your code
hub / github.com/arrayfire/arrayfire-rust / main

Function main

cuda-interop/examples/cuda_af_app.rs:4–60  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2use rustacuda::prelude::*;
3
4fn main() {
5 // MAKE SURE to do all rustacuda initilization before arrayfire API's
6 // first call. It seems like some CUDA context state is getting messed up
7 // if we mix CUDA context init(device, context, module, stream) with ArrayFire API
8 match rustacuda::init(CudaFlags::empty()) {
9 Ok(()) => {}
10 Err(e) => panic!("rustacuda init failure: {:?}", e),
11 }
12 let device = match Device::get_device(0) {
13 Ok(d) => d,
14 Err(e) => panic!("Failed to get device: {:?}", e),
15 };
16 let _context =
17 match Context::create_and_push(ContextFlags::MAP_HOST | ContextFlags::SCHED_AUTO, device) {
18 Ok(c) => c,
19 Err(e) => panic!("Failed to create context: {:?}", e),
20 };
21 let stream = match Stream::new(StreamFlags::NON_BLOCKING, None) {
22 Ok(s) => s,
23 Err(e) => panic!("Failed to create stream: {:?}", e),
24 };
25
26 let mut in_x = DeviceBuffer::from_slice(&[1.0f32; 10]).unwrap();
27 let mut in_y = DeviceBuffer::from_slice(&[2.0f32; 10]).unwrap();
28
29 // wait for any prior kernels to finish before passing
30 // the device pointers to ArrayFire
31 match stream.synchronize() {
32 Ok(()) => {}
33 Err(e) => panic!("Stream sync failure: {:?}", e),
34 };
35
36 set_device(0);
37 info();
38
39 let x = Array::new_from_device_ptr(in_x.as_device_ptr().as_raw_mut(), dim4!(10));
40 let y = Array::new_from_device_ptr(in_y.as_device_ptr().as_raw_mut(), dim4!(10));
41
42 // Lock so that ArrayFire doesn't free pointers from RustaCUDA
43 // But we have to make sure these pointers stay in valid scope
44 // as long as the associated ArrayFire Array objects are valid
45 x.lock();
46 y.lock();
47
48 af_print!("x", x);
49 af_print!("y", y);
50
51 let o = x + y;
52 af_print!("out", o);
53
54 let _o_dptr = unsafe { o.device_ptr() }; // Calls an implicit lock
55
56 // User has to call unlock if they want to relenquish control to ArrayFire
57
58 // Once the non-arrayfire operations are done, call unlock.
59 o.unlock(); // After this, there is no guarantee that value of o_dptr is valid
60}

Callers

nothing calls this directly

Calls 7

initFunction · 0.85
get_deviceFunction · 0.85
set_deviceFunction · 0.85
infoFunction · 0.85
lockMethod · 0.80
device_ptrMethod · 0.80
unlockMethod · 0.80

Tested by

no test coverage detected