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 27 commits
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
6 changes: 5 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"]
python-version: ["3.7", "3.8", "3.9", "3.10"]
dyashuni marked this conversation as resolved.
Show resolved Hide resolved
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
Expand All @@ -19,6 +19,7 @@ jobs:
run: python -m pip install .

- name: Test
timeout-minutes: 15
run: python -m unittest discover -v --start-directory python_bindings/tests --pattern "*_test*.py"

test_cpp:
Expand Down Expand Up @@ -52,13 +53,16 @@ jobs:
shell: bash

- name: Test
timeout-minutes: 15
run: |
cd build
if [ "$RUNNER_OS" == "Windows" ]; then
cp ./Release/* ./
fi
./searchKnnCloserFirst_test
./searchKnnWithFilter_test
./multiThreadLoad_test
./multiThread_replace_test
./test_updates
./test_updates update
shell: bash
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ var/
.idea/
.vscode/
.vs/
**.DS_Store
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
add_executable(searchKnnWithFilter_test examples/searchKnnWithFilter_test.cpp)
target_link_libraries(searchKnnWithFilter_test hnswlib)

add_executable(multiThreadLoad_test examples/multiThreadLoad_test.cpp)
target_link_libraries(multiThreadLoad_test hnswlib)

add_executable(multiThread_replace_test examples/multiThread_replace_test.cpp)
target_link_libraries(multiThread_replace_test hnswlib)

add_executable(main main.cpp sift_1b.cpp)
target_link_libraries(main hnswlib)
endif()
140 changes: 140 additions & 0 deletions examples/multiThreadLoad_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#include "../hnswlib/hnswlib.h"
#include <thread>
#include <chrono>


int main() {
std::cout << "Running multithread load test" << std::endl;
int d = 16;
int max_elements = 1000;

std::mt19937 rng;
rng.seed(47);
std::uniform_real_distribution<> distrib_real;

hnswlib::L2Space space(d);
hnswlib::HierarchicalNSW<float>* alg_hnsw = new hnswlib::HierarchicalNSW<float>(&space, 2 * max_elements);

std::cout << "Building index" << std::endl;
int num_threads = 40;
int num_labels = 10;

int num_iterations = 10;
int start_label = 0;

// run threads that will add elements to the index
// about 7 threads (the number depends on num_threads and num_labels)
// will add/update element with the same label simultaneously
while (true) {
// add elements by batches
std::uniform_int_distribution<> distrib_int(start_label, start_label + num_labels - 1);
std::vector<std::thread> threads;
for (size_t thread_id = 0; thread_id < num_threads; thread_id++) {
threads.push_back(
std::thread(
[&] {
for (int iter = 0; iter < num_iterations; iter++) {
std::vector<float> data(d);
hnswlib::labeltype label = distrib_int(rng);
for (int i = 0; i < d; i++) {
data[i] = distrib_real(rng);
}
alg_hnsw->addPoint(data.data(), label);
}
}
)
);
}
for (auto &thread : threads) {
thread.join();
}
if (alg_hnsw->cur_element_count > max_elements - num_labels) {
break;
}
start_label += num_labels;
}

// insert remaining elements if needed
for (hnswlib::labeltype label = 0; label < max_elements; label++) {
auto search = alg_hnsw->label_lookup_.find(label);
if (search == alg_hnsw->label_lookup_.end()) {
std::cout << "Adding " << label << std::endl;
std::vector<float> data(d);
for (int i = 0; i < d; i++) {
data[i] = distrib_real(rng);
}
alg_hnsw->addPoint(data.data(), label);
}
}

std::cout << "Index is created" << std::endl;

bool stop_threads = false;
std::vector<std::thread> threads;

// create threads that will do markDeleted and unmarkDeleted of random elements
// each thread works with specific range of labels
std::cout << "Starting markDeleted and unmarkDeleted threads" << std::endl;
num_threads = 20;
int chunk_size = max_elements / num_threads;
for (size_t thread_id = 0; thread_id < num_threads; thread_id++) {
threads.push_back(
std::thread(
[&, thread_id] {
std::uniform_int_distribution<> distrib_int(0, chunk_size - 1);
int start_id = thread_id * chunk_size;
std::vector<bool> marked_deleted(chunk_size);
while (!stop_threads) {
int id = distrib_int(rng);
hnswlib::labeltype label = start_id + id;
if (marked_deleted[id]) {
alg_hnsw->unmarkDelete(label);
marked_deleted[id] = false;
} else {
alg_hnsw->markDelete(label);
marked_deleted[id] = true;
}
}
}
)
);
}

// create threads that will add and update random elements
std::cout << "Starting add and update elements threads" << std::endl;
num_threads = 20;
std::uniform_int_distribution<> distrib_int_add(max_elements, 2 * max_elements - 1);
for (size_t thread_id = 0; thread_id < num_threads; thread_id++) {
threads.push_back(
std::thread(
[&] {
std::vector<float> data(d);
while (!stop_threads) {
hnswlib::labeltype label = distrib_int_add(rng);
for (int i = 0; i < d; i++) {
data[i] = distrib_real(rng);
}
alg_hnsw->addPoint(data.data(), label);
std::vector<float> data = alg_hnsw->getDataByLabel<float>(label);
float max_val = *max_element(data.begin(), data.end());
// never happens but prevents compiler from deleting unused code
if (max_val > 10) {
throw std::runtime_error("Unexpected value in data");
}
}
}
)
);
}

std::cout << "Sleep and continue operations with index" << std::endl;
int sleep_ms = 60 * 1000;
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
stop_threads = true;
for (auto &thread : threads) {
thread.join();
}

std::cout << "Finish" << std::endl;
return 0;
}
121 changes: 121 additions & 0 deletions examples/multiThread_replace_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include "../hnswlib/hnswlib.h"
#include <thread>
#include <chrono>


template<class Function>
inline void ParallelFor(size_t start, size_t end, size_t numThreads, Function fn) {
if (numThreads <= 0) {
numThreads = std::thread::hardware_concurrency();
}

if (numThreads == 1) {
for (size_t id = start; id < end; id++) {
fn(id, 0);
}
} else {
std::vector<std::thread> threads;
std::atomic<size_t> current(start);

// keep track of exceptions in threads
// https://stackoverflow.com/a/32428427/1713196
std::exception_ptr lastException = nullptr;
std::mutex lastExceptMutex;

for (size_t threadId = 0; threadId < numThreads; ++threadId) {
threads.push_back(std::thread([&, threadId] {
while (true) {
size_t id = current.fetch_add(1);

if (id >= end) {
break;
}

try {
fn(id, threadId);
} catch (...) {
std::unique_lock<std::mutex> lastExcepLock(lastExceptMutex);
lastException = std::current_exception();
/*
* This will work even when current is the largest value that
* size_t can fit, because fetch_add returns the previous value
* before the increment (what will result in overflow
* and produce 0 instead of current + 1).
*/
current = end;
break;
}
}
}));
}
for (auto &thread : threads) {
thread.join();
}
if (lastException) {
std::rethrow_exception(lastException);
}
}
}


