Skip to content

Commit bb8f2da

Browse files
committed
Improved data generation API and Array creation API
Defined and implemented a trait `HasAfEnum` for the types supported by ArrayFire. This trait is used by the Array creation and data generation functions now making the function API more terse. Changed the examples to reflect the new modifications.
1 parent b088f07 commit bb8f2da

File tree

12 files changed

+100
-48
lines changed

12 files changed

+100
-48
lines changed

examples/helloworld.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn main() {
1010
let num_rows: u64 = 5;
1111
let num_cols: u64 = 3;
1212
let values: &[f32] = &[1.0, 2.0, 3.0];
13-
let indices = Array::new(Dim4::new(&[3, 1, 1, 1]), values, Aftype::F32).unwrap();
13+
let indices = Array::new(values, Dim4::new(&[3, 1, 1, 1])).unwrap();
1414

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

@@ -74,13 +74,13 @@ fn main() {
7474
println!("Set last row to 1's");
7575
let r_dims = Dim4::new(&[3, 1, 1, 1]);
7676
let r_input: [f32; 3] = [1.0, 1.0, 1.0];
77-
let r = Array::new(r_dims, &r_input, Aftype::F32).unwrap();
77+
let r = Array::new(&r_input, r_dims).unwrap();
7878
print(&set_row(&a, &r, num_rows - 1).unwrap());
7979

8080
println!("Create 2-by-3 matrix from host data");
8181
let d_dims = Dim4::new(&[2, 3, 1, 1]);
8282
let d_input: [i32; 6] = [1, 2, 3, 4, 5, 6];
83-
let d = &Array::new(d_dims, &d_input, Aftype::S32).unwrap();
83+
let d = &Array::new(&d_input, d_dims).unwrap();
8484
print(d);
8585

8686
// printf("Copy last column onto first\n");

examples/pi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ fn main() {
1212
let samples = 20_000_000;
1313
let dims = Dim4::new(&[samples, 1, 1, 1]);
1414

15-
let x = &randu(dims, Aftype::F32).unwrap();
16-
let y = &randu(dims, Aftype::F32).unwrap();
15+
let x = &randu::<f32>(dims).unwrap();
16+
let y = &randu::<f32>(dims).unwrap();
1717

1818
let start = PreciseTime::now();
1919

examples/snow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main() {
1616
let dims = Dim4::new(&[1280, 720, 3, 1]);
1717

1818
loop {
19-
randu(dims, Aftype::F32).as_ref()
19+
randu::<f32>(dims).as_ref()
2020
.map(|arr| wnd.draw_image(arr, None));
2121

2222
if wnd.is_closed().unwrap() == true { break; }

examples/unified.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn test_backend(){
1111
let dims = Dim4::new(&[num_rows, num_cols, 1, 1]);
1212

1313
println!("Create a 10-by-10 matrix of random floats on the compute device");
14-
let a = match randu(dims, Aftype::F32) {
14+
let a = match randu::<f32>(dims) {
1515
Ok(value) => value,
1616
Err(error) => panic!("{}", error),
1717
};

src/array.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ extern crate libc;
22

33
use dim4::Dim4;
44
use defines::{AfError, Aftype, Backend};
5+
use util::HasAfEnum;
56
use self::libc::{uint8_t, c_void, c_int, c_uint, c_longlong};
67

78
type MutAfArray = *mut self::libc::c_longlong;
@@ -119,11 +120,12 @@ impl Array {
119120
///
120121
/// ```
121122
/// let values: &[f32] = &[1.0, 2.0, 3.0];
122-
/// let indices = Array::new(Dim4::new(&[3, 1, 1, 1]), values, Aftype::F32).unwrap();
123+
/// let indices = Array::new(values, Dim4::new(&[3, 1, 1, 1])).unwrap();
123124
/// ```
124125
#[allow(unused_mut)]
125-
pub fn new<T>(dims: Dim4, slice: &[T], aftype: Aftype) -> Result<Array, AfError> {
126+
pub fn new<T: HasAfEnum>(slice: &[T], dims: Dim4) -> Result<Array, AfError> {
126127
unsafe {
128+
let aftype = T::get_af_dtype();
127129
let mut temp: i64 = 0;
128130
let err_val = af_create_array(&mut temp as MutAfArray,
129131
slice.as_ptr() as *const c_void,
@@ -140,18 +142,11 @@ impl Array {
140142
/// Constructs a new Array object from strided data
141143
///
142144
/// This data can possiblly offseted using an additiona `offset` parameter.
143-
///
144-
/// # Examples
145-
///
146-
/// ```
147-
/// let values: &[f32] = &[1.0, 2.0, 3.0];
148-
/// let indices = Array::new(Dim4::new(&[3, 1, 1, 1]), values, Aftype::F32).unwrap();
149-
/// ```
150145
#[allow(unused_mut)]
151-
pub fn new_strided<T>(slice: &[T], offset: i64,
152-
dims: Dim4, strides: Dim4,
153-
aftype: Aftype) -> Result<Array, AfError> {
146+
pub fn new_strided<T: HasAfEnum>(slice: &[T], offset: i64,
147+
dims: Dim4, strides: Dim4) -> Result<Array, AfError> {
154148
unsafe {
149+
let aftype = T::get_af_dtype();
155150
let mut temp: i64 = 0;
156151
let err_val = af_create_strided_array(&mut temp as MutAfArray,
157152
slice.as_ptr() as *const c_void,
@@ -342,10 +337,11 @@ impl Array {
342337
is_func!(is_owner, af_is_owner);
343338

344339
/// Cast the Array data type to `target_type`
345-
pub fn cast(&self, target_type: Aftype) -> Result<Array, AfError> {
340+
pub fn cast<T: HasAfEnum>(&self) -> Result<Array, AfError> {
346341
unsafe {
342+
let trgt_type = T::get_af_dtype();
347343
let mut temp: i64 = 0;
348-
let err_val = af_cast(&mut temp as MutAfArray, self.handle as AfArray, target_type as uint8_t);
344+
let err_val = af_cast(&mut temp as MutAfArray, self.handle as AfArray, trgt_type as uint8_t);
349345
match err_val {
350346
0 => Ok(Array::from(temp)),
351347
_ => Err(AfError::from(err_val)),
@@ -394,7 +390,7 @@ impl Drop for Array {
394390
///
395391
/// ```
396392
/// println!("Create a 5-by-3 matrix of random floats on the GPU");
397-
/// let a = match randu(dims, Aftype::F32) {
393+
/// let a = match randu::<f32>(dims) {
398394
/// Ok(value) => value,
399395
/// Err(error) => panic!("{}", error),
400396
/// };

src/data/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ extern crate num;
44
use array::Array;
55
use dim4::Dim4;
66
use defines::AfError;
7-
use defines::Aftype;
87
use self::libc::{uint8_t, c_int, c_uint, c_double};
98
use self::num::Complex;
10-
9+
use util::HasAfEnum;
1110
use std::vec::Vec;
1211

1312
type MutAfArray = *mut self::libc::c_longlong;
@@ -220,15 +219,15 @@ pub fn constant<T : ConstGenerator>(cnst: T, dims: Dim4) -> Result<Array, AfErro
220219
/// - `dims` is the size of Array
221220
/// - `seq_dim` is the dimension along which range values are populated, all values along other
222221
/// dimensions are just repeated
223-
/// - `aftype` is the type of the Array.
224222
///
225223
/// # Return Values
226224
/// Array
227225
#[allow(unused_mut)]
228-
pub fn range(dims: Dim4, seq_dim: i32, aftype: Aftype) -> Result<Array, AfError> {
226+
pub fn range<T: HasAfEnum>(dims: Dim4, seq_dim: i32) -> Result<Array, AfError> {
229227
unsafe {
228+
let aftype = T::get_af_dtype();
230229
let mut temp: i64 = 0;
231-
let err_val =af_range(&mut temp as MutAfArray,
230+
let err_val = af_range(&mut temp as MutAfArray,
232231
dims.ndims() as c_uint, dims.get().as_ptr() as *const DimT,
233232
seq_dim as c_int, aftype as uint8_t);
234233
match err_val {
@@ -246,14 +245,14 @@ pub fn range(dims: Dim4, seq_dim: i32, aftype: Aftype) -> Result<Array, AfError>
246245
///
247246
/// - `dims` is the dimensions of the sequence to be generated
248247
/// - `tdims` is the number of repitions of the unit dimensions
249-
/// - `aftype` is the type of Array to generate
250248
///
251249
/// # Return Values
252250
///
253251
/// Array
254252
#[allow(unused_mut)]
255-
pub fn iota(dims: Dim4, tdims: Dim4, aftype: Aftype) -> Result<Array, AfError> {
253+
pub fn iota<T: HasAfEnum>(dims: Dim4, tdims: Dim4) -> Result<Array, AfError> {
256254
unsafe {
255+
let aftype = T::get_af_dtype();
257256
let mut temp: i64 = 0;
258257
let err_val =af_iota(&mut temp as MutAfArray,
259258
dims.ndims() as c_uint, dims.get().as_ptr() as *const DimT,
@@ -293,8 +292,9 @@ pub fn get_seed() -> Result<u64, AfError> {
293292
macro_rules! data_gen_def {
294293
($fn_name:ident, $ffi_name: ident) => (
295294
#[allow(unused_mut)]
296-
pub fn $fn_name(dims: Dim4, aftype: Aftype) -> Result<Array, AfError> {
295+
pub fn $fn_name<T: HasAfEnum>(dims: Dim4) -> Result<Array, AfError> {
297296
unsafe {
297+
let aftype = T::get_af_dtype();
298298
let mut temp: i64 = 0;
299299
let err_val = $ffi_name(&mut temp as MutAfArray,
300300
dims.ndims() as c_uint, dims.get().as_ptr() as *const DimT,
@@ -412,9 +412,9 @@ pub fn join_many(dim: i32, inputs: Vec<&Array>) -> Result<Array, AfError> {
412412
/// ```
413413
/// # #[macro_use] extern crate arrayfire;
414414
/// # fn main() {
415-
/// let a = randu(Dim4::new(&[5, 3, 1, 1]), Aftype::F32).unwrap();
416-
/// let b = randu(Dim4::new(&[5, 3, 1, 1]), Aftype::F32).unwrap();
417-
/// let c = randu(Dim4::new(&[5, 3, 1, 1]), Aftype::F32).unwrap();
415+
/// let a = randu::<f32>(Dim4::new(&[5, 3, 1, 1])).unwrap();
416+
/// let b = randu::<f32>(Dim4::new(&[5, 3, 1, 1])).unwrap();
417+
/// let c = randu::<f32>(Dim4::new(&[5, 3, 1, 1])).unwrap();
418418
/// let d = join_many![2, a, b, c];
419419
/// # }
420420
/// ```

src/defines.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub enum AfError {
4949
ERR_UNKNOWN = 999
5050
}
5151

52+
/// Compute/Acceleration Backend
5253
#[repr(C)]
5354
#[derive(Clone, Copy, Debug, PartialEq)]
5455
pub enum Backend {

src/image/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ extern crate libc;
22

33
use array::Array;
44
use defines::AfError;
5-
use defines::Aftype;
65
use defines::BorderType;
76
use defines::ColorSpace;
87
use defines::Connectivity;
98
use defines::InterpType;
109
use defines::YCCStd;
10+
use util::HasAfEnum;
1111
use self::libc::{uint8_t, c_uint, c_int, c_float, c_double};
1212

1313
type MutAfArray = *mut self::libc::c_longlong;
@@ -812,14 +812,14 @@ pub fn color_space(input: &Array,
812812
///
813813
/// - `input` is the input image
814814
/// - `conn` can take one of the values of [Connectivity](./enum.Connectivity.html)
815-
/// - `aftype` can take one of the values of [Aftype](./enum.Aftype.html)
816815
///
817816
/// # Return Values
818817
///
819818
/// Array with labels indicating different regions
820819
#[allow(unused_mut)]
821-
pub fn regions(input: &Array, conn: Connectivity, aftype: Aftype) -> Result<Array, AfError> {
820+
pub fn regions<T: HasAfEnum>(input: &Array, conn: Connectivity) -> Result<Array, AfError> {
822821
unsafe {
822+
let aftype = T::get_af_dtype();
823823
let mut temp: i64 = 0;
824824
let err_val = af_regions(&mut temp as MutAfArray, input.get() as AfArray,
825825
conn as uint8_t, aftype as uint8_t);

src/index.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl Drop for Indexer {
125125
/// # Examples
126126
///
127127
/// ```
128-
/// let a = randu(dims, Aftype::F32).unwrap();
128+
/// let a = randu::<f32>(dims).unwrap();
129129
/// let seqs = &[Seq::new(1.0, 3.0, 1.0), Seq::default()];
130130
/// let sub = index(&a, seqs).unwrap();
131131
/// println!("a(seq(1, 3, 1), span)");
@@ -153,7 +153,7 @@ pub fn index<T: Copy>(input: &Array, seqs: &[Seq<T>]) -> Result<Array, AfError>
153153
/// # Examples
154154
///
155155
/// ```
156-
/// let a = randu(dims, Aftype::F32).unwrap();
156+
/// let a = randu::<f32>(dims).unwrap();
157157
/// println!("Grab last row of the random matrix");
158158
/// print(&a);
159159
/// print(&row(&a, num_rows - 1).unwrap());
@@ -189,7 +189,7 @@ pub fn set_rows(input: &Array, new_rows: &Array, first: u64, last: u64) -> Resul
189189
/// # Examples
190190
///
191191
/// ```
192-
/// let a = randu(dims, Aftype::F32).unwrap();
192+
/// let a = randu::<f32>(dims).unwrap();
193193
/// println!("Grab last col of the random matrix");
194194
/// print(&a);
195195
/// print(&row(&a, num_cols - 1).unwrap());
@@ -328,9 +328,9 @@ pub fn assign_seq<T: Copy>(lhs: &Array, seqs: &[Seq<T>], rhs: &Array) -> Result<
328328
///
329329
/// ```
330330
/// let values: &[f32] = &[1.0, 2.0, 3.0];
331-
/// let indices = Array::new(Dim4::new(&[3, 1, 1, 1]), values, Aftype::F32).unwrap();
331+
/// let indices = Array::new(values, Dim4::new(&[3, 1, 1, 1])).unwrap();
332332
/// let seq4gen = Seq::new(0.0, 2.0, 1.0);
333-
/// let a = randu(Dim4::new(&[5, 3, 1, 1]), Aftype::F32).unwrap();
333+
/// let a = randu::<f32>(Dim4::new(&[5, 3, 1, 1])).unwrap();
334334
/// // [5 3 1 1]
335335
/// // 0.0000 0.2190 0.3835
336336
/// // 0.1315 0.0470 0.5194
@@ -371,9 +371,9 @@ pub fn index_gen(input: &Array, indices: Indexer) -> Result<Array, AfError> {
371371
///
372372
/// ```
373373
/// let values: &[f32] = &[1.0, 2.0, 3.0];
374-
/// let indices = Array::new(Dim4::new(&[3, 1, 1, 1]), values, Aftype::F32).unwrap();
374+
/// let indices = Array::new(values, Dim4::new(&[3, 1, 1, 1])).unwrap();
375375
/// let seq4gen = Seq::new(0.0, 2.0, 1.0);
376-
/// let a = randu(Dim4::new(&[5, 3, 1, 1]), Aftype::F32).unwrap();
376+
/// let a = randu::<f32>(Dim4::new(&[5, 3, 1, 1])).unwrap();
377377
/// // [5 3 1 1]
378378
/// // 0.0000 0.2190 0.3835
379379
/// // 0.1315 0.0470 0.5194

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pub use statistics::{var_all, mean_all, stdev_all, median_all};
9191
pub use statistics::{mean_all_weighted, var_all_weighted};
9292
mod statistics;
9393

94+
pub use util::{HasAfEnum};
9495
mod util;
9596

9697
pub use vision::Features;

0 commit comments

Comments
 (0)