Halide 19.0.0
Halide compiler and libraries
Loading...
Searching...
No Matches
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
12extern "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// @{
26extern int halide_metal_initialize_kernels(void *user_context, void **state_ptr,
27 const char *src, int size);
28void halide_metal_finalize_kernels(void *user_context, void *state_ptr);
29
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. */
48extern 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 */
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 */
65
66/** Returns the offset associated with the Metal Buffer allocation via device_crop or device_slice. */
68
69struct halide_metal_device;
70struct halide_metal_command_queue;
71struct halide_metal_command_buffer;
72
73/** This prototype is exported as applications will typically need to
74 * replace it to get Halide filters to execute on the same device and
75 * command queue used for other purposes. The halide_metal_device is an
76 * id <MTLDevice> and halide_metal_command_queue is an id <MTLCommandQueue>.
77 * No reference counting is done by Halide on these objects. They must remain
78 * valid until all off the following are true:
79 * - A balancing halide_metal_release_context has occurred for each
80 * halide_metal_acquire_context which returned the device/queue
81 * - All Halide filters using the context information have completed
82 * - All halide_buffer_t objects on the device have had
83 * halide_device_free called or have been detached via
84 * halide_metal_detach_buffer.
85 * - halide_device_release has been called on the interface returned from
86 * halide_metal_device_interface(). (This releases the programs on the context.)
87 */
88extern int halide_metal_acquire_context(void *user_context, struct halide_metal_device **device_ret,
89 struct halide_metal_command_queue **queue_ret, bool create);
90
91/** This call balances each successful halide_metal_acquire_context call.
92 * If halide_metal_acquire_context is replaced, this routine must be replaced
93 * as well.
94 */
96
97/** This function is called as part of the callback when a Metal command buffer completes.
98 * The return value, if not halide_error_code_success, will be stashed in Metal runtime and returned
99 * to the next call into the runtime, and the error string will be saved as well.
100 * The error string will be freed by the caller. The return value must be a valid Halide error code.
101 * This is called from the Metal driver, and thus:
102 * - Any user_context must be preserved between the call to halide_metal_run and the corresponding callback
103 * - The function must be thread-safe
104 */
105extern int halide_metal_command_buffer_completion_handler(void *user_context, struct halide_metal_command_buffer *buffer,
106 char **returned_error_string);
107
108#ifdef __cplusplus
109} // End extern "C"
110#endif
111
112#endif // HALIDE_HALIDERUNTIMEMETAL_H
This file declares the routines used by Halide internally in its runtime.
int halide_metal_command_buffer_completion_handler(void *user_context, struct halide_metal_command_buffer *buffer, char **returned_error_string)
This function is called as part of the callback when a Metal command buffer completes.
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[])
void halide_metal_finalize_kernels(void *user_context, void *state_ptr)
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...
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.
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.
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.
const struct halide_device_interface_t * halide_metal_device_interface()
uintptr_t halide_metal_get_buffer(void *user_context, struct halide_buffer_t *buf)
Return the underlying MTLBuffer for a halide_buffer_t.
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.
int halide_metal_release_context(void *user_context)
This call balances each successful halide_metal_acquire_context call.
unsigned __INT64_TYPE__ uint64_t
__UINTPTR_TYPE__ uintptr_t
signed __INT8_TYPE__ int8_t
The raw representation of an image passed around by generated Halide code.
Each GPU API provides a halide_device_interface_t struct pointing to the code that manages device all...
A runtime tag for a type in the halide type system.
void * user_context