-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
[QUESTION] Numpy Array to C++ - take ownership of data #3126
Comments
If you don't want to copy use |
Is it possible to allocate the memory on the C++ side and pass it back to Python? You can return a buffer from C++ to python like this:
On the Python side, this return value can be used directly as a numpy array. |
I am not quite sure, how to use this with numpy. Do you have some example? In python I have a very simple example:
and I want to pass
I cannot init memory in C++ via |
@MartinPerry I have the same exact question, did you manage to solve your issue? |
@PierreMarchand20 Unfortunately, no |
@MartinPerry @PierreMarchand20 I have the same exact question, did you manage to solve your issue? |
I may have figured out a solution to this! It looks like the array pointer remains valid as long as the
Note that I've tested this by creating a NumPy array in Python, using it to initialize a |
I had similar problem.
In order to move data from numpy ndarray calculated in python snippet to C++ I wrote the following function:
In my case common use of the function will be:
Therefore I need to cast from pybind11::object into pybind11::array and hope that user will pass numpy array as argument.
The solution is not ideal, because it relies on the user in two crucial things, but it works. At leas on my tests) |
Hello, Thanks for the method. I try this method but I make a new numpy array after delete the old one, PyArray::data is replaced by the new array. Have you tried this? |
I have the same exact question. How can I malloc a buffer from c++ and use it On the Python side? |
Is it possible to pass data from numpy to C++ and take ownership of the memory, so its no longer managed by Python? I have large Numpy matrix and I dont want to copy memory. I can use
py::buffer_info
and get pointer to the data, but the pointer is not valid when Python is shut down. Another reason is I want to release data from C++ side once I no longer need them.The text was updated successfully, but these errors were encountered: