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

Apply numpy ufunc to &mut Vec in rust heap #221

Open
ritchie46 opened this issue Nov 15, 2021 · 3 comments
Open

Apply numpy ufunc to &mut Vec in rust heap #221

ritchie46 opened this issue Nov 15, 2021 · 3 comments
Labels

Comments

@ritchie46
Copy link

Would it be possible to accept any numpy ufunc as &PyAny and apply that to a mutable vec allocated in Rust?

@adamreichold
Copy link
Member

I guess you are asking specifically if this can be done without copying the values into a NumPy array and then back into the Vec? Is the Vec assumed to represent one-dimensional data?

@ritchie46
Copy link
Author

I guess you are asking specifically if this can be done without copying the values into a NumPy array and then back into the Vec?

Indeed. :)

Yes all 1D contiguous data.

@adamreichold
Copy link
Member

adamreichold commented Jan 24, 2022

I thought about this and I am sceptical that it is possible with a safe API. While we could mem::take that &mut Vec and turn the Vec into a &PyArray1 without copying, but it will be difficult to recover ownership of the Vec. We do stash it in the arrays base object which we could inspect and downcast. But the array itself almost surely has an elevated reference count (just passing it to a function elevates it to two) so that we cannot ensure that there are no more aliasing references to the array we are trying to destructure even if the base object (an instance of our PySliceContainer) has a reference count of one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants