Caiyd 1946 cpu2gpu getres (#2124)

* #1946 use new log format in knowhere

Signed-off-by: yudong.cai <yudong.cai@zilliz.com>

* #1946 add more debug info for GPU device initialize

Signed-off-by: yudong.cai <yudong.cai@zilliz.com>

* retry CI

Signed-off-by: yudong.cai <yudong.cai@zilliz.com>

* retry CI

Signed-off-by: yudong.cai <yudong.cai@zilliz.com>
pull/2136/head
Cai Yudong 2020-04-26 18:29:21 +08:00 committed by GitHub
parent 12b3bf0693
commit 150cca6bbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 122 additions and 34 deletions

View File

@ -22,6 +22,7 @@ endif ()
set(external_srcs
knowhere/common/Exception.cpp
knowhere/common/Log.cpp
knowhere/common/Timer.cpp
)

View File

@ -0,0 +1,55 @@
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
//
// Licensed 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.
#include "knowhere/common/Log.h"
#include <cstdarg>
#include <cstdio>
#include <memory>
#include <string>
namespace milvus {
namespace knowhere {
std::string
LogOut(const char* pattern, ...) {
size_t len = strnlen(pattern, 1024) + 256;
auto str_p = std::make_unique<char[]>(len);
memset(str_p.get(), 0, len);
va_list vl;
va_start(vl, pattern);
vsnprintf(str_p.get(), len, pattern, vl);
va_end(vl);
return std::string(str_p.get());
}
void
SetThreadName(const std::string& name) {
pthread_setname_np(pthread_self(), name.c_str());
}
std::string
GetThreadName() {
std::string thread_name = "unamed";
char name[16];
size_t len = 16;
auto err = pthread_getname_np(pthread_self(), name, len);
if (not err) {
thread_name = name;
}
return thread_name;
}
} // namespace knowhere
} // namespace milvus

View File

@ -7,24 +7,50 @@
//
// 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
// or implied. See the License for the specific language governing permissions and limitations under the License.
#pragma once
#include <string>
#include "easyloggingpp/easylogging++.h"
namespace milvus {
namespace knowhere {
#define KNOWHERE_DOMAIN_NAME "[KNOWHERE] "
#define KNOWHERE_ERROR_TEXT "KNOWHERE Error:"
std::string
LogOut(const char* pattern, ...);
#define KNOWHERE_LOG_TRACE LOG(TRACE) << KNOWHERE_DOMAIN_NAME
#define KNOWHERE_LOG_DEBUG LOG(DEBUG) << KNOWHERE_DOMAIN_NAME
#define KNOWHERE_LOG_INFO LOG(INFO) << KNOWHERE_DOMAIN_NAME
#define KNOWHERE_LOG_WARNING LOG(WARNING) << KNOWHERE_DOMAIN_NAME
#define KNOWHERE_LOG_ERROR LOG(ERROR) << KNOWHERE_DOMAIN_NAME
#define KNOWHERE_LOG_FATAL LOG(FATAL) << KNOWHERE_DOMAIN_NAME
void
SetThreadName(const std::string& name);
std::string
GetThreadName();
/*
* Please use LOG_MODULE_LEVEL_C macro in member function of class
* and LOG_MODULE_LEVEL_ macro in other functions.
*/
/////////////////////////////////////////////////////////////////////////////////////////////////
#define KNOWHERE_MODULE_NAME "KNOWHERE"
#define KNOWHERE_MODULE_CLASS_FUNCTION \
LogOut("[%s][%s::%s][%s] ", KNOWHERE_MODULE_NAME, (typeid(*this).name()), __FUNCTION__, GetThreadName().c_str())
#define KNOWHERE_MODULE_FUNCTION LogOut("[%s][%s][%s] ", KNOWHERE_MODULE_NAME, __FUNCTION__, GetThreadName().c_str())
#define LOG_KNOWHERE_TRACE_C LOG(TRACE) << KNOWHERE_MODULE_CLASS_FUNCTION
#define LOG_KNOWHERE_DEBUG_C LOG(DEBUG) << KNOWHERE_MODULE_CLASS_FUNCTION
#define LOG_KNOWHERE_INFO_C LOG(INFO) << KNOWHERE_MODULE_CLASS_FUNCTION
#define LOG_KNOWHERE_WARNING_C LOG(WARNING) << KNOWHERE_MODULE_CLASS_FUNCTION
#define LOG_KNOWHERE_ERROR_C LOG(ERROR) << KNOWHERE_MODULE_CLASS_FUNCTION
#define LOG_KNOWHERE_FATAL_C LOG(FATAL) << KNOWHERE_MODULE_CLASS_FUNCTION
#define LOG_KNOWHERE_TRACE_ LOG(TRACE) << KNOWHERE_MODULE_FUNCTION
#define LOG_KNOWHERE_DEBUG_ LOG(DEBUG) << KNOWHERE_MODULE_FUNCTION
#define LOG_KNOWHERE_INFO_ LOG(INFO) << KNOWHERE_MODULE_FUNCTION
#define LOG_KNOWHERE_WARNING_ LOG(WARNING) << KNOWHERE_MODULE_FUNCTION
#define LOG_KNOWHERE_ERROR_ LOG(ERROR) << KNOWHERE_MODULE_FUNCTION
#define LOG_KNOWHERE_FATAL_ LOG(FATAL) << KNOWHERE_MODULE_FUNCTION
} // namespace knowhere
} // namespace milvus

View File

@ -48,15 +48,15 @@ TimeRecorder::PrintTimeRecord(const std::string& msg, double span) {
break;
}
case 1: {
KNOWHERE_LOG_DEBUG << str_log;
LOG_KNOWHERE_DEBUG_ << str_log;
break;
}
// case 2: {
// KNOWHERE_LOG_TRACE << str_log;
// LOG_KNOWHERE_TRACE_ << str_log;
// break;
// }
// case 3: {
// KNOWHERE_LOG_WARNING << str_log;
// LOG_KNOWHERE_WARNING_ << str_log;
// break;
// }
}

