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?

2 Answers 2

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.

2
  • 1
    Is there a constructor that makes a copy of the data instead of pointing to the array?
    – B.Md
    Oct 7, 2016 at 17:03
  • You can force a copy to the device buf.copy_to_device(halide_opencl_device_interface()) Nov 24, 2020 at 14:37
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.