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

Proposal: ContextCpu nparray_from_context_array and nparray_to_context_array should perform a copy #138

Open
carlidel opened this issue Jul 11, 2024 · 1 comment

Comments

@carlidel
Copy link

carlidel commented Jul 11, 2024

The current implementation in ContextCpu of nparray_from_context_array and nparray_to_context_array currently reads:

    def nparray_to_context_array(self, arr):
        """
        Moves a numpy array to the device memory. No action is performed by
        this function in the CPU context. The method is provided
        so that the CPU context has an identical API to the GPU ones.

        Args:
            arr (numpy.ndarray): Array to be transferred

        Returns:
            numpy.ndarray: The same array (no copy!).

        """
        return arr

    def nparray_from_context_array(self, dev_arr):
        """
        Moves an array to the device to a numpy array. No action is performed by
        this function in the CPU context. The method is provided so that the CPU
        context has an identical API to the GPU ones.

        Args:
            dev_arr (numpy.ndarray): Array to be transferred
        Returns:
            numpy.ndarray: The same array (no copy!)

        """
        return dev_arr

However, this behavior is extremely different from the one provided in the implementation of ContextCupy and ContextPyopencl, where an explicit copy of an array is made from guest memory to host memory.

This big difference in behavour forces the user to require an extra explicit copy whenever an array manipulation is taken from an XObject, in order to be able to cover both CPU and GPU cases. E.g., if I need to access and save some arrays along the tracking, and I want to use nparray_from_context_array, I am forced to do it like so:

my_data_to_save = nparray_from_context_array(my_xobject.the_data).copy()

in order to have my code operating in the same way both on CPU context and on CUPY/PYOPENCL context....

To avoid this extra (wasteful) copy, and to restore coherence in behavior between different contexts, could you just add a .copy() to the CPU context? Or would it break inner dependencies?

Thanks!

@rdemaria
Copy link
Collaborator

It is possible there could be performances issues depending on where this method is used. I suggest to add another method with the behaviour you suggested for your use case, while evaluating the impact.

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

No branches or pull requests

2 participants