1 #ifndef HALIDE_BUFFER_H
2 #define HALIDE_BUFFER_H
11 template<
typename T =
void,
int Dims = Hal
ide::Runtime::AnyDims>
14 struct JITUserContext;
26 template<
typename... Args>
29 template<
typename First,
typename... Rest>
31 all_ints_and_optional_name<Rest...>> {};
35 std::is_convertible<T, int>> {};
41 typename =
typename std::enable_if<!std::is_convertible<T, std::string>::value>::type>
54 template<
typename First,
67 template<
typename... Args>
73 template<
typename... Args>
75 std::vector<int> result;
80 template<
typename T,
typename T2>
88 s << type_to_c_type(type_of<T>(),
false);
98 std::ostringstream oss;
99 if (std::is_const<T>::value) {
102 buffer_type_name_non_const<typename std::remove_const<T>::type>(oss);
119 template<
typename T,
int Dims>
123 template<
typename T2,
int D2>
126 template<
typename T2,
int D2>
127 static void assert_can_convert_from(
const Buffer<T2, D2> &other) {
130 static_assert((!std::is_const<T2>::value || std::is_const<T>::value),
131 "Can't convert from a Buffer<const T> to a Buffer<T>");
132 static_assert(std::is_same<
typename std::remove_const<T>::type,
133 typename std::remove_const<T2>::type>::value ||
134 std::is_void<T>::value ||
135 std::is_void<T2>::value,
136 "type mismatch constructing Buffer");
138 "Can't convert from a Buffer with static dimensionality to a Buffer with different static dimensionality");
149 <<
"Type mismatch constructing Buffer. Can't construct Buffer<"
150 << Internal::buffer_type_name<T>() <<
", " << Dims <<
"> from Buffer<"
151 <<
type_to_c_type(other.
type(),
false) <<
", " << D2 <<
">, dimensions() = " << other.dimensions() <<
"\n";
157 static_assert(Dims ==
AnyDims || Dims >= 0);
178 template<typename T2,
int D2>
180 : contents(other.contents) {
181 assert_can_convert_from(other);
185 template<
typename T2,
int D2>
187 assert_can_convert_from(other);
188 contents = std::move(other.contents);
194 : contents(new
Internal::BufferContents) {
195 contents->buf = std::move(buf);
199 contents->name =
name;
208 template<
typename... Args,
211 int first, Args... rest)
217 const std::string &
name =
"")
221 template<
typename... Args,
229 const std::vector<int> &sizes,
230 const std::string &
name =
"")
235 const std::vector<int> &sizes,
236 const std::vector<int> &storage_order,
237 const std::string &
name =
"")
241 explicit Buffer(
const std::vector<int> &sizes,
242 const std::string &
name =
"")
246 explicit Buffer(
const std::vector<int> &sizes,
247 const std::vector<int> &storage_order,
248 const std::string &
name =
"")
252 template<
typename Array,
size_t N>
254 const std::string &
name =
"")
258 template<
typename... Args,
262 int first, Args &&...rest)
267 template<
typename... Args,
271 const std::vector<int> &sizes,
272 const std::string &
name =
"")
276 template<
typename... Args,
279 int first, Args &&...rest)
285 const std::vector<int> &sizes,
286 const std::string &
name =
"")
292 const std::vector<int> &sizes,
293 const std::string &
name =
"")
301 const std::string &
name =
"")
308 const std::string &
name =
"")
342 template<
typename T2,
int D2>
344 void *(*allocate_fn)(
size_t) =
nullptr,
345 void (*deallocate_fn)(
void *) =
nullptr,
346 const std::string &
name =
"") {
350 template<
typename T2,
int D2>
352 void *(*allocate_fn)(
size_t) =
nullptr,
353 void (*deallocate_fn)(
void *) =
nullptr,
354 const std::string &
name =
"") {
365 const std::string &
name()
const {
366 return contents->name;
371 template<
typename T2,
int D2>
373 return (
const void *)(contents.
get()) == (
const void *)(other.contents.get());
395 #define HALIDE_BUFFER_FORWARD_CONST(method) \
396 template<typename... Args> \
397 auto method(Args &&...args) const->decltype(std::declval<const Runtime::Buffer<T, Dims>>().method(std::forward<Args>(args)...)) { \
398 user_assert(defined()) << "Undefined buffer calling const method " #method "\n"; \
399 return get()->method(std::forward<Args>(args)...); \
402 #define HALIDE_BUFFER_FORWARD(method) \
403 template<typename... Args> \
404 auto method(Args &&...args)->decltype(std::declval<Runtime::Buffer<T, Dims>>().method(std::forward<Args>(args)...)) { \
405 user_assert(defined()) << "Undefined buffer calling method " #method "\n"; \
406 return get()->method(std::forward<Args>(args)...); \
419 #define HALIDE_BUFFER_FORWARD_INITIALIZER_LIST(method, ...) \
420 inline auto method(const __VA_ARGS__ &a)->decltype(std::declval<Runtime::Buffer<T, Dims>>().method(a)) { \
421 user_assert(defined()) << "Undefined buffer calling method " #method "\n"; \
422 return get()->method(a); \
477 #undef HALIDE_BUFFER_FORWARD
478 #undef HALIDE_BUFFER_FORWARD_CONST
480 template<
typename Fn,
typename... Args>
482 get()->for_each_value(std::forward<Fn>(f), (*std::forward<Args>(other_buffers).
get())...);
486 template<
typename Fn,
typename... Args>
488 get()->for_each_value(std::forward<Fn>(f), (*std::forward<Args>(other_buffers).
get())...);
492 template<
typename Fn>
494 get()->for_each_element(std::forward<Fn>(f));
498 template<
typename Fn>
500 get()->for_each_element(std::forward<Fn>(f));
504 template<
typename FnOrValue>
506 get()->fill(std::forward<FnOrValue>(f));
522 template<
typename T2,
int D2>
532 return contents->buf.type();
535 template<
typename T2,
int D2 = Dims>
544 template<
typename T2,
int D2>
546 contents->buf.copy_from(*other.
get());
549 template<
typename... Args>
551 return (*
get())(first, std::forward<Args>(args)...);
554 template<
typename... Args>
556 return (*
get())(first, std::forward<Args>(args)...);
560 return (*
get())(pos);
564 return (*
get())(pos);
578 template<
typename... Args>
580 std::vector<Expr> args = {first, rest...};
581 return (*
this)(args);
584 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 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="")
const Runtime::Buffer< T, Dims > * get() const
auto operator()(const int *pos) const -> decltype(std::declval< const Runtime::Buffer< T, Dims >>()(pos))
const Buffer< T, Dims > & for_each_value(Fn &&f, Args... other_buffers) const
static constexpr int static_dimensions()
Buffer< T, Dims > & for_each_element(Fn &&f)
Buffer(Type t, const std::vector< int > &sizes, const std::string &name="")
auto operator()() const -> decltype(std::declval< const Runtime::Buffer< T, Dims >>()())
Buffer(Runtime::Buffer< T, D2 > &&buf, const std::string &name="")
Construct a Buffer that captures and owns an rvalue Runtime::Buffer.
Expr operator()(const Expr &first, Args... rest) const
Make an Expr that loads from this concrete buffer at a computed coordinate.
Runtime::Buffer< T, Dims > * get()
Get a pointer to the underlying Runtime::Buffer.
void set_name(const std::string &n)
Buffers are optionally named.
static constexpr bool has_static_dimensions
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.
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="")
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...
auto operator()() -> decltype(std::declval< Runtime::Buffer< T, Dims >>()())
Buffer< T, Dims > & fill(FnOrValue &&f)
bool defined() const
Check if this Buffer refers to an existing Buffer.
auto operator()(const int *pos) -> decltype(std::declval< Runtime::Buffer< T, Dims >>()(pos))
const Buffer< T, Dims > & for_each_element(Fn &&f) const
Buffer< T, Dims > copy() const
bool same_as(const Buffer< T2, D2 > &other) const
Check if two Buffer objects point to the same underlying Buffer.
const std::string & name() const
static Buffer make_interleaved(Type t, int width, int height, int channels, const std::string &name="")
auto operator()(int first, Args &&...args) -> decltype(std::declval< Runtime::Buffer< T, Dims >>()(first, std::forward< Args >(args)...))
static Buffer make_scalar(Type t, const std::string &name="")
static Buffer< T, Dims > make_interleaved(int width, int height, int channels, 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="")
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()
Buffer & operator=(Buffer &&) noexcept=default
Trivial move assignment operator.
Buffer(Array(&vals)[N], const std::string &name="")
static Buffer< T, Dims > make_interleaved(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::vector< int > &storage_order, const std::string &name="")
Buffer< T, Dims > & for_each_value(Fn &&f, Args... other_buffers)
Does the same thing as the equivalent Halide::Runtime::Buffer method.
Expr operator()(const std::vector< Expr > &args) const
Buffer & operator=(const Buffer &that)=default
Trivial copy assignment operator.
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.
static Buffer< T, Dims > make_scalar(T *data, const std::string &name="")
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(int first, Args... rest)
Buffer(T *data, const std::vector< int > &sizes, const std::string &name="")
Buffer(T *data, int first, Args &&...rest)
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.
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="")
Buffer(Type t, const std::vector< int > &sizes, const std::vector< int > &storage_order, const std::string &name="")
auto operator()(int first, Args &&...args) const -> decltype(std::declval< const Runtime::Buffer< T, Dims >>()(first, std::forward< Args >(args)...))
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< T, Dims > make_scalar(const std::string &name="")
Buffer(const std::vector< int > &sizes, const std::string &name="")
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)
void copy_from(const Buffer< T2, D2 > &other)
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.
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 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.