int main() {
std::cout << "Running multithread load test" << std::endl;
int d = 16;
int num_elements = 1000;
int max_elements = 2 * num_elements;
int num_threads = 50;

std::mt19937 rng;
rng.seed(47);
std::uniform_real_distribution<> distrib_real;

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);
}
float* batch2 = new float[d * num_elements];
for (int i = 0; i < d * num_elements; i++) {
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;
}
std::shuffle(rand_labels.begin(), rand_labels.end(), rng);

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

// add batch1 data
ParallelFor(0, max_elements, num_threads, [&](size_t row, size_t threadId) {
alg_hnsw->addPoint((void*)(batch1 + d * row), row);
});

// delete half random elements of batch1 data
for (int i = 0; i < num_elements; i++) {
alg_hnsw->markDelete(rand_labels[i]);
}

// replace deleted elements with batch2 data
ParallelFor(0, num_elements, num_threads, [&](size_t row, size_t threadId) {
int label = rand_labels[row] + max_elements;
alg_hnsw->addPoint((void*)(batch2 + d * row), label, true);
});

iter += 1;

delete alg_hnsw;
}

std::cout << "Finish" << std::endl;

delete[] batch1;
delete[] batch2;
return 0;
}
2 changes: 1 addition & 1 deletion hnswlib/bruteforce.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class BruteforceSearch : public AlgorithmInterface<dist_t> {
}


void addPoint(const void *datapoint, labeltype label) {
void addPoint(const void *datapoint, labeltype label, bool replace_deleted = false) {
int idx;
{
std::unique_lock<std::mutex> lock(index_lock);
Expand Down
Loading