Skip to content

Expose pinned(non-pageable memory) alloc and free functions #238

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 2 commits into from
Aug 4, 2020
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
19 changes: 18 additions & 1 deletion src/core/device.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::defines::AfError;
use super::error::HANDLE_ERROR;
use super::util::free_host;
use super::util::{dim_t, free_host, void_ptr};

use libc::{c_char, c_int, size_t};
use std::borrow::Cow;
Expand Down Expand Up @@ -33,6 +33,9 @@ extern "C" {
fn af_get_mem_step_size(step_bytes: *mut size_t) -> c_int;
fn af_device_gc() -> c_int;
fn af_sync(device: c_int) -> c_int;

fn af_alloc_pinned(non_pagable_ptr: *mut void_ptr, bytes: dim_t) -> c_int;
fn af_free_pinned(non_pagable_ptr: void_ptr) -> c_int;
}

/// Get ArrayFire Version Number
Expand Down Expand Up @@ -314,3 +317,17 @@ pub fn sync(device: i32) {
HANDLE_ERROR(AfError::from(err_val));
}
}

/// Allocate non-pageable memory on HOST memory
pub unsafe fn alloc_pinned(bytes: usize) -> void_ptr {
let mut out: void_ptr = std::ptr::null_mut();
let err_val = af_alloc_pinned(&mut out as *mut void_ptr, bytes as dim_t);
HANDLE_ERROR(AfError::from(err_val));
out
}

/// Free the pointer returned by [alloc_pinned](./fn.alloc_pinned.html)
pub unsafe fn free_pinned(ptr: void_ptr) {
let err_val = af_free_pinned(ptr);
HANDLE_ERROR(AfError::from(err_val));
}
14 changes: 10 additions & 4 deletions src/core/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,16 @@ macro_rules! eval {
/// The user can pass 1 or more sizes and the left over values will default to 1.
#[macro_export]
macro_rules! dim4 {
($dim0:literal) => {
($dim0:expr) => {
$crate::Dim4::new(&[$dim0, 1, 1, 1])
};
($dim0:literal, $dim1:literal) => {
($dim0:expr, $dim1:expr) => {
$crate::Dim4::new(&[$dim0, $dim1, 1, 1])
};
($dim0:literal, $dim1:literal, $dim2:literal) => {
($dim0:expr, $dim1:expr, $dim2:expr) => {
$crate::Dim4::new(&[$dim0, $dim1, $dim2, 1])
};
($dim0:literal, $dim1:literal, $dim2:literal, $dim3:literal) => {
($dim0:expr, $dim1:expr, $dim2:expr, $dim3:expr) => {
$crate::Dim4::new(&[$dim0, $dim1, $dim2, $dim3])
};
}
Expand All @@ -164,6 +164,12 @@ macro_rules! seq {
($start:literal : $end:literal : $step:literal) => {
$crate::Seq::<i32>::new($start, $end, $step)
};
($sty:ty; $start:expr , $end:expr , $step:expr) => {
$crate::Seq::<$sty>::new($start, $end, $step)
};
($start:expr , $end:expr , $step:expr) => {
$crate::Seq::<i32>::new($start, $end, $step)
};
}

/// Indexing into an existing Array
Expand Down