Skip to content

Perform reduction on array #259

Answered by christophercrouzet
fritzio asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @fritzio!

Yes, it's possible with wp.atomic_min() and wp.atomic_max(). These can be called from within a kernel, for example:

#!/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()

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@fritzio
Comment options

Answer selected by fritzio
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