inducer / pyopencl

OpenCL integration for Python, plus shiny features
http://mathema.tician.de/software/pyopencl
Other
1.06k stars 241 forks source link

int64 - uint32 (subtraction) not implemented correctly #355

Closed mattwala closed 4 years ago

mattwala commented 4 years ago
>>> arr1 = cl.array.to_device(q, np.array([1], dtype=np.uint32))
>>> arr2 = cl.array.to_device(q, np.array([2], dtype=np.int64))
>>> arr2 - arr1
array([4294967297])
>>> (arr2 - arr1).dtype
dtype('int64')
>>> np.array([2], dtype=np.int64) - np.array([1], dtype=np.uint32)
array([1])
mattwala commented 4 years ago

Similar to loopy, the cause is implementing subtraction as x + (-1) * y.

I see three ways out of this:

The second/third options would be more intrusive changes but would be similar to what NumPy does.

inducer commented 4 years ago

IIUC, (result_dtype)x + (-1)*(result_dtype)y succeeds because it produces a two's-complement (-1) of the correct bit width. Correct?

mattwala commented 4 years ago

IIUC, (result_dtype)x + (-1)*(result_dtype)y succeeds because it produces a two's-complement (-1) of the correct bit width. Correct?

I haven’t tested it, but I think that’s how it should work.