Exemple #1
0
 def test_inner_error(self):
     orig = cupy.ndarray
     with pytest.raises(numpy.AxisError):
         with testing.AssertFunctionIsCalled('cupy.ndarray'):
             cupy.ndarray((2, 3), numpy.float32)
             raise numpy.AxisError('foo')
     assert cupy.ndarray is orig
Exemple #2
0
 def test_fail_called_twice(self):
     orig = cupy.ndarray
     with pytest.raises(AssertionError):
         with testing.AssertFunctionIsCalled('cupy.ndarray'):
             cupy.ndarray((2, 3), numpy.float32)
             cupy.ndarray((2, 3), numpy.float32)
     assert cupy.ndarray is orig
Exemple #3
0
def get_synthetic_warped_circle(nslices):
    # get a subsampled circle
    fname_cicle = get_fnames("reg_o")
    circle = np.load(fname_cicle)[::4, ::4].astype(floating)
    circle = cp.asarray(circle)

    # create a synthetic invertible map and warp the circle
    d, dinv = dipy_vfu.create_harmonic_fields_2d(64, 64, 0.1, 4)
    d = cp.asarray(d, dtype=floating)
    dinv = cp.asarray(dinv, dtype=floating)
    mapping = DiffeomorphicMap(2, (64, 64))
    mapping.forward, mapping.backward = d, dinv
    wcircle = mapping.transform(circle)

    if nslices == 1:
        return circle, wcircle

    # normalize and form the 3d by piling slices
    circle = (circle - circle.min()) / (circle.max() - circle.min())
    circle_3d = cp.ndarray(circle.shape + (nslices, ), dtype=floating)
    circle_3d[...] = circle[..., None]
    circle_3d[..., 0] = 0
    circle_3d[..., -1] = 0

    # do the same with the warped circle
    wcircle = (wcircle - wcircle.min()) / (wcircle.max() - wcircle.min())
    wcircle_3d = cp.ndarray(wcircle.shape + (nslices, ), dtype=floating)
    wcircle_3d[...] = wcircle[..., None]
    wcircle_3d[..., 0] = 0
    wcircle_3d[..., -1] = 0

    return circle_3d, wcircle_3d
Exemple #4
0
def inv(x):
    """ Batch version of np.linalg.inv
    """
    if get_array_module(x) is np:
        return np.linalg.inv(x)

    # cupy case
    if x.ndim == 2:
        return cupy.linalg.inv(x)
    elif x.ndim == 3:
        inv = cupy.ndarray(x.shape, x.dtype)
        for i in len(inv):
            inv[i] = cupy.linalg.inv(x[i])
        return inv
    elif x.ndim == 4:
        inv = cupy.ndarray(x.shape, x.dtype)
        for i in range(inv.shape[0]):
            for j in range(inv.shape[1]):
                inv[i, j] = cupy.linalg.inv(x[i, j])
        return inv
    elif x.ndim == 5:
        inv = cupy.ndarray(x.shape, x.dtype)
        for i in range(inv.shape[0]):
            for j in range(inv.shape[1]):
                for k in range(inv.shape[2]):
                    inv[i, j, k] = cupy.linalg.inv(x[i, j, k])
        return inv
    else:
        raise ValueError('linalg_inv only support x.ndim == 5. Given', x.ndim)
Exemple #5
0
 def test_extend_conv(self):
     objsize = self.pf.shape
     inisize = self.gaussff.shape
     imxsize = objsize[0] + inisize[0]
     imysize = objsize[1] + inisize[1]
     cp.cuda.Device(0).use
     objtmp = cp.ndarray([imxsize, imysize])
     initmp = cp.ndarray([imxsize, imysize])
     objtmp[0:objsize[0], 0:objsize[1]] = self.pf
     initmp[0:inisize[0], 0:inisize[1]] = self.gaussff
     objtmp = self.shift(objtmp, imxsize / 2 - objsize[0] / 2,
                         imysize / 2 - objsize[1] / 2)
     initmp = self.shift(initmp, imxsize / 2 - inisize[0] / 2,
                         imysize / 2 - inisize[1] / 2)
     objfft = self.shift(
         cp.fft.fft2(objtmp) * cp.sqrt(imxsize * imysize), imxsize / 2,
         imysize / 2)
     inifft = self.shift(
         cp.fft.fft2(initmp) * cp.sqrt(imxsize * imysize), imxsize / 2,
         imysize / 2)
     convfft = self.shift(
         cp.fft.ifft2(self.shift(objfft * inifft, imxsize / 2,
                                 imysize / 2)), imxsize / 2, imysize / 2)
     self.conv = cp.real(convfft)
     return float(self.conv)
