コード例 #1
0
ファイル: test_algorithm.py プロジェクト: zeta1999/pyopencl
def test_partition(ctx_factory):
    from pytest import importorskip
    importorskip("mako")

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

    from pyopencl.clrandom import rand as clrand
    for n in scan_test_counts:
        print("part", n)

        a_dev = clrand(queue, (n,), dtype=np.int32, a=0, b=1000)
        a = a_dev.get()

        crit = a_dev.dtype.type(300)
        true_host = a[a > crit]
        false_host = a[a <= crit]

        from pyopencl.algorithm import partition
        true_dev, false_dev, count_true_dev, evt = partition(
                a_dev, "ary[i] > myval", [("myval", crit)])

        count_true_dev = count_true_dev.get()

        assert (true_dev.get()[:count_true_dev] == true_host).all()
        assert (false_dev.get()[:n-count_true_dev] == false_host).all()
コード例 #2
0
ファイル: test_algorithm.py プロジェクト: shinsec/pyopencl
def test_partition(ctx_factory):
    from pytest import importorskip
    importorskip("mako")

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

    from pyopencl.clrandom import rand as clrand
    for n in scan_test_counts:
        print("part", n)

        a_dev = clrand(queue, (n,), dtype=np.int32, a=0, b=1000)
        a = a_dev.get()

        crit = a_dev.dtype.type(300)
        true_host = a[a > crit]
        false_host = a[a <= crit]

        from pyopencl.algorithm import partition
        true_dev, false_dev, count_true_dev, evt = partition(
                a_dev, "ary[i] > myval", [("myval", crit)])

        count_true_dev = count_true_dev.get()

        assert (true_dev.get()[:count_true_dev] == true_host).all()
        assert (false_dev.get()[:n-count_true_dev] == false_host).all()
コード例 #3
0
ファイル: pyopencltests.py プロジェクト: cs362sp16/cs562w16
def pyopencl_filter(cpu_data, func="ary[i] == val", target=None):
    """
    SUT
    This is a function that sets up a GPU context and exposes the pyopencl algorithm filter to the TSTL random tester
    The Filter function checks all the values in some list against a given target value and leaves them if they
    agree with evaluation of the predicate, e.g.: 1 > 3 returns false where 1 is the value and 3 is the target

    It executes the method python_filter as its oracle.
    :param cpu_data: A numpy array to execute the pyopencl filter algorithm on.
    :param func: The predicate used to determine if a value should show up in the resulting array
    :param target: The value to filter the list against, if none is given it will filter against the first value in the array
    :return: A boolean that describes if the CPU oracle and the SUT results match
    """
    # platforms = cl.get_platforms()
    # ctx = cl.Context(dev_type=cl.device_type.ALL, properties=[(cl.context_properties.PLATFORM, platforms[0])])
    # queue = cl.CommandQueue(ctx)
    queue = get_queue()

    gpu_data = pyopencl.array.to_device(queue, cpu_data)

    target = target if target else cpu_data[0]

    true_cpu, false_cpu = python_filter(cpu_data=cpu_data, target=target, func=func)
    true_gpu, false_gpu, n_gpu_true, evt = partition(gpu_data, func, [("val", target)])

    n_gpu_true = n_gpu_true.get()

    return (true_gpu.get()[:n_gpu_true] == true_cpu).all() and \
           (false_gpu.get()[:len(cpu_data) - n_gpu_true] == false_cpu).all()