View File

@ -85,7 +85,7 @@ void
IndexAnnoy::BuildAll(const DatasetPtr& dataset_ptr, const Config& config) {
if (index_) {
// it is builded all
KNOWHERE_LOG_DEBUG << "IndexAnnoy::BuildAll: index_ has been built!";
LOG_KNOWHERE_DEBUG_ << "IndexAnnoy::BuildAll: index_ has been built!";
return;
}

View File

@ -189,9 +189,9 @@ BinaryIVF::QueryImpl(int64_t n, const uint8_t* data, int64_t k, float* distances
stdclock::time_point after = stdclock::now();
double search_cost = (std::chrono::duration<double, std::micro>(after - before)).count();
KNOWHERE_LOG_DEBUG << "IVF search cost: " << search_cost
<< ", quantization cost: " << faiss::indexIVF_stats.quantization_time
<< ", data search cost: " << faiss::indexIVF_stats.search_time;
LOG_KNOWHERE_DEBUG_ << "IVF search cost: " << search_cost
<< ", quantization cost: " << faiss::indexIVF_stats.quantization_time
<< ", data search cost: " << faiss::indexIVF_stats.search_time;
faiss::indexIVF_stats.quantization_time = 0;
faiss::indexIVF_stats.search_time = 0;
}

View File

@ -297,9 +297,9 @@ IVF::QueryImpl(int64_t n, const float* data, int64_t k, float* distances, int64_
ivf_index->search(n, (float*)data, k, distances, labels, bitset_);
stdclock::time_point after = stdclock::now();
double search_cost = (std::chrono::duration<double, std::micro>(after - before)).count();
KNOWHERE_LOG_DEBUG << "IVF search cost: " << search_cost
<< ", quantization cost: " << faiss::indexIVF_stats.quantization_time
<< ", data search cost: " << faiss::indexIVF_stats.search_time;
LOG_KNOWHERE_DEBUG_ << "IVF search cost: " << search_cost
<< ", quantization cost: " << faiss::indexIVF_stats.quantization_time
<< ", data search cost: " << faiss::indexIVF_stats.search_time;
faiss::indexIVF_stats.quantization_time = 0;
faiss::indexIVF_stats.search_time = 0;
}
@ -311,7 +311,7 @@ IVF::SealImpl() {
auto idx = dynamic_cast<faiss::IndexIVF*>(index);
if (idx != nullptr) {
// To be deleted
KNOWHERE_LOG_DEBUG << "Test before to_readonly: IVF READONLY " << std::boolalpha << idx->is_readonly();
LOG_KNOWHERE_DEBUG_ << "Test before to_readonly: IVF READONLY " << std::boolalpha << idx->is_readonly();
idx->to_readonly();
}
#endif

View File

@ -10,6 +10,7 @@
// or implied. See the License for the specific language governing permissions and limitations under the License
#include "knowhere/index/vector_index/helpers/FaissGpuResourceMgr.h"
#include "knowhere/common/Log.h"
#include <fiu-local.h>
#include <utility>
@ -24,7 +25,7 @@ FaissGpuResourceMgr::GetInstance() {
}
void
FaissGpuResourceMgr::AllocateTempMem(ResPtr& resource, const int64_t& device_id, const int64_t& size) {
FaissGpuResourceMgr::AllocateTempMem(ResPtr& resource, const int64_t device_id, const int64_t size) {
if (size) {
resource->faiss_res->setTempMemory(size);
} else {
@ -44,6 +45,8 @@ FaissGpuResourceMgr::InitDevice(int64_t device_id, int64_t pin_mem_size, int64_t
params.resource_num = res_num;
devices_params_.emplace(device_id, params);
LOG_KNOWHERE_DEBUG_ << "DEVICEID " << device_id << ", pin_mem_size " << pin_mem_size << ", temp_mem_size "
<< temp_mem_size << ", resource count " << res_num;
}
void
@ -53,18 +56,15 @@ FaissGpuResourceMgr::InitResource() {
is_init = true;
// std::cout << "InitResource" << std::endl;
for (auto& device : devices_params_) {
auto& device_id = device.first;
mutex_cache_.emplace(device_id, std::make_unique<std::mutex>());
// std::cout << "Device Id: " << DEVICEID << std::endl;
auto& device_param = device.second;
auto& bq = idle_map_[device_id];
for (int64_t i = 0; i < device_param.resource_num; ++i) {
// std::cout << "Resource Id: " << i << std::endl;
auto raw_resource = std::make_shared<faiss::gpu::StandardGpuResources>();
// TODO(linxj): enable set pinned memory
@ -73,12 +73,12 @@ FaissGpuResourceMgr::InitResource() {
bq.Put(res_wrapper);
}
LOG_KNOWHERE_DEBUG_ << "DEVICEID " << device_id << ", resource count " << bq.Size();
}
// std::cout << "End initResource" << std::endl;
}
ResPtr
FaissGpuResourceMgr::GetRes(const int64_t& device_id, const int64_t& alloc_size) {
FaissGpuResourceMgr::GetRes(const int64_t device_id, const int64_t alloc_size) {
fiu_return_on("FaissGpuResourceMgr.GetRes.ret_null", nullptr);
InitResource();
@ -88,12 +88,18 @@ FaissGpuResourceMgr::GetRes(const int64_t& device_id, const int64_t& alloc_size)
auto&& resource = bq.Take();
AllocateTempMem(resource, device_id, alloc_size);
return resource;
} else {
LOG_KNOWHERE_ERROR_ << "GPU device " << device_id << " not initialized";
for (auto& item : idle_map_) {
auto& bq = item.second;
LOG_KNOWHERE_ERROR_ << "DEVICEID " << item.first << ", resource count " << bq.Size();
}
return nullptr;
}
return nullptr;
}
void
FaissGpuResourceMgr::MoveToIdle(const int64_t& device_id, const ResPtr& res) {
FaissGpuResourceMgr::MoveToIdle(const int64_t device_id, const ResPtr& res) {
auto finder = idle_map_.find(device_id);
if (finder != idle_map_.end()) {
auto& bq = finder->second;

View File

@ -58,7 +58,7 @@ class FaissGpuResourceMgr {
Free();
void
AllocateTempMem(ResPtr& resource, const int64_t& device_id, const int64_t& size);
AllocateTempMem(ResPtr& resource, const int64_t device_id, const int64_t size);
void
InitDevice(int64_t device_id, int64_t pin_mem_size = 0, int64_t temp_mem_size = 0, int64_t res_num = 2);
@ -68,10 +68,10 @@ class FaissGpuResourceMgr {
// allocate gpu memory invoke by build or copy_to_gpu
ResPtr
GetRes(const int64_t& device_id, const int64_t& alloc_size = 0);
GetRes(const int64_t device_id, const int64_t alloc_size = 0);
void
MoveToIdle(const int64_t& device_id, const ResPtr& res);
MoveToIdle(const int64_t device_id, const ResPtr& res);
void
Dump();

View File

@ -74,8 +74,8 @@ NsgIndex::Build_with_ids(size_t nb, const float* data, const int64_t* ids, const
for (size_t i = 0; i < ntotal; ++i) {
total_degree += nsg[i].size();
}
KNOWHERE_LOG_DEBUG << "Graph physical size: " << total_degree * sizeof(node_t) / 1024 / 1024 << "m";
KNOWHERE_LOG_DEBUG << "Average degree: " << total_degree / ntotal;
LOG_KNOWHERE_DEBUG_ << "Graph physical size: " << total_degree * sizeof(node_t) / 1024 / 1024 << "m";
LOG_KNOWHERE_DEBUG_ << "Average degree: " << total_degree / ntotal;
// Debug code
// for (size_t i = 0; i < ntotal; i++) {

View File

@ -33,6 +33,7 @@ set(util_srcs
${INDEX_SOURCE_DIR}/knowhere/knowhere/index/vector_index/helpers/IndexParameter.cpp
${INDEX_SOURCE_DIR}/knowhere/knowhere/index/vector_index/IndexType.cpp
${INDEX_SOURCE_DIR}/knowhere/knowhere/common/Exception.cpp
${INDEX_SOURCE_DIR}/knowhere/knowhere/common/Log.cpp
${INDEX_SOURCE_DIR}/knowhere/knowhere/common/Timer.cpp
${INDEX_SOURCE_DIR}/unittest/utils.cpp
)

View File

@ -35,7 +35,6 @@ constexpr int64_t SEARCH_TARGET = BATCH_ENTITY_COUNT / 2; // change this value,
constexpr int64_t ADD_ENTITY_LOOP = 5;
constexpr milvus::IndexType INDEX_TYPE = milvus::IndexType::IVFSQ8;
constexpr int32_t NLIST = 16384;
constexpr uint64_t FIELD_NUM = 3;
void PrintEntity(const std::string& tag, const milvus::Entity& entity) {
std::cout << tag << "\t[";