From 3ce1c8cc2ee641e29e2a63ccb8fc327575d940ed Mon Sep 17 00:00:00 2001 From: Linus Baumberger Date: Wed, 20 May 2026 09:26:22 +0200 Subject: [PATCH] updated structure of how we document the optimizations --- docs/OPTIMIZATIONS.md | 187 ++++------------------------------ docs/optimization_baseline.md | 65 ++++++++++++ docs/optimization_inlining.md | 95 +++++++++++++++++ docs/optimization_template.md | 76 ++++++++++++++ 4 files changed, 257 insertions(+), 166 deletions(-) create mode 100644 docs/optimization_baseline.md create mode 100644 docs/optimization_inlining.md create mode 100644 docs/optimization_template.md diff --git a/docs/OPTIMIZATIONS.md b/docs/OPTIMIZATIONS.md index e1855f7..58c5e92 100644 --- a/docs/OPTIMIZATIONS.md +++ b/docs/OPTIMIZATIONS.md @@ -1,18 +1,16 @@ # Optimizations of the Social Force Model -## Table of Contents +Summary of optimization attempts. Each optimization has its own details file +under `docs/optimization_.md`. Use `docs/optimization_template.md` as +the starting point for a new entry. -1. [Performance Results](#performance-results) -2. [Baseline](#2-baseline) -3. [Optimization 1](#3-optimization-1) -4. [Optimization 2](#4-optimization-2) -5. [Optimization 3](#5-optimization-3) -6. [Optimization 4](#6-optimization-4) -7. [Optimization 5](#7-optimization-5) -8. [Optimization 6](#8-optimization-6) -9. [Optimization 7](#9-optimization-7) -10. [Optimization 8](#10-optimization-8) -11. [Optimization 9](#11-optimization-9) +To document a new optimization: + +``` +cp docs/optimization_template.md docs/optimization_.md +``` + +Fill it in, then add a row to the tables below pointing at your file. --- @@ -23,7 +21,7 @@ **Machine**: ``` -Processors: 16 × AMD Ryzen AI 7 350 w/ Radeon 860M +Processors: 16 x AMD Ryzen AI 7 350 w/ Radeon 860M Memory: 32 GiB of RAM (30.6 GiB usable) Graphics Processor: AMD Radeon 860M Graphics Manufacturer: LENOVO @@ -33,164 +31,21 @@ System Version: IdeaPad Slim 5 14AKP10 **Frequency**: 2 GHz -| # | Optimization | W(n) [Flops] | T(n) [Cycles] | Performance [Flops/Cycle] | Author | -| --- | ------------ | ---------------- | -------------- | ------------------------- | ------ | -| 0 | Baseline | 1'058'400'000.00 | 850'352'900.00 | 1.24 | | -| 1 | Inlining | 1'058'400'000.00 | 756'088'260.00 | 1.40 | Elliot | -| 2 | | | | | | -| 3 | | | | | | -| 4 | | | | | | -| 5 | | | | | | -| 6 | | | | | | -| 7 | | | | | | -| 8 | | | | | | -| 9 | | | | | | +| # | Optimization | W(n) [Flops] | T(n) [Cycles] | Performance [Flops/Cycle] | Author | +| --- | --------------------------------------- | ---------------- | -------------- | ------------------------- | ------ | +| 0 | [Baseline](optimization_baseline.md) | 1'058'400'000.00 | 850'352'900.00 | 1.24 | | +| 1 | [Inlining](optimization_inlining.md) | 1'058'400'000.00 | 756'088'260.00 | 1.40 | Elliot | -### ARM +### ARM (Apple) **Machine**: ``` - +TBD ``` -**Frequency**: ... +**Frequency**: TBD -| # | Optimization | W(n) [Flops] | T(n) [Cycles] | Performance [Flops/Cycle] | Author | -| --- | ------------ | ------------ | ------------- | ------------------------- | ------ | -| 0 | Baseline | | | | | -| 1 | | | | | | -| 2 | | | | | | -| 3 | | | | | | -| 4 | | | | | | -| 5 | | | | | | -| 6 | | | | | | -| 7 | | | | | | -| 8 | | | | | | -| 9 | | | | | | - -## 2. Baseline - -**Author**: [Name] - -**Description**: -Standard implementation without optimizations. - -```cpp -// Paste your code here only the relevant part -``` - ---- - -## 3. Optimization 1 - -**Author**: [Quy Anh] «Elliot» Nguyen - -**Description**: -Manually inlined functions, namely `w`, `acceleration_term_dt`, `pedestrian_repulsion_dt`, `obstacle_repulsion_dt`, `norm`, `dot`, and `find_nearest_obstacle_point`, all into `run_simulation`. Got rid of small number checks after verifying they do not affect results. Removed all references to `Vector2d` and rewrote `Pedestrian` into a struct of doubles. Reduced `sqrt()` calls using mathematical properties. - -**Relevant Files**: `variant_inlining_1.cpp`, `variant_inlining_2.cpp`, `variant_inlining_3.cpp`, `variant_inlining_4.cpp`, `variant_inlining_5.cpp`, `variant_inlining_6.cpp`, `variant_inlining_7.cpp`, `pedestrian_reduced.hpp` - -## 4. Optimization 2 - -**Author**: [Name] - -**Description**: -[Briefly describe the optimization technique] - -```cpp -// Paste your code here only the relevant part -``` - ---- - -## 5. Optimization 3 - -**Author**: [Name] - -**Description**: -[Briefly describe the optimization technique] - -```cpp -// Paste your code here only the relevant part -``` - ---- - -## 6. Optimization 4 - -**Author**: [Name] - -**Description**: -[Briefly describe the optimization technique] - -```cpp -// Paste your code here only the relevant part -``` - ---- - -## 7. Optimization 5 - -**Author**: [Name] - -**Description**: -[Briefly describe the optimization technique] - -```cpp -// Paste your code here only the relevant part -``` - ---- - -## 8. Optimization 6 - -**Author**: [Name] - -**Description**: -[Briefly describe the optimization technique] - -```cpp -// Paste your code here only the relevant part -``` - ---- - -## 9. Optimization 7 - -**Author**: [Name] - -**Description**: -[Briefly describe the optimization technique] - -```cpp -// Paste your code here only the relevant part -``` - ---- - -## 10. Optimization 8 - -**Author**: [Name] - -**Description**: -[Briefly describe the optimization technique] - -```cpp -// Paste your code here only the relevant part -``` - ---- - -## 11. Optimization 9 - -**Author**: [Name] - -**Description**: -[Briefly describe the optimization technique] - -```cpp -// Paste your code here only the relevant part -``` - ---- +| # | Optimization | W(n) [Flops] | T(n) [Cycles] | Performance [Flops/Cycle] | Author | +| --- | --------------------------------------- | ------------ | ------------- | ------------------------- | ------ | +| 0 | [Baseline](optimization_baseline.md) | | | | | diff --git a/docs/optimization_baseline.md b/docs/optimization_baseline.md new file mode 100644 index 0000000..832757d --- /dev/null +++ b/docs/optimization_baseline.md @@ -0,0 +1,65 @@ +# Optimization: Baseline + +- **Author**: TBD +- **Predecessor**: N/A (initial reference) +- **File(s)**: variants/variant_baseline.cpp +- **Status**: reference + +## What this changes + +This is the unoptimized reference implementation of the social force model. +Every other optimization is measured relative to it. See the source file for +the full implementation; key functions are `w()`, `acceleration_term_dt()`, +`pedestrian_repulsion_dt()`, `obstacle_repulsion_dt()`, and `run_simulation()`. + +## Code (relevant snippet) + +TBD + +## Benchmark + +Conditions: TBD. + +### x86 + +**Machine**: + +``` +TBD +``` + +**Frequency**: TBD + +| N | W(n) [Flops] | T(n) [Cycles] | Performance [Flops/Cycle] | Diff vs Predecessor | +| ---- | ------------ | ------------- | ------------------------- | ------------------- | +| 50 | | | | - | +| 100 | | | | - | +| 200 | | | | - | +| 500 | | | | - | +| 1000 | | | | - | + +### ARM (Apple) + +**Machine**: + +``` +TBD +``` + +**Frequency**: TBD + +| N | W(n) [Flops] | T(n) [Cycles] | Performance [Flops/Cycle] | Diff vs Predecessor | +| ---- | ------------ | ------------- | ------------------------- | ------------------- | +| 50 | | | | - | +| 100 | | | | - | +| 200 | | | | - | +| 500 | | | | - | +| 1000 | | | | - | + +## Analysis + +TBD + +## Takeaway + +TBD diff --git a/docs/optimization_inlining.md b/docs/optimization_inlining.md new file mode 100644 index 0000000..83f81d2 --- /dev/null +++ b/docs/optimization_inlining.md @@ -0,0 +1,95 @@ +# Optimization: Inlining + +- **Author**: Elliot +- **Predecessor**: variant_baseline.cpp +- **File(s)**: variants/variant_inlining_1.cpp ... variants/variant_inlining_7.cpp, headers/pedestrian_reduced.hpp +- **Status**: positive (~+13% perf) + +## What this changes + +Manually inlined `w`, `acceleration_term_dt`, `pedestrian_repulsion_dt`, +`obstacle_repulsion_dt`, `norm`, `dot`, and `find_nearest_obstacle_point` all +into `run_simulation`. Got rid of small-number EPS checks after verifying they +do not affect results. Removed all references to `Vector2d` and rewrote +`Pedestrian` as a struct of doubles. Reduced `sqrt()` calls using mathematical +properties. + +Built up progressively across 7 variants (`inlining_1` through `inlining_7`), +each variant adding one more change on top of the previous one. See the file +headers in each variant for the specific incremental change. + +## Code (relevant snippet) + +TBD - see `variants/variant_inlining_7.cpp` for the final version, and the +individual file headers for the incremental steps. + +## Benchmark + +Conditions: single run (TODO: switch to min-of-N), N_ITER=1000, +-O3 -march=native -flto. Predecessor reference for the same config is the +baseline row. + +### x86 + +**Machine**: + +``` +Processors: 16 x AMD Ryzen AI 7 350 w/ Radeon 860M +Memory: 32 GiB of RAM (30.6 GiB usable) +Graphics Processor: AMD Radeon 860M Graphics +Manufacturer: LENOVO +Product Name: 83HX +System Version: IdeaPad Slim 5 14AKP10 +``` + +**Frequency**: 2 GHz + +Existing single-point measurement (N unspecified, kept from the previous +OPTIMIZATIONS.md; W matches roughly N=120 at N_ITER=1000): + +| Variant | W(n) [Flops] | T(n) [Cycles] | Performance [Flops/Cycle] | +| -------- | ---------------- | -------------- | ------------------------- | +| Baseline | 1'058'400'000.00 | 850'352'900.00 | 1.24 | +| Inlining | 1'058'400'000.00 | 756'088'260.00 | 1.40 | + +Inlining is about 12.9% faster on this point. A full N sweep with min-of-3 is TODO. + +Full sweep (TODO): + +| N | W(n) [Flops] | T(n) [Cycles] | Performance [Flops/Cycle] | Diff vs Predecessor | +| ---- | ------------ | ------------- | ------------------------- | ------------------- | +| 50 | | | | | +| 100 | | | | | +| 200 | | | | | +| 500 | | | | | +| 1000 | | | | | + +### ARM (Apple) + +**Machine**: + +``` +TBD +``` + +**Frequency**: TBD + +| N | W(n) [Flops] | T(n) [Cycles] | Performance [Flops/Cycle] | Diff vs Predecessor | +| ---- | ------------ | ------------- | ------------------------- | ------------------- | +| 50 | | | | | +| 100 | | | | | +| 200 | | | | | +| 500 | | | | | +| 1000 | | | | | + +## Analysis + +TBD - speedup likely comes from a mix of: eliminating function-call overhead at +the ~2*N^2 hot-call sites, the struct-of-doubles `Pedestrian` improving +register allocation, and the algebraic sqrt reductions cutting expensive ops. +Worth confirming by looking at the generated assembly for the inner loop in +`variant_inlining_7.cpp`. + +## Takeaway + +TBD diff --git a/docs/optimization_template.md b/docs/optimization_template.md new file mode 100644 index 0000000..1fbedbd --- /dev/null +++ b/docs/optimization_template.md @@ -0,0 +1,76 @@ + + +# Optimization: + +- **Author**: +- **Predecessor**: variant_baseline.cpp +- **File(s)**: variants/variant_.cpp +- **Status**: + +## What this changes + +<2-3 sentences. What did you change and what is the high-level idea? +No full code paste here - just a description.> + +## Code (relevant snippet) + +```cpp +// the part that differs from the predecessor +``` + +## Benchmark + +Conditions: min of runs, N_ITER=1000, -O3 -march=native -flto. + +### x86 + +**Machine**: + +``` + +``` + +**Frequency**: + +| N | W(n) [Flops] | T(n) [Cycles] | Performance [Flops/Cycle] | Diff vs Predecessor | +| ---- | ------------ | ------------- | ------------------------- | ------------------- | +| 50 | | | | | +| 100 | | | | | +| 200 | | | | | +| 500 | | | | | +| 1000 | | | | | + +### ARM (Apple) + +**Machine**: + +``` + +``` + +**Frequency**: + +| N | W(n) [Flops] | T(n) [Cycles] | Performance [Flops/Cycle] | Diff vs Predecessor | +| ---- | ------------ | ------------- | ------------------------- | ------------------- | +| 50 | | | | | +| 100 | | | | | +| 200 | | | | | +| 500 | | | | | +| 1000 | | | | | + +## Analysis + + + +## Takeaway + +