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.