From 16057a8e2041b46a5c7cf4ca45b84b057cd1e80b Mon Sep 17 00:00:00 2001 From: rob thijssen Date: Sat, 13 Sep 2025 06:24:28 +0300 Subject: [PATCH] Add optional CUDA sampler readback and parity check Enable sampler debugging via MINER_CUDA_SAMPLER env var to verify host/device parity of sampler outputs, logging warnings on mismatch Add example env file for RTX 3060 debug configuration --- .github/workflows/cuda-build.yml | 2 +- crates/engine-gpu-cuda/src/lib.rs | 63 +++++++++++++++++++++++++ examples/.env/cuda-miner-3060-debug.env | 40 ++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 examples/.env/cuda-miner-3060-debug.env diff --git a/.github/workflows/cuda-build.yml b/.github/workflows/cuda-build.yml index 8c0e58e..052cf02 100644 --- a/.github/workflows/cuda-build.yml +++ b/.github/workflows/cuda-build.yml @@ -1,4 +1,4 @@ -name: CUDA Build +name: cuda build on: workflow_dispatch: diff --git a/crates/engine-gpu-cuda/src/lib.rs b/crates/engine-gpu-cuda/src/lib.rs index d7bade0..03d9588 100644 --- a/crates/engine-gpu-cuda/src/lib.rs +++ b/crates/engine-gpu-cuda/src/lib.rs @@ -485,6 +485,69 @@ impl CudaEngine { .with_context(|| "stream synchronize (G2)")?; let kernel_ms = t_kernel_start.elapsed().as_millis(); log::info!(target: "miner", "CUDA G2 kernel and sync OK (kernel_ms={kernel_ms})"); + // Optional sampler readback and host parity check + if std::env::var("MINER_CUDA_SAMPLER") + .map(|v| v == "1" || v.eq_ignore_ascii_case("true")) + .unwrap_or(false) + { + if let ( + Ok(mut c_en), + Ok(mut c_idx), + Ok(mut c_dec), + Ok(mut c_y), + Ok(mut c_h), + Ok(mut c_t), + Ok(mut c_th), + ) = ( + module.get_global::(CString::new("C_SAMPLER_ENABLE")?.as_c_str()), + module.get_global::(CString::new("C_SAMPLER_INDEX")?.as_c_str()), + module.get_global::(CString::new("C_SAMPLER_DECISION")?.as_c_str()), + module.get_global::<[u8; 64]>(CString::new("C_SAMPLER_Y_BE")?.as_c_str()), + module.get_global::<[u8; 64]>(CString::new("C_SAMPLER_H_BE")?.as_c_str()), + module.get_global::<[u8; 64]>( + CString::new("C_SAMPLER_TARGET_BE")?.as_c_str(), + ), + module.get_global::<[u8; 64]>( + CString::new("C_SAMPLER_THRESH_BE")?.as_c_str(), + ), + ) { + let mut en: i32 = 0; + c_en.copy_to(&mut en)?; + if en != 0 { + let mut y_be = [0u8; 64]; + let mut h_be = [0u8; 64]; + let mut t_be = [0u8; 64]; + let mut th_be = [0u8; 64]; + let mut samp_idx: u32 = 0; + let mut samp_dec: u32 = 0; + c_y.copy_to(&mut y_be)?; + c_h.copy_to(&mut h_be)?; + c_t.copy_to(&mut t_be)?; + c_th.copy_to(&mut th_be)?; + c_idx.copy_to(&mut samp_idx)?; + c_dec.copy_to(&mut samp_dec)?; + // Host recompute using pow_core from y + let y_u512 = U512::from_big_endian(&y_be); + let host_distance = pow_core::distance_from_y(ctx, y_u512); + let host_ok = pow_core::is_valid_distance(ctx, host_distance); + let dev_ok = samp_dec != 0; + if host_ok != dev_ok { + log::warn!( + target: "miner", + "CUDA G2 sampler mismatch: idx={}, host_ok={}, dev_ok={}, host_distance={}", + samp_idx, + host_ok, + dev_ok, + host_distance + ); + #[cfg(feature = "metrics")] + { + metrics::inc_sample_mismatch("gpu-cuda"); + } + } + } + } + } // Copy back early-exit flag (and details if found) let mut h_found = [0i32; 1]; diff --git a/examples/.env/cuda-miner-3060-debug.env b/examples/.env/cuda-miner-3060-debug.env new file mode 100644 index 0000000..f6ce442 --- /dev/null +++ b/examples/.env/cuda-miner-3060-debug.env @@ -0,0 +1,40 @@ +MINER_CUDA_SAMPLER=1 + +# NVIDIA RTX 3060 — G2 device path (upper-block example: 56 blocks) +# Launch shape targets ~2× SMs (28 SMs → 56 blocks) with 256 threads/block. +# This configuration uses the G2 kernel (device SHA3 + early-exit). +# +# Notes: +# - Increase/decrease ITERS to balance kernel duration vs early-exit responsiveness. +# - Pinned host buffers are harmless here (G2 doesn’t do large copy-backs) but left enabled. +# - If the embedded G2 kernel isn’t available, the engine will fall back to G1 automatically. +# - Keep your service at --workers 1 to avoid multiple concurrent launches on a single GPU. +# +# Blocks = MINER_CUDA_THREADS / MINER_CUDA_BLOCK_DIM +# For 56 blocks with 256 threads/block: 56 * 256 = 14336 threads. + +# Threads per block (warp multiple; 256 is a solid default) +MINER_CUDA_BLOCK_DIM=256 + +# Total threads = blocks * threads_per_block → 56 * 256 = 14336 (56 blocks) +MINER_CUDA_THREADS=14336 + +# Iterations per thread +# G2 guidance on Ampere 3060: use a longer kernel dwell time here; start around 4096. +MINER_CUDA_ITERS=4096 + +# Use pinned (page-locked) host buffers (harmless in G2; helps if we fall back to G1) +MINER_CUDA_PINNED=1 + +# Select G2 kernel (device SHA3 + threshold + early-exit) +MINER_CUDA_MODE=g2 + +# Optional: force embedded image selection (default prefers cubin) +# MINER_CUDA_IMAGE=cubin +# MINER_CUDA_IMAGE=ptx + +# Optional: host SHA3 workers (only used if engine falls back to G1) +# MINER_CUDA_HASH_THREADS=24 + +# Optional: logging verbosity +# RUST_LOG=info