Read config once in optimizer

Former-commit-id: 8fee2a3cfa20cdc8b51e86867b7b3eb71dd2e759
This commit is contained in:
fishpenguin 2019-10-31 16:41:48 +08:00
parent a68255f9d0
commit 1dc7b321a4
4 changed files with 32 additions and 31 deletions

View File

@ -26,9 +26,11 @@
#include "optimizer/OnlyCPUPass.h"
#include "optimizer/OnlyGPUPass.h"
#include "optimizer/Optimizer.h"
#include "server/Config.h"
#include <memory>
#include <mutex>
#include <string>
#include <vector>
namespace milvus {
@ -95,11 +97,21 @@ class OptimizerInst {
if (instance == nullptr) {
std::lock_guard<std::mutex> lock(mutex_);
if (instance == nullptr) {
server::Config& config = server::Config::GetInstance();
std::vector<std::string> search_resources;
bool has_cpu = false;
config.GetResourceConfigSearchResources(search_resources);
for (auto& resource : search_resources) {
if (resource == "cpu") {
has_cpu = true;
}
}
std::vector<PassPtr> pass_list;
pass_list.push_back(std::make_shared<LargeSQ8HPass>());
pass_list.push_back(std::make_shared<HybridPass>());
pass_list.push_back(std::make_shared<OnlyCPUPass>());
pass_list.push_back(std::make_shared<OnlyGPUPass>());
pass_list.push_back(std::make_shared<OnlyGPUPass>(has_cpu));
instance = std::make_shared<Optimizer>(pass_list);
}
}

View File

@ -35,13 +35,13 @@ OnlyCPUPass::Run(const TaskPtr& task) {
}
auto gpu_id = get_gpu_pool();
if (gpu_id.empty()) {
ResourcePtr res_ptr = ResMgrInst::GetInstance()->GetResource("cpu");
auto label = std::make_shared<SpecResLabel>(std::weak_ptr<Resource>(res_ptr));
task->label() = label;
return true;
}
return false;
if (not gpu_id.empty())
return false;
ResourcePtr res_ptr = ResMgrInst::GetInstance()->GetResource("cpu");
auto label = std::make_shared<SpecResLabel>(std::weak_ptr<Resource>(res_ptr));
task->label() = label;
return true;
}
} // namespace scheduler

View File

@ -20,14 +20,16 @@
#include "scheduler/Utils.h"
#include "scheduler/task/SearchTask.h"
#include "scheduler/tasklabel/SpecResLabel.h"
#include "server/Config.h"
namespace milvus {
namespace scheduler {
OnlyGPUPass::OnlyGPUPass(bool has_cpu) : has_cpu_(has_cpu) {
}
bool
OnlyGPUPass::Run(const TaskPtr& task) {
if (task->Type() != TaskType::SearchTask)
if (task->Type() != TaskType::SearchTask || has_cpu_)
return false;
auto search_task = std::static_pointer_cast<XSearchTask>(task);
@ -36,29 +38,15 @@ OnlyGPUPass::Run(const TaskPtr& task) {
return false;
}
server::Config& config = server::Config::GetInstance();
std::vector<std::string> search_resources;
config.GetResourceConfigSearchResources(search_resources);
for (auto& resource : search_resources) {
if (resource == "cpu") {
return false;
}
}
auto gpu_id = get_gpu_pool();
if (!gpu_id.empty()) {
ResourcePtr res_ptr = ResMgrInst::GetInstance()->GetResource(ResourceType::GPU, gpu_id[specified_gpu_id_]);
auto label = std::make_shared<SpecResLabel>(std::weak_ptr<Resource>(res_ptr));
task->label() = label;
} else {
if (gpu_id.empty())
return false;
}
if (specified_gpu_id_ < gpu_id.size() - 1) {
++specified_gpu_id_;
} else {
specified_gpu_id_ = 0;
}
ResourcePtr res_ptr = ResMgrInst::GetInstance()->GetResource(ResourceType::GPU, gpu_id[specified_gpu_id_]);
auto label = std::make_shared<SpecResLabel>(std::weak_ptr<Resource>(res_ptr));
task->label() = label;
specified_gpu_id_ = specified_gpu_id_++ % gpu_id.size();
return true;
}

View File

@ -34,7 +34,7 @@ namespace scheduler {
class OnlyGPUPass : public Pass {
public:
OnlyGPUPass() = default;
explicit OnlyGPUPass(bool has_cpu);
public:
bool
@ -42,6 +42,7 @@ class OnlyGPUPass : public Pass {
private:
uint64_t specified_gpu_id_ = 0;
bool has_cpu_ = false;
};
using OnlyGPUPassPtr = std::shared_ptr<OnlyGPUPass>;