Add OpenMP parallelization to IVFFlatIndex#5
Merged
5000user5000 merged 1 commit intomainfrom Nov 6, 2025
Merged
Conversation
5000user5000
commented
Oct 29, 2025
Owner
Author
5000user5000
left a comment
There was a problem hiding this comment.
Modified Files
-
src/IVFFlatIndex.cpp
- Added
#include <omp.h>header - Parallelized centroid distance calculation with
#pragma omp parallel for schedule(static) - Parallelized list probing using thread-local heaps with
schedule(dynamic)and#pragma omp criticalfor merging - Parallelized batch search with
schedule(dynamic)across multiple queries - Added clear comments explaining parallelization strategy
- Added
-
Makefile
- Added
-fopenmpflag toCXXFLAGSfor compilation - Added
-fopenmptoLDFLAGSfor linking
- Added
Parallelization Strategy
-
Centroid Distance Calculation
- Uses
schedule(static)for balanced workload distribution - Parallel computation of L2 distance to all centroids
- Uses
-
List Probing
- Uses
schedule(dynamic)to handle variable cluster sizes - Each thread maintains a local heap to avoid contention
- Results merged into global heap via
#pragma omp critical
- Uses
-
Batch Search
- Uses
schedule(dynamic)for load balancing across queries - Each query processed independently in parallel
- Uses
| @@ -1,5 +1,5 @@ | |||
| CXX := g++ | |||
| CXXFLAGS := -std=c++17 -O3 -fPIC | |||
| CXXFLAGS := -std=c++17 -O3 -fPIC -fopenmp | |||
Owner
Author
There was a problem hiding this comment.
added -fopenmp for compilation
Comment on lines
+43
to
+44
| # Add OpenMP linking | ||
| LDFLAGS += -fopenmp |
Owner
Author
There was a problem hiding this comment.
added -fopenmp for linking
Comment on lines
+52
to
56
| #pragma omp parallel for schedule(static) | ||
| for (size_t c = 0; c < nlist_; ++c) { | ||
| float d = l2_naive(query.data(), centroids_[c].data(), dimension_); | ||
| cdist[c] = {d, c}; | ||
| } |
Owner
Author
There was a problem hiding this comment.
質心 query 分給多個 threads
Comment on lines
-71
to
+92
| if (heap.size() < k) { | ||
| heap.emplace_back(dist, id); | ||
| if (heap.size() == k) | ||
| std::make_heap(heap.begin(), heap.end()); | ||
| } else if (dist < heap.front().first) { | ||
| std::pop_heap(heap.begin(), heap.end()); | ||
| heap.back() = {dist, id}; | ||
| std::push_heap(heap.begin(), heap.end()); | ||
| if (local.size() < k) { | ||
| local.emplace_back(dist, id); | ||
| if (local.size() == k) { | ||
| std::make_heap(local.begin(), local.end()); | ||
| } | ||
| } else if (dist < local.front().first) { | ||
| std::pop_heap(local.begin(), local.end()); | ||
| local.back() = {dist, id}; | ||
| std::push_heap(local.begin(), local.end()); | ||
| } | ||
| } |
Owner
Author
There was a problem hiding this comment.
Search within this cluster's inverted list , 將原本 heap, 改成讓 openMP 每個 thread 各自有自己的 heap (local)
Comment on lines
+94
to
+107
| // Merge local results into global heap (thread-safe) | ||
| #pragma omp critical | ||
| { | ||
| for (auto& p : local) { | ||
| if (heap.size() < k) { | ||
| heap.push_back(p); | ||
| if (heap.size() == k) { | ||
| std::make_heap(heap.begin(), heap.end()); | ||
| } | ||
| } else if (p.first < heap.front().first) { | ||
| std::pop_heap(heap.begin(), heap.end()); | ||
| heap.back() = p; | ||
| std::push_heap(heap.begin(), heap.end()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds OpenMP-based multi-threading parallelization to
IVFFlatIndex, significantly improving search performance for approximate nearest neighbor queries.Modified Files
src/IVFFlatIndex.cpp
Added
#include <omp.h>Applied OpenMP parallelization:
schedule(static)schedule(dynamic)with thread-local heaps and#pragma omp criticalfor mergingschedule(dynamic)for parallel query processingAdded comments explaining parallelization
Makefile
-fopenmpto bothCXXFLAGSandLDFLAGSParallelization Overview
Related Issue