-
Notifications
You must be signed in to change notification settings - Fork 1
Implement AVX2 SIMD optimization #10
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,39 @@ | ||
| #pragma once | ||
| #include <cstddef> | ||
| #include <immintrin.h> | ||
|
|
||
| namespace zenann { | ||
| inline float l2_naive(const float* a, | ||
| const float* b, | ||
| size_t dim) { | ||
| inline float l2_simd(const float* __restrict a, | ||
| const float* __restrict b, | ||
| size_t dim) { | ||
| #if defined(__AVX2__) | ||
| const size_t step = 8; // 8 × 32-bit floats | ||
| __m256 acc = _mm256_setzero_ps(); | ||
| size_t i = 0; | ||
| for (; i + step - 1 < dim; i += step) { | ||
| __m256 va = _mm256_loadu_ps(a + i); | ||
| __m256 vb = _mm256_loadu_ps(b + i); | ||
| __m256 diff = _mm256_sub_ps(va, vb); | ||
| acc = _mm256_fmadd_ps(diff, diff, acc); // acc += diff² | ||
| } | ||
| float buf[step]; | ||
| _mm256_storeu_ps(buf, acc); | ||
| float d = 0.f; | ||
| for (int j = 0; j < step; ++j) d += buf[j]; | ||
|
|
||
| for (; i < dim; ++i) { | ||
| float diff = a[i] - b[i]; | ||
| d += diff * diff; | ||
| } | ||
| return d; | ||
| #else | ||
| float d = 0.f; | ||
| for (size_t i = 0; i < dim; ++i) { | ||
| float diff = a[i] - b[i]; | ||
| d += diff * diff; | ||
| } | ||
| return d; | ||
| #endif | ||
|
Comment on lines
+6
to
+36
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. L2 計算新增 AVX SIMD 版本,如果不支援 AVX2,則會退回原版 |
||
| } | ||
|
|
||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,7 +51,7 @@ SearchResult IVFFlatIndex::search(const Vector& query, size_t k) const { | |
| // Calculate distance from query to all centroids (parallelized) | ||
| #pragma omp parallel for schedule(static) | ||
| for (size_t c = 0; c < nlist_; ++c) { | ||
| float d = l2_naive(query.data(), centroids_[c].data(), dimension_); | ||
| float d = l2_simd(query.data(), centroids_[c].data(), dimension_); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 將既有 |
||
| cdist[c] = {d, c}; | ||
| } | ||
|
|
||
|
|
@@ -77,7 +77,7 @@ SearchResult IVFFlatIndex::search(const Vector& query, size_t k) const { | |
|
|
||
| // Search within this cluster's inverted list | ||
| for (size_t id : lists_[c]) { | ||
| float dist = l2_naive(query.data(), data[id].data(), dimension_); | ||
| float dist = l2_simd(query.data(), data[id].data(), dimension_); | ||
|
|
||
| if (local.size() < k) { | ||
| local.emplace_back(dist, id); | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
增加 -march=native , 啟用 CPU 支援指令集