0

I can understand the explanation in tutorial 6, which is:

// Func gradient("gradient");
// Var x("x"), y("y");
// gradient(x, y) = x + y;
// gradient.realize(8, 8);
//
// This does three things internally:
// 1) Generates code than can evaluate gradient over an arbitrary
// rectangle.
// 2) Allocates a new 8 x 8 image.
// 3) Runs the generated code to evaluate gradient for all x, y
// from (0, 0) to (7, 7) and puts the result into the image.
// 4) Returns the new image as the result of the realize call.

However, follow the description, I can't figure it out how such a example works:

    Func histogram("hist_serial");
    histogram(i) = 0;
    RDom r(0, input.width(), 0, input.height());
    histogram(input(r.x, r.y) / 32) += 1;

    histogram.vectorize(i, 8);
    histogram.realize(8);

What I am confusing is: in the "gradient" example, evaluating gradient for all x, y from (0,0) to (7,7) can give us a result, like gradient(1,1)=1+1=2. But in the second example, evaluating histogram for i from 0 to 7 looks strange to me, as I think that we are trying to calculate the result from back to front. A more natural way is to evaluate the input first, then calculate histogram.

So, how the "realize" in the second example works?

Lin.Chen
  • 73
  • 4

1 Answers1

1

Halide automatically infers all of the values which need to be computed to produce a requested region of output. realize just asks the pipeline to compute the requested region of the output Func(s). Halide then automatically infers what regions of which earlier Funcs are required, and recursively evaluates all of the those, up to the inputs, before producing the requested region of output.

jrk
  • 2,896
  • 1
  • 22
  • 35