open_cl_low_level/
ll_testing.rs

1use crate::{
2    list_devices_by_type, list_platforms, ClCommandQueue, ClContext, ClDeviceID, ClKernel, ClMem,
3    ClNumber, ClProgram, DeviceType, MemConfig,
4};
5use std::{thread, time};
6
7pub fn sleep(ms: u64) {
8    println!("Sleeping for {:?}ms", ms);
9    let dur = time::Duration::from_millis(ms);
10    thread::sleep(dur);
11}
12
13pub fn get_kernel(
14    src: &str,
15    kernel_name: &str,
16) -> (ClContext, Vec<ClDeviceID>, ClProgram, ClKernel) {
17    let (program, devices, context) = get_program(src);
18    let kernel: ClKernel = unsafe { ClKernel::create(&program, kernel_name).unwrap() };
19    (context, devices, program, kernel)
20}
21
22pub fn get_mem<T: ClNumber>(size: usize) -> (Vec<ClDeviceID>, ClContext, ClMem) {
23    let (context, devices) = get_context();
24    let mem_config = MemConfig::default();
25    let ll_mem = unsafe { ClMem::create_with_config::<T, usize>(&context, size, mem_config).unwrap() };
26    (devices, context, ll_mem)
27}
28
29pub fn mem_from_data_and_context<T: ClNumber>(data: &[T], context: &ClContext) -> ClMem {
30    unsafe { ClMem::create_with_config(context, data, MemConfig::for_data()).unwrap() }
31}
32
33pub fn get_program(src: &str) -> (ClProgram, Vec<ClDeviceID>, ClContext) {
34    let devices = list_devices();
35    let context = context_from_devices(&devices[..]);
36    let mut program = unsafe { ClProgram::create_with_source(&context, src).unwrap() };
37    program.build(&devices[..]).unwrap();
38    (program, devices, context)
39}
40
41pub fn get_context() -> (ClContext, Vec<ClDeviceID>) {
42    let devices = list_devices();
43    (context_from_devices(&devices[..]), devices)
44}
45
46pub fn get_command_queues() -> (Vec<ClCommandQueue>, ClContext, Vec<ClDeviceID>) {
47    let (context, devices) = get_context();
48    let cqs = devices
49        .iter()
50        .map(|device| unsafe { ClCommandQueue::create(&context, device, None).unwrap() })
51        .collect();
52
53    (cqs, context, devices)
54}
55
56pub fn context_from_devices(devices: &[ClDeviceID]) -> ClContext {
57    unsafe { ClContext::create(&devices[..]).unwrap() }
58}
59
60pub fn list_devices() -> Vec<ClDeviceID> {
61    let platforms = list_platforms().expect("Failed to list_platforms");
62    let mut devices = Vec::new();
63    for platform in platforms.iter() {
64        let p_devices =
65            list_devices_by_type(&platform, DeviceType::ALL).expect("Failed to list all devices");
66        devices.extend(p_devices);
67    }
68    if devices.is_empty() {
69        panic!("Failed to list_devices: devices was an empty vec");
70    }
71    devices
72}
73
74pub fn with_each_device<F>(f: F)
75where
76    F: Fn(&ClDeviceID),
77{
78    let devices = list_devices();
79    for d in devices.iter() {
80        f(d)
81    }
82}
83
84#[macro_export]
85macro_rules! expect_method {
86    ($left:expr, $method:ident, $expected:expr) => {
87        let left = &$left;
88        let right = &$expected;
89        if !left.$method(right) {
90            panic!(
91                "
92            Assertion Failed!!!
93                failed expression: {}.{}({})
94                object: {:?}
95                arg: {:?}
96            ",
97                stringify!($left),
98                stringify!($method),
99                stringify!($expected),
100                left,
101                right,
102            )
103        }
104    };
105}