Exemple #6
0
 def nvst_srstdcal_temp(self):
     sitfline = fits.open(self.sitfpath)[0].data
     sitfsize = sitfline.shape
     R0_num = sitfsize[1]
     cp.cuda.Device(0).use
     setf = cp.ndarray([self.srsize, self.srsize])
     sitf = cp.ndarray([self.srsize, self.srsize])
     sitfcube = cp.ndarray([self.srsize, self.srsize, R0_num])
     self.sr_standard = cp.ndarray([self.srsize, self.srsize, R0_num])
     multipre = sitfsize[0] * self.maxfre * 2.0 / self.srsize
     index_matrix = cp.ndarray([self.srsize, self.srsize])
     x = cp.transpose(cp.mgrid[0:self.srsize,
                               0:self.srsize][0]) - self.srsize / 2
     y = cp.mgrid[0:self.srsize, 0:self.srsize][0] - self.srsize / 2
     indexmatrix = cp.round_(cp.sqrt(x**2 + y**2) * multipre)
     for index in range(R0_num):
         self.R0 = self.start_r0 + self.step_r0 * index
         sitfcube[:, :, index] = sitfline[index, indexmatrix].reshape(
             self.srsize, self.srsize)
         sitf = sitfcube[:, :, index]
         sitf = sitf / sitf[self.srsize / 2, self.srsize / 2]
         setf = nvst_tel_sotf()
         sr = setf**2 / (sitf + 0.0000001 * cp.max(sitf))
         sr = sr / sr[self.srsize / 2, self.srsize / 2]
         self.sr_standard[:, :, index] = sr
     print('标准谱比计算完成')
Exemple #7
0
 def test_strides_is_given_and_order_is_ignored(self):
     buf = cupy.ndarray(20, numpy.uint8)
     a = cupy.ndarray((2, 3),
                      numpy.float32,
                      buf.data,
                      strides=(8, 4),
                      order='C')
     assert a.strides == (8, 4)
Exemple #8
0
def update_once(model):
    opt = chainer.optimizers.MomentumSGD()
    opt.setup(model)
    import cupy as cp
    imgs = cp.ndarray((1, 3, 224, 224), dtype=np.float32)
    labels = cp.ndarray((1, ), dtype=np.int32)
    labels += 1
    model(imgs, labels)
    opt.update()
def main():
    parser = argparse.ArgumentParser(description='ChainerMN example: MNIST')
    parser.add_argument('--late',
                        '-l',
                        action='store_true',
                        help='late join to the ring')
    parser.add_argument('--np',
                        '-n',
                        type=int,
                        required=True,
                        help='Number of processes')
    parser.add_argument('--bind',
                        '-p',
                        type=str,
                        required=True,
                        help='address to bind gRPC server')
    parser.add_argument('--etcd', '-c', type=str, help='etcd location')
    # parser.add_argument('--rank', '-r', type=int)
    args = parser.parse_args()

    n = args.np
    bind = args.bind
    policy = MinMaxPolicy(n, n, block=True)

    def uid_gen(intra_rank):
        chainer.cuda.get_device_from_id(intra_rank).use()
        return json.dumps(nccl.get_unique_id())

    comm = NcclCommunicator(policy=policy, bind=bind)
    print(comm.intra_rank)

    nccl_comm = policy.nccl_comm
    stream = cupy.cuda.Stream.null

    a = cupy.ndarray([2, 2, 2], dtype=np.float32)
    b = cupy.ndarray([2, 2, 2], dtype=np.float32)

    a.real[:] = 2
    print(a)

    nccl_comm.allReduce(a.data, b.data, 8, nccl.NCCL_FLOAT32, nccl.NCCL_SUM,
                        stream.ptr)
    print(b)

    a.real[:] = comm.rank
    print('before bcast>', a)
    nccl_comm.bcast(a.data, 8, nccl.NCCL_FLOAT32, 0, stream.ptr)
    print('after bcast>', a)

    a.real[:] = comm.rank
    print('allreduce_obj', a)
    c = comm.allreduce_obj(a)
    print('after allreduce_obj', c)
    comm.leave()
Exemple #10
0
    def test_memptr_with_strides(self):
        buf = cupy.ndarray(20, numpy.uint8)
        memptr = buf.data

        # self-overlapping strides
        a = cupy.ndarray((2, 3), numpy.float32, memptr, strides=(8, 4))
        assert a.strides == (8, 4)

        a[:] = 1
        a[0, 2] = 4
        assert float(a[1, 0]) == 4
