inducer / pyopencl

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

Reductions on arrays containing `nan` give unexpected results #583

Closed majosm closed 2 years ago

majosm commented 2 years ago

Describe the bug Array reductions such as min and max produce unexpected results when nans are present in the input array.

To Reproduce

import pyopencl as cl
import pyopencl.array as cla
import numpy as np

ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)

a = np.full(10, np.nan)
a_dev = cla.to_device(queue, a)

print(f"{cla.max(a_dev)=}")

b = np.full(10, np.nan)
b[0] = 1.
b_dev = cla.to_device(queue, b)

print(f"{cla.max(b_dev)=}")

Output:

cla.max(a_dev)=cl.Array(-inf)
cla.max(b_dev)=cl.Array(1.)

Expected behavior Ideally these should both return cl.Array(nan)?

Environment (please complete the following information):

inducer commented 2 years ago

Could you start a PR with a failing regression test to save me time?

majosm commented 2 years ago

Could you start a PR with a failing regression test to save me time?

https://github.com/inducer/pyopencl/pull/584

minimum/maximum also appear to have some nan-handling issues.