Skip to content

PyO3 backend #29

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 8 commits into from
Jul 4, 2018
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
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ license-file = "LICENSE"

[dependencies]
libc = "0.2"
cpython = "0.1"
python3-sys = "0.1"
num-complex = "0.1"
ndarray = "0.10"

[dependencies.pyo3]
git = "http://github.com/termoshtt/pyo3"
branch = "pyobject_macros"
version = "0.2"
5 changes: 4 additions & 1 deletion example/extensions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ crate-type = ["cdylib"]

[dependencies]
numpy = { path = "../.." }
cpython = "0.1"
ndarray = "0.10"

[dependencies.pyo3]
version = "*"
features = ["extension-module"]
77 changes: 35 additions & 42 deletions example/extensions/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,41 @@
#![feature(proc_macro, proc_macro_path_invoc, specialization)]

#[macro_use]
extern crate cpython;
extern crate numpy;
extern crate ndarray;
extern crate numpy;
extern crate pyo3;

use numpy::*;
use ndarray::*;
use cpython::{PyResult, Python, PyObject};

/* Pure rust-ndarray functions */

// immutable example
fn axpy(a: f64, x: ArrayViewD<f64>, y: ArrayViewD<f64>) -> ArrayD<f64> {
a * &x + &y
}

// mutable example (no return)
fn mult(a: f64, mut x: ArrayViewMutD<f64>) {
x *= a;
}

/* rust-cpython wrappers (to be exposed) */

// wrapper of `axpy`
fn axpy_py(py: Python, a: f64, x: PyArray, y: PyArray) -> PyResult<PyArray> {
let np = PyArrayModule::import(py)?;
let x = x.as_array().into_pyresult(py, "x must be f64 array")?;
let y = y.as_array().into_pyresult(py, "y must be f64 array")?;
Ok(axpy(a, x, y).into_pyarray(py, &np))
}

// wrapper of `mult`
fn mult_py(py: Python, a: f64, x: PyArray) -> PyResult<PyObject> {
let x = x.as_array_mut().into_pyresult(py, "x must be f64 array")?;
mult(a, x);
Ok(py.None()) // Python function must returns
}
use numpy::*;
use pyo3::{py, PyModule, PyObject, PyResult, Python};

#[py::modinit(rust_ext)]
fn init_module(py: Python, m: &PyModule) -> PyResult<()> {
// immutable example
fn axpy(a: f64, x: ArrayViewD<f64>, y: ArrayViewD<f64>) -> ArrayD<f64> {
a * &x + &y
}

// mutable example (no return)
fn mult(a: f64, mut x: ArrayViewMutD<f64>) {
x *= a;
}

// wrapper of `axpy`
#[pyfn(m, "axpy")]
fn axpy_py(py: Python, a: f64, x: PyArray, y: PyArray) -> PyResult<PyArray> {
let np = PyArrayModule::import(py)?;
let x = x.as_array().into_pyresult(py, "x must be f64 array")?;
let y = y.as_array().into_pyresult(py, "y must be f64 array")?;
Ok(axpy(a, x, y).into_pyarray(py, &np))
}

// wrapper of `mult`
#[pyfn(m, "mult")]
fn mult_py(py: Python, a: f64, x: PyArray) -> PyResult<PyObject> {
let x = x.as_array_mut().into_pyresult(py, "x must be f64 array")?;
mult(a, x);
Ok(py.None()) // Python function must returns
}

/* Define module "_rust_ext" */
py_module_initializer!(_rust_ext, init_rust_ext, PyInit__rust_ext, |py, m| {
m.add(py, "__doc__", "Rust extension for NumPy")?;
m.add(py,
"axpy",
py_fn!(py, axpy_py(a: f64, x: PyArray, y: PyArray)))?;
m.add(py, "mult", py_fn!(py, mult_py(a: f64, x: PyArray)))?;
Ok(())
});
}
190 changes: 68 additions & 122 deletions src/array.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
//! Untyped safe interface for NumPy ndarray

use npyffi;
use pyffi;
use cpython::*;
use ndarray::*;
use npyffi;
use pyo3::ffi;
use pyo3::*;

use std::os::raw::c_void;
use std::ptr::null_mut;

use super::*;
use super::error::ArrayCastError;
use super::*;

/// Untyped safe interface for NumPy ndarray.
pub struct PyArray(PyObject);

