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

migrate PyArray contructors to Bound API (Part 3) #419

Merged
merged 5 commits into from
Mar 28, 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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ num-complex = ">= 0.2, < 0.5"
num-integer = "0.1"
num-traits = "0.2"
ndarray = ">= 0.13, < 0.16"
pyo3 = { version = "0.21.0-beta", default-features = false, features = ["gil-refs", "macros"] }
pyo3 = { version = "0.21.0", default-features = false, features = ["gil-refs", "macros"] }
rustc-hash = "1.1"

[dev-dependencies]
pyo3 = { version = "0.21.0-beta", default-features = false, features = ["auto-initialize", "gil-refs"] }
pyo3 = { version = "0.21.0", default-features = false, features = ["auto-initialize", "gil-refs"] }
nalgebra = { version = "0.32", default-features = false, features = ["std"] }

[package.metadata.docs.rs]
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ numpy = "0.20"

```rust
use numpy::ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};
use numpy::{IntoPyArray, PyArrayDyn, PyReadonlyArrayDyn};
use pyo3::{pymodule, types::PyModule, PyResult, Python};
use numpy::{IntoPyArray, PyArrayDyn, PyReadonlyArrayDyn, PyArrayMethods};
use pyo3::{pymodule, types::PyModule, PyResult, Python, Bound};

#[pymodule]
fn rust_ext<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
fn rust_ext<'py>(_py: Python<'py>, m: &Bound<'py, PyModule>) -> PyResult<()> {
// example using immutable borrows producing a new array
fn axpy(a: f64, x: ArrayViewD<'_, f64>, y: ArrayViewD<'_, f64>) -> ArrayD<f64> {
a * &x + &y
Expand All @@ -67,17 +67,17 @@ fn rust_ext<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
a: f64,
x: PyReadonlyArrayDyn<'py, f64>,
y: PyReadonlyArrayDyn<'py, f64>,
) -> &'py PyArrayDyn<f64> {
) -> Bound<'py, PyArrayDyn<f64>> {
let x = x.as_array();
let y = y.as_array();
let z = axpy(a, x, y);
z.into_pyarray(py)
z.into_pyarray_bound(py)
}

// wrapper of `mult`
#[pyfn(m)]
#[pyo3(name = "mult")]
fn mult_py<'py>(a: f64, x: &'py PyArrayDyn<f64>) {
fn mult_py<'py>(a: f64, x: &Bound<'py, PyArrayDyn<f64>>) {
let x = unsafe { x.as_array_mut() };
mult(a, x);
}
Expand Down
6 changes: 3 additions & 3 deletions benches/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn from_iter(bencher: &mut Bencher, size: usize) {
iter_with_gil(bencher, |py| {
let iter = black_box(Iter(0..size));

PyArray1::from_iter(py, iter);
PyArray1::from_iter_bound(py, iter);
});
}

Expand Down Expand Up @@ -141,7 +141,7 @@ fn from_vec2(bencher: &mut Bencher, size: usize) {
iter_with_gil(bencher, |py| {
let vec2 = black_box(&vec2);

PyArray2::from_vec2(py, vec2).unwrap();
PyArray2::from_vec2_bound(py, vec2).unwrap();
});
}

Expand All @@ -166,7 +166,7 @@ fn from_vec3(bencher: &mut Bencher, size: usize) {
iter_with_gil(bencher, |py| {
let vec3 = black_box(&vec3);

PyArray3::from_vec3(py, vec3).unwrap();
PyArray3::from_vec3_bound(py, vec3).unwrap();
});
}

Expand Down
2 changes: 1 addition & 1 deletion examples/linalg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ name = "rust_linalg"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.21.0-beta", features = ["extension-module"] }
pyo3 = { version = "0.21.0", features = ["extension-module"] }
numpy = { path = "../.." }
ndarray-linalg = { version = "0.14.1", features = ["openblas-system"] }

Expand Down
11 changes: 7 additions & 4 deletions examples/linalg/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use ndarray_linalg::solve::Inverse;
use numpy::{IntoPyArray, PyArray2, PyReadonlyArray2};
use pyo3::{exceptions::PyRuntimeError, pymodule, types::PyModule, PyResult, Python};
use pyo3::{exceptions::PyRuntimeError, pymodule, types::PyModule, Bound, PyResult, Python};

#[pymodule]
fn rust_linalg<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
fn rust_linalg<'py>(m: &Bound<'py, PyModule>) -> PyResult<()> {
#[pyfn(m)]
fn inv<'py>(py: Python<'py>, x: PyReadonlyArray2<'py, f64>) -> PyResult<&'py PyArray2<f64>> {
fn inv<'py>(
py: Python<'py>,
x: PyReadonlyArray2<'py, f64>,
) -> PyResult<Bound<'py, PyArray2<f64>>> {
let x = x.as_array();
let y = x
.inv()
.map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
Ok(y.into_pyarray(py))
Ok(y.into_pyarray_bound(py))
}
Ok(())
}
2 changes: 1 addition & 1 deletion examples/parallel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ name = "rust_parallel"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.21.0-beta", features = ["extension-module", "multiple-pymethods"] }
pyo3 = { version = "0.21.0", features = ["extension-module", "multiple-pymethods"] }
numpy = { path = "../.." }
ndarray = { version = "0.15", features = ["rayon", "blas"] }
blas-src = { version = "0.8", features = ["openblas"] }
Expand Down
8 changes: 4 additions & 4 deletions examples/parallel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ extern crate blas_src;

use numpy::ndarray::Zip;
use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1, PyReadonlyArray2};
use pyo3::{pymodule, types::PyModule, PyResult, Python};
use pyo3::{pymodule, types::PyModule, Bound, PyResult, Python};

#[pymodule]
fn rust_parallel<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
fn rust_parallel<'py>(m: &Bound<'py, PyModule>) -> PyResult<()> {
#[pyfn(m)]
fn rows_dot<'py>(
py: Python<'py>,
x: PyReadonlyArray2<'py, f64>,
y: PyReadonlyArray1<'py, f64>,
) -> &'py PyArray1<f64> {
) -> Bound<'py, PyArray1<f64>> {
let x = x.as_array();
let y = y.as_array();
let z = Zip::from(x.rows()).par_map_collect(|row| row.dot(&y));
z.into_pyarray(py)
z.into_pyarray_bound(py)
}
Ok(())
}
2 changes: 1 addition & 1 deletion examples/simple/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ name = "rust_ext"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.21.0-beta", features = ["extension-module", "abi3-py37"] }
pyo3 = { version = "0.21.0", features = ["extension-module", "abi3-py37"] }
numpy = { path = "../.." }

[workspace]
26 changes: 13 additions & 13 deletions examples/simple/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use pyo3::{
exceptions::PyIndexError,
pymodule,
types::{PyDict, PyModule},
FromPyObject, PyAny, PyObject, PyResult, Python,
Bound, FromPyObject, PyAny, PyObject, PyResult, Python,
};

#[pymodule]
fn rust_ext<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
fn rust_ext<'py>(m: &Bound<'py, PyModule>) -> PyResult<()> {
// example using generic PyObject
fn head(x: ArrayViewD<'_, PyObject>) -> PyResult<PyObject> {
x.get(0)
Expand Down Expand Up @@ -60,11 +60,11 @@ fn rust_ext<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
a: f64,
x: PyReadonlyArrayDyn<'py, f64>,
y: PyReadonlyArrayDyn<'py, f64>,
) -> &'py PyArrayDyn<f64> {
) -> Bound<'py, PyArrayDyn<f64>> {
let x = x.as_array();
let y = y.as_array();
let z = axpy(a, x, y);
z.into_pyarray(py)
z.into_pyarray_bound(py)
}

// wrapper of `mult`
Expand All @@ -81,8 +81,8 @@ fn rust_ext<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
fn conj_py<'py>(
py: Python<'py>,
x: PyReadonlyArrayDyn<'py, Complex64>,
) -> &'py PyArrayDyn<Complex64> {
conj(x.as_array()).into_pyarray(py)
) -> Bound<'py, PyArrayDyn<Complex64>> {
conj(x.as_array()).into_pyarray_bound(py)
}

// example of how to extract an array from a dictionary
Expand Down Expand Up @@ -125,28 +125,28 @@ fn rust_ext<'py>(_py: Python<'py>, m: &'py PyModule) -> PyResult<()> {
fn polymorphic_add<'py>(
x: SupportedArray<'py>,
y: SupportedArray<'py>,
) -> PyResult<&'py PyAny> {
) -> PyResult<Bound<'py, PyAny>> {
match (x, y) {
(SupportedArray::F64(x), SupportedArray::F64(y)) => Ok(generic_add(
x.readonly().as_array(),
y.readonly().as_array(),
)
.into_pyarray(x.py())
.into()),
.into_pyarray_bound(x.py())
.into_any()),
(SupportedArray::I64(x), SupportedArray::I64(y)) => Ok(generic_add(
x.readonly().as_array(),
y.readonly().as_array(),
)
.into_pyarray(x.py())
.into()),
.into_pyarray_bound(x.py())
.into_any()),
(SupportedArray::F64(x), SupportedArray::I64(y))
| (SupportedArray::I64(y), SupportedArray::F64(x)) => {
let y = y.cast::<f64>(false)?;

Ok(
generic_add(x.readonly().as_array(), y.readonly().as_array())
.into_pyarray(x.py())
.into(),
.into_pyarray_bound(x.py())
.into_any(),
)
}
}
Expand Down
Loading
Loading