diff --git a/mace/core/runtime/opencl/opencl_runtime.cc b/mace/core/runtime/opencl/opencl_runtime.cc index 3e67ef52e49e8b698ac437ea0d81064a15daf04f..05e0825799b0b735ddafb23a30d8cfad1a68ec0f 100644 --- a/mace/core/runtime/opencl/opencl_runtime.cc +++ b/mace/core/runtime/opencl/opencl_runtime.cc @@ -308,8 +308,6 @@ OpenCLRuntime::OpenCLRuntime(): precompiled_binary_storage_(nullptr), cache_storage_(nullptr), is_profiling_enabled_(false) { - LoadOpenCLLibrary(); - std::vector all_platforms; cl::Platform::get(&all_platforms); if (all_platforms.size() == 0) { @@ -456,7 +454,6 @@ OpenCLRuntime::~OpenCLRuntime() { command_queue_.reset(); context_.reset(); device_.reset(); - UnloadOpenCLLibrary(); } cl::Context &OpenCLRuntime::context() { return *context_; } diff --git a/mace/core/runtime/opencl/opencl_runtime.h b/mace/core/runtime/opencl/opencl_runtime.h index 931df6c455a702f8f65de4eafc46af4a7c3a025e..01a255d20c9dd93e4505b627b7b22400699087d5 100644 --- a/mace/core/runtime/opencl/opencl_runtime.h +++ b/mace/core/runtime/opencl/opencl_runtime.h @@ -24,7 +24,6 @@ #include "mace/core/future.h" #include "mace/core/runtime/opencl/cl2_header.h" -#include "mace/core/runtime/opencl/opencl_wrapper.h" #include "mace/public/mace_runtime.h" #include "mace/utils/string_util.h" #include "mace/utils/timer.h" diff --git a/mace/core/runtime/opencl/opencl_wrapper.cc b/mace/core/runtime/opencl/opencl_wrapper.cc index 7a95b8a3c9960a1c7655c509c227793d0c9ba5b5..26a9fb7e8eef01accd08694cb770ba6ea2650437 100644 --- a/mace/core/runtime/opencl/opencl_wrapper.cc +++ b/mace/core/runtime/opencl/opencl_wrapper.cc @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "mace/core/runtime/opencl/opencl_wrapper.h" - #include #include #include @@ -26,11 +24,17 @@ */ namespace mace { -namespace { -class OpenCLLibraryImpl final { - public: +namespace runtime { +class OpenCLLibrary final { + private: + OpenCLLibrary(); + MACE_DISABLE_COPY_AND_ASSIGN(OpenCLLibrary); + bool Load(); - void Unload(); + void *LoadFromPath(const std::string &path); + + public: + static OpenCLLibrary *Get(); using clGetPlatformIDsFunc = cl_int (*)(cl_uint, cl_platform_id *, cl_uint *); using clGetPlatformInfoFunc = @@ -243,11 +247,19 @@ class OpenCLLibraryImpl final { #undef MACE_CL_DEFINE_FUNC_PTR private: - void *LoadFromPath(const std::string &path); void *handle_ = nullptr; }; -bool OpenCLLibraryImpl::Load() { +OpenCLLibrary *OpenCLLibrary::Get() { + static OpenCLLibrary library; + return &library; +} + +OpenCLLibrary::OpenCLLibrary() { + this->Load(); +} + +bool OpenCLLibrary::Load() { if (handle_ != nullptr) { return true; } @@ -281,25 +293,17 @@ bool OpenCLLibraryImpl::Load() { } if (handle_ == nullptr) { - LOG(ERROR) << "Failed to load OpenCL library, " - "please make sure there exist OpenCL library on your device, " + LOG(FATAL) << "Failed to load OpenCL library, " + "please make sure there exists OpenCL library on your device, " "and your APP have right to access the library."; return false; } + // Do not dlclose, leave it to system. return true; } -void OpenCLLibraryImpl::Unload() { - if (handle_ != nullptr) { - if (dlclose(handle_) != 0) { - LOG(ERROR) << "dlclose failed for OpenCL library"; - } - handle_ = nullptr; - } -} - -void *OpenCLLibraryImpl::LoadFromPath(const std::string &path) { +void *OpenCLLibrary::LoadFromPath(const std::string &path) { void *handle = dlopen(path.c_str(), RTLD_LAZY | RTLD_LOCAL); if (handle == nullptr) { @@ -371,22 +375,7 @@ void *OpenCLLibraryImpl::LoadFromPath(const std::string &path) { return handle; } -OpenCLLibraryImpl *openclLibraryImpl = nullptr; -} // namespace - -void LoadOpenCLLibrary() { - MACE_CHECK(openclLibraryImpl == nullptr); - openclLibraryImpl = new OpenCLLibraryImpl(); - MACE_CHECK(openclLibraryImpl->Load()); -} - -void UnloadOpenCLLibrary() { - MACE_CHECK_NOTNULL(openclLibraryImpl); - openclLibraryImpl->Unload(); - delete openclLibraryImpl; - openclLibraryImpl = nullptr; -} - +} // namespace runtime } // namespace mace // Platform APIs @@ -394,8 +383,7 @@ CL_API_ENTRY cl_int clGetPlatformIDs(cl_uint num_entries, cl_platform_id *platforms, cl_uint *num_platforms) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clGetPlatformIDs; + auto func = mace::runtime::OpenCLLibrary::Get()->clGetPlatformIDs; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clGetPlatformIDs"); return func(num_entries, platforms, num_platforms); @@ -407,8 +395,7 @@ CL_API_ENTRY cl_int clGetPlatformInfo(cl_platform_id platform, void *param_value, size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clGetPlatformInfo; + auto func = mace::runtime::OpenCLLibrary::Get()->clGetPlatformInfo; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clGetPlatformInfo"); return func(platform, param_name, param_value_size, param_value, @@ -422,8 +409,7 @@ CL_API_ENTRY cl_int clGetDeviceIDs(cl_platform_id platform, cl_device_id *devices, cl_uint *num_devices) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clGetDeviceIDs; + auto func = mace::runtime::OpenCLLibrary::Get()->clGetDeviceIDs; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clGetDeviceIDs"); return func(platform, device_type, num_entries, devices, num_devices); @@ -435,8 +421,7 @@ CL_API_ENTRY cl_int clGetDeviceInfo(cl_device_id device, void *param_value, size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clGetDeviceInfo; + auto func = mace::runtime::OpenCLLibrary::Get()->clGetDeviceInfo; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clGetDeviceInfo"); return func(device, param_name, param_value_size, param_value, @@ -445,8 +430,7 @@ CL_API_ENTRY cl_int clGetDeviceInfo(cl_device_id device, CL_API_ENTRY cl_int clRetainDevice(cl_device_id device) CL_API_SUFFIX__VERSION_1_2 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clRetainDevice; + auto func = mace::runtime::OpenCLLibrary::Get()->clRetainDevice; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clRetainDevice"); return func(device); @@ -454,8 +438,7 @@ CL_API_ENTRY cl_int clRetainDevice(cl_device_id device) CL_API_ENTRY cl_int clReleaseDevice(cl_device_id device) CL_API_SUFFIX__VERSION_1_2 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clReleaseDevice; + auto func = mace::runtime::OpenCLLibrary::Get()->clReleaseDevice; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clReleaseDevice"); return func(device); @@ -469,8 +452,7 @@ CL_API_ENTRY cl_context clCreateContext( void(CL_CALLBACK *pfn_notify)(const char *, const void *, size_t, void *), void *user_data, cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clCreateContext; + auto func = mace::runtime::OpenCLLibrary::Get()->clCreateContext; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clCreateContext"); return func(properties, num_devices, devices, pfn_notify, user_data, @@ -483,8 +465,7 @@ CL_API_ENTRY cl_context clCreateContextFromType( void(CL_CALLBACK *pfn_notify)(const char *, const void *, size_t, void *), void *user_data, cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clCreateContextFromType; + auto func = mace::runtime::OpenCLLibrary::Get()->clCreateContextFromType; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clCreateContextFromType"); return func(properties, device_type, pfn_notify, user_data, errcode_ret); @@ -492,8 +473,7 @@ CL_API_ENTRY cl_context clCreateContextFromType( CL_API_ENTRY cl_int clRetainContext(cl_context context) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clRetainContext; + auto func = mace::runtime::OpenCLLibrary::Get()->clRetainContext; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clRetainContext"); return func(context); @@ -501,8 +481,7 @@ CL_API_ENTRY cl_int clRetainContext(cl_context context) CL_API_ENTRY cl_int clReleaseContext(cl_context context) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clReleaseContext; + auto func = mace::runtime::OpenCLLibrary::Get()->clReleaseContext; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clReleaseContext"); return func(context); @@ -514,8 +493,7 @@ CL_API_ENTRY cl_int clGetContextInfo(cl_context context, void *param_value, size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clGetContextInfo; + auto func = mace::runtime::OpenCLLibrary::Get()->clGetContextInfo; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clGetContextInfo"); return func(context, param_name, param_value_size, param_value, @@ -529,8 +507,7 @@ CL_API_ENTRY cl_program clCreateProgramWithSource(cl_context context, const size_t *lengths, cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clCreateProgramWithSource; + auto func = mace::runtime::OpenCLLibrary::Get()->clCreateProgramWithSource; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clCreateProgramWithSource"); return func(context, count, strings, lengths, errcode_ret); @@ -544,8 +521,7 @@ clCreateProgramWithBinary(cl_context context, const unsigned char **binaries, cl_int *binary_status, cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clCreateProgramWithBinary; + auto func = mace::runtime::OpenCLLibrary::Get()->clCreateProgramWithBinary; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clCreateProgramWithBinary"); return func(context, num_devices, device_list, lengths, binaries, @@ -558,8 +534,7 @@ CL_API_ENTRY cl_int clGetProgramInfo(cl_program program, void *param_value, size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clGetProgramInfo; + auto func = mace::runtime::OpenCLLibrary::Get()->clGetProgramInfo; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clGetProgramInfo"); return func(program, param_name, param_value_size, param_value, @@ -573,8 +548,7 @@ CL_API_ENTRY cl_int clGetProgramBuildInfo(cl_program program, void *param_value, size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clGetProgramBuildInfo; + auto func = mace::runtime::OpenCLLibrary::Get()->clGetProgramBuildInfo; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clGetProgramBuildInfo"); return func(program, device, param_name, param_value_size, param_value, @@ -583,8 +557,7 @@ CL_API_ENTRY cl_int clGetProgramBuildInfo(cl_program program, CL_API_ENTRY cl_int clRetainProgram(cl_program program) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clRetainProgram; + auto func = mace::runtime::OpenCLLibrary::Get()->clRetainProgram; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clRetainProgram"); return func(program); @@ -592,8 +565,7 @@ CL_API_ENTRY cl_int clRetainProgram(cl_program program) CL_API_ENTRY cl_int clReleaseProgram(cl_program program) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clReleaseProgram; + auto func = mace::runtime::OpenCLLibrary::Get()->clReleaseProgram; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clReleaseProgram"); return func(program); @@ -606,8 +578,7 @@ CL_API_ENTRY cl_int clBuildProgram( const char *options, void(CL_CALLBACK *pfn_notify)(cl_program program, void *user_data), void *user_data) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clBuildProgram; + auto func = mace::runtime::OpenCLLibrary::Get()->clBuildProgram; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clBuildProgram"); return func(program, num_devices, device_list, options, pfn_notify, @@ -619,8 +590,7 @@ CL_API_ENTRY cl_kernel clCreateKernel(cl_program program, const char *kernel_name, cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clCreateKernel; + auto func = mace::runtime::OpenCLLibrary::Get()->clCreateKernel; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clCreateKernel"); return func(program, kernel_name, errcode_ret); @@ -628,8 +598,7 @@ CL_API_ENTRY cl_kernel clCreateKernel(cl_program program, CL_API_ENTRY cl_int clRetainKernel(cl_kernel kernel) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clRetainKernel; + auto func = mace::runtime::OpenCLLibrary::Get()->clRetainKernel; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clRetainKernel"); return func(kernel); @@ -637,8 +606,7 @@ CL_API_ENTRY cl_int clRetainKernel(cl_kernel kernel) CL_API_ENTRY cl_int clReleaseKernel(cl_kernel kernel) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clReleaseKernel; + auto func = mace::runtime::OpenCLLibrary::Get()->clReleaseKernel; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clReleaseKernel"); return func(kernel); @@ -649,8 +617,7 @@ CL_API_ENTRY cl_int clSetKernelArg(cl_kernel kernel, size_t arg_size, const void *arg_value) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clSetKernelArg; + auto func = mace::runtime::OpenCLLibrary::Get()->clSetKernelArg; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clSetKernelArg"); return func(kernel, arg_index, arg_size, arg_value); @@ -663,8 +630,7 @@ CL_API_ENTRY cl_mem clCreateBuffer(cl_context context, void *host_ptr, cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clCreateBuffer; + auto func = mace::runtime::OpenCLLibrary::Get()->clCreateBuffer; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clCreateBuffer"); return func(context, flags, size, host_ptr, errcode_ret); @@ -677,8 +643,7 @@ CL_API_ENTRY cl_mem clCreateImage(cl_context context, void *host_ptr, cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_2 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clCreateImage; + auto func = mace::runtime::OpenCLLibrary::Get()->clCreateImage; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clCreateImage"); return func(context, flags, image_format, image_desc, host_ptr, errcode_ret); @@ -686,8 +651,7 @@ CL_API_ENTRY cl_mem clCreateImage(cl_context context, CL_API_ENTRY cl_int clRetainMemObject(cl_mem memobj) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clRetainMemObject; + auto func = mace::runtime::OpenCLLibrary::Get()->clRetainMemObject; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clRetainMemObject"); return func(memobj); @@ -695,8 +659,7 @@ CL_API_ENTRY cl_int clRetainMemObject(cl_mem memobj) CL_API_ENTRY cl_int clReleaseMemObject(cl_mem memobj) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clReleaseMemObject; + auto func = mace::runtime::OpenCLLibrary::Get()->clReleaseMemObject; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clReleaseMemObject"); return func(memobj); @@ -708,8 +671,7 @@ CL_API_ENTRY cl_int clGetImageInfo(cl_mem image, void *param_value, size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clGetImageInfo; + auto func = mace::runtime::OpenCLLibrary::Get()->clGetImageInfo; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clGetImageInfo"); return func(image, param_name, param_value_size, param_value, @@ -722,8 +684,8 @@ CL_API_ENTRY cl_command_queue clCreateCommandQueueWithProperties( cl_device_id device, const cl_queue_properties *properties, cl_int *errcode_ret) CL_API_SUFFIX__VERSION_2_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clCreateCommandQueueWithProperties; + auto func = + mace::runtime::OpenCLLibrary::Get()->clCreateCommandQueueWithProperties; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clCreateCommandQueueWithProperties"); return func(context, device, properties, errcode_ret); @@ -731,8 +693,7 @@ CL_API_ENTRY cl_command_queue clCreateCommandQueueWithProperties( CL_API_ENTRY cl_int clRetainCommandQueue(cl_command_queue command_queue) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clRetainCommandQueue; + auto func = mace::runtime::OpenCLLibrary::Get()->clRetainCommandQueue; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clRetainCommandQueue"); return func(command_queue); @@ -740,8 +701,7 @@ CL_API_ENTRY cl_int clRetainCommandQueue(cl_command_queue command_queue) CL_API_ENTRY cl_int clReleaseCommandQueue(cl_command_queue command_queue) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clReleaseCommandQueue; + auto func = mace::runtime::OpenCLLibrary::Get()->clReleaseCommandQueue; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clReleaseCommandQueue"); return func(command_queue); @@ -758,8 +718,7 @@ CL_API_ENTRY cl_int clEnqueueReadBuffer(cl_command_queue command_queue, const cl_event *event_wait_list, cl_event *event) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clEnqueueReadBuffer; + auto func = mace::runtime::OpenCLLibrary::Get()->clEnqueueReadBuffer; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clEnqueueReadBuffer"); return func(command_queue, buffer, blocking_read, offset, size, ptr, @@ -776,8 +735,7 @@ CL_API_ENTRY cl_int clEnqueueWriteBuffer(cl_command_queue command_queue, const cl_event *event_wait_list, cl_event *event) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clEnqueueWriteBuffer; + auto func = mace::runtime::OpenCLLibrary::Get()->clEnqueueWriteBuffer; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clEnqueueWriteBuffer"); return func(command_queue, buffer, blocking_write, offset, size, ptr, @@ -795,8 +753,7 @@ CL_API_ENTRY void *clEnqueueMapBuffer(cl_command_queue command_queue, cl_event *event, cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clEnqueueMapBuffer; + auto func = mace::runtime::OpenCLLibrary::Get()->clEnqueueMapBuffer; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clEnqueueMapBuffer"); return func(command_queue, buffer, blocking_map, map_flags, offset, size, @@ -816,8 +773,7 @@ CL_API_ENTRY void *clEnqueueMapImage(cl_command_queue command_queue, cl_event *event, cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clEnqueueMapImage; + auto func = mace::runtime::OpenCLLibrary::Get()->clEnqueueMapImage; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clEnqueueMapImage"); return func(command_queue, image, blocking_map, map_flags, origin, region, @@ -832,8 +788,7 @@ CL_API_ENTRY cl_int clEnqueueUnmapMemObject(cl_command_queue command_queue, const cl_event *event_wait_list, cl_event *event) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clEnqueueUnmapMemObject; + auto func = mace::runtime::OpenCLLibrary::Get()->clEnqueueUnmapMemObject; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clEnqueueUnmapMemObject"); return func(command_queue, memobj, mapped_ptr, num_events_in_wait_list, @@ -847,8 +802,7 @@ CL_API_ENTRY cl_int clGetKernelWorkGroupInfo( size_t param_value_size, void *param_value, size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clGetKernelWorkGroupInfo; + auto func = mace::runtime::OpenCLLibrary::Get()->clGetKernelWorkGroupInfo; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clGetKernelWorkGroupInfo"); return func(kernel, device, param_name, param_value_size, param_value, @@ -865,8 +819,7 @@ CL_API_ENTRY cl_int clEnqueueNDRangeKernel(cl_command_queue command_queue, const cl_event *event_wait_list, cl_event *event) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clEnqueueNDRangeKernel; + auto func = mace::runtime::OpenCLLibrary::Get()->clEnqueueNDRangeKernel; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clEnqueueNDRangeKernel"); return func(command_queue, kernel, work_dim, global_work_offset, @@ -877,24 +830,21 @@ CL_API_ENTRY cl_int clEnqueueNDRangeKernel(cl_command_queue command_queue, // Event Object APIs CL_API_ENTRY cl_int clWaitForEvents( cl_uint num_events, const cl_event *event_list) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clWaitForEvents; + auto func = mace::runtime::OpenCLLibrary::Get()->clWaitForEvents; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clWaitForEvents"); return func(num_events, event_list); } CL_API_ENTRY cl_int clRetainEvent(cl_event event) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clRetainEvent; + auto func = mace::runtime::OpenCLLibrary::Get()->clRetainEvent; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clRetainEvent"); return func(event); } CL_API_ENTRY cl_int clReleaseEvent(cl_event event) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clReleaseEvent; + auto func = mace::runtime::OpenCLLibrary::Get()->clReleaseEvent; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clReleaseEvent"); return func(event); @@ -907,8 +857,7 @@ CL_API_ENTRY cl_int clGetEventInfo(cl_event event, void *param_value, size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clGetEventInfo; + auto func = mace::runtime::OpenCLLibrary::Get()->clGetEventInfo; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clGetEventInfo"); return func(event, param_name, param_value_size, param_value, @@ -922,8 +871,7 @@ CL_API_ENTRY cl_int clGetEventProfilingInfo(cl_event event, void *param_value, size_t *param_value_size_ret) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clGetEventProfilingInfo; + auto func = mace::runtime::OpenCLLibrary::Get()->clGetEventProfilingInfo; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clGetEventProfilingInfo"); return func(event, param_name, param_value_size, param_value, @@ -933,8 +881,7 @@ CL_API_ENTRY cl_int clGetEventProfilingInfo(cl_event event, // Flush and Finish APIs CL_API_ENTRY cl_int clFlush(cl_command_queue command_queue) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clFlush; + auto func = mace::runtime::OpenCLLibrary::Get()->clFlush; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clFlush"); return func(command_queue); @@ -942,8 +889,7 @@ CL_API_ENTRY cl_int clFlush(cl_command_queue command_queue) CL_API_ENTRY cl_int clFinish(cl_command_queue command_queue) CL_API_SUFFIX__VERSION_1_0 { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clFinish; + auto func = mace::runtime::OpenCLLibrary::Get()->clFinish; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clFinish"); return func(command_queue); @@ -959,8 +905,7 @@ CL_API_ENTRY /* CL_EXT_PREFIX__VERSION_1_1_DEPRECATED */ cl_mem clCreateImage2D( size_t image_row_pitch, void *host_ptr, cl_int *errcode_ret) /* CL_EXT_SUFFIX__VERSION_1_1_DEPRECATED */ { - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clCreateImage2D; + auto func = mace::runtime::OpenCLLibrary::Get()->clCreateImage2D; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clCreateImage2D"); return func(context, flags, image_format, image_width, image_height, @@ -974,8 +919,7 @@ clCreateCommandQueue(cl_context context, cl_command_queue_properties properties, cl_int *errcode_ret) /* CL_EXT_SUFFIX__VERSION_1_2_DEPRECATED */ { // NOLINT - MACE_CHECK_NOTNULL(mace::openclLibraryImpl); - auto func = mace::openclLibraryImpl->clCreateCommandQueue; + auto func = mace::runtime::OpenCLLibrary::Get()->clCreateCommandQueue; MACE_CHECK_NOTNULL(func); MACE_LATENCY_LOGGER(3, "clCreateCommandQueue"); return func(context, device, properties, errcode_ret); diff --git a/mace/core/runtime/opencl/opencl_wrapper.h b/mace/core/runtime/opencl/opencl_wrapper.h deleted file mode 100644 index 529bcea3c49e926e42add0809e49fd5e00621125..0000000000000000000000000000000000000000 --- a/mace/core/runtime/opencl/opencl_wrapper.h +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2018 Xiaomi, Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef MACE_CORE_RUNTIME_OPENCL_OPENCL_WRAPPER_H_ -#define MACE_CORE_RUNTIME_OPENCL_OPENCL_WRAPPER_H_ - -namespace mace { - -// These functions are not thread-safe. -void LoadOpenCLLibrary(); -void UnloadOpenCLLibrary(); - -} // namespace mace - -#endif // MACE_CORE_RUNTIME_OPENCL_OPENCL_WRAPPER_H_