1 #ifndef HALIDE_BUFFER_H
2 #define HALIDE_BUFFER_H
11 template<
typename T =
void>
24 template<
typename... Args>
27 template<
typename First,
typename... Rest>
29 all_ints_and_optional_name<Rest...>> {};
33 std::is_convertible<T, int>> {};
39 typename =
typename std::enable_if<!std::is_convertible<T, std::string>::value>::type>
52 template<
typename First,
65 template<
typename... Args>
71 template<
typename... Args>
73 std::vector<int> result;
78 template<
typename T,
typename T2>
86 s << type_to_c_type(type_of<T>(),
false);
96 std::ostringstream oss;
97 if (std::is_const<T>::value) {
100 buffer_type_name_non_const<typename std::remove_const<T>::type>(oss);
118 template<
typename T2>
121 template<
typename T2>
122 static void assert_can_convert_from(
const Buffer<T2> &other) {
125 static_assert((!std::is_const<T2>::value || std::is_const<T>::value),
126 "Can't convert from a Buffer<const T> to a Buffer<T>");
127 static_assert(std::is_same<
typename std::remove_const<T>::type,
128 typename std::remove_const<T2>::type>::value ||
129 std::is_void<T>::value ||
130 std::is_void<T2>::value,
131 "type mismatch constructing Buffer");
141 <<
"Type mismatch constructing Buffer. Can't construct Buffer<"
142 << Internal::buffer_type_name<T>() <<
"> from Buffer<"
167 template<typename T2>
169 : contents(other.contents) {
170 assert_can_convert_from(other);
174 template<
typename T2>
176 assert_can_convert_from(other);
177 contents = std::move(other.contents);
183 : contents(new
Internal::BufferContents) {
184 contents->buf = std::move(
buf);
188 contents->name =
name;
197 template<
typename... Args,
200 int first, Args... rest)
206 const std::string &
name =
"")
210 template<
typename... Args,
218 const std::vector<int> &sizes,
219 const std::string &
name =
"")
224 const std::vector<int> &sizes,
225 const std::vector<int> &storage_order,
226 const std::string &
name =
"")
230 explicit Buffer(
const std::vector<int> &sizes,
231 const std::string &
name =
"")
235 explicit Buffer(
const std::vector<int> &sizes,
236 const std::vector<int> &storage_order,
237 const std::string &
name =
"")
241 template<
typename Array,
size_t N>
243 const std::string &
name =
"")
247 template<
typename... Args,
251 int first, Args &&...rest)
256 template<
typename... Args,
260 const std::vector<int> &sizes,
261 const std::string &
name =
"")
265 template<
typename... Args,
268 int first, Args &&...rest)
274 const std::vector<int> &sizes,
275 const std::string &
name =
"")
281 const std::vector<int> &sizes,
282 const std::string &
name =
"")
290 const std::string &
name =
"")
297 const std::string &
name =
"")
335 template<
typename T2>
337 void *(*allocate_fn)(
size_t) =
nullptr,
338 void (*deallocate_fn)(
void *) =
nullptr,
339 const std::string &
name =
"") {
344 template<
typename T2>
346 void *(*allocate_fn)(
size_t) =
nullptr,
347 void (*deallocate_fn)(
void *) =
nullptr,
348 const std::string &
name =
"") {
360 const std::string &
name()
const {
361 return contents->name;
366 template<
typename T2>
368 return (
const void *)(contents.
get()) == (
const void *)(other.contents.get());
390 #define HALIDE_BUFFER_FORWARD_CONST(method) \
391 template<typename... Args> \
392 auto method(Args &&...args) const->decltype(std::declval<const Runtime::Buffer<T>>().method(std::forward<Args>(args)...)) { \
393 user_assert(defined()) << "Undefined buffer calling const method " #method "\n"; \
394 return get()->method(std::forward<Args>(args)...); \
397 #define HALIDE_BUFFER_FORWARD(method) \
398 template<typename... Args> \
399 auto method(Args &&...args)->decltype(std::declval<Runtime::Buffer<T>>().method(std::forward<Args>(args)...)) { \
400 user_assert(defined()) << "Undefined buffer calling method " #method "\n"; \
401 return get()->method(std::forward<Args>(args)...); \
414 #define HALIDE_BUFFER_FORWARD_INITIALIZER_LIST(method, ...) \
415 inline auto method(const __VA_ARGS__ &a)->decltype(std::declval<Runtime::Buffer<T>>().method(a)) { \
416 user_assert(defined()) << "Undefined buffer calling method " #method "\n"; \
417 return get()->method(a); \
472 #undef HALIDE_BUFFER_FORWARD
473 #undef HALIDE_BUFFER_FORWARD_CONST
475 template<
typename Fn,
typename... Args>
477 get()->for_each_value(std::forward<Fn>(f), (*std::forward<Args>(other_buffers).
get())...);
481 template<
typename Fn,
typename... Args>
483 get()->for_each_value(std::forward<Fn>(f), (*std::forward<Args>(other_buffers).
get())...);
487 template<
typename Fn>
489 get()->for_each_element(std::forward<Fn>(f));
493 template<
typename Fn>
495 get()->for_each_element(std::forward<Fn>(f));
499 template<
typename FnOrValue>
501 get()->fill(std::forward<FnOrValue>(f));
511 template<
typename T2>
521 return contents->buf.type();
524 template<
typename T2>
530 return Buffer<T>(std::move(contents->buf.as<T>().copy()));
533 template<
typename T2>
535 contents->buf.copy_from(*other.
get());
538 template<
typename... Args>
540 return (*
get())(first, std::forward<Args>(args)...);
543 template<
typename... Args>
545 return (*
get())(first, std::forward<Args>(args)...);
549 return (*
get())(pos);
553 return (*
get())(pos);
567 template<
typename... Args>
569 std::vector<Expr> args = {first, rest...};
570 return (*
this)(args);
573 template<
typename... Args>
#define HALIDE_BUFFER_FORWARD(method)
#define HALIDE_BUFFER_FORWARD_INITIALIZER_LIST(method,...)
#define HALIDE_BUFFER_FORWARD_CONST(method)
Methods for managing device allocations when jitting.
Base classes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt)
Defines a Buffer type that wraps from halide_buffer_t and adds functionality, and methods for more co...
Support classes for reference-counting via intrusive shared pointers.
A Halide::Buffer is a named shared reference to a Halide::Runtime::Buffer.
static halide_type_t static_halide_type()
auto operator()() const -> decltype(std::declval< const Runtime::Buffer< T >>()())
const Runtime::Buffer< T > * get() const
Buffer< T > & for_each_element(Fn &&f)
Buffer(Buffer< T2 > &&other) noexcept
Move construct from a Buffer of a different type.
static Buffer< T > make_scalar(T *data, const std::string &name="")
Buffer(T *data, const std::vector< int > &sizes, const std::string &name="")
Buffer(Array(&vals)[N], const std::string &name="")
bool same_as(const Buffer< T2 > &other) const
Check if two Buffer objects point to the same underlying Buffer.
bool defined() const
Check if this Buffer refers to an existing Buffer.
Expr operator()(const Expr &first, Args... rest) const
Make an Expr that loads from this concrete buffer at a computed coordinate.
Buffer< T > & fill(FnOrValue &&f)
Buffer(const std::vector< int > &sizes, const std::vector< int > &storage_order, const std::string &name="")
static bool can_convert_from(const Buffer< T2 > &other)
Buffer & operator=(Buffer &&) noexcept=default
Trivial move assignment operator.
int copy_to_device(const DeviceAPI &d, const Target &t=get_jit_target_from_environment())
Copy to the GPU, using the given device API.
static Buffer< T > make_interleaved(T *data, int width, int height, int channels, const std::string &name="")
Buffer()=default
Make a null Buffer, which points to no Runtime::Buffer.
Buffer(Type t, int first, Args... rest)
Constructors that match Runtime::Buffer with two differences: 1) They take a Type instead of a halide...
Buffer & operator=(const Buffer &that)=default
Trivial copy assignment operator.
static Buffer< Internal::add_const_if_T_is_const< T, void > > make_interleaved(Type t, T *data, int width, int height, int channels, const std::string &name="")
Buffer(const halide_buffer_t &buf, const std::string &name="")
const Buffer< T > & for_each_value(Fn &&f, Args... other_buffers) const
Buffer(const std::vector< int > &sizes, const std::string &name="")
void set_name(const std::string &n)
Buffers are optionally named.
Buffer(Type t, Internal::add_const_if_T_is_const< T, void > *data, const std::vector< int > &sizes, const std::string &name="")
void copy_from(const Buffer< T2 > &other)
Buffer(int first, Args... rest)
const std::string & name() const
Expr operator()(const std::vector< Expr > &args) const
static Buffer< T > make_interleaved(int width, int height, int channels, const std::string &name="")
const Buffer< T > & for_each_element(Fn &&f) const
int device_malloc(const DeviceAPI &d, const Target &t=get_jit_target_from_environment())
Allocate storage on the GPU, using the given device API.
auto operator()() -> decltype(std::declval< Runtime::Buffer< T >>()())
static Buffer< T > make_scalar(const std::string &name="")
Buffer(const Buffer &that)=default
Trivial copy constructor.
static Buffer make_scalar(Type t, const std::string &name="")
Buffer(Type t, Internal::add_const_if_T_is_const< T, void > *data, int first, Args &&...rest)
Buffer(Type t, Internal::add_const_if_T_is_const< T, void > *data, const std::vector< int > &sizes, const std::string &name="")
auto operator()(const int *pos) const -> decltype(std::declval< const Runtime::Buffer< T >>()(pos))
static constexpr bool has_static_halide_type
int copy_to_device(const Target &t=get_jit_target_from_environment())
Copy to the GPU, using the device API that is the default for the given Target.
Buffer(Type t, const std::vector< int > &sizes, const std::string &name="")
static Buffer< T > make_with_shape_of(const Runtime::Buffer< T2 > &src, void *(*allocate_fn)(size_t)=nullptr, void(*deallocate_fn)(void *)=nullptr, const std::string &name="")
virtual ~Buffer()=default
static Buffer make_interleaved(Type t, int width, int height, int channels, const std::string &name="")
Buffer< T > & for_each_value(Fn &&f, Args... other_buffers)
Does the same thing as the equivalent Halide::Runtime::Buffer method.
Runtime::Buffer< T > * get()
Get a pointer to the underlying Runtime::Buffer.
auto operator()(int first, Args &&...args) -> decltype(std::declval< Runtime::Buffer< T >>()(first, std::forward< Args >(args)...))
Buffer(T *data, int d, const halide_dimension_t *shape, const std::string &name="")
Buffer(Type t, const std::vector< int > &sizes, const std::vector< int > &storage_order, const std::string &name="")
int device_malloc(const Target &t=get_jit_target_from_environment())
Allocate on the GPU, using the device API that is the default for the given Target.
Buffer(Runtime::Buffer< T, D > &&buf, const std::string &name="")
Construct a Buffer that captures and owns an rvalue Runtime::Buffer.
static Buffer< T > make_with_shape_of(Buffer< T2 > src, void *(*allocate_fn)(size_t)=nullptr, void(*deallocate_fn)(void *)=nullptr, const std::string &name="")
auto operator()(const int *pos) -> decltype(std::declval< Runtime::Buffer< T >>()(pos))
int device_wrap_native(const DeviceAPI &d, uint64_t handle, const Target &t=get_jit_target_from_environment())
Wrap a native handle, using the given device API.
auto operator()(int first, Args &&...args) const -> decltype(std::declval< const Runtime::Buffer< T >>()(first, std::forward< Args >(args)...))
Buffer(Type t, Internal::add_const_if_T_is_const< T, void > *data, int d, const halide_dimension_t *shape, const std::string &name="")
Buffer(T *data, int first, Args &&...rest)
A class representing a reference count to be used with IntrusivePtr.
A templated Buffer class that wraps halide_buffer_t and adds functionality.
static halide_type_t static_halide_type()
Get the Halide type of T.
static bool can_convert_from(const Buffer< T2, D2 > &other)
Determine if if an Buffer<T, D> can be constructed from some other Buffer type.
std::string make_entity_name(void *stack_ptr, const std::string &type, char prefix)
Make a unique name for an object based on the name of the stack variable passed in.
std::string get_name_from_end_of_parameter_pack(First first, Second second, Args &&...rest)
void buffer_type_name_non_const< void >(std::ostream &s)
void buffer_type_name_non_const(std::ostream &s)
Expr buffer_accessor(const Buffer<> &buf, const std::vector< Expr > &args)
std::string buffer_type_name()
void get_shape_from_start_of_parameter_pack_helper(std::vector< int > &, const std::string &)
typename std::conditional< std::is_const< T >::value, const T2, T2 >::type add_const_if_T_is_const
std::vector< int > get_shape_from_start_of_parameter_pack(Args &&...args)
std::string get_name_from_end_of_parameter_pack(T &&)
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
std::string type_to_c_type(Type type, bool include_space, bool c_plus_plus=true)
Halide type to a C++ type.
@ Internal
Not visible externally, similar to 'static' linkage in C.
Expr min(const FuncRef &a, const FuncRef &b)
Explicit overloads of min and max for FuncRef.
Target get_jit_target_from_environment()
Return the target that Halide will use for jit-compilation.
DeviceAPI
An enum describing a type of device API.
const halide_device_interface_t * get_device_interface_for_device_api(DeviceAPI d, const Target &t=get_jit_target_from_environment(), const char *error_site=nullptr)
Gets the appropriate halide_device_interface_t * for a DeviceAPI.
unsigned __INT64_TYPE__ uint64_t
A fragment of Halide syntax.
Intrusive shared pointers have a reference count (a RefCount object) stored in the class itself.
HALIDE_ALWAYS_INLINE bool defined() const
T * get() const
Access the raw pointer in a variety of ways.
A struct representing a target machine and os to generate code for.
Types in the halide type system.
The raw representation of an image passed around by generated Halide code.
A runtime tag for a type in the halide type system.