liveBook · Manning

Naive matrix multiplication kernel

As explained in Grokking GPU Programming

Naive matrix multiplication assigns one GPU thread to each output element, with that thread computing the full dot product for that element.

To compute one output element, take a row of A and a column of B, multiply matching values, and add the products. A naive GPU kernel gives each output element to one thread, which calculates the full dot product independently. section 12.1

For matrices A (M×K) and B (K×N):

  • The result C has shape M×N; each element requires K multiply-adds. section 12.1
  • A 2D thread grid maps columns to x and rows to y. Bounds checks handle dimensions that are not multiples of the block size. section 12.1
  • The simple assignment repeats global-memory loads: each A element is read N times and each B element M times, with little data reuse. section 12.14
__global__ void matmulNaive(float* A, float* B, float* C,
                            int M, int N, int K) {
    int row = blockIdx.y * blockDim.y + threadIdx.y;
    int col = blockIdx.x * blockDim.x + threadIdx.x;
    if (row < M && col < N) {
        float sum = 0.0f;
        for (int k = 0; k < K; k++) {
            sum += A[row * K + k] * B[k * N + col];
        }
        C[row * N + col] = sum;
    }
}

The kernel shows the row and column mapping, bounds check, and loop that accumulates each thread’s dot product. section 12.1

Figure 12.1 Multiplying a 2×3 matrix A by a 3×2 matrix B gives a 2×2 result C. Each element of C is the dot product of one row of A and one column of B: row 0 of A and column 0 of B (shaded) produce C[0][0] = 1×7 + 2×9 + 3×11 = 58. The remaining three elements of C come from the other row–column pairs.

A small numeric example traces one output: row 0 of A dotted with column 0 of B gives 58. section 12.1

How Grokking GPU Programming covers it

How deep it goes
Works through dot products, indexing, and grid coverage, then identifies repeated global-memory loads without detailing kernel optimization.
Examples
Uses a 2×3 by 3×2 multiplication yielding 58 and a 1024×1024 by 1024×512 workload.
What it assumes
Introduces matrix dimensions and dot-product computation through a worked example before explaining thread mapping.
What you can do after
Build a basic CUDA matrix-multiplication kernel that maps threads to output elements and accumulates their dot products.
How it connects
Connects matrix multiplication with dot products, CUDA threads, thread blocks, and global memory.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking GPU Programming. The section links point to the parts of the book it draws on.