Example #1
0
    def test_cuFloatComplex(self):
        N = 100
        block = 32
        grid = (N + block - 1) // block
        dtype = cupy.complex64

        mod = cupy.RawModule(code=_test_cuComplex, translate_cucomplex=True)
        a = cupy.random.random((N, )) + 1j * cupy.random.random((N, ))
        a = a.astype(dtype)
        b = cupy.random.random((N, )) + 1j * cupy.random.random((N, ))
        b = b.astype(dtype)
        c = cupy.random.random((N, )) + 1j * cupy.random.random((N, ))
        c = c.astype(dtype)
        out = cupy.zeros((N, ), dtype=dtype)
        out_float = cupy.zeros((N, ), dtype=cupy.float32)
        out_up = cupy.zeros((N, ), dtype=cupy.complex128)

        ker = mod.get_function('test_addf')
        ker((grid, ), (block, ), (a, b, out))
        assert (out == a + b).all()

        ker = mod.get_function('test_subf')
        ker((grid, ), (block, ), (a, b, out))
        assert (out == a - b).all()

        ker = mod.get_function('test_mulf')
        ker((grid, ), (block, ), (a, b, out))
        assert cupy.allclose(out, a * b)

        ker = mod.get_function('test_divf')
        ker((grid, ), (block, ), (a, b, out))
        assert (out == a / b).all()

        ker = mod.get_function('test_conjf')
        ker((grid, ), (block, ), (a, out))
        assert (out == cupy.conj(a)).all()

        ker = mod.get_function('test_absf')
        ker((grid, ), (block, ), (a, out_float))
        assert (out_float == cupy.abs(a)).all()

        ker = mod.get_function('test_fmaf')
        ker((grid, ), (block, ), (a, b, c, out))
        assert cupy.allclose(out, a * b + c)

        ker = mod.get_function('test_makef')
        ker((grid, ), (block, ), (out, ))
        # because of precision issue, the (A==B).all() semantics would fail
        assert cupy.allclose(out, 1.8 - 1j * 8.7)

        ker = mod.get_function('test_upcast')
        ker((grid, ), (block, ), (a, out_up))
        assert (out_up == a.astype(cupy.complex128)).all()

        # NumPy scalars.
        b = cupy.complex64(2 + 3j)
        ker = mod.get_function('test_addf_scalar')
        ker((grid, ), (block, ), (a, b, out))
        assert (out == a + b).all()
Example #2
0
def _remap_lanczos(Fe, x, m, F, fwd=True, cval=0.0):
    """Lanczos resampling from grid Fe to points x.

    At the edges, the Lanczos filter wraps around.

    Parameters
    ----------
    Fe : (H, W)
        The function at equally spaced samples.
    x : (N, 2) float32
        The non-uniform sample positions on the grid.
    m : int > 0
        The Lanczos filter is 2m + 1 wide.
    F : (N, )
        The values at the non-uniform samples.
    """
    assert Fe.ndim == 2
    assert x.ndim == 2 and x.shape[-1] == 2
    assert m > 0
    assert F.shape == x.shape[:-1], F.dtype == Fe.dtype
    assert Fe.dtype == 'complex64'
    assert F.dtype == 'complex64'
    assert x.dtype == 'float32'
    lanczos_width = 2 * m + 1

    if fwd:
        kernel = cp.RawKernel(_cu_source, "fwd_lanczos_interp2D")
    else:
        kernel = cp.RawKernel(_cu_source, "adj_lanczos_interp2D")

    grid = (-(-x.shape[0] // kernel.max_threads_per_block), 0, 0)
    block = (min(x.shape[0], kernel.max_threads_per_block), 0, 0)
    kernel(grid, block, (
        Fe,
        cp.array(Fe.shape, dtype='int32'),
        F,
        x,
        len(x),
        lanczos_width,
        cp.complex64(cval),
    ))