diff --git a/crates/engine-gpu-cuda/src/kernels/qpow_kernel.cu b/crates/engine-gpu-cuda/src/kernels/qpow_kernel.cu index 3590170..96fb1de 100644 --- a/crates/engine-gpu-cuda/src/kernels/qpow_kernel.cu +++ b/crates/engine-gpu-cuda/src/kernels/qpow_kernel.cu @@ -336,6 +336,8 @@ __device__ uint8_t C_SAMPLER_TARGET_BE[64]; __device__ uint8_t C_SAMPLER_THRESH_BE[64]; __device__ uint32_t C_SAMPLER_INDEX; __device__ uint32_t C_SAMPLER_DECISION; +__device__ uint32_t C_WIN_TID; +__device__ uint32_t C_WIN_J; // Load/store helpers (little- and big-endian) __device__ __forceinline__ uint64_t load64_le(const uint8_t* p) { @@ -675,6 +677,9 @@ for (uint32_t j = 0; j < iters; ++j) { if (out_index) { *out_index = tid * iters + j; } + // Record winner thread and iteration for host-side nonce reconstruction + C_WIN_TID = tid; + C_WIN_J = j; // Write distance and debug buffers (if provided) if (out_distance_be) { #pragma unroll diff --git a/crates/engine-gpu-cuda/src/lib.rs b/crates/engine-gpu-cuda/src/lib.rs index 773ee1d..c01d8af 100644 --- a/crates/engine-gpu-cuda/src/lib.rs +++ b/crates/engine-gpu-cuda/src/lib.rs @@ -562,8 +562,24 @@ impl CudaEngine { hash_count = hash_count.saturating_add(k + 1); // Compute nonce from linear index (t, j) - let t_idx = (k as usize) / (iters_per_thread as usize); - let j_idx = (k as usize) % (iters_per_thread as usize); + let mut t_idx = (k as usize) / (iters_per_thread as usize); + let mut j_idx = (k as usize) % (iters_per_thread as usize); + // If the device exported winner coordinates, prefer them + if let (Ok(win_tid_sym), Ok(win_j_sym)) = ( + module.get_global::(std::ffi::CString::new("C_WIN_TID")?.as_c_str()), + module.get_global::(std::ffi::CString::new("C_WIN_J")?.as_c_str()), + ) { + let mut dev_tid: u32 = u32::MAX; + let mut dev_j: u32 = u32::MAX; + let _ = win_tid_sym.copy_to(&mut dev_tid); + let _ = win_j_sym.copy_to(&mut dev_j); + if (dev_tid as usize) < (num_threads as usize) + && (dev_j as usize) < (iters_per_thread as usize) + { + t_idx = dev_tid as usize; + j_idx = dev_j as usize; + } + } // Reconstruct nonce using the exact per-thread base nonce used for y0: // nonce = base_nonces[t_idx] + (j_idx + 1) let base_nonce = base_nonces[t_idx];