--- title: GEMM Memory Access Pattern Visualizer emoji: ๐Ÿ”ฌ colorFrom: blue colorTo: green sdk: docker app_port: 7860 pinned: false license: mit --- # ๐Ÿ”ฌ GEMM Memory Access Pattern Visualizer An interactive educational tool for visualizing and analyzing memory access patterns in General Matrix Multiply (GEMM) operations. Compare different loop orderings, understand cache behavior, and explore the impact of blocking/tiling optimizations. ## ๐Ÿš€ Try It Out This is a live demo running on Hugging Face Spaces. Simply adjust the controls on the left panel to explore different GEMM configurations! ## โœจ Features ### ๐ŸŽฎ Interactive Web Interface - **Real-time animation** of memory access patterns - **Multiple loop orderings**: ijk, ikj, jik, jki, kij, kji - **Blocking comparison**: blocked vs unblocked implementations - **Play/pause/step controls** for detailed analysis ### ๐Ÿ“Š Comprehensive Visualization - **Live memory access animation** showing A, B, C matrix accesses - **Cache hit rate tracking** with real-time performance graphs - **Access frequency heatmaps** for pattern analysis - **Detailed statistics** on cache performance ## ๐ŸŽฏ Understanding Loop Orderings Matrix multiplication: `C[i][j] += A[i][k] * B[k][j]` Different loop orderings affect memory access patterns: | Loop Order | Description | Cache Behavior | |------------|-------------|----------------| | **IJK** | Row-Column-Depth | Good for C, poor for B | | **IKJ** | Row-Depth-Column | Good spatial locality | | **JIK** | Column-Row-Depth | Poor for row-major matrices | | **JKI** | Column-Depth-Row | Poor spatial locality | | **KIJ** | Depth-Row-Column | Moderate performance | | **KJI** | Depth-Column-Row | Good for blocked algorithms | ## ๐ŸŽ›๏ธ How to Use ### Configuration Panel 1. **Matrix Size**: Adjust from 4ร—4 to 32ร—32 2. **Block Size**: Set block size (2-16) for blocked algorithms 3. **Loop Order**: Select from 6 different orderings 4. **Blocking**: Toggle between blocked/unblocked ### Animation Controls - **โ–ถ Play**: Start automatic animation - **โธ Pause**: Pause animation - **๐Ÿ”„ Reset**: Reset to frame 0 - **Speed Slider**: Control animation speed (1-100) - **Frame Slider**: Jump to specific frame ## ๐Ÿ“Š Interpreting Results ### Cache Hit Rate - **> 90%**: Excellent cache utilization โœ… - **70-90%**: Good performance ๐Ÿ‘ - **< 70%**: Poor cache behavior, consider optimization โš ๏ธ ### Access Frequency Heatmap - **Bright spots**: Frequently accessed elements - **Uniform color**: Good spatial locality - **Scattered patterns**: Cache-unfriendly access ## ๐Ÿงช Try These Experiments ### Experiment 1: Blocked vs Unblocked 1. Set matrix size to 16ร—16, block size to 4 2. Select "KJI" loop order, set "Blocked" to ON 3. Note the cache hit rate 4. Switch "Blocked" to OFF 5. Compare performance! You should see a significant difference ### Experiment 2: Best vs Worst Loop Order **For blocked algorithms (16ร—16, block=4):** - Best: KJI or IKJ (~85%+ hit rate) - Worst: JKI or JIK (~60-70% hit rate) ## ๐Ÿ’ก Educational Use This tool is designed for: - **Computer Architecture courses**: Teaching cache hierarchies - **Performance optimization**: Understanding memory access patterns - **Algorithm analysis**: Comparing loop transformations - **Research**: Experimenting with blocking strategies ## ๐Ÿ“š Technical Details ### Memory Layout - **Row-major order**: C[i][j] stored at `base + (i*n + j)*sizeof(element)` - **Cache line**: 64 bytes (8 doubles) - **Spatial locality**: Sequential elements benefit from cache prefetch ### Cache Simulation - **LRU replacement**: Least Recently Used eviction policy - **Set-associative**: 8-way associativity (configurable) - **Address mapping**: Tag-Index-Offset breakdown - **Cache size**: 32KB L1 cache simulation ## ๐Ÿ”ง Local Installation ```bash # Clone the repository git clone https://huggingface.co/spaces/YOUR_USERNAME/gemm-visualizer cd gemm-visualizer # Install dependencies pip install -r requirements.txt # Run locally python app.py ``` Then open your browser to: **http://127.0.0.1:7860** ## ๐Ÿค Contributing Contributions welcome! Visit the [GitHub repository](https://github.com/bugparty/gemm_visualizations) for: - Additional cache replacement policies (FIFO, Random) - 3D visualization of memory access - Performance prediction models - Multi-level cache hierarchy - Non-square matrix support ## ๐Ÿ“„ License MIT License - feel free to use for educational purposes! ## ๐Ÿ™ Acknowledgments Based on classical GEMM optimization techniques from: - *Computer Architecture: A Quantitative Approach* (Hennessy & Patterson) - *Optimizing Matrix Multiply* (Goto & Geijn) ## ๐Ÿ“ž Contact For questions or feedback, please open an issue on the [GitHub repository](https://github.com/bugparty/gemm_visualizations). --- **Happy Learning! ๐Ÿš€**