From 283e087a787b30dc5aac47ddd7aba30a088a11e6 Mon Sep 17 00:00:00 2001 From: Wang XiangYu Date: Fri, 17 Jul 2020 20:59:31 +0800 Subject: [PATCH] add config value gpu.cache.enable (#2899) * add config value gpu.cache.enable Signed-off-by: wxyu * clang-format Signed-off-by: wxyu * clang-format Signed-off-by: wxyu * fix ut Signed-off-by: wxyu --- core/src/config/Config.cpp | 34 +++++++++++++++++++++- core/src/config/Config.h | 6 ++++ core/src/db/engine/ExecutionEngineImpl.cpp | 14 +++++++-- core/src/db/engine/ExecutionEngineImpl.h | 2 ++ 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/core/src/config/Config.cpp b/core/src/config/Config.cpp index a86230d7fa..8267296e07 100644 --- a/core/src/config/Config.cpp +++ b/core/src/config/Config.cpp @@ -126,6 +126,8 @@ const char* CONFIG_GPU_RESOURCE_ENABLE_DEFAULT = "true"; #else const char* CONFIG_GPU_RESOURCE_ENABLE_DEFAULT = "false"; #endif +const char* CONFIG_GPU_RESOURCE_CACHE_ENABLE = "cache.enable"; +const char* CONFIG_GPU_RESOURCE_CACHE_ENABLE_DEFAULT = "false"; const char* CONFIG_GPU_RESOURCE_CACHE_CAPACITY = "cache_size"; const char* CONFIG_GPU_RESOURCE_CACHE_CAPACITY_DEFAULT = "1073741824"; // 1024 * 1024 * 1024 const char* CONFIG_GPU_RESOURCE_CACHE_THRESHOLD = "cache_threshold"; @@ -374,6 +376,9 @@ Config::ValidateConfig() { std::cout << "GPU resources " << (gpu_resource_enable ? "ENABLED !" : "DISABLED !") << std::endl; if (gpu_resource_enable) { + bool resource_cache_enable; + STATUS_CHECK(GetGpuResourceConfigCacheEnable(resource_cache_enable)); + int64_t resource_cache_capacity; STATUS_CHECK(GetGpuResourceConfigCacheCapacity(resource_cache_capacity)); @@ -488,6 +493,7 @@ Config::ResetDefaultConfig() { /* gpu resource config */ #ifdef MILVUS_GPU_VERSION STATUS_CHECK(SetGpuResourceConfigEnable(CONFIG_GPU_RESOURCE_ENABLE_DEFAULT)); + STATUS_CHECK(SetGpuResourceConfigCacheEnable(CONFIG_GPU_RESOURCE_CACHE_ENABLE_DEFAULT)); STATUS_CHECK(SetGpuResourceConfigCacheCapacity(CONFIG_GPU_RESOURCE_CACHE_CAPACITY_DEFAULT)); STATUS_CHECK(SetGpuResourceConfigCacheThreshold(CONFIG_GPU_RESOURCE_CACHE_THRESHOLD_DEFAULT)); STATUS_CHECK(SetGpuResourceConfigGpuSearchThreshold(CONFIG_GPU_RESOURCE_GPU_SEARCH_THRESHOLD_DEFAULT)); @@ -629,6 +635,8 @@ Config::SetConfigCli(const std::string& parent_key, const std::string& child_key } else if (parent_key == CONFIG_GPU_RESOURCE) { if (child_key == CONFIG_GPU_RESOURCE_ENABLE) { status = SetGpuResourceConfigEnable(value); + } else if (child_key == CONFIG_GPU_RESOURCE_CACHE_ENABLE) { + status = SetGpuResourceConfigCacheEnable(value); } else if (child_key == CONFIG_GPU_RESOURCE_CACHE_CAPACITY) { status = SetGpuResourceConfigCacheCapacity(value); } else if (child_key == CONFIG_GPU_RESOURCE_CACHE_THRESHOLD) { @@ -1596,6 +1604,16 @@ Config::CheckGpuResourceConfigEnable(const std::string& value) { return Status::OK(); } +Status +Config::CheckGpuResourceConfigCacheEnable(const std::string& value) { + if (!ValidationUtil::ValidateStringIsBool(value).ok()) { + std::string msg = + "Invalid gpu resource config: " + value + ". Possible reason: gpu.cache.enable is not a boolean."; + return Status(SERVER_INVALID_ARGUMENT, msg); + } + return Status::OK(); +} + Status Config::CheckGpuResourceConfigCacheCapacity(const std::string& value) { fiu_return_on("check_gpu_cache_size_fail", Status(SERVER_INVALID_ARGUMENT, "")); @@ -2297,6 +2315,15 @@ Config::GetGpuResourceConfigEnable(bool& value) { return Status::OK(); } +Status +Config::GetGpuResourceConfigCacheEnable(bool& value) { + std::string str = + GetConfigStr(CONFIG_GPU_RESOURCE, CONFIG_GPU_RESOURCE_CACHE_ENABLE, CONFIG_GPU_RESOURCE_CACHE_ENABLE_DEFAULT); + STATUS_CHECK(CheckGpuResourceConfigCacheEnable(str)); + STATUS_CHECK(StringHelpFunctions::ConvertToBoolean(str, value)); + return Status::OK(); +} + Status Config::GetGpuResourceConfigCacheCapacity(int64_t& value) { bool gpu_resource_enable = false; @@ -2734,7 +2761,6 @@ Config::SetEngineSearchCombineMaxNq(const std::string& value) { /* gpu resource config */ #ifdef MILVUS_GPU_VERSION - Status Config::SetGpuResourceConfigEnable(const std::string& value) { STATUS_CHECK(CheckGpuResourceConfigEnable(value)); @@ -2742,6 +2768,12 @@ Config::SetGpuResourceConfigEnable(const std::string& value) { return ExecCallBacks(CONFIG_GPU_RESOURCE, CONFIG_GPU_RESOURCE_ENABLE, value); } +Status +Config::SetGpuResourceConfigCacheEnable(const std::string& value) { + STATUS_CHECK(CheckGpuResourceConfigCacheEnable(value)); + return SetConfigValueInMem(CONFIG_GPU_RESOURCE, CONFIG_GPU_RESOURCE_CACHE_ENABLE, value); +} + Status Config::SetGpuResourceConfigCacheCapacity(const std::string& value) { STATUS_CHECK(CheckGpuResourceConfigCacheCapacity(value)); diff --git a/core/src/config/Config.h b/core/src/config/Config.h index 89457422ba..77a0b12392 100644 --- a/core/src/config/Config.h +++ b/core/src/config/Config.h @@ -278,6 +278,8 @@ class Config { Status CheckGpuResourceConfigEnable(const std::string& value); Status + CheckGpuResourceConfigCacheEnable(const std::string& value); + Status CheckGpuResourceConfigCacheCapacity(const std::string& value); Status CheckGpuResourceConfigCacheThreshold(const std::string& value); @@ -400,6 +402,8 @@ class Config { Status GetGpuResourceConfigEnable(bool& value); Status + GetGpuResourceConfigCacheEnable(bool& value); + Status GetGpuResourceConfigCacheCapacity(int64_t& value); Status GetGpuResourceConfigCacheThreshold(float& value); @@ -514,6 +518,8 @@ class Config { Status SetGpuResourceConfigEnable(const std::string& value); Status + SetGpuResourceConfigCacheEnable(const std::string& value); + Status SetGpuResourceConfigCacheCapacity(const std::string& value); Status SetGpuResourceConfigCacheThreshold(const std::string& value); diff --git a/core/src/db/engine/ExecutionEngineImpl.cpp b/core/src/db/engine/ExecutionEngineImpl.cpp index 38fc590298..519792f412 100644 --- a/core/src/db/engine/ExecutionEngineImpl.cpp +++ b/core/src/db/engine/ExecutionEngineImpl.cpp @@ -584,9 +584,17 @@ ExecutionEngineImpl::CopyToGpu(uint64_t device_id, bool hybrid) { */ LOG_ENGINE_DEBUG_ << "CPU to GPU" << device_id << " start"; auto gpu_cache_mgr = cache::GpuCacheMgr::GetInstance(device_id); - // gpu_cache_mgr->Reserve(index_->Size()); - index_ = knowhere::cloner::CopyCpuToGpu(index_, device_id, knowhere::Config()); - // gpu_cache_mgr->InsertItem(location_, std::static_pointer_cast(index_)); + + bool gpu_cache_enable = false; + STATUS_CHECK(server::Config::GetInstance().GetGpuResourceConfigCacheEnable(gpu_cache_enable)); + + if (gpu_cache_enable) { + gpu_cache_mgr->Reserve(index_->Size()); + index_ = knowhere::cloner::CopyCpuToGpu(index_, device_id, knowhere::Config()); + gpu_cache_mgr->InsertItem(location_, std::static_pointer_cast(index_)); + } else { + index_ = knowhere::cloner::CopyCpuToGpu(index_, device_id, knowhere::Config()); + } LOG_ENGINE_DEBUG_ << "CPU to GPU" << device_id << " finished"; } catch (std::exception& e) { LOG_ENGINE_ERROR_ << e.what(); diff --git a/core/src/db/engine/ExecutionEngineImpl.h b/core/src/db/engine/ExecutionEngineImpl.h index b053cf9b5d..b4e3de73c7 100644 --- a/core/src/db/engine/ExecutionEngineImpl.h +++ b/core/src/db/engine/ExecutionEngineImpl.h @@ -136,6 +136,8 @@ class ExecutionEngineImpl : public ExecutionEngine { milvus::json index_params_; int64_t gpu_num_ = 0; + + bool gpu_cache_enable_ = false; }; } // namespace engine