Abstract
Due to the variety and importance of applications of treecodes and FMM, the combination of algorithmic acceleration with hardware acceleration can have tremendous impact. Alas, programming these algorithms efficiently is no piece of cake. In this contribution, we aim to present GPU kernels for treecode and FMM in, as much as possible, an uncomplicated, accessible way. The interested reader should consult some of the copious literature on the subject for a deeper understanding of the algorithms themselves. Here, we will offer the briefest of summaries. We will focus our attention on achieving a GPU implementation that is efficient in its utilization of the architecture, but without applying the most advanced techniques known in the field (which would complicate the presentation).
Introduction
The N-body problem—calculating the interactions between $N$ particles—is a fundamental challenge in computational science, appearing in fields ranging from astrophysics to molecular biology. A direct "brute-force" approach requires $O(N^2)$ calculations, which quickly becomes computationally prohibitive as $N$ increases. While Graphics Processing Units (GPUs) provide significant acceleration for direct calculations, algorithmic improvements are necessary for large-scale simulations.
Hierarchical algorithms like Treecode and the Fast Multipole Method (FMM) reduce complexity to $O(N \log N)$ and $O(N)$, respectively. These methods group distant particles into clusters, approximating their collective influence. However, mapping these tree-based structures to the rigid, parallel architecture of GPUs is complex. This paper provides a practical guide to implementing these kernels in CUDA, balancing high performance with code readability.
Algorithmic Framework
The core of these fast algorithms lies in a series of translation operators that move information between particles and hierarchical clusters (multipoles and local expansions).
As shown in the figure above, both algorithms utilize Particle-to-Multipole (P2M), Multipole-to-Multipole (M2M), and Particle-to-Particle (P2P) kernels. Treecode relies on Multipole-to-Particle (M2P) interactions for long-range forces, while FMM introduces Local expansions (M2L, L2L, L2P) to further optimize the calculation to linear complexity.
To achieve efficiency on CUDA, the authors implemented several key strategies:
- Computational Tiling: Thread grids handle subsets of interactions to maximize data reuse in shared memory.
- Memory Coalescing: Source particle data is pre-buffered on the CPU into contiguous arrays to ensure efficient global memory access.
- Branching Avoidance: Target buffers are padded to ensure uniform processing across thread blocks, preventing performance degradation from warp divergence.
- Standard Operators: The implementation uses $O(p^4)$ translation operators, prioritizing clarity and maintainability (keeping the CUDA code under 200 lines).
Experimental Evaluation
The performance was evaluated using synthetic N-body systems with $N$ ranging from $10^3$ to $10^7$ particles. Benchmarks were conducted on an NVIDIA 295GTX GPU compared against an Intel Core i7 CPU. Accuracy was maintained at a normalized $L^2$ norm error of $10^{-4}$.
Performance Results
The direct GPU kernel achieved a 300x speedup over a single-core CPU. However, for $10^7$ particles, the hierarchical algorithms on the GPU provided an additional 150x speedup over the direct GPU method.
| Kernel | Performance (Gflop/s) |
|---|---|
| P2P (Particle-to-Particle) | ~300 |
| M2P (Multipole-to-Particle) | ~300 |
| M2L (Multipole-to-Local) | ~100 |
Discussion: Complexity vs. Hardware
A critical insight is the "crossover point" where fast algorithms begin to outperform direct methods. On a CPU, Treecode becomes beneficial at $N \approx 3,000$. On a GPU, due to the extreme efficiency of the direct $O(N^2)$ kernel, this crossover point shifts to $N \approx 20,000$.
Furthermore, while FMM has better theoretical complexity, Treecode's M2P kernel often exhibits superior data-parallelism on GPU hardware. Consequently, for certain ranges of $N$, Treecode may outperform FMM in practice. As GPU kernels accelerate, the primary bottleneck shifts from the math-heavy kernels to "overhead" tasks like tree construction and data sorting, which were performed on the CPU in this study.
Conclusion
This work demonstrates that accessible, simplified GPU implementations of Treecode and FMM can yield massive performance gains. Future work involves moving tree construction entirely to the GPU to alleviate CPU bottlenecks and implementing more advanced $O(p^2)$ or $O(p^3)$ translation operators to improve efficiency for high-precision requirements.
Summary updated