A collection of CUDA kernels written while learning GPU programming, profiling, and performance optimization. Each program pairs a working kernel with measured benchmarks and, where relevant, Nsight Systems/Compute profiling.
All benchmarks and profiling numbers in this repo (including sub-folder READMEs) were measured on:
- GPU: NVIDIA GeForce GTX 1650, 14 SMs, compute capability 7.5 (Turing)
- Peak memory bandwidth: ~192 GB/s
- Driver: 610.74
- CUDA Toolkit: 13.0 (
nvccrelease 13.0, V13.0.88) - Build flags: none beyond
nvcc <file>.cu -o <file>.exe(see each program's "Compiling and running" section) — device code usesnvcc's default optimization level, not an explicit-Oflag.
-
Convolution/. 1D and 2D mask-centered convolution, each with a naive (global-memory) baseline and a shared-memory tiled variant (
conv_1d/conv_1d_shared,conv_2d/conv_2d_shared). The 2D tiled kernel also uses__constant__memory for the mask andcudaMallocPitchfor its buffers. Benchmarked withcudaEvents and profiled with both Nsight Systems and Nsight Compute. Full writeup in Convolution/README.md. -
histogram/. Counts occurrences of
a-zin a byte buffer, built up as a sequence of measured optimizations: naive globalatomicAdd, block-level privatization, then register-level privatization, plus a launch config sized off the GPU rather than the input. Privatization measured ~2.9x faster than the naive baseline once the grid was sized correctly. Full writeup, numbers, and timeline screenshot in histogram/README.md. -
GEMM/. Tiled, shared-memory matrix multiplication (
tileMatMul.cu/tileMatMulGeneric.cu). Both loadTILE_WIDTH x TILE_WIDTHtiles of the input matrices into shared memory per block to cut down on repeated global memory reads; the generic version additionally handles matrix widths that aren't evenly divisible byTILE_WIDTH, with boundary checks that zero-pad out-of-range shared memory loads. Full writeup in GEMM/README.md.
-
clr_greyscale.cu. Converts an RGB image to grayscale. Each thread maps to one pixel, reading the 3-channel RGB value and writing a single grayscale value using the standard luminance weights (0.21R + 0.71G + 0.07B).
main()is a kernel-launch sketch referencing undeclared variables (n,m,w,h,d_Pin,d_Pout); host-side allocation and launch setup haven't been written yet. -
blurKernel.cu. Applies a simple box blur to a single-channel image. Each thread averages a
(2*BLUR_SIZE+1) x (2*BLUR_SIZE+1)neighborhood around its pixel, clamping at image boundaries.main()is a kernel-launch sketch referencing undeclared variables (n,m,in,out,width,height); host-side setup hasn't been written yet.
Requires the NVIDIA CUDA Toolkit (nvcc) and a CUDA-capable GPU.
Convolution (from the Convolution/ folder):
nvcc convolution.cu -o convolution.exe
.\convolution.exeGEMM (from the GEMM/ folder):
nvcc tileMatMul.cu -o tileMatMul.exe
.\tileMatMul.exe
nvcc tileMatMulGeneric.cu -o tileMatMulGeneric.exe
.\tileMatMulGeneric.exehistogram (from the histogram/ folder) needs an extra build flag plus
a test file, since it reads input at runtime rather than generating it:
cd histogram
nvcc -std=c++17 histogram.cu -o histogram.exe
.\histogram.exe --print input.txtThe -std=c++17 flag is required; CUDA's <cuda/atomic> header won't
compile without it. input.txt isn't checked into the repo (see
histogram/README.md for why); generate your own
before running, e.g. a few MB of random bytes. See
histogram/README.md#usage for the full
input-path/--print argument reference.
clr_greyscale.cu and blurKernel.cu aren't runnable yet; they'll get the
same treatment once their host-side code lands.