Skip to content

Commit 38a4393

Browse files
committed
array memory lock, unlock and get device pointer fns
1 parent 8a9a467 commit 38a4393

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/array.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ type MutDouble = *mut self::libc::c_double;
1111
type MutUint = *mut self::libc::c_uint;
1212
type AfArray = self::libc::c_longlong;
1313
type DimT = self::libc::c_longlong;
14+
type MutVoidPtr = *mut self::libc::c_ulonglong;
15+
type VoidPtr = self::libc::c_ulonglong;
1416

1517
// Some unused functions from array.h in C-API of ArrayFire
1618
// af_create_handle
1719
// af_copy_array
1820
// af_write_array
19-
// af_get_data_ptr
2021
// af_get_data_ref_count
2122

2223
#[allow(dead_code)]
@@ -95,6 +96,12 @@ extern {
9596
fn af_is_linear(result: *mut c_int, arr: AfArray) -> c_int;
9697

9798
fn af_is_owner(result: *mut c_int, arr: AfArray) -> c_int;
99+
100+
fn af_lock_array(arr: AfArray) -> c_int;
101+
102+
fn af_unlock_array(arr: AfArray) -> c_int;
103+
104+
fn af_get_device_ptr(ptr: MutVoidPtr, arr: AfArray) -> c_int;
98105
}
99106

100107
/// A multidimensional data container
@@ -327,6 +334,39 @@ impl Array {
327334
Array::from(temp)
328335
}
329336
}
337+
338+
/// Lock the device buffer in the memory manager
339+
///
340+
/// Locked buffers are not freed by memory manager until unlock is called.
341+
pub fn lock(&self) {
342+
unsafe {
343+
let err_val = af_lock_array(self.handle as AfArray);
344+
HANDLE_ERROR(AfError::from(err_val));
345+
}
346+
}
347+
348+
/// Unlock the device buffer in the memory manager
349+
///
350+
/// This function will give back the control over the device pointer to the
351+
/// memory manager.
352+
pub fn unlock(&self) {
353+
unsafe {
354+
let err_val = af_unlock_array(self.handle as AfArray);
355+
HANDLE_ERROR(AfError::from(err_val));
356+
}
357+
}
358+
359+
/// Get the device pointer and lock the buffer in memory manager
360+
///
361+
/// The device pointer is not freed by memory manager until unlock is called.
362+
pub fn device_ptr(&self) -> u64 {
363+
unsafe {
364+
let mut temp: u64 = 0;
365+
let err_val = af_get_device_ptr(&mut temp as MutVoidPtr, self.handle as AfArray);
366+
HANDLE_ERROR(AfError::from(err_val));
367+
temp
368+
}
369+
}
330370
}
331371

332372
/// Used for creating Array object from native resource id

0 commit comments

Comments
 (0)