0

I'm trying to define a global shared variable, which counts a non-zero elements in a input, like this:

counter = 0
N += select(input[x,y] > 10, 1, 0)

however, this is very hard in halide, is there any global shared variable that targets this goal?

swordguy
  • 21
  • 3

1 Answers1

0

In general, Halide is designed to avoid mutable state, since it constraints possible transformations and optimizations—it’s the enemy of portable performance. In this case, however, you can do exactly what you want by making counter a 0-dimensional Func and using an RDom to iterate over the elements in input:

Func counter;
RDom r(0, input.width(), 0, input.height());
counter() = 0;
counter() += select(input(r.x, r.y) > 10, 1, 0);

You can also write this in many other ways, using the sum helper, potentially using r.where for the predicate, and using the counter.rfactor scheduling directive to parallelize the reduction.

jrk
  • 2,896
  • 1
  • 22
  • 35