inducer / inducer/pyopencl

internal boolean type for array

Open
#73 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
1.2k
Forks
247
Avg merge
7h 2m
Merged PRs (30d)
6

Description

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)
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.