Skip to content

Commit c3f7921

Browse files
committed
Added mem_info macro
Moved the following macros to separate module(macros) * mem_info * join_many
1 parent b2c042e commit c3f7921

File tree

4 files changed

+102
-40
lines changed

4 files changed

+102
-40
lines changed

examples/pi.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[macro_use(mem_info)]
12
extern crate arrayfire as af;
23
extern crate time;
34

@@ -17,6 +18,8 @@ fn main() {
1718

1819
let start = PreciseTime::now();
1920

21+
mem_info!("Before benchmark");
22+
2023
for bench_iter in 0..100 {
2124
let pi_val = add(&mul(x, x, false).unwrap(), &mul(y, y, false).unwrap(), false)
2225
.and_then( |z| sqrt(&z) )
@@ -29,4 +32,6 @@ fn main() {
2932
let end = PreciseTime::now();
3033

3134
println!("Estimated Pi Value in {} seconds", start.to(end) / 100);
35+
36+
mem_info!("After benchmark");
3237
}

src/data/mod.rs

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -395,55 +395,20 @@ pub fn join(dim: i32, first: &Array, second: &Array) -> Result<Array, AfError> {
395395
#[allow(unused_mut)]
396396
pub fn join_many(dim: i32, inputs: Vec<&Array>) -> Result<Array, AfError> {
397397
unsafe {
398+
let mut v = Vec::new();
399+
for i in inputs {
400+
v.push(i.get());
401+
}
398402
let mut temp: i64 = 0;
399403
let err_val = af_join_many(&mut temp as MutAfArray, dim as c_int,
400-
inputs.len() as c_uint, inputs.as_ptr() as *const AfArray);
404+
v.len() as c_uint, v.as_ptr() as *const AfArray);
401405
match err_val {
402406
0 => Ok(Array::from(temp)),
403407
_ => Err(AfError::from(err_val)),
404408
}
405409
}
406410
}
407411

408-
/// Join multiple Arrays along a given dimension
409-
///
410-
/// # Examples
411-
///
412-
/// ```
413-
/// # #[macro_use] extern crate arrayfire;
414-
/// # fn main() {
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();
418-
/// let d = join_many![2, a, b, c];
419-
/// # }
420-
/// ```
421-
///
422-
/// # Panics
423-
///
424-
/// Using this macro to create an Array from multiple Arrays doesn't implicitly check for errors
425-
/// during FFI calls. Therefore, make sure your inputs are correct. In case, you do need proper
426-
/// error checking, use the [function version](./fn.join_many.html) of this macro.
427-
// Using macro to implement join many wrapper
428-
#[macro_export]
429-
macro_rules! join_many {
430-
[$dim: expr; $($x:ident),+] => {
431-
{
432-
let mut temp_vec = Vec::new();
433-
$(
434-
temp_vec.push($x);
435-
)*
436-
let mut temp: i64 = 0;
437-
unsafe {
438-
let mut temp: i64 = 0;
439-
af_join_many(&mut temp as MutAfArray, $dim as c_int,
440-
temp_vec.len() as c_uint, temp_vec.as_ptr() as *const AfArray);
441-
Array::from(temp)
442-
}
443-
}
444-
};
445-
}
446-
447412
macro_rules! data_func_def {
448413
($fn_name:ident, $ffi_name: ident) => (
449414
#[allow(unused_mut)]

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ mod image;
7373
pub use lapack::{svd, lu, qr, cholesky, solve, solve_lu, inverse, det, rank, norm};
7474
pub use lapack::{svd_inplace, lu_inplace, qr_inplace, cholesky_inplace, is_lapack_available};
7575
mod lapack;
76+
mod macros;
7677
mod num;
7778

7879
pub use signal::{approx1, approx2};

src/macros.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/// Macro to print the current stats of ArrayFire's memory manager.
2+
///
3+
/// `mem_info!` print 4 values:
4+
///
5+
/// Name | Description
6+
/// -------------------------|-------------------------
7+
/// Allocated Bytes | Total number of bytes allocated by the memory manager
8+
/// Allocated Buffers | Total number of buffers allocated
9+
/// Locked (In Use) Bytes | Number of bytes that are in use by active arrays
10+
/// Locked (In Use) Buffers | Number of buffers that are in use by active arrays
11+
///
12+
/// The `Allocated Bytes` is always a multiple of the memory step size. The
13+
/// default step size is 1024 bytes. This means when a buffer is to be
14+
/// allocated, the size is always rounded up to a multiple of the step size.
15+
/// You can use [get_mem_step_size](./fn.get_mem_step_size.html) to check the
16+
/// current step size and [set_mem_step_size](./fn.set_mem_step_size.html) to
17+
/// set a custom resolution size.
18+
///
19+
/// The `Allocated Buffers` is the number of buffers that use up the allocated
20+
/// bytes. This includes buffers currently in scope, as well as buffers marked
21+
/// as free, ie, from arrays gone out of scope. The free buffers are available
22+
/// for use by new arrays that might be created.
23+
///
24+
/// The `Locked Bytes` is the number of bytes in use that cannot be
25+
/// reallocated at the moment. The difference of Allocated Bytes and Locked
26+
/// Bytes is the total bytes available for reallocation.
27+
///
28+
/// The `Locked Buffers` is the number of buffer in use that cannot be
29+
/// reallocated at the moment. The difference of Allocated Buffers and Locked
30+
/// Buffers is the number of buffers available for reallocation.
31+
///
32+
/// # Parameters
33+
///
34+
/// - `msg` is the message that is printed to screen before printing stats
35+
///
36+
/// # Examples
37+
///
38+
/// ```
39+
/// mem_info!("Here");
40+
/// ```
41+
///
42+
/// Sample Output:
43+
///
44+
/// ```
45+
/// AF Memory: Here
46+
/// Allocated [ Bytes | Buffers ] = [ 4096 | 4 ]
47+
/// In Use [ Bytes | Buffers ] = [ 2048 | 2 ]
48+
/// ```
49+
#[macro_export]
50+
macro_rules! mem_info {
51+
[$msg: expr] => {
52+
{
53+
let (abytes, abuffs, lbytes, lbuffs) = device_mem_info().unwrap();
54+
println!("AF Memory: {:?}", $msg);
55+
println!("Allocated [Bytes | Buffers] = [ {} | {} ]", abytes, abuffs);
56+
println!("In Use [Bytes | Buffers] = [ {} | {} ]", lbytes, lbuffs);
57+
}
58+
};
59+
}
60+
61+
/// Join multiple Arrays along a given dimension
62+
///
63+
/// # Examples
64+
///
65+
/// ```
66+
/// # #[macro_use] extern crate arrayfire;
67+
/// # fn main() {
68+
/// let a = randu::<f32>(Dim4::new(&[5, 3, 1, 1])).unwrap();
69+
/// let b = randu::<f32>(Dim4::new(&[5, 3, 1, 1])).unwrap();
70+
/// let c = randu::<f32>(Dim4::new(&[5, 3, 1, 1])).unwrap();
71+
/// let d = join_many![2, a, b, c];
72+
/// # }
73+
/// ```
74+
///
75+
/// # Panics
76+
///
77+
/// This macro just calls [join_many](./fn.join_many.html) function after collecting all
78+
/// the input arrays into a vector.
79+
// Using macro to implement join many wrapper
80+
#[macro_export]
81+
macro_rules! join_many {
82+
[$dim: expr; $($x:ident),+] => {
83+
{
84+
let mut temp_vec = Vec::new();
85+
$(
86+
temp_vec.push($x);
87+
)*
88+
join_many($dim, temp_vec)
89+
}
90+
};
91+
}

0 commit comments

Comments
 (0)