Pure GPU compute kernels for common numerical operations, demonstrating CPU-GPU cooperative algorithms with no hostcall overhead.
- SAXPY (
y = a*x + y), the classic GPU compute benchmark - Dot product via GPU element-wise multiply with CPU-side reduction
- Numerically stable softmax using a multi-pass GPU-CPU cooperative strategy
- Approximate
exp()via PTXex2.approxintrinsic and log2(e) scaling - Launching kernels with multi-block grid configurations (256 threads per block)
- SAXPY -- The
saxpykernel computesy[i] = a * x[i] + y[i]for 1024 elements. Each thread handles one element, identified byblockIdx.x * blockDim.x + threadIdx.x. The host uploads vectors, launches the kernel, and verifies every element matches the expected value. - Dot Product -- The
elementwise_mulkernel computesout[i] = x[i] * y[i]on the GPU for 1024 elements. The host then sums the products on the CPU to get the final dot product, demonstrating a split GPU-CPU reduction pattern. - Softmax (Pass 1) -- The CPU scans the input to find
max_valfor numerical stability. Thesoftmax_expkernel computesexp(input[i] - max_val)for each of 256 elements using an approximateexp()implemented as2^(x * log2(e))via inline PTX. - Softmax (Pass 2) -- The host sums the exponentiated values on the CPU. The
softmax_normalizekernel divides each element by this sum, producing a valid probability distribution. - The host verifies the softmax output sums to 1.0 and matches a CPU reference implementation within tolerance.
# Linux/macOS
bash run.sh
# Windows
run.bat=== Vector Math Example ===
[host] CUDA device initialized.
[host] PTX module loaded.
--- Demo 1: SAXPY (y = 2.0 * x + y) ---
[host] SAXPY (1024 elements): PASSED
--- Demo 2: Dot Product (GPU multiply, CPU reduce) ---
[host] dot(x,y) GPU = 9270.00, CPU = 9270.00
[host] Dot Product (1024 elements): PASSED
--- Demo 3: Softmax (GPU-CPU cooperative) ---
[host] softmax sum = 1.000000 (expected 1.0)
[host] max |GPU - CPU| = 0.000000
[host] Softmax (256 elements): PASSED
=== All demos complete! ===
ex2.approx.ftz.f32: Thesoftmax_expkernel uses this PTX instruction for fast approximate 2^x, which is the core of thegpu_exp()helper. Look for themul.f32(by log2(e)) immediately before it.fma.rn.f32: The SAXPY kernel may compile the multiply-add into a fused multiply-add instruction, depending on optimization level.- Thread indexing pattern: All four kernels share the same
mad.lo.s32pattern for computing the global thread index from%ctaid.x,%ntid.x, and%tid.x. - Bounds checking: Each kernel has an early
setp.ge+@%p braexit guard comparing the thread index againstn.