def set(self, arr, stream=None): """Copies an array on the host memory to cuda.ndarray. Args: arr (numpy.ndarray): The source array on the host memory. stream (cupy.cuda.Stream): CUDA stream object. If it is given, the copy runs asynchronously. Otherwise, the copy is synchronous. """ if not isinstance(arr, numpy.ndarray): raise TypeError('Only numpy.ndarray can be set to cupy.ndarray') if self._dtype != arr.dtype: raise TypeError('{} array cannot be set to {} array'.format( arr.dtype, self._dtype)) if self._shape != arr.shape: raise ValueError('Shape mismatch') if not self.flags.c_contiguous: raise RuntimeError('Cannot set to non-contiguous array') arr = numpy.ascontiguousarray(arr) ptr = internal.get_ndarray_ptr(arr) if stream is None: self.data.copy_from_host(ptr, self.nbytes) else: self.data.copy_from_host_async(ptr, self.nbytes, stream)
def get(self, stream=None): """Returns a copy of the array on host memory. Args: stream (cupy.cuda.Stream): CUDA stream object. If it is given, the copy runs asynchronously. Otherwise, the copy is synchronous. Returns: numpy.ndarray: Copy of the array on host memory. """ a_gpu = ascontiguousarray(self) a_cpu = numpy.empty(self._shape, dtype=self._dtype) ptr = internal.get_ndarray_ptr(a_cpu) if stream is None: a_gpu.data.copy_to_host(ptr, a_gpu.nbytes) else: a_gpu.data.copy_to_host_async(ptr, a_gpu.nbytes, stream) return a_cpu
def array(obj, dtype=None, copy=True, ndmin=0): """Creates an array on the current device. This function currently does not support the ``order`` and ``subok`` options. Args: obj: cupy.ndarray object or any other object that can be passed to :func:`numpy.array`. dtype: Data type specifier. copy (bool): If False, this function returns ``obj`` if possible. Otherwise this function always returns a new array. ndmin (int): Minimum number of dimensions. Ones are inserated to the head of the shape if needed. Returns: cupy.ndarray: An array on the current device. .. seealso:: :func:`numpy.array` """ # TODO(beam2d): Support order and subok options if isinstance(obj, cupy.ndarray): if dtype is None: dtype = obj.dtype a = obj.astype(dtype, copy) ndim = a.ndim if ndmin > ndim: a.shape = (1,) * (ndmin - ndim) + a.shape return a else: a_cpu = numpy.array(obj, dtype=dtype, copy=False, ndmin=ndmin) if a_cpu.ndim > 0: a_cpu = numpy.ascontiguousarray(a_cpu) a = cupy.ndarray(a_cpu.shape, dtype=a_cpu.dtype) a.data.copy_from_host(internal.get_ndarray_ptr(a_cpu), a.nbytes) if a_cpu.dtype == a.dtype: return a else: return a.view(dtype=a_cpu.dtype)
def array(obj, dtype=None, copy=True, ndmin=0): """Creates an array on the current device. This function currently does not support the ``order`` and ``subok`` options. Args: obj: cupy.ndarray object or any other object that can be passed to :func:`numpy.array`. dtype: Data type specifier. copy (bool): If False, this function returns ``obj`` if possible. Otherwise this function always returns a new array. ndmin (int): Minimum number of dimensions. Ones are inserated to the head of the shape if needed. Returns: cupy.ndarray: An array on the current device. .. seealso:: :func:`numpy.array` """ # TODO(beam2d): Support order and subok options if isinstance(obj, cupy.ndarray): if dtype is None: dtype = obj.dtype a = obj.astype(dtype, copy) ndim = a.ndim if ndmin > ndim: a.shape = (1, ) * (ndmin - ndim) + a.shape return a else: a_cpu = numpy.array(obj, dtype=dtype, copy=False, ndmin=ndmin) if a_cpu.ndim > 0: a_cpu = numpy.ascontiguousarray(a_cpu) a = cupy.ndarray(a_cpu.shape, dtype=a_cpu.dtype) a.data.copy_from_host(internal.get_ndarray_ptr(a_cpu), a.nbytes) if a_cpu.dtype == a.dtype: return a else: return a.view(dtype=a_cpu.dtype)