|
14 | 14 | namespace cl_adapter { |
15 | 15 | ur_result_t getPlatformVersion(cl_platform_id Plat, |
16 | 16 | oclv::OpenCLVersion &Version); |
| 17 | + |
| 18 | +// Older versions of GCC don't like "const" here |
| 19 | +#if defined(__GNUC__) && (__GNUC__ < 7 || (__GNU__C == 7 && __GNUC_MINOR__ < 2)) |
| 20 | +#define CONSTFIX constexpr |
| 21 | +#else |
| 22 | +#define CONSTFIX const |
| 23 | +#endif |
| 24 | + |
| 25 | +// Names of USM functions that are queried from OpenCL |
| 26 | +CONSTFIX char HostMemAllocName[] = "clHostMemAllocINTEL"; |
| 27 | +CONSTFIX char DeviceMemAllocName[] = "clDeviceMemAllocINTEL"; |
| 28 | +CONSTFIX char SharedMemAllocName[] = "clSharedMemAllocINTEL"; |
| 29 | +CONSTFIX char MemBlockingFreeName[] = "clMemBlockingFreeINTEL"; |
| 30 | +CONSTFIX char CreateBufferWithPropertiesName[] = |
| 31 | + "clCreateBufferWithPropertiesINTEL"; |
| 32 | +CONSTFIX char SetKernelArgMemPointerName[] = "clSetKernelArgMemPointerINTEL"; |
| 33 | +CONSTFIX char EnqueueMemFillName[] = "clEnqueueMemFillINTEL"; |
| 34 | +CONSTFIX char EnqueueMemcpyName[] = "clEnqueueMemcpyINTEL"; |
| 35 | +CONSTFIX char GetMemAllocInfoName[] = "clGetMemAllocInfoINTEL"; |
| 36 | +CONSTFIX char SetProgramSpecializationConstantName[] = |
| 37 | + "clSetProgramSpecializationConstant"; |
| 38 | +CONSTFIX char GetDeviceFunctionPointerName[] = |
| 39 | + "clGetDeviceFunctionPointerINTEL"; |
| 40 | +CONSTFIX char EnqueueWriteGlobalVariableName[] = |
| 41 | + "clEnqueueWriteGlobalVariableINTEL"; |
| 42 | +CONSTFIX char EnqueueReadGlobalVariableName[] = |
| 43 | + "clEnqueueReadGlobalVariableINTEL"; |
| 44 | +// Names of host pipe functions queried from OpenCL |
| 45 | +CONSTFIX char EnqueueReadHostPipeName[] = "clEnqueueReadHostPipeINTEL"; |
| 46 | +CONSTFIX char EnqueueWriteHostPipeName[] = "clEnqueueWriteHostPipeINTEL"; |
| 47 | +// Names of command buffer functions queried from OpenCL |
| 48 | +CONSTFIX char CreateCommandBufferName[] = "clCreateCommandBufferKHR"; |
| 49 | +CONSTFIX char RetainCommandBufferName[] = "clRetainCommandBufferKHR"; |
| 50 | +CONSTFIX char ReleaseCommandBufferName[] = "clReleaseCommandBufferKHR"; |
| 51 | +CONSTFIX char FinalizeCommandBufferName[] = "clFinalizeCommandBufferKHR"; |
| 52 | +CONSTFIX char CommandNRRangeKernelName[] = "clCommandNDRangeKernelKHR"; |
| 53 | +CONSTFIX char CommandCopyBufferName[] = "clCommandCopyBufferKHR"; |
| 54 | +CONSTFIX char CommandCopyBufferRectName[] = "clCommandCopyBufferRectKHR"; |
| 55 | +CONSTFIX char CommandFillBufferName[] = "clCommandFillBufferKHR"; |
| 56 | +CONSTFIX char EnqueueCommandBufferName[] = "clEnqueueCommandBufferKHR"; |
| 57 | + |
| 58 | +#undef CONSTFIX |
| 59 | + |
| 60 | +using clGetDeviceFunctionPointer_fn = CL_API_ENTRY |
| 61 | +cl_int(CL_API_CALL *)(cl_device_id device, cl_program program, |
| 62 | + const char *FuncName, cl_ulong *ret_ptr); |
| 63 | + |
| 64 | +using clEnqueueWriteGlobalVariable_fn = CL_API_ENTRY |
| 65 | +cl_int(CL_API_CALL *)(cl_command_queue, cl_program, const char *, cl_bool, |
| 66 | + size_t, size_t, const void *, cl_uint, const cl_event *, |
| 67 | + cl_event *); |
| 68 | + |
| 69 | +using clEnqueueReadGlobalVariable_fn = CL_API_ENTRY |
| 70 | +cl_int(CL_API_CALL *)(cl_command_queue, cl_program, const char *, cl_bool, |
| 71 | + size_t, size_t, void *, cl_uint, const cl_event *, |
| 72 | + cl_event *); |
| 73 | + |
| 74 | +using clSetProgramSpecializationConstant_fn = CL_API_ENTRY |
| 75 | +cl_int(CL_API_CALL *)(cl_program program, cl_uint spec_id, size_t spec_size, |
| 76 | + const void *spec_value); |
| 77 | + |
| 78 | +using clEnqueueReadHostPipeINTEL_fn = CL_API_ENTRY |
| 79 | +cl_int(CL_API_CALL *)(cl_command_queue queue, cl_program program, |
| 80 | + const char *pipe_symbol, cl_bool blocking, void *ptr, |
| 81 | + size_t size, cl_uint num_events_in_waitlist, |
| 82 | + const cl_event *events_waitlist, cl_event *event); |
| 83 | + |
| 84 | +using clEnqueueWriteHostPipeINTEL_fn = CL_API_ENTRY |
| 85 | +cl_int(CL_API_CALL *)(cl_command_queue queue, cl_program program, |
| 86 | + const char *pipe_symbol, cl_bool blocking, |
| 87 | + const void *ptr, size_t size, |
| 88 | + cl_uint num_events_in_waitlist, |
| 89 | + const cl_event *events_waitlist, cl_event *event); |
| 90 | + |
| 91 | +using clCreateCommandBufferKHR_fn = CL_API_ENTRY cl_command_buffer_khr( |
| 92 | + CL_API_CALL *)(cl_uint num_queues, const cl_command_queue *queues, |
| 93 | + const cl_command_buffer_properties_khr *properties, |
| 94 | + cl_int *errcode_ret); |
| 95 | + |
| 96 | +using clRetainCommandBufferKHR_fn = CL_API_ENTRY |
| 97 | +cl_int(CL_API_CALL *)(cl_command_buffer_khr command_buffer); |
| 98 | + |
| 99 | +using clReleaseCommandBufferKHR_fn = CL_API_ENTRY |
| 100 | +cl_int(CL_API_CALL *)(cl_command_buffer_khr command_buffer); |
| 101 | + |
| 102 | +using clFinalizeCommandBufferKHR_fn = CL_API_ENTRY |
| 103 | +cl_int(CL_API_CALL *)(cl_command_buffer_khr command_buffer); |
| 104 | + |
| 105 | +using clCommandNDRangeKernelKHR_fn = CL_API_ENTRY cl_int(CL_API_CALL *)( |
| 106 | + cl_command_buffer_khr command_buffer, cl_command_queue command_queue, |
| 107 | + const cl_ndrange_kernel_command_properties_khr *properties, |
| 108 | + cl_kernel kernel, cl_uint work_dim, const size_t *global_work_offset, |
| 109 | + const size_t *global_work_size, const size_t *local_work_size, |
| 110 | + cl_uint num_sync_points_in_wait_list, |
| 111 | + const cl_sync_point_khr *sync_point_wait_list, |
| 112 | + cl_sync_point_khr *sync_point, cl_mutable_command_khr *mutable_handle); |
| 113 | + |
| 114 | +using clCommandCopyBufferKHR_fn = CL_API_ENTRY cl_int(CL_API_CALL *)( |
| 115 | + cl_command_buffer_khr command_buffer, cl_command_queue command_queue, |
| 116 | + cl_mem src_buffer, cl_mem dst_buffer, size_t src_offset, size_t dst_offset, |
| 117 | + size_t size, cl_uint num_sync_points_in_wait_list, |
| 118 | + const cl_sync_point_khr *sync_point_wait_list, |
| 119 | + cl_sync_point_khr *sync_point, cl_mutable_command_khr *mutable_handle); |
| 120 | + |
| 121 | +using clCommandCopyBufferRectKHR_fn = CL_API_ENTRY cl_int(CL_API_CALL *)( |
| 122 | + cl_command_buffer_khr command_buffer, cl_command_queue command_queue, |
| 123 | + cl_mem src_buffer, cl_mem dst_buffer, const size_t *src_origin, |
| 124 | + const size_t *dst_origin, const size_t *region, size_t src_row_pitch, |
| 125 | + size_t src_slice_pitch, size_t dst_row_pitch, size_t dst_slice_pitch, |
| 126 | + cl_uint num_sync_points_in_wait_list, |
| 127 | + const cl_sync_point_khr *sync_point_wait_list, |
| 128 | + cl_sync_point_khr *sync_point, cl_mutable_command_khr *mutable_handle); |
| 129 | + |
| 130 | +using clCommandFillBufferKHR_fn = CL_API_ENTRY cl_int(CL_API_CALL *)( |
| 131 | + cl_command_buffer_khr command_buffer, cl_command_queue command_queue, |
| 132 | + cl_mem buffer, const void *pattern, size_t pattern_size, size_t offset, |
| 133 | + size_t size, cl_uint num_sync_points_in_wait_list, |
| 134 | + const cl_sync_point_khr *sync_point_wait_list, |
| 135 | + cl_sync_point_khr *sync_point, cl_mutable_command_khr *mutable_handle); |
| 136 | + |
| 137 | +using clEnqueueCommandBufferKHR_fn = CL_API_ENTRY |
| 138 | +cl_int(CL_API_CALL *)(cl_uint num_queues, cl_command_queue *queues, |
| 139 | + cl_command_buffer_khr command_buffer, |
| 140 | + cl_uint num_events_in_wait_list, |
| 141 | + const cl_event *event_wait_list, cl_event *event); |
| 142 | + |
| 143 | +struct ExtFuncPtrT { |
| 144 | + clHostMemAllocINTEL_fn clHostMemAllocINTELCache; |
| 145 | + clDeviceMemAllocINTEL_fn clDeviceMemAllocINTELCache; |
| 146 | + clSharedMemAllocINTEL_fn clSharedMemAllocINTELCache; |
| 147 | + clGetDeviceFunctionPointer_fn clGetDeviceFunctionPointerCache; |
| 148 | + clCreateBufferWithPropertiesINTEL_fn |
| 149 | + clCreateBufferWithPropertiesINTELCache; |
| 150 | + clMemBlockingFreeINTEL_fn clMemBlockingFreeINTELCache; |
| 151 | + clSetKernelArgMemPointerINTEL_fn |
| 152 | + clSetKernelArgMemPointerINTELCache; |
| 153 | + clEnqueueMemFillINTEL_fn clEnqueueMemFillINTELCache; |
| 154 | + clEnqueueMemcpyINTEL_fn clEnqueueMemcpyINTELCache; |
| 155 | + clGetMemAllocInfoINTEL_fn clGetMemAllocInfoINTELCache; |
| 156 | + clEnqueueWriteGlobalVariable_fn |
| 157 | + clEnqueueWriteGlobalVariableCache; |
| 158 | + clEnqueueReadGlobalVariable_fn clEnqueueReadGlobalVariableCache; |
| 159 | + clEnqueueReadHostPipeINTEL_fn clEnqueueReadHostPipeINTELCache; |
| 160 | + clEnqueueWriteHostPipeINTEL_fn clEnqueueWriteHostPipeINTELCache; |
| 161 | + clSetProgramSpecializationConstant_fn |
| 162 | + clSetProgramSpecializationConstantCache; |
| 163 | + clCreateCommandBufferKHR_fn clCreateCommandBufferKHRCache; |
| 164 | + clRetainCommandBufferKHR_fn clRetainCommandBufferKHRCache; |
| 165 | + clReleaseCommandBufferKHR_fn clReleaseCommandBufferKHRCache; |
| 166 | + clFinalizeCommandBufferKHR_fn clFinalizeCommandBufferKHRCache; |
| 167 | + clCommandNDRangeKernelKHR_fn clCommandNDRangeKernelKHRCache; |
| 168 | + clCommandCopyBufferKHR_fn clCommandCopyBufferKHRCache; |
| 169 | + clCommandCopyBufferRectKHR_fn clCommandCopyBufferRectKHRCache; |
| 170 | + clCommandFillBufferKHR_fn clCommandFillBufferKHRCache; |
| 171 | + clEnqueueCommandBufferKHR_fn clEnqueueCommandBufferKHRCache; |
| 172 | +}; |
17 | 173 | } |
| 174 | + |
| 175 | +struct ur_platform_handle_t_ { |
| 176 | + using native_type = cl_platform_id; |
| 177 | + native_type Platform; |
| 178 | + std::unique_ptr<cl_adapter::ExtFuncPtrT> ExtFuncPtr; |
| 179 | + |
| 180 | + ur_platform_handle_t_(native_type Plat):Platform(Plat) { |
| 181 | + std::make_unique<cl_adapter::ExtFuncPtrT>(); |
| 182 | + } |
| 183 | + |
| 184 | + ~ur_platform_handle_t_() { |
| 185 | + ExtFuncPtr.reset(); |
| 186 | + } |
| 187 | + |
| 188 | + ur_result_t getPlatformVersion(oclv::OpenCLVersion &Version); |
| 189 | + |
| 190 | + template <typename T> |
| 191 | + ur_result_t getExtFunc(T CachedExtFunc, const char *FuncName, T *Fptr) { |
| 192 | + if (!CachedExtFunc) { |
| 193 | + // TODO: check that the function is available |
| 194 | + CachedExtFunc = reinterpret_cast<T>( |
| 195 | + clGetExtensionFunctionAddressForPlatform(Platform, FuncName)); |
| 196 | + if (!CachedExtFunc) { |
| 197 | + return UR_RESULT_ERROR_INVALID_VALUE; |
| 198 | + } |
| 199 | + } |
| 200 | + *Fptr = CachedExtFunc; |
| 201 | + return UR_RESULT_SUCCESS; |
| 202 | + } |
| 203 | + |
| 204 | + native_type get() { return Platform; } |
| 205 | +}; |
0 commit comments