Skip to content

New data generation functions and Neg trait implementation #94

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 3 commits into from
Jan 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
30 changes: 28 additions & 2 deletions src/arith/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ extern crate num;

use dim4::Dim4;
use array::Array;
use defines::AfError;
use defines::{AfError, DType, Scalar};
use error::HANDLE_ERROR;
use self::libc::{c_int};
use data::{constant, tile};
use data::{constant, constant_t, tile};
use self::num::Complex;

use std::ops::Neg;

type Complex32 = Complex<f32>;
type Complex64 = Complex<f64>;
type MutAfArray = *mut self::libc::c_longlong;
type MutDouble = *mut self::libc::c_double;
type MutUint = *mut self::libc::c_uint;
Expand Down Expand Up @@ -485,3 +489,25 @@ bit_assign_func!(BitOrAssign, bitor_assign, bitor);
bit_assign_func!(BitXorAssign, bitxor_assign, bitxor);

}

///Implement negation trait for Array
impl Neg for Array {
type Output = Array;

fn neg(self) -> Self::Output {
match self.get_type() {
DType::S64 => (constant_t(Scalar::S64(0 as i64), self.dims(), DType::S64) - self),
DType::U64 => (constant_t(Scalar::U64(0 as u64), self.dims(), DType::U64) - self),
DType::C32 => (constant_t(Scalar::C32(Complex32::new(0.0, 0.0)), self.dims(), DType::C32) - self),
DType::C64 => (constant_t(Scalar::C64(Complex64::new(0.0, 0.0)), self.dims(), DType::C64) - self),
DType::F32 => (constant_t(Scalar::F32(0 as f32), self.dims(), DType::F32) - self),
DType::F64 => (constant_t(Scalar::F64(0 as f64), self.dims(), DType::F64) - self),
DType::B8 => (constant_t(Scalar::B8 (false ), self.dims(), DType::B8 ) - self),
DType::S32 => (constant_t(Scalar::S32(0 as i32), self.dims(), DType::S32) - self),
DType::U32 => (constant_t(Scalar::U32(0 as u32), self.dims(), DType::U32) - self),
DType::U8 => (constant_t(Scalar::U8 (0 as u8 ), self.dims(), DType::U8 ) - self),
DType::S16 => (constant_t(Scalar::S16(0 as i16), self.dims(), DType::S16) - self),
DType::U16 => (constant_t(Scalar::U16(0 as u16), self.dims(), DType::U16) - self),
}
}
}
138 changes: 137 additions & 1 deletion src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate num;

use array::Array;
use dim4::Dim4;
use defines::AfError;
use defines::{AfError, DType, Scalar};
use error::HANDLE_ERROR;
use self::libc::{uint8_t, c_int, c_uint, c_double};
use self::num::Complex;
Expand Down Expand Up @@ -622,3 +622,139 @@ pub fn replace_scalar(a: &mut Array, cond: &Array, b: f64) {
HANDLE_ERROR(AfError::from(err_val));
}
}

/// Create a range of values of given type([DType](./enum.DType.html))
///
/// Creates an array with [0, n] values along the `seq_dim` which is tiled across other dimensions.
///
/// # Parameters
///
/// - `dims` is the size of Array
/// - `seq_dim` is the dimension along which range values are populated, all values along other
/// dimensions are just repeated
/// - `dtype` indicates whats the type of the Array to be created
///
/// # Return Values
/// Array
#[allow(unused_mut)]
pub fn range_t(dims: Dim4, seq_dim: i32, dtype: DType) -> Array {
unsafe {
let mut temp: i64 = 0;
let err_val = af_range(&mut temp as MutAfArray,
dims.ndims() as c_uint, dims.get().as_ptr() as *const DimT,
seq_dim as c_int, dtype as uint8_t);
HANDLE_ERROR(AfError::from(err_val));
Array::from(temp)
}
}

/// Create a range of values of given type([DType](./enum.DType.html))
///
/// Create an sequence [0, dims.elements() - 1] and modify to specified dimensions dims and then tile it according to tile_dims.
///
/// # Parameters
///
/// - `dims` is the dimensions of the sequence to be generated
/// - `tdims` is the number of repitions of the unit dimensions
/// - `dtype` indicates whats the type of the Array to be created
///
/// # Return Values
///
/// Array
#[allow(unused_mut)]
pub fn iota_t(dims: Dim4, tdims: Dim4, dtype: DType) -> Array {
unsafe {
let mut temp: i64 = 0;
let err_val =af_iota(&mut temp as MutAfArray,
dims.ndims() as c_uint, dims.get().as_ptr() as *const DimT,
tdims.ndims() as c_uint, tdims.get().as_ptr() as *const DimT,
dtype as uint8_t);
HANDLE_ERROR(AfError::from(err_val));
Array::from(temp)
}
}

/// Create an identity array with 1's in diagonal of given type([DType](./enum.DType.html))
///
/// # Parameters
///
/// - `dims` is the output Array dimensions
/// - `dtype` indicates whats the type of the Array to be created
///
/// # Return Values
///
/// Identity matrix
#[allow(unused_mut)]
pub fn identity_t(dims: Dim4, dtype: DType) -> Array {
unsafe {
let mut temp: i64 = 0;
let err_val = af_identity(&mut temp as MutAfArray,
dims.ndims() as c_uint, dims.get().as_ptr() as *const DimT,
dtype as uint8_t);
HANDLE_ERROR(AfError::from(err_val));
Array::from(temp)
}
}

