diff --git a/mace/core/runtime/opencl/opencl_runtime.cc b/mace/core/runtime/opencl/opencl_runtime.cc index e31db894e0a8fd445df2f7a3011809b03c7fcfb0..4f95a9e7abd446ec8839b1998e13e5c7594dfd97 100644 --- a/mace/core/runtime/opencl/opencl_runtime.cc +++ b/mace/core/runtime/opencl/opencl_runtime.cc @@ -33,7 +33,7 @@ bool ReadSourceFile(const std::string &filename, std::string *content) { } // namespace bool OpenCLRuntime::enable_profiling_ = false; -cl::Event* OpenCLRuntime::profiling_ev_ = NULL; +std::unique_ptr OpenCLRuntime::profiling_ev_ = NULL; OpenCLRuntime *OpenCLRuntime::Get() { static std::once_flag init_once; @@ -82,23 +82,25 @@ OpenCLRuntime *OpenCLRuntime::Get() { // a context is like a "runtime link" to the device and platform; // i.e. communication is possible cl::Context context({gpu_device}); - cl::CommandQueue command_queue(context, gpu_device, - enable_profiling_ ? CL_QUEUE_PROFILING_ENABLE : 0); + cl_command_queue_properties properties = 0; + if (enable_profiling_) { + profiling_ev_.reset(new cl::Event()); + properties = CL_QUEUE_PROFILING_ENABLE; + } + cl::CommandQueue command_queue(context, gpu_device, properties); instance = new OpenCLRuntime(context, gpu_device, command_queue); + }); return instance; } void OpenCLRuntime::EnableProfiling() { - if (!enable_profiling_) { - enable_profiling_ = true; - profiling_ev_ = new cl::Event(); - } + enable_profiling_ = true; } cl::Event* OpenCLRuntime::GetDefaultEvent() { - return profiling_ev_; + return profiling_ev_.get(); } cl_ulong OpenCLRuntime::GetEventProfilingStartInfo() { @@ -119,10 +121,7 @@ OpenCLRuntime::OpenCLRuntime(cl::Context context, kernel_path_ = std::string(kernel_path == nullptr ? "" : kernel_path) + "/"; } -OpenCLRuntime::~OpenCLRuntime() { - if (profiling_ev_) - delete profiling_ev_; -} +OpenCLRuntime::~OpenCLRuntime() {} 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 88086998d0779d8c7688de28e77d908611ba36ba..fda5813aa539a77e76659bca175eafd3d7359628 100644 --- a/mace/core/runtime/opencl/opencl_runtime.h +++ b/mace/core/runtime/opencl/opencl_runtime.h @@ -6,6 +6,7 @@ #define MACE_CORE_RUNTIME_OPENCL_OPENCL_RUNTIME_H_ #include +#include #include #include @@ -49,7 +50,7 @@ class OpenCLRuntime { private: static bool enable_profiling_; - static cl::Event* profiling_ev_; + static std::unique_ptr profiling_ev_; cl::Context context_; cl::Device device_;