def cupy_ndarray(cbuf):
    """Return cupy.ndarray view of a pyarrow.cuda.CudaBuffer.
    """
    import cupy
    return cupy.ndarray(cbuf.size,
                        dtype=cupy.uint8,
                        memptr=cupy_cuda_MemoryPointer(cbuf))
def test_chainerx_to_cupy_noncontiguous():
    dtype = 'float32'
    a_chx = chainerx.arange(12, dtype=dtype, device='cuda:0').reshape(
        (2, 6))[::-1, ::2]
    offset = a_chx.offset

    # test preconditions
    assert offset > 0
    assert not a_chx.is_contiguous

    a_cupy = cupy.ndarray(
        a_chx.shape,
        cupy.dtype(a_chx.dtype.name),
        cupy.cuda.MemoryPointer(
            cupy.cuda.UnownedMemory(a_chx.data_ptr, a_chx.data_size, a_chx, 0),
            offset),
        strides=a_chx.strides,
    )

    assert a_chx.strides == a_cupy.strides
    chainerx.testing.assert_array_equal_ex(a_chx,
                                           a_cupy.get(),
                                           strides_check=False)

    a_cupy[1, 1] = 53

    assert a_chx.strides == a_cupy.strides
    chainerx.testing.assert_array_equal_ex(a_chx,
                                           a_cupy.get(),
                                           strides_check=False)
Exemple #13
0
def unpackbits(myarray):
    """Unpacks elements of a uint8 array into a binary-valued output array.

    This function currently does not support ``axis`` option.

    Args:
        myarray (cupy.ndarray): Input array.

    Returns:
        cupy.ndarray: The unpacked array.

    .. seealso:: :func:`numpy.unpackbits`
    """
    if myarray.dtype != cupy.uint8:
        raise TypeError('Expected an input array of unsigned byte data type')

    unpacked = cupy.ndarray((myarray.size * 8), dtype=cupy.uint8)

    cupy.ElementwiseKernel(
        'raw uint8 myarray', 'T unpacked',
        'unpacked = (myarray[i / 8] >> (7 - i % 8)) & 1;',
        'unpackbits_kernel'
    )(myarray, unpacked)

    return unpacked
Exemple #14
0
def unpackbits(myarray):
    """Unpacks elements of a uint8 array into a binary-valued output array.

    This function currently does not support ``axis`` option.

    Args:
        myarray (cupy.ndarray): Input array.

    Returns:
        cupy.ndarray: The unpacked array.

    .. seealso:: :func:`numpy.unpackbits`
    """
    if myarray.dtype != cupy.uint8:
        raise TypeError('Expected an input array of unsigned byte data type')

    unpacked = cupy.ndarray((myarray.size * 8), dtype=cupy.uint8)

    cupy.ElementwiseKernel(
        'raw uint8 myarray', 'T unpacked',
        'unpacked = (myarray[i / 8] >> (7 - i % 8)) & 1;',
        'unpackbits_kernel'
    )(myarray, unpacked)

    return unpacked
Exemple #15
0
    def processing(self):
        try:
            os.mkdir(self.outputpath)
        except Exception as e:
            print("warning:folder have existed")
        obj_item = os.listdir(self.obj_inputpath)
        dark_item = os.listdir(self.dark_inputpath)
        for i in obj_item:
            data = fits.open(self.obj_inputpath + '/' + str(i))[0].data
            M, N = data.shape[0], data.shape[1]
            break
        dark_data = cp.ndarray((M, N), dtype=np.float32)
        dark_data = 0
        cp.cuda.Device(0).use
        with cp.cuda.Device(0):
            for i in dark_item:
                #dark_data +=fits.open(self.dark_inputpath+"//"+i)[0].data
                dark_data += cp.array(fits.open(self.dark_inputpath + "/" +
                                                i)[0].data,
                                      dtype='float32')

            dark_data = dark_data / len(dark_item)
            fits.writeto(self.outputpath + '/' + 'dark.fits', cp.asnum)
            for i in obj_item:
                data = cp.array(cp.array(
                    fits.open(self.obj_inputpath + '/' + str(i))[0].data) -
                                dark_data,
                                dtype='float32')
                #data = np.array(fits.open(self.obj_inputpath+'\\'+str(i))[0].data - dark_data,dtype='float32')
                fits.writeto(self.outputpath + "/dark_" + str(i),
                             cp.asnumpy(data),
                             overwrite=True)
