Search code examples
halide

Halide reduction over 2D domain boundary


In the filter I am implementing there is a step doing some reduction over the boundary of square domain

RDom r(0, filter_size, 0, filter_size);
r.where( (r.x == 0 || r.x == filter_size - 1) 
       || (r.y == 0 || r.y == filter_size - 1));

However this makes domain traversal O(filter_size^2) while useful reduction domain is only O(filter_size). Now my reduction operation is a bit involved, so repeating if for each side of the filter window makes quite a mess. Is there an elegant && efficient way of doing this in Halide?


Solution

  • The code I end up using is neither very elegant nor super-efficient, so contributions are welcome...

    RDom rl(0, filter_size, 0, 2, 0, 2);
    Expr rlx = rl.y*rl.x + rl.z*(1 - rl.y)*filter_size;
    Expr rly = (1 - rl.y)*rl.x + rl.z*rl.y*filter_size;
    Expr x_on_rl = x + rlx - (filter_size+1)/2;
    Expr y_on_rl = y + rly - (filter_size+1)/2;
    

    and usage is like

    range_min(x, y) = Halide::minimum(range_clamped(x_on_rl, y_on_rl));