def test_copy_if(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() from pyopencl.algorithm import copy_if crit = a_dev.dtype.type(300) selected = a[a>crit] selected_dev, count_dev = copy_if(a_dev, "ary[i] > myval", [("myval", crit)]) assert (selected_dev.get()[:count_dev.get()] == selected).all() from gc import collect collect()
def test_copy_if(ctx_factory): from pytest import importorskip importorskip("mako") 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() from pyopencl.algorithm import copy_if crit = a_dev.dtype.type(6) #selected = a[a < crit] selected_dev, count_dev, evt = copy_if(a_dev, "ary[i] < myval", [("myval", crit)]) print count_dev print selected_dev[selected_dev>0]
def global_qbx_centers(self): """Build a list of indices of QBX centers that use global QBX. This indexes into the global list of targets, (see :meth:`target_info`) of which the QBX centers occupy the first *ncenters*. Centers without any associated targets are excluded. """ tree = self.tree() user_target_to_center = self.user_target_to_center() with cl.CommandQueue(self.cl_context) as queue: tgt_assoc_result = ( user_target_to_center.with_queue(queue)[self.ncenters:]) center_is_used = cl.array.zeros(queue, self.ncenters, np.int8) self.code_getter.pick_used_centers( queue, center_is_used=center_is_used, target_to_center=tgt_assoc_result, ncenters=self.ncenters, ntargets=len(tgt_assoc_result)) from pyopencl.algorithm import copy_if result, count, _ = copy_if( cl.array.arange(queue, self.ncenters, tree.particle_id_dtype), "global_qbx_flags[i] != 0 && center_is_used[i] != 0", extra_args=[ ("global_qbx_flags", self.global_qbx_flags()), ("center_is_used", center_is_used) ], queue=queue) if self.debug: logger.debug( "find global qbx centers: using %d/%d centers" % (int(count.get()), self.ncenters)) return result[:count.get()].with_queue(None)
def global_qbx_centers(self): """Build a list of indices of QBX centers that use global QBX. This indexes into the global list of targets, (see :meth:`target_info`) of which the QBX centers occupy the first *ncenters*. Centers without any associated targets are excluded. """ tree = self.tree() user_target_to_center = self.user_target_to_center() with cl.CommandQueue(self.cl_context) as queue: tgt_assoc_result = ( user_target_to_center.with_queue(queue)[self.ncenters:]) center_is_used = cl.array.zeros(queue, self.ncenters, np.int8) self.code_getter.pick_used_centers( queue, center_is_used=center_is_used, target_to_center=tgt_assoc_result, ncenters=self.ncenters, ntargets=len(tgt_assoc_result)) from pyopencl.algorithm import copy_if result, count, _ = copy_if( cl.array.arange(queue, self.ncenters, tree.particle_id_dtype), "global_qbx_flags[i] != 0 && center_is_used[i] != 0", extra_args=[("global_qbx_flags", self.global_qbx_flags()), ("center_is_used", center_is_used)], queue=queue) if self.debug: logger.debug("find global qbx centers: using %d/%d centers", int(count.get()), self.ncenters) return result[:count.get()].with_queue(None)
def __getitem__(self, index): if isinstance(index, myclArray) and index.dtype == dtbool: x, y, z = algorithm.copy_if(self.reshape((self.size,)),\ "index[i]!=0",\ # TODO: avoid type convert [("index", bool2int(index).reshape((index.size,)))]) res = x[:y.get()] elif isinstance(index, tuple) or isinstance(index, slice): indices, newshape = self.createshapes(index) program = programs.sliceget(self.dtype, len(self.shape), indices is None) res = empty(newshape, self.dtype) program.mislice(queue, (res.size,), None, indices.data if indices else np.int32(0), self.data, res.data) elif isinstance(index, myclArray) and self.ndim>0: program = programs.getndbyids(self.dtype, index.dtype) resshape = (index.size,) + self.shape[1:] if index.size>1 else self.shape[1:] dims = (int(np.prod(self.shape[1:])), int(index.size),) res = empty(resshape, self.dtype) program.getbyids(queue, dims, None, index.data, self.data, res.data) else: res = clarray.Array.__getitem__(self, index) if not isinstance(res, myclArray): res.__class__ = myclArray res.reinit() return res
def global_qbx_centers(self): """Build a list of indices of QBX centers that use global QBX. This indexes into the global list of targets, (see :meth:`target_info`) of which the QBX centers occupy the first *ncenters*. Centers without any associated targets are excluded. """ actx = self._setup_actx tree = self.tree() user_target_to_center = actx.thaw(self.user_target_to_center()) tgt_assoc_result = user_target_to_center[self.ncenters:] center_is_used = actx.zeros(self.ncenters, np.int8) self.code_getter.pick_used_centers(actx.queue, center_is_used=center_is_used, target_to_center=tgt_assoc_result, ncenters=self.ncenters, ntargets=len(tgt_assoc_result)) from pyopencl.algorithm import copy_if icenters = actx.from_numpy( np.arange(self.ncenters, dtype=tree.particle_id_dtype)) result, count, _ = copy_if( icenters, "global_qbx_flags[i] != 0 && center_is_used[i] != 0", extra_args=[("global_qbx_flags", self.global_qbx_flags()), ("center_is_used", center_is_used)], queue=actx.queue) count = actx.to_numpy(count).item() if self.debug: logger.debug("find global qbx centers: using %d/%d centers", count, self.ncenters) return actx.freeze(result[:count])
def global_qbx_centers(self): """Build a list of indices of QBX centers that use global QBX. This indexes into the global list of targets, (see :meth:`target_info`) of which the QBX centers occupy the first *ncenters*. """ tree = self.tree() with cl.CommandQueue(self.cl_context) as queue: from pyopencl.algorithm import copy_if logger.info("find global qbx centers: start") result, count, _ = copy_if( cl.array.arange(queue, self.center_info().ncenters, tree.particle_id_dtype), "global_qbx_flags[i] != 0", extra_args=[ ("global_qbx_flags", self.global_qbx_flags()) ], queue=queue) logger.info("find global qbx centers: done") return result[:count.get()].with_queue(None)
import pyopencl as cl import numpy as np from pyopencl import array from pyopencl.algorithm import copy_if context = cl.create_some_context() queue = cl.CommandQueue(context) mf = cl.mem_flags rand = cl_array.to_device (ctx, queue, numpy.random.randn(4,4).astype(np.int32)) #rand = np.random.random_integers(0,2**10,size=(2**10)*8).astype(np.int32) print rand a = array.to_device(queue, rand, allocator=None, async=False) scan_kernel = copy_if(a, predicate = "(a[i]<104) ? 1:0") #a = cl.array.arange(queue, 10000, dtype=np.int32) out, count, event = scan_kernel(a, queue=queue) print a