Exemple #16
0
def zeros_like(a, dtype=None, order='K'):
    """Returns an array of zeros with same shape and dtype as a given array.

    This function currently does not support ``order`` and ``subok`` options.

    Args:
        a (cupy.ndarray): Base array.
        dtype: Data type specifier. The dtype of ``a`` is used by default.
        order ({'C', 'F', 'A', or 'K'}): Overrides the memory layout of the
            result. 'C' means C-order, 'F' means F-order, 'A' means 'F' if
            ``a`` is Fortran contiguous, 'C' otherwise. 'K' means match the
            layout of ``a`` as closely as possible.\

    Returns:
        cupy.ndarray: An array filled with zeros.

    .. seealso:: :func:`numpy.zeros_like`

    """
    if dtype is None:
        dtype = a.dtype
    order, strides, memptr = _new_like_order_and_strides(a, dtype, order)
    a = cupy.ndarray(a.shape, dtype, memptr, strides, order)
    a.data.memset_async(0, a.nbytes)
    return a
Exemple #17
0
 def test_spy_ndarray(self):
     orig = cupy.ndarray
     with testing.AssertFunctionIsCalled('cupy.ndarray',
                                         wraps=cupy.ndarray):
         a = cupy.ndarray((2, 3), numpy.float32)
     assert cupy.ndarray is orig
     assert isinstance(a, cupy.ndarray)
Exemple #18
0
 def test_can_use_cub_oversize_input3(self):
     # full reduction with 2^63-1 elements
     mem = memory.alloc(100)
     max_num = sys.maxsize
     a = cupy.ndarray((max_num, ), dtype=cupy.int8, memptr=mem)
     b = cupy.empty((), dtype=cupy.int8)
     assert self.can_use([a], [b], (0, ), ()) is None
Exemple #19
0
def _create_contraction_plan(desc, algo, ws_pref):
    """Create a contraction plan"""
    handle = get_handle()
    key = (handle.ptr, algo)
    if key in _contraction_finds:
        find = _contraction_finds[key]
    else:
        find = cutensor.ContractionFind()
        cutensor.initContractionFind(handle, find, algo)
        _contraction_finds[key] = find

    ws_allocation_success = False
    for pref in (ws_pref, cutensor.WORKSPACE_MIN):
        ws_size = cutensor.contractionGetWorkspace(handle, desc, find, pref)
        try:
            ws = cupy.ndarray((ws_size, ), dtype=numpy.int8)
            ws_allocation_success = True
        except Exception:
            warnings.warn('cuTENSOR: failed to allocate memory of workspace '
                          'with preference ({}) and size ({}).'
                          ''.format(pref, ws_size))
        if ws_allocation_success:
            break
    if not ws_allocation_success:
        raise RuntimeError('cuTENSOR: failed to allocate memory of workspace.')

    key = (handle.ptr, desc.ptr, find.ptr, ws_size)
    if key in _contraction_plans:
        plan = _contraction_plans[key]
    else:
        plan = cutensor.ContractionPlan()
        cutensor.initContractionPlan(handle, plan, desc, find, ws_size)
        _contraction_plans[key] = plan

    return plan, ws, ws_size
Exemple #20
0
def empty_like(a, dtype=None, order='K', subok=None, shape=None):
    """Returns a new array with same shape and dtype of a given array.

    This function currently does not support ``subok`` option.

    Args:
        a (cupy.ndarray): Base array.
        dtype: Data type specifier. The data type of ``a`` is used by default.
        order ({'C', 'F', 'A', or 'K'}): Overrides the memory layout of the
            result. ``'C'`` means C-order, ``'F'`` means F-order, ``'A'`` means
            ``'F'`` if ``a`` is Fortran contiguous, ``'C'`` otherwise.
            ``'K'`` means match the layout of ``a`` as closely as possible.
        subok: Not supported yet, must be None.
        shape (int or tuple of ints): Overrides the shape of the result. If
            ``order='K'`` and the number of dimensions is unchanged, will try
            to keep order, otherwise, ``order='C'`` is implied.

    Returns:
        cupy.ndarray: A new array with same shape and dtype of ``a`` with
        elements not initialized.

    .. seealso:: :func:`numpy.empty_like`

    """
    if subok is not None:
        raise TypeError('subok is not supported yet')
    if dtype is None:
        dtype = a.dtype

    order, strides, memptr = _new_like_order_and_strides(
        a, dtype, order, shape)
    shape = shape if shape else a.shape
    return cupy.ndarray(shape, dtype, memptr, strides, order)
