How to use the pyopencl.clrandom.rand function in pyopencl

To help you get started, we’ve selected a few pyopencl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github inducer / pyopencl / test / test_algorithm.py View on Github external
def test_key_value_sorter(ctx_factory):
    from pytest import importorskip
    importorskip("mako")

    context = ctx_factory()
    queue = cl.CommandQueue(context)

    n = 10**5
    nkeys = 2000
    from pyopencl.clrandom import rand as clrand
    keys = clrand(queue, n, np.int32, b=nkeys)
    values = clrand(queue, n, np.int32, b=n).astype(np.int64)

    assert np.max(keys.get()) < nkeys

    from pyopencl.algorithm import KeyValueSorter
    kvs = KeyValueSorter(context)
    starts, lists, evt = kvs(queue, keys, values, nkeys, starts_dtype=np.int32)

    starts = starts.get()
    lists = lists.get()

    mydict = dict()
    for k, v in zip(keys.get(), values.get()):
        mydict.setdefault(k, []).append(v)

    for i in range(nkeys):
github inducer / pyopencl / test / test_array.py View on Github external
def test_concatenate(ctx_factory):
    context = ctx_factory()
    queue = cl.CommandQueue(context)

    from pyopencl.clrandom import rand as clrand

    a_dev = clrand(queue, (5, 15, 20), dtype=np.float32)
    b_dev = clrand(queue, (4, 15, 20), dtype=np.float32)
    c_dev = clrand(queue, (3, 15, 20), dtype=np.float32)
    a = a_dev.get()
    b = b_dev.get()
    c = c_dev.get()

    cat_dev = cl.array.concatenate((a_dev, b_dev, c_dev))
    cat = np.concatenate((a, b, c))

    assert la.norm(cat - cat_dev.get()) == 0
github zachjweiner / pystella / test / test_relax.py View on Github external
def zero_mean_array():
        f0 = clr.rand(queue, grid_shape, dtype)
        f = clr.rand(queue, tuple(ni + 2*h for ni in rank_shape), dtype)
        mpi.scatter_array(queue, f0, f, root=0)
        avg = statistics(f)['mean']
        f = f - avg
        mpi.share_halos(queue, f)
        return f
github zachjweiner / pystella / test / test_multigrid.py View on Github external
def zero_mean_array():
        f0 = clr.rand(queue, grid_shape, dtype)
        f = clr.rand(queue, tuple(ni + 2*h for ni in rank_shape), dtype)
        mpi.scatter_array(queue, f0, f, root=0)
        avg = statistics(f)['mean']
        f = f - avg
        mpi.share_halos(queue, f)
        return f
github inducer / pyopencl / test / test_wrapper.py View on Github external
def test_spirv(ctx_factory):
    ctx = ctx_factory()
    queue = cl.CommandQueue(ctx)

    if (ctx._get_cl_version() < (2, 1)
            or cl.get_cl_header_version() < (2, 1)):
        pytest.skip("SPIR-V program creation only available "
                "in OpenCL 2.1 and higher")

    n = 50000

    a_dev = cl.clrandom.rand(queue, n, np.float32)
    b_dev = cl.clrandom.rand(queue, n, np.float32)
    dest_dev = cl_array.empty_like(a_dev)

    with open("add-vectors-%d.spv" % queue.device.address_bits, "rb") as spv_file:
        spv = spv_file.read()

    prg = cl.Program(ctx, spv).build()
    if (not prg.all_kernels()
            and queue.device.platform.name.startswith("AMD Accelerated")):
        pytest.skip("SPIR-V program creation on AMD did not result in any kernels")

    prg.sum(queue, a_dev.shape, None, a_dev.data, b_dev.data, dest_dev.data)

    assert la.norm((dest_dev - (a_dev+b_dev)).get()) < 1e-7
github inducer / pyopencl / test / test_array.py View on Github external
def make_random_array(queue, dtype, size):
    from pyopencl.clrandom import rand

    dtype = np.dtype(dtype)
    if dtype.kind == "c":
        real_dtype = TO_REAL[dtype]
        return (rand(queue, shape=(size,), dtype=real_dtype).astype(dtype)
                + rand(queue, shape=(size,), dtype=real_dtype).astype(dtype)
                * dtype.type(1j))
    else:
        return rand(queue, shape=(size,), dtype=dtype)
github inducer / loopy / test / test_matmul.py View on Github external
def variant_cpu(knl):
        unroll = 16
        block_size = unroll*4096
        knl = lp.split_dimension(knl, "i", block_size, outer_tag="g.0", slabs=(0, 1))
        knl = lp.split_dimension(knl, "i_inner", unroll, inner_tag="unr")
        return knl

    def variant_gpu(knl):
        unroll = 4
        block_size = 256
        knl = lp.split_dimension(knl, "i", unroll*block_size, outer_tag="g.0", slabs=(0, 1))
        knl = lp.split_dimension(knl, "i_inner", block_size, outer_tag="unr", inner_tag="l.0")
        return knl

    a = cl_random.rand(queue, n, dtype=dtype, luxury=2)
    b = cl_random.rand(queue, n, dtype=dtype, luxury=2)
    c = cl_array.zeros_like(a)
    refsol = (2*a+3*b).get()

    for variant in [variant_cpu, variant_gpu]:
        kernel_gen = lp.generate_loop_schedules(variant(knl),
                loop_priority=["i_inner_outer"])
        kernel_gen = lp.check_kernels(kernel_gen, dict(n=n), kill_level_min=5)

        def launcher(kernel, gsize, lsize, check):
            evt = kernel(queue, gsize(n), lsize(n), 2, a.data, 3, b.data, c.data, n,
                    g_times_l=True)

            if check:
                check_error(refsol, c.get())
github zachjweiner / pystella / test / test_stencil.py View on Github external
+ x[i - h + h, j + h, k + h]
        + x[i + h, j - h + h, k + h]
        + x[i + h, j + h, k - h + h]
    )

    if stream:
        try:
            stencil_map = ps.StreamingStencil(
                map_dict, prefetch_args=['x'], halo_shape=h
            )
        except:  # noqa
            pytest.skip("StreamingStencil unavailable")
    else:
        stencil_map = ps.Stencil(map_dict, h, prefetch_args=['x'])

    x = clr.rand(queue, tuple(ni + 2*h for ni in rank_shape), dtype)
    y = clr.rand(queue, rank_shape, dtype)

    x_h = x.get()
    y_true = (
        x_h[2*h:, h:-h, h:-h]
        + x_h[h:-h, 2*h:, h:-h]
        + x_h[h:-h, h:-h, 2*h:]
        + x_h[:-2*h, h:-h, h:-h]
        + x_h[h:-h, :-2*h, h:-h]
        + x_h[h:-h, h:-h, :-2*h]
    )

    stencil_map(queue, x=x, y=y)

    rtol = 5.e-14 if dtype == np.float64 else 1.e-5
github compas-dev / compas / src / compas / hpc / core / opencl / opencl.py View on Github external
Parameters
    ----------
    queue
        PyOpenCL queue.
    shape : tuple
        Size of the random array.

    Returns
    -------
    gpuarray
        Random floats from 0 to 1 in GPUArray.

    """

    return pyopencl.clrandom.rand(queue, shape, dtype=float32)