-
Notifications
You must be signed in to change notification settings - Fork 1
perf(gpu): grind the proof-of-work nonce on the GPU #936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| //! GPU proof-of-work grinding: a parallel Keccak nonce search that mirrors the | ||
| //! host `stark::grinding::generate_nonce`, offloading the ~2^grinding_factor | ||
| //! hashes it does per table per epoch from the CPU (where they dominate the | ||
| //! prove) to the otherwise-idle GPU. | ||
|
|
||
| use cudarc::driver::{LaunchConfig, PushKernelArg}; | ||
|
|
||
| use crate::device::backend; | ||
|
|
||
| const BLOCK_DIM: u32 = 256; | ||
| const GRID_DIM: u32 = 1024; | ||
|
|
||
| /// Below this grinding factor the CPU search finds a valid nonce in well under | ||
| /// a microsecond, so a device launch + shared-stream `synchronize` (which also | ||
| /// stalls whatever a rayon peer queued on that stream) is pure loss. Bounce | ||
| /// those to the CPU. The production factor is 20; only tests use tiny factors. | ||
| const GRIND_MIN_FACTOR: u8 = 12; | ||
|
|
||
| /// Smallest nonce whose grind head is `< limit`, or `None` when the CUDA path | ||
| /// is unavailable/errors (the caller then runs the CPU search). | ||
| /// | ||
| /// `inner_lanes` are the four little-endian-read u64 lanes of the 32-byte | ||
| /// inner hash — build them with `stark::grinding::inner_hash_lanes`, which is | ||
| /// what the prover and the tests here both call. `grinding_factor` (1..=64) | ||
| /// fixes `limit = 1 << (64 - grinding_factor)` and sizes the search: the | ||
| /// expected first valid nonce is ~`2^grinding_factor`, so each launch scans a | ||
| /// contiguous block several times that, from 0 upward, and the first block that | ||
| /// hits yields the globally smallest valid nonce (the kernel `atomicMin`s it). | ||
| pub fn generate_nonce_gpu(inner_lanes: &[u64; 4], grinding_factor: u8) -> Option<u64> { | ||
| if !(GRIND_MIN_FACTOR..=64).contains(&grinding_factor) { | ||
| return None; | ||
| } | ||
| let limit: u64 = 1u64 << (64 - grinding_factor); | ||
|
|
||
| let be = backend().ok()?; | ||
| let stream = be.next_stream(); | ||
| let inner_dev = stream.clone_htod(inner_lanes.as_slice()).ok()?; | ||
|
|
||
| // Per-launch block size: ~8× the expected hit distance, clamped so tiny | ||
| // factors still launch a full grid and huge factors don't ask for an | ||
| // absurd single block. `2^grinding_factor` can overflow u64 (factor 64), so | ||
| // saturate. | ||
| let expected = 1u64.checked_shl(grinding_factor as u32).unwrap_or(u64::MAX); | ||
| let count = expected.saturating_mul(8).clamp(1 << 18, 1 << 28); | ||
|
|
||
| let cfg = LaunchConfig { | ||
| grid_dim: (GRID_DIM, 1, 1), | ||
| block_dim: (BLOCK_DIM, 1, 1), | ||
| shared_mem_bytes: 0, | ||
| }; | ||
|
|
||
| // One reusable device slot for the running minimum, reset to the sentinel | ||
| // (U64_MAX) before each block rather than reallocated every iteration. | ||
| // `sentinel` is a named binding so it outlives every async H2D below. | ||
| let sentinel = [u64::MAX]; | ||
| let mut result_dev = stream.clone_htod(&sentinel).ok()?; | ||
|
|
||
| let mut base: u64 = 0; | ||
| loop { | ||
| stream.memcpy_htod(&sentinel, &mut result_dev).ok()?; | ||
| unsafe { | ||
| stream | ||
| .launch_builder(&be.grind_search) | ||
| .arg(&inner_dev) | ||
| .arg(&limit) | ||
| .arg(&base) | ||
| .arg(&count) | ||
| .arg(&mut result_dev) | ||
| .launch(cfg) | ||
| .ok()?; | ||
| } | ||
| let host = stream.clone_dtoh(&result_dev).ok()?; | ||
| stream.synchronize().ok()?; | ||
| if host[0] != u64::MAX { | ||
| return Some(host[0]); | ||
| } | ||
| // Nothing in `[base, base+count)` — advance. Bail (→ CPU fallback) if | ||
| // the block would run past u64, matching the host search's finite range. | ||
| base = base.checked_add(count)?; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| //! The GPU nonce search must produce nonces the host predicate accepts. There | ||
| //! is nothing to compare against the CPU search itself — any nonce satisfying | ||
| //! `is_valid_nonce` is as good as any other, and the CPU's `find_any` does not | ||
| //! even agree with itself between runs — so what is pinned here is validity, | ||
| //! plus the search completeness that minimality stands in for. | ||
| //! | ||
| //! Runs on the merge-queue GPU box via `make test-math-cuda` | ||
| //! (`cargo test -p math-cuda --release`) — `device::backend()` inside | ||
| //! `generate_nonce_gpu` requires a real GPU, like the other tests here. | ||
| //! | ||
| //! Uses real grinding factors (>= the min-factor gate). The end-to-end prover | ||
| //! suite only exercises `grinding_factor: 1`, where `limit = 1 << 63` lets a | ||
| //! broken kernel return an accepted nonce ~half the time; these factors make a | ||
| //! wrong kernel fail deterministically. | ||
| //! | ||
| //! The lanes come from `stark::grinding::inner_hash_lanes`, the same call the | ||
| //! prover makes — building them here instead would leave the production | ||
| //! conversion untested. | ||
|
|
||
| use stark::grinding::{inner_hash_lanes, is_valid_nonce}; | ||
|
|
||
| /// At a moderate factor the kernel returns a valid nonce, and it is the | ||
| /// smallest one (the exhaustive CPU scan below it is cheap at factor 14). | ||
| /// | ||
| /// Minimality is not a contract — any valid nonce would do — but it is a cheap | ||
| /// probe of search completeness: a stride or bounds bug that skipped part of | ||
| /// the range would still return a *valid* nonce, just not the first one, and | ||
| /// plain validity checking would miss that. Deterministic despite the grid | ||
| /// being parallel, because `atomicMin` is an order-independent reduction. If a | ||
| /// future kernel drops minimality deliberately, relax this to validity rather | ||
| /// than treating the red as a defect. | ||
| #[test] | ||
| fn gpu_grind_returns_smallest_valid_nonce() { | ||
| let seed = [14u8; 32]; | ||
| let factor = 14u8; | ||
| let nonce = math_cuda::grinding::generate_nonce_gpu(&inner_hash_lanes(&seed, factor), factor) | ||
| .expect("GPU grind (needs a GPU)"); | ||
| assert!( | ||
| is_valid_nonce(&seed, nonce, factor), | ||
| "GPU nonce {nonce} fails is_valid_nonce (factor {factor})" | ||
| ); | ||
| assert!( | ||
| (0..nonce).all(|n| !is_valid_nonce(&seed, n, factor)), | ||
| "GPU nonce {nonce} is not the smallest valid nonce (factor {factor})" | ||
| ); | ||
| } | ||
|
|
||
| /// At the production factor the kernel returns a valid nonce (validity only — | ||
| /// scanning 0..nonce would be ~2^20 hashes). | ||
| #[test] | ||
| fn gpu_grind_valid_at_production_factor() { | ||
| let seed = [20u8; 32]; | ||
| let factor = 20u8; | ||
| let nonce = math_cuda::grinding::generate_nonce_gpu(&inner_hash_lanes(&seed, factor), factor) | ||
| .expect("GPU grind (needs a GPU)"); | ||
| assert!( | ||
| is_valid_nonce(&seed, nonce, factor), | ||
| "GPU nonce {nonce} fails is_valid_nonce (factor {factor})" | ||
| ); | ||
| } | ||
|
|
||
| /// Below the min-factor gate the GPU path declines (→ CPU search), so the tiny | ||
| /// factors every non-GPU-benchmark test uses never pay a launch. | ||
| #[test] | ||
| fn gpu_grind_declines_below_min_factor() { | ||
| let seed = [1u8; 32]; | ||
| assert!( | ||
| math_cuda::grinding::generate_nonce_gpu(&inner_hash_lanes(&seed, 1), 1).is_none(), | ||
| "GPU grind should decline factor 1" | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.