diff --git a/examples/acoustic_wave.rs b/examples/acoustic_wave.rs index 21b12b90a..a7f064f92 100644 --- a/examples/acoustic_wave.rs +++ b/examples/acoustic_wave.rs @@ -71,8 +71,8 @@ fn acoustic_wave_simulation() { // Location of the source. let seqs = &[Seq::new(700.0, 800.0, 1.0), Seq::new(800.0, 800.0, 1.0)]; // Set the pressure there. - p = assign_seq( - &p, + assign_seq( + &mut p, seqs, &index(&pulse, &[Seq::new(it as f64, it as f64, 1.0)]), ); diff --git a/examples/helloworld.rs b/examples/helloworld.rs index f4f4017d2..7b3685c2f 100644 --- a/examples/helloworld.rs +++ b/examples/helloworld.rs @@ -22,7 +22,7 @@ fn main() { let dims = Dim4::new(&[num_rows, num_cols, 1, 1]); - let a = randu::(dims); + let mut a = randu::(dims); af_print!("Create a 5-by-3 float matrix on the GPU", a); println!("Element-wise arithmetic"); @@ -67,8 +67,8 @@ fn main() { 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); - let ur = set_row(&a, &r, num_rows - 1); - af_print!("Set last row to 1's", ur); + set_row(&mut a, &r, num_rows - 1); + af_print!("Set last row to 1's", a); let d_dims = Dim4::new(&[2, 3, 1, 1]); let d_input: [i32; 6] = [1, 2, 3, 4, 5, 6]; diff --git a/src/arith/mod.rs b/src/arith/mod.rs index fe7e1ccca..0dfe9149e 100644 --- a/src/arith/mod.rs +++ b/src/arith/mod.rs @@ -831,7 +831,6 @@ mod op_assign { use crate::array::Array; use crate::index::{assign_gen, Indexer}; use crate::seq::Seq; - use std::mem; use std::ops::{AddAssign, DivAssign, MulAssign, RemAssign, SubAssign}; use std::ops::{BitAndAssign, BitOrAssign, BitXorAssign, ShlAssign, ShrAssign}; @@ -852,8 +851,7 @@ mod op_assign { idxrs.set_index(&tmp_seq, n, Some(false)); } let opres = $func(self as &Array, &rhs, false).cast::(); - let tmp = assign_gen(self as &Array, &idxrs, &opres); - let old = mem::replace(self, tmp); + assign_gen(self, &idxrs, &opres); } } }; @@ -884,8 +882,7 @@ mod op_assign { idxrs.set_index(&tmp_seq, n, Some(false)); } let opres = $func(self as &Array, &rhs, false).cast::(); - let tmp = assign_gen(self as &Array, &idxrs, &opres); - let old = mem::replace(self, tmp); + assign_gen(self, &idxrs, &opres); } } }; diff --git a/src/defines.rs b/src/defines.rs index 7c7bb2b9d..ec6de2813 100644 --- a/src/defines.rs +++ b/src/defines.rs @@ -1,7 +1,6 @@ extern crate num; use self::num::Complex; -use std::error::Error; use std::fmt::Error as FmtError; use std::fmt::{Display, Formatter}; @@ -80,13 +79,7 @@ impl Display for Backend { impl Display for AfError { fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> { - write!(f, "{}", self.description()) - } -} - -impl Error for AfError { - fn description(&self) -> &str { - match *self { + let text = match *self { AfError::SUCCESS => "Function returned successfully", AfError::ERR_NO_MEM => "System or Device ran out of memory", AfError::ERR_DRIVER => "Error in the device driver", @@ -104,7 +97,8 @@ impl Error for AfError { AfError::ERR_NO_GFX => "This build of ArrayFire has no graphics support", AfError::ERR_INTERNAL => "Error either in ArrayFire or in a project upstream", AfError::ERR_UNKNOWN => "Unknown Error", - } + }; + write!(f, "{}", text) } } diff --git a/src/error.rs b/src/error.rs index b43fef6c9..e33b2c60c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,7 +3,6 @@ extern crate libc; use self::libc::c_char; use crate::defines::AfError; use crate::util::{free_host, DimT, MutDimT}; -use std::error::Error; use std::ffi::CStr; use std::ops::{Deref, DerefMut}; use std::sync::RwLock; @@ -39,7 +38,7 @@ pub fn handle_error_general(error_code: AfError) { AfError::SUCCESS => {} /* No-op */ _ => panic!( "Error message: {}\nLast error: {}", - error_code.description(), + error_code, get_last_error() ), } @@ -63,7 +62,7 @@ lazy_static! { /// fn handle_error(error_code: AfError) { /// match error_code { /// AfError::SUCCESS => {}, /* No-op */ -/// _ => panic!("Error message: {}", error_code.description()), +/// _ => panic!("Error message: {}", error_code), /// } /// } /// diff --git a/src/index.rs b/src/index.rs index 9c5cca7ca..f5eb00dbb 100644 --- a/src/index.rs +++ b/src/index.rs @@ -9,6 +9,7 @@ use crate::util::{AfArray, AfIndex, DimT, HasAfEnum, MutAfArray, MutAfIndex}; use std::default::Default; use std::marker::PhantomData; +use std::mem; #[allow(dead_code)] extern "C" { @@ -276,7 +277,6 @@ where /// print(&a); /// print(&row(&a, 4)); /// ``` -#[allow(dead_code)] pub fn row(input: &Array, row_num: u64) -> Array where T: HasAfEnum, @@ -290,9 +290,8 @@ where ) } -#[allow(dead_code)] -/// Set `row_num`^th row in `input` Array to a new Array `new_row` -pub fn set_row(input: &Array, new_row: &Array, row_num: u64) -> Array +/// Set `row_num`^th row in `inout` Array to a new Array `new_row` +pub fn set_row(inout: &mut Array, new_row: &Array, row_num: u64) where T: HasAfEnum, { @@ -300,10 +299,9 @@ where Seq::new(row_num as f64, row_num as f64, 1.0), Seq::default(), ]; - assign_seq(input, &seqs, new_row) + assign_seq(inout, &seqs, new_row) } -#[allow(dead_code)] /// Get an Array with all rows from `first` to `last` in the `input` Array pub fn rows(input: &Array, first: u64, last: u64) -> Array where @@ -315,14 +313,13 @@ where ) } -#[allow(dead_code)] -/// Set rows from `first` to `last` in `input` Array with rows from Array `new_rows` -pub fn set_rows(input: &Array, new_rows: &Array, first: u64, last: u64) -> Array +/// Set rows from `first` to `last` in `inout` Array with rows from Array `new_rows` +pub fn set_rows(inout: &mut Array, new_rows: &Array, first: u64, last: u64) where T: HasAfEnum, { let seqs = [Seq::new(first as f64, last as f64, 1.0), Seq::default()]; - assign_seq(input, &seqs, new_rows) + assign_seq(inout, &seqs, new_rows) } /// Extract `col_num` col from `input` Array @@ -337,7 +334,6 @@ where /// println!("Grab last col of the random matrix"); /// print(&col(&a, 4)); /// ``` -#[allow(dead_code)] pub fn col(input: &Array, col_num: u64) -> Array where T: HasAfEnum, @@ -351,9 +347,8 @@ where ) } -#[allow(dead_code)] -/// Set `col_num`^th col in `input` Array to a new Array `new_col` -pub fn set_col(input: &Array, new_col: &Array, col_num: u64) -> Array +/// Set `col_num`^th col in `inout` Array to a new Array `new_col` +pub fn set_col(inout: &mut Array, new_col: &Array, col_num: u64) where T: HasAfEnum, { @@ -361,10 +356,9 @@ where Seq::default(), Seq::new(col_num as f64, col_num as f64, 1.0), ]; - assign_seq(input, &seqs, new_col) + assign_seq(inout, &seqs, new_col) } -#[allow(dead_code)] /// Get all cols from `first` to `last` in the `input` Array pub fn cols(input: &Array, first: u64, last: u64) -> Array where @@ -376,20 +370,18 @@ where ) } -#[allow(dead_code)] -/// Set cols from `first` to `last` in `input` Array with cols from Array `new_cols` -pub fn set_cols(input: &Array, new_cols: &Array, first: u64, last: u64) -> Array +/// Set cols from `first` to `last` in `inout` Array with cols from Array `new_cols` +pub fn set_cols(inout: &mut Array, new_cols: &Array, first: u64, last: u64) where T: HasAfEnum, { let seqs = [Seq::default(), Seq::new(first as f64, last as f64, 1.0)]; - assign_seq(input, &seqs, new_cols) + assign_seq(inout, &seqs, new_cols) } -#[allow(dead_code)] /// Get `slice_num`^th slice from `input` Array /// -/// Note. Slices indicate that the indexing is along 3rd dimension +/// Slices indicate that the indexing is along 3rd dimension pub fn slice(input: &Array, slice_num: u64) -> Array where T: HasAfEnum, @@ -402,11 +394,10 @@ where index(input, &seqs) } -#[allow(dead_code)] -/// Set slice `slice_num` in `input` Array to a new Array `new_slice` +/// Set slice `slice_num` in `inout` Array to a new Array `new_slice` /// /// Slices indicate that the indexing is along 3rd dimension -pub fn set_slice(input: &Array, new_slice: &Array, slice_num: u64) -> Array +pub fn set_slice(inout: &mut Array, new_slice: &Array, slice_num: u64) where T: HasAfEnum, { @@ -415,10 +406,9 @@ where Seq::default(), Seq::new(slice_num as f64, slice_num as f64, 1.0), ]; - assign_seq(input, &seqs, new_slice) + assign_seq(inout, &seqs, new_slice) } -#[allow(dead_code)] /// Get slices from `first` to `last` in `input` Array /// /// Slices indicate that the indexing is along 3rd dimension @@ -434,11 +424,10 @@ where index(input, &seqs) } -#[allow(dead_code)] -/// Set `first` to `last` slices of `input` Array to a new Array `new_slices` +/// Set `first` to `last` slices of `inout` Array to a new Array `new_slices` /// /// Slices indicate that the indexing is along 3rd dimension -pub fn set_slices(input: &Array, new_slices: &Array, first: u64, last: u64) -> Array +pub fn set_slices(inout: &mut Array, new_slices: &Array, first: u64, last: u64) where T: HasAfEnum, { @@ -447,7 +436,7 @@ where Seq::default(), Seq::new(first as f64, last as f64, 1.0), ]; - assign_seq(input, &seqs, new_slices) + assign_seq(inout, &seqs, new_slices) } /// Lookup(hash) an Array using another Array @@ -480,10 +469,7 @@ where /// /// ```rust /// use arrayfire::{constant, Dim4, Seq, assign_seq, print}; -/// let a = constant(2.0 as f32, Dim4::new(&[5, 3, 1, 1])); -/// let b = constant(1.0 as f32, Dim4::new(&[3, 3, 1, 1])); -/// let seqs = &[Seq::new(1.0, 3.0, 1.0), Seq::default()]; -/// let sub = assign_seq(&a, seqs, &b); +/// let mut a = constant(2.0 as f32, Dim4::new(&[5, 3, 1, 1])); /// print(&a); /// // 2.0 2.0 2.0 /// // 2.0 2.0 2.0 @@ -491,14 +477,18 @@ where /// // 2.0 2.0 2.0 /// // 2.0 2.0 2.0 /// -/// print(&sub); +/// let b = constant(1.0 as f32, Dim4::new(&[3, 3, 1, 1])); +/// let seqs = &[Seq::new(1.0, 3.0, 1.0), Seq::default()]; +/// assign_seq(&mut a, seqs, &b); +/// +/// print(&a); /// // 2.0 2.0 2.0 /// // 1.0 1.0 1.0 /// // 1.0 1.0 1.0 /// // 1.0 1.0 1.0 /// // 2.0 2.0 2.0 /// ``` -pub fn assign_seq(lhs: &Array, seqs: &[Seq], rhs: &Array) -> Array +pub fn assign_seq(lhs: &mut Array, seqs: &[Seq], rhs: &Array) where c_double: From, I: HasAfEnum, @@ -516,7 +506,8 @@ where ); HANDLE_ERROR(AfError::from(err_val)); } - temp.into() + let modified = temp.into(); + let _old_arr = mem::replace(lhs, modified); } /// Index an Array using any combination of Array's and Sequence's @@ -574,7 +565,7 @@ where /// let values: [f32; 3] = [1.0, 2.0, 3.0]; /// let indices = Array::new(&values, Dim4::new(&[3, 1, 1, 1])); /// let seq4gen = Seq::new(0.0, 2.0, 1.0); -/// let a = randu::(Dim4::new(&[5, 3, 1, 1])); +/// let mut a = randu::(Dim4::new(&[5, 3, 1, 1])); /// // [5 3 1 1] /// // 0.0000 0.2190 0.3835 /// // 0.1315 0.0470 0.5194 @@ -588,8 +579,8 @@ where /// idxrs.set_index(&indices, 0, None); // 2nd parameter is indexing dimension /// idxrs.set_index(&seq4gen, 1, Some(false)); // 3rd parameter indicates batch operation /// -/// let sub2 = assign_gen(&a, &idxrs, &b); -/// println!("a(indices, seq(0, 2, 1))"); print(&sub2); +/// assign_gen(&mut a, &idxrs, &b); +/// println!("a(indices, seq(0, 2, 1))"); print(&a); /// // [5 3 1 1] /// // 0.0000 0.2190 0.3835 /// // 2.0000 2.0000 2.0000 @@ -597,7 +588,7 @@ where /// // 2.0000 2.0000 2.0000 /// // 0.5328 0.9347 0.0535 /// ``` -pub fn assign_gen(lhs: &Array, indices: &Indexer, rhs: &Array) -> Array +pub fn assign_gen(lhs: &mut Array, indices: &Indexer, rhs: &Array) where T: HasAfEnum, { @@ -612,7 +603,8 @@ where ); HANDLE_ERROR(AfError::from(err_val)); } - temp.into() + let modified = temp.into(); + let _old_arr = mem::replace(lhs, modified); } #[repr(C)] diff --git a/tests/error_handler.rs b/tests/error_handler.rs index 464a0f923..f3df0aae4 100644 --- a/tests/error_handler.rs +++ b/tests/error_handler.rs @@ -8,7 +8,7 @@ macro_rules! implement_handler { pub fn $fn_name(error_code: AfError) { match error_code { AfError::SUCCESS => {} /* No-op */ - _ => panic!("Error message: {}", error_code.description()), + _ => panic!("Error message: {}", error_code), } } };