Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ path = "examples/helloworld.rs"
[[example]]
name = "pi"
path = "examples/pi.rs"

[[example]]
name = "snow"
path = "examples/snow.rs"

[[example]]
name = "histogram"
path = "examples/histogram.rs"
2 changes: 1 addition & 1 deletion build.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build_cpu": "ON",
"build_examples": "OFF",
"build_test": "OFF",
"build_graphics": "OFF",
"build_graphics": "ON",

"glew_static": "OFF",
"freeimage_type": "DYNAMIC",
Expand Down
53 changes: 32 additions & 21 deletions examples/helloworld.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
extern crate arrayfire as af;

use af::Dim4;
use af::Array;
use af::*;

#[allow(unused_must_use)]
fn main() {
af::set_device(0);
af::info();
set_device(0);
info();

let dims = Dim4::new(&[5, 3, 1, 1]);

println!("Create a 5-by-3 matrix of random floats on the GPU");
let a = af::randu(dims, af::Aftype::F32).unwrap();
af::print(&a);
let a = match randu(dims, Aftype::F32) {
Ok(value) => value,
Err(error) => panic!("{}", error),
};
print(&a);

println!("Element-wise arithmetic");
let b = af::add(af::sin(&a), 1.5).unwrap();
let b2 = af::add(af::sin(&a), af::cos(&a)).unwrap();
let b = sin(&a)
.and_then(|x| add(x, 1.5))
.unwrap();

let b2 = sin(&a).
and_then(|x| {
cos(&a)
.and_then(|y| add(x, y))
})
.unwrap();

let b3 = ! &a;
println!("sin(a) + 1.5 => "); af::print(&b);
println!("sin(a) + cos(a) => "); af::print(&b2);
println!("!a => "); af::print(&b3);
println!("sin(a) + 1.5 => "); print(&b);
println!("sin(a) + cos(a) => "); print(&b2);
println!("!a => "); print(&b3);

let test = &a + &b;
println!("a + b"); af::print(&test);
println!("a + b"); print(&test);

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

println!("Fourier transform the result");
let c = &af::fft(&b, 1.0, 0).unwrap();
af::print(&c);
fft(&b, 1.0, 0).map(|x| print(&x));

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

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

// // Sort A
println!("Sort A and print sorted array and corresponding indices");
let (vals, inds) = af::sort_index(&a, 0, true).unwrap();
af::print(&vals);
af::print(&inds);
sort_index(&a, 0, true)
.map(| x | {
print(&x.0);
print(&x.1);
});

println!("u8 constant array");
let u8_cnst = &af::constant(1 as u8, dims).unwrap();
af::print(u8_cnst);
let u8_cnst = &constant(1 as u8, dims).unwrap();
print(u8_cnst);
println!("Is u8_cnst array float precision type ? {}", u8_cnst.is_single().unwrap());
}
46 changes: 46 additions & 0 deletions examples/histogram.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
extern crate arrayfire as af;

use af::*;
use std::env;
use std::path::PathBuf;

#[allow(unused_variables)]
#[allow(unused_must_use)]
fn main() {
set_device(0);
info();

let assets_dir = PathBuf::from(&env::var("CARGO_MANIFEST_DIR").unwrap())
.join("arrayfire").join("assets").join("examples").join("images");

let img_wnd = match Window::new(480, 640, String::from("Input Image")) {
Ok(v) => { v.set_position(100, 100).unwrap(); v },
Err(e)=> panic!("Window creation failed, exiting: {}", e),
};

let hst_wnd = match Window::new(512, 512, String::from("Input Image Histogram")) {
Ok(v) => { v.set_position(600, 100).unwrap(); v },
Err(e)=> panic!("Window creation failed, exiting: {}", e),
};

let (man, hst) = match load_image(format!("{}/man.jpg", assets_dir.display()), false) {
Ok(v) => match histogram(&v, 256, 0.0, 255.0) {
Ok(h) => (v, h),
Err(e)=> panic!("Histogram computation failed, exiting: {}", e),
},
Err(e)=> panic!("Image loading failed, exiting: {}", e),
};

let disp_img = man.dims()
.and_then(|x| constant(255 as f32, x))
.and_then(|x| div(man, x))
.unwrap();

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

if img_wnd.is_closed().unwrap() == true { break; }
if hst_wnd.is_closed().unwrap() == true { break; }
}
}
19 changes: 9 additions & 10 deletions examples/pi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@ extern crate arrayfire as af;
extern crate time;

