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

Function main

opencl-interop/examples/custom_kernel.rs:9–69  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

7use std::ffi::CString;
8
9fn main() {
10 af::info();
11 let dims = af::dim4!(8);
12 let af_buffer = af::constant(0f32, dims.clone());
13 af::af_print!("af_buffer", af_buffer);
14
15 let src = r#"
16 __kernel void add(__global float* buffer, float scalar) {
17 buffer[get_global_id(0)] += scalar;
18 }
19 "#;
20
21 let af_did = afcl::get_device_id();
22 let af_ctx = afcl::get_context(false);
23 let af_que = afcl::get_queue(false);
24
25 let _devid = unsafe { ocl_core::DeviceId::from_raw(af_did) };
26 let contx = unsafe { ocl_core::Context::from_raw_copied_ptr(af_ctx) };
27 let queue = unsafe { ocl_core::CommandQueue::from_raw_copied_ptr(af_que) };
28
29 // Define which platform and device(s) to use. Create a context,
30 // queue, and program then define some dims..
31 let src_cstring = CString::new(src).unwrap();
32 let program = ocl_core::create_program_with_source(&contx, &[src_cstring]).unwrap();
33 ocl_core::build_program(
34 &program,
35 None::<&[()]>,
36 &CString::new("").unwrap(),
37 None,
38 None,
39 )
40 .unwrap();
41
42 // Fetch cl_mem from ArrayFire Array
43 let ptr = unsafe { af_buffer.device_ptr() };
44 let buffer = unsafe { ocl_core::Mem::from_raw_copied_ptr(ptr) };
45
46 // Create a kernel with arguments matching those in the source above:
47 let kernel = ocl_core::create_kernel(&program, "add").unwrap();
48 ocl_core::set_kernel_arg(&kernel, 0, ArgVal::mem(&buffer)).unwrap();
49 ocl_core::set_kernel_arg(&kernel, 1, ArgVal::scalar(&10.0f32)).unwrap();
50
51 let ocl_dims: [usize; 3] = [dims[0] as usize, dims[1] as usize, dims[2] as usize];
52 unsafe {
53 ocl_core::enqueue_kernel(
54 &queue,
55 &kernel,
56 1,
57 None,
58 &ocl_dims,
59 None,
60 None::<Event>,
61 None::<&mut Event>,
62 )
63 .unwrap();
64 }
65 ocl_core::finish(&queue).unwrap();
66 af_buffer.unlock(); //Give back control of cl_mem to ArrayFire memory manager

Callers

nothing calls this directly

Calls 8

infoFunction · 0.85
constantFunction · 0.85
get_device_idFunction · 0.85
get_contextFunction · 0.85
get_queueFunction · 0.85
device_ptrMethod · 0.80
unlockMethod · 0.80
cloneMethod · 0.45

Tested by

no test coverage detected