pyopencl icon indicating copy to clipboard operation
pyopencl copied to clipboard

internal boolean type for array

Open inferrna opened this issue 9 years ago • 2 comments

For equations like array[array>3] results of numpy anp pyopencl completely differs. For myself I had rewritten some operators to support boolean indexing. Is it possible to add internal boolean type into Array class? My solution (dirty and non-optimal, of course) below for example.

class myclArray(clarray.Array):
    def __init__(self, *args, **kwargs):
        clarray.Array.__init__(self, *args, **kwargs)
        self.ndim = len(self.shape)
        self.is_boolean = False

    def __lt__(self, other):
        result = clarray.Array.__lt__(self, other)
        result.is_boolean = True
        return result
    def __getitem__(self, index):
        if isinstance(index, myclArray) and index.is_boolean == True:
            x, y, z = algorithm.copy_if(self.reshape((self.size,)), "index[i]!=0", [("index", index.reshape((index.size,)))])
            _res = x[:y.get()]
            res = myclArray(queue, _res.shape, _res.dtype, data=_res.data)
        else:
            res = clarray.Array.__getitem__(self, index)
        return res

    def __setitem__(self, subscript, value):
        if isinstance(subscript, myclArray) and subscript.is_boolean == True:
            idxcl = clarray.arange(queue, 0, self.size, 1, dtype=np.int32)
            x, y, z = algorithm.copy_if(idxcl, "index[i]!=0", [("index", subscript.reshape((subscript.size,)))])
            _res = x[:y.get()]
            clarray.Array.setitem(self.reshape((self.size,)), _res, value, queue=queue)
        else:
            self.setitem(subscript, value, queue=queue)

inferrna avatar Jan 15 '15 05:01 inferrna

How come you didn't trigger this on the array having dtype of np.bool?

inducer avatar Jan 17 '15 19:01 inducer

It's meant to provide full support for boolean type, but for now it isn't my target and is bit more difficult thing than I have done. Have you a plan for yourself to provide support for np.bool type? That would be a great feature, as for now converting to it gives ValueError: unable to map dtype 'bool'

inferrna avatar Jan 19 '15 02:01 inferrna