Cache Memory for Matrix multiplication
Summary
This activity helps undergraduate computer science students investigate how memory access patterns affect computational performance. Students begin with a C-based cache-memory lab adapted from Computer Systems: A Programmer's Perspective (csapp). They explore how loop reordering and matrix blocking can improve memory access patterns and reduce the cost of data movement.
The activity then introduces MATLAB as a high-level numerical computing environment. Students implement matrix multiplication using explicit loops, compare the results and execution times with MATLAB's built-in matrix multiplication, and analyze the performance differences. The MATLAB experiment connects low-level concepts such as cache locality and memory layout with the performance of optimized numerical software.
The activity combines programming, performance measurement, data visualization, and written analysis. Students are not expected to obtain a particular speedup. Instead, they use experimental evidence to explain their results and discuss the limits of performance comparisons across different implementations.
Learning Goals
After completing this activity, students will be able to:
- Explain how spatial locality, temporal locality, and cache behavior influence the performance of memory-intensive computations.
- Implement and compare alternative loop orders for matrix multiplication and explain how their memory access patterns differ.
- Apply matrix blocking to improve data reuse and evaluate its effect on execution time.
- Measure and compare the execution time of an explicit loop-based MATLAB implementation and MATLAB's built-in matrix multiplication for multiple matrix sizes.
- Analyze performance measurements and explain how memory layout, cache locality, compiler or runtime optimization, and numerical libraries may contribute to the observed results.
Context for Use
This activity is designed for undergraduate students in a computer organization, computer architecture, or systems programming course. It is most appropriate for students who have completed introductory programming and have some familiarity with arrays, loops, and basic matrix operations. Familiarity with C is required for the original cache lab. No prior MATLAB experience is required for the MATLAB extension.
The complete activity can be used over approximately two hours of lecture and discussion, followed by a laboratory assignment completed over several days. Instructors may shorten the activity by assigning the C lab separately or using the MATLAB experiment as an in-class demonstration.
The C component introduces students to cache behavior through low-level programming and performance optimization. The MATLAB component extends the discussion by comparing explicit loop-based matrix multiplication with MATLAB's built-in matrix multiplication. This comparison helps students connect computer architecture concepts with the behavior of high-level numerical software.
Students need access to a C development environment and MATLAB or MATLAB Online. The instructor should provide the MATLAB source files and student instructions so that students without prior MATLAB experience can complete the experiment.
Description and Teaching Materials
Part 1: Investigating cache memory using C
Students complete the provided C-based cache-memory lab. They investigate memory access patterns and explore how loop reordering and matrix blocking affect computational performance. The instructor uses this part to introduce spatial locality, temporal locality, and the relationship between memory access patterns and cache behavior.
Part 2: Comparing matrix multiplication in MATLAB
Students use the supplied MATLAB files to multiply two matrices using explicit nested loops. They compare the results with MATLAB's built-in matrix multiplication operator, A * B, and measure execution time for several matrix sizes.
Students verify that the implementations produce numerically equivalent results before interpreting the timing measurements. They record their results in a table and create a plot showing how execution time changes with matrix size.
Part 3: Analysis and discussion
Students submit a short report that compares their implementations and explains the observed performance differences. They discuss how memory layout and cache locality may affect performance, why optimized numerical libraries can outperform straightforward loop implementations, and why measured results may differ across computers.
Materials provided
The activity includes the original C-based cache-memory lab, cache-memory lecture slides, a MATLAB student handout, MATLAB source files, an experimental results template, and an assessment rubric. The instructor may use all components together or assign the MATLAB extension after a separate lecture on cache memory.
Cache Lab: Understanding Cache Memories (Acrobat (PDF) 46kB Sep8 26)
lecture: cache memories (Acrobat (PDF) 1.5MB Sep8 26)
README.txt (Text File 1kB Oct25 26)
MATLAB_Student_Guide.md ( 3kB Oct25 26)
matmul_ijk.m (Matlab File 938bytes Oct25 26)
matmul_kji.m (Matlab File 922bytes Oct25 26)
matmul_blocked.m (Matlab File 2kB Oct25 26)
cache_matrix_lab.m (Matlab File 4kB Oct25 26)
Teaching Notes and Tips
Students may have different levels of experience with C, linear algebra, and MATLAB. Before the MATLAB experiment, the instructor should briefly demonstrate how to run a script, call a function, create a matrix, and measure execution time. Students should not need to learn MATLAB syntax independently before they can investigate the performance questions.
Begin by asking students to predict which loop order will perform better and why. After students collect timing measurements, ask them to compare their predictions with the results. This prediction–experiment–explanation sequence encourages students to connect theoretical concepts with experimental evidence.
When discussing MATLAB's built-in matrix multiplication, explain that MATLAB can use optimized numerical libraries and runtime optimizations. Therefore, the experiment does not isolate cache behavior alone. The difference in execution time may reflect several factors, including memory layout, loop overhead, compiler or runtime optimization, and numerical library implementation.
Performance results may vary across computers and MATLAB versions. Students should focus on explaining their own measurements rather than obtaining a prescribed speedup. Encourage them to repeat measurements, use the same inputs when comparing implementations, and verify numerical correctness before concluding.
Students may use AI tools to help understand MATLAB syntax or debug their programs, if permitted by the instructor. However, each student should be able to explain the loop structure, the purpose of the timing experiment, and the conclusions in the submitted report. Instructors may ask students to explain a code segment or modify a loop during class to check individual understanding.
Assessment
Students submit a short report containing their implementation details, experimental results, analysis, and conclusions. The report must include evidence from both the C cache lab and the MATLAB extension.
The instructor evaluates the report using the following rubric.
| Assessment criterion | Points | Evidence of achievement |
|---|---|---|
| Cache concepts | 20 | Correctly explains spatial locality, temporal locality, and how memory access patterns affect cache behavior. |
| Loop ordering and blocking | 20 | Describes the tested implementations and explains how loop reordering or blocking changes memory access patterns. |
| MATLAB implementation and correctness | 20 | Runs the supplied MATLAB implementations and verifies their results against MATLAB's built-in matrix multiplication. |
| Performance measurements | 20 | Provides a labeled results table and plot covering at least four matrix sizes, with execution-time units. |
| Interpretation and reflection | 20 | Uses experimental evidence to explain performance differences, discusses at least one limitation, and draws conclusions consistent with the results. |
Total: 100 points
A student meets the learning goals when the submitted work provides evidence for the corresponding criteria. For the MATLAB correctness check, the instructor may require a relative numerical error below (10^{-10}) for the supplied test cases. Timing results are evaluated for completeness and appropriate interpretation, not for achieving a specific speedup.
The instructor can use the rubric to identify whether students have difficulty with cache concepts, implementation, experimental methods, or interpretation. Students who obtain unexpected results can still receive full credit when they report their measurements accurately and provide a technically sound explanation.
References and Resources
References
- Bryant, R. E., and O'Hallaron, D. R. Computer Systems: A Programmer's Perspective. Pearson. Companion website: https://csapp.cs.cmu.edu/
- Carnegie Mellon University. Cache Lab: Understanding Cache Memories. CS course materials. https://csapp.cs.cmu.edu/3e/cachelab.pdf
- MathWorks. Matrix Multiplication. MATLAB documentation. https://www.mathworks.com/help/matlab/ref/mtimes.html
- MathWorks. Measure the Performance of Your Code. MATLAB documentation on performance measurement and timing functions. https://www.mathworks.com/help/matlab/matlab_prog/measure-performance-of-your-program.html