0Pricing
CUDA Academy · Lesson

cuBLAS GEMM Done Right

Handles, column-major, and leading dims.

cuBLAS GEMM Done Right is a free CUDA Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CUDA Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Stand on NVIDIA's Shoulders

Writing a fast matmul by hand is hard. cuBLAS ships a battle-tested GEMM that already squeezes your GPU near peak performance. 🚀

What GEMM Means

GEMM stands for general matrix-matrix multiply. It computes C = alpha * A * B + beta * C, the workhorse behind graphics and deep learning.

C = alpha * (A * B) + beta * C

Every Call Needs a Handle

cuBLAS keeps its state in a handle. You create one at startup, reuse it for every call, and destroy it at the end.

cublasHandle_t h;
cublasCreate(&h);
// ... use h ...
cublasDestroy(h);

The Column-Major Surprise

cuBLAS expects column-major matrices, the Fortran layout. Your C++ arrays are usually row-major, so this mismatch trips up almost everyone.

Leading Dimension

The leading dimension tells cuBLAS the stride between columns in memory. For a tightly packed M-by-N column-major matrix, it is simply M.

int lda = M; // rows, column-major stride

The Transpose Trick

A neat fix: row-major A times B equals the transpose of column-major B times A. Many people just swap the operands instead of transposing data.

Scalars Live in alpha and beta

You pass alpha and beta as pointers. Use alpha = 1 and beta = 0 for a plain C = A * B with nothing added in.

const float alpha = 1.0f, beta = 0.0f;

Calling cublasSgemm

cublasSgemm is the single-precision float GEMM. Its long argument list is just dimensions, transpose flags, scalars, and the three device pointers.

cublasSgemm(h, CUBLAS_OP_N, CUBLAS_OP_N,
  M, N, K, &alpha, dA, M, dB, K, &beta, dC, M);

Pointers Must Be on the Device

dA, dB, and dC point to device memory. Pass host pointers by mistake and cuBLAS returns an error instead of a result.

Pick the Precision Suffix

The letter encodes the type: S for float, D for double, C and Z for complex. Choose Dgemm when you need double precision.

cublasDgemm(...); // double precision GEMM

Check the Return Status

Every cuBLAS call returns a cublasStatus_t. Compare it to CUBLAS_STATUS_SUCCESS so a bad handle or dimension never passes unnoticed.

if (status != CUBLAS_STATUS_SUCCESS) { /* handle */ }

Quick Check

Think about the data layout cuBLAS assumes.

Recap

You created a handle, respected column-major layout and leading dimensions, set alpha and beta, and called Sgemm with device pointers. 🎯

Frequently asked questions

Is the “cuBLAS GEMM Done Right” lesson free?

Yes — the full text of “cuBLAS GEMM Done Right” is free to read here on the web, and the CUDA Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CUDA Academy course, upgrade to CoddyKit PRO.

What will I learn in “cuBLAS GEMM Done Right”?

Handles, column-major, and leading dims. You practise CUDA Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start CUDA Academy?

No prior experience is required. CUDA Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “cuBLAS GEMM Done Right” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this CUDA Academy lesson?

Yes. Every CUDA Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. cuBLAS GEMM Done Right
  2. Thrust Vectors and Transforms
  3. Thrust Reduce, Scan, and Sort
  4. cuDNN for Deep Learning
← Back to CUDA Academy