提交 b576bd6f 编写于 作者: Y Yu Kun

modify GpuCacheMgr GetInsatnce


Former-commit-id: 7323d4a63e58a6e70f8d47f6b2fbe9566197013c
上级 4b4071f2
...@@ -46,9 +46,6 @@ public: ...@@ -46,9 +46,6 @@ public:
double freemem_percent() const { return freemem_percent_; }; double freemem_percent() const { return freemem_percent_; };
void set_freemem_percent(double percent) { freemem_percent_ = percent; } void set_freemem_percent(double percent) { freemem_percent_ = percent; }
void set_gpu_ids(std::vector<uint64_t>& gpu_ids) { gpu_ids_ = gpu_ids; }
std::vector<uint64_t> gpu_ids() const { return gpu_ids_; }
size_t size() const; size_t size() const;
bool exists(const std::string& key); bool exists(const std::string& key);
...@@ -63,7 +60,6 @@ private: ...@@ -63,7 +60,6 @@ private:
int64_t usage_; int64_t usage_;
int64_t capacity_; int64_t capacity_;
double freemem_percent_; double freemem_percent_;
std::vector<uint64_t> gpu_ids_;
LRU<std::string, CacheObjPtr> lru_; LRU<std::string, CacheObjPtr> lru_;
mutable std::mutex mutex_; mutable std::mutex mutex_;
......
...@@ -56,7 +56,6 @@ engine::VecIndexPtr CacheMgr::GetIndex(const std::string& key) { ...@@ -56,7 +56,6 @@ engine::VecIndexPtr CacheMgr::GetIndex(const std::string& key) {
} }
void CacheMgr::InsertItem(const std::string& key, const DataObjPtr& data) { void CacheMgr::InsertItem(const std::string& key, const DataObjPtr& data) {
std::cout << "dashalk\n";
if(cache_ == nullptr) { if(cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist"; SERVER_LOG_ERROR << "Cache doesn't exist";
return; return;
...@@ -131,24 +130,6 @@ void CacheMgr::SetCapacity(int64_t capacity) { ...@@ -131,24 +130,6 @@ void CacheMgr::SetCapacity(int64_t capacity) {
cache_->set_capacity(capacity); cache_->set_capacity(capacity);
} }
std::vector<uint64_t> CacheMgr::GpuIds() const {
if(cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist";
std::vector<uint64_t> gpu_ids;
return gpu_ids;
}
return cache_->gpu_ids();
}
void CacheMgr::SetGpuIds(std::vector<uint64_t> gpu_ids){
if(cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist";
return;
}
cache_->set_gpu_ids(gpu_ids);
}
} }
} }
} }
...@@ -33,8 +33,6 @@ public: ...@@ -33,8 +33,6 @@ public:
int64_t CacheUsage() const; int64_t CacheUsage() const;
int64_t CacheCapacity() const; int64_t CacheCapacity() const;
void SetCapacity(int64_t capacity); void SetCapacity(int64_t capacity);
std::vector<uint64_t > GpuIds() const;
void SetGpuIds(std::vector<uint64_t> gpu_ids);
protected: protected:
CacheMgr(); CacheMgr();
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
// Proprietary and confidential. // Proprietary and confidential.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#include <sstream>
#include "utils/Log.h" #include "utils/Log.h"
#include "GpuCacheMgr.h" #include "GpuCacheMgr.h"
#include "server/ServerConfig.h" #include "server/ServerConfig.h"
...@@ -18,34 +19,39 @@ std::unordered_map<uint64_t, GpuCacheMgrPtr> GpuCacheMgr::instance_; ...@@ -18,34 +19,39 @@ std::unordered_map<uint64_t, GpuCacheMgrPtr> GpuCacheMgr::instance_;
namespace { namespace {
constexpr int64_t unit = 1024 * 1024 * 1024; constexpr int64_t unit = 1024 * 1024 * 1024;
void parse_gpu_ids(std::string gpu_ids_str, std::vector<uint64_t>& gpu_ids) { std::vector<uint64_t> load() {
for (auto i = 0; i < gpu_ids_str.length(); ) { server::ConfigNode& config = server::ServerConfig::GetInstance().GetConfig(server::CONFIG_CACHE);
if (gpu_ids_str[i] != ',') { std::string gpu_ids_str = config.GetValue(server::CONFIG_GPU_IDS, "0,1");
int id = 0;
while (gpu_ids_str[i] <= '9' && gpu_ids_str[i] >= '0') { std::vector<uint64_t > gpu_ids;
id = id * 10 + gpu_ids_str[i] - '0';
++i; std::stringstream ss(gpu_ids_str);
for (int i; ss >> i;) {
gpu_ids.push_back(i);
if (ss.peek() == ',') {
ss.ignore();
} }
gpu_ids.push_back(id);
} else {
++i;
} }
return gpu_ids;
} }
}
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;
} }
GpuCacheMgr::GpuCacheMgr() { GpuCacheMgr::GpuCacheMgr() {
server::ConfigNode& config = server::ServerConfig::GetInstance().GetConfig(server::CONFIG_CACHE); server::ConfigNode& config = server::ServerConfig::GetInstance().GetConfig(server::CONFIG_CACHE);
std::string gpu_ids_str = config.GetValue(server::CONFIG_GPU_IDS, "0,1");
int64_t cap = config.GetInt64Value(server::CONFIG_GPU_CACHE_CAPACITY, 2); int64_t cap = config.GetInt64Value(server::CONFIG_GPU_CACHE_CAPACITY, 2);
cap *= unit; cap *= unit;
cache_ = std::make_shared<Cache>(cap, 1UL<<32); cache_ = std::make_shared<Cache>(cap, 1UL<<32);
std::vector<uint64_t> gpu_ids;
parse_gpu_ids(gpu_ids_str, gpu_ids);
cache_->set_gpu_ids(gpu_ids);
double free_percent = config.GetDoubleValue(server::GPU_CACHE_FREE_PERCENT, 0.85); double free_percent = config.GetDoubleValue(server::GPU_CACHE_FREE_PERCENT, 0.85);
if (free_percent > 0.0 && free_percent <= 1.0) { if (free_percent > 0.0 && free_percent <= 1.0) {
cache_->set_freemem_percent(free_percent); cache_->set_freemem_percent(free_percent);
......
...@@ -19,12 +19,18 @@ class GpuCacheMgr : public CacheMgr { ...@@ -19,12 +19,18 @@ class GpuCacheMgr : public CacheMgr {
public: public:
GpuCacheMgr(); GpuCacheMgr();
public: static bool GpuIdInConfig(uint64_t gpu_id);
static CacheMgr* GetInstance(uint64_t gpu_id) { static CacheMgr* GetInstance(uint64_t gpu_id) {
if (instance_.find(gpu_id) == instance_.end()) { if (instance_.find(gpu_id) == instance_.end()) {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
if (instance_.find(gpu_id) == instance_.end()) {
if (GpuIdInConfig(gpu_id)) {
instance_.insert(std::pair<uint64_t, GpuCacheMgrPtr>(gpu_id, std::make_shared<GpuCacheMgr>())); instance_.insert(std::pair<uint64_t, GpuCacheMgrPtr>(gpu_id, std::make_shared<GpuCacheMgr>()));
// instance_[gpu_id] = std::make_shared<GpuCacheMgr>(); } else {
return nullptr;
}
}
} }
return instance_[gpu_id].get(); return instance_[gpu_id].get();
} }
......
...@@ -263,9 +263,6 @@ ClientTest::Test(const std::string& address, const std::string& port) { ...@@ -263,9 +263,6 @@ ClientTest::Test(const std::string& address, const std::string& port) {
search_record_array.push_back( search_record_array.push_back(
std::make_pair(record_ids[SEARCH_TARGET], record_array[SEARCH_TARGET])); std::make_pair(record_ids[SEARCH_TARGET], record_array[SEARCH_TARGET]));
} }
int64_t row_count;
conn->CountTable(TABLE_NAME, row_count);
std::cout << "\t" << TABLE_NAME << "(" << row_count << " rows)" << std::endl;
} }
} }
......
...@@ -448,7 +448,6 @@ InsertTask::OnExecute() { ...@@ -448,7 +448,6 @@ InsertTask::OnExecute() {
// TODO: change to one dimension array in protobuf or use multiple-thread to copy the data // TODO: change to one dimension array in protobuf or use multiple-thread to copy the data
for (size_t i = 0; i < insert_param_.row_record_array_size(); i++) { for (size_t i = 0; i < insert_param_.row_record_array_size(); i++) {
for (size_t j = 0; j < table_info.dimension_; j++) {
if (insert_param_.row_record_array(i).vector_data().empty()) { if (insert_param_.row_record_array(i).vector_data().empty()) {
return SetError(SERVER_INVALID_ROWRECORD_ARRAY, "Row record float array is empty"); return SetError(SERVER_INVALID_ROWRECORD_ARRAY, "Row record float array is empty");
} }
...@@ -460,6 +459,8 @@ InsertTask::OnExecute() { ...@@ -460,6 +459,8 @@ InsertTask::OnExecute() {
std::to_string(table_info.dimension_); std::to_string(table_info.dimension_);
return SetError(error_code, error_msg); return SetError(error_code, error_msg);
} }
//TODO: use memcpy
for (size_t j = 0; j < table_info.dimension_; j++) {
vec_f[i * table_info.dimension_ + j] = insert_param_.row_record_array(i).vector_data(j); vec_f[i * table_info.dimension_ + j] = insert_param_.row_record_array(i).vector_data(j);
} }
} }
......
...@@ -165,8 +165,8 @@ TEST(CacheTest, GPU_CACHE_TEST) { ...@@ -165,8 +165,8 @@ TEST(CacheTest, GPU_CACHE_TEST) {
gpu_mgr->ClearCache(); gpu_mgr->ClearCache();
ASSERT_EQ(gpu_mgr->ItemCount(), 0); ASSERT_EQ(gpu_mgr->ItemCount(), 0);
gpu_mgr->SetCapacity(4096000000);
for (auto i = 0; i < 3; i++) { for (auto i = 0; i < 3; i++) {
// TODO: use gpu index to mock
MockVecIndex *mock_index = new MockVecIndex(); MockVecIndex *mock_index = new MockVecIndex();
mock_index->ntotal_ = 1000000; //2G mock_index->ntotal_ = 1000000; //2G
engine::VecIndexPtr index(mock_index); engine::VecIndexPtr index(mock_index);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册