Matrix Multiplication in C++: From Naive to Tiled
This article explores three different ways of achieving matrix multiplication from the less efficient to the more efficient one

Introduction
How many people out there are aware that matrix multiplication is one of the most heavily optimized operations in modern computing? Probably fewer than you might think.
Matrix multiplication, which is often introduced in Linear Algebra, appears in many areas of computing, from Deep Learning and Image Processing to Scientific Simulation and Gaming.
The concept is so ubiquitous because matrices provide a convenient way to represent and manipulate large amounts of structured data. However, when the amount of data becomes large, matrix operations can become computationally expensive and resource-intensive. This creates a need for optimization in order to achieve better performance.
In this article, we will explore three different approaches to matrix multiplication in C++ and see how changing the way we access memory can significantly impact performance.
0. Row-Major Matrix Representation
When talking about performance, one of the first things we need to think about is memory access.
In memory, a 2D matrix is typically stored as a contiguous 1D array using a row-major representation, where the elements of each row are placed next to each other.
For example:
| 0, 1, 2 | → 1st row
| 3, 4, 5 | → 2nd row
| 6, 7, 8 | → 3rd row
becomes:
[0, 1, 2, 3, 4, 5, 6, 7, 8]
The elements are contiguous in memory. This is important because modern CPUs load data from memory in blocks called cache lines. Accessing neighboring elements allows the CPU to take advantage of spatial locality and reduces expensive trips to main memory.
For a matrix A(M × N), the formula for accessing the element at row r and column c is:
A[r * N + c]
This simple memory-layout detail will become very important when we look at the different multiplication methods.
1. The Naive Method
Let's suppose we want to multiply:
A(M × N) × B(N × O) = C(M × O)
To calculate each element of C, we take a row from A, a column from B, multiply their corresponding elements, and sum the results.
The classic implementation looks like this:
for (int i = 0; i < M; i++) {
for (int j = 0; j < O; j++) {
float sum = 0;
for (int k = 0; k < N; k++) {
sum += A[i * N + k] * B[k * O + j];
}
C[i * O + j] = sum;
}
}
The problem here is the way we access matrix B.
Accessing A is relatively friendly because k changes sequentially:
A[i][0]
A[i][1]
A[i][2]
A[i][3]
...
However, when accessing B:
B[0][j]
B[1][j]
B[2][j]
B[3][j]
...
we are moving down a column.
Since B is stored in row-major order, these elements are separated in memory by O elements:
B[k * O + j]
As k changes, we therefore make larger jumps through memory. This reduces spatial locality and can lead to more cache misses.
So although the mathematical operation is correct, the memory-access pattern is not ideal.
Can we do better?
Yes.
2. Cache-Friendly Multiplication
The issue with the previous implementation comes mainly from how we access B.
Instead of computing one element of C at a time, what if we changed the order of the loops so that we access both B and C sequentially?
We can transform the loop ordering from:
i → j → k
to:
i → k → j
For example:
for (int i = 0; i < M; i++) {
for (int k = 0; k < N; k++) {
float a = A[i * N + k];
for (int j = 0; j < O; j++) {
C[i * O + j] += a * B[k * O + j];
}
}
}
Now look at how B is accessed:
B[k][0]
B[k][1]
B[k][2]
B[k][3]
...
We are moving across a row, meaning that the accesses are sequential in memory.
C is also accessed sequentially:
C[i][0]
C[i][1]
C[i][2]
C[i][3]
...
This allows the CPU to make much better use of its cache.
The mathematical operation hasn't changed at all. We simply changed the order in which the computation is performed.
Depending on the matrix size, compiler optimizations, and hardware, this simple change in loop ordering can result in substantial speedups, sometimes several times faster than the naive implementation.
And this is one of the most important lessons in performance engineering:
The way you access data can matter just as much as the computation itself.
But we can go even further.
3. Tiled Matrix Multiplication
The next technique is tiled matrix multiplication, also known as blocked matrix multiplication.
Instead of processing the entire matrices at once, we divide them into smaller blocks called tiles.
Suppose we want to calculate:
A(M × N) × B(N × O) = C(M × O)
Instead of processing the entire matrices, we divide them into smaller T × T blocks.
Conceptually:
B
┌────┬────┬────┐
│ B₀ │ B₁ │ B₂ │
├────┼────┼────┤
│ B₃ │ B₄ │ B₅ │
├────┼────┼────┤
│ B₆ │ B₇ │ B₈ │
└────┴────┴────┘
A is divided in the same way.
We then perform multiplication between corresponding tiles and accumulate the results into tiles of C.
The key advantage is data locality.
A small tile can fit much better into the CPU cache. Once the data is loaded into the cache, we can reuse it multiple times before moving on to another tile.
Instead of repeatedly going back to main memory for the same data, we keep frequently used data closer to the processor.
This significantly reduces expensive memory accesses.
Tiling is also particularly important in parallel computing and GPU programming because the computation naturally breaks down into smaller independent pieces.
On a GPU, for example, many threads can cooperate on a tile, load data into fast shared memory, and reuse it for multiple computations.
This is one of the fundamental ideas behind highly optimized GPU matrix multiplication.
Conclusion
Matrix multiplication is everywhere in modern computing, from Deep Learning and computer graphics to scientific simulations and image processing.
But the interesting part isn't just knowing how to multiply two matrices.
It's understanding how the computer actually performs the operation.
By changing the loop ordering, we can improve memory access patterns.
By using tiling, we can improve data locality and reuse.
And when we move to GPUs, these same concepts become even more important because we have thousands of threads working together and multiple levels of memory with very different performance characteristics.
Understanding these concepts is an essential step toward writing high-performance software, especially in the era of AI and accelerated computing.
I've implemented the different approaches discussed in this article in C++ and included the code and benchmarks in the GitHub repository:
GitHub: https://github.com/muhumbania/matrixify-cpp
Thanks for reading, and see you in the next one! :)