diff --git a/include/svs/index/vamana/vamana_build.h b/include/svs/index/vamana/vamana_build.h index f658a974f..161133b76 100644 --- a/include/svs/index/vamana/vamana_build.h +++ b/include/svs/index/vamana/vamana_build.h @@ -505,8 +505,16 @@ class VamanaBuilder { threadpool_, range, [&](const auto& is, uint64_t SVS_UNUSED(tid)) { + std::vector node_neighbors{}; for (auto node_id : is) { - for (auto other_id : graph_.get_node(node_id)) { + // Another thread may append to node_id's list under its lock, so an + // unlocked read of the degree word would be a data race. + { + std::lock_guard lock{vertex_locks_[node_id]}; + const auto& node_list = graph_.get_node(node_id); + node_neighbors.assign(node_list.begin(), node_list.end()); + } + for (auto other_id : node_neighbors) { std::lock_guard lock{vertex_locks_[other_id]}; if (graph_.get_node_degree(other_id) < params_.graph_max_degree) { graph_.add_edge(other_id, node_id); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 88a9ae412..cf37c2e56 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -262,22 +262,23 @@ if (SVS_TEST_CONCURRENT_FULL_SIZE) endif() ##### -##### ThreadSanitizer targets for the concurrent index +##### ThreadSanitizer targets for the concurrent index and the static build ##### -# The correctness of `svs::index::vamana::concurrent` rests almost entirely on memory -# ordering, which a non-instrumented test can only fail to disprove. These targets are -# opt-in because TSan costs roughly an order of magnitude in both time and memory: +# The correctness of `svs::index::vamana::concurrent`, and of locking in the static +# `VamanaBuilder`, rests almost entirely on memory ordering, which a non-instrumented test +# can only fail to disprove. These targets are opt-in because TSan costs roughly an order +# of magnitude in both time and memory: # # cmake -DSVS_EXPERIMENTAL_ENABLE_CONCURRENT_TSAN=YES ... # ctest -L tsan # # `svs::svs` and Catch2 are not instrumented here, only the test translation units. That is -# enough: the concurrent index is header-only, so the accesses under test -- the adjacency -# lists, the sequence-lock counters, the slot metadata -- are all inlined into the -# instrumented TUs. +# enough: `svs::svs` is a header-only INTERFACE library, so the accesses under test -- the +# adjacency lists, the sequence-lock counters, the slot metadata, the builder's per-vertex +# locks -- are all inlined into the instrumented TUs. option(SVS_EXPERIMENTAL_ENABLE_CONCURRENT_TSAN - "Build ThreadSanitizer targets for the concurrent Vamana index (slow)" OFF + "Build ThreadSanitizer targets for the concurrent and static Vamana builds (slow)" OFF ) if (SVS_EXPERIMENTAL_ENABLE_CONCURRENT_TSAN) @@ -322,4 +323,21 @@ if (SVS_EXPERIMENTAL_ENABLE_CONCURRENT_TSAN) target_include_directories(concurrent_tsan_negative PRIVATE ${PROJECT_SOURCE_DIR}) add_test(NAME concurrent_tsan_negative COMMAND concurrent_tsan_negative) set_tests_properties(concurrent_tsan_negative PROPERTIES WILL_FAIL TRUE LABELS "tsan") + + # The builder's per-vertex spinlocks order most cross-thread accesses, hub vertices above + # all, so a clean run here is weak evidence that any one unlocked read is race-free. + add_executable(vamana_build_tsan + ${TEST_DIR}/svs/index/vamana/build_tsan.cpp + ${TEST_DIR}/utils/test_dataset.cpp + ) + target_compile_options(vamana_build_tsan PRIVATE -fsanitize=thread -g -O1) + target_link_options(vamana_build_tsan PRIVATE -fsanitize=thread) + target_compile_definitions(vamana_build_tsan PRIVATE SVS_TEST_DATA_DIR="${DATA_DIRECTORY}") + target_link_libraries(vamana_build_tsan PRIVATE svs::svs Catch2::Catch2WithMain) + target_link_libraries( + vamana_build_tsan PRIVATE svs_compile_options svs_x86_options_base + ) + target_include_directories(vamana_build_tsan PRIVATE ${PROJECT_SOURCE_DIR}) + add_test(NAME vamana_build_tsan COMMAND vamana_build_tsan) + set_tests_properties(vamana_build_tsan PROPERTIES LABELS "tsan") endif() diff --git a/tests/svs/index/vamana/build_tsan.cpp b/tests/svs/index/vamana/build_tsan.cpp new file mode 100644 index 000000000..52576eb75 --- /dev/null +++ b/tests/svs/index/vamana/build_tsan.cpp @@ -0,0 +1,76 @@ +/* + * Copyright 2026 Intel Corporation + * + * 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. + */ + +// A small graph_max_degree and several threads over few points make it likely that two +// threads touch the same node's list in one add_reverse_edges round. + +// header under test +#include "svs/index/vamana/vamana_build.h" + +#include "svs/core/data.h" +#include "svs/core/distance.h" +#include "svs/core/graph.h" +#include "svs/lib/threads.h" + +// test utils +#include "tests/utils/test_dataset.h" + +// catch2 +#include "catch2/catch_test_macros.hpp" + +// stl +#include +#include + +namespace { +constexpr size_t kPoints = 2000; +constexpr size_t kMaxDegree = 4; +constexpr size_t kWindowSize = 16; +constexpr size_t kMaxCandidatePoolSize = 32; +constexpr size_t kThreads = 8; +} // namespace + +CATCH_TEST_CASE("Static Vamana build under TSan", "[vamana][build][tsan]") { + auto full_data = svs::data::SimpleData::load(test_dataset::data_svs_file()); + const size_t dim = full_data.dimensions(); + + auto data = svs::data::SimpleData(kPoints, dim); + for (size_t i = 0; i < kPoints; ++i) { + data.set_datum(i, full_data.get_datum(i)); + } + + auto graph = svs::graphs::SimpleGraph(kPoints, kMaxDegree); + svs::distance::DistanceL2 distance_function; + auto threadpool = svs::threads::DefaultThreadPool(kThreads); + + svs::index::vamana::VamanaBuildParameters build_params( + 1.2f, kMaxDegree, kWindowSize, kMaxCandidatePoolSize, kMaxDegree, true + ); + + auto builder = svs::index::vamana::VamanaBuilder( + graph, data, distance_function, build_params, threadpool + ); + + uint32_t entry_point = 0; + builder.construct(1.0f, entry_point); + builder.construct(build_params.alpha, entry_point); + + size_t total_edges = 0; + for (size_t i = 0; i < kPoints; ++i) { + total_edges += graph.get_node_degree(i); + } + CATCH_REQUIRE(total_edges > 0); +}