Skip to content

Commit 21fc3e6

Browse files
committed
Merge pull request #17 from 9prady9/err_handling
Err handling
2 parents 982ebdf + e29ff49 commit 21fc3e6

File tree

18 files changed

+1098
-642
lines changed

18 files changed

+1098
-642
lines changed

Cargo.lock

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ build = "build.rs"
77
[dependencies]
88
libc = "*"
99
num = "*"
10+
time = "*"
1011

1112
[build-dependencies.rustc-serialize]
1213
rustc-serialize = "*"
@@ -18,3 +19,7 @@ path = "src/lib.rs"
1819
[[example]]
1920
name = "helloworld"
2021
path = "examples/helloworld.rs"
22+
23+
[[example]]
24+
name = "pi"
25+
path = "examples/pi.rs"

examples/helloworld.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
extern crate libc;
21
extern crate arrayfire as af;
32

43
use af::Dim4;
@@ -8,16 +7,18 @@ fn main() {
87
af::set_device(0);
98
af::info();
109

11-
let dims: Dim4 = Dim4::new(&[5, 3, 1, 1]);
10+
let dims = Dim4::new(&[5, 3, 1, 1]);
1211

1312
println!("Create a 5-by-3 matrix of random floats on the GPU");
14-
let a: Array = af::randu(dims, af::Aftype::F32);
13+
let a = af::randu(dims, af::Aftype::F32).unwrap();
1514
af::print(&a);
1615

1716
println!("Element-wise arithmetic");
18-
let b: Array = &af::sin(&a) + 1.5;
19-
let b2: Array = &af::sin(&a) + &af::cos(&a);
20-
let b3: Array = ! &a;
17+
let sin_res = af::sin(&a).unwrap();
18+
let cos_res = af::cos(&a).unwrap();
19+
let b = &sin_res + 1.5;
20+
let b2 = &sin_res + &cos_res;
21+
let b3 = ! &a;
2122
println!("sin(a) + 1.5 => "); af::print(&b);
2223
println!("sin(a) + cos(a) => "); af::print(&b2);
2324
println!("!a => "); af::print(&b3);
@@ -30,31 +31,31 @@ fn main() {
3031
// af_print(B);
3132

3233
println!("Fourier transform the result");
33-
let c: Array = af::fft(&b, 1.0, 0);
34+
let c = &af::fft(&b, 1.0, 0).unwrap();
3435
af::print(&c);
3536

3637
// printf("Grab last row\n");
3738
// array c = C.row(end);
3839
// af_print(c);
3940

4041
println!("Create 2-by-3 matrix from host data");
41-
let d_dims: Dim4 = Dim4::new(&[2, 3, 1, 1]);
42+
let d_dims = Dim4::new(&[2, 3, 1, 1]);
4243
let d_input: [i32; 6] = [1, 2, 3, 4, 5, 6];
43-
let d: Array = Array::new(d_dims, &d_input, af::Aftype::S32);
44-
af::print(&d);
44+
let d = &Array::new(d_dims, &d_input, af::Aftype::S32).unwrap();
45+
af::print(d);
4546

4647
// printf("Copy last column onto first\n");
4748
// D.col(0) = D.col(end);
4849
// af_print(D);
4950

5051
// // Sort A
5152
println!("Sort A and print sorted array and corresponding indices");
52-
let (vals, inds) = af::sort_index(&a, 0, true);
53+
let (vals, inds) = af::sort_index(&a, 0, true).unwrap();
5354
af::print(&vals);
5455
af::print(&inds);
5556

5657
println!("u8 constant array");
57-
let u8_cnst = af::constant(1 as u8, dims);
58-
af::print(&u8_cnst);
59-
println!("Is u8_cnst array float precision type ? {}", u8_cnst.is_single());
58+
let u8_cnst = &af::constant(1 as u8, dims).unwrap();
59+
af::print(u8_cnst);
60+
println!("Is u8_cnst array float precision type ? {}", u8_cnst.is_single().unwrap());
6061
}

examples/pi.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
extern crate arrayfire as af;
2+
extern crate time;
3+
4+
use time::PreciseTime;
5+
use af::Dim4;
6+
use af::Aftype;
7+
8+
#[allow(unused_must_use)]
9+
#[allow(unused_variables)]
10+
fn main() {
11+
af::set_device(0);
12+
af::info();
13+
let samples = 20_000_000;
14+
let dims = Dim4::new(&[samples, 1, 1, 1]);
15+
16+
let x = &af::randu(dims, Aftype::F32).unwrap();
17+
let y = &af::randu(dims, Aftype::F32).unwrap();
18+
19+
let start = PreciseTime::now();
20+
21+
for bench_iter in 0..100 {
22+
let pi_val = af::add(&(x*x), &(y*y))
23+
.and_then( |z| af::sqrt(&z) )
24+
.and_then( |z| af::le(&z, &af::constant(1, dims).unwrap()) )
25+
.and_then( |z| af::sum_all(&z) )
26+
.map( |z| z.0 * 4.0/(samples as f64) )
27+
.unwrap();
28+
}
29+
30+
let end = PreciseTime::now();
31+
32+
println!("Estimated Pi Value in {} seconds", start.to(end) / 100);
33+
}

0 commit comments

Comments
 (0)