1

Is it possible to initialize a Func from an array in a generator class? The code should look like this.

class SobelConv: public Halide::Generator<SobelConv> {

const signed char kernelx[3][3] = {
    {-1, 0, 1},
    {-2, 0, 2},
    {-1, 0, 1}
};

void generate() {
     for (int y = 0; y < 3; y++)
        for (int x = 0; x < 3; x++)
            kernel_x(x, y) = kernelx[y][x];

     conv_x(x, y) += gray(x+win.x, y+win.y) * kernel_x(win.x + 1, win.y + 1);
}

Func kernel_x{"kernel_x"};

Currently, the way I did is to define Input<Buffer<int8_t>> kernel_x. I do not want it to be an argument of the pipeline function, and would like kernel_x to be replaced with the respective numbers directly.

aristg
  • 62
  • 6

1 Answers1

1

The following compiles and illustrates one way to do this:

#include "Halide.h"

class SobelConv: public Halide::Generator<SobelConv> {
  signed char weights[3][3] = {
    {-1, 0, 1},
    {-2, 0, 2},
    {-1, 0, 1}
  };

  Input<Buffer<int8_t>> gray{"gray", 2};
  Halide::Buffer<int8_t> kernel_x{&weights[0][0], 3, 3};

  Output<Buffer<int8_t>> conv_x{"conv_x", 2};

  Var x, y;
  RDom win{0, 3};

  void generate() {
    conv_x(x, y) += gray(x+win.x, y+win.y) * kernel_x(win.x + 1, win.y + 1);
  }
};

The weights will be embedded in the generated code at compile time. We should have a way to provide the constant values for the weights in an initializer list as well, but I'm not finding it at the moment.

Zalman Stern
  • 3,161
  • 12
  • 18
  • 2
    If you have a static array, `Halide::Runtime::Buffer` can be initialized with the array: `kernel_x{ weights }`. The dimensionality and extents are detected. – Khouri Giordano Jun 13 '17 at 14:58