From 1fea51cfc3786036f0cfefbb53368dd23a18f02d Mon Sep 17 00:00:00 2001 From: rob thijssen Date: Mon, 14 Sep 2026 19:51:22 +0300 Subject: [PATCH] engine-cuda: loop kernel takes the diagonal from the launch parameters MiningUniforms carries the twelve diagonal constants (416 bytes now) and the loop kernel reads them there (LAIR_LOOP_PARAM_DIAG=1) instead of from the __constant__ table. Static: still 13 loads per internal round (ptxas reloads either way) but spill 88 -> 32 bytes. Measured, three interleaved rounds, loop kernel only: beast 2135 -> 2162 MH/s (+1.3%), benjy 725 -> 730 (+0.7%); parity 300/300 and 200/200. The unrolled kernel is byte- identical (21,120 instructions, 110 registers). Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01Ue5ZZm1Hiv5zPnucykKKuF --- crates/engine-cuda/src/kernels/mining.cu | 35 +++++++++++++++++------- crates/engine-cuda/src/lib.rs | 9 ++++-- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/crates/engine-cuda/src/kernels/mining.cu b/crates/engine-cuda/src/kernels/mining.cu index bedcdeb..0d527aa 100644 --- a/crates/engine-cuda/src/kernels/mining.cu +++ b/crates/engine-cuda/src/kernels/mining.cu @@ -353,6 +353,11 @@ __device__ __forceinline__ u64 gf_canon(u64 a) { #else #define LAIR_LOOP_LAUNCH_BOUNDS __launch_bounds__(LAIR_LOOP_TPB) #endif +// Loop kernel: take the diagonal from the launch parameters instead of the +// __constant__ table (see MiningUniforms::diag). +#ifndef LAIR_LOOP_PARAM_DIAG +#define LAIR_LOOP_PARAM_DIAG 1 +#endif // Internal rounds per loop iteration in the loop kernel (1 or 2). One // measured 2105 MH/s against 1976 for two on beast: the pair doubles the // live state across the iteration for no scheduling gain. @@ -876,7 +881,7 @@ __device__ __forceinline__ void permute(u64 st[12], bool skip_last_ext = false, // each round stay unrolled so the state lives in registers. #if LAIR_LAZY_ADD && LAIR_FOLD_RC static_assert(N_INTERNAL % 2 == 0, "the paired internal loop assumes an even round count"); -__device__ __forceinline__ void permute_loop(u64 st[12], bool skip_last_ext, bool skip_first_ext) { +__device__ __forceinline__ void permute_loop(u64 st[12], bool skip_last_ext, bool skip_first_ext, const u64* diag) { if (!skip_first_ext) ext_layer_rc(st, RC_INITIAL[0]); #pragma unroll 1 for (int r = 0; r + 1 < N_EXTERNAL_HALF; r++) { @@ -888,7 +893,12 @@ __device__ __forceinline__ void permute_loop(u64 st[12], bool skip_last_ext, boo for (int i = 0; i < 12; i++) st[i] = gf_sbox(st[i]); ext_layer(st); st[0] = gf_add(st[0], RC_INTERNAL[0]); +#if LAIR_LOOP_PARAM_DIAG + const u64* d = diag; +#else const u64* d = MDS_DIAG; + (void)diag; +#endif #if LAIR_LOOP_INT_PAIR == 2 #pragma unroll 1 for (int r = 0; r + 2 < N_INTERNAL; r += 2) { @@ -919,14 +929,15 @@ __device__ __forceinline__ void permute_loop(u64 st[12], bool skip_last_ext, boo if (!skip_last_ext) ext_layer(st); } #else -__device__ __forceinline__ void permute_loop(u64 st[12], bool skip_last_ext, bool skip_first_ext) { +__device__ __forceinline__ void permute_loop(u64 st[12], bool skip_last_ext, bool skip_first_ext, const u64* diag) { + (void)diag; permute(st, skip_last_ext, skip_first_ext); } #endif template -__device__ __forceinline__ void run_permute(u64 st[12], bool skip_last_ext = false, bool skip_first_ext = false) { - if (LOOP) permute_loop(st, skip_last_ext, skip_first_ext); +__device__ __forceinline__ void run_permute(u64 st[12], const u64* diag, bool skip_last_ext = false, bool skip_first_ext = false) { + if (LOOP) permute_loop(st, skip_last_ext, skip_first_ext, diag); else permute(st, skip_last_ext, skip_first_ext); } @@ -954,8 +965,12 @@ struct MiningUniforms { // State after the first external layer + its round constant for the // batch's first nonce (host: first_layer_after_absorb). u64 layer0_base[12]; + // The internal layer's diagonal, same values as MDS_DIAG, for the loop + // kernel: a parameter-bank operand costs no load and no register, where + // ptxas reloaded the __constant__ table every round. + u64 diag[12]; }; -static_assert(sizeof(MiningUniforms) == 320, "MiningUniforms must match the host layout"); +static_assert(sizeof(MiningUniforms) == 416, "MiningUniforms must match the host layout"); #if LAIR_NONCE_DIR // Column 7 of the external matrix circ(2*M4, M4, M4): what the layer adds per @@ -1020,13 +1035,13 @@ __device__ __forceinline__ void mine_nonces(u32* __restrict__ results, for (int i = 0; i < 8; i++) st[i] = gf_add(st[i], (u64)bswap32(current_nonce[7 - i])); ext_layer_rc(st, RC_INITIAL[0]); } - run_permute(st, false, true); + run_permute(st, uni.diag, false, true); #else #pragma unroll for (int i = 0; i < 12; i++) st[i] = uni.midstate[i]; #pragma unroll for (int i = 0; i < 8; i++) st[i] = gf_add(st[i], (u64)bswap32(current_nonce[7 - i])); - run_permute(st); + run_permute(st, uni.diag); #endif st[0] = gf_add(st[0], 1ull); st[1] = gf_add(st[1], 1ull); @@ -1034,7 +1049,7 @@ __device__ __forceinline__ void mine_nonces(u32* __restrict__ results, // Element 0 of the final state is the hash's top 64 bits. Compute it // alone, and only nonces that do not already exceed the target there // pay for the other eleven outputs of the last linear layer. - run_permute(st, true); + run_permute(st, uni.diag, true); { u64 c0 = gf_canon(ext_layer_out0(st)); u32 h0 = bswap32((u32)(c0 & EPS)); @@ -1044,7 +1059,7 @@ __device__ __forceinline__ void mine_nonces(u32* __restrict__ results, } ext_layer(st); #else - run_permute(st); + run_permute(st, uni.diag); #endif // First squeeze: most significant 256 bits of the hash. @@ -1067,7 +1082,7 @@ __device__ __forceinline__ void mine_nonces(u32* __restrict__ results, u32 hash_le[16]; #pragma unroll for (int i = 0; i < 8; i++) hash_le[15 - i] = bswap32(first[i]); - run_permute(st); + run_permute(st, uni.diag); #pragma unroll for (int i = 0; i < 4; i++) { u64 c = gf_canon(st[i]); diff --git a/crates/engine-cuda/src/lib.rs b/crates/engine-cuda/src/lib.rs index d6fa1a5..b5f7d5b 100644 --- a/crates/engine-cuda/src/lib.rs +++ b/crates/engine-cuda/src/lib.rs @@ -173,9 +173,13 @@ struct MiningUniforms { /// derives each nonce's state from this by one scalar times a fixed /// small-integer column instead of recomputing the layer (quantus/miner#3). layer0_base: [u64; 12], + /// lair: the internal layer's diagonal, for the loop kernel (#27). In the + /// parameter bank ptxas reads it straight from the instruction operand; + /// from `__constant__` it reloaded all twelve values every round. + diag: [u64; 12], } -// SAFETY: `#[repr(C)]` plain-old-data with no padding (12 x u64, 32 x u32, 12 x u64), +// SAFETY: `#[repr(C)]` plain-old-data with no padding (12 x u64, 32 x u32, 24 x u64), // matching the kernel's parameter layout; there is nothing to validate beyond // the layout, which is what this marker asserts. unsafe impl DeviceRepr for MiningUniforms {} @@ -183,7 +187,7 @@ unsafe impl DeviceRepr for MiningUniforms {} // A mismatch here would silently feed the kernel the wrong midstate or target, // so it is a build error rather than a mining bug. The kernel carries the same // assertion. -const _: () = assert!(std::mem::size_of::() == 320); +const _: () = assert!(std::mem::size_of::() == 416); pub struct CudaEngine { engine_id: usize, @@ -539,6 +543,7 @@ impl CudaEngine { start_nonce: start_u32s, target: res.target_u32s, layer0_base, + diag: qp_poseidon_constants::POSEIDON2_MATRIX_DIAG_12_RAW, }; let stream = res.stream.clone();