arraysreturnfunchalide

Return Func array from Halide function


I have a function in a Halide program that needs to return an array of Funcs along with a Func. How do I do this?

Tuple Func myFunc() {
const int s = 8;
Func pyramid[s];
Func X("X");
......
return Tuple(pyramid[](x,y), X(x,y));

predictably does not work as pyramid is not an Expr. I asked ChatGPT but it asked me to list each array element individually in the Tuple constructor. Is there a more compact way? One that doesn't require me to change the return command when I change s?

Another question: How do I assign a string name to a Func array? As you see above, I have assigned the string name "X" to the Func X but I'd like to do so to pyramid also.


Solution

  • If you are just trying to return multiple Funcs for later use inside the construction of a larger Halide pipeline, you don't need to make a Halide-level object to group them together. Instead, any C++ object can carry them. e.g., you could return a std::vector<Halide::Func> from your C++ function, and then have later Halide code reference any of those.

    The key thing to understand is that there are really two "stages" of program here, the C++ code which constructs the Halide program (the first stage), and then the Halide program itself which is evaluated later (the second stage, independently of the C++ that constructed it) to produce pixels. This is more clear and explicit if you use the ahead-of-time compilation model (e.g., starting from tutorial 10), but the idea still applies even when you're using it in JIT mode.