Skip to content

set_row and pad function fixes #247

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 3 commits into from
Sep 8, 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
12 changes: 8 additions & 4 deletions src/core/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,20 @@ where
/// An example of creating an Array from half::f16 array
///
/// ```rust
/// use arrayfire::{Array, Dim4, print};
/// use arrayfire::{Array, Dim4, is_half_available, print};
/// use half::f16;
///
/// let values: [f32; 3] = [1.0, 2.0, 3.0];
///
/// let half_values = values.iter().map(|&x| f16::from_f32(x)).collect::<Vec<_>>();
/// if is_half_available(0) { // Default device is 0, hence the argument
/// let half_values = values.iter().map(|&x| f16::from_f32(x)).collect::<Vec<_>>();
///
/// let hvals = Array::new(&half_values, Dim4::new(&[3, 1, 1, 1]));
/// let hvals = Array::new(&half_values, Dim4::new(&[3, 1, 1, 1]));
///
/// print(&hvals);
/// print(&hvals);
/// } else {
/// println!("Half support isn't available on this device");
/// }
/// ```
///
pub fn new(slice: &[T], dims: Dim4) -> Self {
Expand Down
14 changes: 12 additions & 2 deletions src/core/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,9 +948,9 @@ pub fn pad<T: HasAfEnum>(
let err_val = af_pad(
&mut temp as *mut af_array,
input.get(),
begin.ndims() as c_uint,
4,
begin.get().as_ptr() as *const dim_t,
end.ndims() as c_uint,
4,
end.get().as_ptr() as *const dim_t,
fill_type as c_uint,
);
Expand All @@ -963,7 +963,9 @@ pub fn pad<T: HasAfEnum>(
mod tests {
use super::reorder_v2;

use super::super::defines::BorderType;
use super::super::random::randu;
use super::pad;

use crate::dim4;

Expand All @@ -976,4 +978,12 @@ mod tests {
let _swap_1_2 = reorder_v2(&a, 0, 2, Some(vec![1]));
let _swap_0_3 = reorder_v2(&a, 3, 1, Some(vec![2, 0]));
}

#[test]
fn check_pad_api() {
let a = randu::<f32>(dim4![3, 3]);
let begin_dims = dim4!(0, 0, 0, 0);
let end_dims = dim4!(2, 2, 0, 0);
let _padded = pad(&a, begin_dims, end_dims, BorderType::ZERO);
}
}
19 changes: 19 additions & 0 deletions src/core/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ extern "C" {

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;
fn af_get_half_support(available: *mut c_int, device: c_int) -> c_int;
}

/// Get ArrayFire Version Number
Expand Down Expand Up @@ -331,3 +332,21 @@ pub unsafe fn free_pinned(ptr: void_ptr) {
let err_val = af_free_pinned(ptr);
HANDLE_ERROR(AfError::from(err_val));
}

/// Check if a device has half support
///
/// # Parameters
///
/// - `device` is the device for which half precision support is checked for
///
/// # Return Values
///
/// `True` if `device` device has half support, `False` otherwise.
pub fn is_half_available(device: i32) -> bool {
unsafe {
let mut temp: i32 = 0;
let err_val = af_get_half_support(&mut temp as *mut c_int, device as c_int);
HANDLE_ERROR(AfError::from(err_val));
temp > 0
}
}
8 changes: 4 additions & 4 deletions src/core/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ pub fn set_row<T>(inout: &mut Array<T>, new_row: &Array<T>, row_num: u64)
where
T: HasAfEnum,
{
let seqs = [
Seq::new(row_num as f64, row_num as f64, 1.0),
Seq::default(),
];
let mut seqs = vec![Seq::new(row_num as f64, row_num as f64, 1.0)];
if inout.dims().ndims() > 1 {
seqs.push(Seq::default());
}
assign_seq(inout, &seqs, new_row)
}

Expand Down