Exemple #21
0
def test_chainerx_to_cupy_noncontiguous():
    dtype = 'float32'
    a_chx = chainerx.arange(
        12, dtype=dtype, device='cuda:0').reshape((2, 6))[::-1, ::2]
    offset = a_chx.offset

    # test preconditions
    assert offset > 0
    assert not a_chx.is_contiguous

    a_cupy = cupy.ndarray(
        a_chx.shape,
        cupy.dtype(a_chx.dtype.name),
        cupy.cuda.MemoryPointer(cupy.cuda.UnownedMemory(
            a_chx.data_ptr,
            a_chx.data_size,
            a_chx,
            0), offset),
        strides=a_chx.strides,
    )

    assert a_chx.strides == a_cupy.strides
    chainerx.testing.assert_array_equal_ex(
        a_chx, a_cupy.get(), strides_check=False)

    a_cupy[1, 1] = 53

    assert a_chx.strides == a_cupy.strides
    chainerx.testing.assert_array_equal_ex(
        a_chx, a_cupy.get(), strides_check=False)
Exemple #22
0
def empty_like(a, dtype=None, order='K'):
    """Returns a new array with same shape and dtype of a given array.

    This function currently does not support ``order`` and ``subok`` options.

    Args:
        a (cupy.ndarray): Base array.
        dtype: Data type specifier. The data type of ``a`` is used by default.
        order ({'C', 'F', 'A', or 'K'}): Overrides the memory layout of the
            result. 'C' means C-order, 'F' means F-order, 'A' means 'F' if
            ``a`` is Fortran contiguous, 'C' otherwise. 'K' means match the
            layout of ``a`` as closely as possible.

    Returns:
        cupy.ndarray: A new array with same shape and dtype of ``a`` with
        elements not initialized.

    .. seealso:: :func:`numpy.empty_like`

    """
    if dtype is None:
        dtype = a.dtype

    order, strides, memptr = _new_like_order_and_strides(a, dtype, order)
    return cupy.ndarray(a.shape, dtype, memptr, strides, order)
Exemple #23
0
 def cvt(a):
     a_pointer, read_only_flag = a.__array_interface__['data']
     aptr = cp.cuda.MemoryPointer(
         cp.cuda.memory.UnownedMemory(a_pointer, a.size * a.itemsize, a, 0),
         0)
     a = cp.ndarray(a.shape, a.dtype, aptr)
     return a
Exemple #24
0
def test_chainerx_to_cupy_contiguous():
    dtype = 'float32'
    a_chx = chainerx.arange(6, dtype=dtype, device='cuda:0').reshape((2, 3))
    a_cupy = cupy.ndarray(
        a_chx.shape,
        cupy.dtype(a_chx.dtype.name),
        cupy.cuda.MemoryPointer(cupy.cuda.UnownedMemory(
            a_chx.data_ptr + a_chx.offset,
            a_chx.data_size,
            a_chx,
            0), 0),
        strides=a_chx.strides,
    )

    assert a_cupy.device.id == 0
    chainerx.testing.assert_array_equal_ex(a_chx, a_cupy.get())

    # Write to a_cupy
    a_cupy[0, 1] = 8
    chainerx.testing.assert_array_equal_ex(
        a_chx, numpy.array([[0, 8, 2], [3, 4, 5]], dtype))

    # Write to a_chx
    a_chx += 1
    chainerx.testing.assert_array_equal_ex(
        a_cupy.get(), numpy.array([[1, 9, 3], [4, 5, 6]], dtype))
Exemple #25
0
def full(shape, fill_value, dtype=None):
    """Returns a new array of given shape and dtype, filled with a given value.

    This function currently does not support ``order`` option.

    Args:
        shape (tuple of ints): Dimensionalities of the array.
        fill_value: A scalar value to fill a new array.
        dtype: Data type specifier.

    Returns:
        cupy.ndarray: An array filled with ``fill_value``.

    .. seealso:: :func:`numpy.full`

    """
    # TODO(beam2d): Support ordering option
    if dtype is None:
        if isinstance(fill_value, cupy.ndarray):
            dtype = fill_value.dtype
        else:
            dtype = numpy.array(fill_value).dtype
    a = cupy.ndarray(shape, dtype)
    a.fill(fill_value)
    return a
