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 "utils/Log.h"
G
groot 已提交
8 9 10 11
#include "GpuCacheMgr.h"
#include "server/ServerConfig.h"

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

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

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

Y
Yu Kun 已提交
21 22 23 24 25 26 27 28 29 30
    void parse_gpu_ids(std::string gpu_ids_str, std::vector<uint64_t>& gpu_ids) {
        for (auto i = 0; i < gpu_ids_str.length(); ) {
            if (gpu_ids_str[i] != ',') {
                int id = 0;
                while (gpu_ids_str[i] <= '9' && gpu_ids_str[i] >= '0') {
                    id = id * 10 + gpu_ids_str[i] - '0';
                    ++i;
                }
                gpu_ids.push_back(id);
            } else {
Y
Yu Kun 已提交
31 32 33 34
                ++i;
            }
        }
    }
Y
Yu Kun 已提交
35
}
Y
Yu Kun 已提交
36

Y
Yu Kun 已提交
37 38 39
GpuCacheMgr::GpuCacheMgr() {
    server::ConfigNode& config = server::ServerConfig::GetInstance().GetConfig(server::CONFIG_CACHE);
    std::string gpu_ids_str = config.GetValue(server::CONFIG_GPU_IDS, "0,1");
Y
Yu Kun 已提交
40

Y
Yu Kun 已提交
41
    int64_t cap = config.GetInt64Value(server::CONFIG_GPU_CACHE_CAPACITY, 2);
J
jinhai 已提交
42
    cap *= unit;
G
groot 已提交
43
    cache_ = std::make_shared<Cache>(cap, 1UL<<32);
Y
Yu Kun 已提交
44

Y
Yu Kun 已提交
45 46 47 48
    std::vector<uint64_t> gpu_ids;
    parse_gpu_ids(gpu_ids_str, gpu_ids);
    cache_->set_gpu_ids(gpu_ids);

Y
Yu Kun 已提交
49 50 51 52 53 54 55
    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 已提交
56 57 58 59
}

void GpuCacheMgr::InsertItem(const std::string& key, const DataObjPtr& data) {
    //TODO: copy data to gpu
Y
Yu Kun 已提交
60 61 62 63 64 65
    if (cache_ == nullptr) {
        SERVER_LOG_ERROR << "Cache doesn't exist";
        return;
    }

    cache_->insert(key, data);
G
groot 已提交
66 67 68 69 70
}

}
}
}