diff --git a/crates/engine-cuda/src/kernels/mining.cu b/crates/engine-cuda/src/kernels/mining.cu index b55cbbf..2bcb56e 100644 --- a/crates/engine-cuda/src/kernels/mining.cu +++ b/crates/engine-cuda/src/kernels/mining.cu @@ -28,11 +28,43 @@ typedef unsigned int u32; // 2^64 mod P = 2^32 - 1 #define EPS 0xFFFFFFFFull +// lair: whether gf_add and gf_reduce should use inline-PTX carry arithmetic or +// plain C is architecture-dependent, and the difference is large in both +// directions. An `asm` block is opaque to nvcc's optimiser, so carry arithmetic +// written as PTX blocks common-subexpression elimination and strength reduction +// across neighbouring field operations. On sm_120 that costs a quarter of the +// kernel; on sm_86/89 the hand-written sequence still wins. +// +// Whole-kernel instructions / ALU ops (ptxas + cuobjdump, CUDA 13.0) and +// measured hashrate, three interleaved rounds each, parity 40/40 vs CPU: +// +// sm_120 sm_86/89 +// CARRY=1 37,432 / 26,688 1126.6 37,027 benjy 461.6 quadbrat 85.5 +// CARRY=0 27,837 / 19,519 1333.3 41,196 benjy 401.5 quadbrat 76.3 +// +// So +18.3% on the 5090s and -13.0% / -10.8% on the 4090 and 3060. Hence the +// per-arch default. Unlike LAIR_INT_UNROLL -- where the static measures pointed +// one way and the cards' power envelope the other -- this one was measured on +// hardware on all three architectures, and the static count predicted the sign +// correctly on each. +// +// LAIR_PTX_ACC stays 1 everywhere: in the Acc accumulators the add-with-carry +// pair is the whole operation, so there is nothing around it to optimise. On +// sm_120, carry off with Acc off measured 1285.8 against 1333.3 with Acc on. +// +// PR #17 measured the PTX carry path as a win on every card, and that was true +// of the kernel it was written for -- which spilled 104 bytes per thread. Once +// the spills went (see MiningUniforms) the trade reversed on Blackwell only. +// Re-measure per architecture before touching this. #ifndef LAIR_PTX_CARRY -#define LAIR_PTX_CARRY 1 +# if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 1200 +# define LAIR_PTX_CARRY 0 +# else +# define LAIR_PTX_CARRY 1 +# endif #endif #ifndef LAIR_PTX_ACC -#define LAIR_PTX_ACC LAIR_PTX_CARRY +#define LAIR_PTX_ACC 1 #endif // Fold the next round's constant into the linear layer's final reduction // instead of a separate gf_add per element before the S-box. @@ -138,10 +170,14 @@ __device__ __forceinline__ u64 gf_canon(u64 a) { #define LAIR_PRAGMA(x) _Pragma(#x) #define LAIR_UNROLL(n) LAIR_PRAGMA(unroll n) #ifndef LAIR_PTX_CARRY -#define LAIR_PTX_CARRY 1 +# if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 1200 +# define LAIR_PTX_CARRY 0 +# else +# define LAIR_PTX_CARRY 1 +# endif #endif #ifndef LAIR_PTX_ACC -#define LAIR_PTX_ACC LAIR_PTX_CARRY +#define LAIR_PTX_ACC 1 #endif // Fold the next round's constant into the linear layer's final reduction // instead of a separate gf_add per element before the S-box.