Skip to content

Commit 33627c7

Browse files
authored
Merge pull request #102 from botev/devel
Added info_string
2 parents d6fc0c3 + 80bfb4e commit 33627c7

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

examples/helloworld.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use af::*;
77
fn main() {
88
set_device(0);
99
info();
10+
print!("Info String:\n{}", info_string(true));
1011

1112
let num_rows: u64 = 5;
1213
let num_cols: u64 = 3;

src/device/mod.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
extern crate libc;
22

3-
use defines::AfError;
3+
use defines::{AfError, DType};
44
use error::HANDLE_ERROR;
5-
use self::libc::{c_int, size_t, c_char};
6-
use std::ffi::CString;
5+
use self::libc::{c_int, size_t, c_char, c_void};
6+
use std::ffi::{CStr, CString};
7+
use util::free_host;
78

89
extern {
910
fn af_get_version(major: *mut c_int, minor: *mut c_int, patch: *mut c_int) -> c_int;
1011
fn af_info() -> c_int;
12+
fn af_info_string(str: *mut *mut c_char, verbose: bool) -> c_int;
1113
fn af_init() -> c_int;
1214
fn af_get_device_count(nDevices: *mut c_int) -> c_int;
1315
fn af_get_dbl_support(available: *mut c_int, device: c_int) -> c_int;
@@ -56,6 +58,29 @@ pub fn info() {
5658
}
5759
}
5860

61+
/// Return library meta-info as `String`
62+
///
63+
/// # Examples
64+
///
65+
/// An example output of `af::info_string` call looks like below
66+
///
67+
/// ```text
68+
/// ArrayFire v3.0.0 (CUDA, 64-bit Mac OSX, build d8d4b38)
69+
/// Platform: CUDA Toolkit 7, Driver: CUDA Driver Version: 7000
70+
/// [0] GeForce GT 750M, 2048 MB, CUDA Compute 3.0
71+
/// ```
72+
pub fn info_string(verbose: bool) -> String {
73+
let result: String;
74+
unsafe {
75+
let mut tmp: *mut c_char = ::std::ptr::null_mut();
76+
let err_val = af_info_string(&mut tmp, verbose);
77+
HANDLE_ERROR(AfError::from(err_val));
78+
result = CStr::from_ptr(tmp).to_string_lossy().into_owned();
79+
free_host(tmp);
80+
}
81+
result
82+
}
83+
5984
/// Initialize ArrayFire library
6085
///
6186
/// 0th device will be the default device unless init call

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub use data::{select, selectl, selectr, replace, replace_scalar};
4040
pub use data::{range_t, iota_t, identity_t, constant_t};
4141
mod data;
4242

43-
pub use device::{get_version, info, init, device_count, is_double_available, set_device, get_device};
43+
pub use device::{get_version, info, info_string, init, device_count, is_double_available, set_device, get_device};
4444
pub use device::{device_mem_info, print_mem_info, set_mem_step_size, get_mem_step_size, device_gc, sync};
4545
mod device;
4646

src/util.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ use defines::{SparseFormat, BinaryOp, RandomEngineType};
66
use error::HANDLE_ERROR;
77
use std::mem;
88
use self::num::Complex;
9-
use self::libc::{uint8_t, c_int, size_t};
9+
use self::libc::{uint8_t, c_int, size_t, c_void};
10+
11+
// This is private in array
12+
// use array::DimT;
13+
type DimT = self::libc::c_longlong;
1014

1115
#[allow(dead_code)]
1216
extern {
1317
fn af_get_size_of(size: *mut size_t, aftype: uint8_t) -> c_int;
18+
19+
fn af_alloc_host(ptr: *mut *const c_void, bytes: DimT) -> c_int;
20+
fn af_free_host(ptr: *mut c_void) -> c_int;
1421
}
1522

1623
/// Get size, in bytes, of the arrayfire native type
@@ -23,6 +30,25 @@ pub fn get_size(value: DType) -> usize {
2330
}
2431
}
2532

33+
/// Allocates space using Arrayfire allocator in host memory
34+
pub fn alloc_host<T>(elements: usize, _type: DType) -> *const T {
35+
let ptr: *const T = ::std::ptr::null();
36+
let bytes = (elements * get_size(_type)) as DimT;
37+
unsafe {
38+
let err_val = af_alloc_host(&mut (ptr as *const c_void), bytes);
39+
HANDLE_ERROR(AfError::from(err_val));
40+
}
41+
ptr
42+
}
43+
44+
/// Frees memory allocated by Arrayfire allocator in host memory
45+
pub fn free_host<T>(ptr: *mut T) {
46+
unsafe {
47+
let err_val = af_free_host(ptr as *mut c_void);
48+
HANDLE_ERROR(AfError::from(err_val));
49+
}
50+
}
51+
2652
impl From<i32> for AfError {
2753
fn from(t: i32) -> AfError {
2854
assert!(AfError::SUCCESS as i32 <= t && t <= AfError::ERR_UNKNOWN as i32);

0 commit comments

Comments
 (0)