Skip to content

Commit e3f300f

Browse files
committed
Merge pull request #21 from 9prady9/gfx
graphics module
2 parents 0a64db6 + d15eaf6 commit e3f300f

File tree

12 files changed

+377
-44
lines changed

12 files changed

+377
-44
lines changed

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ path = "examples/helloworld.rs"
2323
[[example]]
2424
name = "pi"
2525
path = "examples/pi.rs"
26+
27+
[[example]]
28+
name = "snow"
29+
path = "examples/snow.rs"
30+
31+
[[example]]
32+
name = "histogram"
33+
path = "examples/histogram.rs"

build.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"build_cpu": "ON",
1313
"build_examples": "OFF",
1414
"build_test": "OFF",
15-
"build_graphics": "OFF",
15+
"build_graphics": "ON",
1616

1717
"glew_static": "OFF",
1818
"freeimage_type": "DYNAMIC",

examples/helloworld.rs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,47 @@
11
extern crate arrayfire as af;
22

3-
use af::Dim4;
4-
use af::Array;
3+
use af::*;
54

65
#[allow(unused_must_use)]
76
fn main() {
8-
af::set_device(0);
9-
af::info();
7+
set_device(0);
8+
info();
109

1110
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 = af::randu(dims, af::Aftype::F32).unwrap();
15-
af::print(&a);
13+
let a = match randu(dims, Aftype::F32) {
14+
Ok(value) => value,
15+
Err(error) => panic!("{}", error),
16+
};
17+
print(&a);
1618

1719
println!("Element-wise arithmetic");
18-
let b = af::add(af::sin(&a), 1.5).unwrap();
19-
let b2 = af::add(af::sin(&a), af::cos(&a)).unwrap();
20+
let b = sin(&a)
21+
.and_then(|x| add(x, 1.5))
22+
.unwrap();
23+
24+
let b2 = sin(&a).
25+
and_then(|x| {
26+
cos(&a)
27+
.and_then(|y| add(x, y))
28+
})
29+
.unwrap();
2030

2131
let b3 = ! &a;
22-
println!("sin(a) + 1.5 => "); af::print(&b);
23-
println!("sin(a) + cos(a) => "); af::print(&b2);
24-
println!("!a => "); af::print(&b3);
32+
println!("sin(a) + 1.5 => "); print(&b);
33+
println!("sin(a) + cos(a) => "); print(&b2);
34+
println!("!a => "); print(&b3);
2535

2636
let test = &a + &b;
27-
println!("a + b"); af::print(&test);
37+
println!("a + b"); print(&test);
2838

2939
// printf("Negate the first three elements of second column\n");
3040
// B(seq(0, 2), 1) = B(seq(0, 2), 1) * -1;
3141
// af_print(B);
3242

3343
println!("Fourier transform the result");
34-
let c = &af::fft(&b, 1.0, 0).unwrap();
35-
af::print(&c);
44+
fft(&b, 1.0, 0).map(|x| print(&x));
3645

3746
// printf("Grab last row\n");
3847
// array c = C.row(end);
@@ -41,21 +50,23 @@ fn main() {
4150
println!("Create 2-by-3 matrix from host data");
4251
let d_dims = Dim4::new(&[2, 3, 1, 1]);
4352
let d_input: [i32; 6] = [1, 2, 3, 4, 5, 6];
44-
let d = &Array::new(d_dims, &d_input, af::Aftype::S32).unwrap();
45-
af::print(d);
53+
let d = &Array::new(d_dims, &d_input, Aftype::S32).unwrap();
54+
print(d);
4655

4756
// printf("Copy last column onto first\n");
4857
// D.col(0) = D.col(end);
4958
// af_print(D);
5059

5160
// // Sort A
5261
println!("Sort A and print sorted array and corresponding indices");
53-
let (vals, inds) = af::sort_index(&a, 0, true).unwrap();
54-
af::print(&vals);
55-
af::print(&inds);
62+
sort_index(&a, 0, true)
63+
.map(| x | {
64+
print(&x.0);
65+
print(&x.1);
66+
});
5667

5768
println!("u8 constant array");
58-
let u8_cnst = &af::constant(1 as u8, dims).unwrap();
59-
af::print(u8_cnst);
69+
let u8_cnst = &constant(1 as u8, dims).unwrap();
70+
print(u8_cnst);
6071
println!("Is u8_cnst array float precision type ? {}", u8_cnst.is_single().unwrap());
6172
}

examples/histogram.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
extern crate arrayfire as af;
2+
3+
use af::*;
4+
use std::env;
5+
use std::path::PathBuf;
6+
7+
#[allow(unused_variables)]
8+
#[allow(unused_must_use)]
9+
fn main() {
10+
set_device(0);
11+
info();
12+
13+
let assets_dir = PathBuf::from(&env::var("CARGO_MANIFEST_DIR").unwrap())
14+
.join("arrayfire").join("assets").join("examples").join("images");
15+
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))
37+
.unwrap();
38+
39+
loop {
40+
img_wnd.draw_image(&disp_img, None);
41+
hst_wnd.draw_hist(&hst, 0.0, 255.0, None);
42+
43+
if img_wnd.is_closed().unwrap() == true { break; }
44+
if hst_wnd.is_closed().unwrap() == true { break; }
45+
}
46+
}

