Halide
HalideRuntimeMetal.h
Go to the documentation of this file.
1 #ifndef HALIDE_HALIDERUNTIMEMETAL_H
2 #define HALIDE_HALIDERUNTIMEMETAL_H
3 
4 // Don't include HalideRuntime.h if the contents of it were already pasted into a generated header above this one
5 #ifndef HALIDE_HALIDERUNTIME_H
6 
7 #include "HalideRuntime.h"
8 
9 #endif
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 /** \file
16  * Routines specific to the Halide Metal runtime.
17  */
18 
19 #define HALIDE_RUNTIME_METAL
20 
22 
23 /** These are forward declared here to allow clients to override the
24  * Halide Metal runtime. Do not call them. */
25 // @{
26 extern int halide_metal_initialize_kernels(void *user_context, void **state_ptr,
27  const char *src, int size);
28 void halide_metal_finalize_kernels(void *user_context, void *state_ptr);
29 
30 extern int halide_metal_run(void *user_context,
31  void *state_ptr,
32  const char *entry_name,
33  int blocksX, int blocksY, int blocksZ,
34  int threadsX, int threadsY, int threadsZ,
35  int shared_mem_bytes,
36  struct halide_type_t arg_types[],
37  void *args[],
38  int8_t arg_is_buffer[]);
39 // @}
40 
41 /** Set the underlying MTLBuffer for a halide_buffer_t. This memory should be
42  * allocated using newBufferWithLength:options or similar and must
43  * have an extent large enough to cover that specified by the halide_buffer_t
44  * extent fields. The dev field of the halide_buffer_t must be NULL when this
45  * routine is called. This call can fail due to running out of memory
46  * or being passed an invalid buffer. The device and host dirty bits
47  * are left unmodified. */
48 extern int halide_metal_wrap_buffer(void *user_context, struct halide_buffer_t *buf, uint64_t buffer);
49 
50 /** Disconnect a halide_buffer_t from the memory it was previously
51  * wrapped around. Should only be called for a halide_buffer_t that
52  * halide_metal_wrap_buffer was previously called on. Frees any
53  * storage associated with the binding of the halide_buffer_t and the
54  * buffer, but does not free the MTLBuffer. The dev field of the
55  * halide_buffer_t will be NULL on return.
56  */
57 extern int halide_metal_detach_buffer(void *user_context, struct halide_buffer_t *buf);
58 
59 /** Return the underlying MTLBuffer for a halide_buffer_t. This buffer must be
60  * valid on an Metal device, or not have any associated device
61  * memory. If there is no device memory (dev field is NULL), this
62  * returns 0.
63  */
64 extern uintptr_t halide_metal_get_buffer(void *user_context, struct halide_buffer_t *buf);
65 
66 /** Returns the offset associated with the Metal Buffer allocation via device_crop or device_slice. */
67 extern uint64_t halide_metal_get_crop_offset(void *user_context, struct halide_buffer_t *buf);
68 
69 struct halide_metal_device;
70 struct halide_metal_command_queue;
71 
72 /** This prototype is exported as applications will typically need to
73  * replace it to get Halide filters to execute on the same device and
74  * command queue used for other purposes. The halide_metal_device is an
75  * id <MTLDevice> and halide_metal_command_queue is an id <MTLCommandQueue>.
76  * No reference counting is done by Halide on these objects. They must remain
77  * valid until all off the following are true:
78  * - A balancing halide_metal_release_context has occurred for each
79  * halide_metal_acquire_context which returned the device/queue
80  * - All Halide filters using the context information have completed
81  * - All halide_buffer_t objects on the device have had
82  * halide_device_free called or have been detached via
83  * halide_metal_detach_buffer.
84  * - halide_device_release has been called on the interface returned from
85  * halide_metal_device_interface(). (This releases the programs on the context.)
86  */
87 extern int halide_metal_acquire_context(void *user_context, struct halide_metal_device **device_ret,
88  struct halide_metal_command_queue **queue_ret, bool create);
89 
90 /** This call balances each successful halide_metal_acquire_context call.
91  * If halide_metal_acquire_context is replaced, this routine must be replaced
92  * as well.
93  */
94 extern int halide_metal_release_context(void *user_context);
95 
96 #ifdef __cplusplus
97 } // End extern "C"
98 #endif
99 
100 #endif // HALIDE_HALIDERUNTIMEMETAL_H
halide_metal_run
int halide_metal_run(void *user_context, void *state_ptr, const char *entry_name, int blocksX, int blocksY, int blocksZ, int threadsX, int threadsY, int threadsZ, int shared_mem_bytes, struct halide_type_t arg_types[], void *args[], int8_t arg_is_buffer[])
int8_t
signed __INT8_TYPE__ int8_t
Definition: runtime_internal.h:28
halide_metal_release_context
int halide_metal_release_context(void *user_context)
This call balances each successful halide_metal_acquire_context call.
halide_type_t
A runtime tag for a type in the halide type system.
Definition: HalideRuntime.h:476
uintptr_t
__UINTPTR_TYPE__ uintptr_t
Definition: runtime_internal.h:73
uint64_t
unsigned __INT64_TYPE__ uint64_t
Definition: runtime_internal.h:23
halide_metal_device_interface
const struct halide_device_interface_t * halide_metal_device_interface()
halide_metal_detach_buffer
int halide_metal_detach_buffer(void *user_context, struct halide_buffer_t *buf)
Disconnect a halide_buffer_t from the memory it was previously wrapped around.
halide_metal_acquire_context
int halide_metal_acquire_context(void *user_context, struct halide_metal_device **device_ret, struct halide_metal_command_queue **queue_ret, bool create)
This prototype is exported as applications will typically need to replace it to get Halide filters to...
halide_metal_initialize_kernels
int halide_metal_initialize_kernels(void *user_context, void **state_ptr, const char *src, int size)
These are forward declared here to allow clients to override the Halide Metal runtime.
halide_metal_get_buffer
uintptr_t halide_metal_get_buffer(void *user_context, struct halide_buffer_t *buf)
Return the underlying MTLBuffer for a halide_buffer_t.
HalideRuntime.h
halide_metal_get_crop_offset
uint64_t halide_metal_get_crop_offset(void *user_context, struct halide_buffer_t *buf)
Returns the offset associated with the Metal Buffer allocation via device_crop or device_slice.
halide_buffer_t
The raw representation of an image passed around by generated Halide code.
Definition: HalideRuntime.h:1490
halide_device_interface_t
Each GPU API provides a halide_device_interface_t struct pointing to the code that manages device all...
Definition: HalideRuntime.h:770
halide_metal_finalize_kernels
void halide_metal_finalize_kernels(void *user_context, void *state_ptr)
halide_metal_wrap_buffer
int halide_metal_wrap_buffer(void *user_context, struct halide_buffer_t *buf, uint64_t buffer)
Set the underlying MTLBuffer for a halide_buffer_t.