Halide 22.0.0
Halide compiler and libraries
Loading...
Searching...
No Matches
Buffer.h
Go to the documentation of this file.
1#ifndef HALIDE_BUFFER_H
2#define HALIDE_BUFFER_H
3
4#include "DeviceInterface.h"
5#include "Expr.h"
6#include "IntrusivePtr.h"
8
9namespace Halide {
10
11constexpr int AnyDims = Halide::Runtime::AnyDims; // -1
12
13template<typename T = void, int Dims = AnyDims>
14class Buffer;
15
16struct JITUserContext;
17
18namespace Internal {
19
25
26Expr buffer_accessor(const Buffer<> &buf, const std::vector<Expr> &args);
27
28template<typename... Args>
29struct all_ints_and_optional_name : std::false_type {};
30
31template<typename First, typename... Rest>
32struct all_ints_and_optional_name<First, Rest...> : meta_and<std::is_convertible<First, int>,
33 all_ints_and_optional_name<Rest...>> {};
34
35template<typename T>
36struct all_ints_and_optional_name<T> : meta_or<std::is_convertible<T, std::string>,
37 std::is_convertible<T, int>> {};
38
39template<>
40struct all_ints_and_optional_name<> : std::true_type {};
41
42template<typename T,
43 typename = std::enable_if_t<!std::is_convertible_v<T, std::string>>>
45 return "";
46}
47
48inline std::string get_name_from_end_of_parameter_pack(const std::string &n) {
49 return n;
50}
51
53 return "";
54}
55
56template<typename First,
57 typename Second,
58 typename... Args>
60 return get_name_from_end_of_parameter_pack(second, std::forward<Args>(rest)...);
61}
62
63inline void get_shape_from_start_of_parameter_pack_helper(std::vector<int> &, const std::string &) {
64}
65
66inline void get_shape_from_start_of_parameter_pack_helper(std::vector<int> &) {
67}
68
69template<typename... Args>
70void get_shape_from_start_of_parameter_pack_helper(std::vector<int> &result, int x, Args &&...rest) {
71 result.push_back(x);
72 get_shape_from_start_of_parameter_pack_helper(result, std::forward<Args>(rest)...);
73}
74
75template<typename... Args>
76std::vector<int> get_shape_from_start_of_parameter_pack(Args &&...args) {
77 std::vector<int> result;
78 get_shape_from_start_of_parameter_pack_helper(result, std::forward<Args>(args)...);
79 return result;
80}
81
82template<typename T, typename T2>
83using add_const_if_T_is_const = std::conditional_t<std::is_const_v<T>, const T2, T2>;
84
85// Helpers to produce the name of a Buffer element type (a Halide
86// scalar type, or void, possibly with const). Useful for an error
87// messages.
88template<typename T>
89void buffer_type_name_non_const(std::ostream &s) {
91}
92
93template<>
94inline void buffer_type_name_non_const<void>(std::ostream &s) {
95 s << "void";
96}
97
98template<typename T>
99std::string buffer_type_name() {
100 std::ostringstream oss;
101 if (std::is_const_v<T>) {
102 oss << "const ";
103 }
105 return oss.str();
106}
107
108} // namespace Internal
109
110/** A Halide::Buffer is a named shared reference to a
111 * Halide::Runtime::Buffer.
112 *
113 * A Buffer<T1, D> can refer to a Buffer<T2, D> if T1 is const whenever T2
114 * is const, and either T1 = T2 or T1 is void. A Buffer<void, D> can
115 * refer to any Buffer of any non-const type, and the default
116 * template parameter is T = void.
117 *
118 * A Buffer<T, D1> can refer to a Buffer<T, D2> if D1 == D2,
119 * or if D1 is AnyDims (meaning "dimensionality is checked at runtime, not compiletime").
120 */
121template<typename T, int Dims>
122class Buffer {
124
125 template<typename T2, int D2>
126 friend class Buffer;
127
128 template<typename T2, int D2>
129 static void assert_can_convert_from(const Buffer<T2, D2> &other) {
130 if (!other.defined()) {
131 // Avoid UB of deferencing offset of a null contents ptr
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>> ||
135 std::is_void_v<T> ||
136 std::is_void_v<T2>,
137 "type mismatch constructing Buffer");
138 static_assert(Dims == AnyDims || D2 == AnyDims || Dims == D2,
139 "Can't convert from a Buffer with static dimensionality to a Buffer with different static dimensionality");
140 } else {
141 // Don't delegate to
142 // Runtime::Buffer<T>::assert_can_convert_from. It might
143 // not assert if NDEBUG is defined. user_assert is
144 // friendlier anyway because it reports line numbers when
145 // debugging symbols are found, it throws an exception
146 // when exceptions are enabled, and we can print the
147 // actual types in question.
148 using BufType = Runtime::Buffer<T, Dims>; // alias because commas in user_assert() macro confuses compiler
149 user_assert(BufType::can_convert_from(*(other.get())))
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";
153 }
154 }
155
156public:
157 static constexpr int AnyDims = Halide::AnyDims;
158 static_assert(Dims == AnyDims || Dims >= 0);
159
160 typedef T ElemType;
161
162 // This class isn't final (and is subclassed from the Python binding
163 // code, at least) so it needs a virtual dtor.
164 virtual ~Buffer() = default;
165
166 /** Make a null Buffer, which points to no Runtime::Buffer */
167 Buffer() = default;
168
169 /** Trivial copy constructor. */
170 Buffer(const Buffer &that) = default;
171
172 /** Trivial copy assignment operator. */
173 Buffer &operator=(const Buffer &that) = default;
174
175 /** Trivial move assignment operator. */
177
178 /** Make a Buffer from a Buffer of a different type */
179 template<typename T2, int D2>
181 : contents(other.contents) {
182 assert_can_convert_from(other);
183 }
184
185 /** Move construct from a Buffer of a different type */
186 template<typename T2, int D2>
188 assert_can_convert_from(other);
189 contents = std::move(other.contents);
190 }
191
192 /** Construct a Buffer that captures and owns an rvalue Runtime::Buffer */
193 template<int D2>
194 Buffer(Runtime::Buffer<T, D2> &&buf, const std::string &name = "")
195 : contents(new Internal::BufferContents) {
196 contents->buf = std::move(buf);
197 if (name.empty()) {
198 contents->name = Internal::unique_name('b');
199 } else {
200 contents->name = name;
201 }
202 }
203
204 /** Constructors that match Runtime::Buffer with two differences:
205 * 1) They take a Type instead of a halide_type_t
206 * 2) There is an optional last string argument that gives the buffer a specific name
207 */
208 // @{
209 template<typename... Args,
210 typename = std::enable_if_t<Internal::all_ints_and_optional_name<Args...>::value>>
211 explicit Buffer(Type t,
212 int first, Args... rest)
213 : Buffer(Runtime::Buffer<T, Dims>(t, Internal::get_shape_from_start_of_parameter_pack(first, rest...)),
214 Internal::get_name_from_end_of_parameter_pack(rest...)) {
215 }
216
217 explicit Buffer(const halide_buffer_t &buf,
218 const std::string &name = "")
219 : Buffer(Runtime::Buffer<T, Dims>(buf), name) {
220 }
221
222 template<typename... Args,
223 typename = std::enable_if_t<Internal::all_ints_and_optional_name<Args...>::value>>
224 explicit Buffer(int first, const Args &...rest)
225 : Buffer(Runtime::Buffer<T, Dims>(Internal::get_shape_from_start_of_parameter_pack(first, rest...)),
226 Internal::get_name_from_end_of_parameter_pack(rest...)) {
227 }
228
229 explicit Buffer(Type t,
230 const std::vector<int> &sizes,
231 const std::string &name = "")
232 : Buffer(Runtime::Buffer<T, Dims>(t, sizes), name) {
233 }
234
235 explicit Buffer(Type t,
236 const std::vector<int> &sizes,
237 const std::vector<int> &storage_order,
238 const std::string &name = "")
239 : Buffer(Runtime::Buffer<T, Dims>(t, sizes, storage_order), name) {
240 }
241
242 explicit Buffer(const std::vector<int> &sizes,
243 const std::string &name = "")
244 : Buffer(Runtime::Buffer<T, Dims>(sizes), name) {
245 }
246
247 explicit Buffer(const std::vector<int> &sizes,
248 const std::vector<int> &storage_order,
249 const std::string &name = "")
250 : Buffer(Runtime::Buffer<T, Dims>(sizes, storage_order), name) {
251 }
252
253 template<typename Array, size_t N>
254 explicit Buffer(Array (&vals)[N],
255 const std::string &name = "")
256 : Buffer(Runtime::Buffer<T, Dims>(vals), name) {
257 }
258
259 template<typename... Args,
260 typename = std::enable_if_t<Internal::all_ints_and_optional_name<Args...>::value>>
261 explicit Buffer(Type t,
263 int first, Args &&...rest)
264 : Buffer(Runtime::Buffer<T, Dims>(t, data, Internal::get_shape_from_start_of_parameter_pack(first, rest...)),
265 Internal::get_name_from_end_of_parameter_pack(rest...)) {
266 }
267
268 template<typename... Args,
269 typename = std::enable_if_t<Internal::all_ints_and_optional_name<Args...>::value>>
270 explicit Buffer(Type t,
272 const std::vector<int> &sizes,
273 const std::string &name = "")
274 : Buffer(Runtime::Buffer<T, Dims>(t, data, sizes, name)) {
275 }
276
277 template<typename... Args,
278 typename = std::enable_if_t<Internal::all_ints_and_optional_name<Args...>::value>>
279 explicit Buffer(T *data,
280 int first, Args &&...rest)
281 : Buffer(Runtime::Buffer<T, Dims>(data, Internal::get_shape_from_start_of_parameter_pack(first, rest...)),
282 Internal::get_name_from_end_of_parameter_pack(rest...)) {
283 }
284
285 explicit Buffer(T *data,
286 const std::vector<int> &sizes,
287 const std::string &name = "")
288 : Buffer(Runtime::Buffer<T, Dims>(data, sizes), name) {
289 }
290
291 explicit Buffer(Type t,
293 const std::vector<int> &sizes,
294 const std::string &name = "")
295 : Buffer(Runtime::Buffer<T, Dims>(t, data, sizes), name) {
296 }
297
298 explicit Buffer(Type t,
300 int d,
301 const halide_dimension_t *shape,
302 const std::string &name = "")
303 : Buffer(Runtime::Buffer<T, Dims>(t, data, d, shape), name) {
304 }
305
306 explicit Buffer(T *data,
307 int d,
308 const halide_dimension_t *shape,
309 const std::string &name = "")
310 : Buffer(Runtime::Buffer<T, Dims>(data, d, shape), name) {
311 }
312
313 static Buffer<T, Dims> make_scalar(const std::string &name = "") {
315 }
316
317 static Buffer<> make_scalar(Type t, const std::string &name = "") {
319 }
320
321 static Buffer<T, Dims> make_scalar(T *data, const std::string &name = "") {
323 }
324
325 static Buffer<T, Dims> make_interleaved(int width, int height, int channels, const std::string &name = "") {
326 return Buffer<T, Dims>(Runtime::Buffer<T, Dims>::make_interleaved(width, height, channels), name);
327 }
328
329 static Buffer<> make_interleaved(Type t, int width, int height, int channels, const std::string &name = "") {
330 return Buffer<>(Runtime::Buffer<>::make_interleaved(t, width, height, channels), name);
331 }
332
333 static Buffer<T, Dims> make_interleaved(T *data, int width, int height, int channels, const std::string &name = "") {
334 return Buffer<T, Dims>(Runtime::Buffer<T, Dims>::make_interleaved(data, width, height, channels), name);
335 }
336
338 make_interleaved(Type t, T *data, int width, int height, int channels, const std::string &name = "") {
340 return Buffer<T2, Dims>(Runtime::Buffer<T2, Dims>::make_interleaved(t, data, width, height, channels), name);
341 }
342
343 template<typename T2, int D2>
345 void *(*allocate_fn)(size_t) = nullptr,
346 void (*deallocate_fn)(void *) = nullptr,
347 const std::string &name = "") {
349 }
350
351 template<typename T2, int D2>
353 void *(*allocate_fn)(size_t) = nullptr,
354 void (*deallocate_fn)(void *) = nullptr,
355 const std::string &name = "") {
357 }
358 // @}
359
360 /** Buffers are optionally named. */
361 // @{
362 void set_name(const std::string &n) {
363 contents->name = n;
364 }
365
366 const std::string &name() const {
367 return contents->name;
368 }
369 // @}
370
371 /** Check if two Buffer objects point to the same underlying Buffer */
372 template<typename T2, int D2>
373 bool same_as(const Buffer<T2, D2> &other) const {
374 return (const void *)(contents.get()) == (const void *)(other.contents.get());
375 }
376
377 /** Check if this Buffer refers to an existing
378 * Buffer. Default-constructed Buffer objects do not refer to any
379 * existing Buffer. */
380 bool defined() const {
381 return contents.defined();
382 }
383
384 /** Get a pointer to the underlying Runtime::Buffer */
385 // @{
387 // It's already type-checked, so no need to use as<T>.
388 return (Runtime::Buffer<T, Dims> *)(&contents->buf);
389 }
391 return (const Runtime::Buffer<T, Dims> *)(&contents->buf);
392 }
393 // @}
394
395 // We forward numerous methods from the underlying Buffer
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)...); \
401 }
402
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)...); \
408 }
409
410// This is a weird-looking but effective workaround for a deficiency in "perfect forwarding":
411// namely, it can't really handle initializer-lists. The idea here is that we declare
412// the expected type to be passed on, and that allows the compiler to handle it.
413// The weirdness comes in with the variadic macro: the problem is that the type
414// we want to forward might be something like `std::vector<std::pair<int, int>>`,
415// which contains a comma, which throws a big wrench in C++ macro system.
416// However... since all we really need to do is capture the remainder of the macro,
417// and forward it as is, we can just use ... to allow an arbitrary number of commas,
418// then use __VA_ARGS__ to forward the mess as-is, and while it looks horrible, it
419// works.
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); \
424 }
425
426 /** Does the same thing as the equivalent Halide::Runtime::Buffer method */
427 // @{
428 HALIDE_BUFFER_FORWARD(raw_buffer)
442 HALIDE_BUFFER_FORWARD_CONST(number_of_elements)
443 HALIDE_BUFFER_FORWARD_CONST(size_in_bytes)
450 HALIDE_BUFFER_FORWARD_INITIALIZER_LIST(crop, std::vector<std::pair<int, int>>)
456 HALIDE_BUFFER_FORWARD(set_min)
457 HALIDE_BUFFER_FORWARD(translate)
458 HALIDE_BUFFER_FORWARD_INITIALIZER_LIST(translate, std::vector<int>)
460 HALIDE_BUFFER_FORWARD(transpose)
462 HALIDE_BUFFER_FORWARD(add_dimension)
463 HALIDE_BUFFER_FORWARD(copy_to_host)
465 HALIDE_BUFFER_FORWARD_CONST(has_device_allocation)
467 HALIDE_BUFFER_FORWARD_CONST(device_dirty)
468 HALIDE_BUFFER_FORWARD(set_host_dirty)
469 HALIDE_BUFFER_FORWARD(set_device_dirty)
470 HALIDE_BUFFER_FORWARD(device_sync)
473 HALIDE_BUFFER_FORWARD(device_detach_native)
474 HALIDE_BUFFER_FORWARD(allocate)
475 HALIDE_BUFFER_FORWARD(deallocate)
476 HALIDE_BUFFER_FORWARD(device_deallocate)
477 HALIDE_BUFFER_FORWARD(device_free)
479
480#undef HALIDE_BUFFER_FORWARD
481#undef HALIDE_BUFFER_FORWARD_CONST
482
483 template<typename Fn, typename... Args>
485 get()->for_each_value(std::forward<Fn>(f), (*std::forward<Args>(other_buffers).get())...);
486 return *this;
487 }
488
489 template<typename Fn, typename... Args>
491 get()->for_each_value(std::forward<Fn>(f), (*std::forward<Args>(other_buffers).get())...);
492 return *this;
493 }
494
495 template<typename Fn>
497 get()->for_each_element(std::forward<Fn>(f));
498 return *this;
499 }
500
501 template<typename Fn>
503 get()->for_each_element(std::forward<Fn>(f));
504 return *this;
505 }
506
507 template<typename FnOrValue>
509 get()->fill(std::forward<FnOrValue>(f));
510 return *this;
511 }
512
514
518
520
521 static constexpr int static_dimensions() {
523 }
524
525 template<typename T2, int D2>
529
530 // Note that since Runtime::Buffer stores halide_type_t rather than Halide::Type,
531 // there is no handle-specific type information, so all handle types are
532 // considered equivalent to void* here. (This only matters if you are making
533 // a Buffer-of-handles, which is not really a real use case...)
534 Type type() const {
535 return contents->buf.type();
536 }
537
538 template<typename T2, int D2 = Dims>
540 return Buffer<T2, D2>(*this);
541 }
542
544 return Buffer<T, Dims>(std::move(contents->buf.as<T, Dims>().copy()));
545 }
546
547 template<typename T2, int D2>
549 contents->buf.copy_from(*other.get());
550 }
551
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)...);
555 }
556
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)...);
560 }
561
562 auto operator()(const int *pos) -> decltype(std::declval<Runtime::Buffer<T, Dims>>()(pos)) {
563 return (*get())(pos);
564 }
565
566 auto operator()(const int *pos) const -> decltype(std::declval<const Runtime::Buffer<T, Dims>>()(pos)) {
567 return (*get())(pos);
568 }
569
570 auto operator()() -> decltype(std::declval<Runtime::Buffer<T, Dims>>()()) {
571 return (*get())();
572 }
573
574 auto operator()() const -> decltype(std::declval<const Runtime::Buffer<T, Dims>>()()) {
575 return (*get())();
576 }
577 // @}
578
579 /** Make an Expr that loads from this concrete buffer at a computed
580 * coordinate. Returned Expr is const so that it's not possible to
581 * accidentally treat a buffer like a Func and try to assign an Expr to a
582 * given symbolic coordinate. */
583 // @{
584 template<typename... Args>
585 const Expr operator()(const Expr &first, const Args &...rest) const { // NOLINT(readability-const-return-type)
586 std::vector<Expr> args = {first, rest...};
587 return (*this)(args);
588 }
589
590 template<typename... Args>
591 const Expr operator()(const std::vector<Expr> &args) const { // NOLINT(readability-const-return-type)
592 return buffer_accessor(Buffer<>(*this), args);
593 }
594 // @}
595
596 /** Copy to the GPU, using the device API that is the default for the given Target. */
598 return copy_to_device(DeviceAPI::Default_GPU, t, context);
599 }
600
601 /** Copy to the GPU, using the given device API */
602 int copy_to_device(const DeviceAPI &d, const Target &t = get_jit_target_from_environment(), JITUserContext *context = nullptr) {
603 return contents->buf.copy_to_device(get_device_interface_for_device_api(d, t, "Buffer::copy_to_device"), context);
604 }
605
606 /** Allocate on the GPU, using the device API that is the default for the given Target. */
608 return device_malloc(DeviceAPI::Default_GPU, t, context);
609 }
610
611 /** Allocate storage on the GPU, using the given device API */
612 int device_malloc(const DeviceAPI &d, const Target &t = get_jit_target_from_environment(), JITUserContext *context = nullptr) {
613 return contents->buf.device_malloc(get_device_interface_for_device_api(d, t, "Buffer::device_malloc"), context);
614 }
615
616 /** Wrap a native handle, using the given device API.
617 * It is a bad idea to pass DeviceAPI::Default_GPU to this routine
618 * as the handle argument must match the API that the default
619 * resolves to and it is clearer and more reliable to pass the
620 * resolved DeviceAPI explicitly. */
621 int device_wrap_native(const DeviceAPI &d, uint64_t handle, const Target &t = get_jit_target_from_environment(), JITUserContext *context = nullptr) {
622 return contents->buf.device_wrap_native(get_device_interface_for_device_api(d, t, "Buffer::device_wrap_native"), handle, context);
623 }
624};
625
626} // namespace Halide
627
628#endif
#define HALIDE_BUFFER_FORWARD(method)
Definition Buffer.h:403
#define HALIDE_BUFFER_FORWARD_INITIALIZER_LIST(method,...)
Definition Buffer.h:420
#define HALIDE_BUFFER_FORWARD_CONST(method)
Definition Buffer.h:396
Methods for managing device allocations when jitting.
#define user_assert(c)
Definition Error.h:233
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.
Definition Buffer.h:122
auto operator()() -> decltype(std::declval< Runtime::Buffer< T, Dims > >()())
Definition Buffer.h:570
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="")
Definition Buffer.h:352
static constexpr int static_dimensions()
Definition Buffer.h:521
Type type() const
Definition Buffer.h:534
Buffer(Type t, const std::vector< int > &sizes, const std::string &name="")
Definition Buffer.h:229
Buffer(Runtime::Buffer< T, D2 > &&buf, const std::string &name="")
Construct a Buffer that captures and owns an rvalue Runtime::Buffer.
Definition Buffer.h:194
Buffer< T, Dims > & for_each_value(Fn &&f, Args... other_buffers)
Does the same thing as the equivalent Halide::Runtime::Buffer method.
Definition Buffer.h:484
void set_name(const std::string &n)
Buffers are optionally named.
Definition Buffer.h:362
static constexpr bool has_static_dimensions
Definition Buffer.h:519
const Buffer< T, Dims > & for_each_element(Fn &&f) const
Definition Buffer.h:502
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.
Definition Buffer.h:612
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...
Definition Buffer.h:211
const Buffer< T, Dims > & for_each_value(Fn &&f, Args... other_buffers) const
Definition Buffer.h:490
static Buffer< T, Dims > make_interleaved(int width, int height, int channels, const std::string &name="")
Definition Buffer.h:325
bool defined() const
Check if this Buffer refers to an existing Buffer.
Definition Buffer.h:380
const Expr operator()(const std::vector< Expr > &args) const
Definition Buffer.h:591
bool same_as(const Buffer< T2, D2 > &other) const
Check if two Buffer objects point to the same underlying Buffer.
Definition Buffer.h:373
auto operator()(const int *pos) -> decltype(std::declval< Runtime::Buffer< T, Dims > >()(pos))
Definition Buffer.h:562
Buffer< T, Dims > & for_each_element(Fn &&f)
Definition Buffer.h:496
static Buffer make_interleaved(Type t, int width, int height, int channels, const std::string &name="")
Definition Buffer.h:329
static Buffer make_scalar(Type t, const std::string &name="")
Definition Buffer.h:317
static Buffer< T, Dims > make_scalar(T *data, const std::string &name="")
Definition Buffer.h:321
Buffer(Type t, Internal::add_const_if_T_is_const< T, void > *data, int d, const halide_dimension_t *shape, const std::string &name="")
Definition Buffer.h:298
auto operator()(int first, Args &&...args) -> decltype(std::declval< Runtime::Buffer< T, Dims > >()(first, std::forward< Args >(args)...))
Definition Buffer.h:553
static constexpr int AnyDims
Definition Buffer.h:157
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.
Definition Buffer.h:602
static constexpr halide_type_t static_halide_type()
Definition Buffer.h:515
auto operator()(int first, Args &&...args) const -> decltype(std::declval< const Runtime::Buffer< T, Dims > >()(first, std::forward< Args >(args)...))
Definition Buffer.h:558
Buffer(Array(&vals)[N], const std::string &name="")
Definition Buffer.h:254
Buffer< T, Dims > copy() const
Definition Buffer.h:543
Buffer(const std::vector< int > &sizes, const std::vector< int > &storage_order, const std::string &name="")
Definition Buffer.h:247
Buffer< T, Dims > & fill(FnOrValue &&f)
Definition Buffer.h:508
const std::string & name() const
Definition Buffer.h:366
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="")
Definition Buffer.h:344
Runtime::Buffer< T, Dims > * get()
Get a pointer to the underlying Runtime::Buffer.
Definition Buffer.h:386
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.
Definition Buffer.h:621
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.
Definition Buffer.h:607
Buffer(Type t, Internal::add_const_if_T_is_const< T, void > *data, const std::vector< int > &sizes, const std::string &name="")
Definition Buffer.h:291
static constexpr bool has_static_halide_type
Definition Buffer.h:513
Buffer(T *data, const std::vector< int > &sizes, const std::string &name="")
Definition Buffer.h:285
Buffer(T *data, int first, Args &&...rest)
Definition Buffer.h:279
static Buffer< T, Dims > make_scalar(const std::string &name="")
Definition Buffer.h:313
Buffer(Type t, Internal::add_const_if_T_is_const< T, void > *data, int first, Args &&...rest)
Definition Buffer.h:261
Buffer(Buffer< T2, D2 > &&other) noexcept
Move construct from a Buffer of a different type.
Definition Buffer.h:187
Buffer & operator=(Buffer &&) noexcept=default
Trivial move assignment operator.
Buffer(int first, const Args &...rest)
Definition Buffer.h:224
Buffer(Type t, const std::vector< int > &sizes, const std::vector< int > &storage_order, const std::string &name="")
Definition Buffer.h:235
auto operator()(const int *pos) const -> decltype(std::declval< const Runtime::Buffer< T, Dims > >()(pos))
Definition Buffer.h:566
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.
Definition Buffer.h:597
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="")
Definition Buffer.h:338
Buffer< T2, D2 > as() const
Definition Buffer.h:539
Buffer(const std::vector< int > &sizes, const std::string &name="")
Definition Buffer.h:242
const Expr operator()(const Expr &first, const Args &...rest) const
Make an Expr that loads from this concrete buffer at a computed coordinate.
Definition Buffer.h:585
Buffer(const halide_buffer_t &buf, const std::string &name="")
Definition Buffer.h:217
Buffer(T *data, int d, const halide_dimension_t *shape, const std::string &name="")
Definition Buffer.h:306
Buffer(Type t, Internal::add_const_if_T_is_const< T, void > *data, const std::vector< int > &sizes, const std::string &name="")
Definition Buffer.h:270
static bool can_convert_from(const Buffer< T2, D2 > &other)
Definition Buffer.h:526
const Runtime::Buffer< T, Dims > * get() const
Definition Buffer.h:390
auto operator()() const -> decltype(std::declval< const Runtime::Buffer< T, Dims > >()())
Definition Buffer.h:574
void copy_from(const Buffer< T2, D2 > &other)
Definition Buffer.h:548
static Buffer< T, Dims > make_interleaved(T *data, int width, int height, int channels, const std::string &name="")
Definition Buffer.h:333
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 get_name_from_end_of_parameter_pack()
Definition Buffer.h:52
std::vector< int > get_shape_from_start_of_parameter_pack(Args &&...args)
Definition Buffer.h:76
std::conditional_t< std::is_const_v< T >, const T2, T2 > add_const_if_T_is_const
Definition Buffer.h:83
void buffer_type_name_non_const< void >(std::ostream &s)
Definition Buffer.h:94
void buffer_type_name_non_const(std::ostream &s)
Definition Buffer.h:89
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()
Definition Buffer.h:99
void get_shape_from_start_of_parameter_pack_helper(std::vector< int > &, const std::string &)
Definition Buffer.h:63
constexpr int AnyDims
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.
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.
Definition DeviceAPI.h:15
constexpr int AnyDims
Definition Buffer.h:11
Internal::ConstantInterval cast(Type t, const Internal::ConstantInterval &a)
Cast operators for ConstantIntervals.
unsigned __INT64_TYPE__ uint64_t
A fragment of Halide syntax.
Definition Expr.h:258
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.
Definition JITModule.h:136
A struct representing a target machine and os to generate code for.
Definition Target.h:19
Types in the halide type system.
Definition Type.h:283
The raw representation of an image passed around by generated Halide code.
A runtime tag for a type in the halide type system.