Func.reality在Halide是如何工作的



我能理解教程6中的解释,即:

// 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.

然而,根据描述,我无法理解这样一个例子是如何工作的:

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);

我感到困惑的是:在"梯度"的例子中,评估从(0,0(到(7,7(的所有x,y的梯度可以给我们一个结果,比如梯度(1,1(=1+1=2。但在第二个例子中,评估i从0到7的直方图对我来说很奇怪,因为我认为我们试图从后到前计算结果。更自然的方法是先评估输入,然后计算直方图。

那么,第二个例子中的"实现"是如何运作的呢?

Halide会自动推断出需要计算的所有值,以生成所需的输出区域。CCD_ 1只是要求流水线计算输出CCD_。Halide然后自动推断哪些区域需要早期的Func,并在产生所请求的输出区域之前递归地评估所有这些区域,直到输入。

最新更新