Skip to content

Commit 65cb52d

Browse files
zhagnluluzhang
andauthored
Support dynamic simd framework and using term expr as example (milvus-io#25260)
Signed-off-by: luzhang <luzhang@zilliz.com> Co-authored-by: luzhang <luzhang@zilliz.com>
1 parent 4aed32f commit 65cb52d

27 files changed

Lines changed: 2712 additions & 28 deletions

Makefile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ useasan = false
2424
ifeq (${USE_ASAN}, true)
2525
useasan = true
2626
endif
27-
opensimd = OFF
27+
use_dynamic_simd = OFF
28+
ifdef USE_DYNAMIC_SIMD
29+
use_dynamic_simd = ${USE_DYNAMIC_SIMD}
30+
endif
2831

2932
export GIT_BRANCH=master
3033

@@ -149,19 +152,19 @@ generated-proto: download-milvus-proto build-3rdparty
149152

150153
build-cpp: generated-proto
151154
@echo "Building Milvus cpp library ..."
152-
@(env bash $(PWD)/scripts/core_build.sh -t ${mode} -f "$(CUSTOM_THIRDPARTY_PATH)" -n ${disk_index} -i ${opensimd})
155+
@(env bash $(PWD)/scripts/core_build.sh -t ${mode} -f "$(CUSTOM_THIRDPARTY_PATH)" -n ${disk_index} -y ${use_dynamic_simd})
153156

154157
build-cpp-gpu: generated-proto
155158
@echo "Building Milvus cpp gpu library ..."
156-
@(env bash $(PWD)/scripts/core_build.sh -t ${mode} -g -f "$(CUSTOM_THIRDPARTY_PATH)" -n ${disk_index} -i ${opensimd})
159+
@(env bash $(PWD)/scripts/core_build.sh -t ${mode} -g -f "$(CUSTOM_THIRDPARTY_PATH)" -n ${disk_index} -y ${use_dynamic_simd})
157160

158161
build-cpp-with-unittest: generated-proto
159162
@echo "Building Milvus cpp library with unittest ..."
160-
@(env bash $(PWD)/scripts/core_build.sh -t ${mode} -u -f "$(CUSTOM_THIRDPARTY_PATH)" -n ${disk_index} -i ${opensimd})
163+
@(env bash $(PWD)/scripts/core_build.sh -t ${mode} -u -f "$(CUSTOM_THIRDPARTY_PATH)" -n ${disk_index} -y ${use_dynamic_simd})
161164

162165
build-cpp-with-coverage: generated-proto
163166
@echo "Building Milvus cpp library with coverage and unittest ..."
164-
@(env bash $(PWD)/scripts/core_build.sh -t ${mode} -u -a ${useasan} -c -f "$(CUSTOM_THIRDPARTY_PATH)" -n ${disk_index} -i ${opensimd})
167+
@(env bash $(PWD)/scripts/core_build.sh -t ${mode} -u -a ${useasan} -c -f "$(CUSTOM_THIRDPARTY_PATH)" -n ${disk_index} -y ${use_dynamic_simd})
165168

166169
check-proto-product: generated-proto
167170
@(env bash $(PWD)/scripts/check_proto_product.sh)

internal/core/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ if ( MILVUS_GPU_VERSION )
2929
add_definitions(-DMILVUS_GPU_VERSION)
3030
endif ()
3131

32+
if ( USE_DYNAMIC_SIMD )
33+
add_definitions(-DUSE_DYNAMIC_SIMD)
34+
endif()
35+
3236
project(core)
3337
include(CheckCXXCompilerFlag)
3438
if ( APPLE )

internal/core/src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ add_subdirectory( index )
3535
add_subdirectory( query )
3636
add_subdirectory( segcore )
3737
add_subdirectory( indexbuilder )
38+
if(USE_DYNAMIC_SIMD)
39+
add_subdirectory( simd )
40+
endif()

internal/core/src/common/Types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ using IndexType = knowhere::IndexType;
140140
// Plus 1 because we can't use greater(>) symbol
141141
constexpr size_t REF_SIZE_THRESHOLD = 16 + 1;
142142

143-
using BitSetBlockType = BitsetType::block_type;
143+
using BitsetBlockType = BitsetType::block_type;
144144
constexpr size_t BITSET_BLOCK_SIZE = sizeof(BitsetType::block_type);
145145
constexpr size_t BITSET_BLOCK_BIT_SIZE = sizeof(BitsetType::block_type) * 8;
146146
template <typename T>

internal/core/src/query/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ set(MILVUS_QUERY_SRCS
3030
PlanProto.cpp
3131
)
3232
add_library(milvus_query ${MILVUS_QUERY_SRCS})
33-
target_link_libraries(milvus_query milvus_index)
33+
if(USE_DYNAMIC_SIMD)
34+
target_link_libraries(milvus_query milvus_index milvus_simd)
35+
else()
36+
target_link_libraries(milvus_query milvus_index)
37+
endif()

internal/core/src/query/visitors/ExecExprVisitor.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include "segcore/SegmentGrowingImpl.h"
3636
#include "simdjson/error.h"
3737
#include "query/PlanProto.h"
38+
#include "simd/hook.h"
39+
3840
namespace milvus::query {
3941
// THIS CONTAINS EXTRA BODY FOR VISITOR
4042
// WILL BE USED BY GENERATOR
@@ -186,7 +188,10 @@ AppendOneChunk(BitsetType& result, const FixedVector<bool>& chunk_res) {
186188
// Append a value once instead of BITSET_BLOCK_BIT_SIZE times.
187189
auto AppendBlock = [&result](const bool* ptr, int n) {
188190
for (int i = 0; i < n; ++i) {
189-
BitSetBlockType val = 0;
191+
#if defined(USE_DYNAMIC_SIMD)
192+
auto val = milvus::simd::get_bitset_block(ptr);
193+
#else
194+
BitsetBlockType val = 0;
190195
// This can use CPU SIMD optimzation
191196
uint8_t vals[BITSET_BLOCK_SIZE] = {0};
192197
for (size_t j = 0; j < 8; ++j) {
@@ -195,8 +200,9 @@ AppendOneChunk(BitsetType& result, const FixedVector<bool>& chunk_res) {
195200
}
196201
}
197202
for (size_t j = 0; j < BITSET_BLOCK_SIZE; ++j) {
198-
val |= BitSetBlockType(vals[j]) << (8 * j);
203+
val |= BitsetBlockType(vals[j]) << (8 * j);
199204
}
205+
#endif
200206
result.append(val);
201207
ptr += BITSET_BLOCK_SIZE * 8;
202208
}
@@ -1782,11 +1788,31 @@ ExecExprVisitor::ExecTermVisitorImplTemplate(TermExpr& expr_raw) -> BitsetType {
17821788
auto index_func = [&terms, n](Index* index) {
17831789
return index->In(n, terms.data());
17841790
};
1785-
auto elem_func = [&terms, &term_set](MayConstRef<T> x) {
1786-
//// terms has already been sorted.
1787-
// return std::binary_search(terms.begin(), terms.end(), x);
1791+
1792+
#if defined(USE_DYNAMIC_SIMD)
1793+
std::function<bool(MayConstRef<T> x)> elem_func;
1794+
if (n <= milvus::simd::TERM_EXPR_IN_SIZE_THREAD) {
1795+
elem_func = [&terms, &term_set, n](MayConstRef<T> x) {
1796+
if constexpr (std::is_integral<T>::value ||
1797+
std::is_floating_point<T>::value) {
1798+
return milvus::simd::find_term_func<T>(terms.data(), n, x);
1799+
} else {
1800+
// For string type, simd performance not better than set mode
1801+
static_assert(std::is_same<T, std::string>::value ||
1802+
std::is_same<T, std::string_view>::value);
1803+
return term_set.find(x) != term_set.end();
1804+
}
1805+
};
1806+
} else {
1807+
elem_func = [&term_set, n](MayConstRef<T> x) {
1808+
return term_set.find(x) != term_set.end();
1809+
};
1810+
}
1811+
#else
1812+
auto elem_func = [&term_set](MayConstRef<T> x) {
17881813
return term_set.find(x) != term_set.end();
17891814
};
1815+
#endif
17901816

17911817
return ExecRangeVisitorImpl<T>(
17921818
expr.column_.field_id, index_func, elem_func);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (C) 2019-2020 Zilliz. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
4+
# with the License. You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software distributed under the License
9+
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10+
# or implied. See the License for the specific language governing permissions and limitations under the License
11+
12+
set(MILVUS_SIMD_SRCS
13+
ref.cpp
14+
hook.cpp
15+
)
16+
17+
if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
18+
# x86 cpu simd
19+
message ("simd using x86_64 mode")
20+
list(APPEND MILVUS_SIMD_SRCS
21+
sse2.cpp
22+
sse4.cpp
23+
avx2.cpp
24+
avx512.cpp
25+
)
26+
set_source_files_properties(sse4.cpp PROPERTIES COMPILE_FLAGS "-msse4.2")
27+
set_source_files_properties(avx2.cpp PROPERTIES COMPILE_FLAGS "-mavx2")
28+
set_source_files_properties(avx512.cpp PROPERTIES COMPILE_FLAGS "-mavx512f -mavx512dq -mavx512bw")
29+
elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm*")
30+
# TODO: add arm cpu simd
31+
endif()
32+
33+
add_library(milvus_simd ${MILVUS_SIMD_SRCS})
34+
35+
# Link the milvus_simd library with other libraries as needed
36+
target_link_libraries(milvus_simd milvus_log)

0 commit comments

Comments
 (0)