Exemple #26
0
def unpackbits(a, axis=None, bitorder='big'):
    """Unpacks elements of a uint8 array into a binary-valued output array.

    This function currently does not support ``axis`` option.

    Args:
        a (cupy.ndarray): Input array.
        bitorder (str, optional): bit order to use when unpacking the array,
            allowed values are `'little'` and `'big'`. Defaults to `'big'`.

    Returns:
        cupy.ndarray: The unpacked array.

    .. seealso:: :func:`numpy.unpackbits`
    """
    if a.dtype != cupy.uint8:
        raise TypeError('Expected an input array of unsigned byte data type')

    if axis is not None:
        raise NotImplementedError('axis option is not supported yet')

    if bitorder not in ('big', 'little'):
        raise ValueError("bitorder must be either 'big' or 'little'")

    unpacked = cupy.ndarray((a.size * 8), dtype=cupy.uint8)
    return _unpackbits_kernel[bitorder](a, unpacked)
Exemple #27
0
def from_pycuda(pycuda_arr, device=0):
    """Read in gpuarray from PyCUDA and output CuPy array

    Parameters
    ----------
    pycuda_arr : PyCUDA gpuarray
    device : int
        GPU Device ID

    Returns
    -------
    cupy_arr : CuPy ndarray

    """

    cupy_arr = cp.ndarray(
        pycuda_arr.shape,
        cp.dtype(pycuda_arr.dtype),
        cp.cuda.MemoryPointer(
            cp.cuda.UnownedMemory(
                pycuda_arr.ptr, pycuda_arr.size, pycuda_arr, device
            ),
            0,
        ),
        strides=pycuda_arr.strides,
    )

    return cupy_arr
def cupy_ndarray(xd_arr):
    """Return cupy.ndarray view of a xnd.xnd in CUDA device.
    """
    import cupy
    return cupy.ndarray(xd_arr.type.datasize,
                        dtype=cupy.uint8,
                        memptr=cupy_cuda_MemoryPointer(xd_arr))
Exemple #29
0
def _as_strided(x, shape=None, strides=None):
    """
    Create a view into the array with the given shape and strides.
    .. warning:: This function has to be used with extreme care, see notes.
    Parameters
    ----------
    x : ndarray
        Array to create a new.
    shape : sequence of int, optional
        The shape of the new array. Defaults to ``x.shape``.
    strides : sequence of int, optional
        The strides of the new array. Defaults to ``x.strides``.
    Returns
    -------
    view : ndarray

    Notes
    -----
    ``as_strided`` creates a view into the array given the exact strides
    and shape. This means it manipulates the internal data structure of
    ndarray and, if done incorrectly, the array elements can point to
    invalid memory and can corrupt results or crash your program.
    """
    shape = x.shape if shape is None else tuple(shape)
    strides = x.strides if strides is None else tuple(strides)

    return cp.ndarray(shape=shape,
                      dtype=x.dtype,
                      memptr=x.data,
                      strides=strides)
Exemple #30
0
def full(shape, fill_value, dtype=None, order='C'):
    """Returns a new array of given shape and dtype, filled with a given value.

    This function currently does not support ``order`` option.

    Args:
        shape (int or tuple of ints): Dimensionalities of the array.
        fill_value: A scalar value to fill a new array.
        dtype: Data type specifier.
        order ({'C', 'F'}): Row-major (C-style) or column-major
            (Fortran-style) order.

    Returns:
        cupy.ndarray: An array filled with ``fill_value``.

    .. seealso:: :func:`numpy.full`

    """
    if dtype is None:
        if isinstance(fill_value, cupy.ndarray):
            dtype = fill_value.dtype
        else:
            dtype = numpy.array(fill_value).dtype
    a = cupy.ndarray(shape, dtype, order=order)
    a.fill(fill_value)
    return a
Exemple #31
0
def full_like(a, fill_value, dtype=None, order='K', subok=None, shape=None):
    """Returns a full array with same shape and dtype as a given array.

    This function currently does not support ``subok`` option.

    Args:
        a (cupy.ndarray): Base array.
        fill_value: A scalar value to fill a new array.
        dtype: Data type specifier. The dtype of ``a`` is used by default.
        order ({'C', 'F', 'A', or 'K'}): Overrides the memory layout of the
            result. 'C' means C-order, 'F' means F-order, 'A' means 'F' if
            ``a`` is Fortran contiguous, 'C' otherwise. 'K' means match the
            layout of ``a`` as closely as possible.
        subok: Not supported yet, must be None.
        shape (int or tuple of ints): Overrides the shape of the result. If
            order='K' and the number of dimensions is unchanged, will try to
            keep order, otherwise, order='C' is implied.

    Returns:
        cupy.ndarray: An array filled with ``fill_value``.

    .. seealso:: :func:`numpy.full_like`

    """
    if subok is not None:
        raise TypeError('subok is not supported yet')
    if dtype is None:
        dtype = a.dtype

    order, strides, memptr = _new_like_order_and_strides(
        a, dtype, order, shape)
    shape = shape if shape else a.shape
    a = cupy.ndarray(shape, dtype, memptr, strides, order)
    a.fill(fill_value)
    return a
