-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from PyO3/parallel-example
Add new example showing how to use ndarray's parallel and blas-src features
- Loading branch information
Showing
17 changed files
with
122 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
use ndarray_linalg::solve::Inverse; | ||
use numpy::{IntoPyArray, PyArray2, PyReadonlyArray2}; | ||
use pyo3::exceptions::PyRuntimeError; | ||
use pyo3::prelude::{pymodule, PyErr, PyModule, PyResult, Python}; | ||
use pyo3::{exceptions::PyRuntimeError, pymodule, types::PyModule, PyErr, PyResult, Python}; | ||
|
||
#[pymodule] | ||
fn rust_linalg(_py: Python<'_>, m: &PyModule) -> PyResult<()> { | ||
#[pyfn(m)] | ||
fn inv<'py>(py: Python<'py>, x: PyReadonlyArray2<'py, f64>) -> PyResult<&'py PyArray2<f64>> { | ||
let x = x | ||
.as_array() | ||
let x = x.as_array(); | ||
let y = x | ||
.inv() | ||
.map_err(|e| PyErr::new::<PyRuntimeError, _>(format!("[rust_linalg] {}", e)))?; | ||
Ok(x.into_pyarray(py)) | ||
Ok(y.into_pyarray(py)) | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,5 @@ deps = | |
numpy | ||
pytest | ||
commands = | ||
pip install . | ||
pip install . -v | ||
pytest {posargs} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "numpy-parallel-example" | ||
version = "0.1.0" | ||
authors = ["Yuji Kanagawa <[email protected]>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
name = "rust_parallel" | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
pyo3 = { version = "0.15", features = ["extension-module"] } | ||
numpy = { path = "../.." } | ||
ndarray = { version = "0.15", features = ["rayon", "blas"] } | ||
blas-src = { version = "0.8", features = ["openblas"] } | ||
openblas-src = { version = "0.10", features = ["cblas", "system"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# rust-numpy example extension using optional ndarray features | ||
|
||
An example extension using [optional ndarray features](https://docs.rs/ndarray/latest/ndarray/doc/crate_feature_flags/index.html), parallel execution using Rayon and optimized kernels using BLAS in this case. | ||
|
||
See [simple-extension's README](https://github.com/PyO3/rust-numpy/blob/main/examples/simple-extension/README.md) | ||
for an introduction. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[build-system] | ||
build-backend = "maturin" | ||
requires = ["maturin>=0.12,<0.13"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// We need to link `blas_src` directly, c.f. https://github.com/rust-ndarray/ndarray#how-to-enable-blas-integration | ||
extern crate blas_src; | ||
|
||
use ndarray::Zip; | ||
use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1, PyReadonlyArray2}; | ||
use pyo3::{pymodule, types::PyModule, PyResult, Python}; | ||
|
||
#[pymodule] | ||
fn rust_parallel(_py: Python<'_>, m: &PyModule) -> PyResult<()> { | ||
#[pyfn(m)] | ||
fn rows_dot<'py>( | ||
py: Python<'py>, | ||
x: PyReadonlyArray2<'py, f64>, | ||
y: PyReadonlyArray1<'py, f64>, | ||
) -> &'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) | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import numpy as np | ||
import rust_parallel | ||
|
||
|
||
def test_rows_dot(): | ||
x = np.ones((128, 1024), dtype=np.float64) | ||
y = np.ones((1024,), dtype=np.float64) | ||
z = rust_parallel.rows_dot(x, y) | ||
np.testing.assert_array_almost_equal(z, 1024 * np.ones((128,), dtype=np.float64)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[tox] | ||
skipsdist = True | ||
|
||
[testenv] | ||
deps = | ||
pip | ||
numpy | ||
pytest | ||
commands = | ||
pip install . -v | ||
pytest {posargs} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,5 @@ deps = | |
numpy | ||
pytest | ||
commands = | ||
python -m pip install . | ||
pip install . -v | ||
pytest {posargs} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters