Skip to content

Scalar min/max? #794

Closed Answered by inducer
nicknytko asked this question in Q&A
Discussion options

You must be logged in to vote

Similar to numpy, min is a reduction, so you could say min(i, a[i]) to compute the minimum of an array. In numpy, you would use minimum for the minimum of two numbers. In loopy, I don't think we can parse minimum just yet, so the way to create the corresponding (pymbolic) expression node is kind of clunky ATM:

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 =

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by nicknytko
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants