diff --git a/src/core/device.rs b/src/core/device.rs index cf2a81b42..3d313e421 100644 --- a/src/core/device.rs +++ b/src/core/device.rs @@ -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; @@ -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 @@ -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)); +} diff --git a/src/core/macros.rs b/src/core/macros.rs index 105e59683..81181d2f9 100644 --- a/src/core/macros.rs +++ b/src/core/macros.rs @@ -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]) }; } @@ -164,6 +164,12 @@ macro_rules! seq { ($start:literal : $end:literal : $step:literal) => { $crate::Seq::::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::::new($start, $end, $step) + }; } /// Indexing into an existing Array