Skip to content

Fix reorder_v2 axes computation #216

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
Mar 30, 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
31 changes: 25 additions & 6 deletions src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,16 @@ where
///# Return Values
///
/// Array with data reordered as per the new axes order
///
///# Examples
///
/// ```rust
/// use arrayfire::{Array, Dim4, print, randu, reorder_v2};
/// let a = randu::<f32>(Dim4::new(&[5, 3, 1, 1]));
/// let b = reorder_v2(&a, 1, 0, None);
/// print(&a);
/// print(&b);
/// ```
pub fn reorder_v2<T>(
input: &Array<T>,
new_axis0: u64,
Expand All @@ -538,16 +548,25 @@ pub fn reorder_v2<T>(
where
T: HasAfEnum,
{
let mut new_axes = vec![new_axis0, new_axis1];
let mut new_axes = vec![0, 1, 2, 3];
new_axes[0] = new_axis0;
new_axes[1] = new_axis1;
match next_axes {
Some(v) => {
for axis in v {
new_axes.push(axis);
Some(left_over_new_axes) => {
// At the moment of writing this comment, ArrayFire could
// handle only a maximum of 4 dimensions. Hence, excluding
// the two explicit axes arguments to this function, a maximum
// of only two more axes can be provided. Hence the below condition.
assert!(left_over_new_axes.len() <= 2);

for a_idx in 0..left_over_new_axes.len() {
new_axes[2 + a_idx] = left_over_new_axes[a_idx];
}
}
None => {
new_axes.push(2);
new_axes.push(3);
for a_idx in 2..4 {
new_axes[a_idx] = a_idx as u64;
}
}
};

Expand Down
13 changes: 13 additions & 0 deletions tests/data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use ::arrayfire::*;
use float_cmp::approx_eq;

#[test]
fn check_reorder_api() {
let dims = Dim4::new(&[4, 5, 2, 3]);
let A = randu::<f32>(dims);

let transposedA = reorder_v2(&A, 1, 0, None);
let swap_0_2 = reorder_v2(&A, 2, 1, Some(vec![0]));
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]));
}
1 change: 1 addition & 0 deletions tutorials-book/src/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ can represent one of many different [basic data types](../enum.DType.html):
* [U64](../enum.DType.html) 64-bit unsigned integer (`uintl`)
* [S16](../enum.DType.html) 16-bit signed integer (`short`)
* [U16](../enum.DType.html) 16-bit unsigned integer (`unsigned short`)
* [F16](../enum.DType.html) 16-bit floating point number ([`half::f16`](https://crates.io/crates/half))

Most of these data types are supported on all modern GPUs; however, some
older devices may lack support for double precision arrays. In this case,
Expand Down