From d9a58a5e95fae699c062130ffd4b6d9a6f8e7cf8 Mon Sep 17 00:00:00 2001 From: liuqi Date: Thu, 26 Jul 2018 20:40:18 +0800 Subject: [PATCH] Fix opencl cache update bug. --- mace/core/file_storage.cc | 11 +++++++++-- mace/core/file_storage.h | 1 + mace/core/runtime/opencl/opencl_runtime.cc | 13 ++++++------- mace/core/runtime/opencl/opencl_runtime.h | 1 - mace/public/mace_runtime.h | 1 + 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/mace/core/file_storage.cc b/mace/core/file_storage.cc index 2d2b941a..99af2c19 100644 --- a/mace/core/file_storage.cc +++ b/mace/core/file_storage.cc @@ -121,13 +121,20 @@ int FileStorage::Load() { return 0; } +void FileStorage::Clear() { + utils::WriteLock lock(&data_mutex_); + data_.clear(); + data_changed_ = true; +} + bool FileStorage::Insert(const std::string &key, const std::vector &value) { utils::WriteLock lock(&data_mutex_); auto res = data_.emplace(key, value); - if (res.second) { - data_changed_ = true; + if (!res.second) { + data_[key] = value; } + data_changed_ = true; return true; } diff --git a/mace/core/file_storage.h b/mace/core/file_storage.h index 799719f3..3b648c23 100644 --- a/mace/core/file_storage.h +++ b/mace/core/file_storage.h @@ -30,6 +30,7 @@ class FileStorage : public KVStorage { public: int Load() override; + void Clear() override; bool Insert(const std::string &key, const std::vector &value) override; const std::vector *Find(const std::string &key) override; diff --git a/mace/core/runtime/opencl/opencl_runtime.cc b/mace/core/runtime/opencl/opencl_runtime.cc index 05e08257..c5809ffd 100644 --- a/mace/core/runtime/opencl/opencl_runtime.cc +++ b/mace/core/runtime/opencl/opencl_runtime.cc @@ -393,6 +393,7 @@ OpenCLRuntime::OpenCLRuntime(): MACE_CHECK_CL_SUCCESS(err); extern std::shared_ptr kStorageFactory; + std::string cached_binary_platform_info; if (kStorageFactory != nullptr) { cache_storage_ = kStorageFactory->CreateStorage(kPrecompiledProgramFileName); @@ -405,13 +406,16 @@ OpenCLRuntime::OpenCLRuntime(): auto platform_info_array = this->cache_storage_->Find(kOpenCLPlatformInfoKey); if (platform_info_array != nullptr) { - cached_binary_platform_info_ = + cached_binary_platform_info = std::string(platform_info_array->begin(), platform_info_array->end()); + if (cached_binary_platform_info != platform_info_) { + cache_storage_->Clear(); + } } } - if (cached_binary_platform_info_ != platform_info_) { + if (cached_binary_platform_info != platform_info_) { if (OpenCLRuntime::kPrecompiledBinaryPath.empty()) { LOG(WARNING) << "There is no precompiled OpenCL binary in" " all OpenCL binary paths"; @@ -476,11 +480,6 @@ bool OpenCLRuntime::BuildProgramFromCache( cl::Program *program) { // Find from binary if (this->cache_storage_ == nullptr) return false; - if (cached_binary_platform_info_ != platform_info_) { - VLOG(3) << "cached OpenCL binary version is not same" - " with current version"; - return false; - } auto content = this->cache_storage_->Find(built_program_key); if (content == nullptr) { return false; diff --git a/mace/core/runtime/opencl/opencl_runtime.h b/mace/core/runtime/opencl/opencl_runtime.h index 01a255d2..f87b4580 100644 --- a/mace/core/runtime/opencl/opencl_runtime.h +++ b/mace/core/runtime/opencl/opencl_runtime.h @@ -136,7 +136,6 @@ class OpenCLRuntime { std::string platform_info_; OpenCLVersion opencl_version_; std::string precompiled_binary_platform_info_; - std::string cached_binary_platform_info_; bool out_of_range_check_; uint64_t device_gloabl_mem_cache_size_; uint32_t device_compute_units_; diff --git a/mace/public/mace_runtime.h b/mace/public/mace_runtime.h index a6a628b6..6fac6ec5 100644 --- a/mace/public/mace_runtime.h +++ b/mace/public/mace_runtime.h @@ -51,6 +51,7 @@ class KVStorage { public: // return: 0 for success, -1 for error virtual int Load() = 0; + virtual void Clear() = 0; virtual bool Insert(const std::string &key, const std::vector &value) = 0; virtual const std::vector *Find(const std::string &key) = 0; -- GitLab