Merge branch 'pair_symmetry' into 'main'
implemented pair_symmetry that uses a LUT to calculate the distances See merge request course-asl-fastcode26/team70!21
This commit is contained in:
@@ -35,6 +35,7 @@ System Version: IdeaPad Slim 5 14AKP10
|
|||||||
| --- | --------------------------------------- | ---------------- | -------------- | ------------------------- | ------ |
|
| --- | --------------------------------------- | ---------------- | -------------- | ------------------------- | ------ |
|
||||||
| 0 | [Baseline](optimization_baseline.md) | 1'058'400'000.00 | 850'352'900.00 | 1.24 | |
|
| 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 |
|
| 1 | [Inlining](optimization_inlining.md) | 1'058'400'000.00 | 756'088'260.00 | 1.40 | Elliot |
|
||||||
|
| 2 | [Symmetry](optimization_symmetry.md) | | | | Smail |
|
||||||
|
|
||||||
### ARM (Apple)
|
### ARM (Apple)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
# Optimization: Inlining
|
||||||
|
|
||||||
|
- **Author**: Smail
|
||||||
|
- **Predecessor**: variant_inlining_7.cpp
|
||||||
|
- **File(s)**: variants/variant_inlining_7_symmetry_1.cpp
|
||||||
|
- **Status**: TBA
|
||||||
|
|
||||||
|
## What this changes
|
||||||
|
|
||||||
|
Instead of calculating the distance and norm vector between two pedestrians everytime,
|
||||||
|
we created a LUT that only calculates the distances once and r_a_b_norm (with expensive sqrt flop `dx`, `dy`, `norm`).
|
||||||
|
The forces are somewhat symmetric, however it would still require a lot of calculations,
|
||||||
|
maybe this second symmetry optimization can be done at a later stage.
|
||||||
|
|
||||||
|
When i > j we need the opposite vector which is $curr * -1$.
|
||||||
|
|
||||||
|
|
||||||
|
## Code (relevant snippet)
|
||||||
|
|
||||||
|
TBD - see `variants/variant_inlining_7_symmetry_1.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 |
|
||||||
|
| Symmetry | | | |
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
Speedup likely comes mainly from halving the number of `sqrt` flops.
|
||||||
|
|
||||||
|
|
||||||
|
## Takeaway
|
||||||
|
|
||||||
|
TBD
|
||||||
@@ -0,0 +1,319 @@
|
|||||||
|
// # Use symmetrical properties to reduce flop count
|
||||||
|
// # Changes: initialized a LUT to only compute r_a_b_norm once
|
||||||
|
// # Predecessor: variant_inlining_7.cpp
|
||||||
|
|
||||||
|
#include "../headers/board.hpp"
|
||||||
|
#include "../headers/constants.hpp"
|
||||||
|
#include "../headers/pedestrian_reduced.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cmath>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <math.h>
|
||||||
|
#include <random>
|
||||||
|
#include <tuple>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#define SEED 42
|
||||||
|
#define BOARD_WIDTH 10.0
|
||||||
|
#define BOARD_LENGTH 50.0
|
||||||
|
#define WALL_EPS 1e-3
|
||||||
|
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999
|
||||||
|
|
||||||
|
#ifndef RANDOM_SEED
|
||||||
|
#define RANDOM_SEED 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef BENCHMARK
|
||||||
|
#define BENCHMARK 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
Pedestrian *init_pedestrians(int n_p) {
|
||||||
|
#if RANDOM_SEED
|
||||||
|
random_device rd;
|
||||||
|
mt19937 gen(rd());
|
||||||
|
#else
|
||||||
|
mt19937 gen(SEED);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
normal_distribution<double> velocity_gen(v0_mean, v0_std_dev);
|
||||||
|
uniform_real_distribution<double> y_gen(0.3, BOARD_WIDTH - 0.3);
|
||||||
|
uniform_real_distribution<double> x_gen_left(2.0, BOARD_LENGTH / 2.0);
|
||||||
|
uniform_real_distribution<double> x_gen_right(BOARD_LENGTH / 2.0, BOARD_LENGTH - 2.0);
|
||||||
|
|
||||||
|
Pedestrian *P = new Pedestrian[n_p];
|
||||||
|
|
||||||
|
int n_p_half = n_p / 2;
|
||||||
|
|
||||||
|
for (int i = 0; i < n_p_half; i++) {
|
||||||
|
double x = x_gen_left(gen);
|
||||||
|
double y = y_gen(gen);
|
||||||
|
|
||||||
|
double start_pos_x = x;
|
||||||
|
double start_pos_y = y;
|
||||||
|
|
||||||
|
double destination_x = BOARD_LENGTH + 30.0;
|
||||||
|
double destination_y = y;
|
||||||
|
|
||||||
|
double init_e_x = destination_x - start_pos_x;
|
||||||
|
double init_e_y = destination_y - start_pos_y;
|
||||||
|
double init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
|
||||||
|
double init_e_unit_x = init_e_x / init_e_norm;
|
||||||
|
double init_e_unit_y = init_e_y / init_e_norm;
|
||||||
|
|
||||||
|
double desired_v0;
|
||||||
|
do {
|
||||||
|
desired_v0 = velocity_gen(gen);
|
||||||
|
} while (desired_v0 <= 0.1);
|
||||||
|
|
||||||
|
double v_max = 1.3 * desired_v0;
|
||||||
|
|
||||||
|
P[i] = Pedestrian(0, 0, 0, start_pos_x, start_pos_y, init_e_unit_x, init_e_unit_y, v_max, desired_v0, destination_x, destination_y);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = n_p_half; i < n_p; i++) {
|
||||||
|
double x = x_gen_right(gen);
|
||||||
|
double y = y_gen(gen);
|
||||||
|
|
||||||
|
double start_pos_x = x;
|
||||||
|
double start_pos_y = y;
|
||||||
|
double destination_x = -30;
|
||||||
|
double destination_y = y;
|
||||||
|
|
||||||
|
double init_e_x = destination_x - start_pos_x;
|
||||||
|
double init_e_y = destination_y - start_pos_y;
|
||||||
|
double init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
|
||||||
|
double init_e_unit_x = init_e_x / init_e_norm;
|
||||||
|
double init_e_unit_y = init_e_y / init_e_norm;
|
||||||
|
|
||||||
|
double desired_v0;
|
||||||
|
do {
|
||||||
|
desired_v0 = velocity_gen(gen);
|
||||||
|
} while (desired_v0 <= 0.1);
|
||||||
|
|
||||||
|
double v_max = 1.3 * desired_v0;
|
||||||
|
|
||||||
|
P[i] = Pedestrian(1, 0, 0, start_pos_x, start_pos_y, init_e_unit_x, init_e_unit_y, v_max, desired_v0, destination_x, destination_y);
|
||||||
|
}
|
||||||
|
|
||||||
|
return P;
|
||||||
|
}
|
||||||
|
|
||||||
|
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
|
||||||
|
|
||||||
|
int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, const char *dump_path) {
|
||||||
|
|
||||||
|
#if BENCHMARK
|
||||||
|
(void)dump_path;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !BENCHMARK
|
||||||
|
const int dump_interval = 1;
|
||||||
|
|
||||||
|
vector<tuple<int, int, double, double, double, double, int>> samples;
|
||||||
|
|
||||||
|
if (dump_path) {
|
||||||
|
samples.reserve(static_cast<size_t>(n_iter / dump_interval + 1) * n_p);
|
||||||
|
|
||||||
|
for (int i = 0; i < n_p; i++) {
|
||||||
|
Pedestrian &p = pedestrians[i];
|
||||||
|
samples.emplace_back(0, i, p.curr_pos_x, p.curr_pos_y, p.e_x, p.e_y, p.color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// here we have real (simple) symmetry
|
||||||
|
std::vector<double> r_ab_x_lut(n_p * n_p, 0.0);
|
||||||
|
std::vector<double> r_ab_y_lut(n_p * n_p, 0.0);
|
||||||
|
std::vector<double> r_ab_norm_lut(n_p * n_p, 0.0);
|
||||||
|
// use NAME[i * n_p + j]
|
||||||
|
|
||||||
|
for (int t = 0; t < n_iter; t++) {
|
||||||
|
|
||||||
|
// get all distances first
|
||||||
|
for (int i = 0; i < n_p; i++) {
|
||||||
|
for (int j = i + 1; j < n_p; j++) {
|
||||||
|
double dx = pedestrians[i].curr_pos_x - pedestrians[j].curr_pos_x;
|
||||||
|
double dy = pedestrians[i].curr_pos_y - pedestrians[j].curr_pos_y;
|
||||||
|
double norm = sqrt(dx * dx + dy * dy);
|
||||||
|
r_ab_x_lut[i * n_p + j] = dx;
|
||||||
|
r_ab_y_lut[i * n_p + j] = dy;
|
||||||
|
r_ab_norm_lut[i * n_p + j] = norm;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < n_p; i++) {
|
||||||
|
Pedestrian &p = pedestrians[i];
|
||||||
|
|
||||||
|
double desired_velocity_x = p.e_x * p.desired_v0;
|
||||||
|
double desired_velocity_y = p.e_y * p.desired_v0;
|
||||||
|
|
||||||
|
double f_alpha_dt_x = (desired_velocity_x - p.curr_vel_x) * dt_times_tau_alpha_inv;
|
||||||
|
double f_alpha_dt_y = (desired_velocity_y - p.curr_vel_y) * dt_times_tau_alpha_inv;
|
||||||
|
|
||||||
|
// force between pedestrians
|
||||||
|
for (int j = 0; j < n_p; j++) {
|
||||||
|
if (i == j)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// symmetry
|
||||||
|
//double r_ab_x = p.curr_pos_x - pedestrians[j].curr_pos_x;
|
||||||
|
//double r_ab_y = p.curr_pos_y - pedestrians[j].curr_pos_y;
|
||||||
|
//double r_ab_norm = sqrt(r_ab_x * r_ab_x + r_ab_y * r_ab_y);
|
||||||
|
|
||||||
|
double r_ab_x, r_ab_y, r_ab_norm;
|
||||||
|
if (i < j) {
|
||||||
|
r_ab_x = r_ab_x_lut[i * n_p + j];
|
||||||
|
r_ab_y = r_ab_y_lut[i * n_p + j];
|
||||||
|
r_ab_norm = r_ab_norm_lut[i * n_p + j];
|
||||||
|
} else {
|
||||||
|
// other direction
|
||||||
|
r_ab_x = -1 * r_ab_x_lut[j * n_p + i];
|
||||||
|
r_ab_y = -1 * r_ab_y_lut[j * n_p + i];
|
||||||
|
r_ab_norm = r_ab_norm_lut[j * n_p + i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----
|
||||||
|
|
||||||
|
double r_ab2_x = r_ab_x - pedestrians[j].s_b_e_x;
|
||||||
|
double r_ab2_y = r_ab_y - pedestrians[j].s_b_e_y;
|
||||||
|
|
||||||
|
double r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
|
||||||
|
double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
|
||||||
|
double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - pedestrians[j].s_b_sq;
|
||||||
|
double two_b = sqrt(two_b_sq);
|
||||||
|
double scalar_mult_common = (r_ab_norm_sum / (two_b * r_ab_norm * r_ab2_norm)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b * NEG_TWO_SIGMA_INV));
|
||||||
|
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
|
||||||
|
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
|
||||||
|
|
||||||
|
double f_alpha_beta_neg_x = r_ab_x * scalar_mult_r_ab + r_ab2_x * scalar_mult_r_ab2;
|
||||||
|
double f_alpha_beta_neg_y = r_ab_y * scalar_mult_r_ab + r_ab2_y * scalar_mult_r_ab2;
|
||||||
|
|
||||||
|
double w_val;
|
||||||
|
double a = (p.e_x * f_alpha_beta_neg_x + p.e_y * f_alpha_beta_neg_y) * cos_phi_inv;
|
||||||
|
double b_sq = f_alpha_beta_neg_x * f_alpha_beta_neg_x + f_alpha_beta_neg_y * f_alpha_beta_neg_y;
|
||||||
|
|
||||||
|
w_val = dt_neg;
|
||||||
|
if (a >= 0 && (a * a > b_sq)) {
|
||||||
|
w_val = c_neg_times_dt;
|
||||||
|
}
|
||||||
|
|
||||||
|
f_alpha_dt_x = f_alpha_dt_x + f_alpha_beta_neg_x * w_val;
|
||||||
|
f_alpha_dt_y = f_alpha_dt_y + f_alpha_beta_neg_y * w_val;
|
||||||
|
}
|
||||||
|
|
||||||
|
// force between pedestrian and obstacles
|
||||||
|
for (int k = 0; k < board.n_obstacles; k++) {
|
||||||
|
|
||||||
|
double v1_x = p.curr_pos_x - board.obstacles[k].start.x;
|
||||||
|
double v1_y = p.curr_pos_y - board.obstacles[k].start.y;
|
||||||
|
|
||||||
|
double projection = (v1_x * board.obstacles[k].obstacle_v.x + v1_y * board.obstacles[k].obstacle_v.y) * board.obstacles[k].length_sq_inv;
|
||||||
|
|
||||||
|
if (projection < 0.0)
|
||||||
|
projection = 0.0;
|
||||||
|
else if (projection > 1.0)
|
||||||
|
projection = 1.0;
|
||||||
|
|
||||||
|
double r_aB_x = p.curr_pos_x - (board.obstacles[k].start.x + board.obstacles[k].obstacle_v.x * projection);
|
||||||
|
double r_aB_y = p.curr_pos_y - (board.obstacles[k].start.y + board.obstacles[k].obstacle_v.y * projection);
|
||||||
|
|
||||||
|
double r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
|
||||||
|
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
|
||||||
|
double f_alpha_B_neg_x = r_aB_x * scalar_mult;
|
||||||
|
double f_alpha_B_neg_y = r_aB_y * scalar_mult;
|
||||||
|
|
||||||
|
double w_val;
|
||||||
|
double a = (p.e_x * f_alpha_B_neg_x + p.e_y * f_alpha_B_neg_y) * cos_phi_inv;
|
||||||
|
double b_sq = f_alpha_B_neg_x * f_alpha_B_neg_x + f_alpha_B_neg_y * f_alpha_B_neg_y;
|
||||||
|
|
||||||
|
w_val = dt_neg;
|
||||||
|
if (a >= 0 && (a * a > b_sq)) {
|
||||||
|
w_val = c_neg_times_dt;
|
||||||
|
}
|
||||||
|
|
||||||
|
double f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
|
||||||
|
double f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
|
||||||
|
|
||||||
|
f_alpha_dt_x += f_alpha_B_dt_x;
|
||||||
|
f_alpha_dt_y += f_alpha_B_dt_y;
|
||||||
|
}
|
||||||
|
|
||||||
|
p.w_x += f_alpha_dt_x;
|
||||||
|
p.w_y += f_alpha_dt_y;
|
||||||
|
}
|
||||||
|
|
||||||
|
// update the positions etc.
|
||||||
|
for (int i = 0; i < n_p; i++) {
|
||||||
|
Pedestrian &p = pedestrians[i];
|
||||||
|
|
||||||
|
double w_norm = sqrt(p.w_x * p.w_x + p.w_y * p.w_y);
|
||||||
|
|
||||||
|
double correction_factor = min(1.0, p.v_max / w_norm);
|
||||||
|
|
||||||
|
p.curr_vel_x = p.w_x * correction_factor;
|
||||||
|
p.curr_vel_y = p.w_y * correction_factor;
|
||||||
|
|
||||||
|
p.curr_pos_x = p.curr_pos_x + p.curr_vel_x * dt;
|
||||||
|
p.curr_pos_y = p.curr_pos_y + p.curr_vel_y * dt;
|
||||||
|
|
||||||
|
if (p.color == 0 && p.curr_pos_x > (BOARD_LENGTH - 1.5)) {
|
||||||
|
p.curr_pos_x = 1.0;
|
||||||
|
p.destination_y = p.curr_pos_y;
|
||||||
|
p.w_x = p.desired_v0;
|
||||||
|
p.w_y = 0.0;
|
||||||
|
p.curr_vel_x = p.w_x;
|
||||||
|
p.curr_vel_y = p.w_y;
|
||||||
|
}
|
||||||
|
if (p.color == 1 && p.curr_pos_x < 1.5) {
|
||||||
|
p.curr_pos_x = BOARD_LENGTH - 1.0;
|
||||||
|
p.destination_y = p.curr_pos_y;
|
||||||
|
p.w_x = -p.desired_v0;
|
||||||
|
p.w_y = 0.0;
|
||||||
|
p.curr_vel_x = p.w_x;
|
||||||
|
p.curr_vel_y = p.w_y;
|
||||||
|
}
|
||||||
|
|
||||||
|
p.curr_pos_y = clamp(p.curr_pos_y, WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
|
||||||
|
|
||||||
|
double e_x = p.destination_x - p.curr_pos_x;
|
||||||
|
double e_y = p.destination_y - p.curr_pos_y;
|
||||||
|
|
||||||
|
double e_norm_inv = 1.0 / sqrt(e_x * e_x + e_y * e_y);
|
||||||
|
|
||||||
|
p.e_x = e_x * e_norm_inv;
|
||||||
|
p.e_y = e_y * e_norm_inv;
|
||||||
|
|
||||||
|
double velocity_norm_sq = p.curr_vel_x * p.curr_vel_x + p.curr_vel_y * p.curr_vel_y;
|
||||||
|
|
||||||
|
double velocity_norm = sqrt(velocity_norm_sq);
|
||||||
|
|
||||||
|
p.s_b_sq = velocity_norm_sq * delta_t_sq;
|
||||||
|
|
||||||
|
p.s_b_e_x = p.e_x * (velocity_norm * delta_t);
|
||||||
|
p.s_b_e_y = p.e_y * (velocity_norm * delta_t);
|
||||||
|
|
||||||
|
#if !BENCHMARK
|
||||||
|
if (dump_path && (t % dump_interval == 0)) {
|
||||||
|
samples.emplace_back(t + 1, i, p.curr_pos_x, p.curr_pos_y, p.e_x, p.e_y, p.color);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !BENCHMARK
|
||||||
|
if (dump_path) {
|
||||||
|
ofstream out(dump_path);
|
||||||
|
out << "time,id,x,y,p.e.x,p.e.y,color\n";
|
||||||
|
|
||||||
|
for (auto &[t, id, x, y, e_x, e_y, c] : samples) {
|
||||||
|
out << t << ',' << id << ',' << x << ',' << y << ',' << e_x << ',' << e_y << ',' << c << '\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user