From 08d9caa0dee19cd5d8ee20f2b9973b8c9c2a51ee Mon Sep 17 00:00:00 2001 From: liuqi Date: Thu, 3 May 2018 16:29:13 +0800 Subject: [PATCH] Remove unuseful code for mt opencl runtime. --- mace/core/file_storage.cc | 5 +++- mace/core/file_storage.h | 1 + mace/core/runtime/opencl/opencl_runtime.cc | 14 +++-------- mace/core/runtime/opencl/opencl_runtime.h | 2 -- mace/utils/rwlock.h | 28 +++++++--------------- 5 files changed, 16 insertions(+), 34 deletions(-) diff --git a/mace/core/file_storage.cc b/mace/core/file_storage.cc index cf5f0099..20e98c54 100644 --- a/mace/core/file_storage.cc +++ b/mace/core/file_storage.cc @@ -57,7 +57,7 @@ std::unique_ptr FileStorageFactory::CreateStorage( } FileStorage::FileStorage(const std::string &file_path): - file_path_(file_path) {} + data_changed_(false), file_path_(file_path) {} int FileStorage::Load() { struct stat st; @@ -151,6 +151,7 @@ bool FileStorage::Insert(const std::string &key, const std::vector &value) { utils::WriteLock lock(&data_mutex_); data_.emplace(key, value); + data_changed_ = true; return true; } @@ -164,6 +165,7 @@ const std::vector *FileStorage::Find(const std::string &key) { int FileStorage::Flush() { utils::WriteLock lock(&data_mutex_); + if (!data_changed_) return 0; int fd = open(file_path_.c_str(), O_WRONLY | O_CREAT, 0600); if (fd < 0) { LOG(WARNING) << "open file " << file_path_ @@ -224,6 +226,7 @@ int FileStorage::Flush() { << " failed, error code: " << errno; return -1; } + data_changed_ = false; return 0; } diff --git a/mace/core/file_storage.h b/mace/core/file_storage.h index 7ff12419..799719f3 100644 --- a/mace/core/file_storage.h +++ b/mace/core/file_storage.h @@ -36,6 +36,7 @@ class FileStorage : public KVStorage { int Flush() override; private: + bool data_changed_; std::string file_path_; std::map> data_; utils::RWMutex data_mutex_; diff --git a/mace/core/runtime/opencl/opencl_runtime.cc b/mace/core/runtime/opencl/opencl_runtime.cc index 202a23cd..cf114f4d 100644 --- a/mace/core/runtime/opencl/opencl_runtime.cc +++ b/mace/core/runtime/opencl/opencl_runtime.cc @@ -348,8 +348,6 @@ OpenCLRuntime::OpenCLRuntime(): &err); MACE_CHECK_CL_SUCCESS(err); - this->program_map_changed_ = false; - extern std::shared_ptr kStorageFactory; if (kStorageFactory != nullptr) { const std::string cl_compiled_file_name = "mace_cl_compiled_program.bin"; @@ -512,8 +510,6 @@ void OpenCLRuntime::BuildProgramFromSource( if (this->storage_ != nullptr) { this->storage_->Insert(built_program_key, content); - std::lock_guard lock(program_map_changed_mutex_); - this->program_map_changed_ = true; } VLOG(3) << "Program from source: " << built_program_key; @@ -567,13 +563,9 @@ cl::Kernel OpenCLRuntime::BuildKernel( void OpenCLRuntime::SaveBuiltCLProgram() { if (storage_ != nullptr) { - std::lock_guard lock(program_map_changed_mutex_); - if (program_map_changed_) { - if (storage_->Flush() != 0) { - LOG(FATAL) << "Store OPENCL compiled kernel to file failed." - " Please Make sure the storage directory exist."; - } - program_map_changed_ = false; + if (storage_->Flush() != 0) { + LOG(FATAL) << "Store OPENCL compiled kernel to file failed." + " Please Make sure the storage directory exist."; } } } diff --git a/mace/core/runtime/opencl/opencl_runtime.h b/mace/core/runtime/opencl/opencl_runtime.h index 238ec734..9588a3c6 100644 --- a/mace/core/runtime/opencl/opencl_runtime.h +++ b/mace/core/runtime/opencl/opencl_runtime.h @@ -125,11 +125,9 @@ class OpenCLRuntime { std::shared_ptr command_queue_; std::map built_program_map_; std::mutex program_build_mutex_; - std::mutex program_map_changed_mutex_; std::string platform_info_; std::string opencl_version_; bool out_of_range_check_; - bool program_map_changed_; uint64_t device_gloabl_mem_cache_size_; uint32_t device_compute_units_; GPUType gpu_type_; diff --git a/mace/utils/rwlock.h b/mace/utils/rwlock.h index c3bd2a88..851c7f71 100644 --- a/mace/utils/rwlock.h +++ b/mace/utils/rwlock.h @@ -43,29 +43,22 @@ class RWMutex { class ReadLock { public: explicit ReadLock(RWMutex *rw_mutex): rw_mutex_(rw_mutex) { - if (rw_mutex_ == nullptr) { - return; - } + MACE_CHECK_NOTNULL(rw_mutex); std::unique_lock lock(rw_mutex->mutex_); - rw_mutex->waiting_readers_++; + rw_mutex->waiting_readers_ += 1; rw_mutex->reader_cv_.wait(lock, [&]() -> bool { return rw_mutex->waiting_writers_ == 0 && rw_mutex->counter_ >= 0; }); - rw_mutex->waiting_readers_--; - rw_mutex->counter_++; + rw_mutex->waiting_readers_ -= 1; + rw_mutex->counter_ += 1; } ~ReadLock() { - if (rw_mutex_ == nullptr) { - return; - } std::unique_lock lock(rw_mutex_->mutex_); rw_mutex_->counter_ -= 1; if (rw_mutex_->waiting_writers_ > 0) { if (rw_mutex_->counter_ == 0) { rw_mutex_->writer_cv_.notify_one(); } - } else { - rw_mutex_->reader_cv_.notify_all(); } } ReadLock(const ReadLock &) = delete; @@ -80,21 +73,16 @@ class ReadLock { class WriteLock { public: explicit WriteLock(RWMutex *rw_mutex): rw_mutex_(rw_mutex) { - if (rw_mutex_ == nullptr) { - return; - } + MACE_CHECK_NOTNULL(rw_mutex); std::unique_lock lock(rw_mutex->mutex_); - rw_mutex->waiting_writers_++; + rw_mutex->waiting_writers_ += 1; rw_mutex->writer_cv_.wait(lock, [&]() -> bool { return rw_mutex->counter_ == 0; }); - rw_mutex->waiting_writers_--; - rw_mutex->counter_--; + rw_mutex->waiting_writers_ -= 1; + rw_mutex->counter_ -= 1; } ~WriteLock() { - if (rw_mutex_ == nullptr) { - return; - } std::unique_lock lock(rw_mutex_->mutex_); rw_mutex_->counter_ = 0; if (rw_mutex_->waiting_writers_ > 0) { -- GitLab