Halide
HalideRuntimeOpenCL.h
Go to the documentation of this file.
1 #ifndef HALIDE_HALIDERUNTIMEOPENCL_H
2 #define HALIDE_HALIDERUNTIMEOPENCL_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 OpenCL runtime.
17  */
18 
19 #define HALIDE_RUNTIME_OPENCL
20 
23 
24 /** These are forward declared here to allow clients to override the
25  * Halide OpenCL runtime. Do not call them. */
26 // @{
27 extern int halide_opencl_initialize_kernels(void *user_context, void **state_ptr,
28  const char *src, int size);
29 extern int halide_opencl_run(void *user_context,
30  void *state_ptr,
31  const char *entry_name,
32  int blocksX, int blocksY, int blocksZ,
33  int threadsX, int threadsY, int threadsZ,
34  int shared_mem_bytes,
35  size_t arg_sizes[],
36  void *args[],
37  int8_t arg_is_buffer[]);
38 extern void halide_opencl_finalize_kernels(void *user_context, void *state_ptr);
39 // @}
40 
41 /** Set the platform name for OpenCL to use (e.g. "Intel" or
42  * "NVIDIA"). The argument is copied internally. The opencl runtime
43  * will select a platform that includes this as a substring. If never
44  * called, Halide uses the environment variable HL_OCL_PLATFORM_NAME,
45  * or defaults to the first available platform. */
46 extern void halide_opencl_set_platform_name(const char *n);
47 
48 /** Halide calls this to get the desired OpenCL platform
49  * name. Implement this yourself to use a different platform per
50  * user_context. The default implementation returns the value set by
51  * halide_set_ocl_platform_name, or the value of the environment
52  * variable HL_OCL_PLATFORM_NAME. The output is valid until the next
53  * call to halide_set_ocl_platform_name. */
54 extern const char *halide_opencl_get_platform_name(void *user_context);
55 
56 /** Set the device type for OpenCL to use. The argument is copied
57  * internally. It must be "cpu", "gpu", or "acc". If never called,
58  * Halide uses the environment variable HL_OCL_DEVICE_TYPE. */
59 extern void halide_opencl_set_device_type(const char *n);
60 
61 /** Halide calls this to gets the desired OpenCL device
62  * type. Implement this yourself to use a different device type per
63  * user_context. The default implementation returns the value set by
64  * halide_set_ocl_device_type, or the environment variable
65  * HL_OCL_DEVICE_TYPE. The result is valid until the next call to
66  * halide_set_ocl_device_type. */
67 extern const char *halide_opencl_get_device_type(void *user_context);
68 
69 /** Set the additional build options for OpenCL to use. The argument
70  * is copied internally. If never called,
71  * Halide uses the environment variable HL_OCL_BUILD_OPTIONS. */
72 extern void halide_opencl_set_build_options(const char *n);
73 
74 /** Halide calls this to gets the additional build options for OpenCL to
75  * use. Implement this yourself to use a different build options per
76  * user_context. The default implementation returns the value set by
77  * halide_opencl_set_build_options, or the environment variable
78  * HL_OCL_BUILD_OPTIONS. The result is valid until the next call to
79  * halide_opencl_set_build_options. */
80 extern const char *halide_opencl_get_build_options(void *user_context);
81 
82 /** Set the underlying cl_mem for a halide_buffer_t. This memory should be
83  * allocated using clCreateBuffer or similar and must have an extent
84  * large enough to cover that specified by the halide_buffer_t extent
85  * fields. The dev field of the halide_buffer_t must be NULL when this
86  * routine is called. This call can fail due to running out of memory
87  * or being passed an invalid device pointer. The device and host
88  * dirty bits are left unmodified. */
89 extern int halide_opencl_wrap_cl_mem(void *user_context, struct halide_buffer_t *buf, uint64_t device_ptr);
90 
91 /** Same as halide_opencl_wrap_cl_mem but wraps a cl_mem created with
92  * clCreateImage
93  */
94 extern int halide_opencl_image_wrap_cl_mem(void *user_context, struct halide_buffer_t *buf, uint64_t device_ptr);
95 
96 /** Disconnect a halide_buffer_t from the memory it was previously
97  * wrapped around. Should only be called for a halide_buffer_t that
98  * halide_opencl_wrap_device_ptr was previously called on. Frees any
99  * storage associated with the binding of the halide_buffer_t and the
100  * device pointer, but does not free the cl_mem. The dev field of the
101  * halide_buffer_t will be NULL on return.
102  */
103 extern int halide_opencl_detach_cl_mem(void *user_context, struct halide_buffer_t *buf);
104 
105 /** Return the underlying cl_mem for a halide_buffer_t. This buffer must be
106  * valid on an OpenCL device, or not have any associated device
107  * memory. If there is no device memory (dev field is NULL), this
108  * returns 0.
109  */
110 extern uintptr_t halide_opencl_get_cl_mem(void *user_context, struct halide_buffer_t *buf);
111 
112 /** Returns the offset associated with the OpenCL memory allocation via device_crop or device_slice. */
113 extern uint64_t halide_opencl_get_crop_offset(void *user_context, halide_buffer_t *buf);
114 
115 #ifdef __cplusplus
116 } // End extern "C"
117 #endif
118 
119 #endif // HALIDE_HALIDERUNTIMEOPENCL_H
int8_t
signed __INT8_TYPE__ int8_t
Definition: runtime_internal.h:28
halide_opencl_get_crop_offset
uint64_t halide_opencl_get_crop_offset(void *user_context, halide_buffer_t *buf)
Returns the offset associated with the OpenCL memory allocation via device_crop or device_slice.
halide_opencl_get_device_type
const char * halide_opencl_get_device_type(void *user_context)
Halide calls this to gets the desired OpenCL device type.
halide_opencl_get_build_options
const char * halide_opencl_get_build_options(void *user_context)
Halide calls this to gets the additional build options for OpenCL to use.
halide_opencl_image_device_interface
const struct halide_device_interface_t * halide_opencl_image_device_interface()
uintptr_t
__UINTPTR_TYPE__ uintptr_t
Definition: runtime_internal.h:73
uint64_t
unsigned __INT64_TYPE__ uint64_t
Definition: runtime_internal.h:23
halide_opencl_finalize_kernels
void halide_opencl_finalize_kernels(void *user_context, void *state_ptr)
halide_opencl_set_platform_name
void halide_opencl_set_platform_name(const char *n)
Set the platform name for OpenCL to use (e.g.
halide_opencl_initialize_kernels
int halide_opencl_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 OpenCL runtime.
halide_opencl_device_interface
const struct halide_device_interface_t * halide_opencl_device_interface()
halide_opencl_image_wrap_cl_mem
int halide_opencl_image_wrap_cl_mem(void *user_context, struct halide_buffer_t *buf, uint64_t device_ptr)
Same as halide_opencl_wrap_cl_mem but wraps a cl_mem created with clCreateImage.
halide_opencl_get_platform_name
const char * halide_opencl_get_platform_name(void *user_context)
Halide calls this to get the desired OpenCL platform name.
halide_opencl_get_cl_mem
uintptr_t halide_opencl_get_cl_mem(void *user_context, struct halide_buffer_t *buf)
Return the underlying cl_mem for a halide_buffer_t.
halide_opencl_wrap_cl_mem
int halide_opencl_wrap_cl_mem(void *user_context, struct halide_buffer_t *buf, uint64_t device_ptr)
Set the underlying cl_mem for a halide_buffer_t.
halide_opencl_set_device_type
void halide_opencl_set_device_type(const char *n)
Set the device type for OpenCL to use.
HalideRuntime.h
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_opencl_run
int halide_opencl_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, size_t arg_sizes[], void *args[], int8_t arg_is_buffer[])
halide_opencl_detach_cl_mem
int halide_opencl_detach_cl_mem(void *user_context, struct halide_buffer_t *buf)
Disconnect a halide_buffer_t from the memory it was previously wrapped around.
halide_opencl_set_build_options
void halide_opencl_set_build_options(const char *n)
Set the additional build options for OpenCL to use.