Skip to content

Commit fb1b3cc

Browse files
committed
Added functions for arith operations +, -, /, *
* added pi example * cleaned up helloworld example
1 parent 2efba9b commit fb1b3cc

File tree

6 files changed

+89
-22
lines changed

6 files changed

+89
-22
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 & 16 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,18 +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).ok().unwrap();
13+
let a = af::randu(dims, af::Aftype::F32).unwrap();
1514
af::print(&a);
1615

1716
println!("Element-wise arithmetic");
18-
let sin_res = af::sin(&a).ok().unwrap();
19-
let cos_res = af::cos(&a).ok().unwrap();
20-
let b: Array = &sin_res + 1.5;
21-
let b2: Array = &sin_res + &cos_res;
22-
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;
2322
println!("sin(a) + 1.5 => "); af::print(&b);
2423
println!("sin(a) + cos(a) => "); af::print(&b2);
2524
println!("!a => "); af::print(&b3);
@@ -32,31 +31,31 @@ fn main() {
3231
// af_print(B);
3332

3433
println!("Fourier transform the result");
35-
let c: Array = af::fft(&b, 1.0, 0).ok().unwrap();
34+
let c = &af::fft(&b, 1.0, 0).unwrap();
3635
af::print(&c);
3736

3837
// printf("Grab last row\n");
3938
// array c = C.row(end);
4039
// af_print(c);
4140

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

4847
// printf("Copy last column onto first\n");
4948
// D.col(0) = D.col(end);
5049
// af_print(D);
5150

5251
// // Sort A
5352
println!("Sort A and print sorted array and corresponding indices");
54-
let (vals, inds) = af::sort_index(&a, 0, true).ok().unwrap();
53+
let (vals, inds) = af::sort_index(&a, 0, true).unwrap();
5554
af::print(&vals);
5655
af::print(&inds);
5756

5857
println!("u8 constant array");
59-
let u8_cnst = af::constant(1 as u8, dims).ok().unwrap();
60-
af::print(&u8_cnst);
61-
println!("Is u8_cnst array float precision type ? {}", u8_cnst.is_single().ok().unwrap());
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());
6261
}

examples/pi.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
fn main() {
9+
af::set_device(0);
10+
af::info();
11+
let samples = 20_000_000;
12+
let dims = Dim4::new(&[samples, 1, 1, 1]);
13+
14+
let x = &af::randu(dims, Aftype::F32).unwrap();
15+
let y = &af::randu(dims, Aftype::F32).unwrap();
16+
17+
let start = PreciseTime::now();
18+
let pi_val = af::add(&(x*x), &(y*y))
19+
//let pi_val = af::sqrt(&(x*x) + &(y*y))
20+
.and_then( |z| af::sqrt(&z) )
21+
.and_then( |z| af::le(&z, &af::constant(1, dims).unwrap()) )
22+
.and_then( |z| af::sum_all(&z) )
23+
.map( |z| z.0 * 4.0/(samples as f64) )
24+
.unwrap();
25+
let end = PreciseTime::now();
26+
27+
println!("Estimated Pi Value: {} in {} seconds", pi_val, start.to(end));
28+
}

src/arith/mod.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ macro_rules! binary_func {
180180
)
181181
}
182182

183+
binary_func!(add, af_add);
184+
binary_func!(sub, af_sub);
185+
binary_func!(mul, af_mul);
186+
binary_func!(div, af_div);
183187
binary_func!(lt, af_lt);
184188
binary_func!(gt, af_gt);
185189
binary_func!(le, af_le);
@@ -203,13 +207,14 @@ macro_rules! arith_scalar_func {
203207
type Output = Array;
204208

205209
fn $fn_name(self, rhs: $rust_type) -> Array {
206-
let cnst_arr = constant(rhs, self.dims().ok().unwrap()).ok().unwrap();
210+
let cnst_arr = constant(rhs, self.dims().unwrap()).unwrap();
207211
unsafe {
208212
let mut temp: i64 = 0;
209-
$ffi_fn(&mut temp as MutAfArray,
210-
self.get() as AfArray, cnst_arr.get() as AfArray,
211-
0);
212-
Array::from(temp)
213+
match $ffi_fn(&mut temp as MutAfArray, self.get() as AfArray,
214+
cnst_arr.get() as AfArray, 0) {
215+
0 => Array::from(temp),
216+
_ => panic!("Arithmetic operator on Array failed with error code"),
217+
}
213218
}
214219
}
215220
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub use algorithm::{accum, locate, diff1, diff2, sort, sort_index, sort_by_key};
1010
pub use algorithm::{set_unique, set_union, set_intersect};
1111
mod algorithm;
1212

13-
pub use arith::{lt, gt, le, ge, eq, neq, and, or, minof, maxof};
13+
pub use arith::{add, sub, div, mul, lt, gt, le, ge, eq, neq, and, or, minof, maxof};
1414
pub use arith::{abs, sign, round, trunc, floor, ceil, modulo};
1515
pub use arith::{sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh};
1616
pub use arith::{atan2, cplx2, arg, cplx, real, imag, conjg, hypot};

0 commit comments

Comments
 (0)