use time::PreciseTime;
use af::Dim4;
use af::Aftype;
use af::*;

#[allow(unused_must_use)]
#[allow(unused_variables)]
fn main() {
af::set_device(0);
af::info();
set_device(0);
info();
let samples = 20_000_000;
let dims = Dim4::new(&[samples, 1, 1, 1]);

let x = &af::randu(dims, Aftype::F32).unwrap();
let y = &af::randu(dims, Aftype::F32).unwrap();
let x = &randu(dims, Aftype::F32).unwrap();
let y = &randu(dims, Aftype::F32).unwrap();

let start = PreciseTime::now();

for bench_iter in 0..100 {
let pi_val = af::add(&(x*x), &(y*y))
.and_then( |z| af::sqrt(&z) )
.and_then( |z| af::le(&z, &af::constant(1, dims).unwrap()) )
.and_then( |z| af::sum_all(&z) )
let pi_val = add(x*x, y*y)
.and_then( |z| sqrt(&z) )
.and_then( |z| le(z, constant(1, dims).unwrap()) )
.and_then( |z| sum_all(&z) )
.map( |z| z.0 * 4.0/(samples as f64) )
.unwrap();
}
Expand Down
24 changes: 24 additions & 0 deletions examples/snow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
extern crate arrayfire as af;

use af::*;

#[allow(unused_variables)]
#[allow(unused_must_use)]
fn main() {
set_device(0);
info();

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

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

loop {
randu(dims, Aftype::F32).as_ref()
.map(|arr| wnd.draw_image(arr, None));

if wnd.is_closed().unwrap() == true { break; }
}
}
8 changes: 2 additions & 6 deletions src/arith/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ macro_rules! convertable_type_def {
)
}

convertable_type_def!(u64);
convertable_type_def!(i64);
convertable_type_def!(f64);
convertable_type_def!(f32);
convertable_type_def!(i32);
Expand All @@ -219,12 +221,6 @@ impl Convertable for Array {
}
}

impl Convertable for Result<Array, AfError> {
fn convert(&self) -> Array {
self.clone().unwrap()
}
}

macro_rules! overloaded_binary_func {
($fn_name: ident, $help_name: ident, $ffi_name: ident) => (
fn $help_name(lhs: &Array, rhs: &Array) -> Result<Array, AfError> {
Expand Down
45 changes: 45 additions & 0 deletions src/defines.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::fmt::Error as FmtError;

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub enum AfError {
Expand Down Expand Up @@ -74,6 +78,35 @@ pub enum AfError {
ERR_UNKNOWN = 999
}

impl Display for AfError {
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
write!(f, "{}", self.description())
}
}

impl Error for AfError {
fn description(&self) -> &str {
match *self {
AfError::SUCCESS => "Function returned successfully",
AfError::ERR_NO_MEM => "The system or device ran out of memory",
AfError::ERR_DRIVER => "Device driver error",
AfError::ERR_RUNTIME => "Error in runtime environment",
AfError::ERR_INVALID_ARRAY => "Input is not a valid Array Object",
AfError::ERR_ARG => "One of the function arguments is incorrect",
AfError::ERR_SIZE => "The size is incorrect",
AfError::ERR_TYPE => "The type is not supported by this function",
AfError::ERR_DIFF_TYPE => "The type of input arrays are not compatible",
AfError::ERR_BATCH => "Function does not support GFOR / batch mode",
AfError::ERR_NOT_SUPPORTED => "The option is not supported",
AfError::ERR_NOT_CONFIGURED => "This build of ArrayFire does not support this feature",
AfError::ERR_NO_DBL => "This device does not support double",
AfError::ERR_NO_GFX => "This build of ArrayFire was not built with graphics or this device does not support graphics",
AfError::ERR_INTERNAL => "There was an internal error in either ArrayFire or upstream project",
AfError::ERR_UNKNOWN => "Unkown Error",
}
}
}

#[derive(Copy, Clone)]
pub enum Aftype {
F32 = 0,
Expand Down Expand Up @@ -168,3 +201,15 @@ pub enum NormType {
MATRIX_2 = 6,
MATRIX_L_PQ = 7,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub enum ColorMap {
DEFAULT = 0,
SPECTRUM= 1,
COLORS = 2,
RED = 3,
MOOD = 4,
HEAT = 5,
BLUE = 6,
}
Loading