Skip to content

Bug Fixes #142

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
Jun 10, 2017
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: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ You can find the most recent updated documentation [here](http://arrayfire.githu

## Supported platforms

- Linux and OSX: The bindings have been tested with Rust 1.x.
- Windows: Rust 1.5 (MSVC ABI) is the first version that works with our bindings and ArrayFire library(built using MSVC compiler).

We recommend using Rust 1.5 and higher.

Rust 1.8 stabilized the traits for compound assignment operations. These are automatically enabled
based on the rust version you are using.
Linux, Windows and OSX. We recommend using Rust 1.15.1 or higher.

## Use from Crates.io [![](http://meritbadge.herokuapp.com/arrayfire)](https://crates.io/crates/arrayfire)

Expand Down
11 changes: 11 additions & 0 deletions src/arith/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,17 @@ macro_rules! overloaded_binary_func {
///
/// An Array with results of the binary operation.
///
/// In the case of comparison operations such as the following, the type of output
/// Array is [DType::B8](./enum.DType.html). To retrieve the results of such boolean output
/// to host, an array of 8-bit wide types(eg. u8, i8) should be used since ArrayFire's internal
/// implementation uses char for boolean.
///
/// * [gt](./fn.gt.html)
/// * [lt](./fn.lt.html)
/// * [ge](./fn.ge.html)
/// * [le](./fn.le.html)
/// * [eq](./fn.eq.html)
///
///# Note
///
/// The trait `Convertable` essentially translates to a scalar native type on rust or Array.
Expand Down
9 changes: 6 additions & 3 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,12 @@ impl Array {
}

/// Returns the number of elements in the Array
pub fn elements(&self) -> i64 {
pub fn elements(&self) -> usize {
unsafe {
let mut ret_val: i64 = 0;
let err_val = af_get_elements(&mut ret_val as MutAfArray, self.handle as AfArray);
HANDLE_ERROR(AfError::from(err_val));
ret_val
ret_val as usize
}
}

Expand Down Expand Up @@ -308,7 +308,10 @@ impl Array {
}

/// Copies the data from the Array to the mutable slice `data`
pub fn host<T>(&self, data: &mut [T]) {
pub fn host<T: HasAfEnum>(&self, data: &mut [T]) {
if data.len() != self.elements() {
HANDLE_ERROR(AfError::ERR_SIZE);
}
unsafe {
let err_val = af_get_data_ptr(data.as_mut_ptr() as *mut c_void, self.handle as AfArray);
HANDLE_ERROR(AfError::from(err_val));
Expand Down
2 changes: 2 additions & 0 deletions src/image/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ pub fn gradient(input: &Array) -> (Array, Array) {

/// Load Image into Array
///
/// Only, Images with 8/16/32 bits per channel can be loaded using this function.
///
/// # Parameters
///
/// - `filename` is aboslute path of the image to be loaded.
Expand Down
42 changes: 16 additions & 26 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,33 @@ use defines::AfError;
use error::HANDLE_ERROR;
use seq::Seq;
use self::libc::{c_double, c_int, c_uint};
use util::{AfArray, DimT, IndexT, MutAfArray, MutAfIndex};
use util::{AfArray, AfIndex, DimT, MutAfArray, MutAfIndex};

#[allow(dead_code)]
extern {
fn af_create_indexers(indexers: MutAfIndex) -> c_int;
fn af_set_array_indexer(indexer: MutAfIndex, idx: AfArray, dim: DimT) -> c_int;
fn af_set_seq_indexer(indexer: MutAfIndex, idx: *const SeqInternal, dim: DimT, is_batch: c_int) -> c_int;
fn af_release_indexers(indexers: MutAfIndex) -> c_int;
fn af_set_array_indexer(indexer: AfIndex, idx: AfArray, dim: DimT) -> c_int;
fn af_set_seq_indexer(indexer: AfIndex, idx: *const SeqInternal, dim: DimT, is_batch: c_int) -> c_int;
fn af_release_indexers(indexers: AfIndex) -> c_int;

fn af_index(out: MutAfArray, input: AfArray, ndims: c_uint, index: *const SeqInternal) -> c_int;
fn af_lookup(out: MutAfArray, arr: AfArray, indices: AfArray, dim: c_uint) -> c_int;
fn af_assign_seq(out: MutAfArray, lhs: AfArray, ndims: c_uint, indices: *const SeqInternal, rhs: AfArray) -> c_int;
fn af_index_gen(out: MutAfArray, input: AfArray, ndims: DimT, indices: *const IndexT) -> c_int;
fn af_assign_gen(out: MutAfArray, lhs: AfArray, ndims: DimT, indices: *const IndexT, rhs: AfArray) -> c_int;
fn af_index_gen(out: MutAfArray, input: AfArray, ndims: DimT, indices: AfIndex) -> c_int;
fn af_assign_gen(out: MutAfArray, lhs: AfArray, ndims: DimT, indices: AfIndex, rhs: AfArray) -> c_int;
}

/// Struct to manage an array of resources of type `af_indexer_t`(ArrayFire C struct)
pub struct Indexer {
handle: i64,
count: u32,
}

// Trait that indicates that object can be used for indexing
//
// Any object to be able to be passed on to [./struct.Indexer.html#method.set_index] method
// should implement this trait with appropriate implementation
pub trait Indexable {
fn set(&self, idxr: &Indexer, dim: u32, is_batch: Option<bool>);
fn set(&self, idxr: &mut Indexer, dim: u32, is_batch: Option<bool>);
}

/// Enables [Array](./struct.Array.html) to be used to index another Array
Expand All @@ -41,11 +40,10 @@ pub trait Indexable {
/// [assign_gen](./fn.assign_gen.html)
impl Indexable for Array {
#[allow(unused_variables)]
fn set(&self, idxr: &Indexer, dim: u32, is_batch: Option<bool>) {
fn set(&self, idxr: &mut Indexer, dim: u32, is_batch: Option<bool>) {
unsafe {
let err_val = af_set_array_indexer(idxr.clone().get() as MutAfIndex,
self.get() as AfArray,
dim as DimT);
let err_val = af_set_array_indexer(idxr.get() as AfIndex, self.clone().get() as AfArray,
dim as DimT);
HANDLE_ERROR(AfError::from(err_val));
}
}
Expand All @@ -56,9 +54,9 @@ impl Indexable for Array {
/// This is used in functions [index_gen](./fn.index_gen.html) and
/// [assign_gen](./fn.assign_gen.html)
impl<T: Copy> Indexable for Seq<T> where c_double: From<T> {
fn set(&self, idxr: &Indexer, dim: u32, is_batch: Option<bool>) {
fn set(&self, idxr: &mut Indexer, dim: u32, is_batch: Option<bool>) {
unsafe {
let err_val = af_set_seq_indexer(idxr.clone().get() as MutAfIndex,
let err_val = af_set_seq_indexer(idxr.get() as AfIndex,
&SeqInternal::from_seq(self) as *const SeqInternal,
dim as DimT, is_batch.unwrap() as c_int);
HANDLE_ERROR(AfError::from(err_val));
Expand All @@ -74,33 +72,25 @@ impl Indexer {
let mut temp: i64 = 0;
let err_val = af_create_indexers(&mut temp as MutAfIndex);
HANDLE_ERROR(AfError::from(err_val));
Indexer{handle: temp, count: 0}
Indexer{handle: temp}
}
}

/// Set either [Array](./struct.Array.html) or [Seq](./struct.Seq.html) to index an Array along `idx` dimension
pub fn set_index<T: Indexable>(&mut self, idx: &T, dim: u32, is_batch: Option<bool>) {
self.count = self.count + 1;
idx.set(self, dim, is_batch)
}

/// Get native(ArrayFire) resource handle
pub fn get(&self) -> i64 {
self.handle
}

/// Get number of indexers
///
/// This can be a maximum of four since currently ArrayFire supports maximum of four dimensions
pub fn len(&self) -> u32 {
self.count
}
}

impl Drop for Indexer {
fn drop(&mut self) {
unsafe {
let ret_val = af_release_indexers(self.handle as MutAfIndex);
let ret_val = af_release_indexers(self.handle as AfIndex);
match ret_val {
0 => (),
_ => panic!("Failed to release indexers resource: {}", ret_val),
Expand Down Expand Up @@ -338,7 +328,7 @@ pub fn index_gen(input: &Array, indices: Indexer) -> Array {
unsafe{
let mut temp: i64 = 0;
let err_val = af_index_gen(&mut temp as MutAfArray, input.get() as AfArray,
indices.len() as DimT, indices.get() as *const IndexT);
4, indices.get() as AfIndex);
HANDLE_ERROR(AfError::from(err_val));
Array::from(temp)
}
Expand Down Expand Up @@ -380,7 +370,7 @@ pub fn assign_gen(lhs: &Array, indices: &Indexer, rhs: &Array) -> Array {
unsafe{
let mut temp: i64 = 0;
let err_val = af_assign_gen(&mut temp as MutAfArray, lhs.get() as AfArray,
indices.len() as DimT, indices.get() as *const IndexT,
4, indices.get() as AfIndex,
rhs.get() as AfArray);
HANDLE_ERROR(AfError::from(err_val));
Array::from(temp)
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
html_root_url = "http://arrayfire.com/docs/rust")]
#![warn(missing_docs)]
#![allow(non_camel_case_types)]

#[macro_use]
extern crate lazy_static;
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use self::num::Complex;
use self::libc::{uint8_t, c_int, size_t, c_void};

pub type AfArray = self::libc::c_longlong;
pub type AfIndex = self::libc::c_longlong;
pub type CellPtr = *const self::libc::c_void;
pub type Complex32 = Complex<f32>;
pub type Complex64 = Complex<f64>;
pub type DimT = self::libc::c_longlong;
pub type Feat = *const self::libc::c_void;
pub type IndexT = self::libc::c_longlong;
pub type Intl = self::libc::c_longlong;
pub type MutAfArray = *mut self::libc::c_longlong;
pub type MutAfIndex = *mut self::libc::c_longlong;
Expand Down