/// Create a constant array of given type([DType](./enum.DType.html))
///
/// You can use this function to create arrays of type dictated by the enum
/// [DType](./enum.DType.html) using the scalar `value` that has the shape similar
/// to `dims`.
///
/// # Parameters
///
/// - `value` is the [Scalar](./enum.Scalar.html) to be filled into the array
/// - `dims` is the output Array dimensions
/// - `dtype` indicates the type of Array to be created and is the type of the scalar to be passed
/// via the paramter `value`.
///
/// # Return Values
///
/// Array of `dims` shape and filed with given constant `value`.
#[allow(unused_mut)]
pub fn constant_t(value: Scalar, dims: Dim4, dtype: DType) -> Array {
use Scalar::*;

// Below macro is only visible to this function
// and it is used to abbreviate the repetitive const calls
macro_rules! expand_const_call {
($ffi_name: ident, $temp: expr, $v: expr, $dims: expr, $dt: expr) => ({
$ffi_name(&mut $temp as MutAfArray, $v as c_double,
$dims.ndims() as c_uint, $dims.get().as_ptr() as *const DimT, $dt)
})
}

unsafe {
let dt = dtype as c_int;
let mut temp: i64 = 0;
let err_val = match value {
C32(v) => {
af_constant_complex(&mut temp as MutAfArray, v.re as c_double, v.im as c_double,
dims.ndims() as c_uint, dims.get().as_ptr() as *const DimT, dt)
},
C64(v) => {
af_constant_complex(&mut temp as MutAfArray, v.re as c_double, v.im as c_double,
dims.ndims() as c_uint, dims.get().as_ptr() as *const DimT, dt)
},
S64(v) => {
af_constant_long(&mut temp as MutAfArray, v as Intl,
dims.ndims() as c_uint, dims.get().as_ptr() as *const DimT)
},
U64(v) => {
af_constant_ulong(&mut temp as MutAfArray, v as Uintl,
dims.ndims() as c_uint, dims.get().as_ptr() as *const DimT)
},
F32(v) => expand_const_call!(af_constant, temp, v, dims, dt),
F64(v) => expand_const_call!(af_constant, temp, v, dims, dt),
B8(v) => expand_const_call!(af_constant, temp, v as i32, dims, dt),
S32(v) => expand_const_call!(af_constant, temp, v, dims, dt),
U32(v) => expand_const_call!(af_constant, temp, v, dims, dt),
U8(v) => expand_const_call!(af_constant, temp, v, dims, dt),
S16(v) => expand_const_call!(af_constant, temp, v, dims, dt),
U16(v) => expand_const_call!(af_constant, temp, v, dims, dt),
};
HANDLE_ERROR(AfError::from(err_val));
Array::from(temp)
}
}
32 changes: 32 additions & 0 deletions src/defines.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
extern crate num;

use std::error::Error;
use std::fmt::{Display, Formatter};
use std::fmt::Error as FmtError;
use self::num::Complex;

/// Error codes
#[repr(C)]
Expand Down Expand Up @@ -397,3 +400,32 @@ pub const PHILOX : RandomEngineType = RandomEngineType::PHILOX_4X32_10;
pub const THREEFRY : RandomEngineType = RandomEngineType::THREEFRY_2X32_16;
pub const MERSENNE : RandomEngineType = RandomEngineType::MERSENNE_GP11213;
pub const DEFAULT_RANDOM_ENGINE : RandomEngineType = PHILOX;

/// Scalar value types
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Scalar {
/// 32 bit float
F32(f32),
/// 32 bit complex float
C32(Complex<f32>),
/// 64 bit float
F64(f64),
/// 64 bit complex float
C64(Complex<f64>),
/// 8 bit boolean
B8(bool),
/// 32 bit signed integer
S32(i32),
/// 32 bit unsigned integer
U32(u32),
/// 8 bit unsigned integer
U8(u8),
/// 64 bit signed integer
S64(i64),
/// 64 bit unsigned integer
U64(u64),
/// 16 bit signed integer
S16(i16),
/// 16 bit unsigned integer
U16(u16),
}
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ mod backend;
pub use blas::{matmul, dot, transpose, transpose_inplace};
mod blas;

pub use data::{constant, range, iota};
pub use data::{identity, diag_create, diag_extract, lower, upper};
pub use data::{constant, range, iota, identity};
pub use data::{diag_create, diag_extract, lower, upper};
pub use data::{join, join_many, tile};
pub use data::{reorder, shift, moddims, flat, flip};
pub use data::{select, selectl, selectr, replace, replace_scalar};
pub use data::{range_t, iota_t, identity_t, constant_t};
mod data;

pub use device::{get_version, info, init, device_count, is_double_available, set_device, get_device};
Expand All @@ -47,7 +48,7 @@ pub use defines::{DType, AfError, Backend, ColorMap, YCCStd, HomographyType};
pub use defines::{InterpType, BorderType, MatchType, NormType};
pub use defines::{Connectivity, ConvMode, ConvDomain, ColorSpace, MatProp};
pub use defines::{MarkerType, MomentType, SparseFormat, BinaryOp, RandomEngineType};
pub use defines::{PHILOX, THREEFRY, MERSENNE, DEFAULT_RANDOM_ENGINE};
pub use defines::{PHILOX, THREEFRY, MERSENNE, DEFAULT_RANDOM_ENGINE, Scalar};
mod defines;

pub use dim4::Dim4;
Expand Down