13template<
typename T =
void,
int Dims = AnyDims>
28template<
typename... Args>
31template<
typename First,
typename... Rest>
33 all_ints_and_optional_name<Rest...>> {};
37 std::is_convertible<T, int>> {};
43 typename = std::enable_if_t<!std::is_convertible_v<T, std::string>>>
56template<
typename First,
69template<
typename... Args>
75template<
typename... Args>
77 std::vector<int> result;
82template<
typename T,
typename T2>
90 s << type_to_c_type(type_of<T>(),
false);
100 std::ostringstream oss;
101 if (std::is_const_v<T>) {
104 buffer_type_name_non_const<std::remove_const_t<T>>(oss);
121template<
typename T,
int Dims>
125 template<
typename T2,
int D2>
128 template<
typename T2,
int D2>
129 static void assert_can_convert_from(
const Buffer<T2, D2> &other) {
132 static_assert(!std::is_const_v<T2> || std::is_const_v<T>,
133 "Can't convert from a Buffer<const T> to a Buffer<T>");
134 static_assert(std::is_same_v<std::remove_const_t<T>, std::remove_const_t<T2>> ||
137 "type mismatch constructing Buffer");
139 "Can't convert from a Buffer with static dimensionality to a Buffer with different static dimensionality");
150 <<
"Type mismatch constructing Buffer. Can't construct Buffer<"
151 << Internal::buffer_type_name<T>() <<
", " << Dims <<
"> from Buffer<"
152 <<
type_to_c_type(other.
type(),
false) <<
", " << D2 <<
">, dimensions() = " << other.dimensions() <<
"\n";
158 static_assert(Dims ==
AnyDims || Dims >= 0);
179 template<typename T2,
int D2>
181 : contents(other.contents) {
182 assert_can_convert_from(other);
186 template<
typename T2,
int D2>
188 assert_can_convert_from(other);
189 contents = std::move(other.contents);
195 : contents(new
Internal::BufferContents) {
196 contents->buf = std::move(buf);
200 contents->name =
name;
209 template<
typename... Args,
212 int first, Args... rest)
218 const std::string &
name =
"")
222 template<
typename... Args,
224 explicit Buffer(
int first,
const Args &...rest)
230 const std::vector<int> &sizes,
231 const std::string &
name =
"")
236 const std::vector<int> &sizes,
237 const std::vector<int> &storage_order,
238 const std::string &
name =
"")
242 explicit Buffer(
const std::vector<int> &sizes,
243 const std::string &
name =
"")
247 explicit Buffer(
const std::vector<int> &sizes,
248 const std::vector<int> &storage_order,
249 const std::string &
name =
"")
253 template<
typename Array,
size_t N>
255 const std::string &
name =
"")
259 template<
typename... Args,
263 int first, Args &&...rest)
268 template<
typename... Args,
272 const std::vector<int> &sizes,
273 const std::string &
name =
"")
277 template<
typename... Args,
280 int first, Args &&...rest)
286 const std::vector<int> &sizes,
287 const std::string &
name =
"")
293 const std::vector<int> &sizes,
294 const std::string &
name =
"")
302 const std::string &
name =
"")
309 const std::string &
name =
"")
343 template<
typename T2,
int D2>
345 void *(*allocate_fn)(
size_t) =
nullptr,
346 void (*deallocate_fn)(
void *) =
nullptr,
347 const std::string &
name =
"") {
351 template<
typename T2,
int D2>
353 void *(*allocate_fn)(
size_t) =
nullptr,
354 void (*deallocate_fn)(
void *) =
nullptr,
355 const std::string &
name =
"") {
366 const std::string &
name()
const {
367 return contents->name;
372 template<
typename T2,
int D2>
374 return (
const void *)(contents.
get()) == (
const void *)(other.contents.get());
396#define HALIDE_BUFFER_FORWARD_CONST(method) \
397 template<typename... Args> \
398 auto method(Args &&...args) const -> decltype(std::declval<const Runtime::Buffer<T, Dims>>().method(std::forward<Args>(args)...)) { \
399 user_assert(defined()) << "Undefined buffer calling const method " #method "\n"; \
400 return get()->method(std::forward<Args>(args)...); \
403#define HALIDE_BUFFER_FORWARD(method) \
404 template<typename... Args> \
405 auto method(Args &&...args) -> decltype(std::declval<Runtime::Buffer<T, Dims>>().method(std::forward<Args>(args)...)) { \
406 user_assert(defined()) << "Undefined buffer calling method " #method "\n"; \
407 return get()->method(std::forward<Args>(args)...); \
420#define HALIDE_BUFFER_FORWARD_INITIALIZER_LIST(method, ...) \
421 inline auto method(const __VA_ARGS__ &a) -> decltype(std::declval<Runtime::Buffer<T, Dims>>().method(a)) { \
422 user_assert(defined()) << "Undefined buffer calling method " #method "\n"; \
423 return get()->method(a); \
480#undef HALIDE_BUFFER_FORWARD
481#undef HALIDE_BUFFER_FORWARD_CONST
483 template<
typename Fn,
typename... Args>
485 get()->for_each_value(std::forward<Fn>(f), (*std::forward<Args>(other_buffers).
get())...);
489 template<
typename Fn,
typename... Args>
491 get()->for_each_value(std::forward<Fn>(f), (*std::forward<Args>(other_buffers).
get())...);
495 template<
typename Fn>
497 get()->for_each_element(std::forward<Fn>(f));
501 template<
typename Fn>
503 get()->for_each_element(std::forward<Fn>(f));
507 template<
typename FnOrValue>
509 get()->fill(std::forward<FnOrValue>(f));
525 template<
typename T2,
int D2>
535 return contents->buf.type();
538 template<
typename T2,
int D2 = Dims>
547 template<
typename T2,
int D2>
549 contents->buf.copy_from(*other.
get());
552 template<
typename... Args>
553 auto operator()(
int first, Args &&...args) ->
decltype(std::declval<Runtime::Buffer<T, Dims>>()(first, std::forward<Args>(args)...)) {
554 return (*
get())(first, std::forward<Args>(args)...);
557 template<
typename... Args>
558 auto operator()(
int first, Args &&...args)
const ->
decltype(std::declval<const Runtime::Buffer<T, Dims>>()(first, std::forward<Args>(args)...)) {
559 return (*
get())(first, std::forward<Args>(args)...);
562 auto operator()(
const int *pos) ->
decltype(std::declval<Runtime::Buffer<T, Dims>>()(pos)) {
563 return (*
get())(pos);
566 auto operator()(
const int *pos)
const ->
decltype(std::declval<const Runtime::Buffer<T, Dims>>()(pos)) {
567 return (*
get())(pos);
570 auto operator()() ->
decltype(std::declval<Runtime::Buffer<T, Dims>>()()) {
584 template<
typename... Args>
586 std::vector<Expr> args = {first, rest...};
587 return (*
this)(args);
590 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.
auto operator()() -> decltype(std::declval< Runtime::Buffer< T, Dims > >()())
Buffer & operator=(const Buffer &that)=default
Trivial copy assignment operator.
static Buffer< T, Dims > make_with_shape_of(const Runtime::Buffer< T2, D2 > &src, void *(*allocate_fn)(size_t)=nullptr, void(*deallocate_fn)(void *)=nullptr, const std::string &name="")
static constexpr int static_dimensions()
Buffer(Type t, const std::vector< int > &sizes, const std::string &name="")
Buffer(Runtime::Buffer< T, D2 > &&buf, const std::string &name="")
Construct a Buffer that captures and owns an rvalue Runtime::Buffer.
Buffer< T, Dims > & for_each_value(Fn &&f, Args... other_buffers)
Does the same thing as the equivalent Halide::Runtime::Buffer method.
void set_name(const std::string &n)
Buffers are optionally named.
static constexpr bool has_static_dimensions
const Buffer< T, Dims > & for_each_element(Fn &&f) const
int device_malloc(const DeviceAPI &d, const Target &t=get_jit_target_from_environment(), JITUserContext *context=nullptr)
Allocate storage on the GPU, using the given device API.
virtual ~Buffer()=default
Buffer(const Buffer &that)=default
Trivial copy constructor.
Buffer(Type t, int first, Args... rest)
Constructors that match Runtime::Buffer with two differences: 1) They take a Type instead of a halide...
const Buffer< T, Dims > & for_each_value(Fn &&f, Args... other_buffers) const
static Buffer< T, Dims > make_interleaved(int width, int height, int channels, const std::string &name="")
bool defined() const
Check if this Buffer refers to an existing Buffer.
const Expr operator()(const std::vector< Expr > &args) const
bool same_as(const Buffer< T2, D2 > &other) const
Check if two Buffer objects point to the same underlying Buffer.
auto operator()(const int *pos) -> decltype(std::declval< Runtime::Buffer< T, Dims > >()(pos))
Buffer< T, Dims > & for_each_element(Fn &&f)
static Buffer make_interleaved(Type t, int width, int height, int channels, const std::string &name="")
static Buffer make_scalar(Type t, const std::string &name="")
static Buffer< T, Dims > make_scalar(T *data, const std::string &name="")
Buffer(Type t, Internal::add_const_if_T_is_const< T, void > *data, int d, const halide_dimension_t *shape, const std::string &name="")
auto operator()(int first, Args &&...args) -> decltype(std::declval< Runtime::Buffer< T, Dims > >()(first, std::forward< Args >(args)...))
static constexpr int AnyDims
int copy_to_device(const DeviceAPI &d, const Target &t=get_jit_target_from_environment(), JITUserContext *context=nullptr)
Copy to the GPU, using the given device API.
static constexpr halide_type_t static_halide_type()
auto operator()(int first, Args &&...args) const -> decltype(std::declval< const Runtime::Buffer< T, Dims > >()(first, std::forward< Args >(args)...))
Buffer(Array(&vals)[N], const std::string &name="")
Buffer< T, Dims > copy() const
Buffer(const std::vector< int > &sizes, const std::vector< int > &storage_order, const std::string &name="")
Buffer< T, Dims > & fill(FnOrValue &&f)
const std::string & name() const
static Buffer< T, Dims > make_with_shape_of(Buffer< T2, D2 > src, void *(*allocate_fn)(size_t)=nullptr, void(*deallocate_fn)(void *)=nullptr, const std::string &name="")
Runtime::Buffer< T, Dims > * get()
Get a pointer to the underlying Runtime::Buffer.
int device_wrap_native(const DeviceAPI &d, uint64_t handle, const Target &t=get_jit_target_from_environment(), JITUserContext *context=nullptr)
Wrap a native handle, using the given device API.
Buffer()=default
Make a null Buffer, which points to no Runtime::Buffer.
int device_malloc(const Target &t=get_jit_target_from_environment(), JITUserContext *context=nullptr)
Allocate on the GPU, using the device API that is the default for the given Target.
Buffer(Type t, Internal::add_const_if_T_is_const< T, void > *data, const std::vector< int > &sizes, const std::string &name="")
static constexpr bool has_static_halide_type
Buffer(T *data, const std::vector< int > &sizes, const std::string &name="")
Buffer(T *data, int first, Args &&...rest)
static Buffer< T, Dims > make_scalar(const std::string &name="")
Buffer(Type t, Internal::add_const_if_T_is_const< T, void > *data, int first, Args &&...rest)
Buffer(Buffer< T2, D2 > &&other) noexcept
Move construct from a Buffer of a different type.
Buffer & operator=(Buffer &&) noexcept=default
Trivial move assignment operator.
Buffer(int first, const Args &...rest)
Buffer(Type t, const std::vector< int > &sizes, const std::vector< int > &storage_order, const std::string &name="")
auto operator()(const int *pos) const -> decltype(std::declval< const Runtime::Buffer< T, Dims > >()(pos))
int copy_to_device(const Target &t=get_jit_target_from_environment(), JITUserContext *context=nullptr)
Copy to the GPU, using the device API that is the default for the given Target.
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< T2, D2 > as() const
Buffer(const std::vector< int > &sizes, const std::string &name="")
const Expr operator()(const Expr &first, const Args &...rest) const
Make an Expr that loads from this concrete buffer at a computed coordinate.
Buffer(const halide_buffer_t &buf, const std::string &name="")
Buffer(T *data, int d, const halide_dimension_t *shape, const std::string &name="")
Buffer(Type t, Internal::add_const_if_T_is_const< T, void > *data, const std::vector< int > &sizes, const std::string &name="")
static bool can_convert_from(const Buffer< T2, D2 > &other)
const Runtime::Buffer< T, Dims > * get() const
auto operator()() const -> decltype(std::declval< const Runtime::Buffer< T, Dims > >()())
void copy_from(const Buffer< T2, D2 > &other)
static Buffer< T, Dims > make_interleaved(T *data, int width, int height, int channels, const std::string &name="")
A class representing a reference count to be used with IntrusivePtr.
A templated Buffer class that wraps halide_buffer_t and adds functionality.
static bool can_convert_from(const Buffer< T2, D2, S2 > &other)
Determine if a Buffer<T, Dims, InClassDimStorage> can be constructed from some other Buffer type.
static constexpr halide_type_t static_halide_type()
Get the Halide type of T.
static constexpr int static_dimensions()
Callers should not use the result if has_static_dimensions is false.
HALIDE_ALWAYS_INLINE auto slice(Vec vec, Base base, Stride stride, Lanes lanes) noexcept -> SliceOp< decltype(pattern_arg(vec)), decltype(pattern_arg(base)), decltype(pattern_arg(stride)), decltype(pattern_arg(lanes))>
std::vector< int > get_shape_from_start_of_parameter_pack(Args &&...args)
auto begin(reverse_adaptor< T > i)
std::conditional_t< std::is_const_v< T >, const T2, T2 > add_const_if_T_is_const
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 unique_name(char prefix)
Generate a unique name starting with the given prefix.
std::string buffer_type_name()
void get_shape_from_start_of_parameter_pack_helper(std::vector< int > &, const std::string &)
auto end(reverse_adaptor< T > i)
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.
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.
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.
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 context to be passed to Pipeline::realize.
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.