pyopencl
pyopencl copied to clipboard
internal boolean type for array
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)
How come you didn't trigger this on the array having dtype of np.bool
?
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'