GpuCacheMgr.cpp 2.5 KB
Newer Older
J
jinhai 已提交
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.

G
groot 已提交
18

Y
Yu Kun 已提交
19
#include <sstream>
Y
Yu Kun 已提交
20
#include "utils/Log.h"
G
groot 已提交
21 22 23 24
#include "GpuCacheMgr.h"
#include "server/ServerConfig.h"

namespace zilliz {
J
jinhai 已提交
25
namespace milvus {
G
groot 已提交
26 27
namespace cache {

Y
Yu Kun 已提交
28
std::mutex GpuCacheMgr::mutex_;
Y
Yu Kun 已提交
29
std::unordered_map<uint64_t, GpuCacheMgrPtr> GpuCacheMgr::instance_;
Y
Yu Kun 已提交
30

J
jinhai 已提交
31
namespace {
S
starlord 已提交
32
    constexpr int64_t G_BYTE = 1024 * 1024 * 1024;
Y
Yu Kun 已提交
33 34
}

Y
Yu Kun 已提交
35 36
GpuCacheMgr::GpuCacheMgr() {
    server::ConfigNode& config = server::ServerConfig::GetInstance().GetConfig(server::CONFIG_CACHE);
Y
Yu Kun 已提交
37

38 39
    int64_t cap =
        config.GetInt64Value(server::CONFIG_CACHE_GPU_MEM_CAPACITY, std::stoi(server::CONFIG_CACHE_GPU_MEM_CAPACITY_DEFAULT));
S
starlord 已提交
40
    cap *= G_BYTE;
S
starlord 已提交
41
    cache_ = std::make_shared<Cache<DataObjPtr>>(cap, 1UL<<32);
Y
Yu Kun 已提交
42

43 44
    double free_percent =
        config.GetDoubleValue(server::CONFIG_CACHE_GPU_MEM_THRESHOLD, std::stof(server::CONFIG_CACHE_GPU_MEM_THRESHOLD_DEFAULT));
Y
Yu Kun 已提交
45 46 47 48 49 50
    if (free_percent > 0.0 && free_percent <= 1.0) {
        cache_->set_freemem_percent(free_percent);
    } else {
        SERVER_LOG_ERROR << "Invalid gpu_cache_free_percent: " << free_percent <<
                         ", defaultly set to " << cache_->freemem_percent();
    }
G
groot 已提交
51 52
}

S
starlord 已提交
53
GpuCacheMgr* GpuCacheMgr::GetInstance(uint64_t gpu_id) {
S
starlord 已提交
54 55 56 57 58 59 60 61 62 63 64 65
    if (instance_.find(gpu_id) == instance_.end()) {
        std::lock_guard<std::mutex> lock(mutex_);
        if (instance_.find(gpu_id) == instance_.end()) {
            instance_.insert(std::pair<uint64_t, GpuCacheMgrPtr>(gpu_id, std::make_shared<GpuCacheMgr>()));
        }
        return instance_[gpu_id].get();
    } else {
        std::lock_guard<std::mutex> lock(mutex_);
        return instance_[gpu_id].get();
    }
}

S
starlord 已提交
66 67 68 69
engine::VecIndexPtr GpuCacheMgr::GetIndex(const std::string& key) {
    DataObjPtr obj = GetItem(key);
    if(obj != nullptr) {
        return obj->data();
Y
Yu Kun 已提交
70 71
    }

S
starlord 已提交
72
    return nullptr;
G
groot 已提交
73 74 75 76 77
}

}
}
}