# Cache Memory and Matrix Multiplication in MATLAB

## Purpose

This experiment connects memory-access patterns with measured performance. You will compare three explicit-loop matrix multiplication implementations with MATLAB's built-in `A * B`.

## Learning goals

After completing the activity, you should be able to:

1. Explain how MATLAB's column-major array layout affects memory access.
2. Compare two loop orders and a blocked implementation.
3. Verify numerical correctness against `A * B`.
4. Measure execution time for multiple matrix sizes.
5. Explain results using evidence and discuss limits of the experiment.

## Files

Keep these files in the same folder:

- `cache_matrix_lab.m`
- `matmul_ijk.m`
- `matmul_kji.m`
- `matmul_blocked.m`

## How to run

1. Open MATLAB or MATLAB Online.
2. Set the current folder to the folder containing these files.
3. Run `cache_matrix_lab` in the Command Window, or open the script and select Run.
4. Read the printed timings and inspect the plot.
5. Open `matrix_multiplication_results.csv` to review the results table.

The script uses MATLAB's `timeit` function. If the run takes too long, edit the `sizes` vector near the top of `cache_matrix_lab.m` to use smaller matrix sizes.

## Experiment

The script compares:

- `matmul_ijk`: loop order i-j-k.
- `matmul_kji`: loop order k-j-i.
- `matmul_blocked`: blocked matrix multiplication.
- `A * B`: MATLAB's built-in matrix multiplication.

All implementations are checked against `A * B` before timing. A small floating-point difference is expected because the order of arithmetic operations can differ.

## Analysis questions

1. Why should all implementations produce approximately the same numerical result?
2. MATLAB uses column-major storage. Which accesses in `matmul_kji` move down columns as the inner loop variable changes?
3. Which implementation was fastest for each matrix size? Include measurements from your results table.
4. Did the same implementation win at every size? Suggest reasons for any change.
5. Why might MATLAB's built-in `A * B` be faster than explicit loops?
6. Does this experiment isolate cache effects alone? Explain at least two other factors that may affect execution time.
7. Propose one additional experiment, state your prediction, and explain how you would test it.

## What to submit

Submit a short report containing:

- Your results table and timing plot.
- A brief correctness statement based on the numerical error values.
- Answers to the analysis questions.
- One limitation of the experiment and one proposed follow-up experiment.

## Important interpretation note

MATLAB's built-in multiplication may use highly optimized numerical libraries. The explicit-loop versions also run in MATLAB and may benefit from runtime optimization. Therefore, the experiment compares implementations; it does not measure cache behavior in isolation. Timing results can vary with hardware, MATLAB version, system load, and matrix size. You are graded on sound measurement and reasoning, not on obtaining a particular speedup.
