vanity_core/gpu/
engine.rs1use crate::chain::ChainGrinder;
4use crate::grinder::{CancelToken, GrindResult};
5use crate::pattern::Pattern;
6use crate::system::SystemProfile;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
10pub enum GpuMode {
11 #[default]
13 Off,
14 Auto,
16 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
41pub 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
54pub 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}