Merge branch 'linus/variant_avx256' into 'main'
SoA implementation See merge request course-asl-fastcode26/team70!26
This commit is contained in:
@@ -0,0 +1,70 @@
|
|||||||
|
# Optimization: Struct of Arrays (SoA)
|
||||||
|
|
||||||
|
- **Author**: Linus
|
||||||
|
- **Predecessor**: variant_inlining_7.cpp
|
||||||
|
- **File(s)**: variants/variant_inlining_7_SoA.cpp
|
||||||
|
- **Status**: TBD
|
||||||
|
|
||||||
|
## What this changes
|
||||||
|
|
||||||
|
Replaced the `Pedestrian` struct with parallel `vector<double>` arrays, one
|
||||||
|
per field (`pos_x`, `pos_y`, `e_x`, `e_y`, `sb_e_x`, `sb_e_y`, `sb_sq`, ...).
|
||||||
|
Board obstacles are left as AoS since there are only a handful of them.
|
||||||
|
|
||||||
|
Mostly a setup step for AVX2: with SoA the inner ped-ped loop reads
|
||||||
|
contiguous chunks of `pos_x[j..]`, which is what we'll want for aligned
|
||||||
|
vector loads.
|
||||||
|
|
||||||
|
## Code (relevant snippet)
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
vector<double> pos_x, pos_y;
|
||||||
|
vector<double> sb_sq, sb_e_x, sb_e_y;
|
||||||
|
vector<double> e_x, e_y;
|
||||||
|
vector<double> curr_vel_x, curr_vel_y;
|
||||||
|
// ...
|
||||||
|
```
|
||||||
|
|
||||||
|
See `variants/variant_inlining_7_SoA.cpp`.
|
||||||
|
|
||||||
|
## Benchmark
|
||||||
|
|
||||||
|
Conditions: 2 runs, N_ITER=1000, -O3 -march=native -flto. Diff is vs
|
||||||
|
`variant_inlining_7`.
|
||||||
|
On my local machine i had an improvement of about 10-15% for low N and about 5% speed up for higher N
|
||||||
|
|
||||||
|
### 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
|
||||||
|
|
||||||
|
No in depth Analysis done, just thought i wanna share it so you can also use it for further optimizations
|
||||||
|
|
||||||
|
## Takeaway
|
||||||
|
|
||||||
|
Layout change, not a performance win by itself. Sets up the AVX2 variant.
|
||||||
@@ -0,0 +1,343 @@
|
|||||||
|
// # This file uses avx2
|
||||||
|
// # This is the first variant and should serve as a base for further avx2 optimizations
|
||||||
|
|
||||||
|
// Okay our first step is to use a SoA instead of an AoS
|
||||||
|
// We only do SoA for the pedestrian struct, there we wanna optimize such that we access for the ped-ped forces adjacent pedestrians (in the arrays)
|
||||||
|
|
||||||
|
#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;
|
||||||
|
|
||||||
|
// we wanna use arrays so we can implement SOA, we create arrays for all the data we need and access
|
||||||
|
vector<double> pos_x, pos_y;
|
||||||
|
vector<double> sb_sq;
|
||||||
|
vector<double> sb_e_x, sb_e_y;
|
||||||
|
vector<double> e_x, e_y;
|
||||||
|
vector<double> curr_vel_x, curr_vel_y;
|
||||||
|
vector<double> w_x, w_y;
|
||||||
|
vector<double> desired_v0;
|
||||||
|
vector<double> v_max;
|
||||||
|
vector<double> destination_x, destination_y;
|
||||||
|
vector<int> color;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
// resize all our arrays for this run
|
||||||
|
pos_x.resize(n_p);
|
||||||
|
pos_y.resize(n_p);
|
||||||
|
sb_sq.resize(n_p);
|
||||||
|
sb_e_x.resize(n_p);
|
||||||
|
sb_e_y.resize(n_p);
|
||||||
|
e_x.resize(n_p);
|
||||||
|
e_y.resize(n_p);
|
||||||
|
curr_vel_x.resize(n_p);
|
||||||
|
curr_vel_y.resize(n_p);
|
||||||
|
w_x.resize(n_p);
|
||||||
|
w_y.resize(n_p);
|
||||||
|
desired_v0.resize(n_p);
|
||||||
|
v_max.resize(n_p);
|
||||||
|
destination_x.resize(n_p);
|
||||||
|
destination_y.resize(n_p);
|
||||||
|
color.resize(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 dest_x = BOARD_LENGTH + 30.0; // renamed to avoid shadowing destination_x[]
|
||||||
|
double dest_y = y; // same idea for destination_y[]
|
||||||
|
|
||||||
|
double init_e_x = dest_x - x;
|
||||||
|
double init_e_y = dest_y - 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 d_v0;
|
||||||
|
do {
|
||||||
|
d_v0 = velocity_gen(gen);
|
||||||
|
} while (d_v0 <= 0.1);
|
||||||
|
|
||||||
|
double v_m = 1.3 * d_v0;
|
||||||
|
|
||||||
|
color[i] = 0;
|
||||||
|
pos_x[i] = x;
|
||||||
|
pos_y[i] = y;
|
||||||
|
e_x[i] = init_e_unit_x;
|
||||||
|
e_y[i] = init_e_unit_y;
|
||||||
|
v_max[i] = v_m;
|
||||||
|
desired_v0[i] = d_v0;
|
||||||
|
destination_x[i] = dest_x;
|
||||||
|
destination_y[i] = dest_y;
|
||||||
|
|
||||||
|
// Fields the Pedestrian constructor would have zero-initialized:
|
||||||
|
w_x[i] = 0.0;
|
||||||
|
w_y[i] = 0.0;
|
||||||
|
curr_vel_x[i] = 0.0;
|
||||||
|
curr_vel_y[i] = 0.0;
|
||||||
|
sb_sq[i] = 0.0;
|
||||||
|
sb_e_x[i] = 0.0;
|
||||||
|
sb_e_y[i] = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = n_p_half; i < n_p; i++) {
|
||||||
|
double x = x_gen_right(gen);
|
||||||
|
double y = y_gen(gen);
|
||||||
|
|
||||||
|
double dest_x = -30;
|
||||||
|
double dest_y = y;
|
||||||
|
|
||||||
|
double init_e_x = dest_x - x;
|
||||||
|
double init_e_y = dest_y - 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 d_v0;
|
||||||
|
do {
|
||||||
|
d_v0 = velocity_gen(gen);
|
||||||
|
} while (d_v0 <= 0.1);
|
||||||
|
|
||||||
|
double v_m = 1.3 * d_v0;
|
||||||
|
|
||||||
|
color[i] = 1;
|
||||||
|
pos_x[i] = x;
|
||||||
|
pos_y[i] = y;
|
||||||
|
e_x[i] = init_e_unit_x;
|
||||||
|
e_y[i] = init_e_unit_y;
|
||||||
|
v_max[i] = v_m;
|
||||||
|
desired_v0[i] = d_v0;
|
||||||
|
destination_x[i] = dest_x;
|
||||||
|
destination_y[i] = dest_y;
|
||||||
|
|
||||||
|
// Fields the Pedestrian constructor would have zero-initialized:
|
||||||
|
w_x[i] = 0.0;
|
||||||
|
w_y[i] = 0.0;
|
||||||
|
curr_vel_x[i] = 0.0;
|
||||||
|
curr_vel_y[i] = 0.0;
|
||||||
|
sb_sq[i] = 0.0;
|
||||||
|
sb_e_x[i] = 0.0;
|
||||||
|
sb_e_y[i] = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
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, pos_x[i], pos_y[i], e_x[i], e_y[i], color[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
for (int t = 0; t < n_iter; t++) {
|
||||||
|
|
||||||
|
for (int i = 0; i < n_p; i++) {
|
||||||
|
|
||||||
|
double desired_velocity_x = e_x[i] * desired_v0[i];
|
||||||
|
double desired_velocity_y = e_y[i] * desired_v0[i];
|
||||||
|
|
||||||
|
double f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
|
||||||
|
double f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
|
||||||
|
|
||||||
|
for (int j = 0; j < n_p; j++) {
|
||||||
|
|
||||||
|
if (i == j)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
double r_ab_x = pos_x[i] - pos_x[j];
|
||||||
|
double r_ab_y = pos_y[i] - pos_y[j];
|
||||||
|
|
||||||
|
double r_ab_norm = sqrt(r_ab_x * r_ab_x + r_ab_y * r_ab_y);
|
||||||
|
|
||||||
|
double r_ab2_x = r_ab_x - sb_e_x[j];
|
||||||
|
double r_ab2_y = r_ab_y - sb_e_y[j];
|
||||||
|
|
||||||
|
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 - sb_sq[j];
|
||||||
|
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 = (e_x[i] * f_alpha_beta_neg_x + e_y[i] * 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int k = 0; k < board.n_obstacles; k++) {
|
||||||
|
|
||||||
|
double v1_x = pos_x[i] - board.obstacles[k].start.x;
|
||||||
|
double v1_y = pos_y[i] - 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 = pos_x[i] - (board.obstacles[k].start.x + board.obstacles[k].obstacle_v.x * projection);
|
||||||
|
double r_aB_y = pos_y[i] - (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 = (e_x[i] * f_alpha_B_neg_x + e_y[i] * 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
w_x[i] += f_alpha_dt_x;
|
||||||
|
w_y[i] += f_alpha_dt_y;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < n_p; i++) {
|
||||||
|
|
||||||
|
double w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]);
|
||||||
|
|
||||||
|
double correction_factor = min(1.0, v_max[i] / w_norm);
|
||||||
|
|
||||||
|
curr_vel_x[i] = w_x[i] * correction_factor;
|
||||||
|
curr_vel_y[i] = w_y[i] * correction_factor;
|
||||||
|
|
||||||
|
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt;
|
||||||
|
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt;
|
||||||
|
|
||||||
|
if (color[i] == 0 && pos_x[i] > (BOARD_LENGTH - 1.5)) {
|
||||||
|
pos_x[i] = 1.0;
|
||||||
|
destination_y[i] = pos_y[i];
|
||||||
|
w_x[i] = desired_v0[i];
|
||||||
|
w_y[i] = 0.0;
|
||||||
|
curr_vel_x[i] = w_x[i];
|
||||||
|
curr_vel_y[i] = w_y[i];
|
||||||
|
}
|
||||||
|
if (color[i] == 1 && pos_x[i] < 1.5) {
|
||||||
|
pos_x[i] = BOARD_LENGTH - 1.0;
|
||||||
|
destination_y[i] = pos_y[i];
|
||||||
|
w_x[i] = -desired_v0[i];
|
||||||
|
w_y[i] = 0.0;
|
||||||
|
curr_vel_x[i] = w_x[i];
|
||||||
|
curr_vel_y[i] = w_y[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
pos_y[i] = clamp(pos_y[i], WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
|
||||||
|
|
||||||
|
double ex = destination_x[i] - pos_x[i];
|
||||||
|
double ey = destination_y[i] - pos_y[i];
|
||||||
|
|
||||||
|
double e_norm_inv = 1.0 / sqrt(ex * ex + ey * ey);
|
||||||
|
|
||||||
|
e_x[i] = ex * e_norm_inv;
|
||||||
|
e_y[i] = ey * e_norm_inv;
|
||||||
|
|
||||||
|
double velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i];
|
||||||
|
|
||||||
|
double velocity_norm = sqrt(velocity_norm_sq);
|
||||||
|
|
||||||
|
sb_sq[i] = velocity_norm_sq * delta_t_sq;
|
||||||
|
|
||||||
|
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t);
|
||||||
|
sb_e_y[i] = e_y[i] * (velocity_norm * delta_t);
|
||||||
|
|
||||||
|
#if !BENCHMARK
|
||||||
|
if (dump_path && (t % dump_interval == 0)) {
|
||||||
|
samples.emplace_back(t + 1, i, pos_x[i], pos_y[i], e_x[i], e_y[i], color[i]);
|
||||||
|
}
|
||||||
|
#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