Exemple #32
0
def deserialize_cupy_array(header, frames):
    (frame,) = frames
    if not isinstance(frame, cupy.ndarray):
        frame = PatchedCudaArrayInterface(frame)
    arr = cupy.ndarray(
        header["shape"], dtype=header["typestr"], memptr=cupy.asarray(frame).data
    )
    return arr
Exemple #33
0
def vandermonde(X, M):
    xx = cp.ndarray(shape=(cp.size(X), M), dtype=float)
    for i in range(cp.size(X)):
        cnt = 1.0
        for j in range(M):
            xx[i, j] = float(cnt)
            cnt = cnt * X[i]
    return xx
Exemple #34
0
def _array_to_gpu(array, device, stream):
    if array is None:
        return None

    if isinstance(array, chainerx.ndarray):
        # TODO(niboshi): Update this logic once both CuPy and ChainerX support
        # the array interface.
        if array.device.backend.name == 'cuda':
            # Convert to cupy.ndarray on the same device as source array
            array = cupy.ndarray(
                array.shape,
                array.dtype,
                cupy.cuda.MemoryPointer(
                    cupy.cuda.UnownedMemory(
                        array.data_ptr + array.offset,
                        array.data_size,
                        array,
                        array.device.index),
                    0),
                strides=array.strides)
        else:
            array = chainerx.to_numpy(array)
    elif isinstance(array, (numpy.number, numpy.bool_)):
        array = numpy.asarray(array)
    elif isinstance(array, intel64.mdarray):
        array = numpy.asarray(array)

    if isinstance(array, ndarray):
        if array.device == device:
            return array
        is_numpy = False
    elif isinstance(array, numpy.ndarray):
        is_numpy = True
    else:
        raise TypeError(
            'The array sent to gpu must be an array or a NumPy scalar.'
            '\nActual type: {0}.'.format(type(array)))

    if stream is not None:
        with device:
            with stream:
                if is_numpy:
                    return cupy.asarray(array)
                # Need to make a copy when an array is copied to another device
                return cupy.array(array, copy=True)

    with device:
        if is_numpy:
            return cupy.asarray(array)
        # Need to make a copy when an array is copied to another device
        return cupy.array(array, copy=True)
def _to_cupy(array):
    assert cupy is not None
    # Convert to cupy.ndarray on the same device as source array
    return cupy.ndarray(
        array.shape,
        array.dtype,
        cupy.cuda.MemoryPointer(
            cupy.cuda.UnownedMemory(
                array.data_ptr + array.offset,
                array.data_size,
                array,
                array.device.index),
            0),
        strides=array.strides)
Exemple #36
0
def empty(shape, dtype=float, order='C'):
    """Returns an array without initializing the elements.

    Args:
        shape (tuple of ints): Dimensionalities of the array.
        dtype: Data type specifier.
        order ({'C', 'F'}): Row-major (C-style) or column-major
            (Fortran-style) order.

    Returns:
        cupy.ndarray: A new array with elements not initialized.

    .. seealso:: :func:`numpy.empty`

    """
    return cupy.ndarray(shape, dtype=dtype, order=order)
Exemple #37
0
def test_chainerx_to_cupy_nondefault_device():
    dtype = 'float32'
    a_chx = chainerx.arange(6, dtype=dtype, device='cuda:1').reshape((2, 3))
    a_cupy = cupy.ndarray(
        a_chx.shape,
        cupy.dtype(a_chx.dtype.name),
        cupy.cuda.MemoryPointer(cupy.cuda.UnownedMemory(
            a_chx.data_ptr + a_chx.offset,
            a_chx.data_size,
            a_chx,
            -1), 0),
        strides=a_chx.strides,
    )

    assert a_cupy.device.id == 1
    chainerx.testing.assert_array_equal_ex(a_chx, a_cupy.get())
