diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 34fc75f792d86f81fc9d78d9f09413c86a18d6f0..a8c49657c59eee12cf74777e12d85bbf0aa44421 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -14,6 +14,10 @@ set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -O0 -g") message("CUDA_TOOLKIT_ROOT_DIR=${CUDA_TOOLKIT_ROOT_DIR}") message("CUDA_NVCC_FLAGS=${CUDA_NVCC_FLAGS}") +if (GPU_VERSION STREQUAL "ON") + add_definitions("-DGPU_VERSION") +endif () + set(CMAKE_CXX_STANDARD 14) if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)") diff --git a/cpp/build.sh b/cpp/build.sh index 300175bfeebdf995430cb61cbba7673d4316ac0d..f03b411bbbe4a881c85cb27881d10d811a91e81c 100755 --- a/cpp/build.sh +++ b/cpp/build.sh @@ -2,9 +2,10 @@ BUILD_TYPE="Debug" BUILD_UNITTEST="off" +BUILD_GPU="OFF" INSTALL_PREFIX=$(pwd)/megasearch -while getopts "p:t:uh" arg +while getopts "p:t:uhg" arg do case $arg in t) @@ -17,6 +18,9 @@ do p) INSTALL_PREFIX=$OPTARG ;; + g) + BUILD_GPU="ON" + ;; h) # help echo " @@ -51,6 +55,7 @@ CMAKE_CMD="cmake -DBUILD_UNIT_TEST=${BUILD_UNITTEST} \ -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \ -DCMAKE_CUDA_COMPILER=${CUDA_COMPILER} \ +-DGPU_VERSION=${BUILD_GPU} \ $@ ../" echo ${CMAKE_CMD} diff --git a/cpp/src/CMakeLists.txt b/cpp/src/CMakeLists.txt index f1a23c5c0d0f539abf91b4029d1ba225dd619c37..fff8fd9e6952e2952c27c581b127d041ac0452b0 100644 --- a/cpp/src/CMakeLists.txt +++ b/cpp/src/CMakeLists.txt @@ -30,23 +30,35 @@ set(vecwise_engine_src include_directories(/usr/include) include_directories(/usr/local/cuda/include) -find_library(cuda_library cudart cublas HINTS /usr/local/cuda/lib64) -cuda_add_library(vecwise_engine STATIC ${vecwise_engine_src}) - -set(engine_libs - pthread - libfaiss.a - libgomp.a - libopenblas.a - libgfortran.a - libquadmath.a - cudart - cublas - libsqlite3.a +if (GPU_VERSION STREQUAL "ON") + link_directories(/usr/local/cuda/lib64) + set(engine_libs + pthread + libfaiss.a + libgomp.a + libopenblas.a + libgfortran.a + libquadmath.a + cudart + cublas + libsqlite3.a + ) +else() + set(engine_libs + pthread + libfaiss.a + libgomp.a + libopenblas.a + libgfortran.a + libquadmath.a + libsqlite3.a ) +endif () + +cuda_add_library(vecwise_engine STATIC ${vecwise_engine_src}) -target_link_libraries(vecwise_engine ${engine_libs} ${cuda_library}) +target_link_libraries(vecwise_engine ${engine_libs}) add_executable(vecwise_server ${config_files} @@ -71,6 +83,7 @@ set(server_libs libz.a libzstd.a liblz4.a + dl ) target_link_libraries(vecwise_server ${server_libs}) diff --git a/cpp/src/db/DB.cpp b/cpp/src/db/DB.cpp index 808c87ad1d622fada025bddd7e2beca18685457c..5baa09bd7151c81e2af792e69d8566c0ccc78fe7 100644 --- a/cpp/src/db/DB.cpp +++ b/cpp/src/db/DB.cpp @@ -19,7 +19,14 @@ DB::~DB() {} void DB::Open(const Options& options, DB** dbptr) { *dbptr = nullptr; - *dbptr = DBFactory::Build(options); + +#ifdef GPU_VERSION + std::string default_index_type{"Faiss,IVF"}; +#else + std::string default_index_type{"Faiss,IDMap"}; +#endif + + *dbptr = DBFactory::Build(options, default_index_type); return; } diff --git a/cpp/src/db/DBImpl.cpp b/cpp/src/db/DBImpl.cpp index 79266ddca0d243f22cfe57aa21897322bd3c2544..f977222535cb8a975532a45d5ae3d0f17667ce84 100644 --- a/cpp/src/db/DBImpl.cpp +++ b/cpp/src/db/DBImpl.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include @@ -142,12 +141,27 @@ Status DBImpl::search(const std::string& group_id, size_t k, size_t nq, } }; + auto topk_cpu = [](const std::vector &input_data, + const int &k, + float *output_distence, + long *output_ids) -> void { + std::map inverted_table; + for (int i = 0; i < input_data.size(); ++i) { + inverted_table[input_data[i]] = i; + } + + int count = 0; + for (auto it = inverted_table.begin(); it != inverted_table.end() && count < k; ++it, ++count) { + output_distence[count] = it->first; + output_ids[count] = it->second; + } + }; auto cluster_topk = [&]() -> void { QueryResult res; for (auto &result_pair : batchresult) { auto &dis = result_pair.second; auto &nns = result_pair.first; - TopK(dis.data(), dis.size(), k, output_distence, output_ids); + topk_cpu(dis, k, output_distence, output_ids); for (int i = 0; i < k; ++i) { res.emplace_back(nns[output_ids[i]]); // mapping } diff --git a/cpp/src/wrapper/Index.cpp b/cpp/src/wrapper/Index.cpp index 0dcc8fc7ea7deceeb93f1b038aad0b45f7e48746..6b04ba05b4b1f805aa43e4861baeeebf42b8e8e7 100644 --- a/cpp/src/wrapper/Index.cpp +++ b/cpp/src/wrapper/Index.cpp @@ -4,7 +4,8 @@ // Proprietary and confidential. //////////////////////////////////////////////////////////////////////////////// -#ifdef CUDA_VERSION +// TODO: maybe support static search +#ifdef GPU_VERSION #include "faiss/gpu/GpuAutoTune.h" #include "faiss/gpu/StandardGpuResources.h" #include "faiss/gpu/utils/DeviceUtils.h" diff --git a/cpp/src/wrapper/Index.h b/cpp/src/wrapper/Index.h index b5c5d0cc07c413c6eaba2c4f0da417bf4de4f621..460fdefcf1ca71a50358fe6866e293c220885a5f 100644 --- a/cpp/src/wrapper/Index.h +++ b/cpp/src/wrapper/Index.h @@ -13,8 +13,6 @@ #include #include "faiss/AutoTune.h" -#include "faiss/AuxIndexStructures.h" -#include "faiss/gpu/GpuAutoTune.h" #include "faiss/index_io.h" #include "Operand.h" diff --git a/cpp/src/wrapper/IndexBuilder.cpp b/cpp/src/wrapper/IndexBuilder.cpp index e9552b984dde1f2d919904665e1493ae0251008d..240cf95bfb92f9f9790ed217fbe89e1768436996 100644 --- a/cpp/src/wrapper/IndexBuilder.cpp +++ b/cpp/src/wrapper/IndexBuilder.cpp @@ -6,9 +6,12 @@ #include "mutex" +#ifdef GPU_VERSION #include #include "faiss/gpu/GpuIndexIVFFlat.h" #include "faiss/gpu/GpuAutoTune.h" +#endif + #include "faiss/IndexFlat.h" #include "IndexBuilder.h" @@ -34,6 +37,7 @@ Index_ptr IndexBuilder::build_all(const long &nb, const long &nt, const float *xt) { std::shared_ptr host_index = nullptr; +#ifdef GPU_VERSION { // TODO: list support index-type. faiss::Index *ori_index = faiss::index_factory(opd_->d, opd_->get_index_type(nb).c_str()); @@ -52,6 +56,17 @@ Index_ptr IndexBuilder::build_all(const long &nb, delete device_index; delete ori_index; } +#else + { + faiss::Index *index = faiss::index_factory(opd_->d, opd_->get_index_type(nb).c_str()); + if (!index->is_trained) { + nt == 0 || xt == nullptr ? index->train(nb, xb) + : index->train(nt, xt); + } + index->add_with_ids(nb, xb, ids); + host_index.reset(index); + } +#endif return std::make_shared(host_index); } diff --git a/cpp/src/wrapper/Arithmetic.h b/cpp/src/wrapper/gpu/Arithmetic.h similarity index 100% rename from cpp/src/wrapper/Arithmetic.h rename to cpp/src/wrapper/gpu/Arithmetic.h diff --git a/cpp/src/wrapper/Topk.cu b/cpp/src/wrapper/gpu/Topk.cu similarity index 100% rename from cpp/src/wrapper/Topk.cu rename to cpp/src/wrapper/gpu/Topk.cu diff --git a/cpp/src/wrapper/Topk.h b/cpp/src/wrapper/gpu/Topk.h similarity index 100% rename from cpp/src/wrapper/Topk.h rename to cpp/src/wrapper/gpu/Topk.h diff --git a/cpp/unittest/faiss_wrapper/CMakeLists.txt b/cpp/unittest/faiss_wrapper/CMakeLists.txt index be124ca80062bda6af2c5d703777bad54eedfbad..bdd0d797aacce3ee5bcd167b64feaab9fe7bd602 100644 --- a/cpp/unittest/faiss_wrapper/CMakeLists.txt +++ b/cpp/unittest/faiss_wrapper/CMakeLists.txt @@ -48,7 +48,7 @@ target_link_libraries(wrapper_test ${unittest_libs} ${wrapper_libs}) set(topk_test_src topk_test.cpp - ${CMAKE_SOURCE_DIR}/src/wrapper/Topk.cu) + ${CMAKE_SOURCE_DIR}/src/wrapper/gpu/Topk.cu) -cuda_add_executable(topk_test ${topk_test_src}) -target_link_libraries(topk_test ${unittest_libs} ${faiss_libs}) +#cuda_add_executable(topk_test ${topk_test_src}) +#target_link_libraries(topk_test ${unittest_libs} ${faiss_libs}) diff --git a/cpp/unittest/faiss_wrapper/topk_test.cpp b/cpp/unittest/faiss_wrapper/topk_test.cpp index 6943829130267e5280df2c8096b6ae478fc63bd5..9c972783fc9e5a19c745b4268e5009b7161949ba 100644 --- a/cpp/unittest/faiss_wrapper/topk_test.cpp +++ b/cpp/unittest/faiss_wrapper/topk_test.cpp @@ -6,7 +6,7 @@ #include -#include "wrapper/Topk.h" +#include "wrapper/gpu/Topk.h" using namespace zilliz::vecwise::engine; diff --git a/cpp/unittest/server/CMakeLists.txt b/cpp/unittest/server/CMakeLists.txt index dc79f8ed1b6cb09556d1214b342ad2ddc8bebf3e..7141c45ede1a335d8f0b85b2496ec1303f636018 100644 --- a/cpp/unittest/server/CMakeLists.txt +++ b/cpp/unittest/server/CMakeLists.txt @@ -7,7 +7,7 @@ include_directories(../../src) include_directories(/usr/include) include_directories(/usr/local/cuda/include) -find_library(cuda_library cudart cublas HINTS /usr/local/cuda/lib64) +link_directories(/usr/local/cuda/lib64) aux_source_directory(../../src/config config_files) aux_source_directory(../../src/cache cache_srcs)