2

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?

B.Md
  • 107
  • 2
  • 10

2 Answers2

9

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.

Andrew Adams
  • 1,396
  • 7
  • 3
0

With the latest Halide, you likely want: Buffer<float, 1> my_buffer(in_ptr, size_x);

This will create my_buffer pointing to in_ptr. The 1 in the template parameters is the number of dimensions. For larger numbers of dimensions, when passing prepopulated memory, strides may have to be specified as well. (The above assumes the data in_ptr points to is densely packed -- that is each element is at an index one greater than the previous in in_ptr.)

The syntax around this changed recently, but with the goal of making usage easier and more consistent.

Zalman Stern
  • 3,161
  • 12
  • 18