Search code examples
c++bufferhalide

Initializing Halide Buffer in C++


I'm trying to initialize Halide Buffer with a C++ 1D array. Given some other posts online, this is what I've got so far:

Image<float> in(Buffer(type_of<float>(), size_x, 0, 0, 0, NULL, in_ptr));

Where in_ptr is a pointer to my C++ array. When I run this I get the following error:

error: missing template arguments before ‘(’ token Image in(Buffer(type_of(), padded_size * (jb + 1), 0, 0, 0, NULL, d_In));

So I changed my code to:

Image<float> in(Buffer<float>(type_of<float>(), size_x, 0, 0, 0, NULL, in_ptr));

But that doesn't match any of the constructors either but I couldn't find any good documentations on how to initialize a Buffer.

Is it even possible to do something like this? How can I use a C++ 1D or 2D array to initialize Halide buffer?


Solution

  • The Buffer type changed recently, which is why the stuff you're finding online is not useful. To make a buffer that points to an array, use one of these two constructors:

    https://github.com/halide/Halide/blob/master/src/runtime/HalideBuffer.h#L631

    float my_array[10];
    Halide::Buffer<float> buf(my_array); // Infers the size from the type
    

    https://github.com/halide/Halide/blob/master/src/runtime/HalideBuffer.h#L665

    float *my_pointer = ...
    Halide::Buffer<float> buf(my_pointer, 10); // Accepts a pointer and some sizes
    

    2D works similarly:

    float my_array[30][20]
    Halide::Buffer<float> buf(my_array); // Makes a 20x30 array
    

    or equivalently,

    float *my_pointer = ...
    Halide::Buffer<float> buf(my_pointer, 20, 30); 
    

    Neither of these constructors makes a copy of the data - they just refer to the existing array.