Skip to content

Added info_string #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/helloworld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use af::*;
fn main() {
set_device(0);
info();
print!("Info String:\n{}", info_string(true));

let num_rows: u64 = 5;
let num_cols: u64 = 3;
Expand Down
31 changes: 28 additions & 3 deletions src/device/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
extern crate libc;

use defines::AfError;
use defines::{AfError, DType};
use error::HANDLE_ERROR;
use self::libc::{c_int, size_t, c_char};
use std::ffi::CString;
use self::libc::{c_int, size_t, c_char, c_void};
use std::ffi::{CStr, CString};
use util::free_host;

extern {
fn af_get_version(major: *mut c_int, minor: *mut c_int, patch: *mut c_int) -> c_int;
fn af_info() -> c_int;
fn af_info_string(str: *mut *mut c_char, verbose: bool) -> c_int;
fn af_init() -> c_int;
fn af_get_device_count(nDevices: *mut c_int) -> c_int;
fn af_get_dbl_support(available: *mut c_int, device: c_int) -> c_int;
Expand Down Expand Up @@ -56,6 +58,29 @@ pub fn info() {
}
}

/// Return library meta-info as `String`
///
/// # Examples
///
/// An example output of `af::info_string` call looks like below
///
/// ```text
/// ArrayFire v3.0.0 (CUDA, 64-bit Mac OSX, build d8d4b38)
/// Platform: CUDA Toolkit 7, Driver: CUDA Driver Version: 7000
/// [0] GeForce GT 750M, 2048 MB, CUDA Compute 3.0
/// ```
pub fn info_string(verbose: bool) -> String {
let result: String;
unsafe {
let mut tmp: *mut c_char = ::std::ptr::null_mut();
let err_val = af_info_string(&mut tmp, verbose);
HANDLE_ERROR(AfError::from(err_val));
result = CStr::from_ptr(tmp).to_string_lossy().into_owned();
free_host(tmp);
}
result
}

/// Initialize ArrayFire library
///
/// 0th device will be the default device unless init call
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub use data::{select, selectl, selectr, replace, replace_scalar};
pub use data::{range_t, iota_t, identity_t, constant_t};
mod data;

pub use device::{get_version, info, init, device_count, is_double_available, set_device, get_device};
pub use device::{get_version, info, info_string, init, device_count, is_double_available, set_device, get_device};
pub use device::{device_mem_info, print_mem_info, set_mem_step_size, get_mem_step_size, device_gc, sync};
mod device;

Expand Down
28 changes: 27 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ use defines::{SparseFormat, BinaryOp, RandomEngineType};
use error::HANDLE_ERROR;
use std::mem;
use self::num::Complex;
use self::libc::{uint8_t, c_int, size_t};
use self::libc::{uint8_t, c_int, size_t, c_void};

// This is private in array
// use array::DimT;
type DimT = self::libc::c_longlong;

#[allow(dead_code)]
extern {
fn af_get_size_of(size: *mut size_t, aftype: uint8_t) -> c_int;

fn af_alloc_host(ptr: *mut *const c_void, bytes: DimT) -> c_int;
fn af_free_host(ptr: *mut c_void) -> c_int;
}

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

/// Allocates space using Arrayfire allocator in host memory
pub fn alloc_host<T>(elements: usize, _type: DType) -> *const T {
let ptr: *const T = ::std::ptr::null();
let bytes = (elements * get_size(_type)) as DimT;
unsafe {
let err_val = af_alloc_host(&mut (ptr as *const c_void), bytes);
HANDLE_ERROR(AfError::from(err_val));
}
ptr
}

/// Frees memory allocated by Arrayfire allocator in host memory
pub fn free_host<T>(ptr: *mut T) {
unsafe {
let err_val = af_free_host(ptr as *mut c_void);
HANDLE_ERROR(AfError::from(err_val));
}
}

impl From<i32> for AfError {
fn from(t: i32) -> AfError {
assert!(AfError::SUCCESS as i32 <= t && t <= AfError::ERR_UNKNOWN as i32);
Expand Down