Skip to content

Commit

Permalink
Fixing threading
Browse files Browse the repository at this point in the history
  • Loading branch information
cjnolet committed Nov 4, 2023
1 parent 2963578 commit cde884d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 6 additions & 3 deletions cpp/bench/ann/src/common/benchmark.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ void bench_search(::benchmark::State& state,
* Make sure the first thread loads the algo and dataset
*/
if (state.thread_index() == 0) {
std::lock_guard lk(init_mutex);
std::unique_lock lk(init_mutex);
// algo is static to cache it between close search runs to save time on index loading
static std::string index_file = "";
if (index.file != index_file) {
Expand Down Expand Up @@ -249,9 +249,11 @@ void bench_search(::benchmark::State& state,
query_set = dataset->query_set(current_algo_props->query_memory_type);
cond_var.notify_all();
} else {
// All other threads will wait for the first thread to initialize the algo.
std::unique_lock lk(init_mutex);
cond_var.wait(lk, [] { return current_algo_props.get() != nullptr; });
// All other threads will wait for the first thread to initialize the algo.

cond_var.wait(
lk, [] { return current_algo_props.get() != nullptr && current_algo.get() != nullptr; });
// gbench ensures that all threads are synchronized at the start of the benchmark loop.
// We are accessing shared variables (like current_algo, current_algo_probs) before the
// benchmark loop, therefore the synchronization here is necessary.
Expand Down Expand Up @@ -292,6 +294,7 @@ void bench_search(::benchmark::State& state,

// advance to the next batch
batch_offset = (batch_offset + n_queries) % query_set_size;

queries_processed += n_queries;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,42 @@
dims: 960
base_file: gist-960-euclidean/base.fbin
query_file: gist-960-euclidean/query.fbin
groundtruth_neighbors_file: gist-960-euclidean/groundtruth.neighbors.ibin
distance: euclidean

- name: glove-50-angular
dims: 50
base_file: glove-50-angular/base.fbin
query_file: glove-50-angular/query.fbin
groundtruth_neighbors_file: glove-50-angular/groundtruth.neighbors.ibin
distance: euclidean

- name: glove-50-inner
dims: 50
base_file: glove-50-inner/base.fbin
query_file: glove-50-inner/query.fbin
groundtruth_neighbors_file: glove-50-inner/groundtruth.neighbors.ibin
distance: euclidean

- name: glove-100-angular
dims: 100
base_file: glove-100-angular/base.fbin
query_file: glove-100-angular/query.fbin
groundtruth_neighbors_file: glove-100-angular/groundtruth.neighbors.ibin
distance: euclidean

- name: glove-100-inner
dims: 100
base_file: glove-100-inner/base.fbin
query_file: glove-100-inner/query.fbin
groundtruth_neighbors_file: glove-100-inner/groundtruth.neighbors.ibin
distance: euclidean

- name: lastfm-65-angular
dims: 65
base_file: lastfm-65-angular/base.fbin
query_file: lastfm-65-angular/query.fbin
groundtruth_neighbors_file: lastfm-65-angular/groundtruth.neighbors.ibin
distance: euclidean

- name: mnist-784-euclidean
Expand Down

0 comments on commit cde884d

Please sign in to comment.