-
Notifications
You must be signed in to change notification settings - Fork 125
Add new example showing how to use ndarray's parallel and blas-src features #251
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
294f67e
Add new example showing how to use ndarray's parallel and blas-src fe…
adamreichold 669a228
Move Clippy allow list into source code to simplify running Clippy fo…
adamreichold 28183f1
Limit the scope of allowing clippy::missing_safety_doc to the FFI mod…
adamreichold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 <yuji.kngw.80s.revive@gmail.com>"] | ||
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't look like
blas_src
is used?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No this is actually necessary as the resulting cdylib otherwise does not link to BLAS. https://github.com/rust-ndarray/ndarray#how-to-enable-blas-integration has the details.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will add a comment with this link to the example.