impl PyArray {
pub fn as_ptr(&self) -> *mut npyffi::PyArrayObject {
self.0.as_ptr() as *mut npyffi::PyArrayObject
}
pyobject_native_type!(PyArray, npyffi::PyArray_Type, npyffi::PyArray_Check);

pub fn steal_ptr(self) -> *mut npyffi::PyArrayObject {
self.0.steal_ptr() as *mut npyffi::PyArrayObject
impl PyArray {
pub fn as_array_ptr(&self) -> *mut npyffi::PyArrayObject {
self.as_ptr() as _
}

pub unsafe fn from_owned_ptr(py: Python, ptr: *mut pyffi::PyObject) -> Self {
pub unsafe fn from_owned_ptr(py: Python, ptr: *mut pyo3::ffi::PyObject) -> Self {
let obj = PyObject::from_owned_ptr(py, ptr);
PyArray(obj)
}

pub unsafe fn from_borrowed_ptr(py: Python, ptr: *mut pyffi::PyObject) -> Self {
pub unsafe fn from_borrowed_ptr(py: Python, ptr: *mut pyo3::ffi::PyObject) -> Self {
let obj = PyObject::from_borrowed_ptr(py, ptr);
PyArray(obj)
}
Expand All @@ -37,7 +35,7 @@ impl PyArray {
///
/// https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_NDIM
pub fn ndim(&self) -> usize {
let ptr = self.as_ptr();
let ptr = self.as_array_ptr();
unsafe { (*ptr).nd as usize }
}

Expand All @@ -46,7 +44,7 @@ impl PyArray {
/// https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_DIMS
pub fn dims(&self) -> Vec<usize> {
let n = self.ndim();
let ptr = self.as_ptr();
let ptr = self.as_array_ptr();
let dims = unsafe {
let p = (*ptr).dimensions;
::std::slice::from_raw_parts(p, n)
Expand All @@ -69,7 +67,7 @@ impl PyArray {
/// - https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.strides.html#numpy.ndarray.strides
pub fn strides(&self) -> Vec<isize> {
let n = self.ndim();
let ptr = self.as_ptr();
let ptr = self.as_array_ptr();
let dims = unsafe {
let p = (*ptr).strides;
::std::slice::from_raw_parts(p, n)
Expand All @@ -78,21 +76,23 @@ impl PyArray {
}

unsafe fn data<T>(&self) -> *mut T {
let ptr = self.as_ptr();
let ptr = self.as_array_ptr();
(*ptr).data as *mut T
}

fn ndarray_shape<A>(&self) -> StrideShape<IxDyn> {
// FIXME may be done more simply
let shape: Shape<_> = Dim(self.shape()).into();
let st: Vec<usize> =
self.strides().iter().map(|&x| x as usize / ::std::mem::size_of::<A>()).collect();
let st: Vec<usize> = self.strides()
.iter()
.map(|&x| x as usize / ::std::mem::size_of::<A>())
.collect();
shape.strides(Dim(st))
}

pub fn typenum(&self) -> i32 {
unsafe {
let descr = (*self.as_ptr()).descr;
let descr = (*self.as_array_ptr()).descr;
(*descr).type_num
}
}
Expand All @@ -110,13 +110,23 @@ impl PyArray {
/// Get data as a ndarray::ArrayView
pub fn as_array<A: types::TypeNum>(&self) -> Result<ArrayViewD<A>, ArrayCastError> {
self.type_check::<A>()?;
unsafe { Ok(ArrayView::from_shape_ptr(self.ndarray_shape::<A>(), self.data())) }
unsafe {
Ok(ArrayView::from_shape_ptr(
self.ndarray_shape::<A>(),
self.data(),
))
}
}

/// Get data as a ndarray::ArrayViewMut
pub fn as_array_mut<A: types::TypeNum>(&self) -> Result<ArrayViewMutD<A>, ArrayCastError> {
self.type_check::<A>()?;
unsafe { Ok(ArrayViewMut::from_shape_ptr(self.ndarray_shape::<A>(), self.data())) }
unsafe {
Ok(ArrayViewMut::from_shape_ptr(
self.ndarray_shape::<A>(),
self.data(),
))
}
}

/// Get data as a Rust immutable slice
Expand All @@ -131,23 +141,25 @@ impl PyArray {
unsafe { Ok(::std::slice::from_raw_parts_mut(self.data(), self.len())) }
}

pub unsafe fn new_<T: TypeNum>(py: Python,
np: &PyArrayModule,
dims: &[usize],
strides: *mut npy_intp,
data: *mut c_void)
-> Self {
pub unsafe fn new_<T: types::TypeNum>(
py: Python,
np: &PyArrayModule,
dims: &[usize],
strides: *mut npy_intp,
data: *mut c_void,
) -> Self {
let dims: Vec<_> = dims.iter().map(|d| *d as npy_intp).collect();
let ptr = np.PyArray_New(np.get_type_object(npyffi::ArrayType::PyArray_Type),
dims.len() as i32,
dims.as_ptr() as *mut npy_intp,
T::typenum(),
strides,
data,
0, // itemsize
0, // flag
::std::ptr::null_mut() //obj
);
let ptr = np.PyArray_New(
np.get_type_object(npyffi::ArrayType::PyArray_Type),
dims.len() as i32,
dims.as_ptr() as *mut npy_intp,
T::typenum(),
strides,
data,
0, // itemsize
0, // flag
::std::ptr::null_mut(), //obj
);
Self::from_owned_ptr(py, ptr)
}

Expand All @@ -157,102 +169,36 @@ impl PyArray {
}

/// a wrapper of [PyArray_ZEROS](https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_ZEROS)
pub fn zeros<T: TypeNum>(py: Python,
np: &PyArrayModule,
dims: &[usize],
order: NPY_ORDER)
-> Self {
pub fn zeros<T: TypeNum>(
py: Python,
np: &PyArrayModule,
dims: &[usize],
order: NPY_ORDER,
) -> Self {
let dims: Vec<npy_intp> = dims.iter().map(|d| *d as npy_intp).collect();
unsafe {
let descr = np.PyArray_DescrFromType(T::typenum());
let ptr = np.PyArray_Zeros(dims.len() as i32,
dims.as_ptr() as *mut npy_intp,
descr,
order as i32);
let ptr = np.PyArray_Zeros(
dims.len() as i32,
dims.as_ptr() as *mut npy_intp,
descr,
order as i32,
);
Self::from_owned_ptr(py, ptr)
}
}

/// a wrapper of [PyArray_Arange](https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_Arange)
pub fn arange<T: TypeNum>(py: Python,
np: &PyArrayModule,
start: f64,
stop: f64,
step: f64)
-> Self {
pub fn arange<T: TypeNum>(
py: Python,
np: &PyArrayModule,
start: f64,
stop: f64,
step: f64,
) -> Self {
unsafe {
let ptr = np.PyArray_Arange(start, stop, step, T::typenum());
Self::from_owned_ptr(py, ptr)
}
}
}

impl<'source> FromPyObject<'source> for PyArray {
fn extract(py: Python, obj: &'source PyObject) -> PyResult<Self> {
Ok(obj.clone_ref(py).cast_into::<PyArray>(py)?)
}
}

impl<'source> FromPyObject<'source> for &'source PyArray {
fn extract(py: Python, obj: &'source PyObject) -> PyResult<Self> {
Ok(obj.cast_as::<PyArray>(py)?)
}
}

impl ToPyObject for PyArray {
type ObjectType = Self;

fn to_py_object(&self, py: Python) -> Self {
PyClone::clone_ref(self, py)
}
}

impl PythonObject for PyArray {
#[inline]
fn as_object(&self) -> &PyObject {
&self.0
}

#[inline]
fn into_object(self) -> PyObject {
self.0
}

#[inline]
unsafe fn unchecked_downcast_from(obj: PyObject) -> Self {
PyArray(obj)
}

#[inline]
unsafe fn unchecked_downcast_borrow_from<'a>(obj: &'a PyObject) -> &'a Self {
::std::mem::transmute(obj)
}
}

impl PythonObjectWithCheckedDowncast for PyArray {
fn downcast_from<'p>(py: Python<'p>,
obj: PyObject)
-> Result<PyArray, PythonObjectDowncastError<'p>> {
let np = PyArrayModule::import(py).unwrap();
unsafe {
if npyffi::PyArray_Check(&np, obj.as_ptr()) != 0 {
Ok(PyArray(obj))
} else {
Err(PythonObjectDowncastError(py))
}
}
}

fn downcast_borrow_from<'a, 'p>(py: Python<'p>,
obj: &'a PyObject)
-> Result<&'a PyArray, PythonObjectDowncastError<'p>> {
let np = PyArrayModule::import(py).unwrap();
unsafe {
if npyffi::PyArray_Check(&np, obj.as_ptr()) != 0 {
Ok(::std::mem::transmute(obj))
} else {
Err(PythonObjectDowncastError(py))
}
}
}
}
Loading