CUDA Acceleration

7. Advanced: CUDA Acceleration

One of PolySolve's most powerful features is its ability to offload the genetic algorithm's fitness calculations to a compatible NVIDIA GPU. This can result in dramatic speedups when using a large data_size in your GA_Options.

This feature is ideal for modern NVIDIA GPUs (like the RTX 30 and 40 series). To use it, simply pass the use_cuda=True flag to the solver method.

# A high-degree polynomial that would be slow to solve on CPU
f_hard = Function(10)
f_hard.set_coeffs([1, 0, -45, 0, 210, 0, -420, 0, 315, 0, -1])

# Use a large data size to leverage the GPU's parallelism
ga_opts_gpu = GA_Options(data_size=2_000_000, num_of_generations=20)

# Run the solver with CUDA enabled
gpu_roots = f_hard.get_real_roots(options=ga_opts_gpu, use_cuda=True)

If use_cuda is set to True but PolySolve cannot detect a valid CuPy installation, it will fall back to the NumPy (CPU) implementation and issue a UserWarning.