GpuCacheMgr.cpp 2.2 KB
Newer Older
G
groot 已提交
1 2 3 4 5 6
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////

Y
Yu Kun 已提交
7
#include <sstream>
Y
Yu Kun 已提交
8
#include "utils/Log.h"
G
groot 已提交
9 10 11 12
#include "GpuCacheMgr.h"
#include "server/ServerConfig.h"

namespace zilliz {
J
jinhai 已提交
13
namespace milvus {
G
groot 已提交
14 15
namespace cache {

Y
Yu Kun 已提交
16
std::mutex GpuCacheMgr::mutex_;
Y
Yu Kun 已提交
17
std::unordered_map<uint64_t, GpuCacheMgrPtr> GpuCacheMgr::instance_;
Y
Yu Kun 已提交
18

J
jinhai 已提交
19 20 21
namespace {
    constexpr int64_t unit = 1024 * 1024 * 1024;

Y
Yu Kun 已提交
22 23 24 25 26 27 28 29 30 31 32
    std::vector<uint64_t> load() {
        server::ConfigNode& config = server::ServerConfig::GetInstance().GetConfig(server::CONFIG_CACHE);
        std::string gpu_ids_str = config.GetValue(server::CONFIG_GPU_IDS, "0,1");

        std::vector<uint64_t > gpu_ids;

        std::stringstream ss(gpu_ids_str);
        for (int i; ss >> i;) {
            gpu_ids.push_back(i);
            if (ss.peek() == ',') {
                ss.ignore();
Y
Yu Kun 已提交
33 34
            }
        }
Y
Yu Kun 已提交
35
        return gpu_ids;
Y
Yu Kun 已提交
36
    }
Y
Yu Kun 已提交
37
}
Y
Yu Kun 已提交
38

Y
Yu Kun 已提交
39 40 41 42 43 44 45 46 47

bool GpuCacheMgr::GpuIdInConfig(uint64_t gpu_id) {
    static std::vector<uint64_t > ids = load();
    for (auto id : ids) {
        if (gpu_id == id) return true;
    }
    return false;
}

Y
Yu Kun 已提交
48 49
GpuCacheMgr::GpuCacheMgr() {
    server::ConfigNode& config = server::ServerConfig::GetInstance().GetConfig(server::CONFIG_CACHE);
Y
Yu Kun 已提交
50

Y
Yu Kun 已提交
51
    int64_t cap = config.GetInt64Value(server::CONFIG_GPU_CACHE_CAPACITY, 2);
J
jinhai 已提交
52
    cap *= unit;
G
groot 已提交
53
    cache_ = std::make_shared<Cache>(cap, 1UL<<32);
Y
Yu Kun 已提交
54 55 56 57 58 59 60 61

    double free_percent = config.GetDoubleValue(server::GPU_CACHE_FREE_PERCENT, 0.85);
    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 已提交
62 63 64 65
}

void GpuCacheMgr::InsertItem(const std::string& key, const DataObjPtr& data) {
    //TODO: copy data to gpu
Y
Yu Kun 已提交
66 67 68 69 70 71
    if (cache_ == nullptr) {
        SERVER_LOG_ERROR << "Cache doesn't exist";
        return;
    }

    cache_->insert(key, data);
G
groot 已提交
72 73 74 75 76
}

}
}
}