Description
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!