Skip to main content

vanity_core/gpu/
engine.rs

1//! High-level GPU grind entry points (CPU fallback is the caller's job).
2
3use crate::chain::ChainGrinder;
4use crate::grinder::{CancelToken, GrindResult};
5use crate::pattern::Pattern;
6use crate::system::SystemProfile;
7
8/// How aggressively to prefer the OpenCL path.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
10pub enum GpuMode {
11    /// Never use GPU.
12    #[default]
13    Off,
14    /// Use GPU when built with `opencl`, a device exists, and the chain is supported.
15    Auto,
16    /// Require GPU; error if unavailable / unsupported.
17    Require,
18}
19
20impl GpuMode {
21    pub fn parse(s: &str) -> Result<Self, String> {
22        match s.trim().to_ascii_lowercase().as_str() {
23            "off" | "cpu" | "no" | "false" | "0" => Ok(Self::Off),
24            "auto" | "on" | "yes" | "true" | "1" | "gpu" => Ok(Self::Auto),
25            "require" | "force" | "only" | "gpu-only" | "gpu_only" => Ok(Self::Require),
26            other => Err(format!(
27                "Unknown GPU mode '{other}'. Use: off, auto, require"
28            )),
29        }
30    }
31}
32
33#[derive(Debug, Clone)]
34pub struct GpuGrindReport {
35    pub device_name: String,
36    pub backend: &'static str,
37    pub attempts: u64,
38    pub elapsed_secs: f64,
39}
40
41/// Run a short OpenCL warm-up; `Err` if GPU unavailable.
42pub fn benchmark_gpu(chain_id: &str, duration_secs: f64) -> Result<(f64, String), String> {
43    #[cfg(feature = "opencl")]
44    {
45        crate::gpu::opencl_backend::benchmark(chain_id, duration_secs)
46    }
47    #[cfg(not(feature = "opencl"))]
48    {
49        let _ = (chain_id, duration_secs);
50        Err("this build was compiled without OpenCL (enable the `opencl` feature)".into())
51    }
52}
53
54/// Grind on GPU. On success the keypair has already been CPU-verified.
55pub fn grind_gpu<G: ChainGrinder + Clone + Send + Sync + 'static>(
56    grinder: G,
57    patterns: &[Pattern],
58    _profile: &SystemProfile,
59    cancel: &CancelToken,
60    on_progress: impl Fn(u64, f64, f64) + Sync,
61) -> Result<(GrindResult, GpuGrindReport), String> {
62    #[cfg(feature = "opencl")]
63    {
64        crate::gpu::opencl_backend::grind(grinder, patterns, cancel, on_progress)
65    }
66    #[cfg(not(feature = "opencl"))]
67    {
68        let _ = (grinder, patterns, cancel, on_progress);
69        Err("this build was compiled without OpenCL (enable the `opencl` feature)".into())
70    }
71}