From 03767a01f42a2220ea6758a7a94a2d4c9c85bcdf Mon Sep 17 00:00:00 2001 From: yejianwu Date: Tue, 23 Oct 2018 10:56:35 +0800 Subject: [PATCH] redefine GPUContext life cycle --- mace/core/device_context.cc | 12 ++++++------ mace/core/device_context.h | 12 ++++++------ mace/core/file_storage.cc | 9 ++++----- mace/core/file_storage.h | 4 ++-- mace/core/runtime/opencl/gpu_device.cc | 6 +++--- mace/core/runtime/opencl/gpu_device.h | 6 +++--- mace/core/runtime/opencl/opencl_runtime.cc | 8 ++++---- mace/core/runtime/opencl/opencl_runtime.h | 12 ++++++------ .../macelibrary/src/main/cpp/image_classify.cc | 1 - mace/public/mace.h | 6 ++---- 10 files changed, 36 insertions(+), 40 deletions(-) diff --git a/mace/core/device_context.cc b/mace/core/device_context.cc index 88a965fa..4f5ff8b1 100644 --- a/mace/core/device_context.cc +++ b/mace/core/device_context.cc @@ -58,16 +58,16 @@ GPUContext::GPUContext(const std::string &storage_path, GPUContext::~GPUContext() = default; -KVStorage *GPUContext::opencl_binary_storage() { - return opencl_binary_storage_.get(); +std::shared_ptr GPUContext::opencl_binary_storage() { + return opencl_binary_storage_; } -KVStorage *GPUContext::opencl_cache_storage() { - return opencl_cache_storage_.get(); +std::shared_ptr GPUContext::opencl_cache_storage() { + return opencl_cache_storage_; } -Tuner *GPUContext::opencl_tuner() { - return opencl_tuner_.get(); +std::shared_ptr> GPUContext::opencl_tuner() { + return opencl_tuner_; } } // namespace mace diff --git a/mace/core/device_context.h b/mace/core/device_context.h index 21d07673..ea1e6590 100644 --- a/mace/core/device_context.h +++ b/mace/core/device_context.h @@ -32,15 +32,15 @@ class GPUContext { const std::string &opencl_parameter_path = ""); ~GPUContext(); - KVStorage *opencl_binary_storage(); - KVStorage *opencl_cache_storage(); - Tuner *opencl_tuner(); + std::shared_ptr opencl_binary_storage(); + std::shared_ptr opencl_cache_storage(); + std::shared_ptr> opencl_tuner(); private: std::unique_ptr storage_factory_; - std::unique_ptr> opencl_tuner_; - std::unique_ptr opencl_binary_storage_; - std::unique_ptr opencl_cache_storage_; + std::shared_ptr> opencl_tuner_; + std::shared_ptr opencl_binary_storage_; + std::shared_ptr opencl_cache_storage_; }; } // namespace mace diff --git a/mace/core/file_storage.cc b/mace/core/file_storage.cc index 7c1fb35b..6e6af1e2 100644 --- a/mace/core/file_storage.cc +++ b/mace/core/file_storage.cc @@ -32,7 +32,7 @@ class FileStorageFactory::Impl { public: explicit Impl(const std::string &path); - std::unique_ptr CreateStorage(const std::string &name); + std::shared_ptr CreateStorage(const std::string &name); private: std::string path_; @@ -40,10 +40,9 @@ class FileStorageFactory::Impl { FileStorageFactory::Impl::Impl(const std::string &path): path_(path) {} -std::unique_ptr FileStorageFactory::Impl::CreateStorage( +std::shared_ptr FileStorageFactory::Impl::CreateStorage( const std::string &name) { - return std::move(std::unique_ptr( - new FileStorage(path_ + "/" + name))); + return std::shared_ptr(new FileStorage(path_ + "/" + name)); } FileStorageFactory::FileStorageFactory(const std::string &path): @@ -51,7 +50,7 @@ FileStorageFactory::FileStorageFactory(const std::string &path): FileStorageFactory::~FileStorageFactory() = default; -std::unique_ptr FileStorageFactory::CreateStorage( +std::shared_ptr FileStorageFactory::CreateStorage( const std::string &name) { return impl_->CreateStorage(name); } diff --git a/mace/core/file_storage.h b/mace/core/file_storage.h index c4efe8c3..7d15449f 100644 --- a/mace/core/file_storage.h +++ b/mace/core/file_storage.h @@ -41,7 +41,7 @@ class KVStorage { class KVStorageFactory { public: - virtual std::unique_ptr CreateStorage(const std::string &name) = 0; + virtual std::shared_ptr CreateStorage(const std::string &name) = 0; virtual ~KVStorageFactory() {} }; @@ -53,7 +53,7 @@ class FileStorageFactory : public KVStorageFactory { ~FileStorageFactory(); - std::unique_ptr CreateStorage(const std::string &name) override; + std::shared_ptr CreateStorage(const std::string &name) override; private: class Impl; diff --git a/mace/core/runtime/opencl/gpu_device.cc b/mace/core/runtime/opencl/gpu_device.cc index 1c85273e..112a94bf 100644 --- a/mace/core/runtime/opencl/gpu_device.cc +++ b/mace/core/runtime/opencl/gpu_device.cc @@ -18,11 +18,11 @@ namespace mace { -GPUDevice::GPUDevice(Tuner *tuner, - KVStorage *opencl_cache_storage, +GPUDevice::GPUDevice(std::shared_ptr> tuner, + std::shared_ptr opencl_cache_storage, const GPUPriorityHint priority, const GPUPerfHint perf, - KVStorage *opencl_binary_storage, + std::shared_ptr opencl_binary_storage, const int num_threads, CPUAffinityPolicy cpu_affinity_policy, bool use_gemmlowp) : diff --git a/mace/core/runtime/opencl/gpu_device.h b/mace/core/runtime/opencl/gpu_device.h index 64a2d5e3..1d36461b 100644 --- a/mace/core/runtime/opencl/gpu_device.h +++ b/mace/core/runtime/opencl/gpu_device.h @@ -25,11 +25,11 @@ namespace mace { class GPUDevice : public CPUDevice { public: - GPUDevice(Tuner *tuner, - KVStorage *opencl_cache_storage = nullptr, + GPUDevice(std::shared_ptr> tuner, + std::shared_ptr opencl_cache_storage = nullptr, const GPUPriorityHint priority = GPUPriorityHint::PRIORITY_LOW, const GPUPerfHint perf = GPUPerfHint::PERF_NORMAL, - KVStorage *opencl_binary_storage = nullptr, + std::shared_ptr opencl_binary_storage = nullptr, const int num_threads = -1, CPUAffinityPolicy cpu_affinity_policy = AFFINITY_NONE, bool use_gemmlowp = false); diff --git a/mace/core/runtime/opencl/opencl_runtime.cc b/mace/core/runtime/opencl/opencl_runtime.cc index 97da0e45..18840b71 100644 --- a/mace/core/runtime/opencl/opencl_runtime.cc +++ b/mace/core/runtime/opencl/opencl_runtime.cc @@ -273,11 +273,11 @@ void OpenCLProfilingTimer::ClearTiming() { } OpenCLRuntime::OpenCLRuntime( - KVStorage *cache_storage, + std::shared_ptr cache_storage, const GPUPriorityHint priority_hint, const GPUPerfHint perf_hint, - KVStorage *precompiled_binary_storage, - Tuner *tuner): + std::shared_ptr precompiled_binary_storage, + std::shared_ptr> tuner): cache_storage_(cache_storage), precompiled_binary_storage_(precompiled_binary_storage), tuner_(tuner), @@ -460,7 +460,7 @@ cl::Device &OpenCLRuntime::device() { return *device_; } cl::CommandQueue &OpenCLRuntime::command_queue() { return *command_queue_; } -Tuner *OpenCLRuntime::tuner() { return tuner_; } +Tuner *OpenCLRuntime::tuner() { return tuner_.get(); } uint64_t OpenCLRuntime::device_global_mem_cache_size() const { return device_gloabl_mem_cache_size_; diff --git a/mace/core/runtime/opencl/opencl_runtime.h b/mace/core/runtime/opencl/opencl_runtime.h index 84808489..3d182a9e 100644 --- a/mace/core/runtime/opencl/opencl_runtime.h +++ b/mace/core/runtime/opencl/opencl_runtime.h @@ -65,11 +65,11 @@ const std::string OpenCLErrorToString(cl_int error); class OpenCLRuntime { public: OpenCLRuntime( - KVStorage *cache_storage = nullptr, + std::shared_ptr cache_storage = nullptr, const GPUPriorityHint priority_hint = GPUPriorityHint::PRIORITY_NORMAL, const GPUPerfHint perf_hint = GPUPerfHint::PERF_NORMAL, - KVStorage *precompiled_binary_storage = nullptr, - Tuner *tuner = nullptr); + std::shared_ptr precompiled_binary_storage = nullptr, + std::shared_ptr> tuner = nullptr); ~OpenCLRuntime(); OpenCLRuntime(const OpenCLRuntime &) = delete; OpenCLRuntime &operator=(const OpenCLRuntime &) = delete; @@ -126,9 +126,9 @@ class OpenCLRuntime { OpenCLVersion ParseDeviceVersion(const std::string &device_version); private: - KVStorage *cache_storage_; - KVStorage *precompiled_binary_storage_; - Tuner *tuner_; + std::shared_ptr cache_storage_; + std::shared_ptr precompiled_binary_storage_; + std::shared_ptr> tuner_; bool is_opencl_avaliable_; bool is_profiling_enabled_; OpenCLVersion opencl_version_; diff --git a/mace/examples/android/macelibrary/src/main/cpp/image_classify.cc b/mace/examples/android/macelibrary/src/main/cpp/image_classify.cc index 9928686c..ae5bbc78 100755 --- a/mace/examples/android/macelibrary/src/main/cpp/image_classify.cc +++ b/mace/examples/android/macelibrary/src/main/cpp/image_classify.cc @@ -67,7 +67,6 @@ mace::DeviceType ParseDeviceType(const std::string &device) { } MaceContext& GetMaceContext() { - // stay for the app's life time, only initialize once static auto *mace_context = new MaceContext; return *mace_context; diff --git a/mace/public/mace.h b/mace/public/mace.h index db50a58e..313e1afb 100644 --- a/mace/public/mace.h +++ b/mace/public/mace.h @@ -99,10 +99,8 @@ enum MaceStatus { /// \brief GPU context contain the status used for GPU device. /// -/// The life cycle of GPUContext object is the same as MaceEngines use it. -/// Just use one GPUContext for all MaceEngines, which will speed up the -/// initialization procedure. There are some data in common between different -/// MaceEngines using GPU, use one GPUContext could avoid duplication. +/// There are some data in common between different MaceEngines using GPU, +/// use one GPUContext could avoid duplication. class GPUContext; /// \brief GPUContext builder. -- GitLab