Skip to content

Unify use of OpenMP for HNSW threading models #724

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

Open
wants to merge 10 commits into
base: branch-25.06
Choose a base branch
from

Conversation

divyegala
Copy link
Member

@divyegala divyegala commented Feb 25, 2025

cuvs_bench hnswlib wrapper was using a custom threading pool while cuvs hnsw wrapper was using OpenMP for parallelism. This was causing unexpected deviations in measured timings.

Furthermore, the default for # of threads in cuvs_bench hnswlib wrapper search params was 1, which is incorrect.

@divyegala divyegala added improvement Improves an existing functionality non-breaking Introduces a non-breaking change labels Feb 25, 2025
@divyegala divyegala self-assigned this Feb 25, 2025
@divyegala divyegala requested review from a team as code owners February 25, 2025 20:43
Copy link
Contributor

@achirkin achirkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, very welcome change, but I have a few questions to make sure it works as intended.

  • I don't see any changes to CMakeLists. Is the benchmark executable already linked with openmp?
  • Can we now remove cpp/bench/ann/src/common/thread_pool.hpp completely?

@@ -66,13 +66,13 @@ class hnsw_lib : public algo<T> {
struct build_param {
int m;
int ef_construction;
int num_threads = omp_get_num_procs();
int num_threads = omp_get_max_threads();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the consequences of switching from omp_get_num_procs() to omp_get_max_threads() with respect to the SMT/hyperthreading?
Does hyperthreading make HNSW slower or faster?
Also, does OpenMP take into account whether the number of cores is limited by numactl?

Copy link
Member Author

@divyegala divyegala Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answering your questions one by one:

I don't see any changes to CMakeLists. Is the benchmark executable already linked with openmp?

All benchmark executables link to OpenMP already.

Can we now remove cpp/bench/ann/src/common/thread_pool.hpp completely?

No, unfortunately the FAISS wrappers still use it.

What are the consequences of switching from omp_get_num_procs() to omp_get_max_threads() with respect to the SMT/hyperthreading?

omp_get_num_procs() only returns number of physical cores. The difference in search times are very visibly apparent when accounting for thread usage to be all available hyperthreads vs just using physical cores. It makes HNSW faster.

Also, does OpenMP take into account whether the number of cores is limited by numactl?

No, it does not. We would either have to write a custom implementation or use some thread-pool library that can account for this. I think it would fall out of the scope of this PR and would be a general design philosophy discussion that we would have to undertake with the team, as we use OpenMP in quite a lot of places outside of HNSW.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the answers!

The difference in search times are very visibly apparent when accounting for thread usage to be all available hyperthreads vs just using physical cores. It makes HNSW faster.

Could you please add this to the comment above where we set this default?

Can we now remove cpp/bench/ann/src/common/thread_pool.hpp completely?

No, unfortunately the FAISS wrappers still use it

Would it be worth the effort to adjust FAISS wrappers to use OpenMP in this PR as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth the effort to adjust FAISS wrappers to use OpenMP in this PR as well?

I believe @tarang-jain is reworking the FAISS wrappers so we can do that as a follow-on to his PR

Copy link
Contributor

@tfeher tfeher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Divye, the PR looks good to me!

@divyegala divyegala changed the base branch from branch-25.04 to branch-25.06 April 3, 2025 21:03
@@ -66,13 +67,13 @@ class hnsw_lib : public algo<T> {
struct build_param {
int m;
int ef_construction;
int num_threads = omp_get_num_procs();
int num_threads = omp_get_max_threads();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename num_threads to something else (say build_num_threads or nthreads). The problem arises when trying to data_export. The build dataframe columns are appended at the end of the search dataframe. We want this param to be appended as a separate column at the end of the combined dataframe but currently there is a naming clash because the search params also have num_threads.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tarang-jain the problem you're talking about is in the python post-processing, right? This sounds like a fragile setup, as nothing prevents anyone from adding more algorithms with the same build/search parameters in future. Perhaps, it would be better to solve this problem for good where it occurs? You could either join the columns, or prepend build_/search_ to their names, or just not use names as keys (allow duplicates).
I also think it would be a bit confusing for the user to having differentiate between the two.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right! It occurs in the Python post-processing. Prepending build_/search_ to the names seems like the easiest thing to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cpp improvement Improves an existing functionality non-breaking Introduces a non-breaking change Python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants