diff --git a/mace/core/mace.cc b/mace/core/mace.cc index 2444f66365682ac89567a0116250e759c280a7da..3e64e2b1a60c7c423d8e063f8f70caeda2232b77 100644 --- a/mace/core/mace.cc +++ b/mace/core/mace.cc @@ -355,7 +355,7 @@ void ConfigOpenCLRuntime(GPUPerfHint gpu_perf_hint, GPUPriorityHint gpu_priority_hint) { VLOG(1) << "Set GPU configurations, gpu_perf_hint: " << gpu_perf_hint << ", gpu_priority_hint: " << gpu_priority_hint; - OpenCLRuntime::CreateGlobal(gpu_perf_hint, gpu_priority_hint); + OpenCLRuntime::Configure(gpu_perf_hint, gpu_priority_hint); } void ConfigOmpThreadsAndAffinity(int omp_num_threads, diff --git a/mace/core/runtime/opencl/opencl_runtime.cc b/mace/core/runtime/opencl/opencl_runtime.cc index c5b6557bd550d9c2d9f88a130f76aeb7e725ff22..798409abf683c489104a96a80bf9938b906e3d4f 100644 --- a/mace/core/runtime/opencl/opencl_runtime.cc +++ b/mace/core/runtime/opencl/opencl_runtime.cc @@ -65,23 +65,19 @@ void OpenCLProfilingTimer::ClearTiming() { accumulated_micros_ = 0; } -std::unique_ptr OpenCLRuntime::runtime_instance_ = nullptr; +GPUPerfHint OpenCLRuntime::gpu_perf_hint_ = GPUPerfHint::PERF_DEFAULT; +GPUPriorityHint OpenCLRuntime::gpu_priority_hint_ = + GPUPriorityHint::PRIORITY_DEFAULT; OpenCLRuntime *OpenCLRuntime::Global() { - // FIXME: not thread safe - if (runtime_instance_ == nullptr) { - return CreateGlobal(GPUPerfHint::PERF_DEFAULT, - GPUPriorityHint::PRIORITY_DEFAULT); - } - return runtime_instance_.get(); + static OpenCLRuntime runtime(gpu_perf_hint_, gpu_priority_hint_); + return &runtime; } -OpenCLRuntime *OpenCLRuntime::CreateGlobal(GPUPerfHint gpu_perf_hint, - GPUPriorityHint gpu_priority_hint) { - runtime_instance_ = - std::unique_ptr(new OpenCLRuntime(gpu_perf_hint, - gpu_priority_hint)); - return runtime_instance_.get(); +void OpenCLRuntime::Configure(GPUPerfHint gpu_perf_hint, + GPUPriorityHint gpu_priority_hint) { + OpenCLRuntime::gpu_perf_hint_ = gpu_perf_hint; + OpenCLRuntime::gpu_priority_hint_ = gpu_priority_hint; } void GetAdrenoContextProperties(std::vector *properties, diff --git a/mace/core/runtime/opencl/opencl_runtime.h b/mace/core/runtime/opencl/opencl_runtime.h index 801ee4711dcf07441ece9dcca4bc3ce13c3ff65f..ce375b9aed7bafe47ce7cd97310a76e496008544 100644 --- a/mace/core/runtime/opencl/opencl_runtime.h +++ b/mace/core/runtime/opencl/opencl_runtime.h @@ -39,7 +39,7 @@ class OpenCLProfilingTimer : public Timer { class OpenCLRuntime { public: static OpenCLRuntime *Global(); - static OpenCLRuntime *CreateGlobal(GPUPerfHint, GPUPriorityHint); + static void Configure(GPUPerfHint, GPUPriorityHint); cl::Context &context(); cl::Device &device(); @@ -52,10 +52,10 @@ class OpenCLRuntime { cl::Kernel BuildKernel(const std::string &program_name, const std::string &kernel_name, const std::set &build_options); - ~OpenCLRuntime(); private: OpenCLRuntime(GPUPerfHint, GPUPriorityHint); + ~OpenCLRuntime(); OpenCLRuntime(const OpenCLRuntime &) = delete; OpenCLRuntime &operator=(const OpenCLRuntime &) = delete; @@ -74,7 +74,9 @@ class OpenCLRuntime { std::map built_program_map_; std::mutex program_build_mutex_; std::string kernel_path_; - static std::unique_ptr runtime_instance_; + + static GPUPerfHint gpu_perf_hint_; + static GPUPriorityHint gpu_priority_hint_; }; } // namespace mace