From 6b45f71cefd7b70006816d4207237433aaa58a9c Mon Sep 17 00:00:00 2001 From: Jason Ramapuram Date: Mon, 31 Aug 2015 01:33:40 +0200 Subject: [PATCH 1/9] initial index commit --- src/index.rs | 159 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 6 ++ src/seq.rs | 44 ++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 src/index.rs create mode 100644 src/seq.rs diff --git a/src/index.rs b/src/index.rs new file mode 100644 index 000000000..d2d49d1be --- /dev/null +++ b/src/index.rs @@ -0,0 +1,159 @@ +extern crate libc; + +use array::Array; +use defines::AfError; +use dim4::Dim4; +use seq::Seq; +use defines::Aftype; +use self::libc::{uint8_t, c_void, c_int, c_uint, c_longlong}; + +type MutAfArray = *mut self::libc::c_longlong; +type MutAfSeq = *mut Seq; +type MutMutAfIndex = *mut *mut self::libc::c_longlong; +type MutAfIndex = *mut self::libc::c_longlong; +type MutDouble = *mut self::libc::c_double; +type MutUint = *mut self::libc::c_uint; +type AfArray = self::libc::c_longlong; +type AfIndex = self::libc::c_longlong; +type DimT = self::libc::c_longlong; +type SeqT = self::libc::c_longlong; +type IndexT = self::libc::c_longlong; + +#[allow(dead_code)] +extern { + fn af_create_seq_index(result: MutMutAfIndex, input: *const Seq, is_batch: c_int) -> c_int; + fn af_create_array_index(result: MutMutAfIndex, input: AfArray) -> c_int; + fn af_release_index(indexer: MutAfIndex) -> c_int; + fn af_index(out: MutAfArray, input: AfArray, ndims: c_uint, index: *const IndexT) -> 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 IndexT, 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; +} + +pub struct Index { + handle: i64, + is_batch: bool, + is_seq: bool, +} + +impl Index { + #[allow(unused_mut)] + pub fn new(arr: Option, seq: Option, is_batch: bool) -> Result { + unsafe { + let mut err_val: c_int = 0; + let mut temp: i64 = 0; + let mut is_seq = false; + err_val = match arr { + //c_func(&mut (x as *mut libc::c_void)); --> (&mut x) as *mut _ as *mut *mut libc::c_void. + //&mut temp as MutMutAfIndex, x), + Some(mut x) => { is_seq = false; af_create_array_index((&mut temp) as *mut _ as MutMutAfIndex, x.get() as AfArray) }, + None => 0, + }; + + err_val = match seq { + Some(mut x) => { is_seq = true; af_create_seq_index((&mut temp) as *mut _ as MutMutAfIndex, &mut x, is_batch as c_int) }, + None => AfError::ERR_UNKNOWN as c_int, + }; + + match err_val { + 0 => Ok(Index {handle: temp, is_batch: is_batch, is_seq: is_seq}), + _ => Err(AfError::from(err_val)), + } + } + } + + pub fn index(&self, input: Array, seqs: &[Seq]) -> Result { + unsafe { + let mut temp: i64 = 0; + let err_val = af_index(&mut temp as MutAfArray + , input.get() as AfArray, seqs.len() as u32 + , seqs.as_ptr() as *const SeqT); + match err_val { + 0 => Ok(Array::from(temp)), + _ => Err(AfError::from(err_val)), + } + } + } + + pub fn lookup(&self, input: Array, indices: Array, seq_dim: i32) -> Result { + unsafe { + let mut temp: i64 = 0; + let err_val = af_lookup(&mut temp as MutAfArray + , input.get() as AfArray + , indices.get() as AfArray + , seq_dim as c_uint); + match err_val { + 0 => Ok(Array::from(temp)), + _ => Err(AfError::from(err_val)), + } + } + } + + pub fn assign_seq(&self, lhs: Array, ndims: usize, seq_dim: i32, seqs: &[Seq], rhs: Array) -> Result { + unsafe{ + let mut temp: i64 = 0; + let err_val = af_assign_seq(&mut temp as MutAfArray + , lhs.get() as AfArray + , ndims as c_uint, seqs.as_ptr() as *const IndexT + , rhs.get() as AfArray); + match err_val { + 0 => Ok(Array::from(temp)), + _ => Err(AfError::from(err_val)), + } + } + } + + pub fn index_gen(&self, input: Array, ndims: Dim4, indices: &[Index]) -> Result { + unsafe{ + let mut temp: i64 = 0; + let err_val = af_index_gen(&mut temp as MutAfArray + , input.get() as AfArray + , ndims.get().as_ptr() as DimT + , indices.as_ptr() as *const IndexT); + match err_val { + 0 => Ok(Array::from(temp)), + _ => Err(AfError::from(err_val)), + } + } + } + + pub fn assign_gen(&self, lhs: Array, ndims: Dim4, indices: &[Index], rhs: Array) -> Result { + unsafe{ + let mut temp: i64 = 0; + let err_val = af_assign_gen(&mut temp as MutAfArray + , lhs.get() as AfArray + , ndims.get().as_ptr() as DimT + , indices.as_ptr() as *const IndexT + , rhs.get() as AfArray); + match err_val { + 0 => Ok(Array::from(temp)), + _ => Err(AfError::from(err_val)), + } + } + } + + pub fn get(&self) -> i64 { + self.handle + } + + pub fn is_seq(&self) -> bool{ + self.is_seq + } + + pub fn is_batch(&self) -> bool{ + self.is_seq + } +} + +impl Drop for Index { + fn drop(&mut self) { + unsafe { + let ret_val = af_release_index(self.handle as MutAfIndex); + match ret_val { + 0 => (), + _ => panic!("Failed to destruct Index: {}", ret_val), + } + } + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 48f6218d5..48b35f927 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,6 +44,12 @@ mod defines; pub use dim4::Dim4; mod dim4; +pub use index::Index; +mod index; + +pub use seq::Seq; +mod seq; + pub use graphics::Window; mod graphics; diff --git a/src/seq.rs b/src/seq.rs new file mode 100644 index 000000000..348bcc386 --- /dev/null +++ b/src/seq.rs @@ -0,0 +1,44 @@ +extern crate libc; + +use std::fmt; +use std::ops::Index; +use std::default::Default; +use self::libc::{uint8_t, c_void, c_int, c_uint, c_longlong, c_double}; + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct Seq { + begin: c_double, + end: c_double, + step: c_double, +} + +impl Default for Seq { + fn default() -> Seq { + Seq { begin: 0.0, end: 0.0, step: 0.0, } + } +} + +impl fmt::Display for Seq { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "[begin: {}, end: {}, step: {}", self.begin, self.end, self.step) + } +} + +impl Seq { + pub fn new(begin: f64, end: f64, step: f64) -> Seq { + Seq { begin: begin, end: end, step: step, } + } + + pub fn begin(&self) -> f64 { + self.begin as f64 + } + + pub fn end(&self) -> f64 { + self.end as f64 + } + + pub fn step(&self) -> f64 { + self.step as f64 + } +} From 67c3f98bd808ac86339b73b9bb8d2d33fdd82b01 Mon Sep 17 00:00:00 2001 From: Jason Ramapuram Date: Mon, 31 Aug 2015 18:03:14 +0200 Subject: [PATCH 2/9] more cleanup, pull out index member functions and add col and row helpers --- examples/helloworld.rs | 11 ++++++++--- src/lib.rs | 2 +- src/seq.rs | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/helloworld.rs b/examples/helloworld.rs index e6dff241a..7f060dd64 100644 --- a/examples/helloworld.rs +++ b/examples/helloworld.rs @@ -7,7 +7,10 @@ fn main() { set_device(0); info(); - let dims = Dim4::new(&[5, 3, 1, 1]); + let num_rows: u64 = 5; + let num_columns: u64 = 3; + + let dims = Dim4::new(&[num_rows, num_columns, 1, 1]); println!("Create a 5-by-3 matrix of random floats on the GPU"); let a = match randu(dims, Aftype::F32) { @@ -43,8 +46,10 @@ fn main() { println!("Fourier transform the result"); fft(&b, 1.0, 0).map(|x| print(&x)); - // printf("Grab last row\n"); - // array c = C.row(end); + println!("Grab last row"); + let c = row(&a, 4).unwrap(); + print(&c); + //array c = C.row(end); // af_print(c); println!("Create 2-by-3 matrix from host data"); diff --git a/src/lib.rs b/src/lib.rs index 48b35f927..de3d0a5cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,7 +44,7 @@ mod defines; pub use dim4::Dim4; mod dim4; -pub use index::Index; +pub use index::{Index, index, row, col, lookup, assign_seq, index_gen, assign_gen}; mod index; pub use seq::Seq; diff --git a/src/seq.rs b/src/seq.rs index 348bcc386..d7005e0b5 100644 --- a/src/seq.rs +++ b/src/seq.rs @@ -3,7 +3,7 @@ extern crate libc; use std::fmt; use std::ops::Index; use std::default::Default; -use self::libc::{uint8_t, c_void, c_int, c_uint, c_longlong, c_double}; +use self::libc::{c_double}; #[derive(Copy, Clone)] #[repr(C)] From 417c5810e1f1b23c06aa8cef141dddf92850d213 Mon Sep 17 00:00:00 2001 From: Jason Ramapuram Date: Mon, 31 Aug 2015 18:06:13 +0200 Subject: [PATCH 3/9] forgot to push the actual index changes --- src/index.rs | 204 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 132 insertions(+), 72 deletions(-) diff --git a/src/index.rs b/src/index.rs index d2d49d1be..c3d93a962 100644 --- a/src/index.rs +++ b/src/index.rs @@ -5,7 +5,7 @@ use defines::AfError; use dim4::Dim4; use seq::Seq; use defines::Aftype; -use self::libc::{uint8_t, c_void, c_int, c_uint, c_longlong}; +use self::libc::{c_int, c_uint, c_longlong}; type MutAfArray = *mut self::libc::c_longlong; type MutAfSeq = *mut Seq; @@ -39,7 +39,7 @@ pub struct Index { impl Index { #[allow(unused_mut)] - pub fn new(arr: Option, seq: Option, is_batch: bool) -> Result { + pub fn new(arr: Option, seq: Option, is_batch: bool) -> Result { unsafe { let mut err_val: c_int = 0; let mut temp: i64 = 0; @@ -63,97 +63,157 @@ impl Index { } } - pub fn index(&self, input: Array, seqs: &[Seq]) -> Result { - unsafe { - let mut temp: i64 = 0; - let err_val = af_index(&mut temp as MutAfArray - , input.get() as AfArray, seqs.len() as u32 - , seqs.as_ptr() as *const SeqT); - match err_val { - 0 => Ok(Array::from(temp)), - _ => Err(AfError::from(err_val)), - } - } + pub fn get(&self) -> i64 { + self.handle } - pub fn lookup(&self, input: Array, indices: Array, seq_dim: i32) -> Result { + pub fn is_seq(&self) -> bool{ + self.is_seq + } + + pub fn is_batch(&self) -> bool{ + self.is_batch + } +} + +impl Drop for Index { + fn drop(&mut self) { unsafe { - let mut temp: i64 = 0; - let err_val = af_lookup(&mut temp as MutAfArray - , input.get() as AfArray - , indices.get() as AfArray - , seq_dim as c_uint); - match err_val { - 0 => Ok(Array::from(temp)), - _ => Err(AfError::from(err_val)), + let ret_val = af_release_index(self.handle as MutAfIndex); + match ret_val { + 0 => (), + _ => panic!("Failed to destruct Index: {}", ret_val), } } } +} - pub fn assign_seq(&self, lhs: Array, ndims: usize, seq_dim: i32, seqs: &[Seq], rhs: Array) -> Result { - unsafe{ - let mut temp: i64 = 0; - let err_val = af_assign_seq(&mut temp as MutAfArray - , lhs.get() as AfArray - , ndims as c_uint, seqs.as_ptr() as *const IndexT - , rhs.get() as AfArray); - match err_val { - 0 => Ok(Array::from(temp)), - _ => Err(AfError::from(err_val)), - } +pub fn index(input: &Array, seqs: &[Seq]) -> Result { + unsafe { + let mut temp: i64 = 0; + let err_val = af_index(&mut temp as MutAfArray + , input.get() as AfArray, seqs.len() as u32 + , seqs.as_ptr() as *const SeqT); + match err_val { + 0 => Ok(Array::from(temp)), + _ => Err(AfError::from(err_val)), } } +} - pub fn index_gen(&self, input: Array, ndims: Dim4, indices: &[Index]) -> Result { - unsafe{ - let mut temp: i64 = 0; - let err_val = af_index_gen(&mut temp as MutAfArray - , input.get() as AfArray - , ndims.get().as_ptr() as DimT - , indices.as_ptr() as *const IndexT); - match err_val { - 0 => Ok(Array::from(temp)), - _ => Err(AfError::from(err_val)), - } - } +pub fn row(input: &Array, row_num: u64) -> Result { + let dims_err = input.dims(); + let mut dims = [0, 0, 0, 0]; + match dims_err { + Ok(dim) => dims = dim.get().clone(), + Err(e) => panic!("Error unwrapping dims in row(): {}", e), } - pub fn assign_gen(&self, lhs: Array, ndims: Dim4, indices: &[Index], rhs: Array) -> Result { - unsafe{ - let mut temp: i64 = 0; - let err_val = af_assign_gen(&mut temp as MutAfArray - , lhs.get() as AfArray - , ndims.get().as_ptr() as DimT - , indices.as_ptr() as *const IndexT - , rhs.get() as AfArray); - match err_val { - 0 => Ok(Array::from(temp)), - _ => Err(AfError::from(err_val)), - } - } + let array_end = dims[0] as f64 * dims[1] as f64 * dims[2] as f64 * dims[3] as f64; + let row_begin: f64 = dims[1] as f64 * row_num as f64; + assert!(row_begin <= array_end - dims[1] as f64); + + let row_end: f64 = row_begin + dims[1] as f64 - 1.0; + assert!(row_end <= array_end); + + let index = Index::new(None, Some(Seq::new(row_begin, row_end, 1.0)), false); + println!("origin size {} x {} x {} x {}", dims[0], dims[1], dims[2], dims[3]); + println!("passed index gen from {} to {}", row_begin, row_end); + + match index { + Ok(i) => { println!("index raw handle: {}", i.get()); index_gen(input, Dim4::new(&[1, 1, 1, 1]), &[i])}, + Err(e) => Err(AfError::from(e)), } +} - pub fn get(&self) -> i64 { - self.handle +pub fn col(input: &Array, col_num: u64) -> Result { + let dims_err = input.dims(); + let mut dims = [0, 0, 0, 0]; + match dims_err { + Ok(dim) => dims = dim.get().clone(), + Err(e) => panic!("Error unwrapping dims in col(): {}", e), } - pub fn is_seq(&self) -> bool{ - self.is_seq + let input_type_err = input.get_type(); + let mut input_type = Aftype::F32; + match input_type_err { + Ok(t) => input_type = t, + Err(e) => panic!("Error unwrapping type in col(): {}", e), } - pub fn is_batch(&self) -> bool{ - self.is_seq + let mut indices = vec![col_num]; + for i in 0..dims[0]-1 { + let last_element = indices[indices.len() - 1]; + indices.push(last_element + dims[0]); + } + + let index_array = Array::new(Dim4::new(&dims), &indices, input_type); + match index_array { + Ok(a) => { + let index = Index::new(Some(a), None, false); + match index { + Ok(i) => index_gen(input, Dim4::new(&[1, 1, 1, 1]), &[i]), + Err(e) => Err(AfError::from(e)), + } + }, + Err(e) => Err(AfError::from(e)), } } -impl Drop for Index { - fn drop(&mut self) { - unsafe { - let ret_val = af_release_index(self.handle as MutAfIndex); - match ret_val { - 0 => (), - _ => panic!("Failed to destruct Index: {}", ret_val), - } +pub fn lookup(input: &Array, indices: &Array, seq_dim: i32) -> Result { + unsafe { + let mut temp: i64 = 0; + let err_val = af_lookup(&mut temp as MutAfArray + , input.get() as AfArray + , indices.get() as AfArray + , seq_dim as c_uint); + match err_val { + 0 => Ok(Array::from(temp)), + _ => Err(AfError::from(err_val)), + } + } +} + + +pub fn assign_seq(lhs: &Array, ndims: usize, seqs: &[Seq], rhs: &Array) -> Result { + unsafe{ + let mut temp: i64 = 0; + let err_val = af_assign_seq(&mut temp as MutAfArray + , lhs.get() as AfArray + , ndims as c_uint, seqs.as_ptr() as *const IndexT + , rhs.get() as AfArray); + match err_val { + 0 => Ok(Array::from(temp)), + _ => Err(AfError::from(err_val)), + } + } +} + +pub fn index_gen(input: &Array, ndims: Dim4, indices: &[Index]) -> Result { + unsafe{ + let mut temp: i64 = 0; + let err_val = af_index_gen(&mut temp as MutAfArray + , input.get() as AfArray + , ndims.get().as_ptr() as DimT + , indices.as_ptr() as *const IndexT); + match err_val { + 0 => Ok(Array::from(temp)), + _ => Err(AfError::from(err_val)), + } + } +} + +pub fn assign_gen(lhs: &Array, ndims: Dim4, indices: &[Index], rhs: &Array) -> Result { + unsafe{ + let mut temp: i64 = 0; + let err_val = af_assign_gen(&mut temp as MutAfArray + , lhs.get() as AfArray + , ndims.get().as_ptr() as DimT + , indices.as_ptr() as *const IndexT + , rhs.get() as AfArray); + match err_val { + 0 => Ok(Array::from(temp)), + _ => Err(AfError::from(err_val)), } } } \ No newline at end of file From 0f3e532080d118af63886e4a6a47de780c51c4df Mon Sep 17 00:00:00 2001 From: pradeep Date: Tue, 1 Sep 2015 15:58:37 -0400 Subject: [PATCH 4/9] Modified Seq::default() to return af_span C equivalent --- src/seq.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/seq.rs b/src/seq.rs index d7005e0b5..1c03d1e3b 100644 --- a/src/seq.rs +++ b/src/seq.rs @@ -1,7 +1,6 @@ extern crate libc; use std::fmt; -use std::ops::Index; use std::default::Default; use self::libc::{c_double}; @@ -15,13 +14,13 @@ pub struct Seq { impl Default for Seq { fn default() -> Seq { - Seq { begin: 0.0, end: 0.0, step: 0.0, } + Seq { begin: 1.0, end: 1.0, step: 0.0, } } } impl fmt::Display for Seq { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "[begin: {}, end: {}, step: {}", self.begin, self.end, self.step) + write!(f, "[begin: {}, end: {}, step: {}]", self.begin, self.end, self.step) } } From e43ca435575a0b841f325d4916fafdba209ea6f3 Mon Sep 17 00:00:00 2001 From: Jason Ramapuram Date: Wed, 2 Sep 2015 18:00:26 +0200 Subject: [PATCH 5/9] working example for accessing rows and columns --- examples/helloworld.rs | 13 ++++--- src/index.rs | 81 +++++++++++++----------------------------- 2 files changed, 30 insertions(+), 64 deletions(-) diff --git a/examples/helloworld.rs b/examples/helloworld.rs index 7f060dd64..27672c236 100644 --- a/examples/helloworld.rs +++ b/examples/helloworld.rs @@ -8,9 +8,9 @@ fn main() { info(); let num_rows: u64 = 5; - let num_columns: u64 = 3; + let num_cols: u64 = 3; - let dims = Dim4::new(&[num_rows, num_columns, 1, 1]); + let dims = Dim4::new(&[num_rows, num_cols, 1, 1]); println!("Create a 5-by-3 matrix of random floats on the GPU"); let a = match randu(dims, Aftype::F32) { @@ -46,11 +46,10 @@ fn main() { println!("Fourier transform the result"); fft(&b, 1.0, 0).map(|x| print(&x)); - println!("Grab last row"); - let c = row(&a, 4).unwrap(); - print(&c); - //array c = C.row(end); - // af_print(c); + 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()); println!("Create 2-by-3 matrix from host data"); let d_dims = Dim4::new(&[2, 3, 1, 1]); diff --git a/src/index.rs b/src/index.rs index c3d93a962..dfc7b0a3f 100644 --- a/src/index.rs +++ b/src/index.rs @@ -8,15 +8,11 @@ use defines::Aftype; use self::libc::{c_int, c_uint, c_longlong}; type MutAfArray = *mut self::libc::c_longlong; -type MutAfSeq = *mut Seq; type MutMutAfIndex = *mut *mut self::libc::c_longlong; type MutAfIndex = *mut self::libc::c_longlong; -type MutDouble = *mut self::libc::c_double; -type MutUint = *mut self::libc::c_uint; type AfArray = self::libc::c_longlong; type AfIndex = self::libc::c_longlong; type DimT = self::libc::c_longlong; -type SeqT = self::libc::c_longlong; type IndexT = self::libc::c_longlong; #[allow(dead_code)] @@ -24,7 +20,7 @@ extern { fn af_create_seq_index(result: MutMutAfIndex, input: *const Seq, is_batch: c_int) -> c_int; fn af_create_array_index(result: MutMutAfIndex, input: AfArray) -> c_int; fn af_release_index(indexer: MutAfIndex) -> c_int; - fn af_index(out: MutAfArray, input: AfArray, ndims: c_uint, index: *const IndexT) -> c_int; + fn af_index(out: MutAfArray, input: AfArray, ndims: c_uint, index: *const Seq) -> 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 IndexT, rhs: AfArray) -> c_int; fn af_index_gen(out: MutAfArray, input: AfArray, ndims: DimT, indices: *const IndexT) -> c_int; @@ -63,7 +59,7 @@ impl Index { } } - pub fn get(&self) -> i64 { + pub fn get(&self) -> i64{ self.handle } @@ -91,9 +87,11 @@ impl Drop for Index { pub fn index(input: &Array, seqs: &[Seq]) -> Result { unsafe { let mut temp: i64 = 0; + println!("size is: {}", seqs.len() as u32); let err_val = af_index(&mut temp as MutAfArray - , input.get() as AfArray, seqs.len() as u32 - , seqs.as_ptr() as *const SeqT); + , input.get() as AfArray + , seqs.len() as u32 + , seqs.clone().as_ptr() as *const Seq); match err_val { 0 => Ok(Array::from(temp)), _ => Err(AfError::from(err_val)), @@ -103,61 +101,26 @@ pub fn index(input: &Array, seqs: &[Seq]) -> Result { pub fn row(input: &Array, row_num: u64) -> Result { let dims_err = input.dims(); - let mut dims = [0, 0, 0, 0]; + let mut dims = Dim4::default(); match dims_err { - Ok(dim) => dims = dim.get().clone(), + Ok(dim) => dims = dim.clone(), Err(e) => panic!("Error unwrapping dims in row(): {}", e), } - - let array_end = dims[0] as f64 * dims[1] as f64 * dims[2] as f64 * dims[3] as f64; - let row_begin: f64 = dims[1] as f64 * row_num as f64; - assert!(row_begin <= array_end - dims[1] as f64); - - let row_end: f64 = row_begin + dims[1] as f64 - 1.0; - assert!(row_end <= array_end); - - let index = Index::new(None, Some(Seq::new(row_begin, row_end, 1.0)), false); - println!("origin size {} x {} x {} x {}", dims[0], dims[1], dims[2], dims[3]); - println!("passed index gen from {} to {}", row_begin, row_end); - - match index { - Ok(i) => { println!("index raw handle: {}", i.get()); index_gen(input, Dim4::new(&[1, 1, 1, 1]), &[i])}, - Err(e) => Err(AfError::from(e)), - } + + index(input, &[Seq::new(row_num as f64, row_num as f64, 1.0) + ,Seq::new(0.0, dims[1] as f64 - 1.0, 1.0)]) } pub fn col(input: &Array, col_num: u64) -> Result { let dims_err = input.dims(); - let mut dims = [0, 0, 0, 0]; + let mut dims = Dim4::default(); match dims_err { - Ok(dim) => dims = dim.get().clone(), - Err(e) => panic!("Error unwrapping dims in col(): {}", e), - } - - let input_type_err = input.get_type(); - let mut input_type = Aftype::F32; - match input_type_err { - Ok(t) => input_type = t, - Err(e) => panic!("Error unwrapping type in col(): {}", e), - } - - let mut indices = vec![col_num]; - for i in 0..dims[0]-1 { - let last_element = indices[indices.len() - 1]; - indices.push(last_element + dims[0]); - } - - let index_array = Array::new(Dim4::new(&dims), &indices, input_type); - match index_array { - Ok(a) => { - let index = Index::new(Some(a), None, false); - match index { - Ok(i) => index_gen(input, Dim4::new(&[1, 1, 1, 1]), &[i]), - Err(e) => Err(AfError::from(e)), - } - }, - Err(e) => Err(AfError::from(e)), + Ok(dim) => dims = dim.clone(), + Err(e) => panic!("Error unwrapping dims in row(): {}", e), } + + index(input, &[Seq::new(0.0, dims[0] as f64 - 1.0, 1.0) + ,Seq::new(col_num as f64, col_num as f64, 1.0)]) } pub fn lookup(input: &Array, indices: &Array, seq_dim: i32) -> Result { @@ -174,7 +137,6 @@ pub fn lookup(input: &Array, indices: &Array, seq_dim: i32) -> Result Result { unsafe{ let mut temp: i64 = 0; @@ -189,13 +151,18 @@ pub fn assign_seq(lhs: &Array, ndims: usize, seqs: &[Seq], rhs: &Array) -> Resul } } -pub fn index_gen(input: &Array, ndims: Dim4, indices: &[Index]) -> Result { +pub fn index_gen(input: &Array, ndims: Dim4, indices: &mut [Index]) -> Result { unsafe{ let mut temp: i64 = 0; + let mut index_ptrs = Vec::new(); + for index_struct in &mut indices[..] { + index_ptrs.push(index_struct.get()); + } + let err_val = af_index_gen(&mut temp as MutAfArray , input.get() as AfArray , ndims.get().as_ptr() as DimT - , indices.as_ptr() as *const IndexT); + , index_ptrs.as_ptr() as *const IndexT); match err_val { 0 => Ok(Array::from(temp)), _ => Err(AfError::from(err_val)), From cc567218383cd877cd180b6c724796b41fc4a3e1 Mon Sep 17 00:00:00 2001 From: Jason Ramapuram Date: Sun, 13 Sep 2015 20:08:09 +0200 Subject: [PATCH 6/9] remove errenous println --- src/index.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/index.rs b/src/index.rs index dfc7b0a3f..58ee112ff 100644 --- a/src/index.rs +++ b/src/index.rs @@ -87,7 +87,6 @@ impl Drop for Index { pub fn index(input: &Array, seqs: &[Seq]) -> Result { unsafe { let mut temp: i64 = 0; - println!("size is: {}", seqs.len() as u32); let err_val = af_index(&mut temp as MutAfArray , input.get() as AfArray , seqs.len() as u32 @@ -183,4 +182,4 @@ pub fn assign_gen(lhs: &Array, ndims: Dim4, indices: &[Index], rhs: &Array) -> R _ => Err(AfError::from(err_val)), } } -} \ No newline at end of file +} From 5d731d704eed6566c31fe67058b37255d9537ba5 Mon Sep 17 00:00:00 2001 From: Jason Ramapuram Date: Sun, 13 Sep 2015 20:21:53 +0200 Subject: [PATCH 7/9] resolve type to be a generic type for the host function --- src/array.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/array.rs b/src/array.rs index 18ead3671..3c60094f9 100644 --- a/src/array.rs +++ b/src/array.rs @@ -153,7 +153,7 @@ impl Array { self.handle } - pub fn host(&self, data:&mut [f64]) -> Result<(), AfError> { + pub fn host(&self, data: &mut [T]) -> Result<(), AfError> { unsafe { let ret_val = af_get_data_ptr(data.as_mut_ptr() as *mut c_void, self.handle as AfArray); match ret_val { From c48e8421b92575ad0a2ebf17806068c2a42f1746 Mon Sep 17 00:00:00 2001 From: pradeep Date: Mon, 14 Sep 2015 12:11:54 -0400 Subject: [PATCH 8/9] Fixed indexing functions, udpated arrayfire submodule to latest devel --- arrayfire | 2 +- examples/helloworld.rs | 20 ++++++ src/index.rs | 152 +++++++++++++++++++++-------------------- src/lib.rs | 2 +- 4 files changed, 99 insertions(+), 77 deletions(-) diff --git a/arrayfire b/arrayfire index 93427f09f..2d7567223 160000 --- a/arrayfire +++ b/arrayfire @@ -1 +1 @@ -Subproject commit 93427f09ff928f97df29c0e358c3fcf6b478bec6 +Subproject commit 2d756722341358448106302eb58a9e25fcf86574 diff --git a/examples/helloworld.rs b/examples/helloworld.rs index 27672c236..f10d1759b 100644 --- a/examples/helloworld.rs +++ b/examples/helloworld.rs @@ -9,6 +9,8 @@ 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(Dim4::new(&[3, 1, 1, 1]), values, Aftype::F32).unwrap(); let dims = Dim4::new(&[num_rows, num_cols, 1, 1]); @@ -39,6 +41,24 @@ fn main() { let test = &a + &b; println!("a + b"); print(&test); + // Index array using sequences + let seqs = &[Seq::new(1.0, 3.0, 1.0), Seq::default()]; + let sub = index(&a, seqs).unwrap(); + println!("a(seq(1,3,1), span)"); print(&sub); + + //Index array using array and sequence + let seq4gen = Seq::new(0.0, 2.0, 1.0); + + let mut idxrs = match Indexer::new() { + Ok(v) => v, + Err(e) => panic!("{}",e), + }; + idxrs.set_index(&indices, 0, None); + idxrs.set_index(&seq4gen, 1, Some(false)); + + let sub2 = index_gen(&a, idxrs).unwrap(); + println!("a(indices, seq(0, 2, 1))"); print(&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); diff --git a/src/index.rs b/src/index.rs index 58ee112ff..003ca3689 100644 --- a/src/index.rs +++ b/src/index.rs @@ -2,83 +2,100 @@ extern crate libc; use array::Array; use defines::AfError; -use dim4::Dim4; use seq::Seq; -use defines::Aftype; use self::libc::{c_int, c_uint, c_longlong}; -type MutAfArray = *mut self::libc::c_longlong; -type MutMutAfIndex = *mut *mut self::libc::c_longlong; type MutAfIndex = *mut self::libc::c_longlong; +type MutAfArray = *mut self::libc::c_longlong; type AfArray = self::libc::c_longlong; -type AfIndex = self::libc::c_longlong; type DimT = self::libc::c_longlong; type IndexT = self::libc::c_longlong; #[allow(dead_code)] extern { - fn af_create_seq_index(result: MutMutAfIndex, input: *const Seq, is_batch: c_int) -> c_int; - fn af_create_array_index(result: MutMutAfIndex, input: AfArray) -> c_int; - fn af_release_index(indexer: MutAfIndex) -> c_int; + 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 Seq, dim: DimT, is_batch: c_int) -> c_int; + fn af_release_indexers(indexers: MutAfIndex) -> c_int; + fn af_index(out: MutAfArray, input: AfArray, ndims: c_uint, index: *const Seq) -> 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 IndexT, rhs: AfArray) -> c_int; + fn af_assign_seq(out: MutAfArray, lhs: AfArray, ndims: c_uint, indices: *const Seq, 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; } -pub struct Index { +pub struct Indexer { handle: i64, - is_batch: bool, - is_seq: bool, + count: u32, +} + +pub trait Indexable { + fn set(&self, idxr: &Indexer, dim: u32, is_batch: Option) -> Result<(), AfError>; +} + +impl Indexable for Array { + #[allow(unused_variables)] + fn set(&self, idxr: &Indexer, dim: u32, is_batch: Option) -> Result<(), AfError> { + unsafe { + let err_val = af_set_array_indexer(idxr.clone().get() as MutAfIndex, + self.get() as AfArray, + dim as DimT); + match err_val { + 0 => Ok(()), + _ => Err(AfError::from(err_val)), + } + } + } +} + +impl Indexable for Seq { + fn set(&self, idxr: &Indexer, dim: u32, is_batch: Option) -> Result<(), AfError> { + unsafe { + let err_val = af_set_seq_indexer(idxr.clone().get() as MutAfIndex, self as *const Seq, + dim as DimT, is_batch.unwrap() as c_int); + match err_val { + 0 => Ok(()), + _ => Err(AfError::from(err_val)), + } + } + } } -impl Index { +impl Indexer { #[allow(unused_mut)] - pub fn new(arr: Option, seq: Option, is_batch: bool) -> Result { + pub fn new() -> Result { unsafe { - let mut err_val: c_int = 0; let mut temp: i64 = 0; - let mut is_seq = false; - err_val = match arr { - //c_func(&mut (x as *mut libc::c_void)); --> (&mut x) as *mut _ as *mut *mut libc::c_void. - //&mut temp as MutMutAfIndex, x), - Some(mut x) => { is_seq = false; af_create_array_index((&mut temp) as *mut _ as MutMutAfIndex, x.get() as AfArray) }, - None => 0, - }; - - err_val = match seq { - Some(mut x) => { is_seq = true; af_create_seq_index((&mut temp) as *mut _ as MutMutAfIndex, &mut x, is_batch as c_int) }, - None => AfError::ERR_UNKNOWN as c_int, - }; - + let err_val = af_create_indexers(&mut temp as MutAfIndex); match err_val { - 0 => Ok(Index {handle: temp, is_batch: is_batch, is_seq: is_seq}), + 0 => Ok(Indexer{handle: temp, count: 0}), _ => Err(AfError::from(err_val)), } } } - pub fn get(&self) -> i64{ - self.handle + pub fn set_index(&mut self, idx: &T, dim: u32, is_batch: Option) -> Result<(), AfError> { + self.count = self.count + 1; + idx.set(self, dim, is_batch) } - pub fn is_seq(&self) -> bool{ - self.is_seq + pub fn get(&self) -> i64 { + self.handle } - pub fn is_batch(&self) -> bool{ - self.is_batch + pub fn len(&self) -> u32 { + self.count } } -impl Drop for Index { +impl Drop for Indexer { fn drop(&mut self) { unsafe { - let ret_val = af_release_index(self.handle as MutAfIndex); + let ret_val = af_release_indexers(self.handle as MutAfIndex); match ret_val { 0 => (), - _ => panic!("Failed to destruct Index: {}", ret_val), + _ => panic!("Failed to release indexers resource: {}", ret_val), } } } @@ -88,9 +105,8 @@ pub fn index(input: &Array, seqs: &[Seq]) -> Result { unsafe { let mut temp: i64 = 0; let err_val = af_index(&mut temp as MutAfArray - , input.get() as AfArray - , seqs.len() as u32 - , seqs.clone().as_ptr() as *const Seq); + , input.get() as AfArray, seqs.len() as u32 + , seqs.as_ptr() as *const Seq); match err_val { 0 => Ok(Array::from(temp)), _ => Err(AfError::from(err_val)), @@ -100,24 +116,22 @@ pub fn index(input: &Array, seqs: &[Seq]) -> Result { pub fn row(input: &Array, row_num: u64) -> Result { let dims_err = input.dims(); - let mut dims = Dim4::default(); - match dims_err { - Ok(dim) => dims = dim.clone(), + let dims = match dims_err { + Ok(dim) => dim.clone(), Err(e) => panic!("Error unwrapping dims in row(): {}", e), - } - + }; + index(input, &[Seq::new(row_num as f64, row_num as f64, 1.0) ,Seq::new(0.0, dims[1] as f64 - 1.0, 1.0)]) } pub fn col(input: &Array, col_num: u64) -> Result { let dims_err = input.dims(); - let mut dims = Dim4::default(); - match dims_err { - Ok(dim) => dims = dim.clone(), + let dims = match dims_err { + Ok(dim) => dim.clone(), Err(e) => panic!("Error unwrapping dims in row(): {}", e), - } - + }; + index(input, &[Seq::new(0.0, dims[0] as f64 - 1.0, 1.0) ,Seq::new(col_num as f64, col_num as f64, 1.0)]) } @@ -125,10 +139,8 @@ pub fn col(input: &Array, col_num: u64) -> Result { pub fn lookup(input: &Array, indices: &Array, seq_dim: i32) -> Result { unsafe { let mut temp: i64 = 0; - let err_val = af_lookup(&mut temp as MutAfArray - , input.get() as AfArray - , indices.get() as AfArray - , seq_dim as c_uint); + let err_val = af_lookup(&mut temp as MutAfArray, input.get() as AfArray, + indices.get() as AfArray, seq_dim as c_uint); match err_val { 0 => Ok(Array::from(temp)), _ => Err(AfError::from(err_val)), @@ -139,10 +151,9 @@ pub fn lookup(input: &Array, indices: &Array, seq_dim: i32) -> Result Result { unsafe{ let mut temp: i64 = 0; - let err_val = af_assign_seq(&mut temp as MutAfArray - , lhs.get() as AfArray - , ndims as c_uint, seqs.as_ptr() as *const IndexT - , rhs.get() as AfArray); + let err_val = af_assign_seq(&mut temp as MutAfArray, lhs.get() as AfArray, + ndims as c_uint, seqs.as_ptr() as *const Seq, + rhs.get() as AfArray); match err_val { 0 => Ok(Array::from(temp)), _ => Err(AfError::from(err_val)), @@ -150,18 +161,11 @@ pub fn assign_seq(lhs: &Array, ndims: usize, seqs: &[Seq], rhs: &Array) -> Resul } } -pub fn index_gen(input: &Array, ndims: Dim4, indices: &mut [Index]) -> Result { +pub fn index_gen(input: &Array, indices: Indexer) -> Result { unsafe{ let mut temp: i64 = 0; - let mut index_ptrs = Vec::new(); - for index_struct in &mut indices[..] { - index_ptrs.push(index_struct.get()); - } - - let err_val = af_index_gen(&mut temp as MutAfArray - , input.get() as AfArray - , ndims.get().as_ptr() as DimT - , index_ptrs.as_ptr() as *const IndexT); + let err_val = af_index_gen(&mut temp as MutAfArray, input.get() as AfArray, + indices.len() as DimT, indices.get() as *const IndexT); match err_val { 0 => Ok(Array::from(temp)), _ => Err(AfError::from(err_val)), @@ -169,14 +173,12 @@ pub fn index_gen(input: &Array, ndims: Dim4, indices: &mut [Index]) -> Result Result { +pub fn assign_gen(lhs: &Array, indices: &Indexer, rhs: &Array) -> Result { unsafe{ let mut temp: i64 = 0; - let err_val = af_assign_gen(&mut temp as MutAfArray - , lhs.get() as AfArray - , ndims.get().as_ptr() as DimT - , indices.as_ptr() as *const IndexT - , rhs.get() as AfArray); + let err_val = af_assign_gen(&mut temp as MutAfArray, lhs.get() as AfArray, + indices.len() as DimT, indices.get() as *const IndexT, + rhs.get() as AfArray); match err_val { 0 => Ok(Array::from(temp)), _ => Err(AfError::from(err_val)), diff --git a/src/lib.rs b/src/lib.rs index de3d0a5cf..c712cb484 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,7 +44,7 @@ mod defines; pub use dim4::Dim4; mod dim4; -pub use index::{Index, index, row, col, lookup, assign_seq, index_gen, assign_gen}; +pub use index::{Indexer, index, row, col, lookup, assign_seq, index_gen, assign_gen}; mod index; pub use seq::Seq; From 714771e7000d5dbf8bcaf06accc147b126f580cf Mon Sep 17 00:00:00 2001 From: pradeep Date: Mon, 14 Sep 2015 12:15:04 -0400 Subject: [PATCH 9/9] Fixes for functions that take string input paramters --- src/graphics.rs | 4 ++-- src/image/mod.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/graphics.rs b/src/graphics.rs index 09b3c87df..784f84ab4 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -67,7 +67,7 @@ impl Window { let mut temp: u64 = 0; let err_val = af_create_window(&mut temp as MutWndHandle, width as c_int, height as c_int, - title.as_bytes().as_ptr() as *const u8); + title.clone().as_bytes().as_ptr() as *const u8); match err_val { 0 => Ok(Window::from(temp)), _ => Err(AfError::from(err_val)), @@ -88,7 +88,7 @@ impl Window { pub fn set_title(&self, title: String) -> Result<(), AfError> { unsafe { let err_val = af_set_title(self.handle as WndHandle, - title.as_bytes().as_ptr() as *const u8); + title.clone().as_bytes().as_ptr() as *const u8); match err_val { 0 => Ok(()), _ => Err(AfError::from(err_val)), diff --git a/src/image/mod.rs b/src/image/mod.rs index 8dbcaa49f..d13a6ab8c 100644 --- a/src/image/mod.rs +++ b/src/image/mod.rs @@ -93,7 +93,7 @@ pub fn load_image(filename: String, is_color: bool) -> Result { unsafe { let mut temp: i64 = 0; let err_val = af_load_image(&mut temp as MutAfArray, - filename.as_bytes().as_ptr() as *const u8, + filename.clone().as_bytes().as_ptr() as *const u8, is_color as c_int); match err_val { 0 => Ok(Array::from(temp)), @@ -105,7 +105,7 @@ pub fn load_image(filename: String, is_color: bool) -> Result { #[allow(unused_mut)] pub fn save_image(filename: String, input: &Array) -> Result<(), AfError> { unsafe { - let err_val = af_save_image(filename.as_bytes().as_ptr() as *const u8, + let err_val = af_save_image(filename.clone().as_bytes().as_ptr() as *const u8, input.get() as AfArray); match err_val { 0 => Ok(()),