Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deleted elements at addition #418

Merged
merged 28 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/multiThread_replace_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ int main() {

hnswlib::L2Space space(d);

// generate batch1 and batch2 data
float* batch1 = new float[d * max_elements];
for (int i = 0; i < d * max_elements; i++) {
batch1[i] = distrib_real(rng);
Expand All @@ -80,6 +81,7 @@ int main() {
batch2[i] = distrib_real(rng);
}

// generate random labels to delete them from index
std::vector<int> rand_labels(max_elements);
for (int i = 0; i < max_elements; i++) {
rand_labels[i] = i;
Expand All @@ -92,16 +94,19 @@ int main() {

hnswlib::HierarchicalNSW<float>* alg_hnsw = new hnswlib::HierarchicalNSW<float>(&space, max_elements, 16, 200, 123, true);

// add batch1 data
std::cout << "Building index\n";
ParallelFor(0, max_elements, num_threads, [&](size_t row, size_t threadId) {
alg_hnsw->addPoint((void*)(batch1 + d * row), row);
});

// delete batch1 data
std::cout << "Deleting\n";
for (int i = 0; i < num_elements; i++) {
alg_hnsw->markDelete(rand_labels[i]);
}

// replace batch1 data with batch2 data
std::cout << "Updating elements\n";
ParallelFor(0, num_elements, num_threads, [&](size_t row, size_t threadId) {
int label = rand_labels[row] + max_elements;
Expand Down
4 changes: 4 additions & 0 deletions hnswlib/hnswalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
tableint next_closest_entry_point = selectedNeighbors.back();

{
std::unique_lock <std::mutex> lock(link_list_locks_[cur_c], std::defer_lock);
dyashuni marked this conversation as resolved.
Show resolved Hide resolved
if (isUpdate) {
lock.lock();
}
linklistsizeint *ll_cur;
if (level == 0)
ll_cur = get_linklist0(cur_c);
Expand Down