Skip to content

Commit 328f971

Browse files
author
Jason Ramapuram
committed
add get_available_backends, add display & equality comparison for enum and update example
1 parent 6514984 commit 328f971

File tree

4 files changed

+66
-20
lines changed

4 files changed

+66
-20
lines changed

examples/unified.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,33 @@ fn test_backend(){
2121

2222
#[allow(unused_must_use)]
2323
fn main() {
24-
println!("There are {} available backends", get_backend_count().unwrap());
25-
let err = set_backend(AfBackend::AF_BACKEND_CPU);
26-
match err {
27-
Ok(_) => test_backend(),
28-
Err(e) => println!("CPU backend not available: {}", e),
29-
};
30-
31-
let err = set_backend(AfBackend::AF_BACKEND_CUDA);
32-
match err {
33-
Ok(_) => test_backend(),
34-
Err(e) => println!("CUDA backend not available: {}", e),
35-
};
36-
37-
let err = set_backend(AfBackend::AF_BACKEND_OPENCL);
38-
match err {
39-
Ok(_) => test_backend(),
40-
Err(e) => println!("OpenCL backend not available: {}", e),
41-
};
24+
println!("There are {:?} available backends", get_backend_count().unwrap());
25+
let available = get_available_backends().unwrap();
26+
27+
if available.contains(&AfBackend::AF_BACKEND_CPU){
28+
println!("Evaluating CPU Backend...");
29+
let err = set_backend(AfBackend::AF_BACKEND_CPU);
30+
match err {
31+
Ok(_) => test_backend(),
32+
Err(e) => println!("CPU backend error: {}", e),
33+
};
34+
}
35+
36+
if available.contains(&AfBackend::AF_BACKEND_CUDA){
37+
println!("Evaluating CUDA Backend...");
38+
let err = set_backend(AfBackend::AF_BACKEND_CUDA);
39+
match err {
40+
Ok(_) => test_backend(),
41+
Err(e) => println!("CUDA backend error: {}", e),
42+
};
43+
}
44+
45+
if available.contains(&AfBackend::AF_BACKEND_OPENCL){
46+
println!("Evaluating OpenCL Backend...");
47+
let err = set_backend(AfBackend::AF_BACKEND_OPENCL);
48+
match err {
49+
Ok(_) => test_backend(),
50+
Err(e) => println!("OpenCL backend error: {}", e),
51+
};
52+
}
4253
}

src/defines.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub enum AfError {
4848
}
4949

5050
#[repr(C)]
51-
#[derive(Clone, Copy, Debug)]
51+
#[derive(Clone, Copy, Debug, PartialEq)]
5252
pub enum AfBackend {
5353
/// Default backend order: OpenCL -> CUDA -> CPU
5454
AF_BACKEND_DEFAULT = 0,
@@ -60,6 +60,18 @@ pub enum AfBackend {
6060
AF_BACKEND_OPENCL = 3
6161
}
6262

63+
impl Display for AfBackend {
64+
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
65+
let text = match *self {
66+
AfBackend::AF_BACKEND_OPENCL => "OpenCL",
67+
AfBackend::AF_BACKEND_CUDA => "Cuda",
68+
AfBackend::AF_BACKEND_CPU => "CPU",
69+
AfBackend::AF_BACKEND_DEFAULT => "Default",
70+
};
71+
write!(f, "{}", text)
72+
}
73+
}
74+
6375
impl Display for AfError {
6476
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
6577
write!(f, "{}", self.description())

src/device/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern {
1212
fn af_set_device(device: c_int) -> c_int;
1313
fn af_set_backend(bknd: uint8_t) -> c_int;
1414
fn af_get_backend_count(num_backends: *mut c_uint) -> c_int;
15+
fn af_get_available_backends(backends: *mut c_int) -> c_int;
1516
fn af_get_device(device: *mut c_int) -> c_int;
1617
fn af_sync(device: c_int) -> c_int;
1718
}
@@ -160,3 +161,23 @@ pub fn get_backend_count() -> Result<u32, AfError> {
160161
}
161162
}
162163
}
164+
165+
166+
/// Get the available backends
167+
#[allow(unused_mut)]
168+
pub fn get_available_backends() -> Result<Vec<AfBackend>, AfError> {
169+
unsafe {
170+
let mut temp: i32 = 0;
171+
let err_val = af_get_available_backends(&mut temp as *mut c_int);
172+
match err_val {
173+
0 => {
174+
let mut b = Vec::new();
175+
if temp & 0b0100 == 0b0100 { b.push(AfBackend::AF_BACKEND_OPENCL); }
176+
if temp & 0b0010 == 0b0010 { b.push(AfBackend::AF_BACKEND_CUDA); }
177+
if temp & 0b0001 == 0b0001 { b.push(AfBackend::AF_BACKEND_CPU); }
178+
Ok(b)
179+
},
180+
_ => Err(AfError::from(err_val)),
181+
}
182+
}
183+
}

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ pub use data::{reorder, shift, moddims, flat, flip};
3434
pub use data::{select, selectl, selectr, replace, replace_scalar};
3535
mod data;
3636

37-
pub use device::{get_version, info, device_count, is_double_available, set_device, get_device, sync, get_backend_count, set_backend};
37+
pub use device::{get_version, info, device_count, is_double_available
38+
, set_device, get_device, sync, get_backend_count, set_backend
39+
, get_available_backends};
3840
mod device;
3941

4042
pub use defines::{Aftype, AfError, AfBackend, ColorMap, YCCStd};

0 commit comments

Comments
 (0)