def pyopencl_unique(cpu_data): """ SUT This is a function that sets up a GPU context and exposes the pyopencl algorithm uniqe to the TSTL random tester The Unique function resembles the UNIX command uniq, and requires that values must be sorted to get a list of "unique" values. Therefore, this method sorts the cpu_data array before transferring it to the GPU. It executes the method python_unique as its oracle. :param cpu_data: A numpy array to execute the pyopencl unique algorithm on. :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() cpu_data.sort() gpu_data = pyopencl.array.to_device(queue, cpu_data) unique_gpu, count_unique_gpu, evt = unique(gpu_data) unique_cpu = python_unique(cpu_data) count_unique_gpu = count_unique_gpu.get() unique_gpu = unique_gpu.get() return (unique_gpu[:count_unique_gpu] == unique_cpu).all()
def test_unique(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: a_dev = clrand(queue, (n,), dtype=np.int32, a=0, b=1000) a = a_dev.get() a = np.sort(a) a_dev = cl_array.to_device(queue, a) a_unique_host = np.unique(a) from pyopencl.algorithm import unique a_unique_dev, count_unique_dev, evt = unique(a_dev) count_unique_dev = count_unique_dev.get() assert (a_unique_dev.get()[:count_unique_dev] == a_unique_host).all() from gc import collect collect()
def test_unique(ctx_factory): context = ctx_factory() queue = cl.CommandQueue(context) from pyopencl.clrandom import rand as clrand for n in scan_test_counts: a_dev = clrand(queue, (n,), dtype=np.int32, a=0, b=1000) a = a_dev.get() a = np.sort(a) a_dev = cl_array.to_device(queue, a) a_unique_host = np.unique(a) from pyopencl.algorithm import unique a_unique_dev, count_unique_dev = unique(a_dev) count_unique_dev = count_unique_dev.get() assert (a_unique_dev.get()[:count_unique_dev] == a_unique_host).all() from gc import collect collect()