-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[python] #38 Create 64 byte aligned writable numpy array
- Loading branch information
Showing
5 changed files
with
60 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,35 @@ | ||
use numpy::PyArray1; | ||
use pyo3::prelude::*; | ||
use pyo3::wrap_pyfunction; | ||
|
||
pub mod dataframe; | ||
pub mod error; | ||
pub mod npy; | ||
pub mod series; | ||
|
||
use crate::{dataframe::PyDataFrame, series::PySeries}; | ||
|
||
macro_rules! create_aligned_buffer { | ||
($name:ident, $type:ty) => { | ||
#[pyfunction] | ||
pub fn $name(size: usize) -> Py<PyArray1<$type>> { | ||
npy::aligned_array::<$type>(size) | ||
} | ||
}; | ||
} | ||
|
||
create_aligned_buffer!(aligned_array_f32, f32); | ||
create_aligned_buffer!(aligned_array_f64, f64); | ||
create_aligned_buffer!(aligned_array_i32, i32); | ||
create_aligned_buffer!(aligned_array_i64, i64); | ||
|
||
#[pymodule] | ||
fn polars(_py: Python, m: &PyModule) -> PyResult<()> { | ||
m.add_class::<PySeries>()?; | ||
m.add_class::<PyDataFrame>()?; | ||
m.add_class::<PySeries>().unwrap(); | ||
m.add_class::<PyDataFrame>().unwrap(); | ||
m.add_wrapped(wrap_pyfunction!(aligned_array_f32)).unwrap(); | ||
m.add_wrapped(wrap_pyfunction!(aligned_array_f64)).unwrap(); | ||
m.add_wrapped(wrap_pyfunction!(aligned_array_i32)).unwrap(); | ||
m.add_wrapped(wrap_pyfunction!(aligned_array_i64)).unwrap(); | ||
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,12 @@ | ||
use numpy::{Element, PyArray1}; | ||
use polars::prelude::*; | ||
use pyo3::prelude::*; | ||
|
||
/// Create an empty numpy array arrows 64 byte alignment | ||
pub fn aligned_array<T: Element>(size: usize) -> Py<PyArray1<T>> { | ||
let mut buf: Vec<T> = Vec::with_capacity_aligned(size); | ||
unsafe { buf.set_len(size) } | ||
let gil = Python::acquire_gil(); | ||
let python = gil.python(); | ||
PyArray1::from_vec(python, buf).to_owned() | ||
} |
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