https://github.com/lazear/simd-euclidean
Calculation of euclidean distance between vectors, with SIMD
https://github.com/lazear/simd-euclidean
algorithm clustering euclidean-distances machine-learning rust simd
Last synced: about 16 hours ago
JSON representation
Calculation of euclidean distance between vectors, with SIMD
- Host: GitHub
- URL: https://github.com/lazear/simd-euclidean
- Owner: lazear
- License: mit
- Created: 2019-04-12T01:15:36.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-01-17T02:34:17.000Z (about 2 years ago)
- Last Synced: 2024-01-17T09:40:21.799Z (about 2 years ago)
- Topics: algorithm, clustering, euclidean-distances, machine-learning, rust, simd
- Language: Rust
- Size: 28.3 KB
- Stars: 9
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# simd-euclidean
Blazing fast euclidean distance implementation, utilizing LLVM and Rust's auto-vectorization.
For vectors >= 32 elements, the SIMD-enabled algorithm is 2 to 8 times faster, with longer inputs provided greater speedups.
Two traits are exposed by this library, `Naive` and `Vectorized`, which both provide a `squared_distance` and `distance` function.
```
// Vectorized::distance will dispatch to Naive::distance for an input of this size
let v = Vectorized::distance(&[0.1, 0.2, 0.3, 0.4f32], &[0.4, 0.3, 0.2, 0.1f32]);
let n = Naive::distance(&[0.1, 0.2, 0.3, 0.4f32], &[0.4, 0.3, 0.2, 0.1f32]);
assert!((n-v).abs() < 0.00001);
for &i in [16, 32, 64, 128].into_iter() {
// Dispatch to F32x4 or F32x8 (above 64 elements)
let mut rng = rand::thread_rng();
let a = (0..i).map(|_| rng.gen::()).collect::>();
let b = (0..i).map(|_| rng.gen::()).collect::>();
let v = Vectorized::distance(&a, &b);
let n = Naive::distance(&a, &b);
assert!((n-v).abs() < 0.00001);
}
```
The `Vectorized` trait attempts to heuristically determine which SIMD layout (F32x4, F32x8, etc) will be fastest with the given input size.
Shown below is a comparison between `Naive::distance` and `Vectorized::distance` on random vectors of single precision floating point numbers.

And for double precision `f64` vectors:
