GeezerVison is showing github.com — Added wrappers around `af_alloc_host` and `af_free_host` in the util … · arrayfire/arrayfire-rust@1d80171
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/device/mod.rs +7-11Lines changed: 7 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,7 +3,8 @@ extern crate libc; | 3 | 3 | use defines::{AfError, DType}; | 4 | 4 | use error::HANDLE_ERROR; | 5 | 5 | use self::libc::{c_int, size_t, c_char, c_void}; | 6 | | -use std::ffi::CString; | | 6 | +use std::ffi::{CStr, CString}; | | 7 | +use util::free_host; | 7 | 8 |
| 8 | 9 | extern { | 9 | 10 | fn af_get_version(major: *mut c_int, minor: *mut c_int, patch: *mut c_int) -> c_int; | @@ -21,9 +22,6 @@ extern { | 21 | 22 | fn af_get_mem_step_size(step_bytes: *mut size_t) -> c_int; | 22 | 23 | fn af_device_gc() -> c_int; | 23 | 24 | fn af_sync(device: c_int) -> c_int; | 24 | | - | 25 | | - fn af_alloc_host(elements: size_t, _type: DType) -> *mut c_void; | 26 | | - fn af_free_host(ptr: *mut c_void) -> c_int; | 27 | 25 | } | 28 | 26 |
| 29 | 27 | /// Get ArrayFire Version Number | @@ -72,17 +70,15 @@ pub fn info() { | 72 | 70 | /// [0] GeForce GT 750M, 2048 MB, CUDA Compute 3.0 | 73 | 71 | /// ``` | 74 | 72 | pub fn info_string(verbose: bool) -> String { | 75 | | - let mut tmp: *mut c_char = 0 as *mut c_char; | | 73 | + let result: String; | 76 | 74 | unsafe { | | 75 | + let mut tmp: *mut c_char = 0 as *mut c_char; | 77 | 76 | let err_val = af_info_string(&mut tmp, verbose); | 78 | 77 | HANDLE_ERROR(AfError::from(err_val)); | 79 | | - | 80 | | - let result = (*CString::from_raw(tmp)).to_str().unwrap().to_owned(); | 81 | | - | 82 | | - let err_val = af_free_host(tmp as *mut c_void); | 83 | | - HANDLE_ERROR(AfError::from(err_val)); | 84 | | - result | | 78 | + result = CStr::from_ptr(tmp).to_string_lossy().into_owned(); | | 79 | + free_host(tmp); | 85 | 80 | } | | 81 | + result | 86 | 82 | } | 87 | 83 |
| 88 | 84 | /// Initialize ArrayFire library | |
Collapse file Copy file name to clipboardExpand all lines: src/util.rs +27-1Lines changed: 27 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,11 +6,18 @@ use defines::{SparseFormat, BinaryOp, RandomEngineType}; | 6 | 6 | use error::HANDLE_ERROR; | 7 | 7 | use std::mem; | 8 | 8 | 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; | 10 | 14 |
| 11 | 15 | #[allow(dead_code)] | 12 | 16 | extern { | 13 | 17 | 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; | 14 | 21 | } | 15 | 22 |
| 16 | 23 | /// Get size, in bytes, of the arrayfire native type | @@ -23,6 +30,25 @@ pub fn get_size(value: DType) -> usize { | 23 | 30 | } | 24 | 31 | } | 25 | 32 |
| | 33 | +/// Allocates space using Arrayfire allocator in host memory | | 34 | +pub fn alloc_host<T>(elements: usize, _type: DType) -> *const T { | | 35 | + let ptr = 0 as *const T; | | 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 | + | 26 | 52 | impl From<i32> for AfError { | 27 | 53 | fn from(t: i32) -> AfError { | 28 | 54 | assert!(AfError::SUCCESS as i32 <= t && t <= AfError::ERR_UNKNOWN as i32); | |
|