Skip to content

Commit c6846d6

Browse files
committed
Merge pull request #70 from arrayfire/new_error_handling
Replace Result returns in API with callback based error handling
2 parents 755544e + 8b68882 commit c6846d6

File tree

26 files changed

+974
-1297
lines changed

26 files changed

+974
-1297
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ build = "build.rs"
1515
libc = "0.1.10"
1616
num = "0.1.27"
1717
time = "0.1.32"
18+
lazy_static = "0.2.1"
1819

19-
[build-dependencies.rustc-serialize]
20+
[build-dependencies]
2021
rustc-serialize = "0.3.16"
2122

2223
[lib]

examples/helloworld.rs

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,75 +11,62 @@ fn main() {
1111
let num_rows: u64 = 5;
1212
let num_cols: u64 = 3;
1313
let values: &[f32] = &[1.0, 2.0, 3.0];
14-
let indices = Array::new(values, Dim4::new(&[3, 1, 1, 1])).unwrap();
14+
let indices = Array::new(values, Dim4::new(&[3, 1, 1, 1]));
1515

1616
let dims = Dim4::new(&[num_rows, num_cols, 1, 1]);
1717

18-
let a = match randu::<f32>(dims) {
19-
Ok(value) => value,
20-
Err(error) => panic!("{}", error),
21-
};
18+
let a = randu::<f32>(dims);
2219
af_print!("Create a 5-by-3 matrix of random floats on the GPU", a);
2320

2421
println!("Element-wise arithmetic");
25-
let b = sin(&a)
26-
.and_then(|x| add(&x, &1.5, false))
27-
.unwrap();
22+
let b = add(&sin(&a), &1.5, false);
2823

29-
let b2 = sin(&a).
30-
and_then(|x| {
31-
cos(&a)
32-
.and_then(|y| add(&x, &y, false))
33-
})
34-
.unwrap();
24+
let b2 = add(&sin(&a), &cos(&a), false);
3525

3626
let b3 = ! &a;
3727
af_print!("sin(a) + 1.5 => ", b);
3828
af_print!("sin(a) + cos(a) => ", b2);
3929
af_print!("!a => ", b3);
4030

41-
let test = &a + &b;
31+
let test = a.clone() + b.clone();
4232
af_print!("a + b", test);
4333

4434
// Index array using sequences
4535
let seqs = &[Seq::new(1u32, 3, 1), Seq::default()];
46-
let sub = index(&a, seqs).unwrap();
36+
let sub = index(&a, seqs);
4737
af_print!("a(seq(1,3,1), span)", sub);
4838

4939
//Index array using array and sequence
5040
let seq4gen = Seq::new(0u32, 2, 1);
5141

52-
let mut idxrs = match Indexer::new() {
53-
Ok(v) => v,
54-
Err(e) => panic!("{}",e),
55-
};
42+
let mut idxrs = Indexer::new();
5643
idxrs.set_index(&indices, 0, None);
5744
idxrs.set_index(&seq4gen, 1, Some(false));
5845

59-
let sub2 = index_gen(&a, idxrs).unwrap();
46+
let sub2 = index_gen(&a, idxrs);
6047
af_print!("a(indices, seq(0, 2, 1))", sub2);
6148

6249
// printf("Negate the first three elements of second column\n");
6350
// B(seq(0, 2), 1) = B(seq(0, 2), 1) * -1;
6451
// af_print(B);
6552

6653
println!("Fourier transform the result");
67-
fft(&b, 1.0, 0).map(|x| print(&x));
54+
print(&fft(&b, 1.0, 0));
6855

6956
println!("Grab last row & col of the random matrix");
7057
print(&a);
71-
print(&row(&a, num_rows - 1).unwrap());
72-
print(&col(&a, num_cols - 1).unwrap());
58+
print(&row(&a, num_rows - 1));
59+
print(&col(&a, num_cols - 1));
7360

7461
let r_dims = Dim4::new(&[3, 1, 1, 1]);
7562
let r_input: [f32; 3] = [1.0, 1.0, 1.0];
76-
let r = Array::new(&r_input, r_dims).unwrap();
77-
let ur = set_row(&a, &r, num_rows - 1).unwrap();
63+
let r = Array::new(&r_input, r_dims);
64+
let ur = set_row(&a, &r, num_rows - 1);
7865
af_print!("Set last row to 1's", ur);
7966

8067
let d_dims = Dim4::new(&[2, 3, 1, 1]);
8168
let d_input: [i32; 6] = [1, 2, 3, 4, 5, 6];
82-
let d = Array::new(&d_input, d_dims).unwrap();
69+
let d = Array::new(&d_input, d_dims);
8370
af_print!("Create 2-by-3 matrix from host data", d);
8471

8572
// printf("Copy last column onto first\n");
@@ -88,13 +75,11 @@ fn main() {
8875

8976
// // Sort A
9077
println!("Sort A and print sorted array and corresponding indices");
91-
sort_index(&a, 0, true)
92-
.map(| x | {
93-
print(&x.0);
94-
print(&x.1);
95-
});
78+
let x = sort_index(&a, 0, true);
79+
print(&x.0);
80+
print(&x.1);
9681

97-
let u8_cnst = &constant(1 as u8, dims).unwrap();
82+
let u8_cnst = &constant(1 as u8, dims);
9883
af_print!("u8 constant array", u8_cnst);
99-
println!("Is u8_cnst array float precision type ? {}", u8_cnst.is_single().unwrap());
84+
println!("Is u8_cnst array float precision type ? {}", u8_cnst.is_single());
10085
}

examples/histogram.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,22 @@ fn main() {
1313
let assets_dir = PathBuf::from(&env::var("CARGO_MANIFEST_DIR").unwrap())
1414
.join("arrayfire").join("assets").join("examples").join("images");
1515

16-
let img_wnd = match Window::new(480, 640, String::from("Input Image")) {
17-
Ok(v) => { v.set_position(100, 100).unwrap(); v },
18-
Err(e)=> panic!("Window creation failed, exiting: {}", e),
19-
};
20-
21-
let hst_wnd = match Window::new(512, 512, String::from("Input Image Histogram")) {
22-
Ok(v) => { v.set_position(600, 100).unwrap(); v },
23-
Err(e)=> panic!("Window creation failed, exiting: {}", e),
24-
};
25-
26-
let (man, hst) = match load_image(format!("{}/man.jpg", assets_dir.display()), false) {
27-
Ok(v) => match histogram(&v, 256, 0.0, 255.0) {
28-
Ok(h) => (v, h),
29-
Err(e)=> panic!("Histogram computation failed, exiting: {}", e),
30-
},
31-
Err(e)=> panic!("Image loading failed, exiting: {}", e),
32-
};
33-
34-
let disp_img = man.dims()
35-
.and_then(|x| constant(255 as f32, x))
36-
.and_then(|x| div(&man, &x, false))
37-
.unwrap();
16+
let img_wnd = Window::new(480, 640, String::from("Input Image"));
17+
img_wnd.set_position(100, 100);
18+
19+
let hst_wnd = Window::new(512, 512, String::from("Input Image Histogram"));
20+
hst_wnd.set_position(600, 100);
21+
22+
let man = load_image(format!("{}/man.jpg", assets_dir.display()), false);
23+
let hst = histogram(&man, 256, 0.0, 255.0);
24+
25+
let disp_img = div(&man, &constant(255 as f32, man.dims()), false);
3826

3927
loop {
4028
img_wnd.draw_image(&disp_img, None);
4129
hst_wnd.draw_hist(&hst, 0.0, 255.0, None);
4230

43-
if img_wnd.is_closed().unwrap() == true { break; }
44-
if hst_wnd.is_closed().unwrap() == true { break; }
31+
if img_wnd.is_closed() == true { break; }
32+
if hst_wnd.is_closed() == true { break; }
4533
}
4634
}

examples/pi.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@ fn main() {
1313
let samples = 20_000_000;
1414
let dims = Dim4::new(&[samples, 1, 1, 1]);
1515

16-
let x = &randu::<f32>(dims).unwrap();
17-
let y = &randu::<f32>(dims).unwrap();
16+
let x = &randu::<f32>(dims);
17+
let y = &randu::<f32>(dims);
1818

1919
let start = PreciseTime::now();
2020

2121
mem_info!("Before benchmark");
2222

2323
for bench_iter in 0..100 {
24-
let pi_val = add(&mul(x, x, false).unwrap(), &mul(y, y, false).unwrap(), false)
25-
.and_then( |z| sqrt(&z) )
26-
.and_then( |z| le(&z, &constant(1, dims).unwrap(), false) )
27-
.and_then( |z| sum_all(&z) )
28-
.map( |z| z.0 * 4.0/(samples as f64) )
29-
.unwrap();
24+
let xsqrd = &mul(x, x, false);
25+
let ysqrd = &mul(y, y, false);
26+
let xplusy = &add(xsqrd, ysqrd, false);
27+
let root = &sqrt(xplusy);
28+
let cnst = &constant(1, dims);
29+
let (real, imag) = sum_all(&le(root, cnst, false));
30+
let pi_val = real*4.0/(samples as f64);
3031
}
3132

3233
let end = PreciseTime::now();

examples/snow.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@ fn main() {
88
set_device(0);
99
info();
1010

11-
let wnd = match Window::new(1280, 720, String::from("Snow")) {
12-
Ok(v) => v,
13-
Err(e)=> panic!("Window creation failed, exiting"),
14-
};
11+
let wnd = Window::new(1280, 720, String::from("Snow"));
1512

1613
let dims = Dim4::new(&[1280, 720, 3, 1]);
1714

1815
loop {
19-
randu::<f32>(dims).as_ref()
20-
.map(|arr| wnd.draw_image(arr, None));
16+
wnd.draw_image(&randu::<f32>(dims), None);
2117

22-
if wnd.is_closed().unwrap() == true { break; }
18+
if wnd.is_closed() == true { break; }
2319
}
2420
}

examples/unified.rs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,38 @@ fn test_backend(){
1111
let dims = Dim4::new(&[num_rows, num_cols, 1, 1]);
1212

1313
println!("Create a 10-by-10 matrix of random floats on the compute device");
14-
let a = match randu::<f32>(dims) {
15-
Ok(value) => value,
16-
Err(error) => panic!("{}", error),
17-
};
14+
let mut a = randu::<f32>(dims);
15+
let b = randu::<f32>(dims);
16+
print(&a);
17+
print(&b);
18+
a += b;
1819
print(&a);
1920
}
2021

2122

2223
#[allow(unused_must_use)]
2324
fn main() {
24-
println!("There are {:?} available backends", get_backend_count().unwrap());
25-
let available = get_available_backends().unwrap();
25+
println!("There are {:?} available backends", get_backend_count());
26+
let available = get_available_backends();
2627

27-
if available.contains(&Backend::CPU){
28+
if available.contains(&Backend::CPU) {
2829
println!("Evaluating CPU Backend...");
29-
let err = set_backend(Backend::CPU);
30-
println!("There are {} CPU compute devices", device_count().unwrap());
31-
match err {
32-
Ok(_) => test_backend(),
33-
Err(e) => println!("CPU backend error: {}", e),
34-
};
30+
set_backend(Backend::CPU);
31+
println!("There are {} CPU compute devices", device_count());
32+
test_backend();
3533
}
3634

37-
if available.contains(&Backend::CUDA){
35+
if available.contains(&Backend::CUDA) {
3836
println!("Evaluating CUDA Backend...");
39-
let err = set_backend(Backend::CUDA);
40-
println!("There are {} CUDA compute devices", device_count().unwrap());
41-
match err {
42-
Ok(_) => test_backend(),
43-
Err(e) => println!("CUDA backend error: {}", e),
44-
};
37+
set_backend(Backend::CUDA);
38+
println!("There are {} CUDA compute devices", device_count());
39+
test_backend();
4540
}
4641

47-
if available.contains(&Backend::OPENCL){
42+
if available.contains(&Backend::OPENCL) {
4843
println!("Evaluating OpenCL Backend...");
49-
let err = set_backend(Backend::OPENCL);
50-
println!("There are {} OpenCL compute devices", device_count().unwrap());
51-
match err {
52-
Ok(_) => test_backend(),
53-
Err(e) => println!("OpenCL backend error: {}", e),
54-
};
44+
set_backend(Backend::OPENCL);
45+
println!("There are {} OpenCL compute devices", device_count());
46+
test_backend();
5547
}
5648
}

0 commit comments

Comments
 (0)