1

I have a question about the behaivior of split() in Halide language.

When I use split(), it computes the elements twice at the edge when the computing region is not a multiple of split factor. For example, when computing region is 10 and split factor is 4, Halide will compute the elements [0,1,2,3], [4,5,6,7] and [6,7,8,9] like the result of trace_stores() below.

Is there any ways to calculate only elements [8,9] at the last step in inner loop of split()?

sample code:

#include "Halide.h"
using namespace Halide;

#define INPUT_SIZE 10
int main(int argc, char** argv) {
    Func f("f");
    Var x("x");
    f(x) = x;

    Var xi("xi");
    f.split(x, x, xi, 4); 

    f.trace_stores();
    Image<int32_t> out = f.realize(INPUT_SIZE);
    return 0;
}

trace_stores() results:

Store f.0(0) = 0
Store f.0(1) = 1
Store f.0(2) = 2
Store f.0(3) = 3
Store f.0(4) = 4
Store f.0(5) = 5
Store f.0(6) = 6
Store f.0(7) = 7
Store f.0(6) = 6
Store f.0(7) = 7
Store f.0(8) = 8
Store f.0(9) = 9
3
  • Can you say why do you need to avoid the extra set of stores? Oct 22, 2015 at 18:38
  • I am thinking whether I could take advantage of tile() schedule when I use single core CPU. When the best tile size is not a divisor of input data, I think extra computations should be reduced in that situation.
    – GreatAS
    Oct 23, 2015 at 9:51
  • The extra work only happens in the edge tiles and the goal is to make the non-edge tiles faster. With vectorization, it isn't even necessarily extra work because you need to process the edge values anyway and a vectorized approach usually has the same or lower cost compared to scalarizing the edge. A bigger concern is that if Halide cannot prove the tiling is aligned to the min/extent being processed, it has to insert code that handles any possible alignment. One easy way to improve that is to either assert the min is 0 or that the min is aligned to the tile size. Oct 24, 2015 at 22:21

1 Answer 1

1

It's possible but ugly. Halide generally assumes that it can re-evaluate points in a Func arbitrarily and that inputs don't alias with outputs, so it's always safe to recompute a few values near the edge.

The fact that this matters is a bad sign. There might be other ways to achieve what you're trying to do.

Anyway, the workaround is to use explicit RDoms to tell Halide precisely what to iterate over:

// No pure definition
f(x) = undef<int>(); 

// An update stage that does the vectorized part:
Expr w = (input.width()/4)*4;
RDom r(0, w);
f(r) = something;
f.update(0).vectorize(r, 4);

// An update stage that does the tail end:
RDom r2(input.width(), input.width() - w);
f(r2) = something;
f.update(1); // Don't vectorize the tail end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.