-
Hello, I'm back with hopefully another obvious question 🙂 I'm trying to take the minimum of two scalars in a kernel using import loopy
domain = "{ [i]: 0 <= i < n }"
body = """
out[i] = min(a[i], b[i])
"""
kernel = loopy.make_kernel(domain, body, lang_version=(2018, 2))
print(kernel) The stacktrace that I'm getting:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Similar to numpy, import pyopencl as cl
import numpy as np
import loopy as lp
import pymbolic.primitives as p
from pymbolic import parse
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
domain = "{ [i]: 0 <= i < n }"
body = [
lp.Assignment("out[i]", p.Min((parse("a[i]"), parse("b[i]"))))
]
kernel = lp.make_kernel(domain, body, lang_version=(2018, 2))
a = np.random.randn(1000)
b = np.random.randn(1000)
_evt, (res,) = kernel(queue, a=a, b=b)
assert np.array_equal(res, np.minimum(a, b)) Patches welcome. The code to do this better would go here: Lines 1563 to 1622 in 524f2c4 |
Beta Was this translation helpful? Give feedback.
-
Fixed with patch in #795 |
Beta Was this translation helpful? Give feedback.
Similar to numpy,
min
is a reduction, so you could saymin(i, a[i])
to compute the minimum of an array. In numpy, you would useminimum
for the minimum of two numbers. In loopy, I don't think we can parseminimum
just yet, so the way to create the corresponding (pymbolic) expression node is kind of clunky ATM: