diff --git a/mace/core/device_context.cc b/mace/core/device_context.cc index 88a965fa2635da79dda3f3158b084c8ec8f41b11..4f5ff8b10e700be29a697a7cf92a583777222f7f 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 21d076730f25b3070c0f680de5b6370d860612f9..ea1e6590cfbe1afba0eb7d7df2cdfa57d168710c 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 7c1fb35b3ebac4df42d2aac451ccbbbf0b4de464..6e6af1e23697bc7411c32b1b5b2ba36cee07888e 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 c4efe8c3565229b371a99f59fc71345c041577bf..7d15449f87af39cbdf59adcd41828574a3927d1b 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 1c85273e372a1de1c7e6214c29fe58d8b0a3a063..112a94bff7dd3ee0c24e56aae1431b24b258ce50 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 64a2d5e34f5fa69b9d456782d4538adf6dca4edd..1d36461b219ce5b28e4efa7ce6f769613eb92634 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 97da0e45b3b8f5430e5a487eb74b26e68d3f0df5..18840b7107619adf94aca7ae739caa3358d33fd3 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 8480848944b2aba302dcc6007f92b26d5081085d..3d182a9e0def86d6ecf73cd5140751b0d1702d31 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 9928686c82b3162550d2e99ffd01d632a4b99259..ae5bbc78f58df7ebbcf24abe49118c03960afa18 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 db50a58eebfef013f4a6dee925a7ae87aa95bead..313e1afb551fbffe6bc4c798296d89dcc08171fe 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.