examples/pi.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,26 @@ extern crate arrayfire as af;
22
extern crate time;
33

44
use time::PreciseTime;
5-
use af::Dim4;
6-
use af::Aftype;
5+
use af::*;
76

87
#[allow(unused_must_use)]
98
#[allow(unused_variables)]
109
fn main() {
11-
af::set_device(0);
12-
af::info();
10+
set_device(0);
11+
info();
1312
let samples = 20_000_000;
1413
let dims = Dim4::new(&[samples, 1, 1, 1]);
1514

16-
let x = &af::randu(dims, Aftype::F32).unwrap();
17-
let y = &af::randu(dims, Aftype::F32).unwrap();
15+
let x = &randu(dims, Aftype::F32).unwrap();
16+
let y = &randu(dims, Aftype::F32).unwrap();
1817

1918
let start = PreciseTime::now();
2019

2120
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) )
21+
let pi_val = add(x*x, y*y)
22+
.and_then( |z| sqrt(&z) )
23+
.and_then( |z| le(z, constant(1, dims).unwrap()) )
24+
.and_then( |z| sum_all(&z) )
2625
.map( |z| z.0 * 4.0/(samples as f64) )
2726
.unwrap();
2827
}

examples/snow.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
extern crate arrayfire as af;
2+
3+
use af::*;
4+
5+
#[allow(unused_variables)]
6+
#[allow(unused_must_use)]
7+
fn main() {
8+
set_device(0);
9+
info();
10+
11+
let wnd = match Window::new(1280, 720, String::from("Snow")) {
12+
Ok(v) => v,
13+
Err(e)=> panic!("Window creation failed, exiting"),
14+
};
15+
16+
let dims = Dim4::new(&[1280, 720, 3, 1]);
17+
18+
loop {
19+
randu(dims, Aftype::F32).as_ref()
20+
.map(|arr| wnd.draw_image(arr, None));
21+
22+
if wnd.is_closed().unwrap() == true { break; }
23+
}
24+
}

src/arith/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ macro_rules! convertable_type_def {
207207
)
208208
}
209209

210+
convertable_type_def!(u64);
211+
convertable_type_def!(i64);
210212
convertable_type_def!(f64);
211213
convertable_type_def!(f32);
212214
convertable_type_def!(i32);
@@ -219,12 +221,6 @@ impl Convertable for Array {
219221
}
220222
}
221223

222-
impl Convertable for Result<Array, AfError> {
223-
fn convert(&self) -> Array {
224-
self.clone().unwrap()
225-
}
226-
}
227-
228224
macro_rules! overloaded_binary_func {
229225
($fn_name: ident, $help_name: ident, $ffi_name: ident) => (
230226
fn $help_name(lhs: &Array, rhs: &Array) -> Result<Array, AfError> {

src/defines.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use std::error::Error;
2+
use std::fmt::{Display, Formatter};
3+
use std::fmt::Error as FmtError;
4+
15
#[repr(C)]
26
#[derive(Clone, Copy, Debug)]
37
pub enum AfError {
@@ -74,6 +78,35 @@ pub enum AfError {
7478
ERR_UNKNOWN = 999
7579
}
7680

81+
impl Display for AfError {
82+
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
83+
write!(f, "{}", self.description())
84+
}
85+
}
86+
87+
impl Error for AfError {
88+
fn description(&self) -> &str {
89+
match *self {
90+
AfError::SUCCESS => "Function returned successfully",
91+
AfError::ERR_NO_MEM => "The system or device ran out of memory",
92+
AfError::ERR_DRIVER => "Device driver error",
93+
AfError::ERR_RUNTIME => "Error in runtime environment",
94+
AfError::ERR_INVALID_ARRAY => "Input is not a valid Array Object",
95+
AfError::ERR_ARG => "One of the function arguments is incorrect",
96+
AfError::ERR_SIZE => "The size is incorrect",
97+
AfError::ERR_TYPE => "The type is not supported by this function",
98+
AfError::ERR_DIFF_TYPE => "The type of input arrays are not compatible",
99+
AfError::ERR_BATCH => "Function does not support GFOR / batch mode",
100+
AfError::ERR_NOT_SUPPORTED => "The option is not supported",
101+
AfError::ERR_NOT_CONFIGURED => "This build of ArrayFire does not support this feature",
102+
AfError::ERR_NO_DBL => "This device does not support double",
103+
AfError::ERR_NO_GFX => "This build of ArrayFire was not built with graphics or this device does not support graphics",
104+
AfError::ERR_INTERNAL => "There was an internal error in either ArrayFire or upstream project",
105+
AfError::ERR_UNKNOWN => "Unkown Error",
106+
}
107+
}
108+
}
109+
77110
#[derive(Copy, Clone)]
78111
pub enum Aftype {
79112
F32 = 0,
@@ -168,3 +201,15 @@ pub enum NormType {
168201
MATRIX_2 = 6,
169202
MATRIX_L_PQ = 7,
170203
}
204+
205+
#[repr(C)]
206+
#[derive(Copy, Clone)]
207+
pub enum ColorMap {
208+
DEFAULT = 0,
209+
SPECTRUM= 1,
210+
COLORS = 2,
211+
RED = 3,
212+
MOOD = 4,
213+
HEAT = 5,
214+
BLUE = 6,
215+
}

0 commit comments

Comments
 (0)