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
