FaissIVFSQ8Pass.cpp 2.8 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
S
starlord 已提交
2
//
3 4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
S
starlord 已提交
5
//
6 7 8 9 10
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under the License.
11
#ifdef MILVUS_GPU_VERSION
12
#include "scheduler/selector/FaissIVFSQ8Pass.h"
Y
Yu Kun 已提交
13
#include "cache/GpuCacheMgr.h"
14
#include "config/Config.h"
Y
Yu Kun 已提交
15 16 17 18 19 20 21 22 23
#include "scheduler/SchedInst.h"
#include "scheduler/Utils.h"
#include "scheduler/task/SearchTask.h"
#include "scheduler/tasklabel/SpecResLabel.h"
#include "utils/Log.h"

namespace milvus {
namespace scheduler {

24
void
F
fishpenguin 已提交
25
FaissIVFSQ8Pass::Init() {
G
groot 已提交
26
#ifdef MILVUS_GPU_VERSION
W
wxyu 已提交
27
    server::Config& config = server::Config::GetInstance();
28
    Status s = config.GetEngineConfigGpuSearchThreshold(threshold_);
W
wxyu 已提交
29 30 31
    if (!s.ok()) {
        threshold_ = std::numeric_limits<int32_t>::max();
    }
32
    s = config.GetGpuResourceConfigSearchResources(search_gpus_);
F
fishpenguin 已提交
33
    if (!s.ok()) {
S
shengjh 已提交
34
        throw std::exception();
F
fishpenguin 已提交
35
    }
36 37 38 39

    SetIdentity("FaissIVFSQ8Pass");
    AddGpuEnableListener();
    AddGpuSearchThresholdListener();
40
    AddGpuSearchResourcesListener();
G
groot 已提交
41
#endif
W
wxyu 已提交
42 43
}

X
xiaojun.lin 已提交
44
bool
F
fishpenguin 已提交
45
FaissIVFSQ8Pass::Run(const TaskPtr& task) {
X
xiaojun.lin 已提交
46 47 48 49 50
    if (task->Type() != TaskType::SearchTask) {
        return false;
    }

    auto search_task = std::static_pointer_cast<XSearchTask>(task);
F
fishpenguin 已提交
51
    if (search_task->file_->engine_type_ != (int)engine::EngineType::FAISS_IVFSQ8) {
X
xiaojun.lin 已提交
52 53 54 55
        return false;
    }

    auto search_job = std::static_pointer_cast<SearchJob>(search_task->job_.lock());
F
fishpenguin 已提交
56
    ResourcePtr res_ptr;
57
    if (!gpu_enable_) {
58
        LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissIVFSQ8Pass: gpu disable, specify cpu to search!", "search", 0);
59 60
        res_ptr = ResMgrInst::GetInstance()->GetResource("cpu");
    } else if (search_job->nq() < threshold_) {
61 62
        LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissIVFSQ8Pass: nq < gpu_search_threshold, specify cpu to search!",
                                    "search", 0);
F
fishpenguin 已提交
63 64
        res_ptr = ResMgrInst::GetInstance()->GetResource("cpu");
    } else {
C
Cai Yudong 已提交
65 66 67 68
        LOG_SERVER_DEBUG_ << LogOut("[%s][%d] FaissIVFSQ8Pass: nq >= gpu_search_threshold, specify gpu %d to search!",
                                    "search", 0, search_gpus_[idx_]);
        res_ptr = ResMgrInst::GetInstance()->GetResource(ResourceType::GPU, search_gpus_[idx_]);
        idx_ = (idx_ + 1) % search_gpus_.size();
X
xiaojun.lin 已提交
69
    }
F
fishpenguin 已提交
70
    auto label = std::make_shared<SpecResLabel>(res_ptr);
X
xiaojun.lin 已提交
71 72
    task->label() = label;
    return true;
X
xiaojun.lin 已提交
73
}
Y
Yu Kun 已提交
74 75 76

}  // namespace scheduler
}  // namespace milvus
77
#endif