loopy icon indicating copy to clipboard operation
loopy copied to clipboard

[PyOpenCL execution]: Scalar argument always returned as pyopencl arrays

Open kaushikcfd opened this issue 4 years ago • 3 comments

Minimal reproducer:

knl = lp.make_kernel(
    "{:}",
    """
    y = 2*x
    """)

evt, (out,) = knl(cq, x=np.asarray(3.0, dtype=float))
print(repr(out))  # prints: cl.Array(6.)

However if 'y' was anything other than array with shape () we return a numpy array. Is this a bug?

kaushikcfd avatar Apr 05 '21 04:04 kaushikcfd

  • What would you like to happen?
  • If you pass out_host=True, you get a (transferred) numpy array
  • Using np.float32(3) as an input type gives me the same result. Were you observing something different?

inducer avatar Apr 05 '21 17:04 inducer

What would you like to happen?

I would prefer returning a np ndarrray as we do for any other array with shape != (). We can see this inconsistency in the snippet below:

knl = lp.make_kernel(
    "{:}",
    """
    y = 2*x
    """)
evt, (out,) = knl(cq, x=np.asarray(3.0, dtype=float))
print(repr(out))  # prints: cl.Array(6.)

knl = lp.make_kernel(
    "{:}",
    """
    y = 2*x[0]
    """)
evt, (out,) = knl(cq, x=np.asarray([3.0], dtype=float))
print(repr(out))  # prints: array(6.)

kaushikcfd avatar Apr 05 '21 17:04 kaushikcfd

OIC. I'm not opposed. While I generally have grown to dislike the automatic-transfer-back-to-host functionality, I don't dislike it enough to want to rip it out. In your case, I think it's just a problem with the "are all inputs numpy arrays" predicate. I'd view the inconsistency you point out as a bug.

inducer avatar Apr 05 '21 17:04 inducer