Skip to content

Commit 2801bba

Browse files
committed
FEATURE: Array print function with precision parameter
Also fixed the earlier print function to stop printing the string "No Name Array" when no array message was given.
1 parent 88d4837 commit 2801bba

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

src/array.rs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use dim4::Dim4;
44
use defines::{AfError, DType, Backend};
55
use error::HANDLE_ERROR;
66
use util::HasAfEnum;
7-
use self::libc::{uint8_t, c_void, c_int, c_uint, c_longlong};
7+
use self::libc::{uint8_t, c_void, c_int, c_uint, c_longlong, c_char};
8+
use std::ffi::CString;
89

910
type MutAfArray = *mut self::libc::c_longlong;
1011
type MutDouble = *mut self::libc::c_double;
@@ -78,6 +79,8 @@ extern {
7879

7980
fn af_print_array(arr: AfArray) -> c_int;
8081

82+
fn af_print_array_gen(exp: *const c_char, arr: AfArray, precision: c_int) -> c_int;
83+
8184
fn af_cast(out: MutAfArray, arr: AfArray, aftype: uint8_t) -> c_int;
8285

8386
fn af_get_backend_id(backend: *mut c_int, input: AfArray) -> c_int;
@@ -417,6 +420,10 @@ impl Drop for Array {
417420

418421
/// Print data in the Array
419422
///
423+
/// # Parameters
424+
///
425+
/// - `input` is the Array to be printed
426+
///
420427
/// # Examples
421428
///
422429
/// ```rust
@@ -438,8 +445,52 @@ impl Drop for Array {
438445
/// 0.9251 0.5132 0.6814
439446
/// ```
440447
pub fn print(input: &Array) {
448+
let emptystring = CString::new("").unwrap();
449+
unsafe {
450+
let err_val = af_print_array_gen(emptystring.to_bytes_with_nul().as_ptr() as *const c_char,
451+
input.get() as AfArray, 4);
452+
HANDLE_ERROR(AfError::from(err_val));
453+
}
454+
}
455+
456+
/// Generalized Array print function
457+
///
458+
/// Use this function to print Array data with arbitrary preicsion
459+
///
460+
/// # Parameters
461+
///
462+
/// - `msg` is message to be printed before printing the Array data
463+
/// - `input` is the Array to be printed
464+
/// - `precision` is data precision with which Array has to be printed
465+
///
466+
/// # Examples
467+
///
468+
/// ```rust
469+
/// use arrayfire::{Dim4, print_gen, randu};
470+
/// println!("Create a 5-by-3 matrix of random floats on the GPU");
471+
/// let dims = Dim4::new(&[5, 3, 1, 1]);
472+
/// let a = randu::<f32>(dims);
473+
/// print_gen(String::from("Random Array"), &a, Some(6));
474+
/// ```
475+
///
476+
/// The sample output will look like below:
477+
///
478+
/// ```text
479+
/// Random Array
480+
///
481+
/// [5 3 1 1]
482+
/// 0.740276 0.446440 0.776202
483+
/// 0.921094 0.667321 0.294810
484+
/// 0.039014 0.109939 0.714090
485+
/// 0.969058 0.470269 0.358590
486+
/// 0.925181 0.513225 0.681451
487+
/// ```
488+
pub fn print_gen(msg: String, input: &Array, precision: Option<i32>) {
489+
let emptystring = CString::new(msg.as_bytes()).unwrap();
441490
unsafe {
442-
let err_val = af_print_array(input.get() as AfArray);
491+
let err_val = af_print_array_gen(emptystring.to_bytes_with_nul().as_ptr() as *const c_char,
492+
input.get() as AfArray,
493+
match precision {Some(p)=>p, None=>4} as c_int);
443494
HANDLE_ERROR(AfError::from(err_val));
444495
}
445496
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
extern crate lazy_static;
77

88
pub use array::Array;
9-
pub use array::{print, eval_multiple, is_eval_manual, set_manual_eval};
9+
pub use array::{print, print_gen, eval_multiple, is_eval_manual, set_manual_eval};
1010
mod array;
1111

1212
//pub use algorithm::{sum_nan, product_nan, sum_nan_all, product_nan_all};

src/macros.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ macro_rules! join_many {
112112
/// # #[macro_use] extern crate arrayfire;
113113
///
114114
/// # fn main() {
115-
/// use arrayfire::{Dim4, print, randu};
115+
/// use arrayfire::{Dim4, print_gen, randu};
116116
/// let dims = Dim4::new(&[3, 1, 1, 1]);
117117
/// let a = randu::<f32>(dims);
118118
/// af_print!("Create a 5-by-3 matrix of random floats on the GPU", a);
@@ -123,8 +123,7 @@ macro_rules! join_many {
123123
macro_rules! af_print {
124124
[$msg: expr, $x: ident] => {
125125
{
126-
println!("{}", $msg);
127-
print(&$x);
126+
print_gen(String::from($msg), &$x, Some(4));
128127
}
129128
};
130129
}

0 commit comments

Comments
 (0)