Skip to content

Replace Result returns in API with callback based error handling #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 18, 2016
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ build = "build.rs"
libc = "0.1.10"
num = "0.1.27"
time = "0.1.32"
lazy_static = "0.2.1"

[build-dependencies.rustc-serialize]
[build-dependencies]
rustc-serialize = "0.3.16"

[lib]
Expand Down
53 changes: 19 additions & 34 deletions examples/helloworld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,75 +11,62 @@ fn main() {
let num_rows: u64 = 5;
let num_cols: u64 = 3;
let values: &[f32] = &[1.0, 2.0, 3.0];
let indices = Array::new(values, Dim4::new(&[3, 1, 1, 1])).unwrap();
let indices = Array::new(values, Dim4::new(&[3, 1, 1, 1]));

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

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

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

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

let b3 = ! &a;
af_print!("sin(a) + 1.5 => ", b);
af_print!("sin(a) + cos(a) => ", b2);
af_print!("!a => ", b3);

let test = &a + &b;
let test = a.clone() + b.clone();
af_print!("a + b", test);

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

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

let mut idxrs = match Indexer::new() {
Ok(v) => v,
Err(e) => panic!("{}",e),
};
let mut idxrs = Indexer::new();
idxrs.set_index(&indices, 0, None);
idxrs.set_index(&seq4gen, 1, Some(false));

let sub2 = index_gen(&a, idxrs).unwrap();
let sub2 = index_gen(&a, idxrs);
af_print!("a(indices, seq(0, 2, 1))", sub2);

// 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");
fft(&b, 1.0, 0).map(|x| print(&x));
print(&fft(&b, 1.0, 0));

println!("Grab last row & col of the random matrix");
print(&a);
print(&row(&a, num_rows - 1).unwrap());
print(&col(&a, num_cols - 1).unwrap());
print(&row(&a, num_rows - 1));
print(&col(&a, num_cols - 1));

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

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_input, d_dims).unwrap();
let d = Array::new(&d_input, d_dims);
af_print!("Create 2-by-3 matrix from host data", d);

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

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

let u8_cnst = &constant(1 as u8, dims).unwrap();
let u8_cnst = &constant(1 as u8, dims);
af_print!("u8 constant array", u8_cnst);
println!("Is u8_cnst array float precision type ? {}", u8_cnst.is_single().unwrap());
println!("Is u8_cnst array float precision type ? {}", u8_cnst.is_single());
}
36 changes: 12 additions & 24 deletions examples/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,22 @@ fn main() {
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, false))
.unwrap();
let img_wnd = Window::new(480, 640, String::from("Input Image"));
img_wnd.set_position(100, 100);

let hst_wnd = Window::new(512, 512, String::from("Input Image Histogram"));
hst_wnd.set_position(600, 100);

let man = load_image(format!("{}/man.jpg", assets_dir.display()), false);
let hst = histogram(&man, 256, 0.0, 255.0);

let disp_img = div(&man, &constant(255 as f32, man.dims()), false);

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; }
if img_wnd.is_closed() == true { break; }
if hst_wnd.is_closed() == true { break; }
}
}
17 changes: 9 additions & 8 deletions examples/pi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@ fn main() {
let samples = 20_000_000;
let dims = Dim4::new(&[samples, 1, 1, 1]);

let x = &randu::<f32>(dims).unwrap();
let y = &randu::<f32>(dims).unwrap();
let x = &randu::<f32>(dims);
let y = &randu::<f32>(dims);

let start = PreciseTime::now();

mem_info!("Before benchmark");

for bench_iter in 0..100 {
let pi_val = add(&mul(x, x, false).unwrap(), &mul(y, y, false).unwrap(), false)
.and_then( |z| sqrt(&z) )
.and_then( |z| le(&z, &constant(1, dims).unwrap(), false) )
.and_then( |z| sum_all(&z) )
.map( |z| z.0 * 4.0/(samples as f64) )
.unwrap();
let xsqrd = &mul(x, x, false);
let ysqrd = &mul(y, y, false);
let xplusy = &add(xsqrd, ysqrd, false);
let root = &sqrt(xplusy);
let cnst = &constant(1, dims);
let (real, imag) = sum_all(&le(root, cnst, false));
let pi_val = real*4.0/(samples as f64);
}

let end = PreciseTime::now();
Expand Down
10 changes: 3 additions & 7 deletions examples/snow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ 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 wnd = Window::new(1280, 720, String::from("Snow"));

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

loop {
randu::<f32>(dims).as_ref()
.map(|arr| wnd.draw_image(arr, None));
wnd.draw_image(&randu::<f32>(dims), None);

if wnd.is_closed().unwrap() == true { break; }
if wnd.is_closed() == true { break; }
}
}
46 changes: 19 additions & 27 deletions examples/unified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,38 @@ fn test_backend(){
let dims = Dim4::new(&[num_rows, num_cols, 1, 1]);

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


#[allow(unused_must_use)]
fn main() {
println!("There are {:?} available backends", get_backend_count().unwrap());
let available = get_available_backends().unwrap();
println!("There are {:?} available backends", get_backend_count());
let available = get_available_backends();

if available.contains(&Backend::CPU){
if available.contains(&Backend::CPU) {
println!("Evaluating CPU Backend...");
let err = set_backend(Backend::CPU);
println!("There are {} CPU compute devices", device_count().unwrap());
match err {
Ok(_) => test_backend(),
Err(e) => println!("CPU backend error: {}", e),
};
set_backend(Backend::CPU);
println!("There are {} CPU compute devices", device_count());
test_backend();
}

if available.contains(&Backend::CUDA){
if available.contains(&Backend::CUDA) {
println!("Evaluating CUDA Backend...");
let err = set_backend(Backend::CUDA);
println!("There are {} CUDA compute devices", device_count().unwrap());
match err {
Ok(_) => test_backend(),
Err(e) => println!("CUDA backend error: {}", e),
};
set_backend(Backend::CUDA);
println!("There are {} CUDA compute devices", device_count());
test_backend();
}

if available.contains(&Backend::OPENCL){
if available.contains(&Backend::OPENCL) {
println!("Evaluating OpenCL Backend...");
let err = set_backend(Backend::OPENCL);
println!("There are {} OpenCL compute devices", device_count().unwrap());
match err {
Ok(_) => test_backend(),
Err(e) => println!("OpenCL backend error: {}", e),
};
set_backend(Backend::OPENCL);
println!("There are {} OpenCL compute devices", device_count());
test_backend();
}
}
Loading