This is a C++ vector search library currently implementing exact brute-force
Building secan requires CMake 3.15+ and a C++20-compliant compiler.
# Configure build
cmake -B build
# Build library, executable, and tests
cmake --build build
# Run test suite
ctest --test-dir build --output-on-failuresecan operates on flat contiguous row-major vector datasets and query vectors.
#include <iostream>
#include <vector>
#include "secan/search/search.h"
int main() {
// 3 vectors of dimension 2 (flat row-major layout: N * dim)
std::vector<float> dataset = {
1.0f, 2.0f,
3.0f, 4.0f,
5.0f, 6.0f
};
std::vector<float> query = {3.0f, 4.0f};
int top_k = 2;
// Search using "l2" (squared L2) or "cosine" distance
std::vector<SearchResult> results = linear_scan(dataset, query, top_k, "l2");
for (const auto &result : results) {
std::cout << "Index: " << result.index
<< ", Distance: " << result.distance << "\n";
}
return 0;
}Link against secan_lib in your CMakeLists.txt:
target_link_libraries(your_target PRIVATE secan_lib)Microbenchmarks are implemented using Google Benchmark v1.9.0 with memory clobber barriers (benchmark::DoNotOptimize) to prevent compiler dead-code elimination.
# Build and run microbenchmarks (Release mode required)
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j$(nproc)
./build/benchmarks/bench_distanceBenchmarked on AMD Zen 4 Hawk Point 12-Core @ 4.30 GHz (L1D 32 KiB, L2 1 MiB, L3 16 MiB). Compiler: Release -O3 -mavx2 -mfma -mavx512f -mavx512dq -mavx512bw -mavx512vl -DNDEBUG.
| Vector Dimension ( |
Workload / Embedding Model | Scalar Baseline | AVX2 Single (1-acc) | AVX2 Unroll-4 (4-acc) | AVX-512 Dual (2-acc) | Max Speedup | Peak Bandwidth |
|---|---|---|---|---|---|---|---|
| Micro Embeddings / Image Hashes | |||||||
| SIFT1M / Audio Features | |||||||
| Compact Dense Representations | |||||||
| Small Language Embeddings | |||||||
BERT / all-mpnet-base-v2
|
|||||||
| BGE-Large / Large Text Embeddings | |||||||
OpenAI text-embedding-3-small/large
|
| Vector Dimension ( |
Scalar Baseline | AVX2 Unroll-4 | AVX-512 Dual (2-acc) | Max Speedup | Peak Bandwidth |
|---|---|---|---|---|---|
| Kernel | Dimension ( |
Latency (ns) | IPC | L1D Miss Rate | Branch Miss Rate | Throughput (GiB/s) |
|---|---|---|---|---|---|---|
Scalar l2_squared |
128 | 59.0 ns | 2.18 | 0.007% | 0.003% | 16.16 GiB/s |
AVX2 Single l2_squared |
128 | 7.64 ns | 1.85 | 0.005% | 0.001% | 124.80 GiB/s |
AVX2 Unroll-4 l2_squared |
128 | 5.12 ns | 3.42 | 0.004% | 0.001% | 187.97 GiB/s |
AVX-512 Dual l2_squared |
128 | 4.64 ns | 3.65 | 0.003% | 0.001% | 206.22 GiB/s |
Concurrently computes $\sum a_i b_i$, $\sum a_i^2$, and $\sum b_i^2$ in a single SIMD pass, eliminating redundant memory round-trips and reducing cache line traffic by $66%$.
| Vector Dimension ( |
Workload / Embedding Model | Scalar Cosine | Fast Reciprocal Cosine | Fused AVX2 Cosine | Speedup | Peak Bandwidth |
|---|---|---|---|---|---|---|
| Micro Embeddings / Image Hashes | ||||||
| SIFT1M / Audio Features | ||||||
| Compact Dense Representations | ||||||
| Small Language Embeddings | ||||||
BERT / all-mpnet-base-v2
|
||||||
| BGE-Large / Large Text Embeddings | ||||||
OpenAI text-embedding-3-small/large
|
- Scalar Baseline Distance Kernels (L2, IP, Cosine, Fast Reciprocal Cosine)
- Hardware Floating-Point State Control (FTZ/DAZ)
- AVX2 + FMA Single-Accumulator Distance Kernel
- AVX2 Multi-Accumulator ILP Unrolling (4-way register parallelism)
- Fused 1-Pass AVX2 Cosine Distance Kernel (66% cache bus traffic reduction)
- AVX-512 Distance Kernels (512-bit ZMM dual-accumulator unrolling)
- Cache-aware memory layout & blocked matrix scans
- Multithreaded concurrent query engine
- HNSW graph indexing for sub-millisecond approximate nearest neighbor search
MIT License. Copyright (c) 2026 Adheeb Ahmed.