Backends and performance¶
All backends compute the same field, up to floating-point rounding; this is
enforced by the test-suite. Choose one with generate(..., backend="fft") or
nanofoams generate --backend fft.
| name | device | algorithm | cost |
|---|---|---|---|
direct |
CPU | the formula, term by term (reference) | atoms × \(N\) |
collapsed |
CPU | exact phase collapse | atoms × \(n_\text{dir}\) |
fft (default) |
CPU | inverse FFT on the lattice | \(p^3 \log p\) per sublattice |
cupy |
GPU | collapse / FFT with CuPy | as above |
Phase collapse¶
All waves that share a direction \(\mathbf{q}_d\) add up to a single wave:
This is an identity, not an approximation. With 10 000 waves on 48 directions it removes a factor of ~200 from the work.
FFT on the lattice¶
A sublattice site is \(\mathbf{x} = a_0(\mathbf{n} + \mathbf{b})\), with integer \(\mathbf{n}\) and basis offset \(\mathbf{b}\), and the field period is \(p\) unit cells. Then \(\mathbf{q}_d\cdot\mathbf{x} = 2\pi\,\mathbf{d}\cdot(\mathbf{n}+\mathbf{b})/p\), and the field on the whole sublattice is one inverse 3-D DFT of a \(p^3\) grid of coefficients. The result is exact for any \(p\): directions whose indices alias onto the same grid point just add up. A bcc lattice is two sublattices, fcc four.
Peak memory is about \(16\,p^3\) bytes for the FFT grid plus 8 bytes per atom for the result (plus the positions, 24 bytes per atom).
Benchmarks¶
Field evaluation only, on the laptop used for the thesis benchmarks (AMD Ryzen 3
4300U, 4 cores), with benchmarks/run_benchmarks.py:
| sample | thesis code | direct |
collapsed |
fft |
|---|---|---|---|---|
| sc, 100³ cells, \(H^2 = 54\), \(N = 2500\) (1 M atoms) | 2537 s (1 core), 820 s (4 cores), 64 s (Tesla K80) | 72 s* | 1.6 s | 0.07 s |
| bcc, 300³ cells, \(H^2 = 161\), \(N = 10^4\) (54 M atoms) | ~32 h (CPU node), ~2.25 h (Tesla K80) | 2.7 h* | 112 s* | 2.6 s |
* extrapolated linearly from a subset of atoms.
Writing the output usually takes longer than generating the foam: about 1 s per 330 000 atoms for a data file.
GPU¶
$ pip install "nanofoams[gpu]"
$ nanofoams generate foam.toml --backend cupy
The GPU backend runs the same kernels as the CPU ones and returns NumPy arrays. With the FFT path taking seconds on a CPU, a GPU mostly pays off for very large boxes.
CUDA toolkit¶
CuPy compiles its kernels at run time, so besides an NVIDIA driver it needs the
CUDA headers and NVRTC. If the CUDA toolkit is not installed on the machine
(common on Windows and on laptops), the first GPU call fails with
RuntimeError: Failed to find CUDA headers. Install the toolkit as pip wheels
instead, matching the CuPy version:
$ pip install "cupy-cuda12x[ctk]"
With the wheels, CuPy may still warn CUDA path could not be detected on
import; the warning is harmless. A quick check that the GPU works:
$ python -c "import cupy; print((cupy.arange(3.0) ** 2).sum())"
5.0
The cupy-cuda12x wheels also work with newer drivers (e.g. a driver that
reports CUDA 13): the driver is backward compatible.
Testing the GPU backend¶
The GPU tests (tests/test_gpu.py and the cupy cases in
tests/test_backends.py) are skipped unless CuPy can actually run a kernel on a
device, so CI (which has no GPU) skips them. Run them locally on a machine with
an NVIDIA GPU:
$ pip install -e ".[dev,gpu]"
$ pytest tests/test_gpu.py -v
They check the GPU against the thesis fixtures, the CPU backends (points,
lattices with heavy aliasing, generate, the CLI) and that no deprecated CuPy
API is used. Last verified with CuPy 14.2 on a GeForce GTX 1660 SUPER.