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

Function main

opencl-interop/examples/ocl_af_app.rs:7–67  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5use ocl_core::{ContextProperties, Event};
6
7fn main() {
8 // Choose platform & device(s) to use. Create a context, queue,
9 let platform_id = ocl_core::default_platform().unwrap();
10 let device_ids = ocl_core::get_device_ids(&platform_id, None, None).unwrap();
11 let device_id = device_ids[0];
12 let context_properties = ContextProperties::new().platform(platform_id);
13 let context =
14 ocl_core::create_context(Some(&context_properties), &[device_id], None, None).unwrap();
15 let queue = ocl_core::create_command_queue(&context, &device_id, None).unwrap();
16 let dims = [8, 1, 1];
17
18 // Create a `Buffer`:
19 let mut vec = vec![0.0f32; dims[0]];
20 let buffer = unsafe {
21 ocl_core::create_buffer(
22 &context,
23 ocl_core::MEM_READ_WRITE | ocl_core::MEM_COPY_HOST_PTR,
24 dims[0],
25 Some(&vec),
26 )
27 .unwrap()
28 };
29 ocl_core::finish(&queue).unwrap(); //sync up before switching to arrayfire
30
31 // Add custom device, context and associated queue to ArrayFire
32 afcl::add_device_context(device_id.as_raw(), context.as_ptr(), queue.as_ptr());
33 afcl::set_device_context(device_id.as_raw(), context.as_ptr());
34 af::info();
35
36 let mut af_buffer = af::Array::new_from_device_ptr(
37 buffer.as_ptr() as *mut f32,
38 af::Dim4::new(&[dims[0] as u64, 1, 1, 1]),
39 );
40
41 af::af_print!("GPU Buffer before modification:", af_buffer);
42
43 af_buffer = af_buffer + 10f32;
44
45 af::sync(af::get_device());
46 unsafe {
47 let ptr = af_buffer.device_ptr();
48 let obuf = ocl_core::Mem::from_raw_copied_ptr(ptr);
49
50 // Read results from the device into a vector:
51 ocl_core::enqueue_read_buffer(
52 &queue,
53 &obuf,
54 true,
55 0,
56 &mut vec,
57 None::<Event>,
58 None::<&mut Event>,
59 )
60 .unwrap();
61 }
62 println!("GPU buffer on host after ArrayFire operation: {:?}", vec);
63
64 // Remove device from ArrayFire management towards Application Exit

Callers

nothing calls this directly

Calls 8

add_device_contextFunction · 0.85
set_device_contextFunction · 0.85
infoFunction · 0.85
syncFunction · 0.85
get_deviceFunction · 0.85
set_deviceFunction · 0.85
delete_device_contextFunction · 0.85
device_ptrMethod · 0.80

Tested by

no test coverage detected