Matrix Multiplication 5: Compare Your Kernel with cuBLAS
If you have not finished Stage 4, we strongly recommend doing that first. This stage uses its tiled matmul as the baseline. If shared-memory tiling already makes sense to you, you can start here. Your task is to replace that matmul with a cuBLAS call and compare the results.
Sign in to run this lab. GPU availability and credits are shown in the app.
Languages: CUDA C++, Rust
Lesson
If Stage 4 is not finished yet, go back and complete it first. You will use that tiled kernel as the comparison point. If you already understand shared-memory tiling, you can continue here.
cuBLAS is NVIDIA's GPU linear-algebra library. It calls highly tuned kernels internally, including CUTLASS kernels. For API details, see NVIDIA's cuBLAS documentation: https://docs.nvidia.com/cuda/cublas/index.html
Your task:
1. Set the cuBLAS math mode from `useTensorCores`.
2. Call `cublasSgemm` so it computes C = A x B. The buffers are row-major, but cuBLAS uses column-major order, so pass B first and A second. No transpose is needed.
3. Run the problem. The harness runs your Stage 4 kernel and your cuBLAS function on the same inputs.
4. Open Session Details. Compare the kernel time, total time, GFLOP/s, and the CUTLASS kernels on the timeline.
You should see the library beat the simple tiled kernel. That is the point of the exercise: a correct kernel is only the beginning, and production libraries contain many more optimizations. The current pass condition is correctness; the timing comparison is the lesson.
Prerequisites: row-major indexing, column-major cuBLAS interpretation, and FP32 versus TF32 precision. Library kernel names depend on the CUDA/cuBLAS version; recognize the operation and measured precision, not a fixed name prefix.
What you will learn
- Set cuBLAS math mode for regular FP32 and TF32.
- Call cublasSgemm with row-major inputs without transposing them.
- Compare your kernel with cuBLAS using time, GFLOP/s, occupancy, and the timeline.
- Recognize the SIMT and tensor-core paths in CUTLASS kernel names.
Starter code
// The harness calls this once with false and once with true.
// Both runs are checked and timed.
// 1. Select regular FP32 math or TF32 tensor-core math.
// TODO: cublasSetMathMode(blas, useTensorCores ? ... : ...);
const float alpha = 1.0f;
const float beta = 0.0f;
// 2. cuBLAS uses column-major order. Our buffers are row-major.
// B * A in column-major order produces A * B in row-major order,
// so pass B first and A second. No transpose is needed.
// TODO: cublasSgemm(blas, CUBLAS_OP_N, CUBLAS_OP_N, N, N, N,
// &alpha, /* B */, N, /* A */, N, &beta, C, N);