Exemple #38
0
def empty(shape, dtype=float):
    """Returns an array without initializing the elements.

    This function currently does not support ``order`` option.

    Args:
        shape (tuple of ints): Dimensionalities of the array.
        dtype: Data type specifier.

    Returns:
        cupy.ndarray: A new array with elements not initialized.

    .. seealso:: :func:`numpy.empty`

    """
    # TODO(beam2d): Support ordering option
    return cupy.ndarray(shape, dtype=dtype)
Exemple #39
0
def zeros(shape, dtype=float, order='C'):
    """Returns a new array of given shape and dtype, filled with zeros.

    Args:
        shape (tuple of ints): Dimensionalities of the array.
        dtype: Data type specifier.
        order ({'C', 'F'}): Row-major (C-style) or column-major
            (Fortran-style) order.

    Returns:
        cupy.ndarray: An array filled with ones.

    .. seealso:: :func:`numpy.zeros`

    """
    a = cupy.ndarray(shape, dtype, order=order)
    a.data.memset(0, a.nbytes)
    return a
Exemple #40
0
def test_chainerx_to_cupy_delete_chainerx_first():
    dtype = 'float32'
    a_chx = chainerx.arange(6, dtype=dtype, device='cuda:0').reshape((2, 3))
    a_cupy = cupy.ndarray(
        a_chx.shape,
        cupy.dtype(a_chx.dtype.name),
        cupy.cuda.MemoryPointer(cupy.cuda.UnownedMemory(
            a_chx.data_ptr + a_chx.offset,
            a_chx.data_size,
            a_chx,
            0), 0),
        strides=a_chx.strides,
    )

    del a_chx

    a_cupy += 1
    chainerx.testing.assert_array_equal_ex(
        a_cupy.get(), numpy.array([[1, 2, 3], [4, 5, 6]], dtype))
Exemple #41
0
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)
Exemple #42
0
 def test_shape(self):
     a = cupy.ndarray(self.arg)
     self.assertTupleEqual(a.shape, self.shape)
Exemple #43
0
 def test_shape_int(self):
     a = cupy.ndarray(3)
     self.assertTupleEqual(a.shape, (3, ))
Exemple #44
0
 def test_shape_none(self):
     a = cupy.ndarray(None)
     self.assertTupleEqual(a.shape, ())
Exemple #45
0
 def array(self, shape, offset=0, dtype=np.float32):
     if dtype is None:
         raise TypeError('dtype must be an instance of numpy.dtype class')
     return cp.ndarray(shape, memptr=self.memory + offset, dtype=dtype)
Exemple #46
0
def _array_to_gpu(array, device, stream):
    if array is None:
        return None

    if isinstance(array, chainerx.ndarray):
        # TODO(niboshi): Update this logic once both CuPy and ChainerX support
        # the array interface.
        if array.device.backend.name == 'cuda':
            # Convert to cupy.ndarray on the same device as source array
            array = cupy.ndarray(
                array.shape,
                array.dtype,
                cupy.cuda.MemoryPointer(
                    cupy.cuda.UnownedMemory(
                        array.data_ptr + array.offset,
                        array.data_size,
                        array,
                        array.device.index),
                    0),
                strides=array.strides)
        else:
            array = chainerx.to_numpy(array)
    elif isinstance(array, (numpy.number, numpy.bool_)):
        array = numpy.asarray(array)
    elif isinstance(array, intel64.mdarray):
        array = numpy.asarray(array)

    if isinstance(array, ndarray):
        if array.device == device:
            return array
        is_numpy = False
    elif isinstance(array, numpy.ndarray):
        is_numpy = True
    else:
        raise TypeError(
            'The array sent to gpu must be an array or a NumPy scalar.'
            '\nActual type: {0}.'.format(type(array)))

    if stream is not None and stream.ptr != 0:
        ret = cupy.empty_like(array)
        if is_numpy:
            # cpu to gpu
            mem = cupy.cuda.alloc_pinned_memory(array.nbytes)
            src = numpy.frombuffer(
                mem, array.dtype, array.size).reshape(array.shape)
            src[...] = array
            ret.set(src, stream)
            cupy.cuda.pinned_memory._add_to_watch_list(
                stream.record(), mem)
        else:
            # gpu to gpu
            with array.device:
                src = array.copy()
                event = Stream.null.record()
            stream.wait_event(event)
            ret.data.copy_from_device_async(
                src.data, src.nbytes, stream)

            # to hold a reference until the end of the asynchronous
            # memcpy
            stream.add_callback(lambda *x: None, (src, ret))
        return ret

    with device:
        if is_numpy:
            return cupy.asarray(array)
        # Need to make a copy when an array is copied to another device
        return cupy.array(array, copy=True)