use crate::chain::ChainGrinder;
use crate::grinder::{CancelToken, GrindResult};
use crate::pattern::Pattern;
use crate::system::SystemProfile;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum GpuMode {
#[default]
Off,
Auto,
Require,
}
impl GpuMode {
pub fn parse(s: &str) -> Result<Self, String> {
match s.trim().to_ascii_lowercase().as_str() {
"off" | "cpu" | "no" | "false" | "0" => Ok(Self::Off),
"auto" | "on" | "yes" | "true" | "1" | "gpu" => Ok(Self::Auto),
"require" | "force" | "only" | "gpu-only" | "gpu_only" => Ok(Self::Require),
other => Err(format!(
"Unknown GPU mode '{other}'. Use: off, auto, require"
)),
}
}
}
#[derive(Debug, Clone)]
pub struct GpuGrindReport {
pub device_name: String,
pub backend: &'static str,
pub attempts: u64,
pub elapsed_secs: f64,
}
pub fn benchmark_gpu(chain_id: &str, duration_secs: f64) -> Result<(f64, String), String> {
#[cfg(feature = "opencl")]
{
crate::gpu::opencl_backend::benchmark(chain_id, duration_secs)
}
#[cfg(not(feature = "opencl"))]
{
let _ = (chain_id, duration_secs);
Err("this build was compiled without OpenCL (enable the `opencl` feature)".into())
}
}
pub fn grind_gpu<G: ChainGrinder + Clone + Send + Sync + 'static>(
grinder: G,
patterns: &[Pattern],
_profile: &SystemProfile,
cancel: &CancelToken,
on_progress: impl Fn(u64, f64, f64) + Sync,
) -> Result<(GrindResult, GpuGrindReport), String> {
#[cfg(feature = "opencl")]
{
crate::gpu::opencl_backend::grind(grinder, patterns, cancel, on_progress)
}
#[cfg(not(feature = "opencl"))]
{
let _ = (grinder, patterns, cancel, on_progress);
Err("this build was compiled without OpenCL (enable the `opencl` feature)".into())
}
}