The N-dimensional array (ndarray) — CuPy 14.2.0 documentation

cupy.ndarray

is the CuPy counterpart of NumPy

numpy.ndarray

. It provides an intuitive interface for a fixed-size multidimensional array which resides in a CUDA device.

For the basic concept of ndarrays, please refer to the

NumPy documentation

.

cupy.ndarray

(shape[, dtype, memptr, ...])

Multi-dimensional array on a CUDA device.

Conversion to/from NumPy arrays

#

cupy.ndarray

and

numpy.ndarray

are not implicitly convertible to each other. That means, NumPy functions cannot take

cupy.ndarray

s as inputs, and vice versa.

To convert

numpy.ndarray

to

cupy.ndarray

, use

cupy.array()

or

cupy.asarray()

.

To convert

cupy.ndarray

to

numpy.ndarray

, use

cupy.asnumpy()

or

cupy.ndarray.get()

.

Note that converting between

cupy.ndarray

and

numpy.ndarray

incurs data transfer between the host (CPU) device and the GPU device, which is costly in terms of performance.

cupy.array

(obj[, dtype, copy, order, subok, ...])

Creates an array on the current device.

cupy.asarray

(a[, dtype, order, copy, blocking])

Converts an object to array.

cupy.asnumpy

(a[, stream, order, out, blocking])

Returns an array on the host memory from an arbitrary source array.

Code compatibility features

#

cupy.ndarray

is designed to be interchangeable with

numpy.ndarray

in terms of code compatibility as much as possible. But occasionally, you will need to know whether the arrays you’re handling are

cupy.ndarray

or

numpy.ndarray

. One example is when invoking module-level functions such as

cupy.sum()

or

numpy.sum()

. In such situations,

cupy.get_array_module()

can be used.