Fast k nearest neighbor search using GPU

Vincent Garcia, Eric Debreuve, Michel BarlaudWorkshops @ CVPR 2008 · DOI

This paper, published in 2008 during the early days of the GPGPU (General-Purpose computing on Graphics Processing Units) revolution, explores how to move one of the most fundamental algorithms in computer science—the k-Nearest Neighbor (kNN) search—from the CPU to the GPU. By leveraging the parallel architecture of modern graphics cards, the authors demonstrate how to overcome long-standing performance bottlenecks in high-dimensional data analysis.

What Problem Does This Paper Address?

The k-Nearest Neighbor (kNN) search is a cornerstone of pattern recognition, used in applications ranging from image classification to statistical entropy estimation. The goal is straightforward: given a "query" point, identify the $k$ closest points within a "reference" dataset.

Conceptual illustration of a k-nearest neighbor search.

Despite its simplicity, kNN is computationally expensive. A "brute-force" approach requires calculating the distance between every query point and every reference point. As the number of points ($N$) or the number of dimensions ($D$) increases, the computation time grows significantly. While specialized data structures like KD-trees were developed to mitigate this, they often struggle in high-dimensional spaces—a phenomenon known as the "curse of dimensionality." The authors address this by utilizing the massive parallel processing power of NVIDIA’s CUDA API.

Methodology

The researchers compared four distinct implementations of the kNN search to evaluate performance across different hardware and software environments:

  1. BF-Matlab: Brute-force search using standard Matlab code.
  2. BF-C: Brute-force search written in C.
  3. KDT-C: A sophisticated KD-tree search using the popular ANN (Approximate Nearest Neighbor) C library.
  4. BF-CUDA: The authors' custom brute-force implementation using NVIDIA’s CUDA.

The BF-CUDA approach is "embarrassingly parallel." Since each distance calculation is independent, the GPU can compute thousands of distances simultaneously. Furthermore, the authors parallelized the sorting process—finding the $k$ smallest distances—which typically represents the second most computationally intensive part of the algorithm.

To rigorously test scalability, the paper utilized synthetic datasets, varying the number of points ($N$) from 1,200 to 38,400 and the dimensionality ($D$) from 8 to 96.

Key Results and Performance Analysis

The quantitative results revealed dramatic speedups, particularly as the complexity of the task increased. The CUDA implementation achieved performance gains of up to 120x over the Matlab version and 100x over the standard C implementation.

Computation time (in seconds) for various kNN implementations across different dimensions (D) and dataset sizes (N).

As shown in the data above, the GPU implementation effectively neutralized the "curse of dimensionality." While CPU-based methods saw execution times climb steeply as $D$ increased, the GPU's performance remained relatively flat because it could simply utilize more of its parallel cores to handle the additional mathematical operations. For a task involving 38,400 points in 96 dimensions, the CPU-based C implementation required approximately one hour, whereas the GPU implementation completed the same task in just 43 seconds.

Insights and Limitations

One of the paper's most striking insights is that "brute force" can be the most efficient strategy when paired with the right architecture. In traditional CPU computing, brute force is often a last resort; however, its inherent parallelizability makes it the optimal choice for GPUs, frequently outperforming complex data structures like KD-trees.

There are, however, trade-offs. The authors noted a "Memory Transfer Tax": for very low-dimensional data ($D=8$) and small datasets, the KD-tree on a CPU was sometimes faster. This is due to the overhead required to move data from the system's RAM to the GPU's dedicated memory. The GPU only becomes the clear winner once the computational load is heavy enough to outweigh this transfer time.

Conclusion

This research demonstrated that GPU acceleration is a game-changer for high-dimensional kNN searches. By providing a massive speedup, this approach enables fields like Content-Based Image Retrieval (CBIR) to utilize more precise and complex descriptors without sacrificing real-time performance. While limited by the GPU memory capacities of the era, the work laid the foundation for modern high-performance machine learning libraries.

Reference: Garcia, V., Debreuve, E., & Barlaud, M. (2008). Fast k nearest neighbor search using GPU. arXiv:0804.1448 [cs.CV].

Summary updated

BibTeX
@inproceedings{garcia2008fast,
  author = {Vincent Garcia and Eric Debreuve and Michel Barlaud},
  title = {{Fast k nearest neighbor search using GPU}},
  booktitle = {Workshops @ CVPR},
  year = {2008},
  doi = {10.1109/cvprw.2008.4563100},
}
Figure