Skip to content
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

Fortran layout bigarray view #145

Merged
merged 1 commit into from
Mar 29, 2024
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: 26 additions & 5 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,9 @@ pub mod bigarray {

#[cfg(all(feature = "bigarray-ext", not(feature = "no-std")))]
pub(crate) mod bigarray_ext {
use ndarray::{ArrayView2, ArrayView3, ArrayViewMut2, ArrayViewMut3, Dimension};
use ndarray::{
ArrayView2, ArrayView3, ArrayViewMut2, ArrayViewMut3, Dimension, Shape, ShapeBuilder,
};

use core::{marker::PhantomData, mem, ptr, slice};

Expand All @@ -488,13 +490,17 @@ pub(crate) mod bigarray_ext {
/// Returns array view
pub fn view(&self) -> ArrayView2<T> {
let ba = unsafe { self.0.custom_ptr_val::<bigarray::Bigarray>() };
unsafe { ArrayView2::from_shape_ptr(self.shape(), (*ba).data as *const T) }
unsafe {
ArrayView2::from_shape_ptr(build_shape(ba, self.shape()), (*ba).data as *const T)
}
}

/// Returns mutable array view
pub fn view_mut(&mut self) -> ArrayViewMut2<T> {
let ba = unsafe { self.0.custom_ptr_val::<bigarray::Bigarray>() };
unsafe { ArrayViewMut2::from_shape_ptr(self.shape(), (*ba).data as *mut T) }
unsafe {
ArrayViewMut2::from_shape_ptr(build_shape(ba, self.shape()), (*ba).data as *mut T)
}
}

/// Returns the shape of `self`
Expand Down Expand Up @@ -568,13 +574,17 @@ pub(crate) mod bigarray_ext {
/// Returns array view
pub fn view(&self) -> ArrayView3<T> {
let ba = unsafe { self.0.custom_ptr_val::<bigarray::Bigarray>() };
unsafe { ArrayView3::from_shape_ptr(self.shape(), (*ba).data as *const T) }
unsafe {
ArrayView3::from_shape_ptr(build_shape(ba, self.shape()), (*ba).data as *const T)
}
}

/// Returns mutable array view
pub fn view_mut(&mut self) -> ArrayViewMut3<T> {
let ba = unsafe { self.0.custom_ptr_val::<bigarray::Bigarray>() };
unsafe { ArrayViewMut3::from_shape_ptr(self.shape(), (*ba).data as *mut T) }
unsafe {
ArrayViewMut3::from_shape_ptr(build_shape(ba, self.shape()), (*ba).data as *mut T)
}
}

/// Returns the shape of `self`
Expand Down Expand Up @@ -638,4 +648,15 @@ pub(crate) mod bigarray_ext {
array
}
}

fn build_shape<S: ShapeBuilder>(
ba: *const bigarray::Bigarray,
shape: S,
) -> Shape<<S as ShapeBuilder>::Dim> {
if unsafe { (*ba).is_fortran() } {
shape.f()
} else {
shape.into_shape()
}
}
}
13 changes: 13 additions & 0 deletions sys/src/bigarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ pub struct Bigarray {
pub dim: [Intnat; 0],
}

impl Bigarray {
/// Returns true if array is Fortran contiguous
pub fn is_fortran(&self) -> bool {
(self.flags & Layout::FORTRAN_LAYOUT as isize) != 0
}
}

#[allow(non_camel_case_types)]
pub enum Managed {
EXTERNAL = 0, /* Data is not allocated by OCaml */
Expand All @@ -47,6 +54,12 @@ pub enum Kind {
KIND_MASK = 0xFF, /* Mask for kind in flags field */
}

#[allow(non_camel_case_types)]
pub enum Layout {
C_LAYOUT = 0, /* Row major, indices start at 0 */
FORTRAN_LAYOUT = 0x100, /* Column major, indices start at 1 */
}

extern "C" {
pub fn malloc(size: usize) -> Data;
pub fn caml_ba_alloc(flags: i32, num_dims: i32, data: Data, dim: *const i32) -> Value;
Expand Down
Loading