LargeSQ8HPass.cpp 2.9 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.

Y
Yu Kun 已提交
18 19 20 21 22 23
#include "scheduler/optimizer/LargeSQ8HPass.h"
#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 {

W
wxyu 已提交
30 31 32 33 34 35 36 37
LargeSQ8HPass::LargeSQ8HPass() {
    server::Config& config = server::Config::GetInstance();
    Status s = config.GetEngineConfigUseGpuThreshold(threshold_);
    if (!s.ok()) {
        threshold_ = std::numeric_limits<int32_t>::max();
    }
}

X
xiaojun.lin 已提交
38 39
bool
LargeSQ8HPass::Run(const TaskPtr& task) {
X
xiaojun.lin 已提交
40 41 42 43 44 45 46 47 48 49 50 51
    if (task->Type() != TaskType::SearchTask) {
        return false;
    }

    auto search_task = std::static_pointer_cast<XSearchTask>(task);
    if (search_task->file_->engine_type_ != (int)engine::EngineType::FAISS_IVFSQ8H) {
        return false;
    }

    auto search_job = std::static_pointer_cast<SearchJob>(search_task->job_.lock());

    // TODO: future, Index::IVFSQ8H, if nq < threshold set cpu, else set gpu
W
wxyu 已提交
52 53

    if (search_job->nq() < threshold_) {
X
xiaojun.lin 已提交
54 55 56 57
        return false;
    }

    std::vector<uint64_t> gpus = scheduler::get_gpu_pool();
W
wxyu 已提交
58 59 60 61 62 63 64 65 66 67 68 69
    //    std::vector<int64_t> all_free_mem;
    //    for (auto& gpu : gpus) {
    //        auto cache = cache::GpuCacheMgr::GetInstance(gpu);
    //        auto free_mem = cache->CacheCapacity() - cache->CacheUsage();
    //        all_free_mem.push_back(free_mem);
    //    }
    //
    //    auto max_e = std::max_element(all_free_mem.begin(), all_free_mem.end());
    //    auto best_index = std::distance(all_free_mem.begin(), max_e);
    //    auto best_device_id = gpus[best_index];
    auto best_device_id = count_ % gpus.size();
    count_++;
X
xiaojun.lin 已提交
70 71 72 73 74 75 76 77 78 79 80 81

    ResourcePtr res_ptr = ResMgrInst::GetInstance()->GetResource(ResourceType::GPU, best_device_id);
    if (not res_ptr) {
        SERVER_LOG_ERROR << "GpuResource " << best_device_id << " invalid.";
        // TODO: throw critical error and exit
        return false;
    }

    auto label = std::make_shared<SpecResLabel>(std::weak_ptr<Resource>(res_ptr));
    task->label() = label;

    return true;
X
xiaojun.lin 已提交
82
}
Y
Yu Kun 已提交
83 84 85

}  // namespace scheduler
}  // namespace milvus