Perform reduction on array #259
-
Is there a way to perform a reduction, such as the maximum oder minimum value, on a
|
Beta Was this translation helpful? Give feedback.
Answered by
christophercrouzet
Jul 10, 2024
Replies: 1 comment 1 reply
-
Hi @fritzio! Yes, it's possible with #!/usr/bin/env python3
import math
import warp as wp
@wp.kernel
def compute_min_max(
values: wp.array(dtype=float),
out: wp.array(dtype=float),
):
tid = wp.tid()
wp.atomic_min(out, 0, values[tid])
wp.atomic_max(out, 1, values[tid])
def run():
values = wp.array((5, -3, 8, -2, 7, 4), dtype=float)
out = wp.array((+math.inf, -math.inf), dtype=float)
wp.launch(compute_min_max, dim=values.shape, inputs=(values,), outputs=(out,))
print(out.numpy())
if __name__ == "__main__":
run() |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
fritzio
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @fritzio!
Yes, it's possible with
wp.atomic_min()
andwp.atomic_max()
. These can be called from within a kernel, for example: