FaissIVFFlatPass.cpp 2.3 KB
Newer Older
S
starlord 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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
//
//   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.

F
fishpenguin 已提交
18
#include "scheduler/optimizer/FaissIVFFlatPass.h"
Y
Yu Kun 已提交
19 20 21 22 23
#include "cache/GpuCacheMgr.h"
#include "scheduler/SchedInst.h"
#include "scheduler/Utils.h"
#include "scheduler/task/SearchTask.h"
#include "scheduler/tasklabel/SpecResLabel.h"
W
wxyu 已提交
24
#include "server/Config.h"
Y
Yu Kun 已提交
25 26 27 28 29
#include "utils/Log.h"

namespace milvus {
namespace scheduler {

30
void
F
fishpenguin 已提交
31
FaissIVFFlatPass::Init() {
W
wxyu 已提交
32
    server::Config& config = server::Config::GetInstance();
33
    Status s = config.GetEngineConfigGpuSearchThreshold(threshold_);
W
wxyu 已提交
34 35 36
    if (!s.ok()) {
        threshold_ = std::numeric_limits<int32_t>::max();
    }
37
    s = config.GetGpuResourceConfigSearchResources(gpus);
F
fishpenguin 已提交
38 39 40
    if (!s.ok()) {
        throw;
    }
W
wxyu 已提交
41 42
}

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

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

    auto search_job = std::static_pointer_cast<SearchJob>(search_task->job_.lock());
F
fishpenguin 已提交
55
    ResourcePtr res_ptr;
W
wxyu 已提交
56
    if (search_job->nq() < threshold_) {
F
fishpenguin 已提交
57 58 59 60 61
        res_ptr = ResMgrInst::GetInstance()->GetResource("cpu");
    } else {
        auto best_device_id = count_ % gpus.size();
        count_++;
        res_ptr = ResMgrInst::GetInstance()->GetResource(ResourceType::GPU, best_device_id);
X
xiaojun.lin 已提交
62
    }
F
fishpenguin 已提交
63
    auto label = std::make_shared<SpecResLabel>(res_ptr);
X
xiaojun.lin 已提交
64 65
    task->label() = label;
    return true;
X
xiaojun.lin 已提交
66
}
Y
Yu Kun 已提交
67 68 69

}  // namespace scheduler
}  // namespace milvus