Perf plot

This commit is contained in:
[Quy Anh] «Elliot» Nguyen
2026-06-18 11:59:50 +02:00
committed by Linus Baumberger
parent ca969564a6
commit e7af3103e8
153 changed files with 22042 additions and 24220 deletions
-426
View File
@@ -1,426 +0,0 @@
// This file is merged from the forces.cpp and social_forces_baseline.cpp
// The goal is that we from now on only have one file as our baseline
// This should make it more easy in the future to implement optimizations
// Our naming convention from now on is: variant_<variant_name>.cpp (Please make sure to keep that consistent, otherwise our Makefile wont work anymore)
// # Changes: FLOPCOUNT
// # Predecessor: variant_branchless.cpp
// n_iter * [n_p ** 2 * (15 A + 24 M + 1 D + 4 S + 1 E) + n_p * (- 4 A - 4 M + 1 D - 1 S - 1 E) + n_obstacles * n_p * (13 A + 19 M + 1 D + 2 S + 1 E)]
#include "../headers/board.hpp"
#include "../headers/constants.hpp"
#include "../headers/forces.hpp"
#include "../headers/pedestrian.hpp"
#include "../headers/social_force.hpp"
#include "../headers/vector2d.hpp"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <math.h> // keep — forces code uses unqualified sqrt/exp
#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;
// forces.cpp
// Flop count: 3 ADDs, 6 MULTs, 1 SQRT
double w(Vector2d e, Vector2d f) {
// 1 ADD + 2 MULT
double dot = e.dot(f);
// 1 ADD + 2 MULT + 1 SQRT
double f_norm = f.norm();
// 1 MULT
double cond = (double)(dot >= f_norm * cos_phi); // Should be 1 if in the field of view, else 0
// 1 ADD + 1 MULT
return c_neg_times_dt + cond * (dt_neg - c_neg_times_dt);
}
// Flop count: 2 ADDs, 4 MULTs
Vector2d acceleration_term_dt(const Pedestrian &alpha) {
// # 2 MULTs
Vector2d desired_velocity = alpha.e * alpha.desired_v0;
Vector2d actual_velocity = alpha.curr_vel;
// # 2 ADDs, 2 MULTs
Vector2d f_alpha_0_dt = (desired_velocity - actual_velocity) * dt_times_tau_alpha_inv;
return f_alpha_0_dt;
}
// Flop count: 13 ADDs, 24 MULTs, 1 DIV, 4 SQRTs, 1 EXP
Vector2d pedestrian_repulsion_dt(const Pedestrian &alpha, const Pedestrian &beta) {
// # 2 ADDs
// $\vec{r}_{\alpha\beta}$
Vector2d r_ab = alpha.curr_pos - beta.curr_pos;
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert$
double r_ab_norm = r_ab.norm();
r_ab_norm = r_ab_norm > EPS ? r_ab_norm : EPS; // Avoid division by zero
// $v_\beta \Delta t \vec{e}_\beta$
Vector2d s_b_e = beta.s_b_e;
// # 2 ADDs
// $\vec{r}_{\alpha\beta} - \vec{v}_{\beta}\varDelta_{t}\vec{e}_{\beta}$
Vector2d r_ab2 = r_ab - s_b_e;
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
double r_ab2_norm = r_ab2.norm();
r_ab2_norm = r_ab2_norm > EPS ? r_ab2_norm : EPS; // Avoid division by zero
// # 1 ADD
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
// # 1 ADD, 1 MULTs, 1 SQRT
//
// $2b = \sqrt{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)^2 - (v_\beta \Delta t)^2}$
//
double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - beta.s_b_sq;
two_b_sq = two_b_sq > EPS_SQ ? two_b_sq : EPS_SQ; // Avoid sqrt of negative and avoid division by zero
double two_b = sqrt(two_b_sq);
// # 5 MULTs, 1 DIV, 1 EXP
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}$
//
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));
// # 1 MULT
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}$
//
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// # 1 MULT
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}$
//
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
// # 2 ADDs, 4 MULTs
//
// $\vec{-f}_{\alpha \beta}(\vec{r}_{\alpha \beta}) = \frac{-V^0_{\alpha \beta}}{2\sigma}e^{-b / \sigma} 2\grad_{\vec{r}_{\alpha \beta}}b $
//
//
// $= \frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}\,\vec{r}_{\alpha \beta} + \frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} -
// v_\beta \Delta t \vec{e}_\beta \Arrowvert}\,(\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta)$
//
//
//
// $2\grad_{\vec{r}_{\alpha \beta}}b = \frac{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)(\frac{\vec{r}_{\alpha \beta}}{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert} + \frac{\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta}{\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert})}{2b}$
//
//
// $= \frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}\,\vec{r}_{\alpha \beta} + \frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}\,(\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta)$
//
//
Vector2d f_alpha_beta_neg = r_ab * scalar_mult_r_ab + r_ab2 * scalar_mult_r_ab2;
// # 3 ADDs, 8 MULTs, 1 SQRT
//
// $\vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha \beta}).dt = w(\vec{e}_{\alpha}, -\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta})).\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta}).dt$
//
Vector2d f_alpha_beta_dt = f_alpha_beta_neg * w(alpha.e, f_alpha_beta_neg);
return f_alpha_beta_dt;
}
// Flop count: 11 ADDs, 19 MULTs, 1 DIVs, 2 SQRTs, 1 EXP
Vector2d obstacle_repulsion_dt(const Pedestrian &alpha, const Obstacle &B) {
// # 7 ADDs, 5 MULTs
// $\vec{r}_{\alpha B}$
Vector2d r_aB = alpha.curr_pos - B.find_nearest_obstacle_point(alpha.curr_pos);
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha B} \Arrowvert$
double r_aB_norm = r_aB.norm();
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS; // Avoid division by zero
// # 2 MULTs, 1 DIV, 1 EXP
//
// $\frac{-U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R}$
//
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// # 2 MULTs
//
// $-\vec{F}_{\alpha B}(\vec{r}_{\alpha B}) = \frac{-U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R} \frac{\vec{r}_{\alpha B}}{\Arrowvert \vec{r}_{\alpha B} \Arrowvert}$
//
Vector2d f_alpha_B_neg = r_aB * scalar_mult;
// # 3 ADDs, 8 MULTs, 1 SQRT
//
// $\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha B}).dt = w(\vec{e}_{\alpha}, -\vec{F}_{\alpha B}(\vec{r}_{\alpha B})).\vec{F}_{\alpha B}(\vec{r}_{\alpha B}).dt$
//
Vector2d f_alpha_B_dt = f_alpha_B_neg * w(alpha.e, f_alpha_B_neg);
return f_alpha_B_dt;
}
// social_force_baseline.cpp
Pedestrian *init_pedestrians(int n_p) {
#if RANDOM_SEED
random_device rd; // Platform-provided random seed source.
mt19937 gen(rd()); // Non-reproducible initialization.
#else
mt19937 gen(SEED); // Reproducible initialization.
#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;
// Group of pedestrians walking from left to right.
for (int i = 0; i < n_p_half; i++) {
double x = x_gen_left(gen);
double y = y_gen(gen);
Vector2d start_pos = Vector2d(x, y);
Vector2d destination = Vector2d(BOARD_LENGTH + 30.0, y);
Vector2d init_e = destination - start_pos;
double init_e_norm = init_e.norm();
Vector2d init_e_unit = init_e / init_e_norm;
// Reject near-zero desired speeds to avoid pedestrians that barely move,
// and reject negative speeds because they would reverse the intended
// walking direction.
double desired_v0;
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1);
double v_max = 1.3 * desired_v0;
P[i] = Pedestrian(WHITE, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
}
// Group of pedestrians walking from right to left.
for (int i = n_p_half; i < n_p; i++) {
double x = x_gen_right(gen);
double y = y_gen(gen);
Vector2d start_pos = Vector2d(x, y);
Vector2d destination = Vector2d(-30.0, y);
Vector2d init_e = destination - start_pos;
double init_e_norm = init_e.norm();
Vector2d init_e_unit = init_e / init_e_norm;
// Reject near-zero desired speeds to avoid pedestrians that barely move,
// and reject negative speeds because they would reverse the intended
// walking direction.
double desired_v0;
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1);
double v_max = 1.3 * desired_v0;
P[i] = Pedestrian(BLACK, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
}
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
// Avoid compiler unused parameter warning
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
// Each sample stores: time step, pedestrian id, x, y, e.x, e.y, color.
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);
// Store the initial state before the first simulation step.
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
// n_iter * [ {n_p * {[(n_p - 1) * (13 ADDs, 24 MULTs, 1 DIV, 4 SQRTs, 1 EXP + 2 ADDs)] + [2 ADD + 4 MULT + (n_obstacles * (11 ADDs, 19 MULTs, 1 DIV, 2 SQRTs, 1 EXP) + n_obstacles * 2 ADDs) + 2 ADD ]}} + {(7 ADDs, 16 MULTs, 2 DIVs, 3 SQRTs) * n_p} ]
// n_iter * [ {n_p * {[(n_p - 1) * (13 A + 24 M + 1 D + 4 S + 1 E + 2 A)] + [2 A + 4 M + (n_obstacles * (11 A + 19 M + 1 D + 2 S + 1 E) + n_obstacles * 2 A) + 2 A ]}} + {n_p * (7 A + 16 M + 2 D + 3 S)} ]
// n_iter * [n_p ** 2 * (15 A + 24 M + 1 D + 4 S + 1 E) + n_p * (- 11 A - 20 M - 1 D - 4 S - 1 E) + n_obstacles * n_p * (13 A + 19 M + 1 D + 2 S + 1 E) + n_p * (7 A + 16 M + 2 D + 3 S)]
// n_iter * [n_p ** 2 * (15 A + 24 M + 1 D + 4 S + 1 E) + n_p * (- 11 A - 20 M - 1 D - 4 S - 1 E) + n_p * (7 A + 16 M + 2 D + 3 S) + n_obstacles * n_p * (13 A + 19 M + 1 D + 2 S + 1 E)]
// n_iter * [n_p ** 2 * (15 A + 24 M + 1 D + 4 S + 1 E) + n_p * (- 4 A - 4 M + 1 D - 1 S - 1 E) + n_obstacles * n_p * (13 A + 19 M + 1 D + 2 S + 1 E)]
for (int t = 0; t < n_iter; t++) {
// Pass 1: Compute all forces from the current state.
// n_p * {[(n_p - 1) * (13 ADDs, 24 MULTs, 1 DIV, 4 SQRTs, 1 EXP + 2 ADDs)] + [(2 ADD + 4 MULT + (n_obstacles * (11 ADDs, 19 MULTs, 1 DIV, 2 SQRTs, 1 EXP) + n_obstacles * 2 ADDs) + 2 ADD) ]}
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// # 2 ADDs, 4 MULTs
//
// $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$
//
Vector2d f_alpha_dt = acceleration_term_dt(p);
// # (N-1) * (13 ADDs, 24 MULTs, 1 DIV, 4 SQRTs, 1 EXP)
// + (N-1) * 2 ADDs
//
// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha},\vec{r}_{\alpha} - \vec{r}_{\beta})$
//
// for (int j = 0; j < n_p; j++) {
// if (i == j)
// continue;
//
// f_alpha_dt += pedestrian_repulsion_dt(p, pedestrians[j]);
//}
for (int j = 0; j < i; j++) {
f_alpha_dt += pedestrian_repulsion_dt(p, pedestrians[j]);
}
for (int j = i + 1; j < n_p; j++) {
f_alpha_dt += pedestrian_repulsion_dt(p, pedestrians[j]);
}
// # board.n_obstacles * (11 ADDs, 19 MULTs, 1 DIV, 2 SQRTs, 1 EXP)
// + board.n_obstacles * 2 ADDs
//
// $\sum_{B} \vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$
//
for (int k = 0; k < board.n_obstacles; k++) {
f_alpha_dt += obstacle_repulsion_dt(p, board.obstacles[k]);
}
// # 2 ADDs
//
//
// $\frac{d\vec{w}_\alpha}{dt} = \vec{F}_{\alpha}(t)$
//
p.w += f_alpha_dt;
}
// Pass 2: Update pedestrian states after all forces have been computed.
// # (7 ADDs, 16 MULTs, 2 DIVs, 3 SQRTs) * n_p
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// # 1 ADD, 2 MULTs, 1 SQRT
double w_norm = p.w.norm();
double w_norm2 = w_norm > EPS ? w_norm : EPS; // Avoid division by zero.
// # 1 DIV
double correction_factor = std::min(1.0, p.v_max / w_norm2);
// # 2 MULTs
p.curr_vel = p.w * correction_factor;
// # 2 ADDs, 2 MULTs
//
// $\vec{r}_i^{t+1} = \vec{r}_i^t + \vec{v}_i^t dt$
//
p.curr_pos = p.curr_pos + p.curr_vel * dt;
// When a pedestrian reaches the end of the corridor, teleport them back
// to the opposite entrance while keeping the same y-coordinate. This
// maintains a constant pedestrian flow and makes lane formation easier to
// observe.
if (p.color == WHITE && p.curr_pos.x > (BOARD_LENGTH - 1.5)) {
p.curr_pos.x = 1.0;
p.destination.y = p.curr_pos.y;
p.w = Vector2d(p.desired_v0, 0.0);
p.curr_vel = p.w;
}
if (p.color == BLACK && p.curr_pos.x < 1.5) {
p.curr_pos.x = BOARD_LENGTH - 1.0;
p.destination.y = p.curr_pos.y;
p.w = Vector2d(-p.desired_v0, 0.0);
p.curr_vel = p.w;
}
// Keep pedestrians inside the board boundaries.
p.curr_pos.y = clamp(p.curr_pos.y, WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
// # 2 ADDs
Vector2d e = p.destination - p.curr_pos;
// # 1 ADD, 2 MULTs, 1 DIV, 1 SQRT
double e_norm = e.norm();
double e_norm2 = std::fmax(e_norm, EPS); // Avoid division by zero.
double e_norm_inv = 1.0 / e_norm2;
// # 2 MULTs
p.e = e * e_norm_inv;
// # 1 ADD, 2 MULTs
double velocity_norm_sq = p.curr_vel.dot(p.curr_vel);
// # 1 SQRT
double velocity_norm = std::sqrt(velocity_norm_sq);
// # 1 MULT
p.s_b_sq = velocity_norm_sq * delta_t_sq;
// # 3 MULTs
p.s_b_e = p.e * (velocity_norm * delta_t);
#if !BENCHMARK
// Store the current pedestrian state for visualization.
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
// Export recorded simulation states to CSV.
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;
}
@@ -1,428 +0,0 @@
// This file is merged from the forces.cpp and social_forces_baseline.cpp
// The goal is that we from now on only have one file as our baseline
// This should make it more easy in the future to implement optimizations
// Our naming convention from now on is: variant_<variant_name>.cpp (Please make sure to keep that consistent, otherwise our Makefile wont work anymore)
// # Changes: FLOPCOUNT
// # Predecessor: variant_branchless_sqrtfree.cpp
// n_iter * [n_p ** 2 * (15 A + 25 M + 1 D + 3 S + 1 E) + n_p * (- 4 A - 5 M + 1 D - 1 E) + n_obstacles * n_p * (13 A + 20 M + 1 D + 1 S + 1 E)]
#include "../headers/board.hpp"
#include "../headers/constants.hpp"
#include "../headers/forces.hpp"
#include "../headers/pedestrian.hpp"
#include "../headers/social_force.hpp"
#include "../headers/vector2d.hpp"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <math.h> // keep — forces code uses unqualified sqrt/exp
#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;
// forces.cpp
// Flop count: 3 ADDs, 7 MULTs (no SQRT, no branch)
// Branchless + sqrt-free: cos_phi < 0 so f.norm()*cos_phi <= 0.
// - If dot >= 0: condition is trivially true.
// - If dot < 0: both sides non-positive; square them safely:
// dot^2 <= f.dot(f) * cos_phi^2.
// Bitwise | (not ||) keeps both sides evaluated and avoids a branch.
// 3 ADD + 7 MULT
double w(Vector2d e, Vector2d f) {
double dot = e.x * f.x + e.y * f.y;
double f_dot_f = f.x * f.x + f.y * f.y;
double cond = (dot >= 0.0) | (dot * dot <= f_dot_f * cos_phi_sq);
return c_neg_times_dt + cond * (dt_neg - c_neg_times_dt);
}
// Flop count: 2 ADDs, 4 MULTs
Vector2d acceleration_term_dt(const Pedestrian &alpha) {
// # 2 MULTs
Vector2d desired_velocity = alpha.e * alpha.desired_v0;
Vector2d actual_velocity = alpha.curr_vel;
// # 2 ADDs, 2 MULTs
Vector2d f_alpha_0_dt = (desired_velocity - actual_velocity) * dt_times_tau_alpha_inv;
return f_alpha_0_dt;
}
// Flop count: 13 ADDs, 25 MULTs, 1 DIV, 3 SQRTs, 1 EXP
Vector2d pedestrian_repulsion_dt(const Pedestrian &alpha, const Pedestrian &beta) {
// # 2 ADDs
// $\vec{r}_{\alpha\beta}$
Vector2d r_ab = alpha.curr_pos - beta.curr_pos;
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert$
double r_ab_norm = r_ab.norm();
r_ab_norm = r_ab_norm > EPS ? r_ab_norm : EPS; // Avoid division by zero
// $v_\beta \Delta t \vec{e}_\beta$
Vector2d s_b_e = beta.s_b_e;
// # 2 ADDs
// $\vec{r}_{\alpha\beta} - \vec{v}_{\beta}\varDelta_{t}\vec{e}_{\beta}$
Vector2d r_ab2 = r_ab - s_b_e;
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
double r_ab2_norm = r_ab2.norm();
r_ab2_norm = r_ab2_norm > EPS ? r_ab2_norm : EPS; // Avoid division by zero
// # 1 ADD
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
// # 1 ADD, 1 MULTs, 1 SQRT
//
// $2b = \sqrt{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)^2 - (v_\beta \Delta t)^2}$
//
double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - beta.s_b_sq;
two_b_sq = two_b_sq > EPS_SQ ? two_b_sq : EPS_SQ; // Avoid sqrt of negative and avoid division by zero
double two_b = sqrt(two_b_sq);
// # 5 MULTs, 1 DIV, 1 EXP
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}$
//
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));
// # 1 MULT
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}$
//
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// # 1 MULT
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}$
//
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
// # 2 ADDs, 4 MULTs
//
// $\vec{-f}_{\alpha \beta}(\vec{r}_{\alpha \beta}) = \frac{-V^0_{\alpha \beta}}{2\sigma}e^{-b / \sigma} 2\grad_{\vec{r}_{\alpha \beta}}b $
//
//
// $= \frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}\,\vec{r}_{\alpha \beta} + \frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} -
// v_\beta \Delta t \vec{e}_\beta \Arrowvert}\,(\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta)$
//
//
//
// $2\grad_{\vec{r}_{\alpha \beta}}b = \frac{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)(\frac{\vec{r}_{\alpha \beta}}{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert} + \frac{\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta}{\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert})}{2b}$
//
//
// $= \frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}\,\vec{r}_{\alpha \beta} + \frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}\,(\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta)$
//
//
Vector2d f_alpha_beta_neg = r_ab * scalar_mult_r_ab + r_ab2 * scalar_mult_r_ab2;
// # 3 ADDs, 9 MULTs
//
// $\vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha \beta}).dt = w(\vec{e}_{\alpha}, -\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta})).\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta}).dt$
//
Vector2d f_alpha_beta_dt = f_alpha_beta_neg * w(alpha.e, f_alpha_beta_neg);
return f_alpha_beta_dt;
}
// Flop count: 11 ADDs, 20 MULTs, 1 DIVs, 1 SQRT, 1 EXP
Vector2d obstacle_repulsion_dt(const Pedestrian &alpha, const Obstacle &B) {
// # 7 ADDs, 5 MULTs
// $\vec{r}_{\alpha B}$
Vector2d r_aB = alpha.curr_pos - B.find_nearest_obstacle_point(alpha.curr_pos);
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha B} \Arrowvert$
double r_aB_norm = r_aB.norm();
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS; // Avoid division by zero
// # 2 MULTs, 1 DIV, 1 EXP
//
// $\frac{-U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R}$
//
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// # 2 MULTs
//
// $-\vec{F}_{\alpha B}(\vec{r}_{\alpha B}) = \frac{-U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R} \frac{\vec{r}_{\alpha B}}{\Arrowvert \vec{r}_{\alpha B} \Arrowvert}$
//
Vector2d f_alpha_B_neg = r_aB * scalar_mult;
// # 3 ADDs, 9 MULTs
//
// $\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha B}).dt = w(\vec{e}_{\alpha}, -\vec{F}_{\alpha B}(\vec{r}_{\alpha B})).\vec{F}_{\alpha B}(\vec{r}_{\alpha B}).dt$
//
Vector2d f_alpha_B_dt = f_alpha_B_neg * w(alpha.e, f_alpha_B_neg);
return f_alpha_B_dt;
}
// social_force_baseline.cpp
Pedestrian *init_pedestrians(int n_p) {
#if RANDOM_SEED
random_device rd; // Platform-provided random seed source.
mt19937 gen(rd()); // Non-reproducible initialization.
#else
mt19937 gen(SEED); // Reproducible initialization.
#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;
// Group of pedestrians walking from left to right.
for (int i = 0; i < n_p_half; i++) {
double x = x_gen_left(gen);
double y = y_gen(gen);
Vector2d start_pos = Vector2d(x, y);
Vector2d destination = Vector2d(BOARD_LENGTH + 30.0, y);
Vector2d init_e = destination - start_pos;
double init_e_norm = init_e.norm();
Vector2d init_e_unit = init_e / init_e_norm;
// Reject near-zero desired speeds to avoid pedestrians that barely move,
// and reject negative speeds because they would reverse the intended
// walking direction.
double desired_v0;
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1);
double v_max = 1.3 * desired_v0;
P[i] = Pedestrian(WHITE, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
}
// Group of pedestrians walking from right to left.
for (int i = n_p_half; i < n_p; i++) {
double x = x_gen_right(gen);
double y = y_gen(gen);
Vector2d start_pos = Vector2d(x, y);
Vector2d destination = Vector2d(-30.0, y);
Vector2d init_e = destination - start_pos;
double init_e_norm = init_e.norm();
Vector2d init_e_unit = init_e / init_e_norm;
// Reject near-zero desired speeds to avoid pedestrians that barely move,
// and reject negative speeds because they would reverse the intended
// walking direction.
double desired_v0;
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1);
double v_max = 1.3 * desired_v0;
P[i] = Pedestrian(BLACK, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
}
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
// Avoid compiler unused parameter warning
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
// Each sample stores: time step, pedestrian id, x, y, e.x, e.y, color.
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);
// Store the initial state before the first simulation step.
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
// n_iter * [{n_p * [(n_p - 1) * (13 ADDs, 25 MULTs, 1 DIV, 3 SQRTs, 1 EXP) + (n_p-1) * (2 ADDs) + n_obstacles * (11 ADDs, 20 MULTs, 1 DIV, 1 SQRTs, 1 EXP) + n_obstacles * (2 ADDs) + 4 ADDs + 4 MULTs]} + {(7 ADDs, 16 MULTs, 2 DIVs, 3 SQRTs) * n_p}]
// n_iter * [{n_p * [(n_p - 1) * (13 A + 25 M + 1 D + 3 S + 1 E) + (n_p - 1) * (2 A) + n_obstacles * (11 A + 20 M + 1 D + 1 S + 1 E) + n_obstacles * (2 A) + 4 A + 4 M]} + {(7 A + 16 M + 2 D + 3 S) * n_p}]
// n_iter * [{n_p * [n_p * (13 A + 25 M + 1 D + 3 S + 1 E) - 1 * (13 A + 25 M + 1 D + 3 S + 1 E) + n_p * (2 A) - 1 * (2 A) + n_obstacles * (11 A + 20 M + 1 D + 1 S + 1 E) + n_obstacles * (2 A) + 4 A + 4 M]} + {n_p * (7 A + 16 M + 2 D + 3 S)}]
// n_iter * [{n_p * [n_p * (15 A + 25 M + 1 D + 3 S + 1 E) - 1 * (15 A + 25 M + 1 D + 3 S + 1 E - 4 A - 4 M) + n_obstacles * (13 A + 20 M + 1 D + 1 S + 1 E)]} + {n_p * (7 A + 16 M + 2 D + 3 S)}]
// n_iter * [n_p ** 2 * (15 A + 25 M + 1 D + 3 S + 1 E) - n_p * (11 A + 21 M + 1 D + 3 S + 1 E) + n_obstacles * n_p * (13 A + 20 M + 1 D + 1 S + 1 E) + n_p * (7 A + 16 M + 2 D + 3 S)]
// n_iter * [n_p ** 2 * (15 A + 25 M + 1 D + 3 S + 1 E) + n_p * (- 11 A - 21 M - 1 D - 3 S - 1 E) + n_p * (7 A + 16 M + 2 D + 3 S) + n_obstacles * n_p * (13 A + 20 M + 1 D + 1 S + 1 E)]
// n_iter * [n_p ** 2 * (15 A + 25 M + 1 D + 3 S + 1 E) + n_p * (- 4 A - 5 M + 1 D - 1 E) + n_obstacles * n_p * (13 A + 20 M + 1 D + 1 S + 1 E)]
for (int t = 0; t < n_iter; t++) {
// Pass 1: Compute all forces from the current state.
// n_p * [(n_p - 1) * (13 ADDs, 24 MULTs, 1 DIV, 3 SQRTs, 1 EXP) + (n_p-1) * (2 ADDs) + n_obstacles * (11 ADDs, 19 MULTs, 1 DIV, 1 SQRT, 1 EXP) + n_obstacles * (2 ADDs) + 4 ADDs + 4 MULTs]
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// # 2 ADDs, 4 MULTs
//
// $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$
//
Vector2d f_alpha_dt = acceleration_term_dt(p);
// # (N-1) * (13 ADDs, 25 MULTs, 1 DIV, 3 SQRT, 1 EXP)
// + (N-1) * 2 ADDs
//
// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha},\vec{r}_{\alpha} - \vec{r}_{\beta})$
//
// for (int j = 0; j < n_p; j++) {
// if (i == j)
// continue;
//
// f_alpha_dt += pedestrian_repulsion_dt(p, pedestrians[j]);
//}
for (int j = 0; j < i; j++) {
f_alpha_dt += pedestrian_repulsion_dt(p, pedestrians[j]);
}
for (int j = i + 1; j < n_p; j++) {
f_alpha_dt += pedestrian_repulsion_dt(p, pedestrians[j]);
}
// # board.n_obstacles * (11 ADDs, 20 MULTs, 1 DIV, 1 SQRT, 1 EXP)
// + board.n_obstacles * 2 ADDs
//
// $\sum_{B} \vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$
//
for (int k = 0; k < board.n_obstacles; k++) {
f_alpha_dt += obstacle_repulsion_dt(p, board.obstacles[k]);
}
// # 2 ADDs
//
//
// $\frac{d\vec{w}_\alpha}{dt} = \vec{F}_{\alpha}(t)$
//
p.w += f_alpha_dt;
}
// Pass 2: Update pedestrian states after all forces have been computed.
// # (7 ADDs, 16 MULTs, 2 DIVs, 3 SQRTs) * n_p
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// # 1 ADD, 2 MULTs, 1 SQRT
double w_norm = p.w.norm();
double w_norm2 = w_norm > EPS ? w_norm : EPS; // Avoid division by zero.
// # 1 DIV
double correction_factor = std::min(1.0, p.v_max / w_norm2);
// # 2 MULTs
p.curr_vel = p.w * correction_factor;
// # 2 ADDs, 2 MULTs
//
// $\vec{r}_i^{t+1} = \vec{r}_i^t + \vec{v}_i^t dt$
//
p.curr_pos = p.curr_pos + p.curr_vel * dt;
// When a pedestrian reaches the end of the corridor, teleport them back
// to the opposite entrance while keeping the same y-coordinate. This
// maintains a constant pedestrian flow and makes lane formation easier to
// observe.
if (p.color == WHITE && p.curr_pos.x > (BOARD_LENGTH - 1.5)) {
p.curr_pos.x = 1.0;
p.destination.y = p.curr_pos.y;
p.w = Vector2d(p.desired_v0, 0.0);
p.curr_vel = p.w;
}
if (p.color == BLACK && p.curr_pos.x < 1.5) {
p.curr_pos.x = BOARD_LENGTH - 1.0;
p.destination.y = p.curr_pos.y;
p.w = Vector2d(-p.desired_v0, 0.0);
p.curr_vel = p.w;
}
// Keep pedestrians inside the board boundaries.
p.curr_pos.y = clamp(p.curr_pos.y, WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
// # 2 ADDs
Vector2d e = p.destination - p.curr_pos;
// # 1 ADD, 2 MULTs, 1 DIV, 1 SQRT
double e_norm = e.norm();
double e_norm2 = std::fmax(e_norm, EPS); // Avoid division by zero.
double e_norm_inv = 1.0 / e_norm2;
// # 2 MULTs
p.e = e * e_norm_inv;
// # 1 ADD, 2 MULTs
double velocity_norm_sq = p.curr_vel.dot(p.curr_vel);
// # 1 SQRT
double velocity_norm = std::sqrt(velocity_norm_sq);
// # 1 MULT
p.s_b_sq = velocity_norm_sq * delta_t_sq;
// # 3 MULTs
p.s_b_e = p.e * (velocity_norm * delta_t);
#if !BENCHMARK
// Store the current pedestrian state for visualization.
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
// Export recorded simulation states to CSV.
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;
}
@@ -1,436 +0,0 @@
// This file is copied from the baseline and modified
// Our goal is to only calculate pedestrian-pedestrian forces for pedestrian which are near
// Our tolerance is 1cm and our cutoff is a 5m radius.
// Since we have some tolerance and we put pedestrians back at the end of the hallway once they are on one end. We only test/verify on a smaller subsample.
// Otherwise we had huge gaps (because of those jumps back)
// In this version we do the cutoff before we call the pedestrian forces function
// # Changes: FLOPCOUNT
// # Predecessor: variant_cutoff_callsite.cpp
// // n_iter * [ n_p ** 2 * (14 A + 23 M + 1 D + 4 S + 1 E) + n_p * (- 3 A - 3 M + 1 D - 1 S - 1 E) + n_obstacles * n_p * (12 A + 18 M + 1 D + 2 S + 1 E) ]
#include "../headers/board.hpp"
#include "../headers/constants.hpp"
#include "../headers/pedestrian.hpp"
#include "../headers/vector2d.hpp"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <math.h> // keep — forces code uses unqualified sqrt/exp
#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;
// forces.cpp
// Flop count: 2 ADDs, 5 MULTs, 1 SQRT
double w(Vector2d e, Vector2d f) {
if (e.dot(f) >= f.norm() * cos_phi)
return dt_neg;
else
return c_neg_times_dt;
}
// Flop count: 2 ADDs, 4 MULTs
Vector2d acceleration_term_dt(const Pedestrian &alpha) {
// # 2 MULTs
Vector2d desired_velocity = alpha.e * alpha.desired_v0;
Vector2d actual_velocity = alpha.curr_vel;
// # 2 ADDs, 2 MULTs
Vector2d f_alpha_0_dt = (desired_velocity - actual_velocity) * dt_times_tau_alpha_inv;
return f_alpha_0_dt;
}
// Flop count: 12 ADDs, 23 MULTs, 1 DIV, 4 SQRTs, 1 EXP
Vector2d pedestrian_repulsion_dt(const Pedestrian &alpha, const Pedestrian &beta) {
// # 2 ADDs
// $\vec{r}_{\alpha\beta}$
Vector2d r_ab = alpha.curr_pos - beta.curr_pos;
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert$
double r_ab_norm = r_ab.norm();
// Now we wanna check if it is a close pedestrian or wheter it is far away and can be ignored
// double r_ab_sq = r_ab.x * r_ab.x + r_ab.y * r_ab.y; // squared distance
// if (r_ab_sq > CUTOFF_SQ)
// return Vector2d(0.0, 0.0); // cheap reject
// double r_ab_norm = sqrt(r_ab_sq); // reuse r_ab_sq, no recompute
r_ab_norm = r_ab_norm > EPS ? r_ab_norm : EPS; // Avoid division by zero
// $v_\beta \Delta t \vec{e}_\beta$
Vector2d s_b_e = beta.s_b_e;
// # 2 ADDs
// $\vec{r}_{\alpha\beta} - \vec{v}_{\beta}\varDelta_{t}\vec{e}_{\beta}$
Vector2d r_ab2 = r_ab - s_b_e;
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
double r_ab2_norm = r_ab2.norm();
r_ab2_norm = r_ab2_norm > EPS ? r_ab2_norm : EPS; // Avoid division by zero
// # 1 ADD
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
// # 1 ADD, 1 MULTs, 1 SQRT
//
// $2b = \sqrt{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)^2 - (v_\beta \Delta t)^2}$
//
double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - beta.s_b_sq;
two_b_sq = two_b_sq > EPS_SQ ? two_b_sq : EPS_SQ; // Avoid sqrt of negative and avoid division by zero
double two_b = sqrt(two_b_sq);
// # 5 MULTs, 1 DIV, 1 EXP
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}$
//
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));
// # 1 MULT
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}$
//
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// # 1 MULT
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}$
//
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
// # 2 ADDs, 4 MULTs
//
// $\vec{-f}_{\alpha \beta}(\vec{r}_{\alpha \beta}) = \frac{-V^0_{\alpha \beta}}{2\sigma}e^{-b / \sigma} 2\grad_{\vec{r}_{\alpha \beta}}b $
//
//
// $= \frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}\,\vec{r}_{\alpha \beta} + \frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} -
// v_\beta \Delta t \vec{e}_\beta \Arrowvert}\,(\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta)$
//
//
//
// $2\grad_{\vec{r}_{\alpha \beta}}b = \frac{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)(\frac{\vec{r}_{\alpha \beta}}{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert} + \frac{\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta}{\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert})}{2b}$
//
//
// $= \frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}\,\vec{r}_{\alpha \beta} + \frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}\,(\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta)$
//
//
Vector2d f_alpha_beta_neg = r_ab * scalar_mult_r_ab + r_ab2 * scalar_mult_r_ab2;
// # 2 ADDs, 7 MULTs, 1 SQRT
//
// $\vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha \beta}).dt = w(\vec{e}_{\alpha}, -\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta})).\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta}).dt$
//
Vector2d f_alpha_beta_dt = f_alpha_beta_neg * w(alpha.e, f_alpha_beta_neg);
return f_alpha_beta_dt;
}
// Flop count: 10 ADDs, 18 MULTs, 1 DIVs, 2 SQRTs, 1 EXP
Vector2d obstacle_repulsion_dt(const Pedestrian &alpha, const Obstacle &B) {
// # 7 ADDs, 5 MULTs
// $\vec{r}_{\alpha B}$
Vector2d r_aB = alpha.curr_pos - B.find_nearest_obstacle_point(alpha.curr_pos);
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha B} \Arrowvert$
double r_aB_norm = r_aB.norm();
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS; // Avoid division by zero
// # 2 MULTs, 1 DIV, 1 EXP
//
// $\frac{-U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R}$
//
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// # 2 MULTs
//
// $-\vec{F}_{\alpha B}(\vec{r}_{\alpha B}) = \frac{-U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R} \frac{\vec{r}_{\alpha B}}{\Arrowvert \vec{r}_{\alpha B} \Arrowvert}$
//
Vector2d f_alpha_B_neg = r_aB * scalar_mult;
// # 2 ADDs, 7 MULTs, 1 SQRT
//
// $\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha B}).dt = w(\vec{e}_{\alpha}, -\vec{F}_{\alpha B}(\vec{r}_{\alpha B})).\vec{F}_{\alpha B}(\vec{r}_{\alpha B}).dt$
//
Vector2d f_alpha_B_dt = f_alpha_B_neg * w(alpha.e, f_alpha_B_neg);
return f_alpha_B_dt;
}
// social_force_baseline.cpp
Pedestrian *init_pedestrians(int n_p) {
#if RANDOM_SEED
random_device rd; // Platform-provided random seed source.
mt19937 gen(rd()); // Non-reproducible initialization.
#else
mt19937 gen(SEED); // Reproducible initialization.
#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;
// Group of pedestrians walking from left to right.
for (int i = 0; i < n_p_half; i++) {
double x = x_gen_left(gen);
double y = y_gen(gen);
Vector2d start_pos = Vector2d(x, y);
Vector2d destination = Vector2d(BOARD_LENGTH + 30.0, y);
Vector2d init_e = destination - start_pos;
double init_e_norm = init_e.norm();
Vector2d init_e_unit = init_e / init_e_norm;
// Reject near-zero desired speeds to avoid pedestrians that barely move,
// and reject negative speeds because they would reverse the intended
// walking direction.
double desired_v0;
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1);
double v_max = 1.3 * desired_v0;
P[i] = Pedestrian(WHITE, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
}
// Group of pedestrians walking from right to left.
for (int i = n_p_half; i < n_p; i++) {
double x = x_gen_right(gen);
double y = y_gen(gen);
Vector2d start_pos = Vector2d(x, y);
Vector2d destination = Vector2d(-30.0, y);
Vector2d init_e = destination - start_pos;
double init_e_norm = init_e.norm();
Vector2d init_e_unit = init_e / init_e_norm;
// Reject near-zero desired speeds to avoid pedestrians that barely move,
// and reject negative speeds because they would reverse the intended
// walking direction.
double desired_v0;
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1);
double v_max = 1.3 * desired_v0;
P[i] = Pedestrian(BLACK, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
}
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
// Avoid compiler unused parameter warning
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
// Each sample stores: time step, pedestrian id, x, y, e.x, e.y, color.
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);
// Store the initial state before the first simulation step.
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
// n_iter * [ { n_p * (n_p - 1) * (12 ADDs, 23 MULTs, 1 DIV, 4 SQRTs, 1 EXP + 2 ADDs) + n_p * (4 ADD + 4 MULT + board.n_obstacles * (10 ADDs, 18 MULTs, 1 DIV, 2 SQRTs, 1 EXP + 2 ADD) ) } + {(7 ADDs, 16 MULTs, 2 DIVs, 3 SQRTs) * n_p} ]
// n_iter * [ { n_p * (n_p - 1) * (12 A + 23 M + 1 D + 4 S + 1 E + 2 A) + n_p * (4 A + 4 M + n_obstacles * (10 A + 18 M + 1 D + 2 S + 1 E + 2 A) ) } + {n_p * (7 A + 16 M + 2 D + 3 S)} ]
// n_iter * [ { n_p ** 2 * (12 A + 23 M + 1 D + 4 S + 1 E + 2 A) - n_p * (12 A + 23 M + 1 D + 4 S + 1 E + 2 A) + n_p * (4 A + 4 M) + n_obstacles * n_p * (10 A + 18 M + 1 D + 2 S + 1 E + 2 A) } + {n_p * (7 A + 16 M + 2 D + 3 S)} ]
// n_iter * [ n_p ** 2 * (12 A + 23 M + 1 D + 4 S + 1 E + 2 A) + n_p * (- 8 A - 19 M - 1 D - 4 S - 1 E - 2 A) + n_obstacles * n_p * (10 A + 18 M + 1 D + 2 S + 1 E + 2 A) + n_p * (7 A + 16 M + 2 D + 3 S) ]
// n_iter * [ n_p ** 2 * (14 A + 23 M + 1 D + 4 S + 1 E) + n_p * (- 3 A - 3 M + 1 D - 1 S - 1 E) + n_obstacles * n_p * (12 A + 18 M + 1 D + 2 S + 1 E) ]
// NOTE: this flop count charges every ped-ped pair the full cost. The cutoff
// (L80-81) early-rejects pairs farther than 5m, so the real ped-ped work is only
// a fraction of this (roughly 40-50% on a 10x50 hallway, depending on the layout).
// We keep it at 100% here as the upper-bound / fixed-W baseline, the exact fraction
// can be folded into the n_p**2 term later if we want the actual executed flops.
for (int t = 0; t < n_iter; t++) {
// Pass 1: Compute all forces from the current state.
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// # 2 ADDs, 4 MULTs
//
// $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$
//
Vector2d f_alpha_dt = acceleration_term_dt(p);
// # (N-1) * (12 ADDs, 23 MULTs, 1 DIV, 4 SQRTs, 1 EXP)
// + (N-1) * 2 ADDs
//
// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha},\vec{r}_{\alpha} - \vec{r}_{\beta})$
//
for (int j = 0; j < n_p; j++) {
if (i == j)
continue;
// Cutoff check at the call site (so we dont have an additional call)
Vector2d r_ab_pre = p.curr_pos - pedestrians[j].curr_pos;
if (r_ab_pre.x * r_ab_pre.x + r_ab_pre.y * r_ab_pre.y > CUTOFF_SQ)
continue;
f_alpha_dt += pedestrian_repulsion_dt(p, pedestrians[j]);
}
// # board.n_obstacles * (10 ADDs, 18 MULTs, 1 DIV, 2 SQRTs, 1 EXP)
// + board.n_obstacles * 2 ADDs
//
// $\sum_{B} \vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$
//
for (int k = 0; k < board.n_obstacles; k++) {
f_alpha_dt += obstacle_repulsion_dt(p, board.obstacles[k]);
}
// # 2 ADDs
//
//
// $\frac{d\vec{w}_\alpha}{dt} = \vec{F}_{\alpha}(t)$
//
p.w += f_alpha_dt;
}
// Pass 2: Update pedestrian states after all forces have been computed.
// # (7 ADDs, 16 MULTs, 2 DIVs, 3 SQRTs) * n_p
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// # 1 ADD, 2 MULTs, 1 SQRT
double w_norm = p.w.norm();
double w_norm2 = w_norm > EPS ? w_norm : EPS; // Avoid division by zero.
// # 1 DIV
double correction_factor = std::min(1.0, p.v_max / w_norm2);
// # 2 MULTs
p.curr_vel = p.w * correction_factor;
// # 2 ADDs, 2 MULTs
//
// $\vec{r}_i^{t+1} = \vec{r}_i^t + \vec{v}_i^t dt$
//
p.curr_pos = p.curr_pos + p.curr_vel * dt;
// When a pedestrian reaches the end of the corridor, teleport them back
// to the opposite entrance while keeping the same y-coordinate. This
// maintains a constant pedestrian flow and makes lane formation easier to
// observe.
if (p.color == WHITE && p.curr_pos.x > (BOARD_LENGTH - 1.5)) {
p.curr_pos.x = 1.0;
p.destination.y = p.curr_pos.y;
p.w = Vector2d(p.desired_v0, 0.0);
p.curr_vel = p.w;
}
if (p.color == BLACK && p.curr_pos.x < 1.5) {
p.curr_pos.x = BOARD_LENGTH - 1.0;
p.destination.y = p.curr_pos.y;
p.w = Vector2d(-p.desired_v0, 0.0);
p.curr_vel = p.w;
}
// Keep pedestrians inside the board boundaries.
p.curr_pos.y = clamp(p.curr_pos.y, WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
// # 2 ADDs
Vector2d e = p.destination - p.curr_pos;
// # 1 ADD, 2 MULTs, 1 DIV, 1 SQRT
double e_norm = e.norm();
double e_norm2 = e_norm > EPS ? e_norm : EPS; // Avoid division by zero.
double e_norm_inv = 1.0 / e_norm2;
// # 2 MULTs
p.e = e * e_norm_inv;
// # 1 ADD, 2 MULTs
double velocity_norm_sq = p.curr_vel.dot(p.curr_vel);
// # 1 SQRT
double velocity_norm = std::sqrt(velocity_norm_sq);
// # 1 MULT
p.s_b_sq = velocity_norm_sq * delta_t_sq;
// # 3 MULTs
p.s_b_e = p.e * (velocity_norm * delta_t);
#if !BENCHMARK
// Store the current pedestrian state for visualization.
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
// Export recorded simulation states to CSV.
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;
}
-434
View File
@@ -1,434 +0,0 @@
// This file is copied from the baseline and modified
// Our goal is to only calculate pedestrian-pedestrian forces for pedestrian which are near
// Our tolerance is 1cm and our cutoff is a 5m radius.
// Since we have some tolerance and we put pedestrians back at the end of the hallway once they are on one end. We only test/verify on a smaller subsample.
// Otherwise we had huge gaps (because of those jumps back)
// # Changes: FLOPCOUNT
// # Predecessor: variant_cutoff.cpp
// n_iter * [ n_p ** 2 * (14 A + 23 M + 1 D + 4 S + 1 E) + n_p * (- 3 A - 3 M + 1 D - 1 S - 1 E) + n_obstacles * n_p * (12 A + 18 M + 1 D + 2 S + 1 E) ]
#include "../headers/board.hpp"
#include "../headers/constants.hpp"
#include "../headers/pedestrian.hpp"
#include "../headers/vector2d.hpp"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <math.h> // keep — forces code uses unqualified sqrt/exp
#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;
// forces.cpp
// Flop count: 2 ADDs, 5 MULTs, 1 SQRT
double w(Vector2d e, Vector2d f) {
if (e.dot(f) >= f.norm() * cos_phi)
return dt_neg;
else
return c_neg_times_dt;
}
// Flop count: 2 ADDs, 4 MULTs
Vector2d acceleration_term_dt(const Pedestrian &alpha) {
// # 2 MULTs
Vector2d desired_velocity = alpha.e * alpha.desired_v0;
Vector2d actual_velocity = alpha.curr_vel;
// # 2 ADDs, 2 MULTs
Vector2d f_alpha_0_dt = (desired_velocity - actual_velocity) * dt_times_tau_alpha_inv;
return f_alpha_0_dt;
}
// Flop count: 12 ADDs, 23 MULTs, 1 DIV, 4 SQRTs, 1 EXP
Vector2d pedestrian_repulsion_dt(const Pedestrian &alpha, const Pedestrian &beta) {
// # 2 ADDs
// $\vec{r}_{\alpha\beta}$
Vector2d r_ab = alpha.curr_pos - beta.curr_pos;
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert$
// double r_ab_norm = r_ab.norm();
// Now we wanna check if it is a close pedestrian or wheter it is far away and can be ignored
double r_ab_sq = r_ab.x * r_ab.x + r_ab.y * r_ab.y; // squared distance
if (r_ab_sq > CUTOFF_SQ)
return Vector2d(0.0, 0.0); // cheap reject
double r_ab_norm = sqrt(r_ab_sq); // reuse r_ab_sq, no recompute
r_ab_norm = r_ab_norm > EPS ? r_ab_norm : EPS; // Avoid division by zero
// $v_\beta \Delta t \vec{e}_\beta$
Vector2d s_b_e = beta.s_b_e;
// # 2 ADDs
// $\vec{r}_{\alpha\beta} - \vec{v}_{\beta}\varDelta_{t}\vec{e}_{\beta}$
Vector2d r_ab2 = r_ab - s_b_e;
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
double r_ab2_norm = r_ab2.norm();
r_ab2_norm = r_ab2_norm > EPS ? r_ab2_norm : EPS; // Avoid division by zero
// # 1 ADD
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
// # 1 ADD, 1 MULTs, 1 SQRT
//
// $2b = \sqrt{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)^2 - (v_\beta \Delta t)^2}$
//
double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - beta.s_b_sq;
two_b_sq = two_b_sq > EPS_SQ ? two_b_sq : EPS_SQ; // Avoid sqrt of negative and avoid division by zero
double two_b = sqrt(two_b_sq);
// # 5 MULTs, 1 DIV, 1 EXP
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}$
//
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));
// # 1 MULT
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}$
//
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// # 1 MULT
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}$
//
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
// # 2 ADDs, 4 MULTs
//
// $\vec{-f}_{\alpha \beta}(\vec{r}_{\alpha \beta}) = \frac{-V^0_{\alpha \beta}}{2\sigma}e^{-b / \sigma} 2\grad_{\vec{r}_{\alpha \beta}}b $
//
//
// $= \frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}\,\vec{r}_{\alpha \beta} + \frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} -
// v_\beta \Delta t \vec{e}_\beta \Arrowvert}\,(\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta)$
//
//
//
// $2\grad_{\vec{r}_{\alpha \beta}}b = \frac{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)(\frac{\vec{r}_{\alpha \beta}}{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert} + \frac{\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta}{\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert})}{2b}$
//
//
// $= \frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}\,\vec{r}_{\alpha \beta} + \frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}\,(\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta)$
//
//
Vector2d f_alpha_beta_neg = r_ab * scalar_mult_r_ab + r_ab2 * scalar_mult_r_ab2;
// # 2 ADDs, 7 MULTs, 1 SQRT
//
// $\vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha \beta}).dt = w(\vec{e}_{\alpha}, -\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta})).\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta}).dt$
//
Vector2d f_alpha_beta_dt = f_alpha_beta_neg * w(alpha.e, f_alpha_beta_neg);
return f_alpha_beta_dt;
}
// Flop count: 10 ADDs, 18 MULTs, 1 DIVs, 2 SQRTs, 1 EXP
Vector2d obstacle_repulsion_dt(const Pedestrian &alpha, const Obstacle &B) {
// # 7 ADDs, 5 MULTs
// $\vec{r}_{\alpha B}$
Vector2d r_aB = alpha.curr_pos - B.find_nearest_obstacle_point(alpha.curr_pos);
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha B} \Arrowvert$
double r_aB_norm = r_aB.norm();
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS; // Avoid division by zero
// # 2 MULTs, 1 DIV, 1 EXP
//
// $\frac{-U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R}$
//
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// # 2 MULTs
//
// $-\vec{F}_{\alpha B}(\vec{r}_{\alpha B}) = \frac{-U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R} \frac{\vec{r}_{\alpha B}}{\Arrowvert \vec{r}_{\alpha B} \Arrowvert}$
//
Vector2d f_alpha_B_neg = r_aB * scalar_mult;
// # 2 ADDs, 7 MULTs, 1 SQRT
//
// $\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha B}).dt = w(\vec{e}_{\alpha}, -\vec{F}_{\alpha B}(\vec{r}_{\alpha B})).\vec{F}_{\alpha B}(\vec{r}_{\alpha B}).dt$
//
Vector2d f_alpha_B_dt = f_alpha_B_neg * w(alpha.e, f_alpha_B_neg);
return f_alpha_B_dt;
}
// social_force_baseline.cpp
Pedestrian *init_pedestrians(int n_p) {
#if RANDOM_SEED
random_device rd; // Platform-provided random seed source.
mt19937 gen(rd()); // Non-reproducible initialization.
#else
mt19937 gen(SEED); // Reproducible initialization.
#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;
// Group of pedestrians walking from left to right.
for (int i = 0; i < n_p_half; i++) {
double x = x_gen_left(gen);
double y = y_gen(gen);
Vector2d start_pos = Vector2d(x, y);
Vector2d destination = Vector2d(BOARD_LENGTH + 30.0, y);
Vector2d init_e = destination - start_pos;
double init_e_norm = init_e.norm();
Vector2d init_e_unit = init_e / init_e_norm;
// Reject near-zero desired speeds to avoid pedestrians that barely move,
// and reject negative speeds because they would reverse the intended
// walking direction.
double desired_v0;
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1);
double v_max = 1.3 * desired_v0;
P[i] = Pedestrian(WHITE, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
}
// Group of pedestrians walking from right to left.
for (int i = n_p_half; i < n_p; i++) {
double x = x_gen_right(gen);
double y = y_gen(gen);
Vector2d start_pos = Vector2d(x, y);
Vector2d destination = Vector2d(-30.0, y);
Vector2d init_e = destination - start_pos;
double init_e_norm = init_e.norm();
Vector2d init_e_unit = init_e / init_e_norm;
// Reject near-zero desired speeds to avoid pedestrians that barely move,
// and reject negative speeds because they would reverse the intended
// walking direction.
double desired_v0;
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1);
double v_max = 1.3 * desired_v0;
P[i] = Pedestrian(BLACK, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
}
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
// Avoid compiler unused parameter warning
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
// Each sample stores: time step, pedestrian id, x, y, e.x, e.y, color.
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);
// Store the initial state before the first simulation step.
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
// n_iter * [{ n_p * { [(n_p - 1) * (12 ADDs, 23 MULTs, 1 DIV, 4 SQRTs, 1 EXP + 2 ADDs)] + [n_obstacles * (10 ADDs, 18 MULTs, 1 DIV, 2 SQRTs, 1 EXP + 2 ADDs) + 4 ADDs + 4 MULTs] } } + { (7 ADDs, 16 MULTs, 2 DIVs, 3 SQRTs) * n_p }]
// n_iter * [{ n_p * { [(n_p - 1) * (12 A + 23 M + 1 D + 4 S + 1 E + 2 A)] + [n_obstacles * (10 A + 18 M + 1 D + 2 S + 1 E + 2 A) + 4 A + 4 M] } } + { n_p * (7 A + 16 M + 2 D + 3 S) }]
// n_iter * [{ n_p * { n_p * (14 A + 23 M + 1 D + 4 S + 1 E) - 1 * (10 A + 19 M + 1 D + 4 S + 1 E) + n_obstacles * (12 A + 18 M + 1 D + 2 S + 1 E) }} + { n_p * (7 A + 16 M + 2 D + 3 S) }]
// n_iter * [ n_p ** 2 * (14 A + 23 M + 1 D + 4 S + 1 E) - n_p * (10 A + 19 M + 1 D + 4 S + 1 E) + n_obstacles * n_p * (12 A + 18 M + 1 D + 2 S + 1 E) + n_p * (7 A + 16 M + 2 D + 3 S) ]
// n_iter * [ n_p ** 2 * (14 A + 23 M + 1 D + 4 S + 1 E) + n_p * (- 3 A - 3 M + 1 D - 1 S - 1 E) + n_obstacles * n_p * (12 A + 18 M + 1 D + 2 S + 1 E) ]
// NOTE: this flop count charges every ped-ped pair the full cost. The cutoff
// (L80-81) early-rejects pairs farther than 5m, so the real ped-ped work is only
// a fraction of this (roughly 40-50% on a 10x50 hallway, depending on the layout).
// We keep it at 100% here as the upper-bound / fixed-W baseline, the exact fraction
// can be folded into the n_p**2 term later if we want the actual executed flops.
for (int t = 0; t < n_iter; t++) {
// Pass 1: Compute all forces from the current state.
// n_p * { [(n_p - 1) * (12 ADDs, 23 MULTs, 1 DIV, 4 SQRTs, 1 EXP + 2 ADDs)] + [n_obstacles * (10 ADDs, 18 MULTs, 1 DIV, 2 SQRTs, 1 EXP + 2 ADDs) + 4 ADDs + 4 MULTs] }
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// # 2 ADDs, 4 MULTs
//
// $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$
//
Vector2d f_alpha_dt = acceleration_term_dt(p);
// # (N-1) * (12 ADDs, 23 MULTs, 1 DIV, 4 SQRTs, 1 EXP)
// + (N-1) * 2 ADDs
//
// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha},\vec{r}_{\alpha} - \vec{r}_{\beta})$
//
for (int j = 0; j < n_p; j++) {
if (i == j)
continue;
f_alpha_dt += pedestrian_repulsion_dt(p, pedestrians[j]);
}
// # board.n_obstacles * (10 ADDs, 18 MULTs, 1 DIV, 2 SQRTs, 1 EXP)
// + board.n_obstacles * 2 ADDs
//
// $\sum_{B} \vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$
//
for (int k = 0; k < board.n_obstacles; k++) {
f_alpha_dt += obstacle_repulsion_dt(p, board.obstacles[k]);
}
// # 2 ADDs
//
//
// $\frac{d\vec{w}_\alpha}{dt} = \vec{F}_{\alpha}(t)$
//
p.w += f_alpha_dt;
}
// Pass 2: Update pedestrian states after all forces have been computed.
// # (7 ADDs, 16 MULTs, 2 DIVs, 3 SQRTs) * n_p
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// # 1 ADD, 2 MULTs, 1 SQRT
double w_norm = p.w.norm();
double w_norm2 = w_norm > EPS ? w_norm : EPS; // Avoid division by zero.
// # 1 DIV
double correction_factor = std::min(1.0, p.v_max / w_norm2);
// # 2 MULTs
p.curr_vel = p.w * correction_factor;
// # 2 ADDs, 2 MULTs
//
// $\vec{r}_i^{t+1} = \vec{r}_i^t + \vec{v}_i^t dt$
//
p.curr_pos = p.curr_pos + p.curr_vel * dt;
// When a pedestrian reaches the end of the corridor, teleport them back
// to the opposite entrance while keeping the same y-coordinate. This
// maintains a constant pedestrian flow and makes lane formation easier to
// observe.
if (p.color == WHITE && p.curr_pos.x > (BOARD_LENGTH - 1.5)) {
p.curr_pos.x = 1.0;
p.destination.y = p.curr_pos.y;
p.w = Vector2d(p.desired_v0, 0.0);
p.curr_vel = p.w;
}
if (p.color == BLACK && p.curr_pos.x < 1.5) {
p.curr_pos.x = BOARD_LENGTH - 1.0;
p.destination.y = p.curr_pos.y;
p.w = Vector2d(-p.desired_v0, 0.0);
p.curr_vel = p.w;
}
// Keep pedestrians inside the board boundaries.
p.curr_pos.y = clamp(p.curr_pos.y, WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
// # 2 ADDs
Vector2d e = p.destination - p.curr_pos;
// # 1 ADD, 2 MULTs, 1 DIV, 1 SQRT
double e_norm = e.norm();
double e_norm2 = e_norm > EPS ? e_norm : EPS; // Avoid division by zero.
double e_norm_inv = 1.0 / e_norm2;
// # 2 MULTs
p.e = e * e_norm_inv;
// # 1 ADD, 2 MULTs
double velocity_norm_sq = p.curr_vel.dot(p.curr_vel);
// # 1 SQRT
double velocity_norm = std::sqrt(velocity_norm_sq);
// # 1 MULT
p.s_b_sq = velocity_norm_sq * delta_t_sq;
// # 3 MULTs
p.s_b_e = p.e * (velocity_norm * delta_t);
#if !BENCHMARK
// Store the current pedestrian state for visualization.
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
// Export recorded simulation states to CSV.
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;
}
@@ -1,449 +0,0 @@
// # Changes: AVX2 vectorization of the ped-ped inner loop (8 floats per __m256, exp still scalar)
// # Predecessor: variant_float_inlining_SoA.cpp
#include "../headers/board_reduced_float.hpp"
#include "../headers/constants_float.hpp"
#include "../headers/pedestrian_reduced_float.hpp"
#include <immintrin.h> // avx2
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <math.h>
#include <random>
#include <tuple>
#include <vector>
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
#ifndef RANDOM_SEED
#define RANDOM_SEED 0
#endif
#ifndef BENCHMARK
#define BENCHMARK 0
#endif
using namespace std;
// same SoA arrays as the predecessor, but we pad to multiple of 8 so the avx loop is clean
vector<float> pos_x, pos_y;
vector<float> sb_sq;
vector<float> sb_e_x, sb_e_y;
vector<float> e_x, e_y;
vector<float> curr_vel_x, curr_vel_y;
vector<float> w_x, w_y;
vector<float> desired_v0;
vector<float> v_max;
vector<float> 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<float> velocity_gen(v0_mean, v0_std_dev);
uniform_real_distribution<float> y_gen(0.3, BOARD_WIDTH - 0.3);
uniform_real_distribution<float> x_gen_left(2.0, BOARD_LENGTH / 2.0);
uniform_real_distribution<float> x_gen_right(BOARD_LENGTH / 2.0, BOARD_LENGTH - 2.0);
const int n_p_pad = (n_p + 7) & ~7; // round up to a multiple of 8 for avx float lanes
pos_x.resize(n_p_pad);
pos_y.resize(n_p_pad);
sb_sq.resize(n_p_pad);
sb_e_x.resize(n_p_pad);
sb_e_y.resize(n_p_pad);
e_x.resize(n_p_pad);
e_y.resize(n_p_pad);
curr_vel_x.resize(n_p_pad);
curr_vel_y.resize(n_p_pad);
w_x.resize(n_p_pad);
w_y.resize(n_p_pad);
desired_v0.resize(n_p_pad);
v_max.resize(n_p_pad);
destination_x.resize(n_p_pad);
destination_y.resize(n_p_pad);
color.resize(n_p_pad);
int n_p_half = n_p / 2;
for (int i = 0; i < n_p_half; i++) {
float x = x_gen_left(gen);
float y = y_gen(gen);
float dest_x = BOARD_LENGTH + 30.0; // renamed to avoid shadowing destination_x[]
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1);
float 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;
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
for (int i = n_p_half; i < n_p; i++) {
float x = x_gen_right(gen);
float y = y_gen(gen);
float dest_x = -30;
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1);
float 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;
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
// padding lanes: park them far away, zero everything else.
// exp(very negative) is roughly 0 and the active mask zeroes their contribution anyway.
for (int j = n_p; j < n_p_pad; j++) {
pos_x[j] = 1e9f;
pos_y[j] = 1e9f;
sb_sq[j] = 0.0f;
sb_e_x[j] = 0.0f;
sb_e_y[j] = 0.0f;
}
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) {
(void)pedestrians;
#if BENCHMARK
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
vector<tuple<int, int, float, float, float, float, 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++) {
samples.emplace_back(0, i, pos_x[i], pos_y[i], e_x[i], e_y[i], color[i]);
}
}
#endif
const int n_p_pad = (n_p + 7) & ~7;
// {0,1,2,3,4,5,6,7} so we can build per-iter j indices as (j + lane_off)
alignas(32) const float lane_offset_arr[8] = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f};
const __m256 lane_off = _mm256_load_ps(lane_offset_arr);
// constants broadcast to all 8 lanes once
const __m256 v_neg_v0_div_2s = _mm256_set1_ps(NEG_V0_AB_div_TWO_SIGMA);
const __m256 v_neg_2s_inv = _mm256_set1_ps(NEG_TWO_SIGMA_INV);
const __m256 v_cos_phi_sq = _mm256_set1_ps(cos_phi_sq);
const __m256 v_dt_neg = _mm256_set1_ps(dt_neg);
const __m256 v_c_neg_dt = _mm256_set1_ps(c_neg_times_dt);
const __m256 v_zero = _mm256_setzero_ps();
const __m256 v_one = _mm256_set1_ps(1.0f);
const __m256 v_eps = _mm256_set1_ps(EPS);
const __m256 v_eps_sq = _mm256_set1_ps(EPS_SQ);
const __m256 v_np = _mm256_set1_ps((float)n_p);
// # flops per iteration (one pass of the outer time loop):
// # ADDs: n_p * 22 + n_p * n_p_pad * 15 + n_p * board.n_obstacles * 10 + n_p * 7
// # MULTs: n_p * 4 + n_p * n_p_pad * 24 + n_p * board.n_obstacles * 13 + n_p * 16
// # DIVs: n_p * n_p_pad * 1 + n_p * board.n_obstacles + n_p * 2
// # SQRTs: n_p * n_p_pad * 3 + n_p * board.n_obstacles + n_p * 3
// # EXPs: n_p * n_p_pad * 1 + n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
float desired_velocity_x = e_x[i] * desired_v0[i]; // # 1 MULT
float desired_velocity_y = e_y[i] * desired_v0[i]; // # 1 MULT
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
// i is fixed for the inner loops, broadcast its fields once
const __m256 pix = _mm256_set1_ps(pos_x[i]);
const __m256 piy = _mm256_set1_ps(pos_y[i]);
const __m256 eix = _mm256_set1_ps(e_x[i]);
const __m256 eiy = _mm256_set1_ps(e_y[i]);
const __m256 v_i = _mm256_set1_ps((float)i);
__m256 acc_x = _mm256_setzero_ps();
__m256 acc_y = _mm256_setzero_ps();
for (int j = 0; j < n_p_pad; j += 8) {
// 8 lanes at once, lane k maps to pedestrian j+k
__m256 j_idx = _mm256_add_ps(_mm256_set1_ps((float)j), lane_off); // # 8 ADDs
// active = (j+k != i) AND (j+k < n_p), zero in lanes we wanna skip
__m256 m_neq_i = _mm256_cmp_ps(j_idx, v_i, _CMP_NEQ_OQ);
__m256 m_in_range = _mm256_cmp_ps(j_idx, v_np, _CMP_LT_OQ);
__m256 active = _mm256_and_ps(m_neq_i, m_in_range);
// j-side loads, SoA is contiguous. loadu because std::vector only guarantees 16B align
__m256 pjx = _mm256_loadu_ps(&pos_x[j]);
__m256 pjy = _mm256_loadu_ps(&pos_y[j]);
__m256 sbe_jx = _mm256_loadu_ps(&sb_e_x[j]);
__m256 sbe_jy = _mm256_loadu_ps(&sb_e_y[j]);
__m256 sb_jsq = _mm256_loadu_ps(&sb_sq[j]);
// r_ab = pos_i - pos_j ; |r_ab| with the EPS clamp the scalar version does
__m256 r_ab_x = _mm256_sub_ps(pix, pjx); // # 8 ADDs
__m256 r_ab_y = _mm256_sub_ps(piy, pjy); // # 8 ADDs
__m256 r_ab_n2 = _mm256_fmadd_ps(r_ab_x, r_ab_x, _mm256_mul_ps(r_ab_y, r_ab_y)); // # 16 MULTs, 8 ADDs
__m256 r_ab_n = _mm256_sqrt_ps(r_ab_n2); // # 8 SQRTs
r_ab_n = _mm256_max_ps(r_ab_n, v_eps);
// r_ab2 = r_ab - sb_e_j ; |r_ab2| also clamped
__m256 r_ab2_x = _mm256_sub_ps(r_ab_x, sbe_jx); // # 8 ADDs
__m256 r_ab2_y = _mm256_sub_ps(r_ab_y, sbe_jy); // # 8 ADDs
__m256 r_ab2_n2 = _mm256_fmadd_ps(r_ab2_x, r_ab2_x, _mm256_mul_ps(r_ab2_y, r_ab2_y)); // # 16 MULTs, 8 ADDs
__m256 r_ab2_n = _mm256_sqrt_ps(r_ab2_n2); // # 8 SQRTs
r_ab2_n = _mm256_max_ps(r_ab2_n, v_eps);
// 2b = sqrt((|r_ab| + |r_ab2|)^2 - sb_sq_j) ; clamp the inside with EPS_SQ
__m256 r_sum = _mm256_add_ps(r_ab_n, r_ab2_n); // # 8 ADDs
__m256 two_b_sq = _mm256_fmsub_ps(r_sum, r_sum, sb_jsq); // # 8 MULTs, 8 ADDs
two_b_sq = _mm256_max_ps(two_b_sq, v_eps_sq);
__m256 two_b = _mm256_sqrt_ps(two_b_sq); // # 8 SQRTs
// exp scalar fallback. manually unrolled, otherwise gcc keeps it as a loop with
// one expf call and serialises all 8 (the double avx2 with 4 iters does get unrolled, the 8 here doesn't).
__m256 e_arg = _mm256_mul_ps(two_b, v_neg_2s_inv); // # 8 MULTs
alignas(32) float tmp_arg[8], tmp_val[8];
_mm256_store_ps(tmp_arg, e_arg);
tmp_val[0] = expf(tmp_arg[0]);
tmp_val[1] = expf(tmp_arg[1]);
tmp_val[2] = expf(tmp_arg[2]);
tmp_val[3] = expf(tmp_arg[3]);
tmp_val[4] = expf(tmp_arg[4]);
tmp_val[5] = expf(tmp_arg[5]);
tmp_val[6] = expf(tmp_arg[6]);
tmp_val[7] = expf(tmp_arg[7]); // # 8 EXPs (1 per lane, scalar)
__m256 e_val = _mm256_load_ps(tmp_val);
// smc = (r_sum * NEG_V0_AB_div_TWO_SIGMA * e_val) / (two_b * r_ab_n * r_ab2_n)
// i==j and padding lanes collapse the denom, so we swap in 1.0 there to keep smc finite.
// the AND with active below zeroes those lanes anyway.
__m256 numer = _mm256_mul_ps(_mm256_mul_ps(r_sum, v_neg_v0_div_2s), e_val); // # 16 MULTs
__m256 denom = _mm256_mul_ps(_mm256_mul_ps(two_b, r_ab_n), r_ab2_n); // # 16 MULTs
__m256 denom_safe = _mm256_blendv_ps(v_one, denom, active); // bitwise, no flop
__m256 smc = _mm256_div_ps(numer, denom_safe); // # 8 DIVs
__m256 sm_r_ab = _mm256_mul_ps(smc, r_ab2_n); // # 8 MULTs
__m256 sm_r_ab2 = _mm256_mul_ps(smc, r_ab_n); // # 8 MULTs
// scalar: f_x = r_ab_x * sm_r_ab + r_ab2_x * sm_r_ab2
__m256 f_x = _mm256_fmadd_ps(r_ab2_x, sm_r_ab2, _mm256_mul_ps(r_ab_x, sm_r_ab)); // # 16 MULTs, 8 ADDs
__m256 f_y = _mm256_fmadd_ps(r_ab2_y, sm_r_ab2, _mm256_mul_ps(r_ab_y, sm_r_ab)); // # 16 MULTs, 8 ADDs
// float w_val: default dt_neg, switch to c_neg_times_dt when
//(e_dot_f < 0 AND e_dot_f_sq > f_norm_sq * cos_phi_sq).
// this is the DeMorgan of the scalar's if (e_dot_f >= 0 || e_dot_f_sq <= rhs)
__m256 f_norm_sq = _mm256_fmadd_ps(f_x, f_x, _mm256_mul_ps(f_y, f_y)); // # 16 MULTs, 8 ADDs
__m256 rhs = _mm256_mul_ps(f_norm_sq, v_cos_phi_sq); // # 8 MULTs
__m256 e_dot_f = _mm256_fmadd_ps(eix, f_x, _mm256_mul_ps(eiy, f_y)); // # 16 MULTs, 8 ADDs
__m256 e_dot_f_sq = _mm256_mul_ps(e_dot_f, e_dot_f); // # 8 MULTs
__m256 c1 = _mm256_cmp_ps(e_dot_f, v_zero, _CMP_LT_OQ);
__m256 c2 = _mm256_cmp_ps(e_dot_f_sq, rhs, _CMP_GT_OQ);
__m256 cond = _mm256_and_ps(c1, c2);
__m256 w_val = _mm256_blendv_ps(v_dt_neg, v_c_neg_dt, cond);
// mask out i==j lane and padding before accumulating.
// AND with all-zeros = 0, AND with all-ones passes through, cheaper than blendv here.
__m256 f_x_safe = _mm256_and_ps(f_x, active); // bitwise, no flop
__m256 f_y_safe = _mm256_and_ps(f_y, active); // bitwise, no flop
acc_x = _mm256_fmadd_ps(f_x_safe, w_val, acc_x); // # 8 MULTs, 8 ADDs
acc_y = _mm256_fmadd_ps(f_y_safe, w_val, acc_y); // # 8 MULTs, 8 ADDs
}
// horizontal sum of acc_x and acc_y at the same time
__m256 hsum = _mm256_hadd_ps(acc_x, acc_y); // # 8 ADDs (x and y interleaved)
__m128 hsum_lo = _mm256_castps256_ps128(hsum);
__m128 hsum_hi = _mm256_extractf128_ps(hsum, 1);
__m128 sum4 = _mm_add_ps(hsum_lo, hsum_hi); // # 4 ADDs
sum4 = _mm_hadd_ps(sum4, sum4); // # 4 ADDs (total hadd: 16 ADDs for x+y)
alignas(16) float sum_out[4];
_mm_store_ps(sum_out, sum4);
f_alpha_dt_x += sum_out[0]; // # 1 ADD
f_alpha_dt_y += sum_out[1]; // # 1 ADD
// obstacle loop kept scalar, board.n_obstacles is 0 in this setup anyway
for (int k = 0; k < board.n_obstacles; k++) {
float v1_x = pos_x[i] - board.obstacles[k].start_x; // # 1 ADD
float v1_y = pos_y[i] - board.obstacles[k].start_y; // # 1 ADD
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv; // # 3 MULTs, 1 ADD
if (projection < 0.0)
projection = 0.0;
else if (projection > 1.0)
projection = 1.0;
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection); // # 1 MULT, 2 ADDs
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection); // # 1 MULT, 2 ADDs
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y); // # 2 MULTs, 1 ADD, 1 SQRT
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm; // # 2 MULTs, 1 EXP, 1 DIV
float f_alpha_B_neg_x = r_aB_x * scalar_mult; // # 1 MULT
float f_alpha_B_neg_y = r_aB_y * scalar_mult; // # 1 MULT
float w_val;
float e_dot_f = (f_alpha_B_neg_y == 0) ? 0 : (f_alpha_B_neg_y < 0 ? -e_y[i] : e_y[i]);
if (e_dot_f >= cos_phi)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val; // # 1 MULT
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val; // # 1 MULT
f_alpha_dt_x += f_alpha_B_dt_x; // # 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y; // # 1 ADD
}
w_x[i] += f_alpha_dt_x; // # 1 ADD
w_y[i] += f_alpha_dt_y; // # 1 ADD
}
// position update loop stays scalar, same as the double AVX2 variant does
for (int i = 0; i < n_p; i++) {
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]); // # 2 MULTs, 1 ADD, 1 SQRT
float w_norm2 = w_norm > EPS ? w_norm : EPS;
float correction_factor = min(1.0f, v_max[i] / w_norm2); // # 1 DIV
curr_vel_x[i] = w_x[i] * correction_factor; // # 1 MULT
curr_vel_y[i] = w_y[i] * correction_factor; // # 1 MULT
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt; // # 1 MULT, 1 ADD
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt; // # 1 MULT, 1 ADD
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);
float ex = destination_x[i] - pos_x[i]; // # 1 ADD
float ey = destination_y[i] - pos_y[i]; // # 1 ADD
float e_norm = sqrt(ex * ex + ey * ey); // # 2 MULTs, 1 ADD, 1 SQRT
float e_norm2 = e_norm > EPS ? e_norm : EPS;
float e_norm_inv = 1.0 / e_norm2; // # 1 DIV
e_x[i] = ex * e_norm_inv; // # 1 MULT
e_y[i] = ey * e_norm_inv; // # 1 MULT
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i]; // # 2 MULTs, 1 ADD
float velocity_norm = sqrt(velocity_norm_sq); // # 1 SQRT
sb_sq[i] = velocity_norm_sq * delta_t_sq; // # 1 MULT
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t); // # 2 MULTs (vel_norm*delta_t shared with next line)
sb_e_y[i] = e_y[i] * (velocity_norm * delta_t); // # 1 MULT
#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;
}
@@ -170,46 +170,55 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// vectorized expf for a __m256 (8 lanes), no libm call.
// same trick as the double polyexp: exp(x) = 2^n * exp(r) where n = round(x / ln2), r = x - n*ln2.
// after range reduce r is in [-ln2/2, ln2/2] ~= [-0.347, 0.347].
// degree-5 taylor is enough for float (worst-case abs error ~1e-5, way under 1cm verify tolerance).
// 2^n is free via the schraudolph bit trick on the ieee 754 exponent slot.
// float layout: sign(1) | exp(8, bias 127) | mantissa(23), so we shift the biased exponent by 23.
// simd expf, degree-12 taylor on the [-ln2/2, ln2/2] reduced range,
// schraudolph 2^n bit trick on the exponent slot.
// # 128 MULTs, 112 ADDs
static inline __m256 exp256_ps(__m256 x) {
// n = round(x * log2(e))
const __m256 log2e = _mm256_set1_ps(1.4426950408889634f);
__m256 nf = _mm256_round_ps(_mm256_mul_ps(x, log2e),
_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
__m256 nf = _mm256_round_ps(_mm256_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC); // # 8 MULTs
// r = x - n * ln(2)
const __m256 ln2 = _mm256_set1_ps(0.6931471805599453f);
__m256 r = _mm256_fnmadd_ps(nf, ln2, x);
__m256 r = _mm256_fnmadd_ps(nf, ln2, x); // # 8 MULTs, 8 ADDs
// poly(r) ~ exp(r), degree-5 horner from highest coeff downwards
const __m256 c5 = _mm256_set1_ps(1.0f / 120.0f);
const __m256 c4 = _mm256_set1_ps(1.0f / 24.0f);
const __m256 c3 = _mm256_set1_ps(1.0f / 6.0f);
const __m256 c2 = _mm256_set1_ps(0.5f);
const __m256 c1 = _mm256_set1_ps(1.0f);
const __m256 c0 = _mm256_set1_ps(1.0f);
const __m256 c13 = _mm256_set1_ps(c13_scalar);
const __m256 c12 = _mm256_set1_ps(c12_scalar);
const __m256 c11 = _mm256_set1_ps(c11_scalar);
const __m256 c10 = _mm256_set1_ps(c10_scalar);
const __m256 c9 = _mm256_set1_ps(c9_scalar);
const __m256 c8 = _mm256_set1_ps(c8_scalar);
const __m256 c7 = _mm256_set1_ps(c7_scalar);
const __m256 c6 = _mm256_set1_ps(c6_scalar);
const __m256 c5 = _mm256_set1_ps(c5_scalar);
const __m256 c4 = _mm256_set1_ps(c4_scalar);
const __m256 c3 = _mm256_set1_ps(c3_scalar);
const __m256 c2 = _mm256_set1_ps(c2_scalar);
const __m256 c1 = _mm256_set1_ps(c1_scalar);
const __m256 c0 = _mm256_set1_ps(c0_scalar);
__m256 p = c5;
__m256 p = c13;
// # 13 * (8 MULTs, 8 ADDs)
// # = 104 MULTs, 104 ADDs
p = _mm256_fmadd_ps(p, r, c12);
p = _mm256_fmadd_ps(p, r, c11);
p = _mm256_fmadd_ps(p, r, c10);
p = _mm256_fmadd_ps(p, r, c9);
p = _mm256_fmadd_ps(p, r, c8);
p = _mm256_fmadd_ps(p, r, c7);
p = _mm256_fmadd_ps(p, r, c6);
p = _mm256_fmadd_ps(p, r, c5);
p = _mm256_fmadd_ps(p, r, c4);
p = _mm256_fmadd_ps(p, r, c3);
p = _mm256_fmadd_ps(p, r, c2);
p = _mm256_fmadd_ps(p, r, c1);
p = _mm256_fmadd_ps(p, r, c0);
// 2^n via bit trick: float bias 127, exponent slot at bit 23.
// padding lanes (sb_e=0, pos=1e9) overflow this, but the active mask zeroes them out
// before accumulation so the garbage doesnt matter.
__m256i ni = _mm256_cvtps_epi32(nf);
__m256i ni = _mm256_cvtps_epi32(nf);
__m256i biased = _mm256_add_epi32(ni, _mm256_set1_epi32(127));
__m256i scaled = _mm256_slli_epi32(biased, 23);
__m256 pow2n = _mm256_castsi256_ps(scaled);
__m256 pow2n = _mm256_castsi256_ps(scaled);
return _mm256_mul_ps(p, pow2n);
return _mm256_mul_ps(p, pow2n); // # 8 MULTs
}
int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, const char *dump_path) {
@@ -249,15 +258,21 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
const __m256 v_np = _mm256_set1_ps((float)n_p);
const __m256 v_exp_min = _mm256_set1_ps(-87.0f); // clamp for polyexp, below this exp(x) ~ 0 anyway
// # flops per iteration (one pass of the outer time loop):
// # ADDs: n_p * 22 + n_p * n_p_pad * 29 + n_p * board.n_obstacles * 10 + n_p * 7
// # MULTs: n_p * 4 + n_p * n_p_pad * 40 + n_p * board.n_obstacles * 13 + n_p * 16
// # DIVs: n_p * n_p_pad * 1 + n_p * board.n_obstacles + n_p * 2
// # SQRTs: n_p * n_p_pad * 3 + n_p * board.n_obstacles + n_p * 3
// # EXPs: n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
float desired_velocity_x = e_x[i] * desired_v0[i];
float desired_velocity_y = e_y[i] * desired_v0[i];
float desired_velocity_x = e_x[i] * desired_v0[i]; // # 1 MULT
float desired_velocity_y = e_y[i] * desired_v0[i]; // # 1 MULT
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
const __m256 pix = _mm256_set1_ps(pos_x[i]);
const __m256 piy = _mm256_set1_ps(pos_y[i]);
@@ -269,7 +284,7 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m256 acc_y = _mm256_setzero_ps();
for (int j = 0; j < n_p_pad; j += 8) {
__m256 j_idx = _mm256_add_ps(_mm256_set1_ps((float)j), lane_off);
__m256 j_idx = _mm256_add_ps(_mm256_set1_ps((float)j), lane_off); // # 8 ADDs
__m256 m_neq_i = _mm256_cmp_ps(j_idx, v_i, _CMP_NEQ_OQ);
__m256 m_in_range = _mm256_cmp_ps(j_idx, v_np, _CMP_LT_OQ);
@@ -281,90 +296,90 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m256 sbe_jy = _mm256_loadu_ps(&sb_e_y[j]);
__m256 sb_jsq = _mm256_loadu_ps(&sb_sq[j]);
__m256 r_ab_x = _mm256_sub_ps(pix, pjx);
__m256 r_ab_y = _mm256_sub_ps(piy, pjy);
__m256 r_ab_n2 = _mm256_fmadd_ps(r_ab_x, r_ab_x, _mm256_mul_ps(r_ab_y, r_ab_y));
__m256 r_ab_n = _mm256_sqrt_ps(r_ab_n2);
__m256 r_ab_x = _mm256_sub_ps(pix, pjx); // # 8 ADDs
__m256 r_ab_y = _mm256_sub_ps(piy, pjy); // # 8 ADDs
__m256 r_ab_n2 = _mm256_fmadd_ps(r_ab_x, r_ab_x, _mm256_mul_ps(r_ab_y, r_ab_y)); // # 16 MULTs, 8 ADDs
__m256 r_ab_n = _mm256_sqrt_ps(r_ab_n2); // # 8 SQRTs
r_ab_n = _mm256_max_ps(r_ab_n, v_eps);
__m256 r_ab2_x = _mm256_sub_ps(r_ab_x, sbe_jx);
__m256 r_ab2_y = _mm256_sub_ps(r_ab_y, sbe_jy);
__m256 r_ab2_n2 = _mm256_fmadd_ps(r_ab2_x, r_ab2_x, _mm256_mul_ps(r_ab2_y, r_ab2_y));
__m256 r_ab2_n = _mm256_sqrt_ps(r_ab2_n2);
__m256 r_ab2_x = _mm256_sub_ps(r_ab_x, sbe_jx); // # 8 ADDs
__m256 r_ab2_y = _mm256_sub_ps(r_ab_y, sbe_jy); // # 8 ADDs
__m256 r_ab2_n2 = _mm256_fmadd_ps(r_ab2_x, r_ab2_x, _mm256_mul_ps(r_ab2_y, r_ab2_y)); // # 16 MULTs, 8 ADDs
__m256 r_ab2_n = _mm256_sqrt_ps(r_ab2_n2); // # 8 SQRTs
r_ab2_n = _mm256_max_ps(r_ab2_n, v_eps);
__m256 r_sum = _mm256_add_ps(r_ab_n, r_ab2_n);
__m256 two_b_sq = _mm256_fmsub_ps(r_sum, r_sum, sb_jsq);
__m256 r_sum = _mm256_add_ps(r_ab_n, r_ab2_n); // # 8 ADDs
__m256 two_b_sq = _mm256_fmsub_ps(r_sum, r_sum, sb_jsq); // # 8 MULTs, 8 ADDs
two_b_sq = _mm256_max_ps(two_b_sq, v_eps_sq);
__m256 two_b = _mm256_sqrt_ps(two_b_sq);
__m256 two_b = _mm256_sqrt_ps(two_b_sq); // # 8 SQRTs
// simd polynomial expf, no libm call. replaces the 8-iter scalar loop that gcc refused to unroll.
// clamp e_arg >= -87 first: exp(-87) ~ 1.6e-38 is already basically 0, and the schraudolph 2^n
// bit trick blows up if (nf + 127) goes negative (which happens for padding lanes where e_arg ~ -4.7e9).
__m256 e_arg = _mm256_mul_ps(two_b, v_neg_2s_inv);
__m256 e_arg = _mm256_mul_ps(two_b, v_neg_2s_inv); // # 8 MULTs
e_arg = _mm256_max_ps(e_arg, v_exp_min);
__m256 e_val = exp256_ps(e_arg);
__m256 e_val = exp256_ps(e_arg); // # 128 MULTs, 112 ADDs
__m256 numer = _mm256_mul_ps(_mm256_mul_ps(r_sum, v_neg_v0_div_2s), e_val);
__m256 denom = _mm256_mul_ps(_mm256_mul_ps(two_b, r_ab_n), r_ab2_n);
__m256 denom_safe = _mm256_blendv_ps(v_one, denom, active);
__m256 smc = _mm256_div_ps(numer, denom_safe);
__m256 numer = _mm256_mul_ps(_mm256_mul_ps(r_sum, v_neg_v0_div_2s), e_val); // # 16 MULTs
__m256 denom = _mm256_mul_ps(_mm256_mul_ps(two_b, r_ab_n), r_ab2_n); // # 16 MULTs
__m256 denom_safe = _mm256_blendv_ps(v_one, denom, active); // bitwise, no flop
__m256 smc = _mm256_div_ps(numer, denom_safe); // # 8 DIVs
__m256 sm_r_ab = _mm256_mul_ps(smc, r_ab2_n);
__m256 sm_r_ab2 = _mm256_mul_ps(smc, r_ab_n);
__m256 sm_r_ab = _mm256_mul_ps(smc, r_ab2_n); // # 8 MULTs
__m256 sm_r_ab2 = _mm256_mul_ps(smc, r_ab_n); // # 8 MULTs
__m256 f_x = _mm256_fmadd_ps(r_ab2_x, sm_r_ab2, _mm256_mul_ps(r_ab_x, sm_r_ab));
__m256 f_y = _mm256_fmadd_ps(r_ab2_y, sm_r_ab2, _mm256_mul_ps(r_ab_y, sm_r_ab));
__m256 f_x = _mm256_fmadd_ps(r_ab2_x, sm_r_ab2, _mm256_mul_ps(r_ab_x, sm_r_ab)); // # 16 MULTs, 8 ADDs
__m256 f_y = _mm256_fmadd_ps(r_ab2_y, sm_r_ab2, _mm256_mul_ps(r_ab_y, sm_r_ab)); // # 16 MULTs, 8 ADDs
__m256 f_norm_sq = _mm256_fmadd_ps(f_x, f_x, _mm256_mul_ps(f_y, f_y));
__m256 rhs = _mm256_mul_ps(f_norm_sq, v_cos_phi_sq);
__m256 e_dot_f = _mm256_fmadd_ps(eix, f_x, _mm256_mul_ps(eiy, f_y));
__m256 e_dot_f_sq = _mm256_mul_ps(e_dot_f, e_dot_f);
__m256 f_norm_sq = _mm256_fmadd_ps(f_x, f_x, _mm256_mul_ps(f_y, f_y)); // # 16 MULTs, 8 ADDs
__m256 rhs = _mm256_mul_ps(f_norm_sq, v_cos_phi_sq); // # 8 MULTs
__m256 e_dot_f = _mm256_fmadd_ps(eix, f_x, _mm256_mul_ps(eiy, f_y)); // # 16 MULTs, 8 ADDs
__m256 e_dot_f_sq = _mm256_mul_ps(e_dot_f, e_dot_f); // # 8 MULTs
__m256 c1 = _mm256_cmp_ps(e_dot_f, v_zero, _CMP_LT_OQ);
__m256 c2 = _mm256_cmp_ps(e_dot_f_sq, rhs, _CMP_GT_OQ);
__m256 cond = _mm256_and_ps(c1, c2);
__m256 w_val = _mm256_blendv_ps(v_dt_neg, v_c_neg_dt, cond);
__m256 f_x_safe = _mm256_and_ps(f_x, active);
__m256 f_y_safe = _mm256_and_ps(f_y, active);
acc_x = _mm256_fmadd_ps(f_x_safe, w_val, acc_x);
acc_y = _mm256_fmadd_ps(f_y_safe, w_val, acc_y);
__m256 f_x_safe = _mm256_and_ps(f_x, active); // bitwise, no flop
__m256 f_y_safe = _mm256_and_ps(f_y, active); // bitwise, no flop
acc_x = _mm256_fmadd_ps(f_x_safe, w_val, acc_x); // # 8 MULTs, 8 ADDs
acc_y = _mm256_fmadd_ps(f_y_safe, w_val, acc_y); // # 8 MULTs, 8 ADDs
}
__m256 hsum = _mm256_hadd_ps(acc_x, acc_y);
__m256 hsum = _mm256_hadd_ps(acc_x, acc_y); // # 8 ADDs
__m128 hsum_lo = _mm256_castps256_ps128(hsum);
__m128 hsum_hi = _mm256_extractf128_ps(hsum, 1);
__m128 sum4 = _mm_add_ps(hsum_lo, hsum_hi);
sum4 = _mm_hadd_ps(sum4, sum4);
__m128 sum4 = _mm_add_ps(hsum_lo, hsum_hi); // # 4 ADDs
sum4 = _mm_hadd_ps(sum4, sum4); // # 4 ADDs
alignas(16) float sum_out[4];
_mm_store_ps(sum_out, sum4);
f_alpha_dt_x += sum_out[0];
f_alpha_dt_y += sum_out[1];
f_alpha_dt_x += sum_out[0]; // # 1 ADD
f_alpha_dt_y += sum_out[1]; // # 1 ADD
for (int k = 0; k < board.n_obstacles; k++) {
float v1_x = pos_x[i] - board.obstacles[k].start_x;
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v1_x = pos_x[i] - board.obstacles[k].start_x; // # 1 ADD
float v1_y = pos_y[i] - board.obstacles[k].start_y; // # 1 ADD
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv; // # 3 MULTs, 1 ADD
if (projection < 0.0)
projection = 0.0;
else if (projection > 1.0)
projection = 1.0;
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection); // # 1 MULT, 2 ADDs
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection); // # 1 MULT, 2 ADDs
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y); // # 2 MULTs, 1 ADD, 1 SQRT
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm; // # 2 MULTs, 1 EXP, 1 DIV
float f_alpha_B_neg_x = r_aB_x * scalar_mult; // # 1 MULT
float f_alpha_B_neg_y = r_aB_y * scalar_mult; // # 1 MULT
float w_val;
@@ -375,28 +390,28 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
else
w_val = c_neg_times_dt;
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val; // # 1 MULT
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val; // # 1 MULT
f_alpha_dt_x += f_alpha_B_dt_x;
f_alpha_dt_y += f_alpha_B_dt_y;
f_alpha_dt_x += f_alpha_B_dt_x; // # 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y; // # 1 ADD
}
w_x[i] += f_alpha_dt_x;
w_y[i] += f_alpha_dt_y;
w_x[i] += f_alpha_dt_x; // # 1 ADD
w_y[i] += f_alpha_dt_y; // # 1 ADD
}
for (int i = 0; i < n_p; i++) {
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]);
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]); // # 2 MULTs, 1 ADD, 1 SQRT
float w_norm2 = w_norm > EPS ? w_norm : EPS;
float correction_factor = min(1.0f, v_max[i] / w_norm2);
float correction_factor = min(1.0f, v_max[i] / w_norm2); // # 1 DIV
curr_vel_x[i] = w_x[i] * correction_factor;
curr_vel_y[i] = w_y[i] * correction_factor;
curr_vel_x[i] = w_x[i] * correction_factor; // # 1 MULT
curr_vel_y[i] = w_y[i] * correction_factor; // # 1 MULT
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt;
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt;
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt; // # 1 MULT, 1 ADD
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt; // # 1 MULT, 1 ADD
if (color[i] == 0 && pos_x[i] > (BOARD_LENGTH - 1.5)) {
pos_x[i] = 1.0;
@@ -417,22 +432,22 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
pos_y[i] = clamp(pos_y[i], WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
float ex = destination_x[i] - pos_x[i];
float ey = destination_y[i] - pos_y[i];
float ex = destination_x[i] - pos_x[i]; // # 1 ADD
float ey = destination_y[i] - pos_y[i]; // # 1 ADD
float e_norm = sqrt(ex * ex + ey * ey);
float e_norm = sqrt(ex * ex + ey * ey); // # 2 MULTs, 1 ADD, 1 SQRT
float e_norm2 = e_norm > EPS ? e_norm : EPS;
float e_norm_inv = 1.0 / e_norm2;
float e_norm_inv = 1.0 / e_norm2; // # 1 DIV
e_x[i] = ex * e_norm_inv;
e_y[i] = ey * e_norm_inv;
e_x[i] = ex * e_norm_inv; // # 1 MULT
e_y[i] = ey * e_norm_inv; // # 1 MULT
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i];
float velocity_norm = sqrt(velocity_norm_sq);
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i]; // # 2 MULTs, 1 ADD
float velocity_norm = sqrt(velocity_norm_sq); // # 1 SQRT
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);
sb_sq[i] = velocity_norm_sq * delta_t_sq; // # 1 MULT
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t); // # 2 MULTs (vel_norm*delta_t shared with next line)
sb_e_y[i] = e_y[i] * (velocity_norm * delta_t); // # 1 MULT
#if !BENCHMARK
if (dump_path && (t % dump_interval == 0)) {
@@ -1,472 +0,0 @@
// # Changes: simd polynomial expf, replaces the 8 scalar expf calls in the j loop
// # Predecessor: variant_float_inlining_SoA_AVX2.cpp
// the for-loop of 8 scalar expf calls was serialised by gcc (it didnt unroll), big perf hit.
// replace with one inlined simd polynomial approx, no libm call at all.
#include "../headers/board_reduced_float.hpp"
#include "../headers/constants_float.hpp"
#include "../headers/pedestrian_reduced_float.hpp"
#include <immintrin.h> // avx2
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <math.h>
#include <random>
#include <tuple>
#include <vector>
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
#ifndef RANDOM_SEED
#define RANDOM_SEED 0
#endif
#ifndef BENCHMARK
#define BENCHMARK 0
#endif
using namespace std;
vector<float> pos_x, pos_y;
vector<float> sb_sq;
vector<float> sb_e_x, sb_e_y;
vector<float> e_x, e_y;
vector<float> curr_vel_x, curr_vel_y;
vector<float> w_x, w_y;
vector<float> desired_v0;
vector<float> v_max;
vector<float> 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<float> velocity_gen(v0_mean, v0_std_dev);
uniform_real_distribution<float> y_gen(0.3, BOARD_WIDTH - 0.3);
uniform_real_distribution<float> x_gen_left(2.0, BOARD_LENGTH / 2.0);
uniform_real_distribution<float> x_gen_right(BOARD_LENGTH / 2.0, BOARD_LENGTH - 2.0);
const int n_p_pad = (n_p + 7) & ~7;
pos_x.resize(n_p_pad);
pos_y.resize(n_p_pad);
sb_sq.resize(n_p_pad);
sb_e_x.resize(n_p_pad);
sb_e_y.resize(n_p_pad);
e_x.resize(n_p_pad);
e_y.resize(n_p_pad);
curr_vel_x.resize(n_p_pad);
curr_vel_y.resize(n_p_pad);
w_x.resize(n_p_pad);
w_y.resize(n_p_pad);
desired_v0.resize(n_p_pad);
v_max.resize(n_p_pad);
destination_x.resize(n_p_pad);
destination_y.resize(n_p_pad);
color.resize(n_p_pad);
int n_p_half = n_p / 2;
for (int i = 0; i < n_p_half; i++) {
float x = x_gen_left(gen);
float y = y_gen(gen);
float dest_x = BOARD_LENGTH + 30.0;
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1);
float 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;
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
for (int i = n_p_half; i < n_p; i++) {
float x = x_gen_right(gen);
float y = y_gen(gen);
float dest_x = -30;
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1);
float 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;
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
for (int j = n_p; j < n_p_pad; j++) {
pos_x[j] = 1e9f;
pos_y[j] = 1e9f;
sb_sq[j] = 0.0f;
sb_e_x[j] = 0.0f;
sb_e_y[j] = 0.0f;
}
return nullptr;
}
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// simd expf, degree-12 taylor on the [-ln2/2, ln2/2] reduced range,
// schraudolph 2^n bit trick on the exponent slot.
// # 128 MULTs, 112 ADDs
static inline __m256 exp256_ps(__m256 x) {
const __m256 log2e = _mm256_set1_ps(1.4426950408889634f);
__m256 nf = _mm256_round_ps(_mm256_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC); // # 8 MULTs
const __m256 ln2 = _mm256_set1_ps(0.6931471805599453f);
__m256 r = _mm256_fnmadd_ps(nf, ln2, x); // # 8 MULTs, 8 ADDs
const __m256 c13 = _mm256_set1_ps(c13_scalar);
const __m256 c12 = _mm256_set1_ps(c12_scalar);
const __m256 c11 = _mm256_set1_ps(c11_scalar);
const __m256 c10 = _mm256_set1_ps(c10_scalar);
const __m256 c9 = _mm256_set1_ps(c9_scalar);
const __m256 c8 = _mm256_set1_ps(c8_scalar);
const __m256 c7 = _mm256_set1_ps(c7_scalar);
const __m256 c6 = _mm256_set1_ps(c6_scalar);
const __m256 c5 = _mm256_set1_ps(c5_scalar);
const __m256 c4 = _mm256_set1_ps(c4_scalar);
const __m256 c3 = _mm256_set1_ps(c3_scalar);
const __m256 c2 = _mm256_set1_ps(c2_scalar);
const __m256 c1 = _mm256_set1_ps(c1_scalar);
const __m256 c0 = _mm256_set1_ps(c0_scalar);
__m256 p = c13;
// # 13 * (8 MULTs, 8 ADDs)
// # = 104 MULTs, 104 ADDs
p = _mm256_fmadd_ps(p, r, c12);
p = _mm256_fmadd_ps(p, r, c11);
p = _mm256_fmadd_ps(p, r, c10);
p = _mm256_fmadd_ps(p, r, c9);
p = _mm256_fmadd_ps(p, r, c8);
p = _mm256_fmadd_ps(p, r, c7);
p = _mm256_fmadd_ps(p, r, c6);
p = _mm256_fmadd_ps(p, r, c5);
p = _mm256_fmadd_ps(p, r, c4);
p = _mm256_fmadd_ps(p, r, c3);
p = _mm256_fmadd_ps(p, r, c2);
p = _mm256_fmadd_ps(p, r, c1);
p = _mm256_fmadd_ps(p, r, c0);
__m256i ni = _mm256_cvtps_epi32(nf);
__m256i biased = _mm256_add_epi32(ni, _mm256_set1_epi32(127));
__m256i scaled = _mm256_slli_epi32(biased, 23);
__m256 pow2n = _mm256_castsi256_ps(scaled);
return _mm256_mul_ps(p, pow2n); // # 8 MULTs
}
int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, const char *dump_path) {
(void)pedestrians;
#if BENCHMARK
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
vector<tuple<int, int, float, float, float, float, 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++) {
samples.emplace_back(0, i, pos_x[i], pos_y[i], e_x[i], e_y[i], color[i]);
}
}
#endif
const int n_p_pad = (n_p + 7) & ~7;
alignas(32) const float lane_offset_arr[8] = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f};
const __m256 lane_off = _mm256_load_ps(lane_offset_arr);
const __m256 v_neg_v0_div_2s = _mm256_set1_ps(NEG_V0_AB_div_TWO_SIGMA);
const __m256 v_neg_2s_inv = _mm256_set1_ps(NEG_TWO_SIGMA_INV);
const __m256 v_cos_phi_sq = _mm256_set1_ps(cos_phi_sq);
const __m256 v_dt_neg = _mm256_set1_ps(dt_neg);
const __m256 v_c_neg_dt = _mm256_set1_ps(c_neg_times_dt);
const __m256 v_zero = _mm256_setzero_ps();
const __m256 v_one = _mm256_set1_ps(1.0f);
const __m256 v_eps = _mm256_set1_ps(EPS);
const __m256 v_eps_sq = _mm256_set1_ps(EPS_SQ);
const __m256 v_np = _mm256_set1_ps((float)n_p);
const __m256 v_exp_min = _mm256_set1_ps(-87.0f); // clamp for polyexp, below this exp(x) ~ 0 anyway
// # flops per iteration (one pass of the outer time loop):
// # ADDs: n_p * 22 + n_p * n_p_pad * 29 + n_p * board.n_obstacles * 10 + n_p * 7
// # MULTs: n_p * 4 + n_p * n_p_pad * 40 + n_p * board.n_obstacles * 13 + n_p * 16
// # DIVs: n_p * n_p_pad * 1 + n_p * board.n_obstacles + n_p * 2
// # SQRTs: n_p * n_p_pad * 3 + n_p * board.n_obstacles + n_p * 3
// # EXPs: n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
float desired_velocity_x = e_x[i] * desired_v0[i]; // # 1 MULT
float desired_velocity_y = e_y[i] * desired_v0[i]; // # 1 MULT
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
const __m256 pix = _mm256_set1_ps(pos_x[i]);
const __m256 piy = _mm256_set1_ps(pos_y[i]);
const __m256 eix = _mm256_set1_ps(e_x[i]);
const __m256 eiy = _mm256_set1_ps(e_y[i]);
const __m256 v_i = _mm256_set1_ps((float)i);
__m256 acc_x = _mm256_setzero_ps();
__m256 acc_y = _mm256_setzero_ps();
for (int j = 0; j < n_p_pad; j += 8) {
__m256 j_idx = _mm256_add_ps(_mm256_set1_ps((float)j), lane_off); // # 8 ADDs
__m256 m_neq_i = _mm256_cmp_ps(j_idx, v_i, _CMP_NEQ_OQ);
__m256 m_in_range = _mm256_cmp_ps(j_idx, v_np, _CMP_LT_OQ);
__m256 active = _mm256_and_ps(m_neq_i, m_in_range);
__m256 pjx = _mm256_loadu_ps(&pos_x[j]);
__m256 pjy = _mm256_loadu_ps(&pos_y[j]);
__m256 sbe_jx = _mm256_loadu_ps(&sb_e_x[j]);
__m256 sbe_jy = _mm256_loadu_ps(&sb_e_y[j]);
__m256 sb_jsq = _mm256_loadu_ps(&sb_sq[j]);
__m256 r_ab_x = _mm256_sub_ps(pix, pjx); // # 8 ADDs
__m256 r_ab_y = _mm256_sub_ps(piy, pjy); // # 8 ADDs
__m256 r_ab_n2 = _mm256_fmadd_ps(r_ab_x, r_ab_x, _mm256_mul_ps(r_ab_y, r_ab_y)); // # 16 MULTs, 8 ADDs
__m256 r_ab_n = _mm256_sqrt_ps(r_ab_n2); // # 8 SQRTs
r_ab_n = _mm256_max_ps(r_ab_n, v_eps);
__m256 r_ab2_x = _mm256_sub_ps(r_ab_x, sbe_jx); // # 8 ADDs
__m256 r_ab2_y = _mm256_sub_ps(r_ab_y, sbe_jy); // # 8 ADDs
__m256 r_ab2_n2 = _mm256_fmadd_ps(r_ab2_x, r_ab2_x, _mm256_mul_ps(r_ab2_y, r_ab2_y)); // # 16 MULTs, 8 ADDs
__m256 r_ab2_n = _mm256_sqrt_ps(r_ab2_n2); // # 8 SQRTs
r_ab2_n = _mm256_max_ps(r_ab2_n, v_eps);
__m256 r_sum = _mm256_add_ps(r_ab_n, r_ab2_n); // # 8 ADDs
__m256 two_b_sq = _mm256_fmsub_ps(r_sum, r_sum, sb_jsq); // # 8 MULTs, 8 ADDs
two_b_sq = _mm256_max_ps(two_b_sq, v_eps_sq);
__m256 two_b = _mm256_sqrt_ps(two_b_sq); // # 8 SQRTs
// simd polynomial expf, no libm call. replaces the 8-iter scalar loop that gcc refused to unroll.
// clamp e_arg >= -87 first: exp(-87) ~ 1.6e-38 is already basically 0, and the schraudolph 2^n
// bit trick blows up if (nf + 127) goes negative (which happens for padding lanes where e_arg ~ -4.7e9).
__m256 e_arg = _mm256_mul_ps(two_b, v_neg_2s_inv); // # 8 MULTs
e_arg = _mm256_max_ps(e_arg, v_exp_min);
__m256 e_val = exp256_ps(e_arg); // # 128 MULTs, 112 ADDs
__m256 numer = _mm256_mul_ps(_mm256_mul_ps(r_sum, v_neg_v0_div_2s), e_val); // # 16 MULTs
__m256 denom = _mm256_mul_ps(_mm256_mul_ps(two_b, r_ab_n), r_ab2_n); // # 16 MULTs
__m256 denom_safe = _mm256_blendv_ps(v_one, denom, active); // bitwise, no flop
__m256 smc = _mm256_div_ps(numer, denom_safe); // # 8 DIVs
__m256 sm_r_ab = _mm256_mul_ps(smc, r_ab2_n); // # 8 MULTs
__m256 sm_r_ab2 = _mm256_mul_ps(smc, r_ab_n); // # 8 MULTs
__m256 f_x = _mm256_fmadd_ps(r_ab2_x, sm_r_ab2, _mm256_mul_ps(r_ab_x, sm_r_ab)); // # 16 MULTs, 8 ADDs
__m256 f_y = _mm256_fmadd_ps(r_ab2_y, sm_r_ab2, _mm256_mul_ps(r_ab_y, sm_r_ab)); // # 16 MULTs, 8 ADDs
__m256 f_norm_sq = _mm256_fmadd_ps(f_x, f_x, _mm256_mul_ps(f_y, f_y)); // # 16 MULTs, 8 ADDs
__m256 rhs = _mm256_mul_ps(f_norm_sq, v_cos_phi_sq); // # 8 MULTs
__m256 e_dot_f = _mm256_fmadd_ps(eix, f_x, _mm256_mul_ps(eiy, f_y)); // # 16 MULTs, 8 ADDs
__m256 e_dot_f_sq = _mm256_mul_ps(e_dot_f, e_dot_f); // # 8 MULTs
__m256 c1 = _mm256_cmp_ps(e_dot_f, v_zero, _CMP_LT_OQ);
__m256 c2 = _mm256_cmp_ps(e_dot_f_sq, rhs, _CMP_GT_OQ);
__m256 cond = _mm256_and_ps(c1, c2);
__m256 w_val = _mm256_blendv_ps(v_dt_neg, v_c_neg_dt, cond);
__m256 f_x_safe = _mm256_and_ps(f_x, active); // bitwise, no flop
__m256 f_y_safe = _mm256_and_ps(f_y, active); // bitwise, no flop
acc_x = _mm256_fmadd_ps(f_x_safe, w_val, acc_x); // # 8 MULTs, 8 ADDs
acc_y = _mm256_fmadd_ps(f_y_safe, w_val, acc_y); // # 8 MULTs, 8 ADDs
}
__m256 hsum = _mm256_hadd_ps(acc_x, acc_y); // # 8 ADDs
__m128 hsum_lo = _mm256_castps256_ps128(hsum);
__m128 hsum_hi = _mm256_extractf128_ps(hsum, 1);
__m128 sum4 = _mm_add_ps(hsum_lo, hsum_hi); // # 4 ADDs
sum4 = _mm_hadd_ps(sum4, sum4); // # 4 ADDs
alignas(16) float sum_out[4];
_mm_store_ps(sum_out, sum4);
f_alpha_dt_x += sum_out[0]; // # 1 ADD
f_alpha_dt_y += sum_out[1]; // # 1 ADD
for (int k = 0; k < board.n_obstacles; k++) {
float v1_x = pos_x[i] - board.obstacles[k].start_x; // # 1 ADD
float v1_y = pos_y[i] - board.obstacles[k].start_y; // # 1 ADD
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv; // # 3 MULTs, 1 ADD
if (projection < 0.0)
projection = 0.0;
else if (projection > 1.0)
projection = 1.0;
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection); // # 1 MULT, 2 ADDs
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection); // # 1 MULT, 2 ADDs
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y); // # 2 MULTs, 1 ADD, 1 SQRT
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm; // # 2 MULTs, 1 EXP, 1 DIV
float f_alpha_B_neg_x = r_aB_x * scalar_mult; // # 1 MULT
float f_alpha_B_neg_y = r_aB_y * scalar_mult; // # 1 MULT
float w_val;
float e_dot_f = (f_alpha_B_neg_y == 0) ? 0 : (f_alpha_B_neg_y < 0 ? -e_y[i] : e_y[i]);
if (e_dot_f >= cos_phi)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val; // # 1 MULT
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val; // # 1 MULT
f_alpha_dt_x += f_alpha_B_dt_x; // # 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y; // # 1 ADD
}
w_x[i] += f_alpha_dt_x; // # 1 ADD
w_y[i] += f_alpha_dt_y; // # 1 ADD
}
for (int i = 0; i < n_p; i++) {
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]); // # 2 MULTs, 1 ADD, 1 SQRT
float w_norm2 = w_norm > EPS ? w_norm : EPS;
float correction_factor = min(1.0f, v_max[i] / w_norm2); // # 1 DIV
curr_vel_x[i] = w_x[i] * correction_factor; // # 1 MULT
curr_vel_y[i] = w_y[i] * correction_factor; // # 1 MULT
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt; // # 1 MULT, 1 ADD
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt; // # 1 MULT, 1 ADD
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);
float ex = destination_x[i] - pos_x[i]; // # 1 ADD
float ey = destination_y[i] - pos_y[i]; // # 1 ADD
float e_norm = sqrt(ex * ex + ey * ey); // # 2 MULTs, 1 ADD, 1 SQRT
float e_norm2 = e_norm > EPS ? e_norm : EPS;
float e_norm_inv = 1.0 / e_norm2; // # 1 DIV
e_x[i] = ex * e_norm_inv; // # 1 MULT
e_y[i] = ey * e_norm_inv; // # 1 MULT
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i]; // # 2 MULTs, 1 ADD
float velocity_norm = sqrt(velocity_norm_sq); // # 1 SQRT
sb_sq[i] = velocity_norm_sq * delta_t_sq; // # 1 MULT
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t); // # 2 MULTs (vel_norm*delta_t shared with next line)
sb_e_y[i] = e_y[i] * (velocity_norm * delta_t); // # 1 MULT
#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;
}
@@ -187,23 +187,43 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// simd expf, same as the predecessor. degree-5 taylor on the [-ln2/2, ln2/2] reduced range,
// simd expf, degree-12 taylor on the [-ln2/2, ln2/2] reduced range,
// schraudolph 2^n bit trick on the exponent slot.
// # 128 MULTs, 112 ADDs
static inline __m256 exp256_ps(__m256 x) {
const __m256 log2e = _mm256_set1_ps(1.4426950408889634f);
__m256 nf = _mm256_round_ps(_mm256_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
__m256 nf = _mm256_round_ps(_mm256_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC); // # 8 MULTs
const __m256 ln2 = _mm256_set1_ps(0.6931471805599453f);
__m256 r = _mm256_fnmadd_ps(nf, ln2, x);
__m256 r = _mm256_fnmadd_ps(nf, ln2, x); // # 8 MULTs, 8 ADDs
const __m256 c5 = _mm256_set1_ps(1.0f / 120.0f);
const __m256 c4 = _mm256_set1_ps(1.0f / 24.0f);
const __m256 c3 = _mm256_set1_ps(1.0f / 6.0f);
const __m256 c2 = _mm256_set1_ps(0.5f);
const __m256 c1 = _mm256_set1_ps(1.0f);
const __m256 c0 = _mm256_set1_ps(1.0f);
const __m256 c13 = _mm256_set1_ps(c13_scalar);
const __m256 c12 = _mm256_set1_ps(c12_scalar);
const __m256 c11 = _mm256_set1_ps(c11_scalar);
const __m256 c10 = _mm256_set1_ps(c10_scalar);
const __m256 c9 = _mm256_set1_ps(c9_scalar);
const __m256 c8 = _mm256_set1_ps(c8_scalar);
const __m256 c7 = _mm256_set1_ps(c7_scalar);
const __m256 c6 = _mm256_set1_ps(c6_scalar);
const __m256 c5 = _mm256_set1_ps(c5_scalar);
const __m256 c4 = _mm256_set1_ps(c4_scalar);
const __m256 c3 = _mm256_set1_ps(c3_scalar);
const __m256 c2 = _mm256_set1_ps(c2_scalar);
const __m256 c1 = _mm256_set1_ps(c1_scalar);
const __m256 c0 = _mm256_set1_ps(c0_scalar);
__m256 p = c5;
__m256 p = c13;
// # 13 * (8 MULTs, 8 ADDs)
// # = 104 MULTs, 104 ADDs
p = _mm256_fmadd_ps(p, r, c12);
p = _mm256_fmadd_ps(p, r, c11);
p = _mm256_fmadd_ps(p, r, c10);
p = _mm256_fmadd_ps(p, r, c9);
p = _mm256_fmadd_ps(p, r, c8);
p = _mm256_fmadd_ps(p, r, c7);
p = _mm256_fmadd_ps(p, r, c6);
p = _mm256_fmadd_ps(p, r, c5);
p = _mm256_fmadd_ps(p, r, c4);
p = _mm256_fmadd_ps(p, r, c3);
p = _mm256_fmadd_ps(p, r, c2);
@@ -215,7 +235,7 @@ static inline __m256 exp256_ps(__m256 x) {
__m256i scaled = _mm256_slli_epi32(biased, 23);
__m256 pow2n = _mm256_castsi256_ps(scaled);
return _mm256_mul_ps(p, pow2n);
return _mm256_mul_ps(p, pow2n); // # 8 MULTs
}
int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, const char *dump_path) {
@@ -253,15 +273,21 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
const __m256 v_np = _mm256_set1_ps((float)n_p);
const __m256 v_exp_min = _mm256_set1_ps(-87.0f);
// # flops per iteration (one pass of the outer time loop):
// # ADDs: n_p * 22 + n_p * n_p_pad * 29 + n_p * board.n_obstacles * 10 + n_p * 7
// # MULTs: n_p * 4 + n_p * n_p_pad * 42 + n_p * board.n_obstacles * 13 + n_p * 16
// # DIVs: n_p * n_p_pad * 3 + n_p * board.n_obstacles + n_p * 2
// # SQRTs: n_p * n_p_pad * 3 + n_p * board.n_obstacles + n_p * 3
// # EXPs: n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
float desired_velocity_x = e_x[i] * desired_v0[i];
float desired_velocity_y = e_y[i] * desired_v0[i];
float desired_velocity_x = e_x[i] * desired_v0[i]; // # 1 MULT
float desired_velocity_y = e_y[i] * desired_v0[i]; // # 1 MULT
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
const __m256 pix = _mm256_set1_ps(pos_x[i]);
const __m256 piy = _mm256_set1_ps(pos_y[i]);
@@ -273,7 +299,7 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m256 acc_y = _mm256_setzero_ps();
for (int j = 0; j < n_p_pad; j += 8) {
__m256 j_idx = _mm256_add_ps(_mm256_set1_ps((float)j), lane_off);
__m256 j_idx = _mm256_add_ps(_mm256_set1_ps((float)j), lane_off); // # 8 ADDs
__m256 m_neq_i = _mm256_cmp_ps(j_idx, v_i, _CMP_NEQ_OQ);
__m256 m_in_range = _mm256_cmp_ps(j_idx, v_np, _CMP_LT_OQ);
@@ -285,93 +311,93 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m256 sbe_jy = _mm256_loadu_ps(&sb_e_y[j]);
__m256 sb_jsq = _mm256_loadu_ps(&sb_sq[j]);
__m256 r_ab_x = _mm256_sub_ps(pix, pjx);
__m256 r_ab_y = _mm256_sub_ps(piy, pjy);
__m256 r_ab_n2 = _mm256_fmadd_ps(r_ab_x, r_ab_x, _mm256_mul_ps(r_ab_y, r_ab_y));
__m256 r_ab_x = _mm256_sub_ps(pix, pjx); // # 8 ADDs
__m256 r_ab_y = _mm256_sub_ps(piy, pjy); // # 8 ADDs
__m256 r_ab_n2 = _mm256_fmadd_ps(r_ab_x, r_ab_x, _mm256_mul_ps(r_ab_y, r_ab_y)); // # 16 MULTs, 8 ADDs
// clamp before rsqrt: rsqrt(0) = inf, then x*inf = 0*inf = NaN downstream. EPS_SQ
// means the inv_sqrt tops out at 1/EPS and r_ab_n = EPS_SQ/EPS = EPS, matches the predecessor.
r_ab_n2 = _mm256_max_ps(r_ab_n2, v_eps_sq);
__m256 inv_r_ab_n = _mm256_rsqrt_ps(r_ab_n2);
__m256 r_ab_n = _mm256_mul_ps(r_ab_n2, inv_r_ab_n);
__m256 inv_r_ab_n = _mm256_rsqrt_ps(r_ab_n2); // # 8 SQRTs, 8 DIVs
__m256 r_ab_n = _mm256_mul_ps(r_ab_n2, inv_r_ab_n); // # 8 MULTs
__m256 r_ab2_x = _mm256_sub_ps(r_ab_x, sbe_jx);
__m256 r_ab2_y = _mm256_sub_ps(r_ab_y, sbe_jy);
__m256 r_ab2_n2 = _mm256_fmadd_ps(r_ab2_x, r_ab2_x, _mm256_mul_ps(r_ab2_y, r_ab2_y));
__m256 r_ab2_x = _mm256_sub_ps(r_ab_x, sbe_jx); // # 8 ADDs
__m256 r_ab2_y = _mm256_sub_ps(r_ab_y, sbe_jy); // # 8 ADDs
__m256 r_ab2_n2 = _mm256_fmadd_ps(r_ab2_x, r_ab2_x, _mm256_mul_ps(r_ab2_y, r_ab2_y)); // # 16 MULTs, 8 ADDs
r_ab2_n2 = _mm256_max_ps(r_ab2_n2, v_eps_sq);
__m256 inv_r_ab2_n = _mm256_rsqrt_ps(r_ab2_n2);
__m256 r_ab2_n = _mm256_mul_ps(r_ab2_n2, inv_r_ab2_n);
__m256 inv_r_ab2_n = _mm256_rsqrt_ps(r_ab2_n2); // # 8 SQRTs, 8 DIVs
__m256 r_ab2_n = _mm256_mul_ps(r_ab2_n2, inv_r_ab2_n); // # 8 MULTs
__m256 r_sum = _mm256_add_ps(r_ab_n, r_ab2_n);
__m256 two_b_sq = _mm256_fmsub_ps(r_sum, r_sum, sb_jsq);
__m256 r_sum = _mm256_add_ps(r_ab_n, r_ab2_n); // # 8 ADDs
__m256 two_b_sq = _mm256_fmsub_ps(r_sum, r_sum, sb_jsq); // # 8 MULTs, 8 ADDs
two_b_sq = _mm256_max_ps(two_b_sq, v_eps_sq);
__m256 inv_two_b = _mm256_rsqrt_ps(two_b_sq);
__m256 two_b = _mm256_mul_ps(two_b_sq, inv_two_b);
__m256 inv_two_b = _mm256_rsqrt_ps(two_b_sq); // # 8 SQRTs, 8 DIVs
__m256 two_b = _mm256_mul_ps(two_b_sq, inv_two_b); // # 8 MULTs
__m256 e_arg = _mm256_mul_ps(two_b, v_neg_2s_inv);
__m256 e_arg = _mm256_mul_ps(two_b, v_neg_2s_inv); // # 8 MULTs
e_arg = _mm256_max_ps(e_arg, v_exp_min);
__m256 e_val = exp256_ps(e_arg);
__m256 e_val = exp256_ps(e_arg); // # 128 MULTs, 112 ADDs
// smc = numer/denom in the predecessor. with inv-sqrts in hand the divide is free:
// sm_r_ab = smc * r_ab2_n = numer / (two_b * r_ab_n) = numer * inv_two_b * inv_r_ab_n
// sm_r_ab2 = smc * r_ab_n = numer / (two_b * r_ab2_n) = numer * inv_two_b * inv_r_ab2_n
// for the i==j and padding lanes inv_two_b*inv_r_ab_n is huge, but the active-mask AND on
// f_x/f_y below zeros the lane out before accumulation so it cant pollute anything.
__m256 numer_inv_two_b = _mm256_mul_ps(_mm256_mul_ps(r_sum, v_neg_v0_div_2s), _mm256_mul_ps(e_val, inv_two_b));
__m256 sm_r_ab = _mm256_mul_ps(numer_inv_two_b, inv_r_ab_n);
__m256 sm_r_ab2 = _mm256_mul_ps(numer_inv_two_b, inv_r_ab2_n);
__m256 numer_inv_two_b = _mm256_mul_ps(_mm256_mul_ps(r_sum, v_neg_v0_div_2s), _mm256_mul_ps(e_val, inv_two_b)); // # 24 MULTs
__m256 sm_r_ab = _mm256_mul_ps(numer_inv_two_b, inv_r_ab_n); // # 8 MULTs
__m256 sm_r_ab2 = _mm256_mul_ps(numer_inv_two_b, inv_r_ab2_n); // # 8 MULTs
__m256 f_x = _mm256_fmadd_ps(r_ab2_x, sm_r_ab2, _mm256_mul_ps(r_ab_x, sm_r_ab));
__m256 f_y = _mm256_fmadd_ps(r_ab2_y, sm_r_ab2, _mm256_mul_ps(r_ab_y, sm_r_ab));
__m256 f_x = _mm256_fmadd_ps(r_ab2_x, sm_r_ab2, _mm256_mul_ps(r_ab_x, sm_r_ab)); // # 16 MULTs, 8 ADDs
__m256 f_y = _mm256_fmadd_ps(r_ab2_y, sm_r_ab2, _mm256_mul_ps(r_ab_y, sm_r_ab)); // # 16 MULTs, 8 ADDs
__m256 f_norm_sq = _mm256_fmadd_ps(f_x, f_x, _mm256_mul_ps(f_y, f_y));
__m256 rhs = _mm256_mul_ps(f_norm_sq, v_cos_phi_sq);
__m256 e_dot_f = _mm256_fmadd_ps(eix, f_x, _mm256_mul_ps(eiy, f_y));
__m256 e_dot_f_sq = _mm256_mul_ps(e_dot_f, e_dot_f);
__m256 f_norm_sq = _mm256_fmadd_ps(f_x, f_x, _mm256_mul_ps(f_y, f_y)); // # 16 MULTs, 8 ADDs
__m256 rhs = _mm256_mul_ps(f_norm_sq, v_cos_phi_sq); // # 8 MULTs
__m256 e_dot_f = _mm256_fmadd_ps(eix, f_x, _mm256_mul_ps(eiy, f_y)); // # 16 MULTs, 8 ADDs
__m256 e_dot_f_sq = _mm256_mul_ps(e_dot_f, e_dot_f); // # 8 MULTs
__m256 c1 = _mm256_cmp_ps(e_dot_f, v_zero, _CMP_LT_OQ);
__m256 c2 = _mm256_cmp_ps(e_dot_f_sq, rhs, _CMP_GT_OQ);
__m256 cond = _mm256_and_ps(c1, c2);
__m256 w_val = _mm256_blendv_ps(v_dt_neg, v_c_neg_dt, cond);
__m256 f_x_safe = _mm256_and_ps(f_x, active);
__m256 f_y_safe = _mm256_and_ps(f_y, active);
acc_x = _mm256_fmadd_ps(f_x_safe, w_val, acc_x);
acc_y = _mm256_fmadd_ps(f_y_safe, w_val, acc_y);
__m256 f_x_safe = _mm256_and_ps(f_x, active); // bitwise, no flop
__m256 f_y_safe = _mm256_and_ps(f_y, active); // bitwise, no flop
acc_x = _mm256_fmadd_ps(f_x_safe, w_val, acc_x); // # 8 MULTs, 8 ADDs
acc_y = _mm256_fmadd_ps(f_y_safe, w_val, acc_y); // # 8 MULTs, 8 ADDs
}
__m256 hsum = _mm256_hadd_ps(acc_x, acc_y);
__m256 hsum = _mm256_hadd_ps(acc_x, acc_y); // # 8 ADDs (x and y interleaved)
__m128 hsum_lo = _mm256_castps256_ps128(hsum);
__m128 hsum_hi = _mm256_extractf128_ps(hsum, 1);
__m128 sum4 = _mm_add_ps(hsum_lo, hsum_hi);
sum4 = _mm_hadd_ps(sum4, sum4);
__m128 sum4 = _mm_add_ps(hsum_lo, hsum_hi); // # 4 ADDs
sum4 = _mm_hadd_ps(sum4, sum4); // # 4 ADDs (total hadd: 16 ADDs for x+y)
alignas(16) float sum_out[4];
_mm_store_ps(sum_out, sum4);
f_alpha_dt_x += sum_out[0];
f_alpha_dt_y += sum_out[1];
f_alpha_dt_x += sum_out[0]; // # 1 ADD
f_alpha_dt_y += sum_out[1]; // # 1 ADD
for (int k = 0; k < board.n_obstacles; k++) {
float v1_x = pos_x[i] - board.obstacles[k].start_x;
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v1_x = pos_x[i] - board.obstacles[k].start_x; // # 1 ADD
float v1_y = pos_y[i] - board.obstacles[k].start_y; // # 1 ADD
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv; // # 3 MULTs, 1 ADD
if (projection < 0.0)
projection = 0.0;
else if (projection > 1.0)
projection = 1.0;
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection); // # 1 MULT, 2 ADDs
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection); // # 1 MULT, 2 ADDs
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y); // # 2 MULTs, 1 ADD, 1 SQRT
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm; // # 2 MULTs, 1 EXP, 1 DIV
float f_alpha_B_neg_x = r_aB_x * scalar_mult; // # 1 MULT
float f_alpha_B_neg_y = r_aB_y * scalar_mult; // # 1 MULT
float w_val;
@@ -382,28 +408,28 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
else
w_val = c_neg_times_dt;
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val; // # 1 MULT
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val; // # 1 MULT
f_alpha_dt_x += f_alpha_B_dt_x;
f_alpha_dt_y += f_alpha_B_dt_y;
f_alpha_dt_x += f_alpha_B_dt_x; // # 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y; // # 1 ADD
}
w_x[i] += f_alpha_dt_x;
w_y[i] += f_alpha_dt_y;
w_x[i] += f_alpha_dt_x; // # 1 ADD
w_y[i] += f_alpha_dt_y; // # 1 ADD
}
for (int i = 0; i < n_p; i++) {
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]);
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]); // # 2 MULTs, 1 ADD, 1 SQRT
float w_norm2 = w_norm > EPS ? w_norm : EPS;
float correction_factor = min(1.0f, v_max[i] / w_norm2);
float correction_factor = min(1.0f, v_max[i] / w_norm2); // # 1 DIV
curr_vel_x[i] = w_x[i] * correction_factor;
curr_vel_y[i] = w_y[i] * correction_factor;
curr_vel_x[i] = w_x[i] * correction_factor; // # 1 MULT
curr_vel_y[i] = w_y[i] * correction_factor; // # 1 MULT
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt;
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt;
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt; // # 1 MULT, 1 ADD
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt; // # 1 MULT, 1 ADD
if (color[i] == 0 && pos_x[i] > (BOARD_LENGTH - 1.5)) {
pos_x[i] = 1.0;
@@ -424,22 +450,22 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
pos_y[i] = clamp(pos_y[i], WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
float ex = destination_x[i] - pos_x[i];
float ey = destination_y[i] - pos_y[i];
float ex = destination_x[i] - pos_x[i]; // # 1 ADD
float ey = destination_y[i] - pos_y[i]; // # 1 ADD
float e_norm = sqrt(ex * ex + ey * ey);
float e_norm = sqrt(ex * ex + ey * ey); // # 2 MULTs, 1 ADD, 1 SQRT
float e_norm2 = e_norm > EPS ? e_norm : EPS;
float e_norm_inv = 1.0 / e_norm2;
float e_norm_inv = 1.0 / e_norm2; // # 1 DIV
e_x[i] = ex * e_norm_inv;
e_y[i] = ey * e_norm_inv;
e_x[i] = ex * e_norm_inv; // # 1 MULT
e_y[i] = ey * e_norm_inv; // # 1 MULT
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i];
float velocity_norm = sqrt(velocity_norm_sq);
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i]; // # 2 MULTs, 1 ADD
float velocity_norm = sqrt(velocity_norm_sq); // # 1 SQRT
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);
sb_sq[i] = velocity_norm_sq * delta_t_sq; // # 1 MULT
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t); // # 2 MULTs (vel_norm*delta_t shared with next line)
sb_e_y[i] = e_y[i] * (velocity_norm * delta_t); // # 1 MULT
#if !BENCHMARK
if (dump_path && (t % dump_interval == 0)) {
@@ -1,490 +0,0 @@
// # Changes: pure vrsqrtps
// # Predecessor: variant_float_inlining_SoA_AVX2_polyexp.cpp
//
// We first tried rsqrt + 1 Newton step to keep full sqrt precision, but it
// turned out slower than just using vsqrtps. The Newton step needs 2 muls
// and an fnmadd on top of rsqrt, so the whole recovered sqrt ends up at
// ~20 cycles of latency, while vsqrtps is only 12. Since the OoO core was
// already running multiple sqrts in parallel, sqrt throughput wasn't really
// the bottleneck. All the Newton step did was make the dependency chain
// longer, which hurt more than the faster rsqrt helped.
//
// So we drop the Newton step. rsqrt + 1 mul gives us a sqrt in 8 cycles
// with about 12-bit precision, which is well within the 1cm verify tolerance.
// The main structural win is killing the vdivps. rsqrt produces 1/sqrt(x)
// directly, so we keep the inverses around (inv_r_ab_n, inv_r_ab2_n,
// inv_two_b) and rewrite the force scaling. The original had
// "numer / (two_b * r_ab_n * r_ab2_n)" which needs a vdivps; now it's
// "numer * inv_two_b * inv_r_ab_n * inv_r_ab2_n", trading one divide for
// two muls. Divide was the long-pole op on port 0, so this is the bigger
// of the two changes.
#include "../headers/board_reduced_float.hpp"
#include "../headers/constants_float.hpp"
#include "../headers/pedestrian_reduced_float.hpp"
#include <immintrin.h> // avx2
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <math.h>
#include <random>
#include <tuple>
#include <vector>
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
#ifndef RANDOM_SEED
#define RANDOM_SEED 0
#endif
#ifndef BENCHMARK
#define BENCHMARK 0
#endif
using namespace std;
vector<float> pos_x, pos_y;
vector<float> sb_sq;
vector<float> sb_e_x, sb_e_y;
vector<float> e_x, e_y;
vector<float> curr_vel_x, curr_vel_y;
vector<float> w_x, w_y;
vector<float> desired_v0;
vector<float> v_max;
vector<float> 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<float> velocity_gen(v0_mean, v0_std_dev);
uniform_real_distribution<float> y_gen(0.3, BOARD_WIDTH - 0.3);
uniform_real_distribution<float> x_gen_left(2.0, BOARD_LENGTH / 2.0);
uniform_real_distribution<float> x_gen_right(BOARD_LENGTH / 2.0, BOARD_LENGTH - 2.0);
const int n_p_pad = (n_p + 7) & ~7;
pos_x.resize(n_p_pad);
pos_y.resize(n_p_pad);
sb_sq.resize(n_p_pad);
sb_e_x.resize(n_p_pad);
sb_e_y.resize(n_p_pad);
e_x.resize(n_p_pad);
e_y.resize(n_p_pad);
curr_vel_x.resize(n_p_pad);
curr_vel_y.resize(n_p_pad);
w_x.resize(n_p_pad);
w_y.resize(n_p_pad);
desired_v0.resize(n_p_pad);
v_max.resize(n_p_pad);
destination_x.resize(n_p_pad);
destination_y.resize(n_p_pad);
color.resize(n_p_pad);
int n_p_half = n_p / 2;
for (int i = 0; i < n_p_half; i++) {
float x = x_gen_left(gen);
float y = y_gen(gen);
float dest_x = BOARD_LENGTH + 30.0;
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1);
float 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;
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
for (int i = n_p_half; i < n_p; i++) {
float x = x_gen_right(gen);
float y = y_gen(gen);
float dest_x = -30;
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1);
float 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;
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
for (int j = n_p; j < n_p_pad; j++) {
pos_x[j] = 1e9f;
pos_y[j] = 1e9f;
sb_sq[j] = 0.0f;
sb_e_x[j] = 0.0f;
sb_e_y[j] = 0.0f;
}
return nullptr;
}
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// simd expf, degree-12 taylor on the [-ln2/2, ln2/2] reduced range,
// schraudolph 2^n bit trick on the exponent slot.
// # 128 MULTs, 112 ADDs
static inline __m256 exp256_ps(__m256 x) {
const __m256 log2e = _mm256_set1_ps(1.4426950408889634f);
__m256 nf = _mm256_round_ps(_mm256_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC); // # 8 MULTs
const __m256 ln2 = _mm256_set1_ps(0.6931471805599453f);
__m256 r = _mm256_fnmadd_ps(nf, ln2, x); // # 8 MULTs, 8 ADDs
const __m256 c13 = _mm256_set1_ps(c13_scalar);
const __m256 c12 = _mm256_set1_ps(c12_scalar);
const __m256 c11 = _mm256_set1_ps(c11_scalar);
const __m256 c10 = _mm256_set1_ps(c10_scalar);
const __m256 c9 = _mm256_set1_ps(c9_scalar);
const __m256 c8 = _mm256_set1_ps(c8_scalar);
const __m256 c7 = _mm256_set1_ps(c7_scalar);
const __m256 c6 = _mm256_set1_ps(c6_scalar);
const __m256 c5 = _mm256_set1_ps(c5_scalar);
const __m256 c4 = _mm256_set1_ps(c4_scalar);
const __m256 c3 = _mm256_set1_ps(c3_scalar);
const __m256 c2 = _mm256_set1_ps(c2_scalar);
const __m256 c1 = _mm256_set1_ps(c1_scalar);
const __m256 c0 = _mm256_set1_ps(c0_scalar);
__m256 p = c13;
// # 13 * (8 MULTs, 8 ADDs)
// # = 104 MULTs, 104 ADDs
p = _mm256_fmadd_ps(p, r, c12);
p = _mm256_fmadd_ps(p, r, c11);
p = _mm256_fmadd_ps(p, r, c10);
p = _mm256_fmadd_ps(p, r, c9);
p = _mm256_fmadd_ps(p, r, c8);
p = _mm256_fmadd_ps(p, r, c7);
p = _mm256_fmadd_ps(p, r, c6);
p = _mm256_fmadd_ps(p, r, c5);
p = _mm256_fmadd_ps(p, r, c4);
p = _mm256_fmadd_ps(p, r, c3);
p = _mm256_fmadd_ps(p, r, c2);
p = _mm256_fmadd_ps(p, r, c1);
p = _mm256_fmadd_ps(p, r, c0);
__m256i ni = _mm256_cvtps_epi32(nf);
__m256i biased = _mm256_add_epi32(ni, _mm256_set1_epi32(127));
__m256i scaled = _mm256_slli_epi32(biased, 23);
__m256 pow2n = _mm256_castsi256_ps(scaled);
return _mm256_mul_ps(p, pow2n); // # 8 MULTs
}
int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, const char *dump_path) {
(void)pedestrians;
#if BENCHMARK
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
vector<tuple<int, int, float, float, float, float, 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++) {
samples.emplace_back(0, i, pos_x[i], pos_y[i], e_x[i], e_y[i], color[i]);
}
}
#endif
const int n_p_pad = (n_p + 7) & ~7;
alignas(32) const float lane_offset_arr[8] = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f};
const __m256 lane_off = _mm256_load_ps(lane_offset_arr);
const __m256 v_neg_v0_div_2s = _mm256_set1_ps(NEG_V0_AB_div_TWO_SIGMA);
const __m256 v_neg_2s_inv = _mm256_set1_ps(NEG_TWO_SIGMA_INV);
const __m256 v_cos_phi_sq = _mm256_set1_ps(cos_phi_sq);
const __m256 v_dt_neg = _mm256_set1_ps(dt_neg);
const __m256 v_c_neg_dt = _mm256_set1_ps(c_neg_times_dt);
const __m256 v_zero = _mm256_setzero_ps();
const __m256 v_eps_sq = _mm256_set1_ps(EPS_SQ);
const __m256 v_np = _mm256_set1_ps((float)n_p);
const __m256 v_exp_min = _mm256_set1_ps(-87.0f);
// # flops per iteration (one pass of the outer time loop):
// # ADDs: n_p * 22 + n_p * n_p_pad * 29 + n_p * board.n_obstacles * 10 + n_p * 7
// # MULTs: n_p * 4 + n_p * n_p_pad * 42 + n_p * board.n_obstacles * 13 + n_p * 16
// # DIVs: n_p * n_p_pad * 3 + n_p * board.n_obstacles + n_p * 2
// # SQRTs: n_p * n_p_pad * 3 + n_p * board.n_obstacles + n_p * 3
// # EXPs: n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
float desired_velocity_x = e_x[i] * desired_v0[i]; // # 1 MULT
float desired_velocity_y = e_y[i] * desired_v0[i]; // # 1 MULT
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
const __m256 pix = _mm256_set1_ps(pos_x[i]);
const __m256 piy = _mm256_set1_ps(pos_y[i]);
const __m256 eix = _mm256_set1_ps(e_x[i]);
const __m256 eiy = _mm256_set1_ps(e_y[i]);
const __m256 v_i = _mm256_set1_ps((float)i);
__m256 acc_x = _mm256_setzero_ps();
__m256 acc_y = _mm256_setzero_ps();
for (int j = 0; j < n_p_pad; j += 8) {
__m256 j_idx = _mm256_add_ps(_mm256_set1_ps((float)j), lane_off); // # 8 ADDs
__m256 m_neq_i = _mm256_cmp_ps(j_idx, v_i, _CMP_NEQ_OQ);
__m256 m_in_range = _mm256_cmp_ps(j_idx, v_np, _CMP_LT_OQ);
__m256 active = _mm256_and_ps(m_neq_i, m_in_range);
__m256 pjx = _mm256_loadu_ps(&pos_x[j]);
__m256 pjy = _mm256_loadu_ps(&pos_y[j]);
__m256 sbe_jx = _mm256_loadu_ps(&sb_e_x[j]);
__m256 sbe_jy = _mm256_loadu_ps(&sb_e_y[j]);
__m256 sb_jsq = _mm256_loadu_ps(&sb_sq[j]);
__m256 r_ab_x = _mm256_sub_ps(pix, pjx); // # 8 ADDs
__m256 r_ab_y = _mm256_sub_ps(piy, pjy); // # 8 ADDs
__m256 r_ab_n2 = _mm256_fmadd_ps(r_ab_x, r_ab_x, _mm256_mul_ps(r_ab_y, r_ab_y)); // # 16 MULTs, 8 ADDs
// clamp before rsqrt: rsqrt(0) = inf, then x*inf = 0*inf = NaN downstream. EPS_SQ
// means the inv_sqrt tops out at 1/EPS and r_ab_n = EPS_SQ/EPS = EPS, matches the predecessor.
r_ab_n2 = _mm256_max_ps(r_ab_n2, v_eps_sq);
__m256 inv_r_ab_n = _mm256_rsqrt_ps(r_ab_n2); // # 8 SQRTs, 8 DIVs
__m256 r_ab_n = _mm256_mul_ps(r_ab_n2, inv_r_ab_n); // # 8 MULTs
__m256 r_ab2_x = _mm256_sub_ps(r_ab_x, sbe_jx); // # 8 ADDs
__m256 r_ab2_y = _mm256_sub_ps(r_ab_y, sbe_jy); // # 8 ADDs
__m256 r_ab2_n2 = _mm256_fmadd_ps(r_ab2_x, r_ab2_x, _mm256_mul_ps(r_ab2_y, r_ab2_y)); // # 16 MULTs, 8 ADDs
r_ab2_n2 = _mm256_max_ps(r_ab2_n2, v_eps_sq);
__m256 inv_r_ab2_n = _mm256_rsqrt_ps(r_ab2_n2); // # 8 SQRTs, 8 DIVs
__m256 r_ab2_n = _mm256_mul_ps(r_ab2_n2, inv_r_ab2_n); // # 8 MULTs
__m256 r_sum = _mm256_add_ps(r_ab_n, r_ab2_n); // # 8 ADDs
__m256 two_b_sq = _mm256_fmsub_ps(r_sum, r_sum, sb_jsq); // # 8 MULTs, 8 ADDs
two_b_sq = _mm256_max_ps(two_b_sq, v_eps_sq);
__m256 inv_two_b = _mm256_rsqrt_ps(two_b_sq); // # 8 SQRTs, 8 DIVs
__m256 two_b = _mm256_mul_ps(two_b_sq, inv_two_b); // # 8 MULTs
__m256 e_arg = _mm256_mul_ps(two_b, v_neg_2s_inv); // # 8 MULTs
e_arg = _mm256_max_ps(e_arg, v_exp_min);
__m256 e_val = exp256_ps(e_arg); // # 128 MULTs, 112 ADDs
// smc = numer/denom in the predecessor. with inv-sqrts in hand the divide is free:
// sm_r_ab = smc * r_ab2_n = numer / (two_b * r_ab_n) = numer * inv_two_b * inv_r_ab_n
// sm_r_ab2 = smc * r_ab_n = numer / (two_b * r_ab2_n) = numer * inv_two_b * inv_r_ab2_n
// for the i==j and padding lanes inv_two_b*inv_r_ab_n is huge, but the active-mask AND on
// f_x/f_y below zeros the lane out before accumulation so it cant pollute anything.
__m256 numer_inv_two_b = _mm256_mul_ps(_mm256_mul_ps(r_sum, v_neg_v0_div_2s), _mm256_mul_ps(e_val, inv_two_b)); // # 24 MULTs
__m256 sm_r_ab = _mm256_mul_ps(numer_inv_two_b, inv_r_ab_n); // # 8 MULTs
__m256 sm_r_ab2 = _mm256_mul_ps(numer_inv_two_b, inv_r_ab2_n); // # 8 MULTs
__m256 f_x = _mm256_fmadd_ps(r_ab2_x, sm_r_ab2, _mm256_mul_ps(r_ab_x, sm_r_ab)); // # 16 MULTs, 8 ADDs
__m256 f_y = _mm256_fmadd_ps(r_ab2_y, sm_r_ab2, _mm256_mul_ps(r_ab_y, sm_r_ab)); // # 16 MULTs, 8 ADDs
__m256 f_norm_sq = _mm256_fmadd_ps(f_x, f_x, _mm256_mul_ps(f_y, f_y)); // # 16 MULTs, 8 ADDs
__m256 rhs = _mm256_mul_ps(f_norm_sq, v_cos_phi_sq); // # 8 MULTs
__m256 e_dot_f = _mm256_fmadd_ps(eix, f_x, _mm256_mul_ps(eiy, f_y)); // # 16 MULTs, 8 ADDs
__m256 e_dot_f_sq = _mm256_mul_ps(e_dot_f, e_dot_f); // # 8 MULTs
__m256 c1 = _mm256_cmp_ps(e_dot_f, v_zero, _CMP_LT_OQ);
__m256 c2 = _mm256_cmp_ps(e_dot_f_sq, rhs, _CMP_GT_OQ);
__m256 cond = _mm256_and_ps(c1, c2);
__m256 w_val = _mm256_blendv_ps(v_dt_neg, v_c_neg_dt, cond);
__m256 f_x_safe = _mm256_and_ps(f_x, active); // bitwise, no flop
__m256 f_y_safe = _mm256_and_ps(f_y, active); // bitwise, no flop
acc_x = _mm256_fmadd_ps(f_x_safe, w_val, acc_x); // # 8 MULTs, 8 ADDs
acc_y = _mm256_fmadd_ps(f_y_safe, w_val, acc_y); // # 8 MULTs, 8 ADDs
}
__m256 hsum = _mm256_hadd_ps(acc_x, acc_y); // # 8 ADDs (x and y interleaved)
__m128 hsum_lo = _mm256_castps256_ps128(hsum);
__m128 hsum_hi = _mm256_extractf128_ps(hsum, 1);
__m128 sum4 = _mm_add_ps(hsum_lo, hsum_hi); // # 4 ADDs
sum4 = _mm_hadd_ps(sum4, sum4); // # 4 ADDs (total hadd: 16 ADDs for x+y)
alignas(16) float sum_out[4];
_mm_store_ps(sum_out, sum4);
f_alpha_dt_x += sum_out[0]; // # 1 ADD
f_alpha_dt_y += sum_out[1]; // # 1 ADD
for (int k = 0; k < board.n_obstacles; k++) {
float v1_x = pos_x[i] - board.obstacles[k].start_x; // # 1 ADD
float v1_y = pos_y[i] - board.obstacles[k].start_y; // # 1 ADD
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv; // # 3 MULTs, 1 ADD
if (projection < 0.0)
projection = 0.0;
else if (projection > 1.0)
projection = 1.0;
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection); // # 1 MULT, 2 ADDs
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection); // # 1 MULT, 2 ADDs
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y); // # 2 MULTs, 1 ADD, 1 SQRT
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm; // # 2 MULTs, 1 EXP, 1 DIV
float f_alpha_B_neg_x = r_aB_x * scalar_mult; // # 1 MULT
float f_alpha_B_neg_y = r_aB_y * scalar_mult; // # 1 MULT
float w_val;
float e_dot_f = (f_alpha_B_neg_y == 0) ? 0 : (f_alpha_B_neg_y < 0 ? -e_y[i] : e_y[i]);
if (e_dot_f >= cos_phi)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val; // # 1 MULT
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val; // # 1 MULT
f_alpha_dt_x += f_alpha_B_dt_x; // # 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y; // # 1 ADD
}
w_x[i] += f_alpha_dt_x; // # 1 ADD
w_y[i] += f_alpha_dt_y; // # 1 ADD
}
for (int i = 0; i < n_p; i++) {
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]); // # 2 MULTs, 1 ADD, 1 SQRT
float w_norm2 = w_norm > EPS ? w_norm : EPS;
float correction_factor = min(1.0f, v_max[i] / w_norm2); // # 1 DIV
curr_vel_x[i] = w_x[i] * correction_factor; // # 1 MULT
curr_vel_y[i] = w_y[i] * correction_factor; // # 1 MULT
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt; // # 1 MULT, 1 ADD
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt; // # 1 MULT, 1 ADD
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);
float ex = destination_x[i] - pos_x[i]; // # 1 ADD
float ey = destination_y[i] - pos_y[i]; // # 1 ADD
float e_norm = sqrt(ex * ex + ey * ey); // # 2 MULTs, 1 ADD, 1 SQRT
float e_norm2 = e_norm > EPS ? e_norm : EPS;
float e_norm_inv = 1.0 / e_norm2; // # 1 DIV
e_x[i] = ex * e_norm_inv; // # 1 MULT
e_y[i] = ey * e_norm_inv; // # 1 MULT
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i]; // # 2 MULTs, 1 ADD
float velocity_norm = sqrt(velocity_norm_sq); // # 1 SQRT
sb_sq[i] = velocity_norm_sq * delta_t_sq; // # 1 MULT
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t); // # 2 MULTs (vel_norm*delta_t shared with next line)
sb_e_y[i] = e_y[i] * (velocity_norm * delta_t); // # 1 MULT
#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;
}
@@ -187,23 +187,43 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// simd expf, same as the predecessor. degree-5 taylor on the [-ln2/2, ln2/2] reduced range,
// simd expf, degree-12 taylor on the [-ln2/2, ln2/2] reduced range,
// schraudolph 2^n bit trick on the exponent slot.
// # 128 MULTs, 112 ADDs
static inline __m256 exp256_ps(__m256 x) {
const __m256 log2e = _mm256_set1_ps(1.4426950408889634f);
__m256 nf = _mm256_round_ps(_mm256_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
__m256 nf = _mm256_round_ps(_mm256_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC); // # 8 MULTs
const __m256 ln2 = _mm256_set1_ps(0.6931471805599453f);
__m256 r = _mm256_fnmadd_ps(nf, ln2, x);
__m256 r = _mm256_fnmadd_ps(nf, ln2, x); // # 8 MULTs, 8 ADDs
const __m256 c5 = _mm256_set1_ps(1.0f / 120.0f);
const __m256 c4 = _mm256_set1_ps(1.0f / 24.0f);
const __m256 c3 = _mm256_set1_ps(1.0f / 6.0f);
const __m256 c2 = _mm256_set1_ps(0.5f);
const __m256 c1 = _mm256_set1_ps(1.0f);
const __m256 c0 = _mm256_set1_ps(1.0f);
const __m256 c13 = _mm256_set1_ps(c13_scalar);
const __m256 c12 = _mm256_set1_ps(c12_scalar);
const __m256 c11 = _mm256_set1_ps(c11_scalar);
const __m256 c10 = _mm256_set1_ps(c10_scalar);
const __m256 c9 = _mm256_set1_ps(c9_scalar);
const __m256 c8 = _mm256_set1_ps(c8_scalar);
const __m256 c7 = _mm256_set1_ps(c7_scalar);
const __m256 c6 = _mm256_set1_ps(c6_scalar);
const __m256 c5 = _mm256_set1_ps(c5_scalar);
const __m256 c4 = _mm256_set1_ps(c4_scalar);
const __m256 c3 = _mm256_set1_ps(c3_scalar);
const __m256 c2 = _mm256_set1_ps(c2_scalar);
const __m256 c1 = _mm256_set1_ps(c1_scalar);
const __m256 c0 = _mm256_set1_ps(c0_scalar);
__m256 p = c5;
__m256 p = c13;
// # 13 * (8 MULTs, 8 ADDs)
// # = 104 MULTs, 104 ADDs
p = _mm256_fmadd_ps(p, r, c12);
p = _mm256_fmadd_ps(p, r, c11);
p = _mm256_fmadd_ps(p, r, c10);
p = _mm256_fmadd_ps(p, r, c9);
p = _mm256_fmadd_ps(p, r, c8);
p = _mm256_fmadd_ps(p, r, c7);
p = _mm256_fmadd_ps(p, r, c6);
p = _mm256_fmadd_ps(p, r, c5);
p = _mm256_fmadd_ps(p, r, c4);
p = _mm256_fmadd_ps(p, r, c3);
p = _mm256_fmadd_ps(p, r, c2);
@@ -215,7 +235,7 @@ static inline __m256 exp256_ps(__m256 x) {
__m256i scaled = _mm256_slli_epi32(biased, 23);
__m256 pow2n = _mm256_castsi256_ps(scaled);
return _mm256_mul_ps(p, pow2n);
return _mm256_mul_ps(p, pow2n); // # 8 MULTs
}
int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, const char *dump_path) {
@@ -253,15 +273,21 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
// const __m256 v_np = _mm256_set1_ps((float)n_p);
const __m256 v_exp_min = _mm256_set1_ps(-87.0f);
// # flops per iteration (one pass of the outer time loop):
// # ADDs: n_p * 22 + n_p * n_p_pad * 28 + n_p * board.n_obstacles * 10 + n_p * 7
// # MULTs: n_p * 4 + n_p * n_p_pad * 42 + n_p * board.n_obstacles * 13 + n_p * 16
// # DIVs: n_p * n_p_pad * 3 + n_p * board.n_obstacles + n_p * 2
// # SQRTs: n_p * n_p_pad * 3 + n_p * board.n_obstacles + n_p * 3
// # EXPs: n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
float desired_velocity_x = e_x[i] * desired_v0[i];
float desired_velocity_y = e_y[i] * desired_v0[i];
float desired_velocity_x = e_x[i] * desired_v0[i]; // # 1 MULT
float desired_velocity_y = e_y[i] * desired_v0[i]; // # 1 MULT
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
// ghost-self trick: park pedestrian i at 1e9 for its own j-loop, broadcasting i's
// real position from a saved copy. then the j==i lane reads 1e9 like a padding lane
@@ -291,51 +317,51 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m256 sbe_jy = _mm256_loadu_ps(&sb_e_y[j]);
__m256 sb_jsq = _mm256_loadu_ps(&sb_sq[j]);
__m256 r_ab_x = _mm256_sub_ps(pix, pjx);
__m256 r_ab_y = _mm256_sub_ps(piy, pjy);
__m256 r_ab_n2 = _mm256_fmadd_ps(r_ab_x, r_ab_x, _mm256_mul_ps(r_ab_y, r_ab_y));
__m256 r_ab_x = _mm256_sub_ps(pix, pjx); // # 8 ADDs
__m256 r_ab_y = _mm256_sub_ps(piy, pjy); // # 8 ADDs
__m256 r_ab_n2 = _mm256_fmadd_ps(r_ab_x, r_ab_x, _mm256_mul_ps(r_ab_y, r_ab_y)); // # 16 MULTs, 8 ADDs
// clamp before rsqrt: rsqrt(0) = inf, then x*inf = 0*inf = NaN downstream. EPS_SQ
// means the inv_sqrt tops out at 1/EPS and r_ab_n = EPS_SQ/EPS = EPS, matches the predecessor.
r_ab_n2 = _mm256_max_ps(r_ab_n2, v_eps_sq);
__m256 inv_r_ab_n = _mm256_rsqrt_ps(r_ab_n2);
__m256 inv_r_ab_n = _mm256_rsqrt_ps(r_ab_n2); // # 8 SQRTs, 8 DIVs
//__m256 r_ab_n = _mm256_mul_ps(r_ab_n2, inv_r_ab_n);
__m256 r_ab2_x = _mm256_sub_ps(r_ab_x, sbe_jx);
__m256 r_ab2_y = _mm256_sub_ps(r_ab_y, sbe_jy);
__m256 r_ab2_n2 = _mm256_fmadd_ps(r_ab2_x, r_ab2_x, _mm256_mul_ps(r_ab2_y, r_ab2_y));
__m256 r_ab2_x = _mm256_sub_ps(r_ab_x, sbe_jx); // # 8 ADDs
__m256 r_ab2_y = _mm256_sub_ps(r_ab_y, sbe_jy); // # 8 ADDs
__m256 r_ab2_n2 = _mm256_fmadd_ps(r_ab2_x, r_ab2_x, _mm256_mul_ps(r_ab2_y, r_ab2_y)); // # 16 MULTs, 8 ADDs
r_ab2_n2 = _mm256_max_ps(r_ab2_n2, v_eps_sq);
__m256 inv_r_ab2_n = _mm256_rsqrt_ps(r_ab2_n2);
__m256 inv_r_ab2_n = _mm256_rsqrt_ps(r_ab2_n2); // # 8 SQRTs, 8 DIVs
//__m256 r_ab2_n = _mm256_mul_ps(r_ab2_n2, inv_r_ab2_n);
// r_sum = |r_ab| + |r_ab2|, fused. sqrt(x) = x * rsqrt(x)
// the two norms are used only here (later code uses inv_r_ab_n / inv_r_ab2_n), so fold the two
// recovery muls + add into one mul + one fmadd. -1 FP op, -2 live regs.
__m256 r_sum = _mm256_fmadd_ps(r_ab2_n2, inv_r_ab2_n, _mm256_mul_ps(r_ab_n2, inv_r_ab_n));
__m256 two_b_sq = _mm256_fmsub_ps(r_sum, r_sum, sb_jsq);
__m256 r_sum = _mm256_fmadd_ps(r_ab2_n2, inv_r_ab2_n, _mm256_mul_ps(r_ab_n2, inv_r_ab_n)); // # 16 MULTs, 8 ADDs
__m256 two_b_sq = _mm256_fmsub_ps(r_sum, r_sum, sb_jsq); // # 8 MULTs, 8 ADDs
two_b_sq = _mm256_max_ps(two_b_sq, v_eps_sq);
__m256 inv_two_b = _mm256_rsqrt_ps(two_b_sq);
__m256 two_b = _mm256_mul_ps(two_b_sq, inv_two_b);
__m256 inv_two_b = _mm256_rsqrt_ps(two_b_sq); // # 8 SQRTs, 8 DIVs
__m256 two_b = _mm256_mul_ps(two_b_sq, inv_two_b); // # 8 MULTs
__m256 e_arg = _mm256_mul_ps(two_b, v_neg_2s_inv);
__m256 e_arg = _mm256_mul_ps(two_b, v_neg_2s_inv); // # 8 MULTs
e_arg = _mm256_max_ps(e_arg, v_exp_min);
__m256 e_val = exp256_ps(e_arg);
__m256 e_val = exp256_ps(e_arg); // # 128 MULTs, 112 ADDs
// smc = numer/denom in the predecessor. with inv-sqrts in hand the divide is free:
// sm_r_ab = smc * r_ab2_n = numer / (two_b * r_ab_n) = numer * inv_two_b * inv_r_ab_n
// sm_r_ab2 = smc * r_ab_n = numer / (two_b * r_ab2_n) = numer * inv_two_b * inv_r_ab2_n
// for the i==j and padding lanes inv_two_b*inv_r_ab_n is huge, but the active-mask AND on
// f_x/f_y below zeros the lane out before accumulation so it cant pollute anything.
__m256 numer_inv_two_b = _mm256_mul_ps(_mm256_mul_ps(r_sum, v_neg_v0_div_2s), _mm256_mul_ps(e_val, inv_two_b));
__m256 sm_r_ab = _mm256_mul_ps(numer_inv_two_b, inv_r_ab_n);
__m256 sm_r_ab2 = _mm256_mul_ps(numer_inv_two_b, inv_r_ab2_n);
__m256 numer_inv_two_b = _mm256_mul_ps(_mm256_mul_ps(r_sum, v_neg_v0_div_2s), _mm256_mul_ps(e_val, inv_two_b)); // # 24 MULTs
__m256 sm_r_ab = _mm256_mul_ps(numer_inv_two_b, inv_r_ab_n); // # 8 MULTs
__m256 sm_r_ab2 = _mm256_mul_ps(numer_inv_two_b, inv_r_ab2_n); // # 8 MULTs
__m256 f_x = _mm256_fmadd_ps(r_ab2_x, sm_r_ab2, _mm256_mul_ps(r_ab_x, sm_r_ab));
__m256 f_y = _mm256_fmadd_ps(r_ab2_y, sm_r_ab2, _mm256_mul_ps(r_ab_y, sm_r_ab));
__m256 f_x = _mm256_fmadd_ps(r_ab2_x, sm_r_ab2, _mm256_mul_ps(r_ab_x, sm_r_ab)); // # 16 MULTs, 8 ADDs
__m256 f_y = _mm256_fmadd_ps(r_ab2_y, sm_r_ab2, _mm256_mul_ps(r_ab_y, sm_r_ab)); // # 16 MULTs, 8 ADDs
__m256 f_norm_sq = _mm256_fmadd_ps(f_x, f_x, _mm256_mul_ps(f_y, f_y));
__m256 rhs = _mm256_mul_ps(f_norm_sq, v_cos_phi_sq);
__m256 e_dot_f = _mm256_fmadd_ps(eix, f_x, _mm256_mul_ps(eiy, f_y));
__m256 e_dot_f_sq = _mm256_mul_ps(e_dot_f, e_dot_f);
__m256 f_norm_sq = _mm256_fmadd_ps(f_x, f_x, _mm256_mul_ps(f_y, f_y)); // # 16 MULTs, 8 ADDs
__m256 rhs = _mm256_mul_ps(f_norm_sq, v_cos_phi_sq); // # 8 MULTs
__m256 e_dot_f = _mm256_fmadd_ps(eix, f_x, _mm256_mul_ps(eiy, f_y)); // # 16 MULTs, 8 ADDs
__m256 e_dot_f_sq = _mm256_mul_ps(e_dot_f, e_dot_f); // # 8 MULTs
__m256 c1 = _mm256_cmp_ps(e_dot_f, v_zero, _CMP_LT_OQ);
__m256 c2 = _mm256_cmp_ps(e_dot_f_sq, rhs, _CMP_GT_OQ);
@@ -344,47 +370,47 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
//__m256 f_x_safe = _mm256_and_ps(f_x, active);
//__m256 f_y_safe = _mm256_and_ps(f_y, active);
acc_x = _mm256_fmadd_ps(f_x, w_val, acc_x);
acc_y = _mm256_fmadd_ps(f_y, w_val, acc_y);
acc_x = _mm256_fmadd_ps(f_x, w_val, acc_x); // # 8 MULTs, 8 ADDs
acc_y = _mm256_fmadd_ps(f_y, w_val, acc_y); // # 8 MULTs, 8 ADDs
}
// restore the values
pos_x[i] = saved_pix;
pos_y[i] = saved_piy;
__m256 hsum = _mm256_hadd_ps(acc_x, acc_y);
__m256 hsum = _mm256_hadd_ps(acc_x, acc_y); // # 8 ADDs (x and y interleaved)
__m128 hsum_lo = _mm256_castps256_ps128(hsum);
__m128 hsum_hi = _mm256_extractf128_ps(hsum, 1);
__m128 sum4 = _mm_add_ps(hsum_lo, hsum_hi);
sum4 = _mm_hadd_ps(sum4, sum4);
__m128 sum4 = _mm_add_ps(hsum_lo, hsum_hi); // # 4 ADDs
sum4 = _mm_hadd_ps(sum4, sum4); // # 4 ADDs (total hadd: 16 ADDs for x+y)
alignas(16) float sum_out[4];
_mm_store_ps(sum_out, sum4);
f_alpha_dt_x += sum_out[0];
f_alpha_dt_y += sum_out[1];
f_alpha_dt_x += sum_out[0]; // # 1 ADD
f_alpha_dt_y += sum_out[1]; // # 1 ADD
for (int k = 0; k < board.n_obstacles; k++) {
float v1_x = pos_x[i] - board.obstacles[k].start_x;
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v1_x = pos_x[i] - board.obstacles[k].start_x; // # 1 ADD
float v1_y = pos_y[i] - board.obstacles[k].start_y; // # 1 ADD
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv; // # 3 MULTs, 1 ADD
if (projection < 0.0)
projection = 0.0;
else if (projection > 1.0)
projection = 1.0;
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection); // # 1 MULT, 2 ADDs
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection); // # 1 MULT, 2 ADDs
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y); // # 2 MULTs, 1 ADD, 1 SQRT
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm; // # 2 MULTs, 1 EXP, 1 DIV
float f_alpha_B_neg_x = r_aB_x * scalar_mult; // # 1 MULT
float f_alpha_B_neg_y = r_aB_y * scalar_mult; // # 1 MULT
float w_val;
@@ -395,28 +421,28 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
else
w_val = c_neg_times_dt;
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val; // # 1 MULT
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val; // # 1 MULT
f_alpha_dt_x += f_alpha_B_dt_x;
f_alpha_dt_y += f_alpha_B_dt_y;
f_alpha_dt_x += f_alpha_B_dt_x; // # 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y; // # 1 ADD
}
w_x[i] += f_alpha_dt_x;
w_y[i] += f_alpha_dt_y;
w_x[i] += f_alpha_dt_x; // # 1 ADD
w_y[i] += f_alpha_dt_y; // # 1 ADD
}
for (int i = 0; i < n_p; i++) {
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]);
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]); // # 2 MULTs, 1 ADD, 1 SQRT
float w_norm2 = w_norm > EPS ? w_norm : EPS;
float correction_factor = min(1.0f, v_max[i] / w_norm2);
float correction_factor = min(1.0f, v_max[i] / w_norm2); // # 1 DIV
curr_vel_x[i] = w_x[i] * correction_factor;
curr_vel_y[i] = w_y[i] * correction_factor;
curr_vel_x[i] = w_x[i] * correction_factor; // # 1 MULT
curr_vel_y[i] = w_y[i] * correction_factor; // # 1 MULT
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt;
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt;
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt; // # 1 MULT, 1 ADD
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt; // # 1 MULT, 1 ADD
if (color[i] == 0 && pos_x[i] > (BOARD_LENGTH - 1.5)) {
pos_x[i] = 1.0;
@@ -437,22 +463,22 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
pos_y[i] = clamp(pos_y[i], WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
float ex = destination_x[i] - pos_x[i];
float ey = destination_y[i] - pos_y[i];
float ex = destination_x[i] - pos_x[i]; // # 1 ADD
float ey = destination_y[i] - pos_y[i]; // # 1 ADD
float e_norm = sqrt(ex * ex + ey * ey);
float e_norm = sqrt(ex * ex + ey * ey); // # 2 MULTs, 1 ADD, 1 SQRT
float e_norm2 = e_norm > EPS ? e_norm : EPS;
float e_norm_inv = 1.0 / e_norm2;
float e_norm_inv = 1.0 / e_norm2; // # 1 DIV
e_x[i] = ex * e_norm_inv;
e_y[i] = ey * e_norm_inv;
e_x[i] = ex * e_norm_inv; // # 1 MULT
e_y[i] = ey * e_norm_inv; // # 1 MULT
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i];
float velocity_norm = sqrt(velocity_norm_sq);
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i]; // # 2 MULTs, 1 ADD
float velocity_norm = sqrt(velocity_norm_sq); // # 1 SQRT
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);
sb_sq[i] = velocity_norm_sq * delta_t_sq; // # 1 MULT
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t); // # 2 MULTs (vel_norm*delta_t shared with next line)
sb_e_y[i] = e_y[i] * (velocity_norm * delta_t); // # 1 MULT
#if !BENCHMARK
if (dump_path && (t % dump_interval == 0)) {
@@ -1,503 +0,0 @@
// # Changes: pure vrsqrtps
// # Predecessor: variant_float_inlining_SoA_AVX2_polyexp.cpp
//
// We first tried rsqrt + 1 Newton step to keep full sqrt precision, but it
// turned out slower than just using vsqrtps. The Newton step needs 2 muls
// and an fnmadd on top of rsqrt, so the whole recovered sqrt ends up at
// ~20 cycles of latency, while vsqrtps is only 12. Since the OoO core was
// already running multiple sqrts in parallel, sqrt throughput wasn't really
// the bottleneck. All the Newton step did was make the dependency chain
// longer, which hurt more than the faster rsqrt helped.
//
// So we drop the Newton step. rsqrt + 1 mul gives us a sqrt in 8 cycles
// with about 12-bit precision, which is well within the 1cm verify tolerance.
// The main structural win is killing the vdivps. rsqrt produces 1/sqrt(x)
// directly, so we keep the inverses around (inv_r_ab_n, inv_r_ab2_n,
// inv_two_b) and rewrite the force scaling. The original had
// "numer / (two_b * r_ab_n * r_ab2_n)" which needs a vdivps; now it's
// "numer * inv_two_b * inv_r_ab_n * inv_r_ab2_n", trading one divide for
// two muls. Divide was the long-pole op on port 0, so this is the bigger
// of the two changes.
#include "../headers/board_reduced_float.hpp"
#include "../headers/constants_float.hpp"
#include "../headers/pedestrian_reduced_float.hpp"
#include <immintrin.h> // avx2
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <math.h>
#include <random>
#include <tuple>
#include <vector>
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
#ifndef RANDOM_SEED
#define RANDOM_SEED 0
#endif
#ifndef BENCHMARK
#define BENCHMARK 0
#endif
using namespace std;
vector<float> pos_x, pos_y;
vector<float> sb_sq;
vector<float> sb_e_x, sb_e_y;
vector<float> e_x, e_y;
vector<float> curr_vel_x, curr_vel_y;
vector<float> w_x, w_y;
vector<float> desired_v0;
vector<float> v_max;
vector<float> 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<float> velocity_gen(v0_mean, v0_std_dev);
uniform_real_distribution<float> y_gen(0.3, BOARD_WIDTH - 0.3);
uniform_real_distribution<float> x_gen_left(2.0, BOARD_LENGTH / 2.0);
uniform_real_distribution<float> x_gen_right(BOARD_LENGTH / 2.0, BOARD_LENGTH - 2.0);
const int n_p_pad = (n_p + 7) & ~7;
pos_x.resize(n_p_pad);
pos_y.resize(n_p_pad);
sb_sq.resize(n_p_pad);
sb_e_x.resize(n_p_pad);
sb_e_y.resize(n_p_pad);
e_x.resize(n_p_pad);
e_y.resize(n_p_pad);
curr_vel_x.resize(n_p_pad);
curr_vel_y.resize(n_p_pad);
w_x.resize(n_p_pad);
w_y.resize(n_p_pad);
desired_v0.resize(n_p_pad);
v_max.resize(n_p_pad);
destination_x.resize(n_p_pad);
destination_y.resize(n_p_pad);
color.resize(n_p_pad);
int n_p_half = n_p / 2;
for (int i = 0; i < n_p_half; i++) {
float x = x_gen_left(gen);
float y = y_gen(gen);
float dest_x = BOARD_LENGTH + 30.0;
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1);
float 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;
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
for (int i = n_p_half; i < n_p; i++) {
float x = x_gen_right(gen);
float y = y_gen(gen);
float dest_x = -30;
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1);
float 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;
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
for (int j = n_p; j < n_p_pad; j++) {
pos_x[j] = 1e9f;
pos_y[j] = 1e9f;
sb_sq[j] = 0.0f;
sb_e_x[j] = 0.0f;
sb_e_y[j] = 0.0f;
}
return nullptr;
}
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// simd expf, degree-12 taylor on the [-ln2/2, ln2/2] reduced range,
// schraudolph 2^n bit trick on the exponent slot.
// # 128 MULTs, 112 ADDs
static inline __m256 exp256_ps(__m256 x) {
const __m256 log2e = _mm256_set1_ps(1.4426950408889634f);
__m256 nf = _mm256_round_ps(_mm256_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC); // # 8 MULTs
const __m256 ln2 = _mm256_set1_ps(0.6931471805599453f);
__m256 r = _mm256_fnmadd_ps(nf, ln2, x); // # 8 MULTs, 8 ADDs
const __m256 c13 = _mm256_set1_ps(c13_scalar);
const __m256 c12 = _mm256_set1_ps(c12_scalar);
const __m256 c11 = _mm256_set1_ps(c11_scalar);
const __m256 c10 = _mm256_set1_ps(c10_scalar);
const __m256 c9 = _mm256_set1_ps(c9_scalar);
const __m256 c8 = _mm256_set1_ps(c8_scalar);
const __m256 c7 = _mm256_set1_ps(c7_scalar);
const __m256 c6 = _mm256_set1_ps(c6_scalar);
const __m256 c5 = _mm256_set1_ps(c5_scalar);
const __m256 c4 = _mm256_set1_ps(c4_scalar);
const __m256 c3 = _mm256_set1_ps(c3_scalar);
const __m256 c2 = _mm256_set1_ps(c2_scalar);
const __m256 c1 = _mm256_set1_ps(c1_scalar);
const __m256 c0 = _mm256_set1_ps(c0_scalar);
__m256 p = c13;
// # 13 * (8 MULTs, 8 ADDs)
// # = 104 MULTs, 104 ADDs
p = _mm256_fmadd_ps(p, r, c12);
p = _mm256_fmadd_ps(p, r, c11);
p = _mm256_fmadd_ps(p, r, c10);
p = _mm256_fmadd_ps(p, r, c9);
p = _mm256_fmadd_ps(p, r, c8);
p = _mm256_fmadd_ps(p, r, c7);
p = _mm256_fmadd_ps(p, r, c6);
p = _mm256_fmadd_ps(p, r, c5);
p = _mm256_fmadd_ps(p, r, c4);
p = _mm256_fmadd_ps(p, r, c3);
p = _mm256_fmadd_ps(p, r, c2);
p = _mm256_fmadd_ps(p, r, c1);
p = _mm256_fmadd_ps(p, r, c0);
__m256i ni = _mm256_cvtps_epi32(nf);
__m256i biased = _mm256_add_epi32(ni, _mm256_set1_epi32(127));
__m256i scaled = _mm256_slli_epi32(biased, 23);
__m256 pow2n = _mm256_castsi256_ps(scaled);
return _mm256_mul_ps(p, pow2n); // # 8 MULTs
}
int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, const char *dump_path) {
(void)pedestrians;
#if BENCHMARK
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
vector<tuple<int, int, float, float, float, float, 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++) {
samples.emplace_back(0, i, pos_x[i], pos_y[i], e_x[i], e_y[i], color[i]);
}
}
#endif
const int n_p_pad = (n_p + 7) & ~7;
alignas(32) const float lane_offset_arr[8] = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f};
const __m256 lane_off = _mm256_load_ps(lane_offset_arr);
const __m256 v_neg_v0_div_2s = _mm256_set1_ps(NEG_V0_AB_div_TWO_SIGMA);
const __m256 v_neg_2s_inv = _mm256_set1_ps(NEG_TWO_SIGMA_INV);
const __m256 v_cos_phi_sq = _mm256_set1_ps(cos_phi_sq);
const __m256 v_dt_neg = _mm256_set1_ps(dt_neg);
const __m256 v_c_neg_dt = _mm256_set1_ps(c_neg_times_dt);
const __m256 v_zero = _mm256_setzero_ps();
const __m256 v_eps_sq = _mm256_set1_ps(EPS_SQ);
// const __m256 v_np = _mm256_set1_ps((float)n_p);
const __m256 v_exp_min = _mm256_set1_ps(-87.0f);
// # flops per iteration (one pass of the outer time loop):
// # ADDs: n_p * 22 + n_p * n_p_pad * 28 + n_p * board.n_obstacles * 10 + n_p * 7
// # MULTs: n_p * 4 + n_p * n_p_pad * 42 + n_p * board.n_obstacles * 13 + n_p * 16
// # DIVs: n_p * n_p_pad * 3 + n_p * board.n_obstacles + n_p * 2
// # SQRTs: n_p * n_p_pad * 3 + n_p * board.n_obstacles + n_p * 3
// # EXPs: n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
float desired_velocity_x = e_x[i] * desired_v0[i]; // # 1 MULT
float desired_velocity_y = e_y[i] * desired_v0[i]; // # 1 MULT
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
// ghost-self trick: park pedestrian i at 1e9 for its own j-loop, broadcasting i's
// real position from a saved copy. then the j==i lane reads 1e9 like a padding lane
// -> ~1e-37 force, self-cancels with no mask. restored right after the loop.
const float saved_pix = pos_x[i];
const float saved_piy = pos_y[i];
const __m256 pix = _mm256_set1_ps(saved_pix);
const __m256 piy = _mm256_set1_ps(saved_piy);
const __m256 eix = _mm256_set1_ps(e_x[i]);
const __m256 eiy = _mm256_set1_ps(e_y[i]);
pos_x[i] = 1e9f;
pos_y[i] = 1e9f;
__m256 acc_x = _mm256_setzero_ps();
__m256 acc_y = _mm256_setzero_ps();
for (int j = 0; j < n_p_pad; j += 8) {
// __m256 j_idx = _mm256_add_ps(_mm256_set1_ps((float)j), lane_off);
//
// __m256 m_neq_i = _mm256_cmp_ps(j_idx, v_i, _CMP_NEQ_OQ);
// __m256 m_in_range = _mm256_cmp_ps(j_idx, v_np, _CMP_LT_OQ);
// __m256 active = _mm256_and_ps(m_neq_i, m_in_range);
__m256 pjx = _mm256_loadu_ps(&pos_x[j]);
__m256 pjy = _mm256_loadu_ps(&pos_y[j]);
__m256 sbe_jx = _mm256_loadu_ps(&sb_e_x[j]);
__m256 sbe_jy = _mm256_loadu_ps(&sb_e_y[j]);
__m256 sb_jsq = _mm256_loadu_ps(&sb_sq[j]);
__m256 r_ab_x = _mm256_sub_ps(pix, pjx); // # 8 ADDs
__m256 r_ab_y = _mm256_sub_ps(piy, pjy); // # 8 ADDs
__m256 r_ab_n2 = _mm256_fmadd_ps(r_ab_x, r_ab_x, _mm256_mul_ps(r_ab_y, r_ab_y)); // # 16 MULTs, 8 ADDs
// clamp before rsqrt: rsqrt(0) = inf, then x*inf = 0*inf = NaN downstream. EPS_SQ
// means the inv_sqrt tops out at 1/EPS and r_ab_n = EPS_SQ/EPS = EPS, matches the predecessor.
r_ab_n2 = _mm256_max_ps(r_ab_n2, v_eps_sq);
__m256 inv_r_ab_n = _mm256_rsqrt_ps(r_ab_n2); // # 8 SQRTs, 8 DIVs
//__m256 r_ab_n = _mm256_mul_ps(r_ab_n2, inv_r_ab_n);
__m256 r_ab2_x = _mm256_sub_ps(r_ab_x, sbe_jx); // # 8 ADDs
__m256 r_ab2_y = _mm256_sub_ps(r_ab_y, sbe_jy); // # 8 ADDs
__m256 r_ab2_n2 = _mm256_fmadd_ps(r_ab2_x, r_ab2_x, _mm256_mul_ps(r_ab2_y, r_ab2_y)); // # 16 MULTs, 8 ADDs
r_ab2_n2 = _mm256_max_ps(r_ab2_n2, v_eps_sq);
__m256 inv_r_ab2_n = _mm256_rsqrt_ps(r_ab2_n2); // # 8 SQRTs, 8 DIVs
//__m256 r_ab2_n = _mm256_mul_ps(r_ab2_n2, inv_r_ab2_n);
// r_sum = |r_ab| + |r_ab2|, fused. sqrt(x) = x * rsqrt(x)
// the two norms are used only here (later code uses inv_r_ab_n / inv_r_ab2_n), so fold the two
// recovery muls + add into one mul + one fmadd. -1 FP op, -2 live regs.
__m256 r_sum = _mm256_fmadd_ps(r_ab2_n2, inv_r_ab2_n, _mm256_mul_ps(r_ab_n2, inv_r_ab_n)); // # 16 MULTs, 8 ADDs
__m256 two_b_sq = _mm256_fmsub_ps(r_sum, r_sum, sb_jsq); // # 8 MULTs, 8 ADDs
two_b_sq = _mm256_max_ps(two_b_sq, v_eps_sq);
__m256 inv_two_b = _mm256_rsqrt_ps(two_b_sq); // # 8 SQRTs, 8 DIVs
__m256 two_b = _mm256_mul_ps(two_b_sq, inv_two_b); // # 8 MULTs
__m256 e_arg = _mm256_mul_ps(two_b, v_neg_2s_inv); // # 8 MULTs
e_arg = _mm256_max_ps(e_arg, v_exp_min);
__m256 e_val = exp256_ps(e_arg); // # 128 MULTs, 112 ADDs
// smc = numer/denom in the predecessor. with inv-sqrts in hand the divide is free:
// sm_r_ab = smc * r_ab2_n = numer / (two_b * r_ab_n) = numer * inv_two_b * inv_r_ab_n
// sm_r_ab2 = smc * r_ab_n = numer / (two_b * r_ab2_n) = numer * inv_two_b * inv_r_ab2_n
// for the i==j and padding lanes inv_two_b*inv_r_ab_n is huge, but the active-mask AND on
// f_x/f_y below zeros the lane out before accumulation so it cant pollute anything.
__m256 numer_inv_two_b = _mm256_mul_ps(_mm256_mul_ps(r_sum, v_neg_v0_div_2s), _mm256_mul_ps(e_val, inv_two_b)); // # 24 MULTs
__m256 sm_r_ab = _mm256_mul_ps(numer_inv_two_b, inv_r_ab_n); // # 8 MULTs
__m256 sm_r_ab2 = _mm256_mul_ps(numer_inv_two_b, inv_r_ab2_n); // # 8 MULTs
__m256 f_x = _mm256_fmadd_ps(r_ab2_x, sm_r_ab2, _mm256_mul_ps(r_ab_x, sm_r_ab)); // # 16 MULTs, 8 ADDs
__m256 f_y = _mm256_fmadd_ps(r_ab2_y, sm_r_ab2, _mm256_mul_ps(r_ab_y, sm_r_ab)); // # 16 MULTs, 8 ADDs
__m256 f_norm_sq = _mm256_fmadd_ps(f_x, f_x, _mm256_mul_ps(f_y, f_y)); // # 16 MULTs, 8 ADDs
__m256 rhs = _mm256_mul_ps(f_norm_sq, v_cos_phi_sq); // # 8 MULTs
__m256 e_dot_f = _mm256_fmadd_ps(eix, f_x, _mm256_mul_ps(eiy, f_y)); // # 16 MULTs, 8 ADDs
__m256 e_dot_f_sq = _mm256_mul_ps(e_dot_f, e_dot_f); // # 8 MULTs
__m256 c1 = _mm256_cmp_ps(e_dot_f, v_zero, _CMP_LT_OQ);
__m256 c2 = _mm256_cmp_ps(e_dot_f_sq, rhs, _CMP_GT_OQ);
__m256 cond = _mm256_and_ps(c1, c2);
__m256 w_val = _mm256_blendv_ps(v_dt_neg, v_c_neg_dt, cond);
//__m256 f_x_safe = _mm256_and_ps(f_x, active);
//__m256 f_y_safe = _mm256_and_ps(f_y, active);
acc_x = _mm256_fmadd_ps(f_x, w_val, acc_x); // # 8 MULTs, 8 ADDs
acc_y = _mm256_fmadd_ps(f_y, w_val, acc_y); // # 8 MULTs, 8 ADDs
}
// restore the values
pos_x[i] = saved_pix;
pos_y[i] = saved_piy;
__m256 hsum = _mm256_hadd_ps(acc_x, acc_y); // # 8 ADDs (x and y interleaved)
__m128 hsum_lo = _mm256_castps256_ps128(hsum);
__m128 hsum_hi = _mm256_extractf128_ps(hsum, 1);
__m128 sum4 = _mm_add_ps(hsum_lo, hsum_hi); // # 4 ADDs
sum4 = _mm_hadd_ps(sum4, sum4); // # 4 ADDs (total hadd: 16 ADDs for x+y)
alignas(16) float sum_out[4];
_mm_store_ps(sum_out, sum4);
f_alpha_dt_x += sum_out[0]; // # 1 ADD
f_alpha_dt_y += sum_out[1]; // # 1 ADD
for (int k = 0; k < board.n_obstacles; k++) {
float v1_x = pos_x[i] - board.obstacles[k].start_x; // # 1 ADD
float v1_y = pos_y[i] - board.obstacles[k].start_y; // # 1 ADD
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv; // # 3 MULTs, 1 ADD
if (projection < 0.0)
projection = 0.0;
else if (projection > 1.0)
projection = 1.0;
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection); // # 1 MULT, 2 ADDs
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection); // # 1 MULT, 2 ADDs
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y); // # 2 MULTs, 1 ADD, 1 SQRT
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm; // # 2 MULTs, 1 EXP, 1 DIV
float f_alpha_B_neg_x = r_aB_x * scalar_mult; // # 1 MULT
float f_alpha_B_neg_y = r_aB_y * scalar_mult; // # 1 MULT
float w_val;
float e_dot_f = (f_alpha_B_neg_y == 0) ? 0 : (f_alpha_B_neg_y < 0 ? -e_y[i] : e_y[i]);
if (e_dot_f >= cos_phi)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val; // # 1 MULT
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val; // # 1 MULT
f_alpha_dt_x += f_alpha_B_dt_x; // # 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y; // # 1 ADD
}
w_x[i] += f_alpha_dt_x; // # 1 ADD
w_y[i] += f_alpha_dt_y; // # 1 ADD
}
for (int i = 0; i < n_p; i++) {
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]); // # 2 MULTs, 1 ADD, 1 SQRT
float w_norm2 = w_norm > EPS ? w_norm : EPS;
float correction_factor = min(1.0f, v_max[i] / w_norm2); // # 1 DIV
curr_vel_x[i] = w_x[i] * correction_factor; // # 1 MULT
curr_vel_y[i] = w_y[i] * correction_factor; // # 1 MULT
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt; // # 1 MULT, 1 ADD
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt; // # 1 MULT, 1 ADD
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);
float ex = destination_x[i] - pos_x[i]; // # 1 ADD
float ey = destination_y[i] - pos_y[i]; // # 1 ADD
float e_norm = sqrt(ex * ex + ey * ey); // # 2 MULTs, 1 ADD, 1 SQRT
float e_norm2 = e_norm > EPS ? e_norm : EPS;
float e_norm_inv = 1.0 / e_norm2; // # 1 DIV
e_x[i] = ex * e_norm_inv; // # 1 MULT
e_y[i] = ey * e_norm_inv; // # 1 MULT
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i]; // # 2 MULTs, 1 ADD
float velocity_norm = sqrt(velocity_norm_sq); // # 1 SQRT
sb_sq[i] = velocity_norm_sq * delta_t_sq; // # 1 MULT
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t); // # 2 MULTs (vel_norm*delta_t shared with next line)
sb_e_y[i] = e_y[i] * (velocity_norm * delta_t); // # 1 MULT
#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;
}
@@ -13,6 +13,8 @@
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
@@ -161,21 +163,46 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// # 256 MULTs, 224 ADDs
static inline __m512 _avx512_exp_ps(__m512 x) {
const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
// # 16 MULTs
__m512 nf = _mm512_roundscale_ps(_mm512_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
const __m512 ln2 = _mm512_set1_ps(0.6931471805599453f);
// # 16 MULTs, 16 ADDs
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
const __m512 c5 = _mm512_set1_ps(1.0f / 120.0f);
const __m512 c4 = _mm512_set1_ps(1.0f / 24.0f);
const __m512 c3 = _mm512_set1_ps(1.0f / 6.0f);
const __m512 c2 = _mm512_set1_ps(0.5f);
const __m512 c1 = _mm512_set1_ps(1.0f);
const __m512 c0 = _mm512_set1_ps(1.0f);
const __m512 c13 = _mm512_set1_ps(c13_scalar);
const __m512 c12 = _mm512_set1_ps(c12_scalar);
const __m512 c11 = _mm512_set1_ps(c11_scalar);
const __m512 c10 = _mm512_set1_ps(c10_scalar);
const __m512 c9 = _mm512_set1_ps(c9_scalar);
const __m512 c8 = _mm512_set1_ps(c8_scalar);
const __m512 c7 = _mm512_set1_ps(c7_scalar);
const __m512 c6 = _mm512_set1_ps(c6_scalar);
const __m512 c5 = _mm512_set1_ps(c5_scalar);
const __m512 c4 = _mm512_set1_ps(c4_scalar);
const __m512 c3 = _mm512_set1_ps(c3_scalar);
const __m512 c2 = _mm512_set1_ps(c2_scalar);
const __m512 c1 = _mm512_set1_ps(c1_scalar);
const __m512 c0 = _mm512_set1_ps(c0_scalar);
__m512 p = c5;
__m512 p = c13;
// # 13 * (16 MULTs, 16 ADDs)
// # = 208 MULTs, 208 ADDs
p = _mm512_fmadd_ps(p, r, c12);
p = _mm512_fmadd_ps(p, r, c11);
p = _mm512_fmadd_ps(p, r, c10);
p = _mm512_fmadd_ps(p, r, c9);
p = _mm512_fmadd_ps(p, r, c8);
p = _mm512_fmadd_ps(p, r, c7);
p = _mm512_fmadd_ps(p, r, c6);
p = _mm512_fmadd_ps(p, r, c5);
p = _mm512_fmadd_ps(p, r, c4);
p = _mm512_fmadd_ps(p, r, c3);
p = _mm512_fmadd_ps(p, r, c2);
@@ -187,6 +214,7 @@ static inline __m512 _avx512_exp_ps(__m512 x) {
__m512i scaled = _mm512_slli_epi32(biased, 23);
__m512 pow2n = _mm512_castsi512_ps(scaled);
// # 16 MULTs
return _mm512_mul_ps(p, pow2n);
}
@@ -225,11 +253,42 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
const __m512 v_np = _mm512_set1_ps((float)n_p);
const __m512 v_exp_min = _mm512_set1_ps(-87.0f);
// # ADDs: n_iter * (
// # n_p * 36 +
// # n_p * n_p_pad * 29 +
// # n_p * board.n_obstacles * 10 +
// # n_p * 7
// # )
// # MULTs: n_iter * (
// # n_p * 4 +
// # n_p * n_p_pad * 40 +
// # n_p * board.n_obstacles * 13 +
// # n_p * 17
// # )
// # DIVs: n_iter * (
// # n_p * n_p_pad +
// # n_p * board.n_obstacles +
// # n_p * 2
// # )
// # SQRTs: n_iter * (
// # n_p * n_p_pad * 3 +
// # n_p * board.n_obstacles +
// # n_p * 3
// # )
// # EXPs: n_iter * n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
// # 2 MULTs
float desired_velocity_x = e_x[i] * desired_v0[i];
float desired_velocity_y = e_y[i] * desired_v0[i];
// # 2 ADDs, 2 MULTs
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
@@ -243,6 +302,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 acc_y = _mm512_setzero_ps();
for (int j = 0; j < n_p_pad; j += 16) {
// # 16 ADDs
__m512 j_idx = _mm512_add_ps(_mm512_set1_ps((float)j), lane_off);
__mmask16 m_neq_i = _mm512_cmp_ps_mask(j_idx, v_i, _CMP_NEQ_OQ);
@@ -255,42 +316,76 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 sbe_jy = _mm512_loadu_ps(&sb_e_y[j]);
__m512 sb_jsq = _mm512_loadu_ps(&sb_sq[j]);
// # 32 ADDs
__m512 r_ab_x = _mm512_sub_ps(pix, pjx);
__m512 r_ab_y = _mm512_sub_ps(piy, pjy);
// # 32 MULTs, 16 ADDs
__m512 r_ab_n2 = _mm512_fmadd_ps(r_ab_x, r_ab_x, _mm512_mul_ps(r_ab_y, r_ab_y));
// # 16 SQRTs
__m512 r_ab_n = _mm512_sqrt_ps(r_ab_n2);
r_ab_n = _mm512_max_ps(r_ab_n, v_eps);
// # 32 ADDs
__m512 r_ab2_x = _mm512_sub_ps(r_ab_x, sbe_jx);
__m512 r_ab2_y = _mm512_sub_ps(r_ab_y, sbe_jy);
// # 32 MULTs, 16 ADDs
__m512 r_ab2_n2 = _mm512_fmadd_ps(r_ab2_x, r_ab2_x, _mm512_mul_ps(r_ab2_y, r_ab2_y));
// # 16 SQRTs
__m512 r_ab2_n = _mm512_sqrt_ps(r_ab2_n2);
r_ab2_n = _mm512_max_ps(r_ab2_n, v_eps);
// # 16 ADDs
__m512 r_sum = _mm512_add_ps(r_ab_n, r_ab2_n);
// # 16 MULTs, 16 ADDs
__m512 two_b_sq = _mm512_fmsub_ps(r_sum, r_sum, sb_jsq);
two_b_sq = _mm512_max_ps(two_b_sq, v_eps_sq);
// # 16 SQRTs
__m512 two_b = _mm512_sqrt_ps(two_b_sq);
// # 16 MULTs
__m512 e_arg = _mm512_mul_ps(two_b, v_neg_2s_inv);
e_arg = _mm512_max_ps(e_arg, v_exp_min);
// # 256 MULTs, 224 ADDs
__m512 e_val = _avx512_exp_ps(e_arg);
// # 64 MULTs
__m512 numer = _mm512_mul_ps(_mm512_mul_ps(r_sum, v_neg_v0_div_2s), e_val);
__m512 denom = _mm512_mul_ps(_mm512_mul_ps(two_b, r_ab_n), r_ab2_n);
__m512 denom_safe = _mm512_mask_blend_ps(active, v_one, denom);
// # 16 DIVs
__m512 smc = _mm512_div_ps(numer, denom_safe);
// # 32 MULTs
__m512 sm_r_ab = _mm512_mul_ps(smc, r_ab2_n);
__m512 sm_r_ab2 = _mm512_mul_ps(smc, r_ab_n);
// # 64 MULTs, 32 ADDs
__m512 f_x = _mm512_fmadd_ps(r_ab2_x, sm_r_ab2, _mm512_mul_ps(r_ab_x, sm_r_ab));
__m512 f_y = _mm512_fmadd_ps(r_ab2_y, sm_r_ab2, _mm512_mul_ps(r_ab_y, sm_r_ab));
// # 32 MULTs, 16 ADDs
__m512 f_norm_sq = _mm512_fmadd_ps(f_x, f_x, _mm512_mul_ps(f_y, f_y));
// # 16 MULTs
__m512 rhs = _mm512_mul_ps(f_norm_sq, v_cos_phi_sq);
// # 32 MULTs, 16 ADDs
__m512 e_dot_f = _mm512_fmadd_ps(eix, f_x, _mm512_mul_ps(eiy, f_y));
// # 16 MULTs
__m512 e_dot_f_sq = _mm512_mul_ps(e_dot_f, e_dot_f);
__mmask16 c1 = _mm512_cmp_ps_mask(e_dot_f, v_zero, _CMP_LT_OQ);
@@ -302,19 +397,24 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 f_x_safe = _mm512_maskz_mov_ps(active, f_x);
__m512 f_y_safe = _mm512_maskz_mov_ps(active, f_y);
// # 32 MULTs, 32 ADDs
acc_x = _mm512_fmadd_ps(f_x_safe, w_val, acc_x);
acc_y = _mm512_fmadd_ps(f_y_safe, w_val, acc_y);
}
// # 32 ADDs
f_alpha_dt_x += _mm512_reduce_add_ps(acc_x);
f_alpha_dt_y += _mm512_reduce_add_ps(acc_y);
for (int k = 0; k < board.n_obstacles; k++) {
// # 2 ADDs
float v1_x = pos_x[i] - board.obstacles[k].start_x;
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
// # 3 MULTs, 1 ADD
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
if (projection < 0.0f)
@@ -322,12 +422,15 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
else if (projection > 1.0f)
projection = 1.0f;
// # 4 ADDs, 2 MULTs
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
// # 1 SQRT, 2 MULTs, 1 ADD
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
// # 4 MULTs, 1 EXP, 1 DIV
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
@@ -338,9 +441,11 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
if (e_dot_f < cos_phi)
w_val = c_neg_times_dt;
// # 2 MULTS
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
// # 2 ADDs
f_alpha_dt_x += f_alpha_B_dt_x;
f_alpha_dt_y += f_alpha_B_dt_y;
}
@@ -350,17 +455,24 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
}
for (int i = 0; i < n_p; i++) {
// # 1 SQRT, 2 MULTs, 1 ADD
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]);
float w_norm2 = w_norm > EPS ? w_norm : EPS;
// # 1 DIV
float correction_factor = min(1.0f, v_max[i] / w_norm2);
// # 2 MULTs
curr_vel_x[i] = w_x[i] * correction_factor;
curr_vel_y[i] = w_y[i] * correction_factor;
// # 2 MULTs, 2 ADDs
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.5f)) {
if (color[i] == 0 && pos_x[i] > BOARD_LENGTH_MINUS_ONE_POINT_FIVE) {
pos_x[i] = 1.0f;
destination_y[i] = pos_y[i];
w_x[i] = desired_v0[i];
@@ -369,7 +481,7 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
curr_vel_y[i] = w_y[i];
}
if (color[i] == 1 && pos_x[i] < 1.5f) {
pos_x[i] = BOARD_LENGTH - 1.0f;
pos_x[i] = BOARD_LENGTH_MINUS_ONE;
destination_y[i] = pos_y[i];
w_x[i] = -desired_v0[i];
w_y[i] = 0.0f;
@@ -379,19 +491,29 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
pos_y[i] = clamp(pos_y[i], WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
// # 2 ADDs
float ex = destination_x[i] - pos_x[i];
float ey = destination_y[i] - pos_y[i];
// # 1 SQRT, 2 MULTs, 1 ADD
float e_norm = sqrt(ex * ex + ey * ey);
float e_norm2 = e_norm > EPS ? e_norm : EPS;
// # 1 DIV
float e_norm_inv = 1.0f / e_norm2;
// # 2 MULTs
e_x[i] = ex * e_norm_inv;
e_y[i] = ey * e_norm_inv;
// # 2 MULTs, 1 ADD
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i];
// # 1 SQRT
float velocity_norm = sqrt(velocity_norm_sq);
// # 5 MULTs
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);
@@ -13,6 +13,8 @@
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
@@ -161,21 +163,46 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// # 256 MULTs, 224 ADDs
static inline __m512 _avx512_exp_ps(__m512 x) {
const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
// # 16 MULTs
__m512 nf = _mm512_roundscale_ps(_mm512_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
const __m512 ln2 = _mm512_set1_ps(0.6931471805599453f);
// # 16 MULTs, 16 ADDs
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
const __m512 c5 = _mm512_set1_ps(1.0f / 120.0f);
const __m512 c4 = _mm512_set1_ps(1.0f / 24.0f);
const __m512 c3 = _mm512_set1_ps(1.0f / 6.0f);
const __m512 c2 = _mm512_set1_ps(0.5f);
const __m512 c1 = _mm512_set1_ps(1.0f);
const __m512 c0 = _mm512_set1_ps(1.0f);
const __m512 c13 = _mm512_set1_ps(c13_scalar);
const __m512 c12 = _mm512_set1_ps(c12_scalar);
const __m512 c11 = _mm512_set1_ps(c11_scalar);
const __m512 c10 = _mm512_set1_ps(c10_scalar);
const __m512 c9 = _mm512_set1_ps(c9_scalar);
const __m512 c8 = _mm512_set1_ps(c8_scalar);
const __m512 c7 = _mm512_set1_ps(c7_scalar);
const __m512 c6 = _mm512_set1_ps(c6_scalar);
const __m512 c5 = _mm512_set1_ps(c5_scalar);
const __m512 c4 = _mm512_set1_ps(c4_scalar);
const __m512 c3 = _mm512_set1_ps(c3_scalar);
const __m512 c2 = _mm512_set1_ps(c2_scalar);
const __m512 c1 = _mm512_set1_ps(c1_scalar);
const __m512 c0 = _mm512_set1_ps(c0_scalar);
__m512 p = c5;
__m512 p = c13;
// # 13 * (16 MULTs, 16 ADDs)
// # = 208 MULTs, 208 ADDs
p = _mm512_fmadd_ps(p, r, c12);
p = _mm512_fmadd_ps(p, r, c11);
p = _mm512_fmadd_ps(p, r, c10);
p = _mm512_fmadd_ps(p, r, c9);
p = _mm512_fmadd_ps(p, r, c8);
p = _mm512_fmadd_ps(p, r, c7);
p = _mm512_fmadd_ps(p, r, c6);
p = _mm512_fmadd_ps(p, r, c5);
p = _mm512_fmadd_ps(p, r, c4);
p = _mm512_fmadd_ps(p, r, c3);
p = _mm512_fmadd_ps(p, r, c2);
@@ -187,9 +214,11 @@ static inline __m512 _avx512_exp_ps(__m512 x) {
__m512i scaled = _mm512_slli_epi32(biased, 23);
__m512 pow2n = _mm512_castsi512_ps(scaled);
// # 16 MULTs
return _mm512_mul_ps(p, pow2n);
}
// # 7 MULTs, 2 ADDs
static inline float fast_inv_sqrt(float number) {
float y = number;
@@ -199,7 +228,10 @@ static inline float fast_inv_sqrt(float number) {
y = bit_cast<float>(i);
// # 1 MULT
float x2 = number * 0.5;
// # 6 MULTs, 2 ADDs
y = y * (1.5 - (x2 * y * y));
y = y * (1.5 - (x2 * y * y));
@@ -241,11 +273,41 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
const __m512 v_np = _mm512_set1_ps((float)n_p);
const __m512 v_exp_min = _mm512_set1_ps(-87.0f);
// # ADDs: n_iter * (
// # n_p * 36 +
// # n_p * n_p_pad * 29 +
// # n_p * board.n_obstacles * 10 +
// # n_p * 11
// # )
// # MULTs: n_iter * (
// # n_p * 4 +
// # n_p * n_p_pad * 40 +
// # n_p * board.n_obstacles * 13 +
// # n_p * 30
// # )
// # DIVs: n_iter * (
// # n_p * n_p_pad +
// # n_p * board.n_obstacles
// # )
// # SQRTs: n_iter * (
// # n_p * n_p_pad * 3 +
// # n_p * board.n_obstacles +
// # n_p
// # )
// # EXPs: n_iter * n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
// # 2 MULTs
float desired_velocity_x = e_x[i] * desired_v0[i];
float desired_velocity_y = e_y[i] * desired_v0[i];
// # 2 ADDs, 2 MULTs
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
@@ -259,6 +321,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 acc_y = _mm512_setzero_ps();
for (int j = 0; j < n_p_pad; j += 16) {
// # 16 ADDs
__m512 j_idx = _mm512_add_ps(_mm512_set1_ps((float)j), lane_off);
__mmask16 m_neq_i = _mm512_cmp_ps_mask(j_idx, v_i, _CMP_NEQ_OQ);
@@ -271,42 +335,76 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 sbe_jy = _mm512_loadu_ps(&sb_e_y[j]);
__m512 sb_jsq = _mm512_loadu_ps(&sb_sq[j]);
// # 32 ADDs
__m512 r_ab_x = _mm512_sub_ps(pix, pjx);
__m512 r_ab_y = _mm512_sub_ps(piy, pjy);
// # 32 MULTs, 16 ADDs
__m512 r_ab_n2 = _mm512_fmadd_ps(r_ab_x, r_ab_x, _mm512_mul_ps(r_ab_y, r_ab_y));
// # 16 SQRTs
__m512 r_ab_n = _mm512_sqrt_ps(r_ab_n2);
r_ab_n = _mm512_max_ps(r_ab_n, v_eps);
// # 32 ADDs
__m512 r_ab2_x = _mm512_sub_ps(r_ab_x, sbe_jx);
__m512 r_ab2_y = _mm512_sub_ps(r_ab_y, sbe_jy);
// # 32 MULTs, 16 ADDs
__m512 r_ab2_n2 = _mm512_fmadd_ps(r_ab2_x, r_ab2_x, _mm512_mul_ps(r_ab2_y, r_ab2_y));
// # 16 SQRTs
__m512 r_ab2_n = _mm512_sqrt_ps(r_ab2_n2);
r_ab2_n = _mm512_max_ps(r_ab2_n, v_eps);
// # 16 ADDs
__m512 r_sum = _mm512_add_ps(r_ab_n, r_ab2_n);
// # 16 MULTs, 16 ADDs
__m512 two_b_sq = _mm512_fmsub_ps(r_sum, r_sum, sb_jsq);
two_b_sq = _mm512_max_ps(two_b_sq, v_eps_sq);
// # 16 SQRTs
__m512 two_b = _mm512_sqrt_ps(two_b_sq);
// # 16 MULTs
__m512 e_arg = _mm512_mul_ps(two_b, v_neg_2s_inv);
e_arg = _mm512_max_ps(e_arg, v_exp_min);
// # 256 MULTs, 224 ADDs
__m512 e_val = _avx512_exp_ps(e_arg);
// # 64 MULTs
__m512 numer = _mm512_mul_ps(_mm512_mul_ps(r_sum, v_neg_v0_div_2s), e_val);
__m512 denom = _mm512_mul_ps(_mm512_mul_ps(two_b, r_ab_n), r_ab2_n);
__m512 denom_safe = _mm512_mask_blend_ps(active, v_one, denom);
// # 16 DIVs
__m512 smc = _mm512_div_ps(numer, denom_safe);
// # 32 MULTs
__m512 sm_r_ab = _mm512_mul_ps(smc, r_ab2_n);
__m512 sm_r_ab2 = _mm512_mul_ps(smc, r_ab_n);
// # 64 MULTs, 32 ADDs
__m512 f_x = _mm512_fmadd_ps(r_ab2_x, sm_r_ab2, _mm512_mul_ps(r_ab_x, sm_r_ab));
__m512 f_y = _mm512_fmadd_ps(r_ab2_y, sm_r_ab2, _mm512_mul_ps(r_ab_y, sm_r_ab));
// # 32 MULTs, 16 ADDs
__m512 f_norm_sq = _mm512_fmadd_ps(f_x, f_x, _mm512_mul_ps(f_y, f_y));
// # 16 MULTs
__m512 rhs = _mm512_mul_ps(f_norm_sq, v_cos_phi_sq);
// # 32 MULTs, 16 ADDs
__m512 e_dot_f = _mm512_fmadd_ps(eix, f_x, _mm512_mul_ps(eiy, f_y));
// # 16 MULTs
__m512 e_dot_f_sq = _mm512_mul_ps(e_dot_f, e_dot_f);
__mmask16 c1 = _mm512_cmp_ps_mask(e_dot_f, v_zero, _CMP_LT_OQ);
@@ -318,19 +416,24 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 f_x_safe = _mm512_maskz_mov_ps(active, f_x);
__m512 f_y_safe = _mm512_maskz_mov_ps(active, f_y);
// # 32 MULTs, 32 ADDs
acc_x = _mm512_fmadd_ps(f_x_safe, w_val, acc_x);
acc_y = _mm512_fmadd_ps(f_y_safe, w_val, acc_y);
}
// # 32 ADDs
f_alpha_dt_x += _mm512_reduce_add_ps(acc_x);
f_alpha_dt_y += _mm512_reduce_add_ps(acc_y);
for (int k = 0; k < board.n_obstacles; k++) {
// # 2 ADDs
float v1_x = pos_x[i] - board.obstacles[k].start_x;
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
// # 3 MULTs, 1 ADD
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
if (projection < 0.0f)
@@ -338,12 +441,17 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
else if (projection > 1.0f)
projection = 1.0f;
// # 4 ADDs, 2 MULTs
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
// # 1 SQRT, 2 MULTs, 1 ADD
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
// # 2 MULTs, 1 EXP, 1 DIV
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// # 2 MULTs
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
@@ -353,28 +461,36 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
if (e_dot_f < cos_phi)
w_val = c_neg_times_dt;
// # 2 MULTs
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
// # 2 ADDs
f_alpha_dt_x += f_alpha_B_dt_x;
f_alpha_dt_y += f_alpha_B_dt_y;
}
// # 2 ADDs
w_x[i] += f_alpha_dt_x;
w_y[i] += f_alpha_dt_y;
}
for (int i = 0; i < n_p; i++) {
// # 9 MULTs, 3 ADDs
float w_norm_inv = fast_inv_sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]);
// # 1 MULT
float correction_factor = min(1.0f, v_max[i] * w_norm_inv);
// # 2 MULTs
curr_vel_x[i] = w_x[i] * correction_factor;
curr_vel_y[i] = w_y[i] * correction_factor;
// # 2 MULTs, 2 ADDs
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.5f)) {
if (color[i] == 0 && pos_x[i] > (BOARD_LENGTH_MINUS_ONE_POINT_FIVE)) {
pos_x[i] = 1.0f;
destination_y[i] = pos_y[i];
w_x[i] = desired_v0[i];
@@ -383,7 +499,7 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
curr_vel_y[i] = w_y[i];
}
if (color[i] == 1 && pos_x[i] < 1.5f) {
pos_x[i] = BOARD_LENGTH - 1.0f;
pos_x[i] = BOARD_LENGTH_MINUS_ONE;
destination_y[i] = pos_y[i];
w_x[i] = -desired_v0[i];
w_y[i] = 0.0f;
@@ -393,19 +509,29 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
pos_y[i] = clamp(pos_y[i], WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
// # 2 ADDs
float ex = destination_x[i] - pos_x[i];
float ey = destination_y[i] - pos_y[i];
// # 9 MULTs, 3 ADDs
float e_norm_inv = fast_inv_sqrt(ex * ex + ey * ey);
// # 2 MULTs
e_x[i] = ex * e_norm_inv;
e_y[i] = ey * e_norm_inv;
// # 2 MULTs, 1 ADD
double velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i];
// # 1 MULT
double sb_sq_i = velocity_norm_sq * delta_t_sq;
// # 1 SQRT
double velocity_norm_mul_delta_t = sqrt(sb_sq_i);
sb_sq[i] = sb_sq_i;
// # 2 MULTs
sb_e_x[i] = e_x[i] * velocity_norm_mul_delta_t;
sb_e_y[i] = e_y[i] * velocity_norm_mul_delta_t;
@@ -13,6 +13,8 @@
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
@@ -161,21 +163,46 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// # 256 MULTs, 224 ADDs
static inline __m512 _avx512_exp_ps(__m512 x) {
const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
// # 16 MULTs
__m512 nf = _mm512_roundscale_ps(_mm512_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
const __m512 ln2 = _mm512_set1_ps(0.6931471805599453f);
// # 16 MULTs, 16 ADDs
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
const __m512 c5 = _mm512_set1_ps(1.0f / 120.0f);
const __m512 c4 = _mm512_set1_ps(1.0f / 24.0f);
const __m512 c3 = _mm512_set1_ps(1.0f / 6.0f);
const __m512 c2 = _mm512_set1_ps(0.5f);
const __m512 c1 = _mm512_set1_ps(1.0f);
const __m512 c0 = _mm512_set1_ps(1.0f);
const __m512 c13 = _mm512_set1_ps(c13_scalar);
const __m512 c12 = _mm512_set1_ps(c12_scalar);
const __m512 c11 = _mm512_set1_ps(c11_scalar);
const __m512 c10 = _mm512_set1_ps(c10_scalar);
const __m512 c9 = _mm512_set1_ps(c9_scalar);
const __m512 c8 = _mm512_set1_ps(c8_scalar);
const __m512 c7 = _mm512_set1_ps(c7_scalar);
const __m512 c6 = _mm512_set1_ps(c6_scalar);
const __m512 c5 = _mm512_set1_ps(c5_scalar);
const __m512 c4 = _mm512_set1_ps(c4_scalar);
const __m512 c3 = _mm512_set1_ps(c3_scalar);
const __m512 c2 = _mm512_set1_ps(c2_scalar);
const __m512 c1 = _mm512_set1_ps(c1_scalar);
const __m512 c0 = _mm512_set1_ps(c0_scalar);
__m512 p = c5;
__m512 p = c13;
// # 13 * (16 MULTs, 16 ADDs)
// # = 208 MULTs, 208 ADDs
p = _mm512_fmadd_ps(p, r, c12);
p = _mm512_fmadd_ps(p, r, c11);
p = _mm512_fmadd_ps(p, r, c10);
p = _mm512_fmadd_ps(p, r, c9);
p = _mm512_fmadd_ps(p, r, c8);
p = _mm512_fmadd_ps(p, r, c7);
p = _mm512_fmadd_ps(p, r, c6);
p = _mm512_fmadd_ps(p, r, c5);
p = _mm512_fmadd_ps(p, r, c4);
p = _mm512_fmadd_ps(p, r, c3);
p = _mm512_fmadd_ps(p, r, c2);
@@ -187,6 +214,7 @@ static inline __m512 _avx512_exp_ps(__m512 x) {
__m512i scaled = _mm512_slli_epi32(biased, 23);
__m512 pow2n = _mm512_castsi512_ps(scaled);
// # 16 MULTs
return _mm512_mul_ps(p, pow2n);
}
@@ -225,11 +253,42 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
const __m512 v_np = _mm512_set1_ps((float)n_p);
const __m512 v_exp_min = _mm512_set1_ps(-87.0f);
// # ADDs: n_iter * (
// # n_p * 36 +
// # n_p * n_p_pad * 29 +
// # n_p * board.n_obstacles * 10 +
// # n_p_pad * 7
// # )
// # MULTs: n_iter * (
// # n_p * 4 +
// # n_p * n_p_pad * 40 +
// # n_p * board.n_obstacles * 13 +
// # n_p_pad * 15
// # )
// # DIVs: n_iter * (
// # n_p * n_p_pad +
// # n_p * board.n_obstacles +
// # n_p_pad * 2
// # )
// # SQRTs: n_iter * (
// # n_p * n_p_pad * 3 +
// # n_p * board.n_obstacles +
// # n_p_pad * 3
// # )
// # EXPs: n_iter * n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
// # 2 MULTs
float desired_velocity_x = e_x[i] * desired_v0[i];
float desired_velocity_y = e_y[i] * desired_v0[i];
// # 2 ADDs, 2 MULTs
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
@@ -243,6 +302,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 acc_y = _mm512_setzero_ps();
for (int j = 0; j < n_p_pad; j += 16) {
// # 16 ADDs
__m512 j_idx = _mm512_add_ps(_mm512_set1_ps((float)j), lane_off);
__mmask16 m_neq_i = _mm512_cmp_ps_mask(j_idx, v_i, _CMP_NEQ_OQ);
@@ -255,42 +316,75 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 sbe_jy = _mm512_loadu_ps(&sb_e_y[j]);
__m512 sb_jsq = _mm512_loadu_ps(&sb_sq[j]);
// # 32 ADDs
__m512 r_ab_x = _mm512_sub_ps(pix, pjx);
__m512 r_ab_y = _mm512_sub_ps(piy, pjy);
// # 32 MULTs, 16 ADDs
__m512 r_ab_n2 = _mm512_fmadd_ps(r_ab_x, r_ab_x, _mm512_mul_ps(r_ab_y, r_ab_y));
// # 16 SQRTs
__m512 r_ab_n = _mm512_sqrt_ps(r_ab_n2);
r_ab_n = _mm512_max_ps(r_ab_n, v_eps);
// # 32 ADDs
__m512 r_ab2_x = _mm512_sub_ps(r_ab_x, sbe_jx);
__m512 r_ab2_y = _mm512_sub_ps(r_ab_y, sbe_jy);
// # 32 MULTs, 16 ADDs
__m512 r_ab2_n2 = _mm512_fmadd_ps(r_ab2_x, r_ab2_x, _mm512_mul_ps(r_ab2_y, r_ab2_y));
// # 16 SQRTs
__m512 r_ab2_n = _mm512_sqrt_ps(r_ab2_n2);
r_ab2_n = _mm512_max_ps(r_ab2_n, v_eps);
// # 16 ADDs
__m512 r_sum = _mm512_add_ps(r_ab_n, r_ab2_n);
// # 16 MULTs, 16 ADDs
__m512 two_b_sq = _mm512_fmsub_ps(r_sum, r_sum, sb_jsq);
two_b_sq = _mm512_max_ps(two_b_sq, v_eps_sq);
// # 16 SQRTs
__m512 two_b = _mm512_sqrt_ps(two_b_sq);
// # 16 MULTs
__m512 e_arg = _mm512_mul_ps(two_b, v_neg_2s_inv);
e_arg = _mm512_max_ps(e_arg, v_exp_min);
// # 256 MULTs, 224 ADDs
__m512 e_val = _avx512_exp_ps(e_arg);
// # 64 MULTs
__m512 numer = _mm512_mul_ps(_mm512_mul_ps(r_sum, v_neg_v0_div_2s), e_val);
__m512 denom = _mm512_mul_ps(_mm512_mul_ps(two_b, r_ab_n), r_ab2_n);
__m512 denom_safe = _mm512_mask_blend_ps(active, v_one, denom);
// # 16 DIVs
__m512 smc = _mm512_div_ps(numer, denom_safe);
// # 32 MULTs
__m512 sm_r_ab = _mm512_mul_ps(smc, r_ab2_n);
__m512 sm_r_ab2 = _mm512_mul_ps(smc, r_ab_n);
// # 64 MULTs, 32 ADDs
__m512 f_x = _mm512_fmadd_ps(r_ab2_x, sm_r_ab2, _mm512_mul_ps(r_ab_x, sm_r_ab));
__m512 f_y = _mm512_fmadd_ps(r_ab2_y, sm_r_ab2, _mm512_mul_ps(r_ab_y, sm_r_ab));
// # 32 MULTs, 16 ADDs
__m512 f_norm_sq = _mm512_fmadd_ps(f_x, f_x, _mm512_mul_ps(f_y, f_y));
// # 16 MULTs
__m512 rhs = _mm512_mul_ps(f_norm_sq, v_cos_phi_sq);
// # 32 MULTs, 16 ADDs
__m512 e_dot_f = _mm512_fmadd_ps(eix, f_x, _mm512_mul_ps(eiy, f_y));
// # 16 MULTs
__m512 e_dot_f_sq = _mm512_mul_ps(e_dot_f, e_dot_f);
__mmask16 c1 = _mm512_cmp_ps_mask(e_dot_f, v_zero, _CMP_LT_OQ);
@@ -302,19 +396,24 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 f_x_safe = _mm512_maskz_mov_ps(active, f_x);
__m512 f_y_safe = _mm512_maskz_mov_ps(active, f_y);
// # 32 MULTs, 32 ADDs
acc_x = _mm512_fmadd_ps(f_x_safe, w_val, acc_x);
acc_y = _mm512_fmadd_ps(f_y_safe, w_val, acc_y);
}
// # 32 ADDs
f_alpha_dt_x += _mm512_reduce_add_ps(acc_x);
f_alpha_dt_y += _mm512_reduce_add_ps(acc_y);
for (int k = 0; k < board.n_obstacles; k++) {
// # 2 ADDs
float v1_x = pos_x[i] - board.obstacles[k].start_x;
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
// # 3 MULTs, 1 ADD
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
if (projection < 0.0f)
@@ -322,12 +421,15 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
else if (projection > 1.0f)
projection = 1.0f;
// # 4 ADDs, 2 MULTs
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
// # 1 SQRT, 2 MULTs, 1 ADD
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
// # 4 MULTs, 1 EXP, 1 DIV
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
@@ -338,19 +440,22 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
if (e_dot_f < cos_phi)
w_val = c_neg_times_dt;
// # 2 MULTS
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
// # 2 ADDs
f_alpha_dt_x += f_alpha_B_dt_x;
f_alpha_dt_y += f_alpha_B_dt_y;
}
// # 2 ADDs
w_x[i] += f_alpha_dt_x;
w_y[i] += f_alpha_dt_y;
}
for (int i = 0; i < n_p_pad; i += 16) {
__m512 w_x_i_vec = _mm512_loadu_ps(&w_x[i]);
__m512 w_y_i_vec = _mm512_loadu_ps(&w_y[i]);
__m512 v_max_i_vec = _mm512_loadu_ps(&v_max[i]);
@@ -359,23 +464,31 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 destination_y_i_vec = _mm512_loadu_ps(&destination_y[i]);
__m512 destination_x_i_vec = _mm512_loadu_ps(&destination_x[i]);
__m512 desired_v0_i_vec = _mm512_loadu_ps(&desired_v0[i]);
__m512i color_i_vec = _mm512_loadu_si512((__m512i*)&color[i]);
__m512i color_i_vec = _mm512_loadu_si512((__m512i *)&color[i]);
// # 32 MULTs, 16 ADDs
__m512 w_norm_sq = _mm512_fmadd_ps(w_x_i_vec, w_x_i_vec, _mm512_mul_ps(w_y_i_vec, w_y_i_vec));
// # 16 SQRTs
__m512 w_norm = _mm512_sqrt_ps(w_norm_sq);
__m512 w_norm_safe = _mm512_max_ps(w_norm, _mm512_set1_ps(EPS));
// # 16 DIVs
__m512 correction_factor = _mm512_min_ps(_mm512_set1_ps(1.0f), _mm512_div_ps(v_max_i_vec, w_norm_safe));
// # 32 MULTs
__m512 curr_vel_x_i_vec = _mm512_mul_ps(w_x_i_vec, correction_factor);
__m512 curr_vel_y_i_vec = _mm512_mul_ps(w_y_i_vec, correction_factor);
__m512 dt_vec = _mm512_set1_ps(dt);
// # 32 MULTs, 32 ADDs
pos_x_i_vec = _mm512_fmadd_ps(curr_vel_x_i_vec, dt_vec, pos_x_i_vec);
pos_y_i_vec = _mm512_fmadd_ps(curr_vel_y_i_vec, dt_vec, pos_y_i_vec);
__mmask16 cond_1_left = _mm512_cmpeq_epi32_mask(color_i_vec, _mm512_setzero_si512());
__mmask16 cond_1_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH - 1.5f), _CMP_GT_OQ);
__mmask16 cond_1_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE_POINT_FIVE), _CMP_GT_OQ);
__mmask16 cond_1 = cond_1_left & cond_1_right;
pos_x_i_vec = _mm512_mask_blend_ps(cond_1, pos_x_i_vec, _mm512_set1_ps(1.0f));
@@ -389,7 +502,7 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__mmask16 cond_2_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(1.5f), _CMP_LT_OQ);
__mmask16 cond_2 = cond_2_left & cond_2_right;
pos_x_i_vec = _mm512_mask_blend_ps(cond_2, pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH - 1.0f));
pos_x_i_vec = _mm512_mask_blend_ps(cond_2, pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE));
destination_y_i_vec = _mm512_mask_blend_ps(cond_2, destination_y_i_vec, pos_y_i_vec);
__m512 neg_desired_v0 = _mm512_xor_ps(desired_v0_i_vec, _mm512_set1_ps(-0.0f));
w_x_i_vec = _mm512_mask_blend_ps(cond_2, w_x_i_vec, neg_desired_v0);
@@ -400,16 +513,30 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
pos_y_i_vec = _mm512_max_ps(pos_y_i_vec, _mm512_set1_ps(WALL_EPS));
pos_y_i_vec = _mm512_min_ps(pos_y_i_vec, _mm512_set1_ps(BOARD_WIDTH_MINUS_WALL_EPS));
// # 32 ADDs
__m512 ex_vec = _mm512_sub_ps(destination_x_i_vec, pos_x_i_vec);
__m512 ey_vec = _mm512_sub_ps(destination_y_i_vec, pos_y_i_vec);
// # 32 MULTs, 16 ADDs
__m512 e_norm_sq = _mm512_fmadd_ps(ex_vec, ex_vec, _mm512_mul_ps(ey_vec, ey_vec));
// # 16 SQRTs
__m512 e_norm = _mm512_sqrt_ps(e_norm_sq);
// # 16 DIVs
__m512 e_norm_inv = _mm512_div_ps(_mm512_set1_ps(1.0f), e_norm);
// # 32 MULTs
__m512 e_x_i_vec = _mm512_mul_ps(ex_vec, e_norm_inv);
__m512 e_y_i_vec = _mm512_mul_ps(ey_vec, e_norm_inv);
// # 32 MULTs, 16 ADDs
__m512 vel_norm_sq = _mm512_fmadd_ps(curr_vel_x_i_vec, curr_vel_x_i_vec, _mm512_mul_ps(curr_vel_y_i_vec, curr_vel_y_i_vec));
// # 16 MULTs
__m512 sb_sq_i_vec = _mm512_mul_ps(vel_norm_sq, _mm512_set1_ps(delta_t_sq));
// # 16 SQRTs
__m512 vel_norm_dt = _mm512_sqrt_ps(sb_sq_i_vec);
_mm512_storeu_ps(&curr_vel_x[i], curr_vel_x_i_vec);
@@ -422,6 +549,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
_mm512_storeu_ps(&e_y[i], e_y_i_vec);
_mm512_storeu_ps(&destination_y[i], destination_y_i_vec);
_mm512_storeu_ps(&sb_sq[i], sb_sq_i_vec);
// # 32 MULTs
_mm512_storeu_ps(&sb_e_x[i], _mm512_mul_ps(e_x_i_vec, vel_norm_dt));
_mm512_storeu_ps(&sb_e_y[i], _mm512_mul_ps(e_y_i_vec, vel_norm_dt));
}
@@ -13,6 +13,8 @@
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
@@ -160,21 +162,46 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// # 256 MULTs, 224 ADDs
static inline __m512 _avx512_exp_ps(__m512 x) {
const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
// # 16 MULTs
__m512 nf = _mm512_roundscale_ps(_mm512_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
const __m512 ln2 = _mm512_set1_ps(0.6931471805599453f);
// # 16 MULTs, 16 ADDs
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
const __m512 c5 = _mm512_set1_ps(1.0f / 120.0f);
const __m512 c4 = _mm512_set1_ps(1.0f / 24.0f);
const __m512 c3 = _mm512_set1_ps(1.0f / 6.0f);
const __m512 c2 = _mm512_set1_ps(0.5f);
const __m512 c1 = _mm512_set1_ps(1.0f);
const __m512 c0 = _mm512_set1_ps(1.0f);
const __m512 c13 = _mm512_set1_ps(c13_scalar);
const __m512 c12 = _mm512_set1_ps(c12_scalar);
const __m512 c11 = _mm512_set1_ps(c11_scalar);
const __m512 c10 = _mm512_set1_ps(c10_scalar);
const __m512 c9 = _mm512_set1_ps(c9_scalar);
const __m512 c8 = _mm512_set1_ps(c8_scalar);
const __m512 c7 = _mm512_set1_ps(c7_scalar);
const __m512 c6 = _mm512_set1_ps(c6_scalar);
const __m512 c5 = _mm512_set1_ps(c5_scalar);
const __m512 c4 = _mm512_set1_ps(c4_scalar);
const __m512 c3 = _mm512_set1_ps(c3_scalar);
const __m512 c2 = _mm512_set1_ps(c2_scalar);
const __m512 c1 = _mm512_set1_ps(c1_scalar);
const __m512 c0 = _mm512_set1_ps(c0_scalar);
__m512 p = c5;
__m512 p = c13;
// # 13 * (16 MULTs, 16 ADDs)
// # = 208 MULTs, 208 ADDs
p = _mm512_fmadd_ps(p, r, c12);
p = _mm512_fmadd_ps(p, r, c11);
p = _mm512_fmadd_ps(p, r, c10);
p = _mm512_fmadd_ps(p, r, c9);
p = _mm512_fmadd_ps(p, r, c8);
p = _mm512_fmadd_ps(p, r, c7);
p = _mm512_fmadd_ps(p, r, c6);
p = _mm512_fmadd_ps(p, r, c5);
p = _mm512_fmadd_ps(p, r, c4);
p = _mm512_fmadd_ps(p, r, c3);
p = _mm512_fmadd_ps(p, r, c2);
@@ -186,6 +213,7 @@ static inline __m512 _avx512_exp_ps(__m512 x) {
__m512i scaled = _mm512_slli_epi32(biased, 23);
__m512 pow2n = _mm512_castsi512_ps(scaled);
// # 16 MULTs
return _mm512_mul_ps(p, pow2n);
}
@@ -224,11 +252,42 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
const __m512 v_np = _mm512_set1_ps((float)n_p);
const __m512 v_exp_min = _mm512_set1_ps(-87.0f);
// # ADDs: n_iter * (
// # n_p * 68 +
// # n_p * n_p_pad * 29 +
// # n_p * board.n_obstacles * 10 +
// # n_p_pad * 7
// # )
// # MULTs: n_iter * (
// # n_p * 4 +
// # n_p * n_p_pad * 40 +
// # n_p * board.n_obstacles * 13 +
// # n_p_pad * 15
// # )
// # SQRTs: n_iter * (
// # n_p * n_p_pad * 3 +
// # n_p * board.n_obstacles +
// # n_p_pad * 3
// # )
// # DIVs: n_iter * (
// # n_p * n_p_pad +
// # n_p * board.n_obstacles +
// # n_p_pad * 2
// # )
// # EXPs: n_iter * n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
// # 2 MULTs
float desired_velocity_x = e_x[i] * desired_v0[i];
float desired_velocity_y = e_y[i] * desired_v0[i];
// # 2 ADDs, 2 MULTs
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
@@ -244,6 +303,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 acc_y_1 = _mm512_setzero_ps();
for (int j = 0; j < n_p_pad; j += 32) {
// # 32 ADDs
__m512 j_idx_0 = _mm512_add_ps(_mm512_set1_ps((float)j), lane_off);
__m512 j_idx_1 = _mm512_add_ps(_mm512_set1_ps((float)j + 16), lane_off);
@@ -259,11 +320,9 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 pjx_0 = _mm512_loadu_ps(&pos_x[j]);
__m512 pjx_1 = _mm512_loadu_ps(&pos_x[j + 16]);
__m512 pjy_0 = _mm512_loadu_ps(&pos_y[j]);
__m512 pjy_1 = _mm512_loadu_ps(&pos_y[j + 16]);
__m512 sbe_jx_0 = _mm512_loadu_ps(&sb_e_x[j]);
__m512 sbe_jx_1 = _mm512_loadu_ps(&sb_e_x[j + 16]);
@@ -273,81 +332,105 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 sb_jsq_0 = _mm512_loadu_ps(&sb_sq[j]);
__m512 sb_jsq_1 = _mm512_loadu_ps(&sb_sq[j + 16]);
// # 32 ADDs
__m512 r_ab_x_0 = _mm512_sub_ps(pix, pjx_0);
__m512 r_ab_x_1 = _mm512_sub_ps(pix, pjx_1);
// # 32 ADDs
__m512 r_ab_y_0 = _mm512_sub_ps(piy, pjy_0);
__m512 r_ab_y_1 = _mm512_sub_ps(piy, pjy_1);
// # 64 MULTs, 32 ADDs
__m512 r_ab_n2_0 = _mm512_fmadd_ps(r_ab_x_0, r_ab_x_0, _mm512_mul_ps(r_ab_y_0, r_ab_y_0));
__m512 r_ab_n2_1 = _mm512_fmadd_ps(r_ab_x_1, r_ab_x_1, _mm512_mul_ps(r_ab_y_1, r_ab_y_1));
// # 32 SQRTs
__m512 r_ab_n_0 = _mm512_sqrt_ps(r_ab_n2_0);
__m512 r_ab_n_1 = _mm512_sqrt_ps(r_ab_n2_1);
// # 32 ADDs
__m512 r_ab2_x_0 = _mm512_sub_ps(r_ab_x_0, sbe_jx_0);
__m512 r_ab2_x_1 = _mm512_sub_ps(r_ab_x_1, sbe_jx_1);
// # 32 ADDs
__m512 r_ab2_y_0 = _mm512_sub_ps(r_ab_y_0, sbe_jy_0);
__m512 r_ab2_y_1 = _mm512_sub_ps(r_ab_y_1, sbe_jy_1);
// # 64 MULTs, 32 ADDs
__m512 r_ab2_n2_0 = _mm512_fmadd_ps(r_ab2_x_0, r_ab2_x_0, _mm512_mul_ps(r_ab2_y_0, r_ab2_y_0));
__m512 r_ab2_n2_1 = _mm512_fmadd_ps(r_ab2_x_1, r_ab2_x_1, _mm512_mul_ps(r_ab2_y_1, r_ab2_y_1));
// # 32 SQRTs
__m512 r_ab2_n_0 = _mm512_sqrt_ps(r_ab2_n2_0);
__m512 r_ab2_n_1 = _mm512_sqrt_ps(r_ab2_n2_1);
// # 32 ADDs
__m512 r_sum_0 = _mm512_add_ps(r_ab_n_0, r_ab2_n_0);
__m512 r_sum_1 = _mm512_add_ps(r_ab_n_1, r_ab2_n_1);
// # 32 MULTs, 32 ADDs
__m512 two_b_sq_0 = _mm512_fmsub_ps(r_sum_0, r_sum_0, sb_jsq_0);
__m512 two_b_sq_1 = _mm512_fmsub_ps(r_sum_1, r_sum_1, sb_jsq_1);
// # 32 SQRTs
__m512 two_b_0 = _mm512_sqrt_ps(two_b_sq_0);
__m512 two_b_1 = _mm512_sqrt_ps(two_b_sq_1);
// # 32 MULTs
__m512 e_arg_0 = _mm512_mul_ps(two_b_0, v_neg_2s_inv);
__m512 e_arg_1 = _mm512_mul_ps(two_b_1, v_neg_2s_inv);
e_arg_0 = _mm512_max_ps(e_arg_0, v_exp_min);
e_arg_1 = _mm512_max_ps(e_arg_1, v_exp_min);
// # 512 MULTs, 448 ADDs
__m512 e_val_0 = _avx512_exp_ps(e_arg_0);
__m512 e_val_1 = _avx512_exp_ps(e_arg_1);
// # 64 MULTs
__m512 numer_0 = _mm512_mul_ps(_mm512_mul_ps(r_sum_0, v_neg_v0_div_2s), e_val_0);
__m512 numer_1 = _mm512_mul_ps(_mm512_mul_ps(r_sum_1, v_neg_v0_div_2s), e_val_1);
// # 64 MULTs
__m512 denom_0 = _mm512_mul_ps(_mm512_mul_ps(two_b_0, r_ab_n_0), r_ab2_n_0);
__m512 denom_1 = _mm512_mul_ps(_mm512_mul_ps(two_b_1, r_ab_n_1), r_ab2_n_1);
__m512 denom_safe_0 = _mm512_mask_blend_ps(active_0, v_one, denom_0);
__m512 denom_safe_1 = _mm512_mask_blend_ps(active_1, v_one, denom_1);
// # 32 DIVs
__m512 smc_0 = _mm512_div_ps(numer_0, denom_safe_0);
__m512 smc_1 = _mm512_div_ps(numer_1, denom_safe_1);
// # 32 MULTs
__m512 sm_r_ab_0 = _mm512_mul_ps(smc_0, r_ab2_n_0);
__m512 sm_r_ab_1 = _mm512_mul_ps(smc_1, r_ab2_n_1);
// # 32 MULTs
__m512 sm_r_ab2_0 = _mm512_mul_ps(smc_0, r_ab_n_0);
__m512 sm_r_ab2_1 = _mm512_mul_ps(smc_1, r_ab_n_1);
// # 64 MULTs, 32 ADDs
__m512 f_x_0 = _mm512_fmadd_ps(r_ab2_x_0, sm_r_ab2_0, _mm512_mul_ps(r_ab_x_0, sm_r_ab_0));
__m512 f_x_1 = _mm512_fmadd_ps(r_ab2_x_1, sm_r_ab2_1, _mm512_mul_ps(r_ab_x_1, sm_r_ab_1));
// # 64 MULTs, 32 ADDs
__m512 f_y_0 = _mm512_fmadd_ps(r_ab2_y_0, sm_r_ab2_0, _mm512_mul_ps(r_ab_y_0, sm_r_ab_0));
__m512 f_y_1 = _mm512_fmadd_ps(r_ab2_y_1, sm_r_ab2_1, _mm512_mul_ps(r_ab_y_1, sm_r_ab_1));
// # 64 MULTs, 32 ADDs
__m512 f_norm_sq_0 = _mm512_fmadd_ps(f_x_0, f_x_0, _mm512_mul_ps(f_y_0, f_y_0));
__m512 f_norm_sq_1 = _mm512_fmadd_ps(f_x_1, f_x_1, _mm512_mul_ps(f_y_1, f_y_1));
// # 32 MULTs
__m512 rhs_0 = _mm512_mul_ps(f_norm_sq_0, v_cos_phi_sq);
__m512 rhs_1 = _mm512_mul_ps(f_norm_sq_1, v_cos_phi_sq);
// # 64 MULTs, 32 ADDs
__m512 e_dot_f_0 = _mm512_fmadd_ps(eix, f_x_0, _mm512_mul_ps(eiy, f_y_0));
__m512 e_dot_f_1 = _mm512_fmadd_ps(eix, f_x_1, _mm512_mul_ps(eiy, f_y_1));
// # 32 MULTs
__m512 e_dot_f_sq_0 = _mm512_mul_ps(e_dot_f_0, e_dot_f_0);
__m512 e_dot_f_sq_1 = _mm512_mul_ps(e_dot_f_1, e_dot_f_1);
@@ -369,22 +452,28 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 f_y_safe_0 = _mm512_maskz_mov_ps(active_0, f_y_0);
__m512 f_y_safe_1 = _mm512_maskz_mov_ps(active_1, f_y_1);
// # 32 MULTs, 32 ADDs
acc_x_0 = _mm512_fmadd_ps(f_x_safe_0, w_val_0, acc_x_0);
acc_x_1 = _mm512_fmadd_ps(f_x_safe_1, w_val_1, acc_x_1);
// # 32 MULTs, 32 ADDs
acc_y_0 = _mm512_fmadd_ps(f_y_safe_0, w_val_0, acc_y_0);
acc_y_1 = _mm512_fmadd_ps(f_y_safe_1, w_val_1, acc_y_1);
}
// # 64 ADDs
f_alpha_dt_x += _mm512_reduce_add_ps(_mm512_add_ps(acc_x_0, acc_x_1));
f_alpha_dt_y += _mm512_reduce_add_ps(_mm512_add_ps(acc_y_0, acc_y_1));
for (int k = 0; k < board.n_obstacles; k++) {
// # 2 ADDs
float v1_x = pos_x[i] - board.obstacles[k].start_x;
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
// # 3 MULTs, 1 ADD
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
if (projection < 0.0f)
@@ -392,12 +481,15 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
else if (projection > 1.0f)
projection = 1.0f;
// # 4 ADDs, 2 MULTs
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
// # 1 SQRT, 2 MULTs, 1 ADD
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
// # 1 EXP, 4 MULTs, 1 DIV
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
@@ -408,19 +500,22 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
if (e_dot_f < cos_phi)
w_val = c_neg_times_dt;
// # 2 MULTs
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
// # 2 ADDs
f_alpha_dt_x += f_alpha_B_dt_x;
f_alpha_dt_y += f_alpha_B_dt_y;
}
// # 2 ADDs
w_x[i] += f_alpha_dt_x;
w_y[i] += f_alpha_dt_y;
}
for (int i = 0; i < n_p_pad; i += 16) {
__m512 w_x_i_vec = _mm512_loadu_ps(&w_x[i]);
__m512 w_y_i_vec = _mm512_loadu_ps(&w_y[i]);
__m512 v_max_i_vec = _mm512_loadu_ps(&v_max[i]);
@@ -429,22 +524,30 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 destination_y_i_vec = _mm512_loadu_ps(&destination_y[i]);
__m512 destination_x_i_vec = _mm512_loadu_ps(&destination_x[i]);
__m512 desired_v0_i_vec = _mm512_loadu_ps(&desired_v0[i]);
__m512i color_i_vec = _mm512_loadu_si512((__m512i*)&color[i]);
__m512i color_i_vec = _mm512_loadu_si512((__m512i *)&color[i]);
// # 32 MULTs, 16 ADDs
__m512 w_norm_sq = _mm512_fmadd_ps(w_x_i_vec, w_x_i_vec, _mm512_mul_ps(w_y_i_vec, w_y_i_vec));
// # 16 SQRTs
__m512 w_norm = _mm512_sqrt_ps(w_norm_sq);
// # 16 DIVs
__m512 correction_factor = _mm512_min_ps(_mm512_set1_ps(1.0f), _mm512_div_ps(v_max_i_vec, w_norm));
// # 32 MULTs
__m512 curr_vel_x_i_vec = _mm512_mul_ps(w_x_i_vec, correction_factor);
__m512 curr_vel_y_i_vec = _mm512_mul_ps(w_y_i_vec, correction_factor);
__m512 dt_vec = _mm512_set1_ps(dt);
// # 32 MULTs, 32 ADDs
pos_x_i_vec = _mm512_fmadd_ps(curr_vel_x_i_vec, dt_vec, pos_x_i_vec);
pos_y_i_vec = _mm512_fmadd_ps(curr_vel_y_i_vec, dt_vec, pos_y_i_vec);
__mmask16 cond_1_left = _mm512_cmpeq_epi32_mask(color_i_vec, _mm512_setzero_si512());
__mmask16 cond_1_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH - 1.5f), _CMP_GT_OQ);
__mmask16 cond_1_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE_POINT_FIVE), _CMP_GT_OQ);
__mmask16 cond_1 = cond_1_left & cond_1_right;
pos_x_i_vec = _mm512_mask_blend_ps(cond_1, pos_x_i_vec, _mm512_set1_ps(1.0f));
@@ -458,7 +561,7 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__mmask16 cond_2_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(1.5f), _CMP_LT_OQ);
__mmask16 cond_2 = cond_2_left & cond_2_right;
pos_x_i_vec = _mm512_mask_blend_ps(cond_2, pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH - 1.0f));
pos_x_i_vec = _mm512_mask_blend_ps(cond_2, pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE));
destination_y_i_vec = _mm512_mask_blend_ps(cond_2, destination_y_i_vec, pos_y_i_vec);
__m512 neg_desired_v0 = _mm512_xor_ps(desired_v0_i_vec, _mm512_set1_ps(-0.0f));
w_x_i_vec = _mm512_mask_blend_ps(cond_2, w_x_i_vec, neg_desired_v0);
@@ -469,16 +572,30 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
pos_y_i_vec = _mm512_max_ps(pos_y_i_vec, _mm512_set1_ps(WALL_EPS));
pos_y_i_vec = _mm512_min_ps(pos_y_i_vec, _mm512_set1_ps(BOARD_WIDTH_MINUS_WALL_EPS));
// # 32 ADDs
__m512 ex_vec = _mm512_sub_ps(destination_x_i_vec, pos_x_i_vec);
__m512 ey_vec = _mm512_sub_ps(destination_y_i_vec, pos_y_i_vec);
// # 32 MULTs, 16 ADDs
__m512 e_norm_sq = _mm512_fmadd_ps(ex_vec, ex_vec, _mm512_mul_ps(ey_vec, ey_vec));
// # 16 SQRTs
__m512 e_norm = _mm512_sqrt_ps(e_norm_sq);
// # 16 DIVs
__m512 e_norm_inv = _mm512_div_ps(_mm512_set1_ps(1.0f), e_norm);
// # 32 MULTs
__m512 e_x_i_vec = _mm512_mul_ps(ex_vec, e_norm_inv);
__m512 e_y_i_vec = _mm512_mul_ps(ey_vec, e_norm_inv);
// # 32 MULTs, 16 ADDs
__m512 vel_norm_sq = _mm512_fmadd_ps(curr_vel_x_i_vec, curr_vel_x_i_vec, _mm512_mul_ps(curr_vel_y_i_vec, curr_vel_y_i_vec));
// # 16 MULTs
__m512 sb_sq_i_vec = _mm512_mul_ps(vel_norm_sq, _mm512_set1_ps(delta_t_sq));
// # 16 SQRTs
__m512 vel_norm_dt = _mm512_sqrt_ps(sb_sq_i_vec);
_mm512_storeu_ps(&curr_vel_x[i], curr_vel_x_i_vec);
@@ -491,11 +608,12 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
_mm512_storeu_ps(&e_y[i], e_y_i_vec);
_mm512_storeu_ps(&destination_y[i], destination_y_i_vec);
_mm512_storeu_ps(&sb_sq[i], sb_sq_i_vec);
// # 32 MULTs
_mm512_storeu_ps(&sb_e_x[i], _mm512_mul_ps(e_x_i_vec, vel_norm_dt));
_mm512_storeu_ps(&sb_e_y[i], _mm512_mul_ps(e_y_i_vec, vel_norm_dt));
}
#if !BENCHMARK
for (int i = 0; i < n_p; i++) {
if (dump_path && (t % dump_interval == 0)) {
@@ -13,6 +13,8 @@
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
@@ -160,21 +162,46 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// # 256 MULTs, 224 ADDs
static inline __m512 _avx512_exp_ps(__m512 x) {
const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
// # 16 MULTs
__m512 nf = _mm512_roundscale_ps(_mm512_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
const __m512 ln2 = _mm512_set1_ps(0.6931471805599453f);
// # 16 MULTs, 16 ADDs
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
const __m512 c5 = _mm512_set1_ps(1.0f / 120.0f);
const __m512 c4 = _mm512_set1_ps(1.0f / 24.0f);
const __m512 c3 = _mm512_set1_ps(1.0f / 6.0f);
const __m512 c2 = _mm512_set1_ps(0.5f);
const __m512 c1 = _mm512_set1_ps(1.0f);
const __m512 c0 = _mm512_set1_ps(1.0f);
const __m512 c13 = _mm512_set1_ps(c13_scalar);
const __m512 c12 = _mm512_set1_ps(c12_scalar);
const __m512 c11 = _mm512_set1_ps(c11_scalar);
const __m512 c10 = _mm512_set1_ps(c10_scalar);
const __m512 c9 = _mm512_set1_ps(c9_scalar);
const __m512 c8 = _mm512_set1_ps(c8_scalar);
const __m512 c7 = _mm512_set1_ps(c7_scalar);
const __m512 c6 = _mm512_set1_ps(c6_scalar);
const __m512 c5 = _mm512_set1_ps(c5_scalar);
const __m512 c4 = _mm512_set1_ps(c4_scalar);
const __m512 c3 = _mm512_set1_ps(c3_scalar);
const __m512 c2 = _mm512_set1_ps(c2_scalar);
const __m512 c1 = _mm512_set1_ps(c1_scalar);
const __m512 c0 = _mm512_set1_ps(c0_scalar);
__m512 p = c5;
__m512 p = c13;
// # 13 * (16 MULTs, 16 ADDs)
// # = 208 MULTs, 208 ADDs
p = _mm512_fmadd_ps(p, r, c12);
p = _mm512_fmadd_ps(p, r, c11);
p = _mm512_fmadd_ps(p, r, c10);
p = _mm512_fmadd_ps(p, r, c9);
p = _mm512_fmadd_ps(p, r, c8);
p = _mm512_fmadd_ps(p, r, c7);
p = _mm512_fmadd_ps(p, r, c6);
p = _mm512_fmadd_ps(p, r, c5);
p = _mm512_fmadd_ps(p, r, c4);
p = _mm512_fmadd_ps(p, r, c3);
p = _mm512_fmadd_ps(p, r, c2);
@@ -186,6 +213,7 @@ static inline __m512 _avx512_exp_ps(__m512 x) {
__m512i scaled = _mm512_slli_epi32(biased, 23);
__m512 pow2n = _mm512_castsi512_ps(scaled);
// # 16 MULTs
return _mm512_mul_ps(p, pow2n);
}
@@ -224,11 +252,42 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
const __m512 v_np = _mm512_set1_ps((float)n_p);
const __m512 v_exp_min = _mm512_set1_ps(-87.0f);
// # ADDs: n_iter * (
// # n_p * 68 +
// # n_p * n_p_pad * 29 +
// # n_p * board.n_obstacles * 10 +
// # n_p_pad * 7
// # )
// # MULTs: n_iter * (
// # n_p * 4 +
// # n_p * n_p_pad * 40 +
// # n_p * board.n_obstacles * 13 +
// # n_p_pad * 15
// # )
// # DIVs: n_iter * (
// # n_p * n_p_pad +
// # n_p * board.n_obstacles +
// # n_p_pad * 2
// # )
// # SQRTs: n_iter * (
// # n_p * n_p_pad * 3 +
// # n_p * board.n_obstacles +
// # n_p_pad * 3
// # )
// # EXPs: n_iter * n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
// # 2 MULTs
float desired_velocity_x = e_x[i] * desired_v0[i];
float desired_velocity_y = e_y[i] * desired_v0[i];
// # 2 ADDs, 2 MULTs
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
@@ -244,6 +303,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 acc_y_1 = _mm512_setzero_ps();
for (int j = 0; j < n_p_pad; j += 32) {
// # 32 ADDs
__m512 j_idx_0 = _mm512_add_ps(_mm512_set1_ps((float)j), lane_off);
__m512 j_idx_1 = _mm512_add_ps(_mm512_set1_ps((float)j + 16), lane_off);
@@ -271,81 +332,105 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 sb_jsq_0 = _mm512_loadu_ps(&sb_sq[j]);
__m512 sb_jsq_1 = _mm512_loadu_ps(&sb_sq[j + 16]);
// # 32 ADDs
__m512 r_ab_x_0 = _mm512_sub_ps(pix, pjx_0);
__m512 r_ab_x_1 = _mm512_sub_ps(pix, pjx_1);
// # 32 ADDs
__m512 r_ab_y_0 = _mm512_sub_ps(piy, pjy_0);
__m512 r_ab_y_1 = _mm512_sub_ps(piy, pjy_1);
// # 64 MULTs, 32 ADDs
__m512 r_ab_n2_0 = _mm512_fmadd_ps(r_ab_x_0, r_ab_x_0, _mm512_mul_ps(r_ab_y_0, r_ab_y_0));
__m512 r_ab_n2_1 = _mm512_fmadd_ps(r_ab_x_1, r_ab_x_1, _mm512_mul_ps(r_ab_y_1, r_ab_y_1));
// # 32 SQRTs
__m512 r_ab_n_0 = _mm512_sqrt_ps(r_ab_n2_0);
__m512 r_ab_n_1 = _mm512_sqrt_ps(r_ab_n2_1);
// # 32 ADDs
__m512 r_ab2_x_0 = _mm512_sub_ps(r_ab_x_0, sbe_jx_0);
__m512 r_ab2_x_1 = _mm512_sub_ps(r_ab_x_1, sbe_jx_1);
// # 32 ADDs
__m512 r_ab2_y_0 = _mm512_sub_ps(r_ab_y_0, sbe_jy_0);
__m512 r_ab2_y_1 = _mm512_sub_ps(r_ab_y_1, sbe_jy_1);
// # 64 MULTs, 32 ADDs
__m512 r_ab2_n2_0 = _mm512_fmadd_ps(r_ab2_x_0, r_ab2_x_0, _mm512_mul_ps(r_ab2_y_0, r_ab2_y_0));
__m512 r_ab2_n2_1 = _mm512_fmadd_ps(r_ab2_x_1, r_ab2_x_1, _mm512_mul_ps(r_ab2_y_1, r_ab2_y_1));
// # 32 SQRTs
__m512 r_ab2_n_0 = _mm512_sqrt_ps(r_ab2_n2_0);
__m512 r_ab2_n_1 = _mm512_sqrt_ps(r_ab2_n2_1);
// # 32 ADDs
__m512 r_sum_0 = _mm512_add_ps(r_ab_n_0, r_ab2_n_0);
__m512 r_sum_1 = _mm512_add_ps(r_ab_n_1, r_ab2_n_1);
// # 32 MULTs, 32 ADDs
__m512 two_b_sq_0 = _mm512_fmsub_ps(r_sum_0, r_sum_0, sb_jsq_0);
__m512 two_b_sq_1 = _mm512_fmsub_ps(r_sum_1, r_sum_1, sb_jsq_1);
// # 32 SQRTs
__m512 two_b_0 = _mm512_sqrt_ps(two_b_sq_0);
__m512 two_b_1 = _mm512_sqrt_ps(two_b_sq_1);
// # 32 MULTs
__m512 e_arg_0 = _mm512_mul_ps(two_b_0, v_neg_2s_inv);
__m512 e_arg_1 = _mm512_mul_ps(two_b_1, v_neg_2s_inv);
e_arg_0 = _mm512_max_ps(e_arg_0, v_exp_min);
e_arg_1 = _mm512_max_ps(e_arg_1, v_exp_min);
// # 512 MULTs, 448 ADDs
__m512 e_val_0 = _avx512_exp_ps(e_arg_0);
__m512 e_val_1 = _avx512_exp_ps(e_arg_1);
// # 64 MULTs
__m512 numer_0 = _mm512_mul_ps(_mm512_mul_ps(r_sum_0, v_neg_v0_div_2s), e_val_0);
__m512 numer_1 = _mm512_mul_ps(_mm512_mul_ps(r_sum_1, v_neg_v0_div_2s), e_val_1);
// # 64 MULTs
__m512 denom_0 = _mm512_mul_ps(_mm512_mul_ps(two_b_0, r_ab_n_0), r_ab2_n_0);
__m512 denom_1 = _mm512_mul_ps(_mm512_mul_ps(two_b_1, r_ab_n_1), r_ab2_n_1);
__m512 denom_safe_0 = _mm512_mask_blend_ps(active_0, v_one, denom_0);
__m512 denom_safe_1 = _mm512_mask_blend_ps(active_1, v_one, denom_1);
// # 32 DIVs
__m512 smc_0 = _mm512_div_ps(numer_0, denom_safe_0);
__m512 smc_1 = _mm512_div_ps(numer_1, denom_safe_1);
// # 32 MULTs
__m512 sm_r_ab_0 = _mm512_mul_ps(smc_0, r_ab2_n_0);
__m512 sm_r_ab_1 = _mm512_mul_ps(smc_1, r_ab2_n_1);
// # 32 MULTs
__m512 sm_r_ab2_0 = _mm512_mul_ps(smc_0, r_ab_n_0);
__m512 sm_r_ab2_1 = _mm512_mul_ps(smc_1, r_ab_n_1);
// # 64 MULTs, 32 ADDs
__m512 f_x_0 = _mm512_fmadd_ps(r_ab2_x_0, sm_r_ab2_0, _mm512_mul_ps(r_ab_x_0, sm_r_ab_0));
__m512 f_x_1 = _mm512_fmadd_ps(r_ab2_x_1, sm_r_ab2_1, _mm512_mul_ps(r_ab_x_1, sm_r_ab_1));
// # 64 MULTs, 32 ADDs
__m512 f_y_0 = _mm512_fmadd_ps(r_ab2_y_0, sm_r_ab2_0, _mm512_mul_ps(r_ab_y_0, sm_r_ab_0));
__m512 f_y_1 = _mm512_fmadd_ps(r_ab2_y_1, sm_r_ab2_1, _mm512_mul_ps(r_ab_y_1, sm_r_ab_1));
// # 64 MULTs, 32 ADDs
__m512 f_norm_sq_0 = _mm512_fmadd_ps(f_x_0, f_x_0, _mm512_mul_ps(f_y_0, f_y_0));
__m512 f_norm_sq_1 = _mm512_fmadd_ps(f_x_1, f_x_1, _mm512_mul_ps(f_y_1, f_y_1));
// # 32 MULTs
__m512 rhs_0 = _mm512_mul_ps(f_norm_sq_0, v_cos_phi_sq);
__m512 rhs_1 = _mm512_mul_ps(f_norm_sq_1, v_cos_phi_sq);
// # 64 MULTs, 32 ADDs
__m512 e_dot_f_0 = _mm512_fmadd_ps(eix, f_x_0, _mm512_mul_ps(eiy, f_y_0));
__m512 e_dot_f_1 = _mm512_fmadd_ps(eix, f_x_1, _mm512_mul_ps(eiy, f_y_1));
// # 32 MULTs
__m512 e_dot_f_sq_0 = _mm512_mul_ps(e_dot_f_0, e_dot_f_0);
__m512 e_dot_f_sq_1 = _mm512_mul_ps(e_dot_f_1, e_dot_f_1);
@@ -367,22 +452,28 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 f_y_safe_0 = _mm512_maskz_mov_ps(active_0, f_y_0);
__m512 f_y_safe_1 = _mm512_maskz_mov_ps(active_1, f_y_1);
// # 32 MULTs, 32 ADDs
acc_x_0 = _mm512_fmadd_ps(f_x_safe_0, w_val_0, acc_x_0);
acc_x_1 = _mm512_fmadd_ps(f_x_safe_1, w_val_1, acc_x_1);
// # 32 MULTs, 32 ADDs
acc_y_0 = _mm512_fmadd_ps(f_y_safe_0, w_val_0, acc_y_0);
acc_y_1 = _mm512_fmadd_ps(f_y_safe_1, w_val_1, acc_y_1);
}
// # 64 ADDs
f_alpha_dt_x += _mm512_reduce_add_ps(_mm512_add_ps(acc_x_0, acc_x_1));
f_alpha_dt_y += _mm512_reduce_add_ps(_mm512_add_ps(acc_y_0, acc_y_1));
for (int k = 0; k < board.n_obstacles; k++) {
// # 2 ADDs
float v1_x = pos_x[i] - board.obstacles[k].start_x;
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
// # 3 MULTs, 1 ADD
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
if (projection < 0.0f)
@@ -390,12 +481,15 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
else if (projection > 1.0f)
projection = 1.0f;
// # 4 ADDs, 2 MULTs
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
// # 1 SQRT, 2 MULTs, 1 ADD
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
// # 4 MULTs, 1 EXP, 1 DIV
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
@@ -406,13 +500,16 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
if (e_dot_f < cos_phi)
w_val = c_neg_times_dt;
// # 2 MULTS
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
// # 2 ADDs
f_alpha_dt_x += f_alpha_B_dt_x;
f_alpha_dt_y += f_alpha_B_dt_y;
}
// # 2 ADDs
w_x[i] += f_alpha_dt_x;
w_y[i] += f_alpha_dt_y;
}
@@ -446,34 +543,41 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512i color_i_vec_0 = _mm512_loadu_si512((__m512i *)&color[i]);
__m512i color_i_vec_1 = _mm512_loadu_si512((__m512i *)&color[i + 16]);
// # 64 MULTs, 32 ADDs
__m512 w_norm_sq_0 = _mm512_fmadd_ps(w_x_i_vec_0, w_x_i_vec_0, _mm512_mul_ps(w_y_i_vec_0, w_y_i_vec_0));
__m512 w_norm_sq_1 = _mm512_fmadd_ps(w_x_i_vec_1, w_x_i_vec_1, _mm512_mul_ps(w_y_i_vec_1, w_y_i_vec_1));
// # 32 SQRTs
__m512 w_norm_0 = _mm512_sqrt_ps(w_norm_sq_0);
__m512 w_norm_1 = _mm512_sqrt_ps(w_norm_sq_1);
// # 32 DIVs
__m512 correction_factor_0 = _mm512_min_ps(_mm512_set1_ps(1.0f), _mm512_div_ps(v_max_i_vec_0, w_norm_0));
__m512 correction_factor_1 = _mm512_min_ps(_mm512_set1_ps(1.0f), _mm512_div_ps(v_max_i_vec_1, w_norm_1));
// # 32 MULTs
__m512 curr_vel_x_i_vec_0 = _mm512_mul_ps(w_x_i_vec_0, correction_factor_0);
__m512 curr_vel_x_i_vec_1 = _mm512_mul_ps(w_x_i_vec_1, correction_factor_1);
// # 32 MULTs
__m512 curr_vel_y_i_vec_0 = _mm512_mul_ps(w_y_i_vec_0, correction_factor_0);
__m512 curr_vel_y_i_vec_1 = _mm512_mul_ps(w_y_i_vec_1, correction_factor_1);
__m512 dt_vec = _mm512_set1_ps(dt);
// # 32 MULTs, 32 ADDs
pos_x_i_vec_0 = _mm512_fmadd_ps(curr_vel_x_i_vec_0, dt_vec, pos_x_i_vec_0);
pos_x_i_vec_1 = _mm512_fmadd_ps(curr_vel_x_i_vec_1, dt_vec, pos_x_i_vec_1);
// # 32 MULTs, 32 ADDs
pos_y_i_vec_0 = _mm512_fmadd_ps(curr_vel_y_i_vec_0, dt_vec, pos_y_i_vec_0);
pos_y_i_vec_1 = _mm512_fmadd_ps(curr_vel_y_i_vec_1, dt_vec, pos_y_i_vec_1);
__mmask16 cond_1_left_0 = _mm512_cmpeq_epi32_mask(color_i_vec_0, _mm512_setzero_si512());
__mmask16 cond_1_left_1 = _mm512_cmpeq_epi32_mask(color_i_vec_1, _mm512_setzero_si512());
__mmask16 cond_1_right_0 = _mm512_cmp_ps_mask(pos_x_i_vec_0, _mm512_set1_ps(BOARD_LENGTH - 1.5f), _CMP_GT_OQ);
__mmask16 cond_1_right_1 = _mm512_cmp_ps_mask(pos_x_i_vec_1, _mm512_set1_ps(BOARD_LENGTH - 1.5f), _CMP_GT_OQ);
__mmask16 cond_1_right_0 = _mm512_cmp_ps_mask(pos_x_i_vec_0, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE_POINT_FIVE), _CMP_GT_OQ);
__mmask16 cond_1_right_1 = _mm512_cmp_ps_mask(pos_x_i_vec_1, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE_POINT_FIVE), _CMP_GT_OQ);
__mmask16 cond_1_0 = cond_1_left_0 & cond_1_right_0;
__mmask16 cond_1_1 = cond_1_left_1 & cond_1_right_1;
@@ -529,33 +633,43 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
pos_y_i_vec_0 = _mm512_min_ps(pos_y_i_vec_0, _mm512_set1_ps(BOARD_WIDTH_MINUS_WALL_EPS));
pos_y_i_vec_1 = _mm512_min_ps(pos_y_i_vec_1, _mm512_set1_ps(BOARD_WIDTH_MINUS_WALL_EPS));
// # 32 ADDs
__m512 ex_vec_0 = _mm512_sub_ps(destination_x_i_vec_0, pos_x_i_vec_0);
__m512 ex_vec_1 = _mm512_sub_ps(destination_x_i_vec_1, pos_x_i_vec_1);
// # 32 ADDs
__m512 ey_vec_0 = _mm512_sub_ps(destination_y_i_vec_0, pos_y_i_vec_0);
__m512 ey_vec_1 = _mm512_sub_ps(destination_y_i_vec_1, pos_y_i_vec_1);
// # 64 MULTs, 32 ADDs
__m512 e_norm_sq_0 = _mm512_fmadd_ps(ex_vec_0, ex_vec_0, _mm512_mul_ps(ey_vec_0, ey_vec_0));
__m512 e_norm_sq_1 = _mm512_fmadd_ps(ex_vec_1, ex_vec_1, _mm512_mul_ps(ey_vec_1, ey_vec_1));
// # 32 SQRTs
__m512 e_norm_0 = _mm512_sqrt_ps(e_norm_sq_0);
__m512 e_norm_1 = _mm512_sqrt_ps(e_norm_sq_1);
// # 32 DIVs
__m512 e_norm_inv_0 = _mm512_div_ps(_mm512_set1_ps(1.0f), e_norm_0);
__m512 e_norm_inv_1 = _mm512_div_ps(_mm512_set1_ps(1.0f), e_norm_1);
// # 32 MULTs
__m512 e_x_i_vec_0 = _mm512_mul_ps(ex_vec_0, e_norm_inv_0);
__m512 e_x_i_vec_1 = _mm512_mul_ps(ex_vec_1, e_norm_inv_1);
// # 32 MULTs
__m512 e_y_i_vec_0 = _mm512_mul_ps(ey_vec_0, e_norm_inv_0);
__m512 e_y_i_vec_1 = _mm512_mul_ps(ey_vec_1, e_norm_inv_1);
// # 64 MULTs, 32 ADDs
__m512 vel_norm_sq_0 = _mm512_fmadd_ps(curr_vel_x_i_vec_0, curr_vel_x_i_vec_0, _mm512_mul_ps(curr_vel_y_i_vec_0, curr_vel_y_i_vec_0));
__m512 vel_norm_sq_1 = _mm512_fmadd_ps(curr_vel_x_i_vec_1, curr_vel_x_i_vec_1, _mm512_mul_ps(curr_vel_y_i_vec_1, curr_vel_y_i_vec_1));
// # 32 MULTs
__m512 sb_sq_i_vec_0 = _mm512_mul_ps(vel_norm_sq_0, _mm512_set1_ps(delta_t_sq));
__m512 sb_sq_i_vec_1 = _mm512_mul_ps(vel_norm_sq_1, _mm512_set1_ps(delta_t_sq));
// # 32 SQRTs
__m512 vel_norm_dt_0 = _mm512_sqrt_ps(sb_sq_i_vec_0);
__m512 vel_norm_dt_1 = _mm512_sqrt_ps(sb_sq_i_vec_1);
@@ -589,9 +703,11 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
_mm512_storeu_ps(&sb_sq[i], sb_sq_i_vec_0);
_mm512_storeu_ps(&sb_sq[i + 16], sb_sq_i_vec_1);
// # 32 MULTs
_mm512_storeu_ps(&sb_e_x[i], _mm512_mul_ps(e_x_i_vec_0, vel_norm_dt_0));
_mm512_storeu_ps(&sb_e_x[i + 16], _mm512_mul_ps(e_x_i_vec_1, vel_norm_dt_1));
// # 32 MULTs
_mm512_storeu_ps(&sb_e_y[i], _mm512_mul_ps(e_y_i_vec_0, vel_norm_dt_0));
_mm512_storeu_ps(&sb_e_y[i + 16], _mm512_mul_ps(e_y_i_vec_1, vel_norm_dt_1));
}
@@ -13,6 +13,8 @@
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
@@ -160,21 +162,46 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// # 256 MULTs, 224 ADDs
static inline __m512 _avx512_exp_ps(__m512 x) {
const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
// # 16 MULTs
__m512 nf = _mm512_roundscale_ps(_mm512_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
const __m512 ln2 = _mm512_set1_ps(0.6931471805599453f);
// # 16 MULTs, 16 ADDs
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
const __m512 c5 = _mm512_set1_ps(1.0f / 120.0f);
const __m512 c4 = _mm512_set1_ps(1.0f / 24.0f);
const __m512 c3 = _mm512_set1_ps(1.0f / 6.0f);
const __m512 c2 = _mm512_set1_ps(0.5f);
const __m512 c1 = _mm512_set1_ps(1.0f);
const __m512 c0 = _mm512_set1_ps(1.0f);
const __m512 c13 = _mm512_set1_ps(c13_scalar);
const __m512 c12 = _mm512_set1_ps(c12_scalar);
const __m512 c11 = _mm512_set1_ps(c11_scalar);
const __m512 c10 = _mm512_set1_ps(c10_scalar);
const __m512 c9 = _mm512_set1_ps(c9_scalar);
const __m512 c8 = _mm512_set1_ps(c8_scalar);
const __m512 c7 = _mm512_set1_ps(c7_scalar);
const __m512 c6 = _mm512_set1_ps(c6_scalar);
const __m512 c5 = _mm512_set1_ps(c5_scalar);
const __m512 c4 = _mm512_set1_ps(c4_scalar);
const __m512 c3 = _mm512_set1_ps(c3_scalar);
const __m512 c2 = _mm512_set1_ps(c2_scalar);
const __m512 c1 = _mm512_set1_ps(c1_scalar);
const __m512 c0 = _mm512_set1_ps(c0_scalar);
__m512 p = c5;
__m512 p = c13;
// # 13 * (16 MULTs, 16 ADDs)
// # = 208 MULTs, 208 ADDs
p = _mm512_fmadd_ps(p, r, c12);
p = _mm512_fmadd_ps(p, r, c11);
p = _mm512_fmadd_ps(p, r, c10);
p = _mm512_fmadd_ps(p, r, c9);
p = _mm512_fmadd_ps(p, r, c8);
p = _mm512_fmadd_ps(p, r, c7);
p = _mm512_fmadd_ps(p, r, c6);
p = _mm512_fmadd_ps(p, r, c5);
p = _mm512_fmadd_ps(p, r, c4);
p = _mm512_fmadd_ps(p, r, c3);
p = _mm512_fmadd_ps(p, r, c2);
@@ -186,6 +213,7 @@ static inline __m512 _avx512_exp_ps(__m512 x) {
__m512i scaled = _mm512_slli_epi32(biased, 23);
__m512 pow2n = _mm512_castsi512_ps(scaled);
// # 16 MULTs
return _mm512_mul_ps(p, pow2n);
}
@@ -224,11 +252,42 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
const __m512 v_np = _mm512_set1_ps((float)n_p);
const __m512 v_exp_min = _mm512_set1_ps(-87.0f);
// # ADDs: n_iter * (
// # n_p * 68 +
// # n_p * n_p_pad * 29
// # n_p * board.n_obstacles * 10
// # n_p_pad * 9
// # )
// # MULTs: n_iter * (
// # n_p * 4 +
// # n_p * n_p_pad * 40
// # n_p * board.n_obstacles * 13
// # n_p_pad * 17
// # )
// # DIVs: n_iter * (
// # n_p * n_p_pad
// # n_p * board.n_obstacles
// # n_p_pad * 2
// # )
// # SQRTs: n_iter * (
// # n_p * n_p_pad * 3
// # n_p * board.n_obstacles
// # n_p_pad * 3
// # )
// # EXPs: n_iter * n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
// # 2 MULTs
float desired_velocity_x = e_x[i] * desired_v0[i];
float desired_velocity_y = e_y[i] * desired_v0[i];
// # 2 ADDs, 2 MULTs
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
@@ -244,6 +303,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 acc_y_1 = _mm512_setzero_ps();
for (int j = 0; j < n_p_pad; j += 32) {
// # 32 ADDs
__m512 j_idx_0 = _mm512_add_ps(_mm512_set1_ps((float)j), lane_off);
__m512 j_idx_1 = _mm512_add_ps(_mm512_set1_ps((float)j + 16), lane_off);
@@ -271,81 +332,105 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 sb_jsq_0 = _mm512_loadu_ps(&sb_sq[j]);
__m512 sb_jsq_1 = _mm512_loadu_ps(&sb_sq[j + 16]);
// # 32 ADDs
__m512 r_ab_x_0 = _mm512_sub_ps(pix, pjx_0);
__m512 r_ab_x_1 = _mm512_sub_ps(pix, pjx_1);
// # 32 ADDs
__m512 r_ab_y_0 = _mm512_sub_ps(piy, pjy_0);
__m512 r_ab_y_1 = _mm512_sub_ps(piy, pjy_1);
// # 64 MULTs, 32 ADDs
__m512 r_ab_n2_0 = _mm512_fmadd_ps(r_ab_x_0, r_ab_x_0, _mm512_mul_ps(r_ab_y_0, r_ab_y_0));
__m512 r_ab_n2_1 = _mm512_fmadd_ps(r_ab_x_1, r_ab_x_1, _mm512_mul_ps(r_ab_y_1, r_ab_y_1));
// # 32 SQRTs
__m512 r_ab_n_0 = _mm512_sqrt_ps(r_ab_n2_0);
__m512 r_ab_n_1 = _mm512_sqrt_ps(r_ab_n2_1);
// # 32 ADDs
__m512 r_ab2_x_0 = _mm512_sub_ps(r_ab_x_0, sbe_jx_0);
__m512 r_ab2_x_1 = _mm512_sub_ps(r_ab_x_1, sbe_jx_1);
// # 32 ADDs
__m512 r_ab2_y_0 = _mm512_sub_ps(r_ab_y_0, sbe_jy_0);
__m512 r_ab2_y_1 = _mm512_sub_ps(r_ab_y_1, sbe_jy_1);
// # 64 MULTs, 32 ADDs
__m512 r_ab2_n2_0 = _mm512_fmadd_ps(r_ab2_x_0, r_ab2_x_0, _mm512_mul_ps(r_ab2_y_0, r_ab2_y_0));
__m512 r_ab2_n2_1 = _mm512_fmadd_ps(r_ab2_x_1, r_ab2_x_1, _mm512_mul_ps(r_ab2_y_1, r_ab2_y_1));
// # 32 SQRTs
__m512 r_ab2_n_0 = _mm512_sqrt_ps(r_ab2_n2_0);
__m512 r_ab2_n_1 = _mm512_sqrt_ps(r_ab2_n2_1);
// # 32 ADDs
__m512 r_sum_0 = _mm512_add_ps(r_ab_n_0, r_ab2_n_0);
__m512 r_sum_1 = _mm512_add_ps(r_ab_n_1, r_ab2_n_1);
// # 32 MULTs, 32 ADDs
__m512 two_b_sq_0 = _mm512_fmsub_ps(r_sum_0, r_sum_0, sb_jsq_0);
__m512 two_b_sq_1 = _mm512_fmsub_ps(r_sum_1, r_sum_1, sb_jsq_1);
// # 32 SQRTs
__m512 two_b_0 = _mm512_sqrt_ps(two_b_sq_0);
__m512 two_b_1 = _mm512_sqrt_ps(two_b_sq_1);
// # 32 MULTs
__m512 e_arg_0 = _mm512_mul_ps(two_b_0, v_neg_2s_inv);
__m512 e_arg_1 = _mm512_mul_ps(two_b_1, v_neg_2s_inv);
e_arg_0 = _mm512_max_ps(e_arg_0, v_exp_min);
e_arg_1 = _mm512_max_ps(e_arg_1, v_exp_min);
// # 512 MULTs, 448 ADDs
__m512 e_val_0 = _avx512_exp_ps(e_arg_0);
__m512 e_val_1 = _avx512_exp_ps(e_arg_1);
// # 64 MULTs
__m512 numer_0 = _mm512_mul_ps(_mm512_mul_ps(r_sum_0, v_neg_v0_div_2s), e_val_0);
__m512 numer_1 = _mm512_mul_ps(_mm512_mul_ps(r_sum_1, v_neg_v0_div_2s), e_val_1);
// # 64 MULTs
__m512 denom_0 = _mm512_mul_ps(_mm512_mul_ps(two_b_0, r_ab_n_0), r_ab2_n_0);
__m512 denom_1 = _mm512_mul_ps(_mm512_mul_ps(two_b_1, r_ab_n_1), r_ab2_n_1);
__m512 denom_safe_0 = _mm512_mask_blend_ps(active_0, v_one, denom_0);
__m512 denom_safe_1 = _mm512_mask_blend_ps(active_1, v_one, denom_1);
// # 32 DIVs
__m512 smc_0 = _mm512_div_ps(numer_0, denom_safe_0);
__m512 smc_1 = _mm512_div_ps(numer_1, denom_safe_1);
// # 32 MULTs
__m512 sm_r_ab_0 = _mm512_mul_ps(smc_0, r_ab2_n_0);
__m512 sm_r_ab_1 = _mm512_mul_ps(smc_1, r_ab2_n_1);
// # 32 MULTs
__m512 sm_r_ab2_0 = _mm512_mul_ps(smc_0, r_ab_n_0);
__m512 sm_r_ab2_1 = _mm512_mul_ps(smc_1, r_ab_n_1);
// # 64 MULTs, 32 ADDs
__m512 f_x_0 = _mm512_fmadd_ps(r_ab2_x_0, sm_r_ab2_0, _mm512_mul_ps(r_ab_x_0, sm_r_ab_0));
__m512 f_x_1 = _mm512_fmadd_ps(r_ab2_x_1, sm_r_ab2_1, _mm512_mul_ps(r_ab_x_1, sm_r_ab_1));
// # 64 MULTs, 32 ADDs
__m512 f_y_0 = _mm512_fmadd_ps(r_ab2_y_0, sm_r_ab2_0, _mm512_mul_ps(r_ab_y_0, sm_r_ab_0));
__m512 f_y_1 = _mm512_fmadd_ps(r_ab2_y_1, sm_r_ab2_1, _mm512_mul_ps(r_ab_y_1, sm_r_ab_1));
// # 64 MULTs, 32 ADDs
__m512 f_norm_sq_0 = _mm512_fmadd_ps(f_x_0, f_x_0, _mm512_mul_ps(f_y_0, f_y_0));
__m512 f_norm_sq_1 = _mm512_fmadd_ps(f_x_1, f_x_1, _mm512_mul_ps(f_y_1, f_y_1));
// # 32 MULTs
__m512 rhs_0 = _mm512_mul_ps(f_norm_sq_0, v_cos_phi_sq);
__m512 rhs_1 = _mm512_mul_ps(f_norm_sq_1, v_cos_phi_sq);
// # 64 MULTs, 32 ADDs
__m512 e_dot_f_0 = _mm512_fmadd_ps(eix, f_x_0, _mm512_mul_ps(eiy, f_y_0));
__m512 e_dot_f_1 = _mm512_fmadd_ps(eix, f_x_1, _mm512_mul_ps(eiy, f_y_1));
// # 32 MULTs
__m512 e_dot_f_sq_0 = _mm512_mul_ps(e_dot_f_0, e_dot_f_0);
__m512 e_dot_f_sq_1 = _mm512_mul_ps(e_dot_f_1, e_dot_f_1);
@@ -367,22 +452,28 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 f_y_safe_0 = _mm512_maskz_mov_ps(active_0, f_y_0);
__m512 f_y_safe_1 = _mm512_maskz_mov_ps(active_1, f_y_1);
// # 32 MULTs, 32 ADDs
acc_x_0 = _mm512_fmadd_ps(f_x_safe_0, w_val_0, acc_x_0);
acc_x_1 = _mm512_fmadd_ps(f_x_safe_1, w_val_1, acc_x_1);
// # 32 MULTs, 32 ADDs
acc_y_0 = _mm512_fmadd_ps(f_y_safe_0, w_val_0, acc_y_0);
acc_y_1 = _mm512_fmadd_ps(f_y_safe_1, w_val_1, acc_y_1);
}
// # 64 ADDs
f_alpha_dt_x += _mm512_reduce_add_ps(_mm512_add_ps(acc_x_0, acc_x_1));
f_alpha_dt_y += _mm512_reduce_add_ps(_mm512_add_ps(acc_y_0, acc_y_1));
for (int k = 0; k < board.n_obstacles; k++) {
// # 2 ADDs
float v1_x = pos_x[i] - board.obstacles[k].start_x;
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
// # 3 MULTs, 1 ADD
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
if (projection < 0.0f)
@@ -390,12 +481,15 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
else if (projection > 1.0f)
projection = 1.0f;
// # 4 ADDs, 2 MULTs
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
// # 1 SQRT, 2 MULTs, 1 ADD
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
// # 4 MULTs, 1 EXP, 1 DIV
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
@@ -406,13 +500,16 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
if (e_dot_f < cos_phi)
w_val = c_neg_times_dt;
// # 2 MULTS
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
// # 2 ADDs
f_alpha_dt_x += f_alpha_B_dt_x;
f_alpha_dt_y += f_alpha_B_dt_y;
}
// # 2 ADDs
w_x[i] += f_alpha_dt_x;
w_y[i] += f_alpha_dt_y;
}
@@ -430,19 +527,27 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512i color_i_vec = _mm512_loadu_si512((__m512i *)&color[i]);
// # 32 MULTs, 16 ADDs
__m512 w_norm_sq = _mm512_fmadd_ps(w_x_i_vec, w_x_i_vec, _mm512_mul_ps(w_y_i_vec, w_y_i_vec));
// # 16 SQRTs, 16 DIVs
__m512 w_norm_inv = _mm512_rsqrt14_ps(w_norm_sq);
// # 16 MULTs
__m512 correction_factor = _mm512_min_ps(_mm512_set1_ps(1.0f), _mm512_mul_ps(v_max_i_vec, w_norm_inv));
// # 32 MULTs
__m512 curr_vel_x_i_vec = _mm512_mul_ps(w_x_i_vec, correction_factor);
__m512 curr_vel_y_i_vec = _mm512_mul_ps(w_y_i_vec, correction_factor);
__m512 dt_vec = _mm512_set1_ps(dt);
// # 32 MULTs, 32 ADDs
pos_x_i_vec = _mm512_fmadd_ps(curr_vel_x_i_vec, dt_vec, pos_x_i_vec);
pos_y_i_vec = _mm512_fmadd_ps(curr_vel_y_i_vec, dt_vec, pos_y_i_vec);
__mmask16 cond_1_left = _mm512_cmpeq_epi32_mask(color_i_vec, _mm512_setzero_si512());
__mmask16 cond_1_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH - 1.5f), _CMP_GT_OQ);
__mmask16 cond_1_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE_POINT_FIVE), _CMP_GT_OQ);
__mmask16 cond_1 = cond_1_left & cond_1_right;
pos_x_i_vec = _mm512_mask_blend_ps(cond_1, pos_x_i_vec, _mm512_set1_ps(1.0f));
@@ -456,7 +561,7 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__mmask16 cond_2_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(1.5f), _CMP_LT_OQ);
__mmask16 cond_2 = cond_2_left & cond_2_right;
pos_x_i_vec = _mm512_mask_blend_ps(cond_2, pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH - 1.0f));
pos_x_i_vec = _mm512_mask_blend_ps(cond_2, pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE));
destination_y_i_vec = _mm512_mask_blend_ps(cond_2, destination_y_i_vec, pos_y_i_vec);
__m512 neg_desired_v0 = _mm512_xor_ps(desired_v0_i_vec, _mm512_set1_ps(-0.0f));
w_x_i_vec = _mm512_mask_blend_ps(cond_2, w_x_i_vec, neg_desired_v0);
@@ -467,15 +572,27 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
pos_y_i_vec = _mm512_max_ps(pos_y_i_vec, _mm512_set1_ps(WALL_EPS));
pos_y_i_vec = _mm512_min_ps(pos_y_i_vec, _mm512_set1_ps(BOARD_WIDTH_MINUS_WALL_EPS));
// # 32 ADDs
__m512 ex_vec = _mm512_sub_ps(destination_x_i_vec, pos_x_i_vec);
__m512 ey_vec = _mm512_sub_ps(destination_y_i_vec, pos_y_i_vec);
// # 32 MULTs, 16 ADDs
__m512 e_norm_sq = _mm512_fmadd_ps(ex_vec, ex_vec, _mm512_mul_ps(ey_vec, ey_vec));
// # 16 DIVs, 16 SQRTs
__m512 e_norm_inv = _mm512_rsqrt14_ps(e_norm_sq);
// # 32 MULTs
__m512 e_x_i_vec = _mm512_mul_ps(ex_vec, e_norm_inv);
__m512 e_y_i_vec = _mm512_mul_ps(ey_vec, e_norm_inv);
// # 32 MULTs, 16 ADDs
__m512 vel_norm_sq = _mm512_fmadd_ps(curr_vel_x_i_vec, curr_vel_x_i_vec, _mm512_mul_ps(curr_vel_y_i_vec, curr_vel_y_i_vec));
// # 16 MULTs
__m512 sb_sq_i_vec = _mm512_mul_ps(vel_norm_sq, _mm512_set1_ps(delta_t_sq));
// # 16 SQRTs
__m512 vel_norm_dt = _mm512_sqrt_ps(sb_sq_i_vec);
_mm512_storeu_ps(&curr_vel_x[i], curr_vel_x_i_vec);
@@ -488,6 +605,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
_mm512_storeu_ps(&e_y[i], e_y_i_vec);
_mm512_storeu_ps(&destination_y[i], destination_y_i_vec);
_mm512_storeu_ps(&sb_sq[i], sb_sq_i_vec);
// # 32 MULTs
_mm512_storeu_ps(&sb_e_x[i], _mm512_mul_ps(e_x_i_vec, vel_norm_dt));
_mm512_storeu_ps(&sb_e_y[i], _mm512_mul_ps(e_y_i_vec, vel_norm_dt));
}
@@ -13,6 +13,8 @@
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
@@ -160,41 +162,58 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// # 256 MULTs, 224 ADDs
static inline __m512 _avx512_exp_ps(__m512 x) {
// define own inlined intrinsic function for approximating the exponent function
// e^x = 2^n * e^r --> x = n * ln(2) + r
const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
// # 16 MULTs
__m512 nf = _mm512_roundscale_ps(_mm512_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
const __m512 ln2 = _mm512_set1_ps(0.6931471805599453f);
// r = x - n * ln(2)
// # 16 MULTs, 16 ADDs
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
// approximation of e^r using taylor polynomial
// cNUM --> coefficients
const __m512 c5 = _mm512_set1_ps(1.0f / 120.0f);
const __m512 c4 = _mm512_set1_ps(1.0f / 24.0f);
const __m512 c3 = _mm512_set1_ps(1.0f / 6.0f);
const __m512 c2 = _mm512_set1_ps(0.5f);
const __m512 c1 = _mm512_set1_ps(1.0f);
const __m512 c0 = _mm512_set1_ps(1.0f);
const __m512 c13 = _mm512_set1_ps(c13_scalar);
const __m512 c12 = _mm512_set1_ps(c12_scalar);
const __m512 c11 = _mm512_set1_ps(c11_scalar);
const __m512 c10 = _mm512_set1_ps(c10_scalar);
const __m512 c9 = _mm512_set1_ps(c9_scalar);
const __m512 c8 = _mm512_set1_ps(c8_scalar);
const __m512 c7 = _mm512_set1_ps(c7_scalar);
const __m512 c6 = _mm512_set1_ps(c6_scalar);
const __m512 c5 = _mm512_set1_ps(c5_scalar);
const __m512 c4 = _mm512_set1_ps(c4_scalar);
const __m512 c3 = _mm512_set1_ps(c3_scalar);
const __m512 c2 = _mm512_set1_ps(c2_scalar);
const __m512 c1 = _mm512_set1_ps(c1_scalar);
const __m512 c0 = _mm512_set1_ps(c0_scalar);
// apply coefficients
__m512 p = c5;
__m512 p = c13;
// # 13 * (16 MULTs, 16 ADDs)
// # = 208 MULTs, 208 ADDs
p = _mm512_fmadd_ps(p, r, c12);
p = _mm512_fmadd_ps(p, r, c11);
p = _mm512_fmadd_ps(p, r, c10);
p = _mm512_fmadd_ps(p, r, c9);
p = _mm512_fmadd_ps(p, r, c8);
p = _mm512_fmadd_ps(p, r, c7);
p = _mm512_fmadd_ps(p, r, c6);
p = _mm512_fmadd_ps(p, r, c5);
p = _mm512_fmadd_ps(p, r, c4);
p = _mm512_fmadd_ps(p, r, c3);
p = _mm512_fmadd_ps(p, r, c2);
p = _mm512_fmadd_ps(p, r, c1);
p = _mm512_fmadd_ps(p, r, c0);
// use bit shifting for 2^n and bias because float have 23 bit mantissa
__m512i ni = _mm512_cvtps_epi32(nf);
__m512i biased = _mm512_add_epi32(ni, _mm512_set1_epi32(127));
__m512i scaled = _mm512_slli_epi32(biased, 23);
__m512 pow2n = _mm512_castsi512_ps(scaled);
// e^x
// # 16 MULTs
return _mm512_mul_ps(p, pow2n);
}
@@ -233,254 +252,227 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
const __m512 v_np = _mm512_set1_ps((float)n_p);
const __m512 v_exp_min = _mm512_set1_ps(-87.0f);
// # ADDs: n_iter * (
// # n_p * 68 +
// # n_p * n_p_pad * 29 +
// # n_p * board.n_obstacles * 10 +
// # n_p_pad * 7
// # )
// # MULTs: n_iter * (
// # n_p * 4 +
// # n_p * n_p_pad * 42 +
// # n_p * board.n_obstacles * 13 +
// # n_p_pad * 16
// # )
// # DIVs: n_iter * (
// # n_p * n_p_pad * 3 +
// # n_p * board.n_obstacles +
// # n_p_pad * 2
// # )
// # SQRTs: n_iter * (
// # n_p * n_p_pad * 3 +
// # n_p * board.n_obstacles +
// # n_p_pad * 3
// # )
// # EXPs: n_iter * n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
// set the vector for the current pedestrian
// get desired velocity
// # 2 MULTs
float desired_velocity_x = e_x[i] * desired_v0[i];
float desired_velocity_y = e_y[i] * desired_v0[i];
// velocity change accumulator
// # 2 ADDs, 2 MULTs
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
// pos
const __m512 pix = _mm512_set1_ps(pos_x[i]);
const __m512 piy = _mm512_set1_ps(pos_y[i]);
// desired direction
const __m512 eix = _mm512_set1_ps(e_x[i]);
const __m512 eiy = _mm512_set1_ps(e_y[i]);
// broadcast index used by mask to prevent situation pedestrian[i] == pedestrian[j], see m_neq_i_0 and m_neq_i_1
const __m512 v_i = _mm512_set1_ps((float)i);
// accumulators --> 16 pedestrians per accumulator
__m512 acc_x_0 = _mm512_setzero_ps();
__m512 acc_y_0 = _mm512_setzero_ps();
__m512 acc_x_1 = _mm512_setzero_ps();
__m512 acc_y_1 = _mm512_setzero_ps();
// force between pedestrians
// loop unroll factor 32 with 2 vectors
// --> each vector has 16 lanes
for (int j = 0; j < n_p_pad; j += 32) {
// [j, j+1, j+2, ..., j+15] and [j+16, j+17, j+18, ..., j+31]
// # 32 ADDs
__m512 j_idx_0 = _mm512_add_ps(_mm512_set1_ps((float)j), lane_off);
__m512 j_idx_1 = _mm512_add_ps(_mm512_set1_ps((float)j + 16), lane_off);
// lane is active if j != i
// [1,1,1,0,1,1,1,...] 0 --> i == j, 1 --> i != j
__mmask16 m_neq_i_0 = _mm512_cmp_ps_mask(j_idx_0, v_i, _CMP_NEQ_OQ);
__mmask16 m_neq_i_1 = _mm512_cmp_ps_mask(j_idx_1, v_i, _CMP_NEQ_OQ);
// lane is active if j < n_p --> valid range
// [1,1,1,0,1,1,1,...] 0 --> j >= n_p, 1 --> j < n_p
__mmask16 m_in_range_0 = _mm512_cmp_ps_mask(j_idx_0, v_np, _CMP_LT_OQ);
__mmask16 m_in_range_1 = _mm512_cmp_ps_mask(j_idx_1, v_np, _CMP_LT_OQ);
// bitwise comparison if not valid --> 0
// finally check if lane is valid or not --> prevents i == j, and
// j >= n_p because of loop unrolling --> we can therefore omit a remainings for loop
__mmask16 active_0 = m_neq_i_0 & m_in_range_0;
__mmask16 active_1 = m_neq_i_1 & m_in_range_1;
// ---
// load the positions of the other pedestrians x value
// [pos[j], pos[j+1], ..., pos[j+15]] and [pos[16], pos[j+17], ..., pos[j+31]] for x and y
__m512 pjx_0 = _mm512_loadu_ps(&pos_x[j]);
__m512 pjx_1 = _mm512_loadu_ps(&pos_x[j + 16]);
__m512 pjy_0 = _mm512_loadu_ps(&pos_y[j]);
__m512 pjy_1 = _mm512_loadu_ps(&pos_y[j + 16]);
// ---
// step vector of j
// [(v_j * delta_t * e_j).x, (v_j+1 * delta_t * e_j+1).x, ...]
__m512 sbe_jx_0 = _mm512_loadu_ps(&sb_e_x[j]);
__m512 sbe_jx_1 = _mm512_loadu_ps(&sb_e_x[j + 16]);
__m512 sbe_jy_0 = _mm512_loadu_ps(&sb_e_y[j]);
__m512 sbe_jy_1 = _mm512_loadu_ps(&sb_e_y[j + 16]);
// ---
// squared step length
// [j, j+1, ...]
__m512 sb_jsq_0 = _mm512_loadu_ps(&sb_sq[j]);
__m512 sb_jsq_1 = _mm512_loadu_ps(&sb_sq[j + 16]);
// ---
// distance vector
// [j, j+1, ...]
// # 64 ADDs
__m512 r_ab_x_0 = _mm512_sub_ps(pix, pjx_0);
__m512 r_ab_x_1 = _mm512_sub_ps(pix, pjx_1);
__m512 r_ab_y_0 = _mm512_sub_ps(piy, pjy_0);
__m512 r_ab_y_1 = _mm512_sub_ps(piy, pjy_1);
// |r_ab|^2 needed for rsqrt to prevent 0 values --> x^2 + y^2
// # 64 MULTs, 32 ADDs
__m512 r_ab_n2_0 = _mm512_fmadd_ps(r_ab_x_0, r_ab_x_0, _mm512_mul_ps(r_ab_y_0, r_ab_y_0));
__m512 r_ab_n2_1 = _mm512_fmadd_ps(r_ab_x_1, r_ab_x_1, _mm512_mul_ps(r_ab_y_1, r_ab_y_1));
// prevent cases where distance = 0 (=pedestrian on top of each other)
// [dist_i_j, dist_i_j+1, ...]
r_ab_n2_0 = _mm512_max_ps(r_ab_n2_0, v_eps_sq);
r_ab_n2_1 = _mm512_max_ps(r_ab_n2_1, v_eps_sq);
// ---
// 1 / |r_ab| --> 14 bit rsqrt approx. rsqrt --> 1 / x^2 --> rsqrt(x^2) = 1/x
// [1 / dist_i_j, 1 / dist_i_j+1, ...]
// # 32 DIVs, 32 SQRTs
__m512 inv_r_ab_n_0 = _mm512_rsqrt14_ps(r_ab_n2_0);
__m512 inv_r_ab_n_1 = _mm512_rsqrt14_ps(r_ab_n2_1);
// r_ab^2 * 1 / r_ab = |r_ab| --> normed
// [r_i_j, r_i_j+1, ...]
// # 32 MULTs
__m512 r_ab_n_0 = _mm512_mul_ps(r_ab_n2_0, inv_r_ab_n_0);
__m512 r_ab_n_1 = _mm512_mul_ps(r_ab_n2_1, inv_r_ab_n_1);
// r_ab - step[j]
// j next pos
// # 64 ADDs
__m512 r_ab2_x_0 = _mm512_sub_ps(r_ab_x_0, sbe_jx_0);
__m512 r_ab2_x_1 = _mm512_sub_ps(r_ab_x_1, sbe_jx_1);
__m512 r_ab2_y_0 = _mm512_sub_ps(r_ab_y_0, sbe_jy_0);
__m512 r_ab2_y_1 = _mm512_sub_ps(r_ab_y_1, sbe_jy_1);
// |r_ab_2| --> needed for rsqrt trick (r_ab - step)
// # 64 MULTs, 32 ADDs
__m512 r_ab2_n2_0 = _mm512_fmadd_ps(r_ab2_x_0, r_ab2_x_0, _mm512_mul_ps(r_ab2_y_0, r_ab2_y_0));
__m512 r_ab2_n2_1 = _mm512_fmadd_ps(r_ab2_x_1, r_ab2_x_1, _mm512_mul_ps(r_ab2_y_1, r_ab2_y_1));
// prevent 0 case --> infinity
r_ab2_n2_0 = _mm512_max_ps(r_ab2_n2_0, v_eps_sq);
r_ab2_n2_1 = _mm512_max_ps(r_ab2_n2_1, v_eps_sq);
// same as before use rsqrt with 14bit approx
// [1 / |r_ab_2|, 1 / |r_ab_2|, ...]
// # 32 DIVs, 32 SQRTs
__m512 inv_r_ab2_n_0 = _mm512_rsqrt14_ps(r_ab2_n2_0);
__m512 inv_r_ab2_n_1 = _mm512_rsqrt14_ps(r_ab2_n2_1);
// |r_ab_2_second_distance_vector| * 1 / |r_ab_2_first_distance_vector|
// # 32 MULTs
__m512 r_ab2_n_0 = _mm512_mul_ps(r_ab2_n2_0, inv_r_ab2_n_0);
__m512 r_ab2_n_1 = _mm512_mul_ps(r_ab2_n2_1, inv_r_ab2_n_1);
// r_sum = |r_ab| + |r_ab - step[j]| --> [w_0, w_1, w_2, ..., w_15] and [w_16, ..., w_31]
// # 32 ADDs
__m512 r_sum_0 = _mm512_add_ps(r_ab_n_0, r_ab2_n_0);
__m512 r_sum_1 = _mm512_add_ps(r_ab_n_1, r_ab2_n_1);
// 2_b^2 = r_sum^2 - step[j]^2
// # 32 MULTs, 32 ADDs
__m512 two_b_sq_0 = _mm512_fmsub_ps(r_sum_0, r_sum_0, sb_jsq_0);
__m512 two_b_sq_1 = _mm512_fmsub_ps(r_sum_1, r_sum_1, sb_jsq_1);
// prevent 0 case
two_b_sq_0 = _mm512_max_ps(two_b_sq_0, v_eps_sq);
two_b_sq_1 = _mm512_max_ps(two_b_sq_1, v_eps_sq);
// 2_b
// 1 / 2_b^(2*0.5) --> 1 / 2_b
// # 32 DIVs, 32 SQRTs
__m512 inv_two_b_0 = _mm512_rsqrt14_ps(two_b_sq_0);
__m512 inv_two_b_1 = _mm512_rsqrt14_ps(two_b_sq_1);
// 2_b = 2_b^2 * 1 / 2_b
// # 32 MULTs
__m512 two_b_0 = _mm512_mul_ps(two_b_sq_0, inv_two_b_0);
__m512 two_b_1 = _mm512_mul_ps(two_b_sq_1, inv_two_b_1);
// ---
// two_b * -1 / (2*sigma) --> exp
// # 32 MULTs
__m512 e_arg_0 = _mm512_mul_ps(two_b_0, v_neg_2s_inv);
__m512 e_arg_1 = _mm512_mul_ps(two_b_1, v_neg_2s_inv);
// exp(x)
// when x is small just assume constant value --> faster exp
e_arg_0 = _mm512_max_ps(e_arg_0, v_exp_min);
e_arg_1 = _mm512_max_ps(e_arg_1, v_exp_min);
// custom fast exponent intrinsic using polynomial approximations and some precomputed constants
// # 512 MULTs, 448 ADDs
__m512 e_val_0 = _avx512_exp_ps(e_arg_0);
__m512 e_val_1 = _avx512_exp_ps(e_arg_1);
// [−(V0/σ) * e^(−b/σ) * w/(4b), ...] --> scalar
// # 96 MULTs
__m512 numer_inv_two_b_0 = _mm512_mul_ps(_mm512_mul_ps(r_sum_0, v_neg_v0_div_2s), _mm512_mul_ps(e_val_0, inv_two_b_0));
__m512 numer_inv_two_b_1 = _mm512_mul_ps(_mm512_mul_ps(r_sum_1, v_neg_v0_div_2s), _mm512_mul_ps(e_val_1, inv_two_b_1));
// scalar * 1 / |r_ab|
// # 32 MULTs
__m512 sm_r_ab_0 = _mm512_mul_ps(numer_inv_two_b_0, inv_r_ab_n_0);
__m512 sm_r_ab_1 = _mm512_mul_ps(numer_inv_two_b_1, inv_r_ab_n_1);
// scalar * 1 / |r_ab_2|
// # 32 MULTs
__m512 sm_r_ab2_0 = _mm512_mul_ps(numer_inv_two_b_0, inv_r_ab2_n_0);
__m512 sm_r_ab2_1 = _mm512_mul_ps(numer_inv_two_b_1, inv_r_ab2_n_1);
// summed force
// # 128 MULTs, 64 ADDs
__m512 f_x_0 = _mm512_fmadd_ps(r_ab2_x_0, sm_r_ab2_0, _mm512_mul_ps(r_ab_x_0, sm_r_ab_0));
__m512 f_x_1 = _mm512_fmadd_ps(r_ab2_x_1, sm_r_ab2_1, _mm512_mul_ps(r_ab_x_1, sm_r_ab_1));
__m512 f_y_0 = _mm512_fmadd_ps(r_ab2_y_0, sm_r_ab2_0, _mm512_mul_ps(r_ab_y_0, sm_r_ab_0));
__m512 f_y_1 = _mm512_fmadd_ps(r_ab2_y_1, sm_r_ab2_1, _mm512_mul_ps(r_ab_y_1, sm_r_ab_1));
// using masking to perform --> field of view formula
// if (e_dot_f >= 0.0 || e_dot_f_sq <= rhs)
// return dt_neg;
// else
// return c_neg_times_dt;
//
// # 64 MULTs, 32 ADDs
__m512 f_norm_sq_0 = _mm512_fmadd_ps(f_x_0, f_x_0, _mm512_mul_ps(f_y_0, f_y_0));
__m512 f_norm_sq_1 = _mm512_fmadd_ps(f_x_1, f_x_1, _mm512_mul_ps(f_y_1, f_y_1));
// # 32 MULTs
__m512 rhs_0 = _mm512_mul_ps(f_norm_sq_0, v_cos_phi_sq);
__m512 rhs_1 = _mm512_mul_ps(f_norm_sq_1, v_cos_phi_sq);
// # 64 MULTs, 32 ADDs
__m512 e_dot_f_0 = _mm512_fmadd_ps(eix, f_x_0, _mm512_mul_ps(eiy, f_y_0));
__m512 e_dot_f_1 = _mm512_fmadd_ps(eix, f_x_1, _mm512_mul_ps(eiy, f_y_1));
// # 32 MULTs
__m512 e_dot_f_sq_0 = _mm512_mul_ps(e_dot_f_0, e_dot_f_0);
__m512 e_dot_f_sq_1 = _mm512_mul_ps(e_dot_f_1, e_dot_f_1);
// e_i * f < 0 --> behind
__mmask16 c1_0 = _mm512_cmp_ps_mask(e_dot_f_0, v_zero, _CMP_LT_OQ);
__mmask16 c1_1 = _mm512_cmp_ps_mask(e_dot_f_1, v_zero, _CMP_LT_OQ);
// (e_i * f)^2 > cos(phi) --> out of view
__mmask16 c2_0 = _mm512_cmp_ps_mask(e_dot_f_sq_0, rhs_0, _CMP_GT_OQ);
__mmask16 c2_1 = _mm512_cmp_ps_mask(e_dot_f_sq_1, rhs_1, _CMP_GT_OQ);
// get the case (in field of view or not)
// 1 = reduced weight, 0 = regular
__mmask16 cond_0 = c1_0 & c2_0;
__mmask16 cond_1 = c1_1 & c2_1;
// apply the correct weight
__m512 w_val_0 = _mm512_mask_blend_ps(cond_0, v_dt_neg, v_c_neg_dt);
__m512 w_val_1 = _mm512_mask_blend_ps(cond_1, v_dt_neg, v_c_neg_dt);
// take care of i == j case by zeroing it out
// see active_0 and active_1 at the beginning of this loop
// [f.x or 0, ...]
__m512 f_x_safe_0 = _mm512_maskz_mov_ps(active_0, f_x_0);
__m512 f_x_safe_1 = _mm512_maskz_mov_ps(active_1, f_x_1);
__m512 f_y_safe_0 = _mm512_maskz_mov_ps(active_0, f_y_0);
__m512 f_y_safe_1 = _mm512_maskz_mov_ps(active_1, f_y_1);
// accumulate force with weight
// force_P_total += weight * force_P_other
// # 64 MULTs, 64 ADDs
acc_x_0 = _mm512_fmadd_ps(f_x_safe_0, w_val_0, acc_x_0);
acc_x_1 = _mm512_fmadd_ps(f_x_safe_1, w_val_1, acc_x_1);
acc_y_0 = _mm512_fmadd_ps(f_y_safe_0, w_val_0, acc_y_0);
acc_y_1 = _mm512_fmadd_ps(f_y_safe_1, w_val_1, acc_y_1);
}
// scalar force accumulator
// # 64 ADDs
f_alpha_dt_x += _mm512_reduce_add_ps(_mm512_add_ps(acc_x_0, acc_x_1));
f_alpha_dt_y += _mm512_reduce_add_ps(_mm512_add_ps(acc_y_0, acc_y_1));
// force between pedestrian and obstacles
// no optimizations, because force between pedestrian is the bottleneck of the computation
for (int k = 0; k < board.n_obstacles; k++) {
// # 2 ADDs
float v1_x = pos_x[i] - board.obstacles[k].start_x;
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
// # 3 MULTs, 1 ADD
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
if (projection < 0.0f)
@@ -488,12 +480,15 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
else if (projection > 1.0f)
projection = 1.0f;
// # 4 ADDs, 2 MULTs
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
// # 1 SQRT, 2 MULTs, 1 ADD
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
// # 4 MULTs, 1 EXP, 1 DIV
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
@@ -504,21 +499,21 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
if (e_dot_f < cos_phi)
w_val = c_neg_times_dt;
// # 2 MULTS
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
// # 2 ADDs
f_alpha_dt_x += f_alpha_B_dt_x;
f_alpha_dt_y += f_alpha_B_dt_y;
}
// # 2 ADDs
w_x[i] += f_alpha_dt_x;
w_y[i] += f_alpha_dt_y;
}
// update each pedestrian
for (int i = 0; i < n_p_pad; i += 16) {
// load 16 pedestrians per iteration with their data
// SoA in vectors
__m512 w_x_i_vec = _mm512_loadu_ps(&w_x[i]);
__m512 w_y_i_vec = _mm512_loadu_ps(&w_y[i]);
__m512 v_max_i_vec = _mm512_loadu_ps(&v_max[i]);
@@ -530,20 +525,27 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512i color_i_vec = _mm512_loadu_si512((__m512i *)&color[i]);
// # 32 MULTs, 16 ADDs
__m512 w_norm_sq = _mm512_fmadd_ps(w_x_i_vec, w_x_i_vec, _mm512_mul_ps(w_y_i_vec, w_y_i_vec));
// # 16 SQRTs, 16 DIVs
__m512 w_norm_inv = _mm512_rsqrt14_ps(w_norm_sq);
// # 16 MULTs
__m512 correction_factor = _mm512_min_ps(_mm512_set1_ps(1.0f), _mm512_mul_ps(v_max_i_vec, w_norm_inv));
// # 32 MULTs
__m512 curr_vel_x_i_vec = _mm512_mul_ps(w_x_i_vec, correction_factor);
__m512 curr_vel_y_i_vec = _mm512_mul_ps(w_y_i_vec, correction_factor);
__m512 dt_vec = _mm512_set1_ps(dt);
// # 32 MULTs, 32 ADDs
pos_x_i_vec = _mm512_fmadd_ps(curr_vel_x_i_vec, dt_vec, pos_x_i_vec);
pos_y_i_vec = _mm512_fmadd_ps(curr_vel_y_i_vec, dt_vec, pos_y_i_vec);
// differentiate whether pedestrian came from left or right using masking
__mmask16 cond_1_left = _mm512_cmpeq_epi32_mask(color_i_vec, _mm512_setzero_si512());
__mmask16 cond_1_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH - 1.5f), _CMP_GT_OQ);
__mmask16 cond_1_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE_POINT_FIVE), _CMP_GT_OQ);
__mmask16 cond_1 = cond_1_left & cond_1_right;
pos_x_i_vec = _mm512_mask_blend_ps(cond_1, pos_x_i_vec, _mm512_set1_ps(1.0f));
@@ -557,7 +559,7 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__mmask16 cond_2_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(1.5f), _CMP_LT_OQ);
__mmask16 cond_2 = cond_2_left & cond_2_right;
pos_x_i_vec = _mm512_mask_blend_ps(cond_2, pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH - 1.0f));
pos_x_i_vec = _mm512_mask_blend_ps(cond_2, pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE));
destination_y_i_vec = _mm512_mask_blend_ps(cond_2, destination_y_i_vec, pos_y_i_vec);
__m512 neg_desired_v0 = _mm512_xor_ps(desired_v0_i_vec, _mm512_set1_ps(-0.0f));
w_x_i_vec = _mm512_mask_blend_ps(cond_2, w_x_i_vec, neg_desired_v0);
@@ -568,18 +570,29 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
pos_y_i_vec = _mm512_max_ps(pos_y_i_vec, _mm512_set1_ps(WALL_EPS));
pos_y_i_vec = _mm512_min_ps(pos_y_i_vec, _mm512_set1_ps(BOARD_WIDTH_MINUS_WALL_EPS));
// # 32 ADDs
__m512 ex_vec = _mm512_sub_ps(destination_x_i_vec, pos_x_i_vec);
__m512 ey_vec = _mm512_sub_ps(destination_y_i_vec, pos_y_i_vec);
// # 32 MULTs, 16 ADDs
__m512 e_norm_sq = _mm512_fmadd_ps(ex_vec, ex_vec, _mm512_mul_ps(ey_vec, ey_vec));
// # 16 SQRTs, 16 DIVs
__m512 e_norm_inv = _mm512_rsqrt14_ps(e_norm_sq);
// # 32 MULTs
__m512 e_x_i_vec = _mm512_mul_ps(ex_vec, e_norm_inv);
__m512 e_y_i_vec = _mm512_mul_ps(ey_vec, e_norm_inv);
// # 32 MULTs, 16 ADDs
__m512 vel_norm_sq = _mm512_fmadd_ps(curr_vel_x_i_vec, curr_vel_x_i_vec, _mm512_mul_ps(curr_vel_y_i_vec, curr_vel_y_i_vec));
// # 16 MULTs
__m512 sb_sq_i_vec = _mm512_mul_ps(vel_norm_sq, _mm512_set1_ps(delta_t_sq));
// # 16 SQRTs
__m512 vel_norm_dt = _mm512_sqrt_ps(sb_sq_i_vec);
// write the data
_mm512_storeu_ps(&curr_vel_x[i], curr_vel_x_i_vec);
_mm512_storeu_ps(&curr_vel_y[i], curr_vel_y_i_vec);
_mm512_storeu_ps(&w_x[i], w_x_i_vec);
@@ -590,6 +603,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
_mm512_storeu_ps(&e_y[i], e_y_i_vec);
_mm512_storeu_ps(&destination_y[i], destination_y_i_vec);
_mm512_storeu_ps(&sb_sq[i], sb_sq_i_vec);
// # 32 MULTs
_mm512_storeu_ps(&sb_e_x[i], _mm512_mul_ps(e_x_i_vec, vel_norm_dt));
_mm512_storeu_ps(&sb_e_y[i], _mm512_mul_ps(e_y_i_vec, vel_norm_dt));
}
@@ -1,632 +0,0 @@
// # Changes: Added more correction rounds to _avx512_exp_ps to do 30 flops per scalar
// # Predecessor: variant_float_inlining_SoA_AVX512_polyexp_moreavx_ilp_2_rsqrt_morersqrt.cpp
#include "../headers/board_reduced_float.hpp"
#include "../headers/constants_float.hpp"
#include "../headers/pedestrian_reduced_float.hpp"
#include <immintrin.h>
#include <fstream>
#include <random>
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
#ifndef RANDOM_SEED
#define RANDOM_SEED 0
#endif
#ifndef BENCHMARK
#define BENCHMARK 0
#endif
using namespace std;
alignas(64) vector<float> pos_x, pos_y;
alignas(64) vector<float> sb_sq;
alignas(64) vector<float> sb_e_x, sb_e_y;
alignas(64) vector<float> e_x, e_y;
alignas(64) vector<float> curr_vel_x, curr_vel_y;
alignas(64) vector<float> w_x, w_y;
alignas(64) vector<float> desired_v0;
alignas(64) vector<float> v_max;
alignas(64) vector<float> destination_x, destination_y;
alignas(64) 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<float> velocity_gen(v0_mean, v0_std_dev);
uniform_real_distribution<float> y_gen(0.3, BOARD_WIDTH - 0.3);
uniform_real_distribution<float> x_gen_left(2.0, BOARD_LENGTH / 2.0);
uniform_real_distribution<float> x_gen_right(BOARD_LENGTH / 2.0, BOARD_LENGTH - 2.0);
const int n_p_pad = (n_p + 31) & ~31;
pos_x.resize(n_p_pad);
pos_y.resize(n_p_pad);
sb_sq.resize(n_p_pad);
sb_e_x.resize(n_p_pad);
sb_e_y.resize(n_p_pad);
e_x.resize(n_p_pad);
e_y.resize(n_p_pad);
curr_vel_x.resize(n_p_pad);
curr_vel_y.resize(n_p_pad);
w_x.resize(n_p_pad);
w_y.resize(n_p_pad);
desired_v0.resize(n_p_pad);
v_max.resize(n_p_pad);
destination_x.resize(n_p_pad);
destination_y.resize(n_p_pad);
color.resize(n_p_pad);
int n_p_half = n_p / 2;
for (int i = 0; i < n_p_half; i++) {
float x = x_gen_left(gen);
float y = y_gen(gen);
float dest_x = BOARD_LENGTH + 30.0f;
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1f);
float v_m = 1.3f * 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;
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
for (int i = n_p_half; i < n_p; i++) {
float x = x_gen_right(gen);
float y = y_gen(gen);
float dest_x = -30.0f;
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1f);
float v_m = 1.3f * 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;
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
for (int j = n_p; j < n_p_pad; j++) {
pos_x[j] = 1e9f;
pos_y[j] = 1e9f;
sb_sq[j] = 0.0f;
sb_e_x[j] = 0.0f;
sb_e_y[j] = 0.0f;
}
return nullptr;
}
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// # 256 MULTs, 224 ADDs
static inline __m512 _avx512_exp_ps(__m512 x) {
const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
// # 16 MULTs
__m512 nf = _mm512_roundscale_ps(_mm512_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
const __m512 ln2 = _mm512_set1_ps(0.6931471805599453f);
// # 16 MULTs, 16 ADDs
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
const __m512 c13 = _mm512_set1_ps(c13_scalar);
const __m512 c12 = _mm512_set1_ps(c12_scalar);
const __m512 c11 = _mm512_set1_ps(c11_scalar);
const __m512 c10 = _mm512_set1_ps(c10_scalar);
const __m512 c9 = _mm512_set1_ps(c9_scalar);
const __m512 c8 = _mm512_set1_ps(c8_scalar);
const __m512 c7 = _mm512_set1_ps(c7_scalar);
const __m512 c6 = _mm512_set1_ps(c6_scalar);
const __m512 c5 = _mm512_set1_ps(c5_scalar);
const __m512 c4 = _mm512_set1_ps(c4_scalar);
const __m512 c3 = _mm512_set1_ps(c3_scalar);
const __m512 c2 = _mm512_set1_ps(c2_scalar);
const __m512 c1 = _mm512_set1_ps(c1_scalar);
const __m512 c0 = _mm512_set1_ps(c0_scalar);
__m512 p = c13;
// # 13 * (16 MULTs, 16 ADDs)
// # = 208 MULTs, 208 ADDs
p = _mm512_fmadd_ps(p, r, c12);
p = _mm512_fmadd_ps(p, r, c11);
p = _mm512_fmadd_ps(p, r, c10);
p = _mm512_fmadd_ps(p, r, c9);
p = _mm512_fmadd_ps(p, r, c8);
p = _mm512_fmadd_ps(p, r, c7);
p = _mm512_fmadd_ps(p, r, c6);
p = _mm512_fmadd_ps(p, r, c5);
p = _mm512_fmadd_ps(p, r, c4);
p = _mm512_fmadd_ps(p, r, c3);
p = _mm512_fmadd_ps(p, r, c2);
p = _mm512_fmadd_ps(p, r, c1);
p = _mm512_fmadd_ps(p, r, c0);
__m512i ni = _mm512_cvtps_epi32(nf);
__m512i biased = _mm512_add_epi32(ni, _mm512_set1_epi32(127));
__m512i scaled = _mm512_slli_epi32(biased, 23);
__m512 pow2n = _mm512_castsi512_ps(scaled);
// # 16 MULTs
return _mm512_mul_ps(p, pow2n);
}
int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, const char *dump_path) {
(void)pedestrians;
#if BENCHMARK
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
vector<tuple<int, int, float, float, float, float, 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++) {
samples.emplace_back(0, i, pos_x[i], pos_y[i], e_x[i], e_y[i], color[i]);
}
}
#endif
const int n_p_pad = (n_p + 31) & ~31;
alignas(64) const float lane_offset_arr[16] = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f};
const __m512 lane_off = _mm512_load_ps(lane_offset_arr);
const __m512 v_neg_v0_div_2s = _mm512_set1_ps(NEG_V0_AB_div_TWO_SIGMA);
const __m512 v_neg_2s_inv = _mm512_set1_ps(NEG_TWO_SIGMA_INV);
const __m512 v_cos_phi_sq = _mm512_set1_ps(cos_phi_sq);
const __m512 v_dt_neg = _mm512_set1_ps(dt_neg);
const __m512 v_c_neg_dt = _mm512_set1_ps(c_neg_times_dt);
const __m512 v_zero = _mm512_setzero_ps();
const __m512 v_one = _mm512_set1_ps(1.0f);
const __m512 v_eps = _mm512_set1_ps(EPS);
const __m512 v_eps_sq = _mm512_set1_ps(EPS_SQ);
const __m512 v_np = _mm512_set1_ps((float)n_p);
const __m512 v_exp_min = _mm512_set1_ps(-87.0f);
// # ADDs: n_iter * (
// # n_p * 68 +
// # n_p * n_p_pad * 29 +
// # n_p * board.n_obstacles * 10 +
// # n_p_pad * 7
// # )
// # MULTs: n_iter * (
// # n_p * 4 +
// # n_p * n_p_pad * 42 +
// # n_p * board.n_obstacles * 13 +
// # n_p_pad * 16
// # )
// # DIVs: n_iter * (
// # n_p * n_p_pad * 3 +
// # n_p * board.n_obstacles +
// # n_p_pad * 2
// # )
// # SQRTs: n_iter * (
// # n_p * n_p_pad * 3 +
// # n_p * board.n_obstacles +
// # n_p_pad * 3
// # )
// # EXPs: n_iter * n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
// # 2 MULTs
float desired_velocity_x = e_x[i] * desired_v0[i];
float desired_velocity_y = e_y[i] * desired_v0[i];
// # 2 ADDs, 2 MULTs
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
const __m512 pix = _mm512_set1_ps(pos_x[i]);
const __m512 piy = _mm512_set1_ps(pos_y[i]);
const __m512 eix = _mm512_set1_ps(e_x[i]);
const __m512 eiy = _mm512_set1_ps(e_y[i]);
const __m512 v_i = _mm512_set1_ps((float)i);
__m512 acc_x_0 = _mm512_setzero_ps();
__m512 acc_y_0 = _mm512_setzero_ps();
__m512 acc_x_1 = _mm512_setzero_ps();
__m512 acc_y_1 = _mm512_setzero_ps();
for (int j = 0; j < n_p_pad; j += 32) {
// # 32 ADDs
__m512 j_idx_0 = _mm512_add_ps(_mm512_set1_ps((float)j), lane_off);
__m512 j_idx_1 = _mm512_add_ps(_mm512_set1_ps((float)j + 16), lane_off);
__mmask16 m_neq_i_0 = _mm512_cmp_ps_mask(j_idx_0, v_i, _CMP_NEQ_OQ);
__mmask16 m_neq_i_1 = _mm512_cmp_ps_mask(j_idx_1, v_i, _CMP_NEQ_OQ);
__mmask16 m_in_range_0 = _mm512_cmp_ps_mask(j_idx_0, v_np, _CMP_LT_OQ);
__mmask16 m_in_range_1 = _mm512_cmp_ps_mask(j_idx_1, v_np, _CMP_LT_OQ);
__mmask16 active_0 = m_neq_i_0 & m_in_range_0;
__mmask16 active_1 = m_neq_i_1 & m_in_range_1;
__m512 pjx_0 = _mm512_loadu_ps(&pos_x[j]);
__m512 pjx_1 = _mm512_loadu_ps(&pos_x[j + 16]);
__m512 pjy_0 = _mm512_loadu_ps(&pos_y[j]);
__m512 pjy_1 = _mm512_loadu_ps(&pos_y[j + 16]);
__m512 sbe_jx_0 = _mm512_loadu_ps(&sb_e_x[j]);
__m512 sbe_jx_1 = _mm512_loadu_ps(&sb_e_x[j + 16]);
__m512 sbe_jy_0 = _mm512_loadu_ps(&sb_e_y[j]);
__m512 sbe_jy_1 = _mm512_loadu_ps(&sb_e_y[j + 16]);
__m512 sb_jsq_0 = _mm512_loadu_ps(&sb_sq[j]);
__m512 sb_jsq_1 = _mm512_loadu_ps(&sb_sq[j + 16]);
// # 64 ADDs
__m512 r_ab_x_0 = _mm512_sub_ps(pix, pjx_0);
__m512 r_ab_x_1 = _mm512_sub_ps(pix, pjx_1);
__m512 r_ab_y_0 = _mm512_sub_ps(piy, pjy_0);
__m512 r_ab_y_1 = _mm512_sub_ps(piy, pjy_1);
// # 64 MULTs, 32 ADDs
__m512 r_ab_n2_0 = _mm512_fmadd_ps(r_ab_x_0, r_ab_x_0, _mm512_mul_ps(r_ab_y_0, r_ab_y_0));
__m512 r_ab_n2_1 = _mm512_fmadd_ps(r_ab_x_1, r_ab_x_1, _mm512_mul_ps(r_ab_y_1, r_ab_y_1));
r_ab_n2_0 = _mm512_max_ps(r_ab_n2_0, v_eps_sq);
r_ab_n2_1 = _mm512_max_ps(r_ab_n2_1, v_eps_sq);
// # 32 DIVs, 32 SQRTs
__m512 inv_r_ab_n_0 = _mm512_rsqrt14_ps(r_ab_n2_0);
__m512 inv_r_ab_n_1 = _mm512_rsqrt14_ps(r_ab_n2_1);
// # 32 MULTs
__m512 r_ab_n_0 = _mm512_mul_ps(r_ab_n2_0, inv_r_ab_n_0);
__m512 r_ab_n_1 = _mm512_mul_ps(r_ab_n2_1, inv_r_ab_n_1);
// # 64 ADDs
__m512 r_ab2_x_0 = _mm512_sub_ps(r_ab_x_0, sbe_jx_0);
__m512 r_ab2_x_1 = _mm512_sub_ps(r_ab_x_1, sbe_jx_1);
__m512 r_ab2_y_0 = _mm512_sub_ps(r_ab_y_0, sbe_jy_0);
__m512 r_ab2_y_1 = _mm512_sub_ps(r_ab_y_1, sbe_jy_1);
// # 64 MULTs, 32 ADDs
__m512 r_ab2_n2_0 = _mm512_fmadd_ps(r_ab2_x_0, r_ab2_x_0, _mm512_mul_ps(r_ab2_y_0, r_ab2_y_0));
__m512 r_ab2_n2_1 = _mm512_fmadd_ps(r_ab2_x_1, r_ab2_x_1, _mm512_mul_ps(r_ab2_y_1, r_ab2_y_1));
r_ab2_n2_0 = _mm512_max_ps(r_ab2_n2_0, v_eps_sq);
r_ab2_n2_1 = _mm512_max_ps(r_ab2_n2_1, v_eps_sq);
// # 32 DIVs, 32 SQRTs
__m512 inv_r_ab2_n_0 = _mm512_rsqrt14_ps(r_ab2_n2_0);
__m512 inv_r_ab2_n_1 = _mm512_rsqrt14_ps(r_ab2_n2_1);
// # 32 MULTs
__m512 r_ab2_n_0 = _mm512_mul_ps(r_ab2_n2_0, inv_r_ab2_n_0);
__m512 r_ab2_n_1 = _mm512_mul_ps(r_ab2_n2_1, inv_r_ab2_n_1);
// # 32 ADDs
__m512 r_sum_0 = _mm512_add_ps(r_ab_n_0, r_ab2_n_0);
__m512 r_sum_1 = _mm512_add_ps(r_ab_n_1, r_ab2_n_1);
// # 32 MULTs, 32 ADDs
__m512 two_b_sq_0 = _mm512_fmsub_ps(r_sum_0, r_sum_0, sb_jsq_0);
__m512 two_b_sq_1 = _mm512_fmsub_ps(r_sum_1, r_sum_1, sb_jsq_1);
two_b_sq_0 = _mm512_max_ps(two_b_sq_0, v_eps_sq);
two_b_sq_1 = _mm512_max_ps(two_b_sq_1, v_eps_sq);
// # 32 DIVs, 32 SQRTs
__m512 inv_two_b_0 = _mm512_rsqrt14_ps(two_b_sq_0);
__m512 inv_two_b_1 = _mm512_rsqrt14_ps(two_b_sq_1);
// # 32 MULTs
__m512 two_b_0 = _mm512_mul_ps(two_b_sq_0, inv_two_b_0);
__m512 two_b_1 = _mm512_mul_ps(two_b_sq_1, inv_two_b_1);
// # 32 MULTs
__m512 e_arg_0 = _mm512_mul_ps(two_b_0, v_neg_2s_inv);
__m512 e_arg_1 = _mm512_mul_ps(two_b_1, v_neg_2s_inv);
e_arg_0 = _mm512_max_ps(e_arg_0, v_exp_min);
e_arg_1 = _mm512_max_ps(e_arg_1, v_exp_min);
// # 512 MULTs, 448 ADDs
__m512 e_val_0 = _avx512_exp_ps(e_arg_0);
__m512 e_val_1 = _avx512_exp_ps(e_arg_1);
// # 96 MULTs
__m512 numer_inv_two_b_0 = _mm512_mul_ps(_mm512_mul_ps(r_sum_0, v_neg_v0_div_2s), _mm512_mul_ps(e_val_0, inv_two_b_0));
__m512 numer_inv_two_b_1 = _mm512_mul_ps(_mm512_mul_ps(r_sum_1, v_neg_v0_div_2s), _mm512_mul_ps(e_val_1, inv_two_b_1));
// # 32 MULTs
__m512 sm_r_ab_0 = _mm512_mul_ps(numer_inv_two_b_0, inv_r_ab_n_0);
__m512 sm_r_ab_1 = _mm512_mul_ps(numer_inv_two_b_1, inv_r_ab_n_1);
// # 32 MULTs
__m512 sm_r_ab2_0 = _mm512_mul_ps(numer_inv_two_b_0, inv_r_ab2_n_0);
__m512 sm_r_ab2_1 = _mm512_mul_ps(numer_inv_two_b_1, inv_r_ab2_n_1);
// # 128 MULTs, 64 ADDs
__m512 f_x_0 = _mm512_fmadd_ps(r_ab2_x_0, sm_r_ab2_0, _mm512_mul_ps(r_ab_x_0, sm_r_ab_0));
__m512 f_x_1 = _mm512_fmadd_ps(r_ab2_x_1, sm_r_ab2_1, _mm512_mul_ps(r_ab_x_1, sm_r_ab_1));
__m512 f_y_0 = _mm512_fmadd_ps(r_ab2_y_0, sm_r_ab2_0, _mm512_mul_ps(r_ab_y_0, sm_r_ab_0));
__m512 f_y_1 = _mm512_fmadd_ps(r_ab2_y_1, sm_r_ab2_1, _mm512_mul_ps(r_ab_y_1, sm_r_ab_1));
// # 64 MULTs, 32 ADDs
__m512 f_norm_sq_0 = _mm512_fmadd_ps(f_x_0, f_x_0, _mm512_mul_ps(f_y_0, f_y_0));
__m512 f_norm_sq_1 = _mm512_fmadd_ps(f_x_1, f_x_1, _mm512_mul_ps(f_y_1, f_y_1));
// # 32 MULTs
__m512 rhs_0 = _mm512_mul_ps(f_norm_sq_0, v_cos_phi_sq);
__m512 rhs_1 = _mm512_mul_ps(f_norm_sq_1, v_cos_phi_sq);
// # 64 MULTs, 32 ADDs
__m512 e_dot_f_0 = _mm512_fmadd_ps(eix, f_x_0, _mm512_mul_ps(eiy, f_y_0));
__m512 e_dot_f_1 = _mm512_fmadd_ps(eix, f_x_1, _mm512_mul_ps(eiy, f_y_1));
// # 32 MULTs
__m512 e_dot_f_sq_0 = _mm512_mul_ps(e_dot_f_0, e_dot_f_0);
__m512 e_dot_f_sq_1 = _mm512_mul_ps(e_dot_f_1, e_dot_f_1);
__mmask16 c1_0 = _mm512_cmp_ps_mask(e_dot_f_0, v_zero, _CMP_LT_OQ);
__mmask16 c1_1 = _mm512_cmp_ps_mask(e_dot_f_1, v_zero, _CMP_LT_OQ);
__mmask16 c2_0 = _mm512_cmp_ps_mask(e_dot_f_sq_0, rhs_0, _CMP_GT_OQ);
__mmask16 c2_1 = _mm512_cmp_ps_mask(e_dot_f_sq_1, rhs_1, _CMP_GT_OQ);
__mmask16 cond_0 = c1_0 & c2_0;
__mmask16 cond_1 = c1_1 & c2_1;
__m512 w_val_0 = _mm512_mask_blend_ps(cond_0, v_dt_neg, v_c_neg_dt);
__m512 w_val_1 = _mm512_mask_blend_ps(cond_1, v_dt_neg, v_c_neg_dt);
__m512 f_x_safe_0 = _mm512_maskz_mov_ps(active_0, f_x_0);
__m512 f_x_safe_1 = _mm512_maskz_mov_ps(active_1, f_x_1);
__m512 f_y_safe_0 = _mm512_maskz_mov_ps(active_0, f_y_0);
__m512 f_y_safe_1 = _mm512_maskz_mov_ps(active_1, f_y_1);
// # 64 MULTs, 64 ADDs
acc_x_0 = _mm512_fmadd_ps(f_x_safe_0, w_val_0, acc_x_0);
acc_x_1 = _mm512_fmadd_ps(f_x_safe_1, w_val_1, acc_x_1);
acc_y_0 = _mm512_fmadd_ps(f_y_safe_0, w_val_0, acc_y_0);
acc_y_1 = _mm512_fmadd_ps(f_y_safe_1, w_val_1, acc_y_1);
}
// # 64 ADDs
f_alpha_dt_x += _mm512_reduce_add_ps(_mm512_add_ps(acc_x_0, acc_x_1));
f_alpha_dt_y += _mm512_reduce_add_ps(_mm512_add_ps(acc_y_0, acc_y_1));
for (int k = 0; k < board.n_obstacles; k++) {
// # 2 ADDs
float v1_x = pos_x[i] - board.obstacles[k].start_x;
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
// # 3 MULTs, 1 ADD
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
if (projection < 0.0f)
projection = 0.0f;
else if (projection > 1.0f)
projection = 1.0f;
// # 4 ADDs, 2 MULTs
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
// # 1 SQRT, 2 MULTs, 1 ADD
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
// # 4 MULTs, 1 EXP, 1 DIV
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
float w_val = dt_neg;
float e_dot_f = (f_alpha_B_neg_y == 0) ? 0 : (f_alpha_B_neg_y < 0 ? -e_y[i] : e_y[i]);
if (e_dot_f < cos_phi)
w_val = c_neg_times_dt;
// # 2 MULTS
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
// # 2 ADDs
f_alpha_dt_x += f_alpha_B_dt_x;
f_alpha_dt_y += f_alpha_B_dt_y;
}
// # 2 ADDs
w_x[i] += f_alpha_dt_x;
w_y[i] += f_alpha_dt_y;
}
for (int i = 0; i < n_p_pad; i += 16) {
__m512 w_x_i_vec = _mm512_loadu_ps(&w_x[i]);
__m512 w_y_i_vec = _mm512_loadu_ps(&w_y[i]);
__m512 v_max_i_vec = _mm512_loadu_ps(&v_max[i]);
__m512 pos_x_i_vec = _mm512_loadu_ps(&pos_x[i]);
__m512 pos_y_i_vec = _mm512_loadu_ps(&pos_y[i]);
__m512 destination_y_i_vec = _mm512_loadu_ps(&destination_y[i]);
__m512 destination_x_i_vec = _mm512_loadu_ps(&destination_x[i]);
__m512 desired_v0_i_vec = _mm512_loadu_ps(&desired_v0[i]);
__m512i color_i_vec = _mm512_loadu_si512((__m512i *)&color[i]);
// # 32 MULTs, 16 ADDs
__m512 w_norm_sq = _mm512_fmadd_ps(w_x_i_vec, w_x_i_vec, _mm512_mul_ps(w_y_i_vec, w_y_i_vec));
// # 16 SQRTs, 16 DIVs
__m512 w_norm_inv = _mm512_rsqrt14_ps(w_norm_sq);
// # 16 MULTs
__m512 correction_factor = _mm512_min_ps(_mm512_set1_ps(1.0f), _mm512_mul_ps(v_max_i_vec, w_norm_inv));
// # 32 MULTs
__m512 curr_vel_x_i_vec = _mm512_mul_ps(w_x_i_vec, correction_factor);
__m512 curr_vel_y_i_vec = _mm512_mul_ps(w_y_i_vec, correction_factor);
__m512 dt_vec = _mm512_set1_ps(dt);
// # 32 MULTs, 32 ADDs
pos_x_i_vec = _mm512_fmadd_ps(curr_vel_x_i_vec, dt_vec, pos_x_i_vec);
pos_y_i_vec = _mm512_fmadd_ps(curr_vel_y_i_vec, dt_vec, pos_y_i_vec);
__mmask16 cond_1_left = _mm512_cmpeq_epi32_mask(color_i_vec, _mm512_setzero_si512());
__mmask16 cond_1_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE_POINT_FIVE), _CMP_GT_OQ);
__mmask16 cond_1 = cond_1_left & cond_1_right;
pos_x_i_vec = _mm512_mask_blend_ps(cond_1, pos_x_i_vec, _mm512_set1_ps(1.0f));
destination_y_i_vec = _mm512_mask_blend_ps(cond_1, destination_y_i_vec, pos_y_i_vec);
w_x_i_vec = _mm512_mask_blend_ps(cond_1, w_x_i_vec, desired_v0_i_vec);
w_y_i_vec = _mm512_mask_blend_ps(cond_1, w_y_i_vec, _mm512_setzero_ps());
curr_vel_x_i_vec = _mm512_mask_blend_ps(cond_1, curr_vel_x_i_vec, w_x_i_vec);
curr_vel_y_i_vec = _mm512_mask_blend_ps(cond_1, curr_vel_y_i_vec, w_y_i_vec);
__mmask16 cond_2_left = _mm512_cmpeq_epi32_mask(color_i_vec, _mm512_set1_epi32(1));
__mmask16 cond_2_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(1.5f), _CMP_LT_OQ);
__mmask16 cond_2 = cond_2_left & cond_2_right;
pos_x_i_vec = _mm512_mask_blend_ps(cond_2, pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE));
destination_y_i_vec = _mm512_mask_blend_ps(cond_2, destination_y_i_vec, pos_y_i_vec);
__m512 neg_desired_v0 = _mm512_xor_ps(desired_v0_i_vec, _mm512_set1_ps(-0.0f));
w_x_i_vec = _mm512_mask_blend_ps(cond_2, w_x_i_vec, neg_desired_v0);
w_y_i_vec = _mm512_mask_blend_ps(cond_2, w_y_i_vec, _mm512_setzero_ps());
curr_vel_x_i_vec = _mm512_mask_blend_ps(cond_2, curr_vel_x_i_vec, w_x_i_vec);
curr_vel_y_i_vec = _mm512_mask_blend_ps(cond_2, curr_vel_y_i_vec, w_y_i_vec);
pos_y_i_vec = _mm512_max_ps(pos_y_i_vec, _mm512_set1_ps(WALL_EPS));
pos_y_i_vec = _mm512_min_ps(pos_y_i_vec, _mm512_set1_ps(BOARD_WIDTH_MINUS_WALL_EPS));
// # 32 ADDs
__m512 ex_vec = _mm512_sub_ps(destination_x_i_vec, pos_x_i_vec);
__m512 ey_vec = _mm512_sub_ps(destination_y_i_vec, pos_y_i_vec);
// # 32 MULTs, 16 ADDs
__m512 e_norm_sq = _mm512_fmadd_ps(ex_vec, ex_vec, _mm512_mul_ps(ey_vec, ey_vec));
// # 16 SQRTs, 16 DIVs
__m512 e_norm_inv = _mm512_rsqrt14_ps(e_norm_sq);
// # 32 MULTs
__m512 e_x_i_vec = _mm512_mul_ps(ex_vec, e_norm_inv);
__m512 e_y_i_vec = _mm512_mul_ps(ey_vec, e_norm_inv);
// # 32 MULTs, 16 ADDs
__m512 vel_norm_sq = _mm512_fmadd_ps(curr_vel_x_i_vec, curr_vel_x_i_vec, _mm512_mul_ps(curr_vel_y_i_vec, curr_vel_y_i_vec));
// # 16 MULTs
__m512 sb_sq_i_vec = _mm512_mul_ps(vel_norm_sq, _mm512_set1_ps(delta_t_sq));
// # 16 SQRTs
__m512 vel_norm_dt = _mm512_sqrt_ps(sb_sq_i_vec);
_mm512_storeu_ps(&curr_vel_x[i], curr_vel_x_i_vec);
_mm512_storeu_ps(&curr_vel_y[i], curr_vel_y_i_vec);
_mm512_storeu_ps(&w_x[i], w_x_i_vec);
_mm512_storeu_ps(&w_y[i], w_y_i_vec);
_mm512_storeu_ps(&pos_x[i], pos_x_i_vec);
_mm512_storeu_ps(&pos_y[i], pos_y_i_vec);
_mm512_storeu_ps(&e_x[i], e_x_i_vec);
_mm512_storeu_ps(&e_y[i], e_y_i_vec);
_mm512_storeu_ps(&destination_y[i], destination_y_i_vec);
_mm512_storeu_ps(&sb_sq[i], sb_sq_i_vec);
// # 32 MULTs
_mm512_storeu_ps(&sb_e_x[i], _mm512_mul_ps(e_x_i_vec, vel_norm_dt));
_mm512_storeu_ps(&sb_e_y[i], _mm512_mul_ps(e_y_i_vec, vel_norm_dt));
}
#if !BENCHMARK
for (int i = 0; i < n_p; i++) {
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;
}
@@ -1,5 +1,5 @@
// # Changes: - used _mm512_mask3_fmadd_ps, to save one masking instruction
// - avoid doing an expensive brodcast for index vectors update, update them through additions instead
// - avoid doing an expensive broadcast for index vectors update, update them through additions instead
// - replace array allocation + load with a direct set for lane_off initialization
// # Predecessor: variant_float_inlining_SoA_AVX512_polyexp_moreavx_ilp_2_rsqrt_morersqrt_expfix.cpp
@@ -13,6 +13,8 @@
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
@@ -160,21 +162,46 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// # 256 MULTs, 224 ADDs
static inline __m512 _avx512_exp_ps(__m512 x) {
const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
// # 16 MULTs
__m512 nf = _mm512_roundscale_ps(_mm512_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
const __m512 ln2 = _mm512_set1_ps(0.6931471805599453f);
// # 16 MULTs, 16 ADDs
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
const __m512 c5 = _mm512_set1_ps(1.0f / 120.0f);
const __m512 c4 = _mm512_set1_ps(1.0f / 24.0f);
const __m512 c3 = _mm512_set1_ps(1.0f / 6.0f);
const __m512 c2 = _mm512_set1_ps(0.5f);
const __m512 c1 = _mm512_set1_ps(1.0f);
const __m512 c0 = _mm512_set1_ps(1.0f);
const __m512 c13 = _mm512_set1_ps(c13_scalar);
const __m512 c12 = _mm512_set1_ps(c12_scalar);
const __m512 c11 = _mm512_set1_ps(c11_scalar);
const __m512 c10 = _mm512_set1_ps(c10_scalar);
const __m512 c9 = _mm512_set1_ps(c9_scalar);
const __m512 c8 = _mm512_set1_ps(c8_scalar);
const __m512 c7 = _mm512_set1_ps(c7_scalar);
const __m512 c6 = _mm512_set1_ps(c6_scalar);
const __m512 c5 = _mm512_set1_ps(c5_scalar);
const __m512 c4 = _mm512_set1_ps(c4_scalar);
const __m512 c3 = _mm512_set1_ps(c3_scalar);
const __m512 c2 = _mm512_set1_ps(c2_scalar);
const __m512 c1 = _mm512_set1_ps(c1_scalar);
const __m512 c0 = _mm512_set1_ps(c0_scalar);
__m512 p = c5;
__m512 p = c13;
// # 13 * (16 MULTs, 16 ADDs)
// # = 208 MULTs, 208 ADDs
p = _mm512_fmadd_ps(p, r, c12);
p = _mm512_fmadd_ps(p, r, c11);
p = _mm512_fmadd_ps(p, r, c10);
p = _mm512_fmadd_ps(p, r, c9);
p = _mm512_fmadd_ps(p, r, c8);
p = _mm512_fmadd_ps(p, r, c7);
p = _mm512_fmadd_ps(p, r, c6);
p = _mm512_fmadd_ps(p, r, c5);
p = _mm512_fmadd_ps(p, r, c4);
p = _mm512_fmadd_ps(p, r, c3);
p = _mm512_fmadd_ps(p, r, c2);
@@ -186,6 +213,7 @@ static inline __m512 _avx512_exp_ps(__m512 x) {
__m512i scaled = _mm512_slli_epi32(biased, 23);
__m512 pow2n = _mm512_castsi512_ps(scaled);
// # 16 MULTs
return _mm512_mul_ps(p, pow2n);
}
@@ -224,11 +252,42 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
const __m512 v_np = _mm512_set1_ps((float)n_p);
const __m512 v_exp_min = _mm512_set1_ps(-87.0f);
// # ADDs: n_iter * (
// # n_p * 132 +
// # n_p * n_p_pad * 29 +
// # n_p * board.n_obstacles * 10 +
// # n_p_pad * 7
// # )
// # MULTs: n_iter * (
// # n_p * 4 +
// # n_p * n_p_pad * 40 +
// # n_p * board.n_obstacles * 13 +
// # n_p_pad * 15
// # )
// # DIVs: n_iter * (
// # n_p * n_p_pad +
// # n_p * board.n_obstacles +
// # n_p_pad * 2
// # )
// # SQRTs: n_iter * (
// # n_p * n_p_pad * 3 +
// # n_p * board.n_obstacles +
// # n_p_pad * 3
// # )
// # EXPs: n_iter * n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
// # 2 MULTs
float desired_velocity_x = e_x[i] * desired_v0[i];
float desired_velocity_y = e_y[i] * desired_v0[i];
// # 2 ADDs, 2 MULTs
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
@@ -249,6 +308,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 acc_y_3 = _mm512_setzero_ps();
for (int j = 0; j < n_p_pad; j += 64) {
// # 64 ADDs
__m512 j_idx_0 = _mm512_add_ps(_mm512_set1_ps((float)j), lane_off);
__m512 j_idx_1 = _mm512_add_ps(_mm512_set1_ps((float)j + 16), lane_off);
__m512 j_idx_2 = _mm512_add_ps(_mm512_set1_ps((float)j + 32), lane_off);
@@ -294,61 +355,73 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 sb_jsq_2 = _mm512_loadu_ps(&sb_sq[j + 32]);
__m512 sb_jsq_3 = _mm512_loadu_ps(&sb_sq[j + 48]);
// # 64 ADDs
__m512 r_ab_x_0 = _mm512_sub_ps(pix, pjx_0);
__m512 r_ab_x_1 = _mm512_sub_ps(pix, pjx_1);
__m512 r_ab_x_2 = _mm512_sub_ps(pix, pjx_2);
__m512 r_ab_x_3 = _mm512_sub_ps(pix, pjx_3);
// # 64 ADDs
__m512 r_ab_y_0 = _mm512_sub_ps(piy, pjy_0);
__m512 r_ab_y_1 = _mm512_sub_ps(piy, pjy_1);
__m512 r_ab_y_2 = _mm512_sub_ps(piy, pjy_2);
__m512 r_ab_y_3 = _mm512_sub_ps(piy, pjy_3);
// # 128 MULTs, 64 ADDs
__m512 r_ab_n2_0 = _mm512_fmadd_ps(r_ab_x_0, r_ab_x_0, _mm512_mul_ps(r_ab_y_0, r_ab_y_0));
__m512 r_ab_n2_1 = _mm512_fmadd_ps(r_ab_x_1, r_ab_x_1, _mm512_mul_ps(r_ab_y_1, r_ab_y_1));
__m512 r_ab_n2_2 = _mm512_fmadd_ps(r_ab_x_2, r_ab_x_2, _mm512_mul_ps(r_ab_y_2, r_ab_y_2));
__m512 r_ab_n2_3 = _mm512_fmadd_ps(r_ab_x_3, r_ab_x_3, _mm512_mul_ps(r_ab_y_3, r_ab_y_3));
// # 64 SQRTs
__m512 r_ab_n_0 = _mm512_sqrt_ps(r_ab_n2_0);
__m512 r_ab_n_1 = _mm512_sqrt_ps(r_ab_n2_1);
__m512 r_ab_n_2 = _mm512_sqrt_ps(r_ab_n2_2);
__m512 r_ab_n_3 = _mm512_sqrt_ps(r_ab_n2_3);
// # 64 ADDs
__m512 r_ab2_x_0 = _mm512_sub_ps(r_ab_x_0, sbe_jx_0);
__m512 r_ab2_x_1 = _mm512_sub_ps(r_ab_x_1, sbe_jx_1);
__m512 r_ab2_x_2 = _mm512_sub_ps(r_ab_x_2, sbe_jx_2);
__m512 r_ab2_x_3 = _mm512_sub_ps(r_ab_x_3, sbe_jx_3);
// # 64 ADDs
__m512 r_ab2_y_0 = _mm512_sub_ps(r_ab_y_0, sbe_jy_0);
__m512 r_ab2_y_1 = _mm512_sub_ps(r_ab_y_1, sbe_jy_1);
__m512 r_ab2_y_2 = _mm512_sub_ps(r_ab_y_2, sbe_jy_2);
__m512 r_ab2_y_3 = _mm512_sub_ps(r_ab_y_3, sbe_jy_3);
// # 128 MULTs, 64 ADDs
__m512 r_ab2_n2_0 = _mm512_fmadd_ps(r_ab2_x_0, r_ab2_x_0, _mm512_mul_ps(r_ab2_y_0, r_ab2_y_0));
__m512 r_ab2_n2_1 = _mm512_fmadd_ps(r_ab2_x_1, r_ab2_x_1, _mm512_mul_ps(r_ab2_y_1, r_ab2_y_1));
__m512 r_ab2_n2_2 = _mm512_fmadd_ps(r_ab2_x_2, r_ab2_x_2, _mm512_mul_ps(r_ab2_y_2, r_ab2_y_2));
__m512 r_ab2_n2_3 = _mm512_fmadd_ps(r_ab2_x_3, r_ab2_x_3, _mm512_mul_ps(r_ab2_y_3, r_ab2_y_3));
// # 64 SQRTs
__m512 r_ab2_n_0 = _mm512_sqrt_ps(r_ab2_n2_0);
__m512 r_ab2_n_1 = _mm512_sqrt_ps(r_ab2_n2_1);
__m512 r_ab2_n_2 = _mm512_sqrt_ps(r_ab2_n2_2);
__m512 r_ab2_n_3 = _mm512_sqrt_ps(r_ab2_n2_3);
// # 64 ADDs
__m512 r_sum_0 = _mm512_add_ps(r_ab_n_0, r_ab2_n_0);
__m512 r_sum_1 = _mm512_add_ps(r_ab_n_1, r_ab2_n_1);
__m512 r_sum_2 = _mm512_add_ps(r_ab_n_2, r_ab2_n_2);
__m512 r_sum_3 = _mm512_add_ps(r_ab_n_3, r_ab2_n_3);
// # 64 MULTs, 64 ADDs
__m512 two_b_sq_0 = _mm512_fmsub_ps(r_sum_0, r_sum_0, sb_jsq_0);
__m512 two_b_sq_1 = _mm512_fmsub_ps(r_sum_1, r_sum_1, sb_jsq_1);
__m512 two_b_sq_2 = _mm512_fmsub_ps(r_sum_2, r_sum_2, sb_jsq_2);
__m512 two_b_sq_3 = _mm512_fmsub_ps(r_sum_3, r_sum_3, sb_jsq_3);
// # 64 SQRTs
__m512 two_b_0 = _mm512_sqrt_ps(two_b_sq_0);
__m512 two_b_1 = _mm512_sqrt_ps(two_b_sq_1);
__m512 two_b_2 = _mm512_sqrt_ps(two_b_sq_2);
__m512 two_b_3 = _mm512_sqrt_ps(two_b_sq_3);
// # 64 MULTs
__m512 e_arg_0 = _mm512_mul_ps(two_b_0, v_neg_2s_inv);
__m512 e_arg_1 = _mm512_mul_ps(two_b_1, v_neg_2s_inv);
__m512 e_arg_2 = _mm512_mul_ps(two_b_2, v_neg_2s_inv);
@@ -359,16 +432,19 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
e_arg_2 = _mm512_max_ps(e_arg_2, v_exp_min);
e_arg_3 = _mm512_max_ps(e_arg_3, v_exp_min);
// # 1024 MULTs, 896 ADDs
__m512 e_val_0 = _avx512_exp_ps(e_arg_0);
__m512 e_val_1 = _avx512_exp_ps(e_arg_1);
__m512 e_val_2 = _avx512_exp_ps(e_arg_2);
__m512 e_val_3 = _avx512_exp_ps(e_arg_3);
// # 128 MULTs
__m512 numer_0 = _mm512_mul_ps(_mm512_mul_ps(r_sum_0, v_neg_v0_div_2s), e_val_0);
__m512 numer_1 = _mm512_mul_ps(_mm512_mul_ps(r_sum_1, v_neg_v0_div_2s), e_val_1);
__m512 numer_2 = _mm512_mul_ps(_mm512_mul_ps(r_sum_2, v_neg_v0_div_2s), e_val_2);
__m512 numer_3 = _mm512_mul_ps(_mm512_mul_ps(r_sum_3, v_neg_v0_div_2s), e_val_3);
// # 128 MULTs
__m512 denom_0 = _mm512_mul_ps(_mm512_mul_ps(two_b_0, r_ab_n_0), r_ab2_n_0);
__m512 denom_1 = _mm512_mul_ps(_mm512_mul_ps(two_b_1, r_ab_n_1), r_ab2_n_1);
__m512 denom_2 = _mm512_mul_ps(_mm512_mul_ps(two_b_2, r_ab_n_2), r_ab2_n_2);
@@ -379,46 +455,55 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 denom_safe_2 = _mm512_mask_blend_ps(active_2, v_one, denom_2);
__m512 denom_safe_3 = _mm512_mask_blend_ps(active_3, v_one, denom_3);
// # 64 DIVs
__m512 smc_0 = _mm512_div_ps(numer_0, denom_safe_0);
__m512 smc_1 = _mm512_div_ps(numer_1, denom_safe_1);
__m512 smc_2 = _mm512_div_ps(numer_2, denom_safe_2);
__m512 smc_3 = _mm512_div_ps(numer_3, denom_safe_3);
// # 64 MULTs
__m512 sm_r_ab_0 = _mm512_mul_ps(smc_0, r_ab2_n_0);
__m512 sm_r_ab_1 = _mm512_mul_ps(smc_1, r_ab2_n_1);
__m512 sm_r_ab_2 = _mm512_mul_ps(smc_2, r_ab2_n_2);
__m512 sm_r_ab_3 = _mm512_mul_ps(smc_3, r_ab2_n_3);
// # 64 MULTs
__m512 sm_r_ab2_0 = _mm512_mul_ps(smc_0, r_ab_n_0);
__m512 sm_r_ab2_1 = _mm512_mul_ps(smc_1, r_ab_n_1);
__m512 sm_r_ab2_2 = _mm512_mul_ps(smc_2, r_ab_n_2);
__m512 sm_r_ab2_3 = _mm512_mul_ps(smc_3, r_ab_n_3);
// # 128 MULTs, 64 ADDs
__m512 f_x_0 = _mm512_fmadd_ps(r_ab2_x_0, sm_r_ab2_0, _mm512_mul_ps(r_ab_x_0, sm_r_ab_0));
__m512 f_x_1 = _mm512_fmadd_ps(r_ab2_x_1, sm_r_ab2_1, _mm512_mul_ps(r_ab_x_1, sm_r_ab_1));
__m512 f_x_2 = _mm512_fmadd_ps(r_ab2_x_2, sm_r_ab2_2, _mm512_mul_ps(r_ab_x_2, sm_r_ab_2));
__m512 f_x_3 = _mm512_fmadd_ps(r_ab2_x_3, sm_r_ab2_3, _mm512_mul_ps(r_ab_x_3, sm_r_ab_3));
// # 128 MULTs, 64 ADDs
__m512 f_y_0 = _mm512_fmadd_ps(r_ab2_y_0, sm_r_ab2_0, _mm512_mul_ps(r_ab_y_0, sm_r_ab_0));
__m512 f_y_1 = _mm512_fmadd_ps(r_ab2_y_1, sm_r_ab2_1, _mm512_mul_ps(r_ab_y_1, sm_r_ab_1));
__m512 f_y_2 = _mm512_fmadd_ps(r_ab2_y_2, sm_r_ab2_2, _mm512_mul_ps(r_ab_y_2, sm_r_ab_2));
__m512 f_y_3 = _mm512_fmadd_ps(r_ab2_y_3, sm_r_ab2_3, _mm512_mul_ps(r_ab_y_3, sm_r_ab_3));
// # 128 MULTs, 64 ADDs
__m512 f_norm_sq_0 = _mm512_fmadd_ps(f_x_0, f_x_0, _mm512_mul_ps(f_y_0, f_y_0));
__m512 f_norm_sq_1 = _mm512_fmadd_ps(f_x_1, f_x_1, _mm512_mul_ps(f_y_1, f_y_1));
__m512 f_norm_sq_2 = _mm512_fmadd_ps(f_x_2, f_x_2, _mm512_mul_ps(f_y_2, f_y_2));
__m512 f_norm_sq_3 = _mm512_fmadd_ps(f_x_3, f_x_3, _mm512_mul_ps(f_y_3, f_y_3));
// # 64 MULTs
__m512 rhs_0 = _mm512_mul_ps(f_norm_sq_0, v_cos_phi_sq);
__m512 rhs_1 = _mm512_mul_ps(f_norm_sq_1, v_cos_phi_sq);
__m512 rhs_2 = _mm512_mul_ps(f_norm_sq_2, v_cos_phi_sq);
__m512 rhs_3 = _mm512_mul_ps(f_norm_sq_3, v_cos_phi_sq);
// # 128 MULTs, 64 ADDs
__m512 e_dot_f_0 = _mm512_fmadd_ps(eix, f_x_0, _mm512_mul_ps(eiy, f_y_0));
__m512 e_dot_f_1 = _mm512_fmadd_ps(eix, f_x_1, _mm512_mul_ps(eiy, f_y_1));
__m512 e_dot_f_2 = _mm512_fmadd_ps(eix, f_x_2, _mm512_mul_ps(eiy, f_y_2));
__m512 e_dot_f_3 = _mm512_fmadd_ps(eix, f_x_3, _mm512_mul_ps(eiy, f_y_3));
// # 64 MULTs
__m512 e_dot_f_sq_0 = _mm512_mul_ps(e_dot_f_0, e_dot_f_0);
__m512 e_dot_f_sq_1 = _mm512_mul_ps(e_dot_f_1, e_dot_f_1);
__m512 e_dot_f_sq_2 = _mm512_mul_ps(e_dot_f_2, e_dot_f_2);
@@ -454,26 +539,31 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 f_y_safe_2 = _mm512_maskz_mov_ps(active_2, f_y_2);
__m512 f_y_safe_3 = _mm512_maskz_mov_ps(active_3, f_y_3);
// # 64 MULTs, 64 ADDs
acc_x_0 = _mm512_fmadd_ps(f_x_safe_0, w_val_0, acc_x_0);
acc_x_1 = _mm512_fmadd_ps(f_x_safe_1, w_val_1, acc_x_1);
acc_x_2 = _mm512_fmadd_ps(f_x_safe_2, w_val_2, acc_x_2);
acc_x_3 = _mm512_fmadd_ps(f_x_safe_3, w_val_3, acc_x_3);
// # 64 MULTs, 64 ADDs
acc_y_0 = _mm512_fmadd_ps(f_y_safe_0, w_val_0, acc_y_0);
acc_y_1 = _mm512_fmadd_ps(f_y_safe_1, w_val_1, acc_y_1);
acc_y_2 = _mm512_fmadd_ps(f_y_safe_2, w_val_2, acc_y_2);
acc_y_3 = _mm512_fmadd_ps(f_y_safe_3, w_val_3, acc_y_3);
}
// # 128 ADDs
f_alpha_dt_x += _mm512_reduce_add_ps(_mm512_add_ps(_mm512_add_ps(acc_x_0, acc_x_1), _mm512_add_ps(acc_x_2, acc_x_3)));
f_alpha_dt_y += _mm512_reduce_add_ps(_mm512_add_ps(_mm512_add_ps(acc_y_0, acc_y_1), _mm512_add_ps(acc_y_2, acc_y_3)));
for (int k = 0; k < board.n_obstacles; k++) {
// # 2 ADDs
float v1_x = pos_x[i] - board.obstacles[k].start_x;
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
// # 3 MULTs, 1 ADD
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
if (projection < 0.0f)
@@ -481,12 +571,15 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
else if (projection > 1.0f)
projection = 1.0f;
// # 4 ADDs, 2 MULTs
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
// # 1 SQRT, 2 MULTs, 1 ADD
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
// # 1 EXP, 4 MULTs, 1 DIV
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
@@ -497,19 +590,22 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
if (e_dot_f < cos_phi)
w_val = c_neg_times_dt;
// # 2 MULTs
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
// # 2 ADDs
f_alpha_dt_x += f_alpha_B_dt_x;
f_alpha_dt_y += f_alpha_B_dt_y;
}
// # 2 ADDs
w_x[i] += f_alpha_dt_x;
w_y[i] += f_alpha_dt_y;
}
for (int i = 0; i < n_p_pad; i += 16) {
__m512 w_x_i_vec = _mm512_loadu_ps(&w_x[i]);
__m512 w_y_i_vec = _mm512_loadu_ps(&w_y[i]);
__m512 v_max_i_vec = _mm512_loadu_ps(&v_max[i]);
@@ -518,22 +614,30 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512 destination_y_i_vec = _mm512_loadu_ps(&destination_y[i]);
__m512 destination_x_i_vec = _mm512_loadu_ps(&destination_x[i]);
__m512 desired_v0_i_vec = _mm512_loadu_ps(&desired_v0[i]);
__m512i color_i_vec = _mm512_loadu_si512((__m512i*)&color[i]);
__m512i color_i_vec = _mm512_loadu_si512((__m512i *)&color[i]);
// # 32 MULTs, 16 ADDs
__m512 w_norm_sq = _mm512_fmadd_ps(w_x_i_vec, w_x_i_vec, _mm512_mul_ps(w_y_i_vec, w_y_i_vec));
// # 16 SQRTs
__m512 w_norm = _mm512_sqrt_ps(w_norm_sq);
// # 16 DIVs
__m512 correction_factor = _mm512_min_ps(_mm512_set1_ps(1.0f), _mm512_div_ps(v_max_i_vec, w_norm));
// # 32 MULTs
__m512 curr_vel_x_i_vec = _mm512_mul_ps(w_x_i_vec, correction_factor);
__m512 curr_vel_y_i_vec = _mm512_mul_ps(w_y_i_vec, correction_factor);
__m512 dt_vec = _mm512_set1_ps(dt);
// # 32 MULTs, 32 ADDs
pos_x_i_vec = _mm512_fmadd_ps(curr_vel_x_i_vec, dt_vec, pos_x_i_vec);
pos_y_i_vec = _mm512_fmadd_ps(curr_vel_y_i_vec, dt_vec, pos_y_i_vec);
__mmask16 cond_1_left = _mm512_cmpeq_epi32_mask(color_i_vec, _mm512_setzero_si512());
__mmask16 cond_1_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH - 1.5f), _CMP_GT_OQ);
__mmask16 cond_1_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE_POINT_FIVE), _CMP_GT_OQ);
__mmask16 cond_1 = cond_1_left & cond_1_right;
pos_x_i_vec = _mm512_mask_blend_ps(cond_1, pos_x_i_vec, _mm512_set1_ps(1.0f));
@@ -547,7 +651,7 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__mmask16 cond_2_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(1.5f), _CMP_LT_OQ);
__mmask16 cond_2 = cond_2_left & cond_2_right;
pos_x_i_vec = _mm512_mask_blend_ps(cond_2, pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH - 1.0f));
pos_x_i_vec = _mm512_mask_blend_ps(cond_2, pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE));
destination_y_i_vec = _mm512_mask_blend_ps(cond_2, destination_y_i_vec, pos_y_i_vec);
__m512 neg_desired_v0 = _mm512_xor_ps(desired_v0_i_vec, _mm512_set1_ps(-0.0f));
w_x_i_vec = _mm512_mask_blend_ps(cond_2, w_x_i_vec, neg_desired_v0);
@@ -558,16 +662,30 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
pos_y_i_vec = _mm512_max_ps(pos_y_i_vec, _mm512_set1_ps(WALL_EPS));
pos_y_i_vec = _mm512_min_ps(pos_y_i_vec, _mm512_set1_ps(BOARD_WIDTH_MINUS_WALL_EPS));
// # 32 ADDs
__m512 ex_vec = _mm512_sub_ps(destination_x_i_vec, pos_x_i_vec);
__m512 ey_vec = _mm512_sub_ps(destination_y_i_vec, pos_y_i_vec);
// # 32 MULTs, 16 ADDs
__m512 e_norm_sq = _mm512_fmadd_ps(ex_vec, ex_vec, _mm512_mul_ps(ey_vec, ey_vec));
// # 16 SQRTs
__m512 e_norm = _mm512_sqrt_ps(e_norm_sq);
// # 16 DIVs
__m512 e_norm_inv = _mm512_div_ps(_mm512_set1_ps(1.0f), e_norm);
// # 32 MULTs
__m512 e_x_i_vec = _mm512_mul_ps(ex_vec, e_norm_inv);
__m512 e_y_i_vec = _mm512_mul_ps(ey_vec, e_norm_inv);
// # 32 MULTs, 16 ADDs
__m512 vel_norm_sq = _mm512_fmadd_ps(curr_vel_x_i_vec, curr_vel_x_i_vec, _mm512_mul_ps(curr_vel_y_i_vec, curr_vel_y_i_vec));
// # 16 MULTs
__m512 sb_sq_i_vec = _mm512_mul_ps(vel_norm_sq, _mm512_set1_ps(delta_t_sq));
// # 16 SQRTs
__m512 vel_norm_dt = _mm512_sqrt_ps(sb_sq_i_vec);
_mm512_storeu_ps(&curr_vel_x[i], curr_vel_x_i_vec);
@@ -580,11 +698,12 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
_mm512_storeu_ps(&e_y[i], e_y_i_vec);
_mm512_storeu_ps(&destination_y[i], destination_y_i_vec);
_mm512_storeu_ps(&sb_sq[i], sb_sq_i_vec);
// # 32 MULTs
_mm512_storeu_ps(&sb_e_x[i], _mm512_mul_ps(e_x_i_vec, vel_norm_dt));
_mm512_storeu_ps(&sb_e_y[i], _mm512_mul_ps(e_y_i_vec, vel_norm_dt));
}
#if !BENCHMARK
for (int i = 0; i < n_p; i++) {
if (dump_path && (t % dump_interval == 0)) {
File diff suppressed because it is too large Load Diff
@@ -1,703 +0,0 @@
// # Changes: Added ILP for j-loop with K = 2
// # added flopcount
// FLOPCOUNT = n_iter * ( 69 * n_p ** 2 - 24 * n_p + 55 * n_obstacles * n_p )
// # Predecessor: variant_float_inlining_SoA_symmetry.cpp
#include "../headers/board_reduced_float.hpp"
#include "../headers/constants_float.hpp"
#include "../headers/pedestrian_reduced_float.hpp"
#include <algorithm>
#include <fstream>
#include <random>
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
#ifndef RANDOM_SEED
#define RANDOM_SEED 0
#endif
#ifndef BENCHMARK
#define BENCHMARK 0
#endif
using namespace std;
vector<float> pos_x, pos_y;
vector<float> sb_sq;
vector<float> sb_e_x, sb_e_y;
vector<float> e_x, e_y;
vector<float> curr_vel_x, curr_vel_y;
vector<float> w_x, w_y;
vector<float> desired_v0;
vector<float> v_max;
vector<float> 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<float> velocity_gen(v0_mean, v0_std_dev);
uniform_real_distribution<float> y_gen(0.3, BOARD_WIDTH - 0.3);
uniform_real_distribution<float> x_gen_left(2.0, BOARD_LENGTH / 2.0);
uniform_real_distribution<float> x_gen_right(BOARD_LENGTH / 2.0, BOARD_LENGTH - 2.0);
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++) {
float x = x_gen_left(gen);
float y = y_gen(gen);
float dest_x = BOARD_LENGTH + 30.0;
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1);
float 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;
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
for (int i = n_p_half; i < n_p; i++) {
float x = x_gen_right(gen);
float y = y_gen(gen);
float dest_x = -30;
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1);
float 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;
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
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) {
(void)pedestrians;
#if BENCHMARK
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
vector<tuple<int, int, float, float, float, float, 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++) {
samples.emplace_back(0, i, pos_x[i], pos_y[i], e_x[i], e_y[i], color[i]);
}
}
#endif
vector<float> r_ab_x_lut(n_p * n_p, 0.0);
vector<float> r_ab_y_lut(n_p * n_p, 0.0);
vector<float> r_ab_norm_lut(n_p * n_p, 0.0);
// FLOPCOUNT (everything inside the t-loop). EXP kept as its own op (one real exp() per call).
// section costs: overhead 4 ADD 4 MULT (+ reductions 4*K = 8 ADD, K=2) |
// distance pair 3 ADD 2 MULT 1 SQRT |
// ped-ped j 11 ADD 22 MULT 2 SQRT 1 DIV 1 EXP |
// obstacle 10 ADD 13 MULT 1 SQRT 1 DIV 1 EXP | update 7 ADD 17 MULT 3 SQRT 2 DIV
// # ADDs: n_iter * (
// # n_p * 12 +
// # 0.5 * (n_p*n_p - n_p) * 3 +
// # n_p * (n_p - 1) * 11 +
// # n_p * board.n_obstacles * 10 +
// # n_p * 7
// # )
// # MULTs: n_iter * (
// # n_p * 4 +
// # 0.5 * (n_p*n_p - n_p) * 2 +
// # n_p * (n_p - 1) * 22 +
// # n_p * board.n_obstacles * 13 +
// # n_p * 17
// # )
// # DIVs: n_iter * (
// # n_p * (n_p - 1) +
// # n_p * board.n_obstacles +
// # n_p * 2
// # )
// # SQRTs: n_iter * (
// # 0.5 * (n_p*n_p - n_p) +
// # n_p * (n_p - 1) * 2 +
// # n_p * board.n_obstacles +
// # n_p * 3
// # )
// # EXPs: n_iter * (
// # n_p * (n_p - 1) +
// # n_p * board.n_obstacles
// # )
// total with EXP = 30: n_iter * ( 69 * n_p ** 2 - 24 * n_p + 55 * n_obstacles * n_p )
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
// # 1 MULT
float desired_velocity_x = e_x[i] * desired_v0[i];
// # 1 MULT
float desired_velocity_y = e_y[i] * desired_v0[i];
// # 1 ADD, 1 MULT
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
// # 1 ADD, 1 MULT
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
float e_x_i = e_x[i];
float e_y_i = e_y[i];
float pos_x_i = pos_x[i];
float pos_y_i = pos_y[i];
// distance precompute, # each pair: 6 FLOPS, total over all i = 0.5*(n_p*n_p - n_p)*6
for (int j = i + 1; j < n_p; j++) {
// # 1 ADD
float dx = pos_x_i - pos_x[j];
// # 1 ADD
float dy = pos_y_i - pos_y[j];
// # 1 ADD, 2 MULTs, 1 SQRT
float norm = sqrt(dx * dx + dy * dy);
int idx = i * n_p + j;
r_ab_x_lut[idx] = dx;
r_ab_y_lut[idx] = dy;
r_ab_norm_lut[idx] = norm;
}
int j;
float acc_x_0 = 0.0f, acc_x_1 = 0.0f;
float acc_y_0 = 0.0f, acc_y_1 = 0.0f;
// ILP over the j < i half. # each lane = 66 FLOPS (same body as the remainder
// loop below where the per-line breakdown is given), K = 2 lanes per step
for (j = 0; j + 2 < i; j += 2) {
float r_ab_x_0 = -1 * r_ab_x_lut[j * n_p + i];
float r_ab_x_1 = -1 * r_ab_x_lut[(j + 1) * n_p + i];
float r_ab_y_0 = -1 * r_ab_y_lut[j * n_p + i];
float r_ab_y_1 = -1 * r_ab_y_lut[(j + 1) * n_p + i];
float r_ab_norm_0 = r_ab_norm_lut[j * n_p + i];
float r_ab_norm_1 = r_ab_norm_lut[(j + 1) * n_p + i];
r_ab_norm_0 = r_ab_norm_0 > EPS ? r_ab_norm_0 : EPS;
r_ab_norm_1 = r_ab_norm_1 > EPS ? r_ab_norm_1 : EPS;
float r_ab2_x_0 = r_ab_x_0 - sb_e_x[j];
float r_ab2_x_1 = r_ab_x_1 - sb_e_x[j + 1];
float r_ab2_y_0 = r_ab_y_0 - sb_e_y[j];
float r_ab2_y_1 = r_ab_y_1 - sb_e_y[j + 1];
float r_ab2_norm_0 = sqrt(r_ab2_x_0 * r_ab2_x_0 + r_ab2_y_0 * r_ab2_y_0);
float r_ab2_norm_1 = sqrt(r_ab2_x_1 * r_ab2_x_1 + r_ab2_y_1 * r_ab2_y_1);
r_ab2_norm_0 = r_ab2_norm_0 > EPS ? r_ab2_norm_0 : EPS;
r_ab2_norm_1 = r_ab2_norm_1 > EPS ? r_ab2_norm_1 : EPS;
float r_ab_norm_sum_0 = r_ab_norm_0 + r_ab2_norm_0;
float r_ab_norm_sum_1 = r_ab_norm_1 + r_ab2_norm_1;
float two_b_sq_0 = r_ab_norm_sum_0 * r_ab_norm_sum_0 - sb_sq[j];
float two_b_sq_1 = r_ab_norm_sum_1 * r_ab_norm_sum_1 - sb_sq[j + 1];
two_b_sq_0 = two_b_sq_0 > EPS_SQ ? two_b_sq_0 : EPS_SQ;
two_b_sq_1 = two_b_sq_1 > EPS_SQ ? two_b_sq_1 : EPS_SQ;
float two_b_0 = sqrt(two_b_sq_0);
float two_b_1 = sqrt(two_b_sq_1);
float scalar_mult_common_0 = (r_ab_norm_sum_0 / (two_b_0 * r_ab_norm_0 * r_ab2_norm_0)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_0 * NEG_TWO_SIGMA_INV));
float scalar_mult_common_1 = (r_ab_norm_sum_1 / (two_b_1 * r_ab_norm_1 * r_ab2_norm_1)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_1 * NEG_TWO_SIGMA_INV));
float scalar_mult_r_ab_0 = scalar_mult_common_0 * r_ab2_norm_0;
float scalar_mult_r_ab_1 = scalar_mult_common_1 * r_ab2_norm_1;
float scalar_mult_r_ab2_0 = scalar_mult_common_0 * r_ab_norm_0;
float scalar_mult_r_ab2_1 = scalar_mult_common_1 * r_ab_norm_1;
float f_alpha_beta_neg_x_0 = r_ab_x_0 * scalar_mult_r_ab_0 + r_ab2_x_0 * scalar_mult_r_ab2_0;
float f_alpha_beta_neg_x_1 = r_ab_x_1 * scalar_mult_r_ab_1 + r_ab2_x_1 * scalar_mult_r_ab2_1;
float f_alpha_beta_neg_y_0 = r_ab_y_0 * scalar_mult_r_ab_0 + r_ab2_y_0 * scalar_mult_r_ab2_0;
float f_alpha_beta_neg_y_1 = r_ab_y_1 * scalar_mult_r_ab_1 + r_ab2_y_1 * scalar_mult_r_ab2_1;
float w_val_0, w_val_1;
float f_norm_sq_0 = f_alpha_beta_neg_x_0 * f_alpha_beta_neg_x_0 + f_alpha_beta_neg_y_0 * f_alpha_beta_neg_y_0;
float f_norm_sq_1 = f_alpha_beta_neg_x_1 * f_alpha_beta_neg_x_1 + f_alpha_beta_neg_y_1 * f_alpha_beta_neg_y_1;
float rhs_0 = f_norm_sq_0 * cos_phi_sq;
float rhs_1 = f_norm_sq_1 * cos_phi_sq;
float e_dot_f_0 = e_x_i * f_alpha_beta_neg_x_0 + e_y_i * f_alpha_beta_neg_y_0;
float e_dot_f_1 = e_x_i * f_alpha_beta_neg_x_1 + e_y_i * f_alpha_beta_neg_y_1;
float e_dot_f_sq_0 = e_dot_f_0 * e_dot_f_0;
float e_dot_f_sq_1 = e_dot_f_1 * e_dot_f_1;
if (e_dot_f_0 >= 0.0 || e_dot_f_sq_0 <= rhs_0)
w_val_0 = dt_neg;
else
w_val_0 = c_neg_times_dt;
if (e_dot_f_1 >= 0.0 || e_dot_f_sq_1 <= rhs_1)
w_val_1 = dt_neg;
else
w_val_1 = c_neg_times_dt;
acc_x_0 += f_alpha_beta_neg_x_0 * w_val_0;
acc_x_1 += f_alpha_beta_neg_x_1 * w_val_1;
acc_y_0 += f_alpha_beta_neg_y_0 * w_val_0;
acc_y_1 += f_alpha_beta_neg_y_1 * w_val_1;
}
// remainder of the j < i half, single lane. # one j = 66 FLOPS
for (; j < i; j++) {
// lut lookup + -1* sign flip: not counted
float r_ab_x = -1 * r_ab_x_lut[j * n_p + i];
float r_ab_y = -1 * r_ab_y_lut[j * n_p + i];
float r_ab_norm = r_ab_norm_lut[j * n_p + i];
r_ab_norm = r_ab_norm > EPS ? r_ab_norm : EPS;
// # 1 ADD
float r_ab2_x = r_ab_x - sb_e_x[j];
// # 1 ADD
float r_ab2_y = r_ab_y - sb_e_y[j];
// # 1 ADD, 2 MULTs, 1 SQRT
float r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
r_ab2_norm = r_ab2_norm > EPS ? r_ab2_norm : EPS;
// # 1 ADD
float r_ab_norm_sum = r_ab_norm + r_ab2_norm;
// # 1 ADD, 1 MULT
float two_b_sq = r_ab_norm_sum * r_ab_norm_sum - sb_sq[j];
two_b_sq = two_b_sq > EPS_SQ ? two_b_sq : EPS_SQ;
// # 1 SQRT
float two_b = sqrt(two_b_sq);
// # 5 MULTs, 1 DIV, 1 EXP
float 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));
// # 1 MULT
float scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// # 1 MULT
float scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
// # 1 ADD, 2 MULTs
float f_alpha_beta_neg_x = r_ab_x * scalar_mult_r_ab + r_ab2_x * scalar_mult_r_ab2;
// # 1 ADD, 2 MULTs
float f_alpha_beta_neg_y = r_ab_y * scalar_mult_r_ab + r_ab2_y * scalar_mult_r_ab2;
float w_val;
// # 1 ADD, 2 MULTs
float f_norm_sq = f_alpha_beta_neg_x * f_alpha_beta_neg_x + f_alpha_beta_neg_y * f_alpha_beta_neg_y;
// # 1 MULT
float rhs = f_norm_sq * cos_phi_sq;
// # 1 ADD, 2 MULTs
float e_dot_f = e_x[i] * f_alpha_beta_neg_x + e_y[i] * f_alpha_beta_neg_y;
// # 1 MULT
float e_dot_f_sq = e_dot_f * e_dot_f;
if (e_dot_f >= 0.0 || e_dot_f_sq <= rhs)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
// # 1 ADD, 1 MULT
f_alpha_dt_x += f_alpha_beta_neg_x * w_val;
// # 1 ADD, 1 MULT
f_alpha_dt_y += f_alpha_beta_neg_y * w_val;
}
// reduction of the j < i accumulators. # K adds per line (K=2) => 4 ADDs
f_alpha_dt_x += acc_x_0 + acc_x_1;
f_alpha_dt_y += acc_y_0 + acc_y_1;
acc_x_0 = 0.0f; acc_x_1 = 0.0f;
acc_y_0 = 0.0f; acc_y_1 = 0.0f;
// ILP over the j > i half. # each lane = 66 FLOPS, K = 2 lanes per step
for (j = i + 1; j + 2 < n_p; j += 2) {
float r_ab_x_0 = r_ab_x_lut[i * n_p + j];
float r_ab_x_1 = r_ab_x_lut[i * n_p + j + 1];
float r_ab_y_0 = r_ab_y_lut[i * n_p + j];
float r_ab_y_1 = r_ab_y_lut[i * n_p + j + 1];
float r_ab_norm_0 = r_ab_norm_lut[i * n_p + j];
float r_ab_norm_1 = r_ab_norm_lut[i * n_p + j + 1];
r_ab_norm_0 = r_ab_norm_0 > EPS ? r_ab_norm_0 : EPS;
r_ab_norm_1 = r_ab_norm_1 > EPS ? r_ab_norm_1 : EPS;
float r_ab2_x_0 = r_ab_x_0 - sb_e_x[j];
float r_ab2_x_1 = r_ab_x_1 - sb_e_x[j + 1];
float r_ab2_y_0 = r_ab_y_0 - sb_e_y[j];
float r_ab2_y_1 = r_ab_y_1 - sb_e_y[j + 1];
float r_ab2_norm_0 = sqrt(r_ab2_x_0 * r_ab2_x_0 + r_ab2_y_0 * r_ab2_y_0);
float r_ab2_norm_1 = sqrt(r_ab2_x_1 * r_ab2_x_1 + r_ab2_y_1 * r_ab2_y_1);
r_ab2_norm_0 = r_ab2_norm_0 > EPS ? r_ab2_norm_0 : EPS;
r_ab2_norm_1 = r_ab2_norm_1 > EPS ? r_ab2_norm_1 : EPS;
float r_ab_norm_sum_0 = r_ab_norm_0 + r_ab2_norm_0;
float r_ab_norm_sum_1 = r_ab_norm_1 + r_ab2_norm_1;
float two_b_sq_0 = r_ab_norm_sum_0 * r_ab_norm_sum_0 - sb_sq[j];
float two_b_sq_1 = r_ab_norm_sum_1 * r_ab_norm_sum_1 - sb_sq[j + 1];
two_b_sq_0 = two_b_sq_0 > EPS_SQ ? two_b_sq_0 : EPS_SQ;
two_b_sq_1 = two_b_sq_1 > EPS_SQ ? two_b_sq_1 : EPS_SQ;
float two_b_0 = sqrt(two_b_sq_0);
float two_b_1 = sqrt(two_b_sq_1);
float scalar_mult_common_0 = (r_ab_norm_sum_0 / (two_b_0 * r_ab_norm_0 * r_ab2_norm_0)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_0 * NEG_TWO_SIGMA_INV));
float scalar_mult_common_1 = (r_ab_norm_sum_1 / (two_b_1 * r_ab_norm_1 * r_ab2_norm_1)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_1 * NEG_TWO_SIGMA_INV));
float scalar_mult_r_ab_0 = scalar_mult_common_0 * r_ab2_norm_0;
float scalar_mult_r_ab_1 = scalar_mult_common_1 * r_ab2_norm_1;
float scalar_mult_r_ab2_0 = scalar_mult_common_0 * r_ab_norm_0;
float scalar_mult_r_ab2_1 = scalar_mult_common_1 * r_ab_norm_1;
float f_alpha_beta_neg_x_0 = r_ab_x_0 * scalar_mult_r_ab_0 + r_ab2_x_0 * scalar_mult_r_ab2_0;
float f_alpha_beta_neg_x_1 = r_ab_x_1 * scalar_mult_r_ab_1 + r_ab2_x_1 * scalar_mult_r_ab2_1;
float f_alpha_beta_neg_y_0 = r_ab_y_0 * scalar_mult_r_ab_0 + r_ab2_y_0 * scalar_mult_r_ab2_0;
float f_alpha_beta_neg_y_1 = r_ab_y_1 * scalar_mult_r_ab_1 + r_ab2_y_1 * scalar_mult_r_ab2_1;
float w_val_0, w_val_1;
float f_norm_sq_0 = f_alpha_beta_neg_x_0 * f_alpha_beta_neg_x_0 + f_alpha_beta_neg_y_0 * f_alpha_beta_neg_y_0;
float f_norm_sq_1 = f_alpha_beta_neg_x_1 * f_alpha_beta_neg_x_1 + f_alpha_beta_neg_y_1 * f_alpha_beta_neg_y_1;
float rhs_0 = f_norm_sq_0 * cos_phi_sq;
float rhs_1 = f_norm_sq_1 * cos_phi_sq;
float e_dot_f_0 = e_x_i * f_alpha_beta_neg_x_0 + e_y_i * f_alpha_beta_neg_y_0;
float e_dot_f_1 = e_x_i * f_alpha_beta_neg_x_1 + e_y_i * f_alpha_beta_neg_y_1;
float e_dot_f_sq_0 = e_dot_f_0 * e_dot_f_0;
float e_dot_f_sq_1 = e_dot_f_1 * e_dot_f_1;
if (e_dot_f_0 >= 0.0 || e_dot_f_sq_0 <= rhs_0)
w_val_0 = dt_neg;
else
w_val_0 = c_neg_times_dt;
if (e_dot_f_1 >= 0.0 || e_dot_f_sq_1 <= rhs_1)
w_val_1 = dt_neg;
else
w_val_1 = c_neg_times_dt;
acc_x_0 += f_alpha_beta_neg_x_0 * w_val_0;
acc_x_1 += f_alpha_beta_neg_x_1 * w_val_1;
acc_y_0 += f_alpha_beta_neg_y_0 * w_val_0;
acc_y_1 += f_alpha_beta_neg_y_1 * w_val_1;
}
// remainder of the j > i half, single lane. # one j = 66 FLOPS
for (; j < n_p; j++) {
float r_ab_x = r_ab_x_lut[i * n_p + j];
float r_ab_y = r_ab_y_lut[i * n_p + j];
float r_ab_norm = r_ab_norm_lut[i * n_p + j];
r_ab_norm = r_ab_norm > EPS ? r_ab_norm : EPS;
// # 1 ADD
float r_ab2_x = r_ab_x - sb_e_x[j];
// # 1 ADD
float r_ab2_y = r_ab_y - sb_e_y[j];
// # 1 ADD, 2 MULTs, 1 SQRT
float r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
r_ab2_norm = r_ab2_norm > EPS ? r_ab2_norm : EPS;
// # 1 ADD
float r_ab_norm_sum = r_ab_norm + r_ab2_norm;
// # 1 ADD, 1 MULT
float two_b_sq = r_ab_norm_sum * r_ab_norm_sum - sb_sq[j];
two_b_sq = two_b_sq > EPS_SQ ? two_b_sq : EPS_SQ;
// # 1 SQRT
float two_b = sqrt(two_b_sq);
// # 5 MULTs, 1 DIV, 1 EXP
float 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));
// # 1 MULT
float scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// # 1 MULT
float scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
// # 1 ADD, 2 MULTs
float f_alpha_beta_neg_x = r_ab_x * scalar_mult_r_ab + r_ab2_x * scalar_mult_r_ab2;
// # 1 ADD, 2 MULTs
float f_alpha_beta_neg_y = r_ab_y * scalar_mult_r_ab + r_ab2_y * scalar_mult_r_ab2;
float w_val;
// # 1 ADD, 2 MULTs
float f_norm_sq = f_alpha_beta_neg_x * f_alpha_beta_neg_x + f_alpha_beta_neg_y * f_alpha_beta_neg_y;
// # 1 MULT
float rhs = f_norm_sq * cos_phi_sq;
// # 1 ADD, 2 MULTs
float e_dot_f = e_x[i] * f_alpha_beta_neg_x + e_y[i] * f_alpha_beta_neg_y;
// # 1 MULT
float e_dot_f_sq = e_dot_f * e_dot_f;
if (e_dot_f >= 0.0 || e_dot_f_sq <= rhs)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
// # 1 ADD, 1 MULT
f_alpha_dt_x += f_alpha_beta_neg_x * w_val;
// # 1 ADD, 1 MULT
f_alpha_dt_y += f_alpha_beta_neg_y * w_val;
}
// reduction of the j > i accumulators. # K adds per line (K=2) => 4 ADDs
f_alpha_dt_x += acc_x_0 + acc_x_1;
f_alpha_dt_y += acc_y_0 + acc_y_1;
// force between pedestrian and obstacles
// # one obstacle = 55 FLOPS (w_val sign-based, no a*a / b_sq)
for (int k = 0; k < board.n_obstacles; k++) {
// # 1 ADD
float v1_x = pos_x[i] - board.obstacles[k].start_x;
// # 1 ADD
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
// # 1 ADD, 3 MULTs
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
if (projection < 0.0)
projection = 0.0;
else if (projection > 1.0)
projection = 1.0;
// # 2 ADDs, 1 MULT
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
// # 2 ADDs, 1 MULT
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
// # 1 ADD, 2 MULTs, 1 SQRT
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
// # 2 MULTs, 1 DIV, 1 EXP
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// # 1 MULT
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
// # 1 MULT
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
float w_val;
// sign select only, no flops
float e_dot_f = (f_alpha_B_neg_y == 0) ? 0 : (f_alpha_B_neg_y < 0 ? -e_y[i] : e_y[i]);
if (e_dot_f >= cos_phi)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
// # 1 MULT
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
// # 1 MULT
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
// # 1 ADD
f_alpha_dt_x += f_alpha_B_dt_x;
// # 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y;
}
// # 1 ADD
w_x[i] += f_alpha_dt_x;
// # 1 ADD
w_y[i] += f_alpha_dt_y;
}
// update each pedestrian
// # 29 FLOPS per pedestrian
for (int i = 0; i < n_p; i++) {
// # 1 ADD, 2 MULTs, 1 SQRT
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]);
float w_norm2 = w_norm > EPS ? w_norm : EPS;
// # 1 DIV
float correction_factor = min(1.0f, v_max[i] / w_norm2);
// # 1 MULT
curr_vel_x[i] = w_x[i] * correction_factor;
// # 1 MULT
curr_vel_y[i] = w_y[i] * correction_factor;
// # 1 ADD, 1 MULT
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt;
// # 1 ADD, 1 MULT
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);
// # 1 ADD
float ex = destination_x[i] - pos_x[i];
// # 1 ADD
float ey = destination_y[i] - pos_y[i];
// # 1 ADD, 2 MULTs, 1 SQRT
float e_norm = sqrt(ex * ex + ey * ey);
float e_norm2 = e_norm > EPS ? e_norm : EPS;
// # 1 DIV
float e_norm_inv = 1.0 / e_norm2;
// # 1 MULT
e_x[i] = ex * e_norm_inv;
// # 1 MULT
e_y[i] = ey * e_norm_inv;
// # 1 ADD, 2 MULTs
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i];
// # 1 SQRT
float velocity_norm = sqrt(velocity_norm_sq);
// # 1 MULT
sb_sq[i] = velocity_norm_sq * delta_t_sq;
// # 2 MULTs (velocity_norm*delta_t written on both lines, counted as is)
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t);
// # 2 MULTs
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;
}
@@ -1,816 +0,0 @@
// # Changes: Added ILP for j-loop with K = 4
// # added flopcount
// FLOPCOUNT = n_iter * ( 69 * n_p ** 2 - 16 * n_p + 55 * n_obstacles * n_p )
// # Predecessor: variant_float_inlining_SoA_symmetry_ILP_2.cpp
#include "../headers/board_reduced_float.hpp"
#include "../headers/constants_float.hpp"
#include "../headers/pedestrian_reduced_float.hpp"
#include <algorithm>
#include <fstream>
#include <random>
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
#ifndef RANDOM_SEED
#define RANDOM_SEED 0
#endif
#ifndef BENCHMARK
#define BENCHMARK 0
#endif
using namespace std;
vector<float> pos_x, pos_y;
vector<float> sb_sq;
vector<float> sb_e_x, sb_e_y;
vector<float> e_x, e_y;
vector<float> curr_vel_x, curr_vel_y;
vector<float> w_x, w_y;
vector<float> desired_v0;
vector<float> v_max;
vector<float> 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<float> velocity_gen(v0_mean, v0_std_dev);
uniform_real_distribution<float> y_gen(0.3, BOARD_WIDTH - 0.3);
uniform_real_distribution<float> x_gen_left(2.0, BOARD_LENGTH / 2.0);
uniform_real_distribution<float> x_gen_right(BOARD_LENGTH / 2.0, BOARD_LENGTH - 2.0);
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++) {
float x = x_gen_left(gen);
float y = y_gen(gen);
float dest_x = BOARD_LENGTH + 30.0;
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1);
float 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;
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
for (int i = n_p_half; i < n_p; i++) {
float x = x_gen_right(gen);
float y = y_gen(gen);
float dest_x = -30;
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1);
float 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;
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
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) {
(void)pedestrians;
#if BENCHMARK
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
vector<tuple<int, int, float, float, float, float, 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++) {
samples.emplace_back(0, i, pos_x[i], pos_y[i], e_x[i], e_y[i], color[i]);
}
}
#endif
vector<float> r_ab_x_lut(n_p * n_p, 0.0);
vector<float> r_ab_y_lut(n_p * n_p, 0.0);
vector<float> r_ab_norm_lut(n_p * n_p, 0.0);
// FLOPCOUNT (everything inside the t-loop). EXP kept as its own op (one real exp() per call).
// section costs: overhead 4 ADD 4 MULT (+ reductions 4*K = 16 ADD, K=4) |
// distance pair 3 ADD 2 MULT 1 SQRT |
// ped-ped j 11 ADD 22 MULT 2 SQRT 1 DIV 1 EXP |
// obstacle 10 ADD 13 MULT 1 SQRT 1 DIV 1 EXP | update 7 ADD 17 MULT 3 SQRT 2 DIV
// # ADDs: n_iter * (
// # n_p * 20 +
// # 0.5 * (n_p*n_p - n_p) * 3 +
// # n_p * (n_p - 1) * 11 +
// # n_p * board.n_obstacles * 10 +
// # n_p * 7
// # )
// # MULTs: n_iter * (
// # n_p * 4 +
// # 0.5 * (n_p*n_p - n_p) * 2 +
// # n_p * (n_p - 1) * 22 +
// # n_p * board.n_obstacles * 13 +
// # n_p * 17
// # )
// # DIVs: n_iter * (
// # n_p * (n_p - 1) +
// # n_p * board.n_obstacles +
// # n_p * 2
// # )
// # SQRTs: n_iter * (
// # 0.5 * (n_p*n_p - n_p) +
// # n_p * (n_p - 1) * 2 +
// # n_p * board.n_obstacles +
// # n_p * 3
// # )
// # EXPs: n_iter * (
// # n_p * (n_p - 1) +
// # n_p * board.n_obstacles
// # )
// total with EXP = 30: n_iter * ( 69 * n_p ** 2 - 16 * n_p + 55 * n_obstacles * n_p )
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
// # 1 MULT
float desired_velocity_x = e_x[i] * desired_v0[i];
// # 1 MULT
float desired_velocity_y = e_y[i] * desired_v0[i];
// # 1 ADD, 1 MULT
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
// # 1 ADD, 1 MULT
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
float e_x_i = e_x[i];
float e_y_i = e_y[i];
float pos_x_i = pos_x[i];
float pos_y_i = pos_y[i];
// distance precompute, # each pair: 6 FLOPS, total over all i = 0.5*(n_p*n_p - n_p)*6
for (int j = i + 1; j < n_p; j++) {
// # 1 ADD
float dx = pos_x_i - pos_x[j];
// # 1 ADD
float dy = pos_y_i - pos_y[j];
// # 1 ADD, 2 MULTs, 1 SQRT
float norm = sqrt(dx * dx + dy * dy);
int idx = i * n_p + j;
r_ab_x_lut[idx] = dx;
r_ab_y_lut[idx] = dy;
r_ab_norm_lut[idx] = norm;
}
int j;
float acc_x_0 = 0.0f, acc_x_1 = 0.0f, acc_x_2 = 0.0f, acc_x_3 = 0.0f;
float acc_y_0 = 0.0f, acc_y_1 = 0.0f, acc_y_2 = 0.0f, acc_y_3 = 0.0f;
// ILP over the j < i half. # each lane = 66 FLOPS (per-line breakdown is the
// same as the single-lane remainder loop below), K = 4 lanes per step
for (j = 0; j + 4 < i; j += 4) {
float r_ab_x_0 = -1 * r_ab_x_lut[j * n_p + i];
float r_ab_x_1 = -1 * r_ab_x_lut[(j + 1) * n_p + i];
float r_ab_x_2 = -1 * r_ab_x_lut[(j + 2) * n_p + i];
float r_ab_x_3 = -1 * r_ab_x_lut[(j + 3) * n_p + i];
float r_ab_y_0 = -1 * r_ab_y_lut[j * n_p + i];
float r_ab_y_1 = -1 * r_ab_y_lut[(j + 1) * n_p + i];
float r_ab_y_2 = -1 * r_ab_y_lut[(j + 2) * n_p + i];
float r_ab_y_3 = -1 * r_ab_y_lut[(j + 3) * n_p + i];
float r_ab_norm_0 = r_ab_norm_lut[j * n_p + i];
float r_ab_norm_1 = r_ab_norm_lut[(j + 1) * n_p + i];
float r_ab_norm_2 = r_ab_norm_lut[(j + 2) * n_p + i];
float r_ab_norm_3 = r_ab_norm_lut[(j + 3) * n_p + i];
r_ab_norm_0 = r_ab_norm_0 > EPS ? r_ab_norm_0 : EPS;
r_ab_norm_1 = r_ab_norm_1 > EPS ? r_ab_norm_1 : EPS;
r_ab_norm_2 = r_ab_norm_2 > EPS ? r_ab_norm_2 : EPS;
r_ab_norm_3 = r_ab_norm_3 > EPS ? r_ab_norm_3 : EPS;
float r_ab2_x_0 = r_ab_x_0 - sb_e_x[j];
float r_ab2_x_1 = r_ab_x_1 - sb_e_x[j + 1];
float r_ab2_x_2 = r_ab_x_2 - sb_e_x[j + 2];
float r_ab2_x_3 = r_ab_x_3 - sb_e_x[j + 3];
float r_ab2_y_0 = r_ab_y_0 - sb_e_y[j];
float r_ab2_y_1 = r_ab_y_1 - sb_e_y[j + 1];
float r_ab2_y_2 = r_ab_y_2 - sb_e_y[j + 2];
float r_ab2_y_3 = r_ab_y_3 - sb_e_y[j + 3];
float r_ab2_norm_0 = sqrt(r_ab2_x_0 * r_ab2_x_0 + r_ab2_y_0 * r_ab2_y_0);
float r_ab2_norm_1 = sqrt(r_ab2_x_1 * r_ab2_x_1 + r_ab2_y_1 * r_ab2_y_1);
float r_ab2_norm_2 = sqrt(r_ab2_x_2 * r_ab2_x_2 + r_ab2_y_2 * r_ab2_y_2);
float r_ab2_norm_3 = sqrt(r_ab2_x_3 * r_ab2_x_3 + r_ab2_y_3 * r_ab2_y_3);
r_ab2_norm_0 = r_ab2_norm_0 > EPS ? r_ab2_norm_0 : EPS;
r_ab2_norm_1 = r_ab2_norm_1 > EPS ? r_ab2_norm_1 : EPS;
r_ab2_norm_2 = r_ab2_norm_2 > EPS ? r_ab2_norm_2 : EPS;
r_ab2_norm_3 = r_ab2_norm_3 > EPS ? r_ab2_norm_3 : EPS;
float r_ab_norm_sum_0 = r_ab_norm_0 + r_ab2_norm_0;
float r_ab_norm_sum_1 = r_ab_norm_1 + r_ab2_norm_1;
float r_ab_norm_sum_2 = r_ab_norm_2 + r_ab2_norm_2;
float r_ab_norm_sum_3 = r_ab_norm_3 + r_ab2_norm_3;
float two_b_sq_0 = r_ab_norm_sum_0 * r_ab_norm_sum_0 - sb_sq[j];
float two_b_sq_1 = r_ab_norm_sum_1 * r_ab_norm_sum_1 - sb_sq[j + 1];
float two_b_sq_2 = r_ab_norm_sum_2 * r_ab_norm_sum_2 - sb_sq[j + 2];
float two_b_sq_3 = r_ab_norm_sum_3 * r_ab_norm_sum_3 - sb_sq[j + 3];
two_b_sq_0 = two_b_sq_0 > EPS_SQ ? two_b_sq_0 : EPS_SQ;
two_b_sq_1 = two_b_sq_1 > EPS_SQ ? two_b_sq_1 : EPS_SQ;
two_b_sq_2 = two_b_sq_2 > EPS_SQ ? two_b_sq_2 : EPS_SQ;
two_b_sq_3 = two_b_sq_3 > EPS_SQ ? two_b_sq_3 : EPS_SQ;
float two_b_0 = sqrt(two_b_sq_0);
float two_b_1 = sqrt(two_b_sq_1);
float two_b_2 = sqrt(two_b_sq_2);
float two_b_3 = sqrt(two_b_sq_3);
float scalar_mult_common_0 = (r_ab_norm_sum_0 / (two_b_0 * r_ab_norm_0 * r_ab2_norm_0)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_0 * NEG_TWO_SIGMA_INV));
float scalar_mult_common_1 = (r_ab_norm_sum_1 / (two_b_1 * r_ab_norm_1 * r_ab2_norm_1)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_1 * NEG_TWO_SIGMA_INV));
float scalar_mult_common_2 = (r_ab_norm_sum_2 / (two_b_2 * r_ab_norm_2 * r_ab2_norm_2)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_2 * NEG_TWO_SIGMA_INV));
float scalar_mult_common_3 = (r_ab_norm_sum_3 / (two_b_3 * r_ab_norm_3 * r_ab2_norm_3)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_3 * NEG_TWO_SIGMA_INV));
float scalar_mult_r_ab_0 = scalar_mult_common_0 * r_ab2_norm_0;
float scalar_mult_r_ab_1 = scalar_mult_common_1 * r_ab2_norm_1;
float scalar_mult_r_ab_2 = scalar_mult_common_2 * r_ab2_norm_2;
float scalar_mult_r_ab_3 = scalar_mult_common_3 * r_ab2_norm_3;
float scalar_mult_r_ab2_0 = scalar_mult_common_0 * r_ab_norm_0;
float scalar_mult_r_ab2_1 = scalar_mult_common_1 * r_ab_norm_1;
float scalar_mult_r_ab2_2 = scalar_mult_common_2 * r_ab_norm_2;
float scalar_mult_r_ab2_3 = scalar_mult_common_3 * r_ab_norm_3;
float f_alpha_beta_neg_x_0 = r_ab_x_0 * scalar_mult_r_ab_0 + r_ab2_x_0 * scalar_mult_r_ab2_0;
float f_alpha_beta_neg_x_1 = r_ab_x_1 * scalar_mult_r_ab_1 + r_ab2_x_1 * scalar_mult_r_ab2_1;
float f_alpha_beta_neg_x_2 = r_ab_x_2 * scalar_mult_r_ab_2 + r_ab2_x_2 * scalar_mult_r_ab2_2;
float f_alpha_beta_neg_x_3 = r_ab_x_3 * scalar_mult_r_ab_3 + r_ab2_x_3 * scalar_mult_r_ab2_3;
float f_alpha_beta_neg_y_0 = r_ab_y_0 * scalar_mult_r_ab_0 + r_ab2_y_0 * scalar_mult_r_ab2_0;
float f_alpha_beta_neg_y_1 = r_ab_y_1 * scalar_mult_r_ab_1 + r_ab2_y_1 * scalar_mult_r_ab2_1;
float f_alpha_beta_neg_y_2 = r_ab_y_2 * scalar_mult_r_ab_2 + r_ab2_y_2 * scalar_mult_r_ab2_2;
float f_alpha_beta_neg_y_3 = r_ab_y_3 * scalar_mult_r_ab_3 + r_ab2_y_3 * scalar_mult_r_ab2_3;
float w_val_0, w_val_1, w_val_2, w_val_3;
float f_norm_sq_0 = f_alpha_beta_neg_x_0 * f_alpha_beta_neg_x_0 + f_alpha_beta_neg_y_0 * f_alpha_beta_neg_y_0;
float f_norm_sq_1 = f_alpha_beta_neg_x_1 * f_alpha_beta_neg_x_1 + f_alpha_beta_neg_y_1 * f_alpha_beta_neg_y_1;
float f_norm_sq_2 = f_alpha_beta_neg_x_2 * f_alpha_beta_neg_x_2 + f_alpha_beta_neg_y_2 * f_alpha_beta_neg_y_2;
float f_norm_sq_3 = f_alpha_beta_neg_x_3 * f_alpha_beta_neg_x_3 + f_alpha_beta_neg_y_3 * f_alpha_beta_neg_y_3;
float rhs_0 = f_norm_sq_0 * cos_phi_sq;
float rhs_1 = f_norm_sq_1 * cos_phi_sq;
float rhs_2 = f_norm_sq_2 * cos_phi_sq;
float rhs_3 = f_norm_sq_3 * cos_phi_sq;
float e_dot_f_0 = e_x_i * f_alpha_beta_neg_x_0 + e_y_i * f_alpha_beta_neg_y_0;
float e_dot_f_1 = e_x_i * f_alpha_beta_neg_x_1 + e_y_i * f_alpha_beta_neg_y_1;
float e_dot_f_2 = e_x_i * f_alpha_beta_neg_x_2 + e_y_i * f_alpha_beta_neg_y_2;
float e_dot_f_3 = e_x_i * f_alpha_beta_neg_x_3 + e_y_i * f_alpha_beta_neg_y_3;
float e_dot_f_sq_0 = e_dot_f_0 * e_dot_f_0;
float e_dot_f_sq_1 = e_dot_f_1 * e_dot_f_1;
float e_dot_f_sq_2 = e_dot_f_2 * e_dot_f_2;
float e_dot_f_sq_3 = e_dot_f_3 * e_dot_f_3;
if (e_dot_f_0 >= 0.0 || e_dot_f_sq_0 <= rhs_0)
w_val_0 = dt_neg;
else
w_val_0 = c_neg_times_dt;
if (e_dot_f_1 >= 0.0 || e_dot_f_sq_1 <= rhs_1)
w_val_1 = dt_neg;
else
w_val_1 = c_neg_times_dt;
if (e_dot_f_2 >= 0.0 || e_dot_f_sq_2 <= rhs_2)
w_val_2 = dt_neg;
else
w_val_2 = c_neg_times_dt;
if (e_dot_f_3 >= 0.0 || e_dot_f_sq_3 <= rhs_3)
w_val_3 = dt_neg;
else
w_val_3 = c_neg_times_dt;
acc_x_0 += f_alpha_beta_neg_x_0 * w_val_0;
acc_x_1 += f_alpha_beta_neg_x_1 * w_val_1;
acc_x_2 += f_alpha_beta_neg_x_2 * w_val_2;
acc_x_3 += f_alpha_beta_neg_x_3 * w_val_3;
acc_y_0 += f_alpha_beta_neg_y_0 * w_val_0;
acc_y_1 += f_alpha_beta_neg_y_1 * w_val_1;
acc_y_2 += f_alpha_beta_neg_y_2 * w_val_2;
acc_y_3 += f_alpha_beta_neg_y_3 * w_val_3;
}
// remainder of the j < i half, single lane. # one j = 66 FLOPS
for (; j < i; j++) {
// lut lookup + -1* sign flip: not counted
float r_ab_x = -1 * r_ab_x_lut[j * n_p + i];
float r_ab_y = -1 * r_ab_y_lut[j * n_p + i];
float r_ab_norm = r_ab_norm_lut[j * n_p + i];
r_ab_norm = r_ab_norm > EPS ? r_ab_norm : EPS;
// # 1 ADD
float r_ab2_x = r_ab_x - sb_e_x[j];
// # 1 ADD
float r_ab2_y = r_ab_y - sb_e_y[j];
// # 1 ADD, 2 MULTs, 1 SQRT
float r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
r_ab2_norm = r_ab2_norm > EPS ? r_ab2_norm : EPS;
// # 1 ADD
float r_ab_norm_sum = r_ab_norm + r_ab2_norm;
// # 1 ADD, 1 MULT
float two_b_sq = r_ab_norm_sum * r_ab_norm_sum - sb_sq[j];
two_b_sq = two_b_sq > EPS_SQ ? two_b_sq : EPS_SQ;
// # 1 SQRT
float two_b = sqrt(two_b_sq);
// # 5 MULTs, 1 DIV, 1 EXP
float 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));
// # 1 MULT
float scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// # 1 MULT
float scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
// # 1 ADD, 2 MULTs
float f_alpha_beta_neg_x = r_ab_x * scalar_mult_r_ab + r_ab2_x * scalar_mult_r_ab2;
// # 1 ADD, 2 MULTs
float f_alpha_beta_neg_y = r_ab_y * scalar_mult_r_ab + r_ab2_y * scalar_mult_r_ab2;
float w_val;
// # 1 ADD, 2 MULTs
float f_norm_sq = f_alpha_beta_neg_x * f_alpha_beta_neg_x + f_alpha_beta_neg_y * f_alpha_beta_neg_y;
// # 1 MULT
float rhs = f_norm_sq * cos_phi_sq;
// # 1 ADD, 2 MULTs
float e_dot_f = e_x[i] * f_alpha_beta_neg_x + e_y[i] * f_alpha_beta_neg_y;
// # 1 MULT
float e_dot_f_sq = e_dot_f * e_dot_f;
if (e_dot_f >= 0.0 || e_dot_f_sq <= rhs)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
// # 1 ADD, 1 MULT
f_alpha_dt_x += f_alpha_beta_neg_x * w_val;
// # 1 ADD, 1 MULT
f_alpha_dt_y += f_alpha_beta_neg_y * w_val;
}
// reduction of the j < i accumulators. # K adds per line (K=4) => 8 ADDs
f_alpha_dt_x += acc_x_0 + acc_x_1 + acc_x_2 + acc_x_3;
f_alpha_dt_y += acc_y_0 + acc_y_1 + acc_y_2 + acc_y_3;
acc_x_0 = 0.0f;
acc_x_1 = 0.0f;
acc_x_2 = 0.0f;
acc_x_3 = 0.0f;
acc_y_0 = 0.0f;
acc_y_1 = 0.0f;
acc_y_2 = 0.0f;
acc_y_3 = 0.0f;
// ILP over the j > i half. # each lane = 66 FLOPS, K = 4 lanes per step
for (j = i + 1; j + 4 < n_p; j += 4) {
float r_ab_x_0 = r_ab_x_lut[i * n_p + j];
float r_ab_x_1 = r_ab_x_lut[i * n_p + j + 1];
float r_ab_x_2 = r_ab_x_lut[i * n_p + j + 2];
float r_ab_x_3 = r_ab_x_lut[i * n_p + j + 3];
float r_ab_y_0 = r_ab_y_lut[i * n_p + j];
float r_ab_y_1 = r_ab_y_lut[i * n_p + j + 1];
float r_ab_y_2 = r_ab_y_lut[i * n_p + j + 2];
float r_ab_y_3 = r_ab_y_lut[i * n_p + j + 3];
float r_ab_norm_0 = r_ab_norm_lut[i * n_p + j];
float r_ab_norm_1 = r_ab_norm_lut[i * n_p + j + 1];
float r_ab_norm_2 = r_ab_norm_lut[i * n_p + j + 2];
float r_ab_norm_3 = r_ab_norm_lut[i * n_p + j + 3];
r_ab_norm_0 = r_ab_norm_0 > EPS ? r_ab_norm_0 : EPS;
r_ab_norm_1 = r_ab_norm_1 > EPS ? r_ab_norm_1 : EPS;
r_ab_norm_2 = r_ab_norm_2 > EPS ? r_ab_norm_2 : EPS;
r_ab_norm_3 = r_ab_norm_3 > EPS ? r_ab_norm_3 : EPS;
float r_ab2_x_0 = r_ab_x_0 - sb_e_x[j];
float r_ab2_x_1 = r_ab_x_1 - sb_e_x[j + 1];
float r_ab2_x_2 = r_ab_x_2 - sb_e_x[j + 2];
float r_ab2_x_3 = r_ab_x_3 - sb_e_x[j + 3];
float r_ab2_y_0 = r_ab_y_0 - sb_e_y[j];
float r_ab2_y_1 = r_ab_y_1 - sb_e_y[j + 1];
float r_ab2_y_2 = r_ab_y_2 - sb_e_y[j + 2];
float r_ab2_y_3 = r_ab_y_3 - sb_e_y[j + 3];
float r_ab2_norm_0 = sqrt(r_ab2_x_0 * r_ab2_x_0 + r_ab2_y_0 * r_ab2_y_0);
float r_ab2_norm_1 = sqrt(r_ab2_x_1 * r_ab2_x_1 + r_ab2_y_1 * r_ab2_y_1);
float r_ab2_norm_2 = sqrt(r_ab2_x_2 * r_ab2_x_2 + r_ab2_y_2 * r_ab2_y_2);
float r_ab2_norm_3 = sqrt(r_ab2_x_3 * r_ab2_x_3 + r_ab2_y_3 * r_ab2_y_3);
r_ab2_norm_0 = r_ab2_norm_0 > EPS ? r_ab2_norm_0 : EPS;
r_ab2_norm_1 = r_ab2_norm_1 > EPS ? r_ab2_norm_1 : EPS;
r_ab2_norm_2 = r_ab2_norm_2 > EPS ? r_ab2_norm_2 : EPS;
r_ab2_norm_3 = r_ab2_norm_3 > EPS ? r_ab2_norm_3 : EPS;
float r_ab_norm_sum_0 = r_ab_norm_0 + r_ab2_norm_0;
float r_ab_norm_sum_1 = r_ab_norm_1 + r_ab2_norm_1;
float r_ab_norm_sum_2 = r_ab_norm_2 + r_ab2_norm_2;
float r_ab_norm_sum_3 = r_ab_norm_3 + r_ab2_norm_3;
float two_b_sq_0 = r_ab_norm_sum_0 * r_ab_norm_sum_0 - sb_sq[j];
float two_b_sq_1 = r_ab_norm_sum_1 * r_ab_norm_sum_1 - sb_sq[j + 1];
float two_b_sq_2 = r_ab_norm_sum_2 * r_ab_norm_sum_2 - sb_sq[j + 2];
float two_b_sq_3 = r_ab_norm_sum_3 * r_ab_norm_sum_3 - sb_sq[j + 3];
two_b_sq_0 = two_b_sq_0 > EPS_SQ ? two_b_sq_0 : EPS_SQ;
two_b_sq_1 = two_b_sq_1 > EPS_SQ ? two_b_sq_1 : EPS_SQ;
two_b_sq_2 = two_b_sq_2 > EPS_SQ ? two_b_sq_2 : EPS_SQ;
two_b_sq_3 = two_b_sq_3 > EPS_SQ ? two_b_sq_3 : EPS_SQ;
float two_b_0 = sqrt(two_b_sq_0);
float two_b_1 = sqrt(two_b_sq_1);
float two_b_2 = sqrt(two_b_sq_2);
float two_b_3 = sqrt(two_b_sq_3);
float scalar_mult_common_0 = (r_ab_norm_sum_0 / (two_b_0 * r_ab_norm_0 * r_ab2_norm_0)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_0 * NEG_TWO_SIGMA_INV));
float scalar_mult_common_1 = (r_ab_norm_sum_1 / (two_b_1 * r_ab_norm_1 * r_ab2_norm_1)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_1 * NEG_TWO_SIGMA_INV));
float scalar_mult_common_2 = (r_ab_norm_sum_2 / (two_b_2 * r_ab_norm_2 * r_ab2_norm_2)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_2 * NEG_TWO_SIGMA_INV));
float scalar_mult_common_3 = (r_ab_norm_sum_3 / (two_b_3 * r_ab_norm_3 * r_ab2_norm_3)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_3 * NEG_TWO_SIGMA_INV));
float scalar_mult_r_ab_0 = scalar_mult_common_0 * r_ab2_norm_0;
float scalar_mult_r_ab_1 = scalar_mult_common_1 * r_ab2_norm_1;
float scalar_mult_r_ab_2 = scalar_mult_common_2 * r_ab2_norm_2;
float scalar_mult_r_ab_3 = scalar_mult_common_3 * r_ab2_norm_3;
float scalar_mult_r_ab2_0 = scalar_mult_common_0 * r_ab_norm_0;
float scalar_mult_r_ab2_1 = scalar_mult_common_1 * r_ab_norm_1;
float scalar_mult_r_ab2_2 = scalar_mult_common_2 * r_ab_norm_2;
float scalar_mult_r_ab2_3 = scalar_mult_common_3 * r_ab_norm_3;
float f_alpha_beta_neg_x_0 = r_ab_x_0 * scalar_mult_r_ab_0 + r_ab2_x_0 * scalar_mult_r_ab2_0;
float f_alpha_beta_neg_x_1 = r_ab_x_1 * scalar_mult_r_ab_1 + r_ab2_x_1 * scalar_mult_r_ab2_1;
float f_alpha_beta_neg_x_2 = r_ab_x_2 * scalar_mult_r_ab_2 + r_ab2_x_2 * scalar_mult_r_ab2_2;
float f_alpha_beta_neg_x_3 = r_ab_x_3 * scalar_mult_r_ab_3 + r_ab2_x_3 * scalar_mult_r_ab2_3;
float f_alpha_beta_neg_y_0 = r_ab_y_0 * scalar_mult_r_ab_0 + r_ab2_y_0 * scalar_mult_r_ab2_0;
float f_alpha_beta_neg_y_1 = r_ab_y_1 * scalar_mult_r_ab_1 + r_ab2_y_1 * scalar_mult_r_ab2_1;
float f_alpha_beta_neg_y_2 = r_ab_y_2 * scalar_mult_r_ab_2 + r_ab2_y_2 * scalar_mult_r_ab2_2;
float f_alpha_beta_neg_y_3 = r_ab_y_3 * scalar_mult_r_ab_3 + r_ab2_y_3 * scalar_mult_r_ab2_3;
float w_val_0, w_val_1, w_val_2, w_val_3;
float f_norm_sq_0 = f_alpha_beta_neg_x_0 * f_alpha_beta_neg_x_0 + f_alpha_beta_neg_y_0 * f_alpha_beta_neg_y_0;
float f_norm_sq_1 = f_alpha_beta_neg_x_1 * f_alpha_beta_neg_x_1 + f_alpha_beta_neg_y_1 * f_alpha_beta_neg_y_1;
float f_norm_sq_2 = f_alpha_beta_neg_x_2 * f_alpha_beta_neg_x_2 + f_alpha_beta_neg_y_2 * f_alpha_beta_neg_y_2;
float f_norm_sq_3 = f_alpha_beta_neg_x_3 * f_alpha_beta_neg_x_3 + f_alpha_beta_neg_y_3 * f_alpha_beta_neg_y_3;
float rhs_0 = f_norm_sq_0 * cos_phi_sq;
float rhs_1 = f_norm_sq_1 * cos_phi_sq;
float rhs_2 = f_norm_sq_2 * cos_phi_sq;
float rhs_3 = f_norm_sq_3 * cos_phi_sq;
float e_dot_f_0 = e_x_i * f_alpha_beta_neg_x_0 + e_y_i * f_alpha_beta_neg_y_0;
float e_dot_f_1 = e_x_i * f_alpha_beta_neg_x_1 + e_y_i * f_alpha_beta_neg_y_1;
float e_dot_f_2 = e_x_i * f_alpha_beta_neg_x_2 + e_y_i * f_alpha_beta_neg_y_2;
float e_dot_f_3 = e_x_i * f_alpha_beta_neg_x_3 + e_y_i * f_alpha_beta_neg_y_3;
float e_dot_f_sq_0 = e_dot_f_0 * e_dot_f_0;
float e_dot_f_sq_1 = e_dot_f_1 * e_dot_f_1;
float e_dot_f_sq_2 = e_dot_f_2 * e_dot_f_2;
float e_dot_f_sq_3 = e_dot_f_3 * e_dot_f_3;
if (e_dot_f_0 >= 0.0 || e_dot_f_sq_0 <= rhs_0)
w_val_0 = dt_neg;
else
w_val_0 = c_neg_times_dt;
if (e_dot_f_1 >= 0.0 || e_dot_f_sq_1 <= rhs_1)
w_val_1 = dt_neg;
else
w_val_1 = c_neg_times_dt;
if (e_dot_f_2 >= 0.0 || e_dot_f_sq_2 <= rhs_2)
w_val_2 = dt_neg;
else
w_val_2 = c_neg_times_dt;
if (e_dot_f_3 >= 0.0 || e_dot_f_sq_3 <= rhs_3)
w_val_3 = dt_neg;
else
w_val_3 = c_neg_times_dt;
acc_x_0 += f_alpha_beta_neg_x_0 * w_val_0;
acc_x_1 += f_alpha_beta_neg_x_1 * w_val_1;
acc_x_2 += f_alpha_beta_neg_x_2 * w_val_2;
acc_x_3 += f_alpha_beta_neg_x_3 * w_val_3;
acc_y_0 += f_alpha_beta_neg_y_0 * w_val_0;
acc_y_1 += f_alpha_beta_neg_y_1 * w_val_1;
acc_y_2 += f_alpha_beta_neg_y_2 * w_val_2;
acc_y_3 += f_alpha_beta_neg_y_3 * w_val_3;
}
// remainder of the j > i half, single lane. # one j = 66 FLOPS
for (; j < n_p; j++) {
float r_ab_x = r_ab_x_lut[i * n_p + j];
float r_ab_y = r_ab_y_lut[i * n_p + j];
float r_ab_norm = r_ab_norm_lut[i * n_p + j];
r_ab_norm = r_ab_norm > EPS ? r_ab_norm : EPS;
// # 1 ADD
float r_ab2_x = r_ab_x - sb_e_x[j];
// # 1 ADD
float r_ab2_y = r_ab_y - sb_e_y[j];
// # 1 ADD, 2 MULTs, 1 SQRT
float r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
r_ab2_norm = r_ab2_norm > EPS ? r_ab2_norm : EPS;
// # 1 ADD
float r_ab_norm_sum = r_ab_norm + r_ab2_norm;
// # 1 ADD, 1 MULT
float two_b_sq = r_ab_norm_sum * r_ab_norm_sum - sb_sq[j];
two_b_sq = two_b_sq > EPS_SQ ? two_b_sq : EPS_SQ;
// # 1 SQRT
float two_b = sqrt(two_b_sq);
// # 5 MULTs, 1 DIV, 1 EXP
float 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));
// # 1 MULT
float scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// # 1 MULT
float scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
// # 1 ADD, 2 MULTs
float f_alpha_beta_neg_x = r_ab_x * scalar_mult_r_ab + r_ab2_x * scalar_mult_r_ab2;
// # 1 ADD, 2 MULTs
float f_alpha_beta_neg_y = r_ab_y * scalar_mult_r_ab + r_ab2_y * scalar_mult_r_ab2;
float w_val;
// # 1 ADD, 2 MULTs
float f_norm_sq = f_alpha_beta_neg_x * f_alpha_beta_neg_x + f_alpha_beta_neg_y * f_alpha_beta_neg_y;
// # 1 MULT
float rhs = f_norm_sq * cos_phi_sq;
// # 1 ADD, 2 MULTs
float e_dot_f = e_x[i] * f_alpha_beta_neg_x + e_y[i] * f_alpha_beta_neg_y;
// # 1 MULT
float e_dot_f_sq = e_dot_f * e_dot_f;
if (e_dot_f >= 0.0 || e_dot_f_sq <= rhs)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
// # 1 ADD, 1 MULT
f_alpha_dt_x += f_alpha_beta_neg_x * w_val;
// # 1 ADD, 1 MULT
f_alpha_dt_y += f_alpha_beta_neg_y * w_val;
}
// reduction of the j > i accumulators. # K adds per line (K=4) => 8 ADDs
f_alpha_dt_x += acc_x_0 + acc_x_1 + acc_x_2 + acc_x_3;
f_alpha_dt_y += acc_y_0 + acc_y_1 + acc_y_2 + acc_y_3;
// force between pedestrian and obstacles
// # one obstacle = 55 FLOPS (w_val sign-based, no a*a / b_sq)
for (int k = 0; k < board.n_obstacles; k++) {
// # 1 ADD
float v1_x = pos_x[i] - board.obstacles[k].start_x;
// # 1 ADD
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
// # 1 ADD, 3 MULTs
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
if (projection < 0.0)
projection = 0.0;
else if (projection > 1.0)
projection = 1.0;
// # 2 ADDs, 1 MULT
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
// # 2 ADDs, 1 MULT
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
// # 1 ADD, 2 MULTs, 1 SQRT
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
// # 2 MULTs, 1 DIV, 1 EXP
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// # 1 MULT
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
// # 1 MULT
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
float w_val;
// sign select only, no flops
float e_dot_f = (f_alpha_B_neg_y == 0) ? 0 : (f_alpha_B_neg_y < 0 ? -e_y[i] : e_y[i]);
if (e_dot_f >= cos_phi)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
// # 1 MULT
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
// # 1 MULT
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
// # 1 ADD
f_alpha_dt_x += f_alpha_B_dt_x;
// # 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y;
}
// # 1 ADD
w_x[i] += f_alpha_dt_x;
// # 1 ADD
w_y[i] += f_alpha_dt_y;
}
// update each pedestrian
// # 29 FLOPS per pedestrian
for (int i = 0; i < n_p; i++) {
// # 1 ADD, 2 MULTs, 1 SQRT
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]);
float w_norm2 = w_norm > EPS ? w_norm : EPS;
// # 1 DIV
float correction_factor = min(1.0f, v_max[i] / w_norm2);
// # 1 MULT
curr_vel_x[i] = w_x[i] * correction_factor;
// # 1 MULT
curr_vel_y[i] = w_y[i] * correction_factor;
// # 1 ADD, 1 MULT
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt;
// # 1 ADD, 1 MULT
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);
// # 1 ADD
float ex = destination_x[i] - pos_x[i];
// # 1 ADD
float ey = destination_y[i] - pos_y[i];
// # 1 ADD, 2 MULTs, 1 SQRT
float e_norm = sqrt(ex * ex + ey * ey);
float e_norm2 = e_norm > EPS ? e_norm : EPS;
// # 1 DIV
float e_norm_inv = 1.0 / e_norm2;
// # 1 MULT
e_x[i] = ex * e_norm_inv;
// # 1 MULT
e_y[i] = ey * e_norm_inv;
// # 1 ADD, 2 MULTs
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i];
// # 1 SQRT
float velocity_norm = sqrt(velocity_norm_sq);
// # 1 MULT
sb_sq[i] = velocity_norm_sq * delta_t_sq;
// # 2 MULTs (velocity_norm*delta_t written on both lines, counted as is)
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t);
// # 2 MULTs
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;
}
File diff suppressed because it is too large Load Diff
@@ -1,489 +0,0 @@
// # Changes: SoA layout for pedestrians (file-scope arrays instead of struct fields)
// # added flopcount
// FLOPCOUNT = n_iter * ( 69 * n_p ** 2 - 32 * n_p + 55 * n_obstacles * n_p )
// # Predecessor: variant_float_inlining.cpp
#include "../headers/board_reduced_float.hpp"
#include "../headers/constants_float.hpp"
#include "../headers/pedestrian_reduced_float.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.0f
#define BOARD_LENGTH 50.0f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
#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, one array per pedestrian field
vector<float> pos_x, pos_y;
vector<float> sb_sq;
vector<float> sb_e_x, sb_e_y;
vector<float> e_x, e_y;
vector<float> curr_vel_x, curr_vel_y;
vector<float> w_x, w_y;
vector<float> desired_v0;
vector<float> v_max;
vector<float> 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<float> velocity_gen(v0_mean, v0_std_dev);
uniform_real_distribution<float> y_gen(0.3, BOARD_WIDTH - 0.3);
uniform_real_distribution<float> x_gen_left(2.0, BOARD_LENGTH / 2.0);
uniform_real_distribution<float> x_gen_right(BOARD_LENGTH / 2.0, BOARD_LENGTH - 2.0);
// resize all 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++) {
float x = x_gen_left(gen);
float y = y_gen(gen);
float dest_x = BOARD_LENGTH + 30.0; // renamed to avoid shadowing destination_x[]
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1);
float 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 (c_v_x=c_v_y=0):
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
for (int i = n_p_half; i < n_p; i++) {
float x = x_gen_right(gen);
float y = y_gen(gen);
float dest_x = -30;
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1);
float 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;
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
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) {
(void)pedestrians;
#if BENCHMARK
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
vector<tuple<int, int, float, float, float, float, 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++) {
samples.emplace_back(0, i, pos_x[i], pos_y[i], e_x[i], e_y[i], color[i]);
}
}
#endif
// here we have real (simple) symmetry
std::vector<float> r_ab_x_lut(n_p * n_p, 0.0);
std::vector<float> r_ab_y_lut(n_p * n_p, 0.0);
std::vector<float> r_ab_norm_lut(n_p * n_p, 0.0);
// use NAME[i * n_p + j]
// FLOPCOUNT (everything inside the t-loop). EXP kept as its own op (one real exp() per call).
// section costs: overhead 4 ADD 4 MULT | distance pair 3 ADD 2 MULT 1 SQRT |
// ped-ped j 11 ADD 22 MULT 2 SQRT 1 DIV 1 EXP |
// obstacle 10 ADD 13 MULT 1 SQRT 1 DIV 1 EXP | update 7 ADD 17 MULT 3 SQRT 2 DIV
// # ADDs: n_iter * (
// # n_p * 4 +
// # 0.5 * (n_p*n_p - n_p) * 3 +
// # n_p * (n_p - 1) * 11 +
// # n_p * board.n_obstacles * 10 +
// # n_p * 7
// # )
// # MULTs: n_iter * (
// # n_p * 4 +
// # 0.5 * (n_p*n_p - n_p) * 2 +
// # n_p * (n_p - 1) * 22 +
// # n_p * board.n_obstacles * 13 +
// # n_p * 17
// # )
// # DIVs: n_iter * (
// # n_p * (n_p - 1) +
// # n_p * board.n_obstacles +
// # n_p * 2
// # )
// # SQRTs: n_iter * (
// # 0.5 * (n_p*n_p - n_p) +
// # n_p * (n_p - 1) * 2 +
// # n_p * board.n_obstacles +
// # n_p * 3
// # )
// # EXPs: n_iter * (
// # n_p * (n_p - 1) +
// # n_p * board.n_obstacles
// # )
// total with EXP = 30: n_iter * ( 69 * n_p ** 2 - 32 * n_p + 55 * n_obstacles * n_p )
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
// # 1 MULT
float desired_velocity_x = e_x[i] * desired_v0[i];
// # 1 MULT
float desired_velocity_y = e_y[i] * desired_v0[i];
// # 1 ADD, 1 MULT
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
// # 1 ADD, 1 MULT
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
// lut for distances
// get all distances first
// # each pair: 6 FLOPS, total over all i = 0.5*(n_p*n_p - n_p) * 6
for (int j = i + 1; j < n_p; j++) {
// # 1 ADD
float dx = pos_x[i] - pos_x[j];
// # 1 ADD
float dy = pos_y[i] - pos_y[j];
// # 1 ADD, 2 MULTs, 1 SQRT
float norm = sqrt(dx * dx + dy * dy);
int idx = i * n_p + j;
r_ab_x_lut[idx] = dx;
r_ab_y_lut[idx] = dy;
r_ab_norm_lut[idx] = norm;
}
// force between pedestrians
// # one j (the i==j case is skipped) = 66 FLOPS, runs (n_p-1) times per i
for (int j = 0; j < n_p; j++) {
if (i == j) continue;
// symmetry: lut lookup is for free, the -1* sign flip is not counted
float 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];
}
//float r_ab_x = pos_x[i] - pos_x[j];
//float r_ab_y = pos_y[i] - pos_y[j];
//float r_ab_norm = sqrt(r_ab_x * r_ab_x + r_ab_y * r_ab_y);
r_ab_norm = r_ab_norm > EPS ? r_ab_norm : EPS;
// # 1 ADD
float r_ab2_x = r_ab_x - sb_e_x[j];
// # 1 ADD
float r_ab2_y = r_ab_y - sb_e_y[j];
// # 1 ADD, 2 MULTs, 1 SQRT
float r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
r_ab2_norm = r_ab2_norm > EPS ? r_ab2_norm : EPS;
// # 1 ADD
float r_ab_norm_sum = r_ab_norm + r_ab2_norm;
// # 1 ADD, 1 MULT
float two_b_sq = r_ab_norm_sum * r_ab_norm_sum - sb_sq[j];
two_b_sq = two_b_sq > EPS_SQ ? two_b_sq : EPS_SQ;
// # 1 SQRT
float two_b = sqrt(two_b_sq);
// # 5 MULTs, 1 DIV, 1 EXP
float 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));
// # 1 MULT
float scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// # 1 MULT
float scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
// # 1 ADD, 2 MULTs
float f_alpha_beta_neg_x = r_ab_x * scalar_mult_r_ab + r_ab2_x * scalar_mult_r_ab2;
// # 1 ADD, 2 MULTs
float f_alpha_beta_neg_y = r_ab_y * scalar_mult_r_ab + r_ab2_y * scalar_mult_r_ab2;
float w_val;
// # 1 ADD, 2 MULTs
float f_norm_sq = f_alpha_beta_neg_x * f_alpha_beta_neg_x + f_alpha_beta_neg_y * f_alpha_beta_neg_y;
// # 1 MULT
float rhs = f_norm_sq * cos_phi_sq;
// # 1 ADD, 2 MULTs
float e_dot_f = e_x[i] * f_alpha_beta_neg_x + e_y[i] * f_alpha_beta_neg_y;
// # 1 MULT
float e_dot_f_sq = e_dot_f * e_dot_f;
if (e_dot_f >= 0.0 || e_dot_f_sq <= rhs)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
// # 1 ADD, 1 MULT
f_alpha_dt_x = f_alpha_dt_x + f_alpha_beta_neg_x * w_val;
// # 1 ADD, 1 MULT
f_alpha_dt_y = f_alpha_dt_y + f_alpha_beta_neg_y * w_val;
}
// force between pedestrian and obstacles
// # one obstacle = 55 FLOPS (w_val here is sign-based, no a*a / b_sq)
for (int k = 0; k < board.n_obstacles; k++) {
// # 1 ADD
float v1_x = pos_x[i] - board.obstacles[k].start_x;
// # 1 ADD
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
// # 1 ADD, 3 MULTs
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
if (projection < 0.0)
projection = 0.0;
else if (projection > 1.0)
projection = 1.0;
// # 2 ADDs, 1 MULT
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
// # 2 ADDs, 1 MULT
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
// # 1 ADD, 2 MULTs, 1 SQRT
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
// # 2 MULTs, 1 DIV, 1 EXP
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// # 1 MULT
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
// # 1 MULT
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
float w_val;
// sign select only, no flops
float e_dot_f = (f_alpha_B_neg_y == 0) ? 0 : (f_alpha_B_neg_y < 0 ? -e_y[i] : e_y[i]);
if (e_dot_f >= cos_phi)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
// # 1 MULT
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
// # 1 MULT
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
// # 1 ADD
f_alpha_dt_x += f_alpha_B_dt_x;
// # 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y;
}
// # 1 ADD
w_x[i] += f_alpha_dt_x;
// # 1 ADD
w_y[i] += f_alpha_dt_y;
}
// update each pedestrian
// # 29 FLOPS per pedestrian
for (int i = 0; i < n_p; i++) {
// # 1 ADD, 2 MULTs, 1 SQRT
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]);
float w_norm2 = w_norm > EPS ? w_norm : EPS;
// # 1 DIV
float correction_factor = min(1.0f, v_max[i] / w_norm2);
// # 1 MULT
curr_vel_x[i] = w_x[i] * correction_factor;
// # 1 MULT
curr_vel_y[i] = w_y[i] * correction_factor;
// # 1 ADD, 1 MULT
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt;
// # 1 ADD, 1 MULT
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);
// # 1 ADD
float ex = destination_x[i] - pos_x[i];
// # 1 ADD
float ey = destination_y[i] - pos_y[i];
// # 1 ADD, 2 MULTs, 1 SQRT
float e_norm = sqrt(ex * ex + ey * ey);
float e_norm2 = e_norm > EPS ? e_norm : EPS;
// # 1 DIV
float e_norm_inv = 1.0 / e_norm2;
// # 1 MULT
e_x[i] = ex * e_norm_inv;
// # 1 MULT
e_y[i] = ey * e_norm_inv;
// # 1 ADD, 2 MULTs
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i];
// # 1 SQRT
float velocity_norm = sqrt(velocity_norm_sq);
// # 1 MULT
sb_sq[i] = velocity_norm_sq * delta_t_sq;
// # 2 MULTs (velocity_norm*delta_t written on both lines, counted as is)
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t);
// # 2 MULTs
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;
}
@@ -1,458 +0,0 @@
// # 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)
// Now in a second step we wanna really use AVX2 for the mainloop (where we wana optimize the most innerloop first)
#include "../headers/board.hpp"
#include "../headers/constants.hpp"
#include "../headers/pedestrian_reduced.hpp"
#include <immintrin.h> // to use avx2
#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);
const int n_p_pad = (n_p + 3) & ~3; // round up to a multiple of 4 (so its easy to use avx)
// resize all our arrays for this run
pos_x.resize(n_p_pad);
pos_y.resize(n_p_pad);
sb_sq.resize(n_p_pad);
sb_e_x.resize(n_p_pad);
sb_e_y.resize(n_p_pad);
e_x.resize(n_p_pad);
e_y.resize(n_p_pad);
curr_vel_x.resize(n_p_pad);
curr_vel_y.resize(n_p_pad);
w_x.resize(n_p_pad);
w_y.resize(n_p_pad);
desired_v0.resize(n_p_pad);
v_max.resize(n_p_pad);
destination_x.resize(n_p_pad);
destination_y.resize(n_p_pad);
color.resize(n_p_pad);
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;
}
// Padding lanes: positions far away, everything else zero. Any unmasked SIMD
// contribution from these lanes will vanish through exp(very negative) roughly 0.
for (int j = n_p; j < n_p_pad; j++) {
pos_x[j] = 1e9;
pos_y[j] = 1e9;
sb_sq[j] = 0.0;
sb_e_x[j] = 0.0;
sb_e_y[j] = 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
// all the constants for the AVX2 setup
const int n_p_pad = (n_p + 3) & ~3;
alignas(32) const double lane_offset_arr[4] = {0.0, 1.0, 2.0, 3.0};
const __m256d lane_off = _mm256_load_pd(lane_offset_arr);
const __m256d v_neg_v0_div_2s = _mm256_set1_pd(NEG_V0_AB_div_TWO_SIGMA);
const __m256d v_neg_2s_inv = _mm256_set1_pd(NEG_TWO_SIGMA_INV);
const __m256d v_cos_phi_inv = _mm256_set1_pd(cos_phi_inv);
const __m256d v_dt_neg = _mm256_set1_pd(dt_neg);
const __m256d v_c_neg_dt = _mm256_set1_pd(c_neg_times_dt);
const __m256d v_zero = _mm256_setzero_pd();
const __m256d v_np = _mm256_set1_pd((double)n_p);
// # flops per iteration (one pass of the outer time loop):
// # ADDs: n_p * 12 + n_p * n_p_pad * 15 + n_p * board.n_obstacles * 12 + n_p * 7
// # MULTs: n_p * 4 + n_p * n_p_pad * 24 + n_p * board.n_obstacles * 19 + n_p * 16
// # DIVs: n_p * n_p_pad * 1 + n_p * board.n_obstacles + n_p * 2
// # SQRTs: n_p * n_p_pad * 3 + n_p * board.n_obstacles + n_p * 3
// # EXPs: n_p * n_p_pad * 1 + n_p * board.n_obstacles
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]; // # 1 MULT
double desired_velocity_y = e_y[i] * desired_v0[i]; // # 1 MULT
double f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
double f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
// since i is fixed for the innermost loops we now wanna set all those variables
const __m256d pix = _mm256_set1_pd(pos_x[i]);
const __m256d piy = _mm256_set1_pd(pos_y[i]);
const __m256d eix = _mm256_set1_pd(e_x[i]);
const __m256d eiy = _mm256_set1_pd(e_y[i]);
const __m256d v_i = _mm256_set1_pd((double)i);
__m256d acc_x = _mm256_setzero_pd();
__m256d acc_y = _mm256_setzero_pd();
for (int j = 0; j < n_p_pad; j += 4) {
// 4 lanes at once, lane k maps to pedestrian j+k
__m256d j_idx = _mm256_add_pd(_mm256_set1_pd((double)j), lane_off); // # 4 ADDs
// active = (j+k != i) AND (j+k < n_p), zero in lanes we wanna skip
__m256d m_neq_i = _mm256_cmp_pd(j_idx, v_i, _CMP_NEQ_OQ);
__m256d m_in_range = _mm256_cmp_pd(j_idx, v_np, _CMP_LT_OQ);
__m256d active = _mm256_and_pd(m_neq_i, m_in_range);
// j-side loads, SoA contiguous. loadu because std::vector only guarantees 16B align.
__m256d pjx = _mm256_loadu_pd(&pos_x[j]);
__m256d pjy = _mm256_loadu_pd(&pos_y[j]);
__m256d sbe_jx = _mm256_loadu_pd(&sb_e_x[j]);
__m256d sbe_jy = _mm256_loadu_pd(&sb_e_y[j]);
__m256d sb_jsq = _mm256_loadu_pd(&sb_sq[j]);
// 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);
// r_ab = pos_i - pos_j ; |r_ab|
__m256d r_ab_x = _mm256_sub_pd(pix, pjx); // # 4 ADDs
__m256d r_ab_y = _mm256_sub_pd(piy, pjy); // # 4 ADDs
__m256d r_ab_n2 = _mm256_fmadd_pd(r_ab_x, r_ab_x, _mm256_mul_pd(r_ab_y, r_ab_y)); // # 8 MULTs, 4 ADDs
__m256d r_ab_n = _mm256_sqrt_pd(r_ab_n2); // # 4 SQRTs
// 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);
// r_ab2 = r_ab - sb_e_j ; |r_ab2|
__m256d r_ab2_x = _mm256_sub_pd(r_ab_x, sbe_jx); // # 4 ADDs
__m256d r_ab2_y = _mm256_sub_pd(r_ab_y, sbe_jy); // # 4 ADDs
__m256d r_ab2_n2 = _mm256_fmadd_pd(r_ab2_x, r_ab2_x, _mm256_mul_pd(r_ab2_y, r_ab2_y)); // # 8 MULTs, 4 ADDs
__m256d r_ab2_n = _mm256_sqrt_pd(r_ab2_n2); // # 4 SQRTs
// 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);
// 2b = sqrt((|r_ab| + |r_ab2|)^2 - sb_sq_j)
__m256d r_sum = _mm256_add_pd(r_ab_n, r_ab2_n); // # 4 ADDs
__m256d two_b_sq = _mm256_fmsub_pd(r_sum, r_sum, sb_jsq); // # 4 MULTs, 4 ADDs
__m256d two_b = _mm256_sqrt_pd(two_b_sq); // # 4 SQRTs
// exp scalar fallback for now, we vectorize in a later step. probably the biggest perf win left.
__m256d e_arg = _mm256_mul_pd(two_b, v_neg_2s_inv); // # 4 MULTs
alignas(32) double tmp_arg[4], tmp_val[4];
_mm256_store_pd(tmp_arg, e_arg);
tmp_val[0] = exp(tmp_arg[0]);
tmp_val[1] = exp(tmp_arg[1]);
tmp_val[2] = exp(tmp_arg[2]);
tmp_val[3] = exp(tmp_arg[3]); // # 4 EXPs (1 per lane, scalar)
__m256d e_val = _mm256_load_pd(tmp_val);
// 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));
// smc = (r_sum * NEG_V0_AB_div_TWO_SIGMA * e_val) / (two_b * r_ab_n * r_ab2_n)
// the i==j lane collapses denom to 0, so we swap in 1.0 there to keep smc finite.
// the AND with active zeroes the contribution from that lane anyway.
__m256d numer = _mm256_mul_pd(_mm256_mul_pd(r_sum, v_neg_v0_div_2s), e_val); // # 8 MULTs
__m256d denom = _mm256_mul_pd(_mm256_mul_pd(two_b, r_ab_n), r_ab2_n); // # 8 MULTs
__m256d denom_safe = _mm256_blendv_pd(_mm256_set1_pd(1.0), denom, active); // bitwise, no flop
__m256d smc = _mm256_div_pd(numer, denom_safe); // # 4 DIVs
// double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
__m256d sm_r_ab = _mm256_mul_pd(smc, r_ab2_n); // # 4 MULTs
__m256d sm_r_ab2 = _mm256_mul_pd(smc, r_ab_n); // # 4 MULTs
// 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;
// scalar wants r_ab * sm_r_ab + r_ab2 * sm_r_ab2, the previous fma had the layout wrong
__m256d f_x = _mm256_fmadd_pd(r_ab2_x, sm_r_ab2, _mm256_mul_pd(r_ab_x, sm_r_ab)); // # 8 MULTs, 4 ADDs
__m256d f_y = _mm256_fmadd_pd(r_ab2_y, sm_r_ab2, _mm256_mul_pd(r_ab_y, sm_r_ab)); // # 8 MULTs, 4 ADDs
// w_val = dt_neg by default, c_neg_times_dt when (a >= 0 && a*a > b_sq)
__m256d a = _mm256_mul_pd(_mm256_fmadd_pd(eix, f_x, _mm256_mul_pd(eiy, f_y)), v_cos_phi_inv); // # 12 MULTs, 4 ADDs
__m256d b_sq = _mm256_fmadd_pd(f_x, f_x, _mm256_mul_pd(f_y, f_y)); // # 8 MULTs, 4 ADDs
__m256d a_sq = _mm256_mul_pd(a, a); // # 4 MULTs
__m256d c1 = _mm256_cmp_pd(a, v_zero, _CMP_GE_OQ);
__m256d c2 = _mm256_cmp_pd(a_sq, b_sq, _CMP_GT_OQ);
__m256d cond = _mm256_and_pd(c1, c2);
__m256d w_val = _mm256_blendv_pd(v_dt_neg, v_c_neg_dt, cond);
// zero out inactive lanes (i==j and padding) before accumulating.
// AND with all-zero mask = 0, AND with all-one mask passes through. cheaper than blendv here.
__m256d f_x_safe = _mm256_and_pd(f_x, active); // bitwise, no flop
__m256d f_y_safe = _mm256_and_pd(f_y, active); // bitwise, no flop
acc_x = _mm256_fmadd_pd(f_x_safe, w_val, acc_x); // # 4 MULTs, 4 ADDs
acc_y = _mm256_fmadd_pd(f_y_safe, w_val, acc_y); // # 4 MULTs, 4 ADDs
}
// horizontal sum acc_x, acc_y into the scalar f_alpha_dt
__m256d hsum_xy = _mm256_hadd_pd(acc_x, acc_y); // [x0+x1, y0+y1, x2+x3, y2+y3] // # 4 ADDs
__m128d lo = _mm256_castpd256_pd128(hsum_xy);
__m128d hi = _mm256_extractf128_pd(hsum_xy, 1);
__m128d sum_xy = _mm_add_pd(lo, hi); // [x_total, y_total] // # 2 ADDs
alignas(16) double sum_out[2];
_mm_store_pd(sum_out, sum_xy);
f_alpha_dt_x += sum_out[0]; // # 1 ADD
f_alpha_dt_y += sum_out[1]; // # 1 ADD
for (int k = 0; k < board.n_obstacles; k++) {
double v1_x = pos_x[i] - board.obstacles[k].start.x; // # 1 ADD
double v1_y = pos_y[i] - board.obstacles[k].start.y; // # 1 ADD
double projection = (v1_x * board.obstacles[k].obstacle_v.x + v1_y * board.obstacles[k].obstacle_v.y) * board.obstacles[k].length_sq_inv; // # 3 MULTs, 1 ADD
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); // # 1 MULT, 2 ADDs
double r_aB_y = pos_y[i] - (board.obstacles[k].start.y + board.obstacles[k].obstacle_v.y * projection); // # 1 MULT, 2 ADDs
double r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y); // # 2 MULTs, 1 ADD, 1 SQRT
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm; // # 2 MULTs, 1 EXP, 1 DIV
double f_alpha_B_neg_x = r_aB_x * scalar_mult; // # 1 MULT
double f_alpha_B_neg_y = r_aB_y * scalar_mult; // # 1 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; // # 3 MULTs, 1 ADD
double b_sq = f_alpha_B_neg_x * f_alpha_B_neg_x + f_alpha_B_neg_y * f_alpha_B_neg_y; // # 2 MULTs, 1 ADD
w_val = dt_neg;
if (a >= 0 && (a * a > b_sq)) { // # 1 MULT for a*a
w_val = c_neg_times_dt;
}
double f_alpha_B_dt_x = f_alpha_B_neg_x * w_val; // # 1 MULT
double f_alpha_B_dt_y = f_alpha_B_neg_y * w_val; // # 1 MULT
f_alpha_dt_x += f_alpha_B_dt_x; // # 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y; // # 1 ADD
}
w_x[i] += f_alpha_dt_x; // 1 ADD
w_y[i] += f_alpha_dt_y; // 1 ADD
}
for (int i = 0; i < n_p; i++) {
double w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]); // # 2 MULTs, 1 ADD, 1 SQRT
double correction_factor = min(1.0, v_max[i] / w_norm); // # 1 DIV
curr_vel_x[i] = w_x[i] * correction_factor; // # 1 MULT
curr_vel_y[i] = w_y[i] * correction_factor; // # 1 MULT
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt; // # 1 MULT,1 ADD
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt; // # 1 MULT, 1 ADD
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]; // # 1 ADD
double ey = destination_y[i] - pos_y[i]; // # 1 ADD
double e_norm_inv = 1.0 / sqrt(ex * ex + ey * ey); // # 2 MULTs, 1 ADD, 1 SQRT, 1 DIV
e_x[i] = ex * e_norm_inv; // # 1 MULT
e_y[i] = ey * e_norm_inv; // # 1 MULT
double velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i]; // # 2 MULTs, 1 ADD
double velocity_norm = sqrt(velocity_norm_sq); // # 1 SQRT
sb_sq[i] = velocity_norm_sq * delta_t_sq; // # 1 MULT
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t); // # 2 MULTs (vel_norm*delta_t shared with next line)
sb_e_y[i] = e_y[i] * (velocity_norm * delta_t); // # 1 MULT
#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;
}
+120 -113
View File
@@ -180,35 +180,41 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// vectorized exp for a __m256d, no libm call.
// idea: exp(x) = 2^n * exp(r), where n = round(x / ln2) and r = x - n*ln2.
// after the range reduce, r lives in [-ln(2)/2, ln(2)/2] ~ [-0.347, 0.347], small enough
// that degree-7 taylor is accurate to ~1e-9 absolute, way under the 1e-3 verify.py tolerance.
// the 2^n part is free: in ieee 754 doubles, 2^n is just (n + 1023) << 52 in the bit pattern.
// Schraudolph 1999 for the 2^n bit trick.
// ieee 754 double layout (bias 1023, exp slot at bit 52): https://en.wikipedia.org/wiki/Double-precision_floating-point_format
// Horner's method for the poly eval: https://en.wikipedia.org/wiki/Horner%27s_method
// simd expd, degree-12 taylor on the [-ln2/2, ln2/2] reduced range,
// schraudolph 2^n bit trick on the exponent slot.
// # 64 MULTs, 56 ADDs
static inline __m256d exp256_pd(__m256d x) {
// n = round(x / ln(2))
const __m256d log2e = _mm256_set1_pd(1.4426950408889634); // 1/ln(2)
__m256d nf = _mm256_round_pd(_mm256_mul_pd(x, log2e),
_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
const __m256d log2e = _mm256_set1_pd(1.4426950408889634);
__m256d nf = _mm256_round_pd(_mm256_mul_pd(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC); // # 4 MULTs
// r = x - n * ln(2)
const __m256d ln2 = _mm256_set1_pd(0.6931471805599453);
__m256d r = _mm256_fnmadd_pd(nf, ln2, x); // r = x - nf*ln2
__m256d r = _mm256_fnmadd_pd(nf, ln2, x); // # 4 MULTs, 4 ADDs
// poly(r) ~ exp(r). degree-7 taylor, horner from highest coeff downwards.
const __m256d c7 = _mm256_set1_pd(1.0 / 5040.0);
const __m256d c6 = _mm256_set1_pd(1.0 / 720.0);
const __m256d c5 = _mm256_set1_pd(1.0 / 120.0);
const __m256d c4 = _mm256_set1_pd(1.0 / 24.0);
const __m256d c3 = _mm256_set1_pd(1.0 / 6.0);
const __m256d c2 = _mm256_set1_pd(0.5);
const __m256d c1 = _mm256_set1_pd(1.0);
const __m256d c0 = _mm256_set1_pd(1.0);
const __m256d c13 = _mm256_set1_pd(c13_scalar);
const __m256d c12 = _mm256_set1_pd(c12_scalar);
const __m256d c11 = _mm256_set1_pd(c11_scalar);
const __m256d c10 = _mm256_set1_pd(c10_scalar);
const __m256d c9 = _mm256_set1_pd(c9_scalar);
const __m256d c8 = _mm256_set1_pd(c8_scalar);
const __m256d c7 = _mm256_set1_pd(c7_scalar);
const __m256d c6 = _mm256_set1_pd(c6_scalar);
const __m256d c5 = _mm256_set1_pd(c5_scalar);
const __m256d c4 = _mm256_set1_pd(c4_scalar);
const __m256d c3 = _mm256_set1_pd(c3_scalar);
const __m256d c2 = _mm256_set1_pd(c2_scalar);
const __m256d c1 = _mm256_set1_pd(c1_scalar);
const __m256d c0 = _mm256_set1_pd(c0_scalar);
__m256d p = c7;
__m256d p = c13;
// # 13 * (4 MULTs, 4 ADDs)
// # = 52 MULTs, 52 ADDs
p = _mm256_fmadd_pd(p, r, c12);
p = _mm256_fmadd_pd(p, r, c11);
p = _mm256_fmadd_pd(p, r, c10);
p = _mm256_fmadd_pd(p, r, c9);
p = _mm256_fmadd_pd(p, r, c8);
p = _mm256_fmadd_pd(p, r, c7);
p = _mm256_fmadd_pd(p, r, c6);
p = _mm256_fmadd_pd(p, r, c5);
p = _mm256_fmadd_pd(p, r, c4);
@@ -217,18 +223,13 @@ static inline __m256d exp256_pd(__m256d x) {
p = _mm256_fmadd_pd(p, r, c1);
p = _mm256_fmadd_pd(p, r, c0);
// scale by 2^n via bit trick on the exponent field.
// nf is already integer-valued from the round above, cvt to int32, sign-extend to int64,
// add ieee bias 1023, shift left 52 to land in the exponent slot of a double.
// note: for padding lanes (sb_e=0, pos=1e9) n can overflow int32, the resulting pow2n is garbage
// but the active mask in the j loop zeroes that lane out before accumulation so it doesn't matter.
__m128i ni32 = _mm256_cvtpd_epi32(nf);
__m256i ni64 = _mm256_cvtepi32_epi64(ni32);
__m128i ni32 = _mm256_cvtpd_epi32(nf);
__m256i ni64 = _mm256_cvtepi32_epi64(ni32);
__m256i biased = _mm256_add_epi64(ni64, _mm256_set1_epi64x(1023));
__m256i scaled = _mm256_slli_epi64(biased, 52);
__m256d pow2n = _mm256_castsi256_pd(scaled);
__m256d pow2n = _mm256_castsi256_pd(scaled);
return _mm256_mul_pd(p, pow2n);
return _mm256_mul_pd(p, pow2n); // # 4 MULTs
}
int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, const char *dump_path) {
@@ -267,15 +268,21 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
const __m256d v_zero = _mm256_setzero_pd();
const __m256d v_np = _mm256_set1_pd((double)n_p);
// # flops per iteration (one pass of the outer time loop):
// # ADDs: n_p * 12 + n_p * n_p_pad * 29 + n_p * board.n_obstacles * 12 + n_p * 7
// # MULTs: n_p * 4 + n_p * n_p_pad * 40 + n_p * board.n_obstacles * 19 + n_p * 16
// # DIVs: n_p * n_p_pad * 1 + n_p * board.n_obstacles + n_p * 2
// # SQRTs: n_p * n_p_pad * 3 + n_p * board.n_obstacles + n_p * 3
// # EXPs: n_p * board.n_obstacles
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 desired_velocity_x = e_x[i] * desired_v0[i]; // # 1 MULT
double desired_velocity_y = e_y[i] * desired_v0[i]; // # 1 MULT
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;
double f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
double f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
// since i is fixed for the innermost loops we now wanna set all those variables
const __m256d pix = _mm256_set1_pd(pos_x[i]);
@@ -289,16 +296,16 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
for (int j = 0; j < n_p_pad; j += 4) {
// 4 lanes at once, lane k maps to pedestrian j+k
__m256d j_idx = _mm256_add_pd(_mm256_set1_pd((double)j), lane_off);
__m256d j_idx = _mm256_add_pd(_mm256_set1_pd((double)j), lane_off); // # 4 ADDs
// active = (j+k != i) AND (j+k < n_p), zero in lanes we wanna skip
__m256d m_neq_i = _mm256_cmp_pd(j_idx, v_i, _CMP_NEQ_OQ);
__m256d m_neq_i = _mm256_cmp_pd(j_idx, v_i, _CMP_NEQ_OQ);
__m256d m_in_range = _mm256_cmp_pd(j_idx, v_np, _CMP_LT_OQ);
__m256d active = _mm256_and_pd(m_neq_i, m_in_range);
__m256d active = _mm256_and_pd(m_neq_i, m_in_range);
// j-side loads, SoA contiguous. loadu because std::vector only guarantees 16B align.
__m256d pjx = _mm256_loadu_pd(&pos_x[j]);
__m256d pjy = _mm256_loadu_pd(&pos_y[j]);
__m256d pjx = _mm256_loadu_pd(&pos_x[j]);
__m256d pjy = _mm256_loadu_pd(&pos_y[j]);
__m256d sbe_jx = _mm256_loadu_pd(&sb_e_x[j]);
__m256d sbe_jy = _mm256_loadu_pd(&sb_e_y[j]);
__m256d sb_jsq = _mm256_loadu_pd(&sb_sq[j]);
@@ -309,10 +316,10 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
// double r_ab_norm = sqrt(r_ab_x * r_ab_x + r_ab_y * r_ab_y);
// r_ab = pos_i - pos_j ; |r_ab|
__m256d r_ab_x = _mm256_sub_pd(pix, pjx);
__m256d r_ab_y = _mm256_sub_pd(piy, pjy);
__m256d r_ab_n2 = _mm256_fmadd_pd(r_ab_x, r_ab_x, _mm256_mul_pd(r_ab_y, r_ab_y));
__m256d r_ab_n = _mm256_sqrt_pd(r_ab_n2);
__m256d r_ab_x = _mm256_sub_pd(pix, pjx); // # 4 ADDs
__m256d r_ab_y = _mm256_sub_pd(piy, pjy); // # 4 ADDs
__m256d r_ab_n2 = _mm256_fmadd_pd(r_ab_x, r_ab_x, _mm256_mul_pd(r_ab_y, r_ab_y)); // # 8 MULTs, 4 ADDs
__m256d r_ab_n = _mm256_sqrt_pd(r_ab_n2); // # 4 SQRTs
// double r_ab2_x = r_ab_x - sb_e_x[j];
// double r_ab2_y = r_ab_y - sb_e_y[j];
@@ -320,21 +327,21 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
// double r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
// r_ab2 = r_ab - sb_e_j ; |r_ab2|
__m256d r_ab2_x = _mm256_sub_pd(r_ab_x, sbe_jx);
__m256d r_ab2_y = _mm256_sub_pd(r_ab_y, sbe_jy);
__m256d r_ab2_n2 = _mm256_fmadd_pd(r_ab2_x, r_ab2_x, _mm256_mul_pd(r_ab2_y, r_ab2_y));
__m256d r_ab2_n = _mm256_sqrt_pd(r_ab2_n2);
__m256d r_ab2_x = _mm256_sub_pd(r_ab_x, sbe_jx); // # 4 ADDs
__m256d r_ab2_y = _mm256_sub_pd(r_ab_y, sbe_jy); // # 4 ADDs
__m256d r_ab2_n2 = _mm256_fmadd_pd(r_ab2_x, r_ab2_x, _mm256_mul_pd(r_ab2_y, r_ab2_y)); // # 8 MULTs, 4 ADDs
__m256d r_ab2_n = _mm256_sqrt_pd(r_ab2_n2); // # 4 SQRTs
// 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);
// 2b = sqrt((|r_ab| + |r_ab2|)^2 - sb_sq_j)
__m256d r_sum = _mm256_add_pd(r_ab_n, r_ab2_n);
__m256d two_b_sq = _mm256_fmsub_pd(r_sum, r_sum, sb_jsq);
__m256d two_b = _mm256_sqrt_pd(two_b_sq);
__m256d r_sum = _mm256_add_pd(r_ab_n, r_ab2_n); // # 4 ADDs
__m256d two_b_sq = _mm256_fmsub_pd(r_sum, r_sum, sb_jsq); // # 4 MULTs, 4 ADDs
__m256d two_b = _mm256_sqrt_pd(two_b_sq); // # 4 SQRTs
__m256d e_arg = _mm256_mul_pd(two_b, v_neg_2s_inv);
__m256d e_arg = _mm256_mul_pd(two_b, v_neg_2s_inv); // # 4 MULTs
// we used to call libm exp() 4 times into a scratch array here, now do it in simd
// alignas(32) double tmp_arg[4], tmp_val[4];
// _mm256_store_pd(tmp_arg, e_arg);
@@ -343,109 +350,109 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
// tmp_val[2] = exp(tmp_arg[2]);
// tmp_val[3] = exp(tmp_arg[3]);
// __m256d e_val = _mm256_load_pd(tmp_val);
__m256d e_val = exp256_pd(e_arg);
__m256d e_val = exp256_pd(e_arg); // # 64 MULTs, 56 ADDs
// 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));
// smc = (r_sum * NEG_V0_AB_div_TWO_SIGMA * e_val) / (two_b * r_ab_n * r_ab2_n)
// the i==j lane collapses denom to 0, so we swap in 1.0 there to keep smc finite.
// the AND with active zeroes the contribution from that lane anyway.
__m256d numer = _mm256_mul_pd(_mm256_mul_pd(r_sum, v_neg_v0_div_2s), e_val);
__m256d denom = _mm256_mul_pd(_mm256_mul_pd(two_b, r_ab_n), r_ab2_n);
__m256d denom_safe = _mm256_blendv_pd(_mm256_set1_pd(1.0), denom, active);
__m256d smc = _mm256_div_pd(numer, denom_safe);
__m256d numer = _mm256_mul_pd(_mm256_mul_pd(r_sum, v_neg_v0_div_2s), e_val); // # 8 MULTs
__m256d denom = _mm256_mul_pd(_mm256_mul_pd(two_b, r_ab_n), r_ab2_n); // # 8 MULTs
__m256d denom_safe = _mm256_blendv_pd(_mm256_set1_pd(1.0), denom, active); // bitwise, no flop
__m256d smc = _mm256_div_pd(numer, denom_safe); // # 4 DIVs
// double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
__m256d sm_r_ab = _mm256_mul_pd(smc, r_ab2_n);
__m256d sm_r_ab2 = _mm256_mul_pd(smc, r_ab_n);
__m256d sm_r_ab = _mm256_mul_pd(smc, r_ab2_n); // # 4 MULTs
__m256d sm_r_ab2 = _mm256_mul_pd(smc, r_ab_n); // # 4 MULTs
// 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;
// scalar wants r_ab * sm_r_ab + r_ab2 * sm_r_ab2, the previous fma had the layout wrong
__m256d f_x = _mm256_fmadd_pd(r_ab2_x, sm_r_ab2, _mm256_mul_pd(r_ab_x, sm_r_ab));
__m256d f_y = _mm256_fmadd_pd(r_ab2_y, sm_r_ab2, _mm256_mul_pd(r_ab_y, sm_r_ab));
__m256d f_x = _mm256_fmadd_pd(r_ab2_x, sm_r_ab2, _mm256_mul_pd(r_ab_x, sm_r_ab)); // # 8 MULTs, 4 ADDs
__m256d f_y = _mm256_fmadd_pd(r_ab2_y, sm_r_ab2, _mm256_mul_pd(r_ab_y, sm_r_ab)); // # 8 MULTs, 4 ADDs
// w_val = dt_neg by default, c_neg_times_dt when (a >= 0 && a*a > b_sq)
__m256d a = _mm256_mul_pd(_mm256_fmadd_pd(eix, f_x, _mm256_mul_pd(eiy, f_y)), v_cos_phi_inv);
__m256d b_sq = _mm256_fmadd_pd(f_x, f_x, _mm256_mul_pd(f_y, f_y));
__m256d a_sq = _mm256_mul_pd(a, a);
__m256d a = _mm256_mul_pd(_mm256_fmadd_pd(eix, f_x, _mm256_mul_pd(eiy, f_y)), v_cos_phi_inv); // # 12 MULTs, 4 ADDs
__m256d b_sq = _mm256_fmadd_pd(f_x, f_x, _mm256_mul_pd(f_y, f_y)); // # 8 MULTs, 4 ADDs
__m256d a_sq = _mm256_mul_pd(a, a); // # 4 MULTs
__m256d c1 = _mm256_cmp_pd(a, v_zero, _CMP_GE_OQ);
__m256d c2 = _mm256_cmp_pd(a_sq, b_sq, _CMP_GT_OQ);
__m256d cond = _mm256_and_pd(c1, c2);
__m256d c1 = _mm256_cmp_pd(a, v_zero, _CMP_GE_OQ);
__m256d c2 = _mm256_cmp_pd(a_sq, b_sq, _CMP_GT_OQ);
__m256d cond = _mm256_and_pd(c1, c2);
__m256d w_val = _mm256_blendv_pd(v_dt_neg, v_c_neg_dt, cond);
// zero out inactive lanes (i==j and padding) before accumulating.
// AND with all-zero mask = 0, AND with all-one mask passes through. cheaper than blendv here.
__m256d f_x_safe = _mm256_and_pd(f_x, active);
__m256d f_y_safe = _mm256_and_pd(f_y, active);
acc_x = _mm256_fmadd_pd(f_x_safe, w_val, acc_x);
acc_y = _mm256_fmadd_pd(f_y_safe, w_val, acc_y);
__m256d f_x_safe = _mm256_and_pd(f_x, active); // bitwise, no flop
__m256d f_y_safe = _mm256_and_pd(f_y, active); // bitwise, no flop
acc_x = _mm256_fmadd_pd(f_x_safe, w_val, acc_x); // # 4 MULTs, 4 ADDs
acc_y = _mm256_fmadd_pd(f_y_safe, w_val, acc_y); // # 4 MULTs, 4 ADDs
}
// horizontal sum acc_x, acc_y into the scalar f_alpha_dt
__m256d hsum_xy = _mm256_hadd_pd(acc_x, acc_y); // [x0+x1, y0+y1, x2+x3, y2+y3]
__m128d lo = _mm256_castpd256_pd128(hsum_xy);
__m128d hi = _mm256_extractf128_pd(hsum_xy, 1);
__m128d sum_xy = _mm_add_pd(lo, hi); // [x_total, y_total]
__m256d hsum_xy = _mm256_hadd_pd(acc_x, acc_y); // [x0+x1, y0+y1, x2+x3, y2+y3] // # 4 ADDs
__m128d lo = _mm256_castpd256_pd128(hsum_xy);
__m128d hi = _mm256_extractf128_pd(hsum_xy, 1);
__m128d sum_xy = _mm_add_pd(lo, hi); // [x_total, y_total] // # 2 ADDs
alignas(16) double sum_out[2];
_mm_store_pd(sum_out, sum_xy);
f_alpha_dt_x += sum_out[0];
f_alpha_dt_y += sum_out[1];
f_alpha_dt_x += sum_out[0]; // # 1 ADD
f_alpha_dt_y += sum_out[1]; // # 1 ADD
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 v1_x = pos_x[i] - board.obstacles[k].start.x; // # 1 ADD
double v1_y = pos_y[i] - board.obstacles[k].start.y; // # 1 ADD
double projection = (v1_x * board.obstacles[k].obstacle_v.x + v1_y * board.obstacles[k].obstacle_v.y) * board.obstacles[k].length_sq_inv;
double projection = (v1_x * board.obstacles[k].obstacle_v.x + v1_y * board.obstacles[k].obstacle_v.y) * board.obstacles[k].length_sq_inv; // # 3 MULTs, 1 ADD
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_x = pos_x[i] - (board.obstacles[k].start.x + board.obstacles[k].obstacle_v.x * projection); // # 1 MULT, 2 ADDs
double r_aB_y = pos_y[i] - (board.obstacles[k].start.y + board.obstacles[k].obstacle_v.y * projection); // # 1 MULT, 2 ADDs
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 r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y); // # 2 MULTs, 1 ADD, 1 SQRT
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm; // # 2 MULTs, 1 EXP, 1 DIV
double f_alpha_B_neg_x = r_aB_x * scalar_mult; // # 1 MULT
double f_alpha_B_neg_y = r_aB_y * scalar_mult; // # 1 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;
double a = (e_x[i] * f_alpha_B_neg_x + e_y[i] * f_alpha_B_neg_y) * cos_phi_inv; // # 3 MULTs, 1 ADD
double b_sq = f_alpha_B_neg_x * f_alpha_B_neg_x + f_alpha_B_neg_y * f_alpha_B_neg_y; // # 2 MULTs, 1 ADD
w_val = dt_neg;
if (a >= 0 && (a * a > b_sq)) {
if (a >= 0 && (a * a > b_sq)) { // # 1 MULT for a*a
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;
double f_alpha_B_dt_x = f_alpha_B_neg_x * w_val; // # 1 MULT
double f_alpha_B_dt_y = f_alpha_B_neg_y * w_val; // # 1 MULT
f_alpha_dt_x += f_alpha_B_dt_x;
f_alpha_dt_y += f_alpha_B_dt_y;
f_alpha_dt_x += f_alpha_B_dt_x; // # 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y; // # 1 ADD
}
w_x[i] += f_alpha_dt_x;
w_y[i] += f_alpha_dt_y;
w_x[i] += f_alpha_dt_x; // # 1 ADD
w_y[i] += f_alpha_dt_y; // # 1 ADD
}
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 w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]); // # 2 MULTs, 1 ADD, 1 SQRT
double correction_factor = min(1.0, v_max[i] / w_norm);
double correction_factor = min(1.0, v_max[i] / w_norm); // # 1 DIV
curr_vel_x[i] = w_x[i] * correction_factor;
curr_vel_y[i] = w_y[i] * correction_factor;
curr_vel_x[i] = w_x[i] * correction_factor; // # 1 MULT
curr_vel_y[i] = w_y[i] * correction_factor; // # 1 MULT
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt;
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt;
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt; // # 1 MULT, 1 ADD
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt; // # 1 MULT, 1 ADD
if (color[i] == 0 && pos_x[i] > (BOARD_LENGTH - 1.5)) {
pos_x[i] = 1.0;
@@ -466,22 +473,22 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
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 ex = destination_x[i] - pos_x[i]; // # 1 ADD
double ey = destination_y[i] - pos_y[i]; // # 1 ADD
double e_norm_inv = 1.0 / sqrt(ex * ex + ey * ey);
double e_norm_inv = 1.0 / sqrt(ex * ex + ey * ey); // # 2 MULTs, 1 ADD, 1 SQRT, 1 DIV
e_x[i] = ex * e_norm_inv;
e_y[i] = ey * e_norm_inv;
e_x[i] = ex * e_norm_inv; // # 1 MULT
e_y[i] = ey * e_norm_inv; // # 1 MULT
double velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i];
double velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i]; // # 2 MULTs, 1 ADD
double velocity_norm = sqrt(velocity_norm_sq);
double velocity_norm = sqrt(velocity_norm_sq); // # 1 SQRT
sb_sq[i] = velocity_norm_sq * delta_t_sq;
sb_sq[i] = velocity_norm_sq * delta_t_sq; // # 1 MULT
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t);
sb_e_y[i] = e_y[i] * (velocity_norm * delta_t);
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t); // # 2 MULTs (vel_norm*delta_t shared with next line)
sb_e_y[i] = e_y[i] * (velocity_norm * delta_t); // # 1 MULT
#if !BENCHMARK
if (dump_path && (t % dump_interval == 0)) {
@@ -1,513 +0,0 @@
// # This file uses avx2
// # builds on variant_inlining_7_SoA_AVX2 by killing the 4 scalar libm exp() calls
// # in the j loop and doing it in simd with a polynomial approximation instead.
// 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)
// Now in a second step we wanna really use AVX2 for the mainloop (where we wana optimize the most innerloop first)
// next step now: the scalar exp() inside the j loop was ~54% of inner-loop cycles, we replace with one simd call.
#include "../headers/board.hpp"
#include "../headers/constants.hpp"
#include "../headers/pedestrian_reduced.hpp"
#include <immintrin.h> // to use avx2
#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);
const int n_p_pad = (n_p + 3) & ~3; // round up to a multiple of 4 (so its easy to use avx)
// resize all our arrays for this run
pos_x.resize(n_p_pad);
pos_y.resize(n_p_pad);
sb_sq.resize(n_p_pad);
sb_e_x.resize(n_p_pad);
sb_e_y.resize(n_p_pad);
e_x.resize(n_p_pad);
e_y.resize(n_p_pad);
curr_vel_x.resize(n_p_pad);
curr_vel_y.resize(n_p_pad);
w_x.resize(n_p_pad);
w_y.resize(n_p_pad);
desired_v0.resize(n_p_pad);
v_max.resize(n_p_pad);
destination_x.resize(n_p_pad);
destination_y.resize(n_p_pad);
color.resize(n_p_pad);
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;
}
// Padding lanes: positions far away, everything else zero. Any unmasked SIMD
// contribution from these lanes will vanish through exp(very negative) roughly 0.
for (int j = n_p; j < n_p_pad; j++) {
pos_x[j] = 1e9;
pos_y[j] = 1e9;
sb_sq[j] = 0.0;
sb_e_x[j] = 0.0;
sb_e_y[j] = 0.0;
}
return nullptr;
}
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// simd expd, degree-12 taylor on the [-ln2/2, ln2/2] reduced range,
// schraudolph 2^n bit trick on the exponent slot.
// # 64 MULTs, 56 ADDs
static inline __m256d exp256_pd(__m256d x) {
const __m256d log2e = _mm256_set1_pd(1.4426950408889634);
__m256d nf = _mm256_round_pd(_mm256_mul_pd(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC); // # 4 MULTs
const __m256d ln2 = _mm256_set1_pd(0.6931471805599453);
__m256d r = _mm256_fnmadd_pd(nf, ln2, x); // # 4 MULTs, 4 ADDs
const __m256d c13 = _mm256_set1_pd(c13_scalar);
const __m256d c12 = _mm256_set1_pd(c12_scalar);
const __m256d c11 = _mm256_set1_pd(c11_scalar);
const __m256d c10 = _mm256_set1_pd(c10_scalar);
const __m256d c9 = _mm256_set1_pd(c9_scalar);
const __m256d c8 = _mm256_set1_pd(c8_scalar);
const __m256d c7 = _mm256_set1_pd(c7_scalar);
const __m256d c6 = _mm256_set1_pd(c6_scalar);
const __m256d c5 = _mm256_set1_pd(c5_scalar);
const __m256d c4 = _mm256_set1_pd(c4_scalar);
const __m256d c3 = _mm256_set1_pd(c3_scalar);
const __m256d c2 = _mm256_set1_pd(c2_scalar);
const __m256d c1 = _mm256_set1_pd(c1_scalar);
const __m256d c0 = _mm256_set1_pd(c0_scalar);
__m256d p = c13;
// # 13 * (4 MULTs, 4 ADDs)
// # = 52 MULTs, 52 ADDs
p = _mm256_fmadd_pd(p, r, c12);
p = _mm256_fmadd_pd(p, r, c11);
p = _mm256_fmadd_pd(p, r, c10);
p = _mm256_fmadd_pd(p, r, c9);
p = _mm256_fmadd_pd(p, r, c8);
p = _mm256_fmadd_pd(p, r, c7);
p = _mm256_fmadd_pd(p, r, c6);
p = _mm256_fmadd_pd(p, r, c5);
p = _mm256_fmadd_pd(p, r, c4);
p = _mm256_fmadd_pd(p, r, c3);
p = _mm256_fmadd_pd(p, r, c2);
p = _mm256_fmadd_pd(p, r, c1);
p = _mm256_fmadd_pd(p, r, c0);
__m128i ni32 = _mm256_cvtpd_epi32(nf);
__m256i ni64 = _mm256_cvtepi32_epi64(ni32);
__m256i biased = _mm256_add_epi64(ni64, _mm256_set1_epi64x(1023));
__m256i scaled = _mm256_slli_epi64(biased, 52);
__m256d pow2n = _mm256_castsi256_pd(scaled);
return _mm256_mul_pd(p, pow2n); // # 4 MULTs
}
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
// all the constants for the AVX2 setup
const int n_p_pad = (n_p + 3) & ~3;
alignas(32) const double lane_offset_arr[4] = {0.0, 1.0, 2.0, 3.0};
const __m256d lane_off = _mm256_load_pd(lane_offset_arr);
const __m256d v_neg_v0_div_2s = _mm256_set1_pd(NEG_V0_AB_div_TWO_SIGMA);
const __m256d v_neg_2s_inv = _mm256_set1_pd(NEG_TWO_SIGMA_INV);
const __m256d v_cos_phi_inv = _mm256_set1_pd(cos_phi_inv);
const __m256d v_dt_neg = _mm256_set1_pd(dt_neg);
const __m256d v_c_neg_dt = _mm256_set1_pd(c_neg_times_dt);
const __m256d v_zero = _mm256_setzero_pd();
const __m256d v_np = _mm256_set1_pd((double)n_p);
// # flops per iteration (one pass of the outer time loop):
// # ADDs: n_p * 12 + n_p * n_p_pad * 29 + n_p * board.n_obstacles * 12 + n_p * 7
// # MULTs: n_p * 4 + n_p * n_p_pad * 40 + n_p * board.n_obstacles * 19 + n_p * 16
// # DIVs: n_p * n_p_pad * 1 + n_p * board.n_obstacles + n_p * 2
// # SQRTs: n_p * n_p_pad * 3 + n_p * board.n_obstacles + n_p * 3
// # EXPs: n_p * board.n_obstacles
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]; // # 1 MULT
double desired_velocity_y = e_y[i] * desired_v0[i]; // # 1 MULT
double f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
double f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
// since i is fixed for the innermost loops we now wanna set all those variables
const __m256d pix = _mm256_set1_pd(pos_x[i]);
const __m256d piy = _mm256_set1_pd(pos_y[i]);
const __m256d eix = _mm256_set1_pd(e_x[i]);
const __m256d eiy = _mm256_set1_pd(e_y[i]);
const __m256d v_i = _mm256_set1_pd((double)i);
__m256d acc_x = _mm256_setzero_pd();
__m256d acc_y = _mm256_setzero_pd();
for (int j = 0; j < n_p_pad; j += 4) {
// 4 lanes at once, lane k maps to pedestrian j+k
__m256d j_idx = _mm256_add_pd(_mm256_set1_pd((double)j), lane_off); // # 4 ADDs
// active = (j+k != i) AND (j+k < n_p), zero in lanes we wanna skip
__m256d m_neq_i = _mm256_cmp_pd(j_idx, v_i, _CMP_NEQ_OQ);
__m256d m_in_range = _mm256_cmp_pd(j_idx, v_np, _CMP_LT_OQ);
__m256d active = _mm256_and_pd(m_neq_i, m_in_range);
// j-side loads, SoA contiguous. loadu because std::vector only guarantees 16B align.
__m256d pjx = _mm256_loadu_pd(&pos_x[j]);
__m256d pjy = _mm256_loadu_pd(&pos_y[j]);
__m256d sbe_jx = _mm256_loadu_pd(&sb_e_x[j]);
__m256d sbe_jy = _mm256_loadu_pd(&sb_e_y[j]);
__m256d sb_jsq = _mm256_loadu_pd(&sb_sq[j]);
// 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);
// r_ab = pos_i - pos_j ; |r_ab|
__m256d r_ab_x = _mm256_sub_pd(pix, pjx); // # 4 ADDs
__m256d r_ab_y = _mm256_sub_pd(piy, pjy); // # 4 ADDs
__m256d r_ab_n2 = _mm256_fmadd_pd(r_ab_x, r_ab_x, _mm256_mul_pd(r_ab_y, r_ab_y)); // # 8 MULTs, 4 ADDs
__m256d r_ab_n = _mm256_sqrt_pd(r_ab_n2); // # 4 SQRTs
// 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);
// r_ab2 = r_ab - sb_e_j ; |r_ab2|
__m256d r_ab2_x = _mm256_sub_pd(r_ab_x, sbe_jx); // # 4 ADDs
__m256d r_ab2_y = _mm256_sub_pd(r_ab_y, sbe_jy); // # 4 ADDs
__m256d r_ab2_n2 = _mm256_fmadd_pd(r_ab2_x, r_ab2_x, _mm256_mul_pd(r_ab2_y, r_ab2_y)); // # 8 MULTs, 4 ADDs
__m256d r_ab2_n = _mm256_sqrt_pd(r_ab2_n2); // # 4 SQRTs
// 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);
// 2b = sqrt((|r_ab| + |r_ab2|)^2 - sb_sq_j)
__m256d r_sum = _mm256_add_pd(r_ab_n, r_ab2_n); // # 4 ADDs
__m256d two_b_sq = _mm256_fmsub_pd(r_sum, r_sum, sb_jsq); // # 4 MULTs, 4 ADDs
__m256d two_b = _mm256_sqrt_pd(two_b_sq); // # 4 SQRTs
__m256d e_arg = _mm256_mul_pd(two_b, v_neg_2s_inv); // # 4 MULTs
// we used to call libm exp() 4 times into a scratch array here, now do it in simd
// alignas(32) double tmp_arg[4], tmp_val[4];
// _mm256_store_pd(tmp_arg, e_arg);
// tmp_val[0] = exp(tmp_arg[0]);
// tmp_val[1] = exp(tmp_arg[1]);
// tmp_val[2] = exp(tmp_arg[2]);
// tmp_val[3] = exp(tmp_arg[3]);
// __m256d e_val = _mm256_load_pd(tmp_val);
__m256d e_val = exp256_pd(e_arg); // # 64 MULTs, 56 ADDs
// 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));
// smc = (r_sum * NEG_V0_AB_div_TWO_SIGMA * e_val) / (two_b * r_ab_n * r_ab2_n)
// the i==j lane collapses denom to 0, so we swap in 1.0 there to keep smc finite.
// the AND with active zeroes the contribution from that lane anyway.
__m256d numer = _mm256_mul_pd(_mm256_mul_pd(r_sum, v_neg_v0_div_2s), e_val); // # 8 MULTs
__m256d denom = _mm256_mul_pd(_mm256_mul_pd(two_b, r_ab_n), r_ab2_n); // # 8 MULTs
__m256d denom_safe = _mm256_blendv_pd(_mm256_set1_pd(1.0), denom, active); // bitwise, no flop
__m256d smc = _mm256_div_pd(numer, denom_safe); // # 4 DIVs
// double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
__m256d sm_r_ab = _mm256_mul_pd(smc, r_ab2_n); // # 4 MULTs
__m256d sm_r_ab2 = _mm256_mul_pd(smc, r_ab_n); // # 4 MULTs
// 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;
// scalar wants r_ab * sm_r_ab + r_ab2 * sm_r_ab2, the previous fma had the layout wrong
__m256d f_x = _mm256_fmadd_pd(r_ab2_x, sm_r_ab2, _mm256_mul_pd(r_ab_x, sm_r_ab)); // # 8 MULTs, 4 ADDs
__m256d f_y = _mm256_fmadd_pd(r_ab2_y, sm_r_ab2, _mm256_mul_pd(r_ab_y, sm_r_ab)); // # 8 MULTs, 4 ADDs
// w_val = dt_neg by default, c_neg_times_dt when (a >= 0 && a*a > b_sq)
__m256d a = _mm256_mul_pd(_mm256_fmadd_pd(eix, f_x, _mm256_mul_pd(eiy, f_y)), v_cos_phi_inv); // # 12 MULTs, 4 ADDs
__m256d b_sq = _mm256_fmadd_pd(f_x, f_x, _mm256_mul_pd(f_y, f_y)); // # 8 MULTs, 4 ADDs
__m256d a_sq = _mm256_mul_pd(a, a); // # 4 MULTs
__m256d c1 = _mm256_cmp_pd(a, v_zero, _CMP_GE_OQ);
__m256d c2 = _mm256_cmp_pd(a_sq, b_sq, _CMP_GT_OQ);
__m256d cond = _mm256_and_pd(c1, c2);
__m256d w_val = _mm256_blendv_pd(v_dt_neg, v_c_neg_dt, cond);
// zero out inactive lanes (i==j and padding) before accumulating.
// AND with all-zero mask = 0, AND with all-one mask passes through. cheaper than blendv here.
__m256d f_x_safe = _mm256_and_pd(f_x, active); // bitwise, no flop
__m256d f_y_safe = _mm256_and_pd(f_y, active); // bitwise, no flop
acc_x = _mm256_fmadd_pd(f_x_safe, w_val, acc_x); // # 4 MULTs, 4 ADDs
acc_y = _mm256_fmadd_pd(f_y_safe, w_val, acc_y); // # 4 MULTs, 4 ADDs
}
// horizontal sum acc_x, acc_y into the scalar f_alpha_dt
__m256d hsum_xy = _mm256_hadd_pd(acc_x, acc_y); // [x0+x1, y0+y1, x2+x3, y2+y3] // # 4 ADDs
__m128d lo = _mm256_castpd256_pd128(hsum_xy);
__m128d hi = _mm256_extractf128_pd(hsum_xy, 1);
__m128d sum_xy = _mm_add_pd(lo, hi); // [x_total, y_total] // # 2 ADDs
alignas(16) double sum_out[2];
_mm_store_pd(sum_out, sum_xy);
f_alpha_dt_x += sum_out[0]; // # 1 ADD
f_alpha_dt_y += sum_out[1]; // # 1 ADD
for (int k = 0; k < board.n_obstacles; k++) {
double v1_x = pos_x[i] - board.obstacles[k].start.x; // # 1 ADD
double v1_y = pos_y[i] - board.obstacles[k].start.y; // # 1 ADD
double projection = (v1_x * board.obstacles[k].obstacle_v.x + v1_y * board.obstacles[k].obstacle_v.y) * board.obstacles[k].length_sq_inv; // # 3 MULTs, 1 ADD
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); // # 1 MULT, 2 ADDs
double r_aB_y = pos_y[i] - (board.obstacles[k].start.y + board.obstacles[k].obstacle_v.y * projection); // # 1 MULT, 2 ADDs
double r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y); // # 2 MULTs, 1 ADD, 1 SQRT
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm; // # 2 MULTs, 1 EXP, 1 DIV
double f_alpha_B_neg_x = r_aB_x * scalar_mult; // # 1 MULT
double f_alpha_B_neg_y = r_aB_y * scalar_mult; // # 1 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; // # 3 MULTs, 1 ADD
double b_sq = f_alpha_B_neg_x * f_alpha_B_neg_x + f_alpha_B_neg_y * f_alpha_B_neg_y; // # 2 MULTs, 1 ADD
w_val = dt_neg;
if (a >= 0 && (a * a > b_sq)) { // # 1 MULT for a*a
w_val = c_neg_times_dt;
}
double f_alpha_B_dt_x = f_alpha_B_neg_x * w_val; // # 1 MULT
double f_alpha_B_dt_y = f_alpha_B_neg_y * w_val; // # 1 MULT
f_alpha_dt_x += f_alpha_B_dt_x; // # 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y; // # 1 ADD
}
w_x[i] += f_alpha_dt_x; // # 1 ADD
w_y[i] += f_alpha_dt_y; // # 1 ADD
}
for (int i = 0; i < n_p; i++) {
double w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]); // # 2 MULTs, 1 ADD, 1 SQRT
double correction_factor = min(1.0, v_max[i] / w_norm); // # 1 DIV
curr_vel_x[i] = w_x[i] * correction_factor; // # 1 MULT
curr_vel_y[i] = w_y[i] * correction_factor; // # 1 MULT
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt; // # 1 MULT, 1 ADD
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt; // # 1 MULT, 1 ADD
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]; // # 1 ADD
double ey = destination_y[i] - pos_y[i]; // # 1 ADD
double e_norm_inv = 1.0 / sqrt(ex * ex + ey * ey); // # 2 MULTs, 1 ADD, 1 SQRT, 1 DIV
e_x[i] = ex * e_norm_inv; // # 1 MULT
e_y[i] = ey * e_norm_inv; // # 1 MULT
double velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i]; // # 2 MULTs, 1 ADD
double velocity_norm = sqrt(velocity_norm_sq); // # 1 SQRT
sb_sq[i] = velocity_norm_sq * delta_t_sq; // # 1 MULT
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t); // # 2 MULTs (vel_norm*delta_t shared with next line)
sb_e_y[i] = e_y[i] * (velocity_norm * delta_t); // # 1 MULT
#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;
}
@@ -1,423 +0,0 @@
// # Use symmetrical properties to reduce flop count
// # Changes: flopcount
// FLOPCOUNT = n_iter * ( 69 * n_p ** 2 - 32 * n_p + 63 * n_obstacles * n_p )
// n_iter * [ n_p ** 2 * (12.5 ADD + 23 MULT + 1 DIV + 1 EXP + 2.5 SQRT) + n_p * (- 1.5 ADD + 0.5 SQRT - 2 MULT - 1 EXP + 1 DIV) + n_obstacles * n_p * (12 ADD + 19 MULT + 1 SQRT + 1 DIV + 1 EXP) ]
// # Predecessor: variant_inlining_7_symmetry_1.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]
// FLOPCOUNT = n_iter * ( {0.5 * (n_p * n_p - n_p) * 6 FLOPS} + {n_p * [ (8 FLOPS) + ((n_p - 1) * 66 FLOPS) + (n_obstacles * 63 FLOPS) ]} + {n_p * 29 FLOPS} )
// FLOPCOUNT = n_iter * ( {3 * n_p ** 2 - 3 * n_p} + {66 * n_p ** 2 - 58 * n_p + 63 * n_obstacles * n_p} + {29 * n_p} )
// FLOPCOUNT = n_iter * ( 3 * n_p ** 2 - 3 * n_p + 66 * n_p ** 2 - 58 * n_p + 63 * n_obstacles * n_p + 29 * n_p)
// FLOPCOUNT = n_iter * ( [3 * n_p ** 2 + 66 * n_p ** 2] + [- 3 * n_p - 58 * n_p + 29 * n_p] + [63 * n_obstacles * n_p] )
// FLOPCOUNT = n_iter * ( 69 * n_p ** 2 - 32 * n_p + 63 * n_obstacles * n_p )
// n_iter * {0.5 * (n_p * n_p - n_p) * (3 ADD + 1 SQRT + 2 MULT) + n_p * [(4 MULT + 4 ADD) + ((n_p - 1) * (11 ADD + 22 MULT + 1 DIV + 1 EXP + 2 SQRT)) + (n_obstacles * (12 ADD + 19 MULT + 1 SQRT + 1 DIV + 1 EXP))] + n_p * [ 7 ADD + 17 MULT + 3 SQRT + 2 DIV ]}
// 0.5 * (n_p * n_p - n_p) * (3 ADD + 1 SQRT + 2 MULT)
// n_p ** 2 * (1.5 ADD + 0.5 SQRT + 1 MULT) + n_p * (- 1.5 ADD - 0.5 SQRT - 1 MULT)
// n_p * [(4 MULT + 4 ADD) + ((n_p - 1) * (11 ADD + 22 MULT + 1 DIV + 1 EXP + 2 SQRT)) + (n_obstacles * (12 ADD + 19 MULT + 1 SQRT + 1 DIV + 1 EXP))]
// n_p * [n_p * (11 ADD + 22 MULT + 1 DIV + 1 EXP + 2 SQRT) - 1 * (7 ADD + 18 MULT + 1 DIV + 1 EXP + 2 SQRT) + n_obstacles * (12 ADD + 19 MULT + 1 SQRT + 1 DIV + 1 EXP)]
// n_p ** 2 * (11 ADD + 22 MULT + 1 DIV + 1 EXP + 2 SQRT) - n_p * (7 ADD + 18 MULT + 1 DIV + 1 EXP + 2 SQRT) + n_obstacles * n_p * (12 ADD + 19 MULT + 1 SQRT + 1 DIV + 1 EXP)
// n_p * (7 ADD + 17 MULT + 3 SQRT + 2 DIV)
// n_p ** 2 * (1.5 ADD + 0.5 SQRT + 1 MULT) + n_p * (- 1.5 ADD - 0.5 SQRT - 1 MULT) + n_p ** 2 * (11 ADD + 22 MULT + 1 DIV + 1 EXP + 2 SQRT) - n_p * (7 ADD + 18 MULT + 1 DIV + 1 EXP + 2 SQRT) + n_obstacles * n_p * (12 ADD + 19 MULT + 1 SQRT + 1 DIV + 1 EXP) + n_p * (7 ADD + 17 MULT + 3 SQRT + 2 DIV)
// n_p ** 2 * (12.5 ADD + 23 MULT + 1 DIV + 1 EXP + 2.5 SQRT) + n_p * (- 1.5 ADD + 0.5 SQRT - 2 MULT - 1 EXP + 1 DIV) + n_obstacles * n_p * (12 ADD + 19 MULT + 1 SQRT + 1 DIV + 1 EXP)
// n_p ** 2 * (12.5 A + 23 M + 1 D + 1 E + 2.5 S) + n_p * (- 1.5 A + 0.5 S - 2 M - 1 E + 1 D) + n_obstacles * n_p * (12 A + 19 M + 1 S + 1 D + 1 E)
for (int t = 0; t < n_iter; t++) {
// get all distances first
// total iterations = 0.5 * (n_p * n_p - n_p)
// flops per iter = 6 FLOPS
// FLOPCOUNT = 0.5 * (n_p * n_p - n_p) * 6 FLOPS
// 0.5 * (n_p * n_p - n_p) * (3 ADD + 1 SQRT + 2 MULT)
for (int i = 0; i < n_p; i++) {
for (int j = i + 1; j < n_p; j++) {
// 1 FLOP = 1 ADD
double dx = pedestrians[i].curr_pos_x - pedestrians[j].curr_pos_x;
// 1 FLOP = 1 ADD
double dy = pedestrians[i].curr_pos_y - pedestrians[j].curr_pos_y;
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
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;
}
}
// FLOPCOUNT = n_p * [ (8 FLOPS) + ((n_p - 1) * 66 FLOPS) + (n_obstacles * 63 FLOPS) ]
// n_p * [(4 MULT + 4 ADD) + ((n_p - 1) * (11 ADD + 22 MULT + 1 DIV + 1 EXP + 2 SQRT)) + (n_obstacles * (12 ADD + 19 MULT + 1 SQRT + 1 DIV + 1 EXP))]
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// 1 FLOP = 1 MULT
double desired_velocity_x = p.e_x * p.desired_v0;
// 1 FLOP = 1 MULT
double desired_velocity_y = p.e_y * p.desired_v0;
// 2 FLOPS = 1 ADD + 1 MULT
double f_alpha_dt_x = (desired_velocity_x - p.curr_vel_x) * dt_times_tau_alpha_inv;
// 2 FLOPS = 1 ADD + 1 MULT
double f_alpha_dt_y = (desired_velocity_y - p.curr_vel_y) * dt_times_tau_alpha_inv;
// force between pedestrians
// FLOPS = ((n_p - 1) * 66 FLOPS)
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];
}
// ----
// 1 FLOP = 1 ADD
double r_ab2_x = r_ab_x - pedestrians[j].s_b_e_x;
// 1 FLOP = 1 ADD
double r_ab2_y = r_ab_y - pedestrians[j].s_b_e_y;
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
double r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
// 1 FLOP = 1 ADD
double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
// 2 FLOPS = 1 MULT + 1 ADD
double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - pedestrians[j].s_b_sq;
// 1 FLOP = 1 SQRT
double two_b = sqrt(two_b_sq);
// 36 FLOPS = 1 DIV + 5 MULTS + 1 EXP
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));
// 1 FLOP = 1 MULT
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// 1 FLOP = 1 MULT
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
// 3 FLOPS = 2 MULT + 1 ADD
double f_alpha_beta_neg_x = r_ab_x * scalar_mult_r_ab + r_ab2_x * scalar_mult_r_ab2;
// 3 FLOPS = 2 MULT + 1 ADD
double f_alpha_beta_neg_y = r_ab_y * scalar_mult_r_ab + r_ab2_y * scalar_mult_r_ab2;
double w_val;
// 4 FLOPS = 3 MULT + 1 ADD
double a = (p.e_x * f_alpha_beta_neg_x + p.e_y * f_alpha_beta_neg_y) * cos_phi_inv;
// 3 FLOPS = 2 MULT + 1 ADD
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;
// 1 FLOP = 1 MULT
if (a >= 0 && (a * a > b_sq)) {
w_val = c_neg_times_dt;
}
// 2 FLOPS = 1 ADD + 1 MULT
f_alpha_dt_x = f_alpha_dt_x + f_alpha_beta_neg_x * w_val;
// 2 FLOPS = 1 ADD + 1 MULT
f_alpha_dt_y = f_alpha_dt_y + f_alpha_beta_neg_y * w_val;
}
// force between pedestrian and obstacles
// FLOPS = n_obstacles * 63 FLOPS
for (int k = 0; k < board.n_obstacles; k++) {
// 1 FLOP = 1 ADD
double v1_x = p.curr_pos_x - board.obstacles[k].start.x;
// 1 FLOP = 1 ADD
double v1_y = p.curr_pos_y - board.obstacles[k].start.y;
// 4 FLOPS = 3 MULT + 1 ADD
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;
// 3 FLOPS = 1 MULT + 2 ADD
double r_aB_x = p.curr_pos_x - (board.obstacles[k].start.x + board.obstacles[k].obstacle_v.x * projection);
// 3 FLOPS = 1 MULT + 2 ADD
double r_aB_y = p.curr_pos_y - (board.obstacles[k].start.y + board.obstacles[k].obstacle_v.y * projection);
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
double r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
// 33 FLOPS = 2 MULT + 1 DIV + 1 EXP
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// 1 FLOP = 1 MULT
double f_alpha_B_neg_x = r_aB_x * scalar_mult;
// 1 FLOP = 1 MULT
double f_alpha_B_neg_y = r_aB_y * scalar_mult;
double w_val;
// 4 FLOPS = 3 MULTS + 1 ADD
double a = (p.e_x * f_alpha_B_neg_x + p.e_y * f_alpha_B_neg_y) * cos_phi_inv;
// 3 FLOPS = 2 MULT + 1 ADD
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;
// 1 FLOP = 1 MULT
if (a >= 0 && (a * a > b_sq)) {
w_val = c_neg_times_dt;
}
// 1 FLOP = 1 MULT
double f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
// 1 FLOP = 1 MULT
double f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
// 1 FLOP = 1 ADD
f_alpha_dt_x += f_alpha_B_dt_x;
// 1 FLOP = 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y;
}
// 1 FLOP = 1 ADD
p.w_x += f_alpha_dt_x;
// 1 FLOP = 1 ADD
p.w_y += f_alpha_dt_y;
}
// update the positions etc.
// FLOPCOUNT = n_p * 29 FLOPS
// n_p * [ 7 ADD + 17 MULT + 3 SQRT + 2 DIV ]
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
double w_norm = sqrt(p.w_x * p.w_x + p.w_y * p.w_y);
// TODO: check correctness of this
// 1 FLOP = 1 DIV
double correction_factor = min(1.0, p.v_max / w_norm);
// 1 FLOP = 1 MULT
p.curr_vel_x = p.w_x * correction_factor;
// 1 FLOP = 1 MULT
p.curr_vel_y = p.w_y * correction_factor;
// 2 FLOPS = 1 ADD + 1 MULT
p.curr_pos_x = p.curr_pos_x + p.curr_vel_x * dt;
// 2 FLOPS = 1 ADD + 1 MULT
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);
// 1 FLOP = 1 ADD
double e_x = p.destination_x - p.curr_pos_x;
// 1 FLOP = 1 ADD
double e_y = p.destination_y - p.curr_pos_y;
// 5 FLOPS = 1 DIV + 1 SQRT + 2 MULT + 1 ADD
double e_norm_inv = 1.0 / sqrt(e_x * e_x + e_y * e_y);
// 1 FLOP = 1 MULT
p.e_x = e_x * e_norm_inv;
// 1 FLOP = 1 MULT
p.e_y = e_y * e_norm_inv;
// 3 FLOPS = 2 MULT + 1 ADD
double velocity_norm_sq = p.curr_vel_x * p.curr_vel_x + p.curr_vel_y * p.curr_vel_y;
// 1 FLOP = 1 SQRT
double velocity_norm = sqrt(velocity_norm_sq);
// 1 FLOP = 1 MULT
p.s_b_sq = velocity_norm_sq * delta_t_sq;
// 2 FLOPS = 2 MULT
p.s_b_e_x = p.e_x * (velocity_norm * delta_t);
// 2 FLOP = 2 MULT
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;
}
@@ -1,385 +0,0 @@
// # Use symmetrical properties to reduce flop count
// # Changes: flopcount
// FLOPCOUNT = n_iter * ( 69 * n_p ** 2 - 32 * n_p + 63 * n_obstacles * n_p )
// n_iter * [ n_p ** 2 * (12.5 ADD + 23 MULT + 1 DIV + 1 EXP + 2.5 SQRT) + n_p * (- 1.5 ADD + 0.5 SQRT - 2 MULT - 1 EXP + 1 DIV) + n_obstacles * n_p * (12 ADD + 19 MULT + 1 SQRT + 1 DIV + 1 EXP) ]
// # Predecessor: variant_inlining_7_symmetry_2.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]
// FLOPCOUNT = n_iter * ( {0.5 * (n_p * n_p - n_p) * 6 FLOPS} + {n_p * [ (8 FLOPS) + ((n_p - 1) * 66 FLOPS) + (n_obstacles * 63 FLOPS) ]} + {n_p * 29 FLOPS} )
// FLOPCOUNT = n_iter * ( {3 * n_p ** 2 - 3 * n_p} + {66 * n_p ** 2 - 58 * n_p + 63 * n_obstacles * n_p} + {29 * n_p} )
// FLOPCOUNT = n_iter * ( 3 * n_p ** 2 - 3 * n_p + 66 * n_p ** 2 - 58 * n_p + 63 * n_obstacles * n_p + 29 * n_p)
// FLOPCOUNT = n_iter * ( [3 * n_p ** 2 + 66 * n_p ** 2] + [- 3 * n_p - 58 * n_p + 29 * n_p] + [63 * n_obstacles * n_p] )
// FLOPCOUNT = n_iter * ( 69 * n_p ** 2 - 32 * n_p + 63 * n_obstacles * n_p )
for (int t = 0; t < n_iter; t++) {
// get all distances first
// FLOPCOUNT = 0.5 * (n_p * n_p - n_p) * 6 FLOPS
for (int i = 0; i < n_p; i++) {
for (int j = i + 1; j < n_p; j++) {
// 1 FLOP = 1 ADD
double dx = pedestrians[i].curr_pos_x - pedestrians[j].curr_pos_x;
// 1 FLOP = 1 ADD
double dy = pedestrians[i].curr_pos_y - pedestrians[j].curr_pos_y;
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
double norm = sqrt(dx * dx + dy * dy);
int idx = i * n_p + j;
r_ab_x_lut[idx] = dx;
r_ab_y_lut[idx] = dy;
r_ab_norm_lut[idx] = norm;
}
}
// FLOPCOUNT = n_p * [ 8 FLOPS + (((n_p - 1) * 66 FLOPS)) + (n_obstacles * 63 FLOPS) ]
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// 1 FLOP = 1 MULT
double desired_velocity_x = p.e_x * p.desired_v0;
// 1 FLOP = 1 MULT
double desired_velocity_y = p.e_y * p.desired_v0;
// 2 FLOPS = 1 ADD + 1 MULT
double f_alpha_dt_x = (desired_velocity_x - p.curr_vel_x) * dt_times_tau_alpha_inv;
// 2 FLOPS = 1 ADD + 1 MULT
double f_alpha_dt_y = (desired_velocity_y - p.curr_vel_y) * dt_times_tau_alpha_inv;
// force between pedestrians --> no symmetry because of different accellerations
// FLOPCOUNT = (((n_p - 1) * 66 FLOPS))
for (int j = 0; j < n_p; j++) {
if (i == j)
continue;
// symmetry
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];
}
// ----
// 1 FLOP = 1 ADD
double r_ab2_x = r_ab_x - pedestrians[j].s_b_e_x;
// 1 FLOP = 1 ADD
double r_ab2_y = r_ab_y - pedestrians[j].s_b_e_y;
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
double r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
// 1 FLOP = 1 ADD
double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
// 2 FLOPS = 1 MULT + 1 ADD
double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - pedestrians[j].s_b_sq;
// 1 FLOP = 1 SQRT
double two_b = sqrt(two_b_sq);
// 36 FLOPS = 1 DIV + 5 MULT + 1 EXP
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));
// 1 FLOP = 1 MULT
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// 1 FLOP = 1 MULT
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
// 3 FLOPS = 2 MULT + 1 ADD
double f_alpha_beta_neg_x = r_ab_x * scalar_mult_r_ab + r_ab2_x * scalar_mult_r_ab2;
// 3 FLOPS = 2 MULT + 1 ADD
double f_alpha_beta_neg_y = r_ab_y * scalar_mult_r_ab + r_ab2_y * scalar_mult_r_ab2;
double w_val;
// 4 FLOPS = 3 MULT + 1 ADD
double a = (p.e_x * f_alpha_beta_neg_x + p.e_y * f_alpha_beta_neg_y) * cos_phi_inv;
// 3 FLOPS = 2 MULT + 1 ADD
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;
// 1 FLOP = 1 MULT
if (a >= 0 && (a * a > b_sq)) {
w_val = c_neg_times_dt;
}
// 2 FLOPS = 1 ADD + 1 MULT
f_alpha_dt_x = f_alpha_dt_x + f_alpha_beta_neg_x * w_val;
// 2 FLOPS = 1 ADD + 1 MULT
f_alpha_dt_y = f_alpha_dt_y + f_alpha_beta_neg_y * w_val;
}
// force between pedestrian and obstacles
// FLOPCOUNT = n_obstacles * 63 FLOPS
for (int k = 0; k < board.n_obstacles; k++) {
// 1 FLOP = 1 ADD
double v1_x = p.curr_pos_x - board.obstacles[k].start.x;
// 1 FLOP = 1 ADD
double v1_y = p.curr_pos_y - board.obstacles[k].start.y;
// 4 FLOPS = 3 MULT + 1 ADD
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;
// 3 FLOPS = 2 ADD + 1 MULT
double r_aB_x = p.curr_pos_x - (board.obstacles[k].start.x + board.obstacles[k].obstacle_v.x * projection);
// 3 FLOPS = 2 ADD + 1 MULT
double r_aB_y = p.curr_pos_y - (board.obstacles[k].start.y + board.obstacles[k].obstacle_v.y * projection);
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
double r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
// 33 FLOPS = 1 EXP + 2 MULT + 1 DIV
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// 1 FLOP = 1 MULT
double f_alpha_B_neg_x = r_aB_x * scalar_mult;
// 1 FLOP = 1 MULT
double f_alpha_B_neg_y = r_aB_y * scalar_mult;
double w_val;
// 4 FLOPS = 3 MULT + 1 ADD
double a = (p.e_x * f_alpha_B_neg_x + p.e_y * f_alpha_B_neg_y) * cos_phi_inv;
// 3 FLOPS = 2 MULT + 1 ADD
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;
// 1 FLOP = 1 MULT
if (a >= 0 && (a * a > b_sq)) {
w_val = c_neg_times_dt;
}
// 1 FLOP = 1 MULT
double f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
// 1 FLOP = 1 MULT
double f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
// 1 FLOP = 1 ADD
f_alpha_dt_x += f_alpha_B_dt_x;
// 1 FLOP = 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y;
}
// 1 FLOP = 1 ADD
p.w_x += f_alpha_dt_x;
// 1 FLOP = 1 ADD
p.w_y += f_alpha_dt_y;
}
// update the positions etc.
// FLOPCOUNT = n_p * 29 FLOPS
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
double w_norm = sqrt(p.w_x * p.w_x + p.w_y * p.w_y);
// 1 FLOP = 1 DIV
double correction_factor = min(1.0, p.v_max / w_norm);
// 1 FLOP = 1 MULT
p.curr_vel_x = p.w_x * correction_factor;
// 1 FLOP = 1 MULT
p.curr_vel_y = p.w_y * correction_factor;
// 2 FLOPS = 1 MULT + 1 ADD
p.curr_pos_x = p.curr_pos_x + p.curr_vel_x * dt;
// 2 FLOPS = 1 MULT + 1 ADD
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);
// 1 FLOP = 1 ADD
double e_x = p.destination_x - p.curr_pos_x;
// 1 FLOP = 1 ADD
double e_y = p.destination_y - p.curr_pos_y;
// 5 FLOPS = 1 DIV + 1 SQRT + 2 MULT + 1 ADD
double e_norm_inv = 1.0 / sqrt(e_x * e_x + e_y * e_y);
// 1 FLOP = 1 MULT
p.e_x = e_x * e_norm_inv;
// 1 FLOP = 1 MULT
p.e_y = e_y * e_norm_inv;
// 3 FLOPS = 2 MULT + 1 ADD
double velocity_norm_sq = p.curr_vel_x * p.curr_vel_x + p.curr_vel_y * p.curr_vel_y;
// 1 FLOP = 1 SQRT
double velocity_norm = sqrt(velocity_norm_sq);
// 1 FLOP = 1 MULT
p.s_b_sq = velocity_norm_sq * delta_t_sq;
// 2 FLOPS = 2 MULT
p.s_b_e_x = p.e_x * (velocity_norm * delta_t);
// 2 FLOPS = 2 MULT
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;
}
@@ -1,633 +0,0 @@
// # ILP
// # Changes: FLOPCOUNT
// n_iter * [ n_p ** 2 * (12.5 A + 2.5 S + 23 M + 1 D + 1 E) + n_p * (6.5 A + 0.5 S - 2 M + 1 D - 1 E) + n_obstacles * n_p * (12 A + 19 M + 1 S + 1 D + 1 E)]
// # Predecessor: variant_inlining_7_symmetry_2_ilp.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]
// n_iter * [ 0.5 * (n_p * n_p - n_p) * (3 ADD + 1 SQRT + 2 MULT) + n_p * [ (12 ADD + 4 MULT) + ((n_p - 1) * (11 ADD + 22 MULT + 2 SQRT + 1 DIV + 1 EXP)) ] + n_p * (7 ADD + 17 MULT + 3 SQRT + 2 DIV) + n_obstacles * n_p * (12 ADD + 19 MULT + 1 SQRT + 1 DIV + 1 EXP)]
// n_iter * [ 0.5 * n_p ** 2 * (3 A + 1 S + 2 M) - 0.5 * n_p * (3 A + 1 S + 2 M) + n_p ** 2 * (11 A + 22 M + 2 S + 1 D + 1 E) + n_p * (1 A - 18 M - 2 S - 1 D - 1 E) + n_p * (7 A + 17 M + 3 S + 2 D) + n_obstacles * n_p * (12 A + 19 M + 1 S + 1 D + 1 E)]
// n_iter * [ n_p ** 2 * (1.5 A + 0.5 S + 1 M + 11 A + 22 M + 2 S + 1 D + 1 E) + n_p * (- 1.5 A - 0.5 S - 1 M + 1 A - 18 M - 2 S - 1 D - 1 E + 7 A + 17 M + 3 S + 2 D) + n_obstacles * n_p * (12 A + 19 M + 1 S + 1 D + 1 E)]
// n_iter * [ n_p ** 2 * (12.5 A + 2.5 S + 23 M + 1 D + 1 E) + n_p * (6.5 A + 0.5 S - 2 M + 1 D - 1 E) + n_obstacles * n_p * (12 A + 19 M + 1 S + 1 D + 1 E)]
for (int t = 0; t < n_iter; t++) {
// get all distances first
// No ILP because no accumulators
// FLOPCOUNT = 0.5 * (n_p * n_p - n_p) * 6 FLOPS
// 0.5 * (n_p * n_p - n_p) * (3 ADD + 1 SQRT + 2 MULT)
for (int i = 0; i < n_p; i++) {
// only upper triangle
for (int j = i + 1; j < n_p; j++) {
// 1 FLOP = 1 ADD
double dx = pedestrians[i].curr_pos_x - pedestrians[j].curr_pos_x;
// 1 FLOP = 1 ADD
double dy = pedestrians[i].curr_pos_y - pedestrians[j].curr_pos_y;
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
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;
}
}
// FLOPCOUNT = n_p * [ 16 FLOPS + ((n_p - 1) * 66 FLOPS) + (n_obstacles * 63 FLOPS)]
// n_p * [ (12 ADD + 4 MULT) + ((n_p - 1) * (11 ADD + 22 MULT + 2 SQRT + 1 DIV + 1 EXP)) + n_obstacles * (12 ADD + 19 MULT + 1 SQRT + 1 DIV + 1 EXP) ]
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// 1 FLOP = 1 MULT
double desired_velocity_x = p.e_x * p.desired_v0;
// 1 FLOP = 1 MULT
double desired_velocity_y = p.e_y * p.desired_v0;
// 2 FLOPS = 1 ADD + 1 MULT
double f_alpha_dt_x = (desired_velocity_x - p.curr_vel_x) * dt_times_tau_alpha_inv;
// 2 FLOPS = 1 ADD + 1 MULT
double f_alpha_dt_y = (desired_velocity_y - p.curr_vel_y) * dt_times_tau_alpha_inv;
// accumulators for ILP
// 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;
double acc_0_x = 0.0, acc_1_x = 0.0, acc_2_x = 0.0, acc_3_x = 0.0;
double acc_0_y = 0.0, acc_1_y = 0.0, acc_2_y = 0.0, acc_3_y = 0.0;
const double p_e_x = p.e_x;
const double p_e_y = p.e_y;
// force between pedestrians --> no symmetry because of different accelerations
// ILP
int j = 0;
// FLOPCOUNT = ((n_p - 1) * 66 FLOPS)
// ((n_p - 1) * (11 ADD + 22 MULT + 2 SQRT + 1 DIV + 1 EXP))
for (; j <= n_p - 4; j+=4) {
// distance symmetry
double r_ab_x_0, r_ab_y_0, r_ab_norm_0;
double r_ab_x_1, r_ab_y_1, r_ab_norm_1;
double r_ab_x_2, r_ab_y_2, r_ab_norm_2;
double r_ab_x_3, r_ab_y_3, r_ab_norm_3;
int j0 = j, j1 = j+1, j2 = j+2, j3 = j+3;
// skip i == j
// FLOPCOUNT = 66 FLOPS
// 11 ADD + 22 MULT + 2 SQRT + 1 DIV + 1 EXP
if (i != j0) {
if (i < j0) {
r_ab_x_0 = r_ab_x_lut[i * n_p + j0];
r_ab_y_0 = r_ab_y_lut[i * n_p + j0];
r_ab_norm_0 = r_ab_norm_lut[i * n_p + j0];
} else {
// other direction
// I think here we can optimize for sure, currently not using LUT good
r_ab_x_0 = -1 * r_ab_x_lut[j0 * n_p + i];
r_ab_y_0 = -1 * r_ab_y_lut[j0 * n_p + i];
r_ab_norm_0 = r_ab_norm_lut[j0 * n_p + i];
}
// the math, see paper
// 1 FLOP = 1 ADD
double r_ab2_x_0 = r_ab_x_0 - pedestrians[j0].s_b_e_x;
// 1 FLOP = 1 ADD
double r_ab2_y_0 = r_ab_y_0 - pedestrians[j0].s_b_e_y;
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
double r_ab2_norm_0 = sqrt(r_ab2_x_0 * r_ab2_x_0 + r_ab2_y_0 * r_ab2_y_0);
// 1 FLOP = 1 ADD
double r_ab_norm_sum_0 = r_ab_norm_0 + r_ab2_norm_0;
// 2 FLOPS = 1 MULT + 1 ADD
double two_b_sq_0 = r_ab_norm_sum_0 * r_ab_norm_sum_0 - pedestrians[j0].s_b_sq;
// 1 FLOP = 1 SQRT
double two_b_0 = sqrt(two_b_sq_0);
// 36 FLOPS = 1 DIV + 5 MULT + 1 EXP
double scalar_mult_common_0 = (r_ab_norm_sum_0 / (two_b_0 * r_ab_norm_0 * r_ab2_norm_0)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_0 * NEG_TWO_SIGMA_INV));
// 1 FLOP = 1 MULT
double scalar_mult_r_ab_0 = scalar_mult_common_0 * r_ab2_norm_0;
// 1 FLOP = 1 MULT
double scalar_mult_r_ab2_0 = scalar_mult_common_0 * r_ab_norm_0;
// 3 FLOPS = 2 MULT + 1 ADD
double f_alpha_beta_neg_x_0 = r_ab_x_0 * scalar_mult_r_ab_0 + r_ab2_x_0 * scalar_mult_r_ab2_0;
// 3 FLOPS = 2 MULT + 1 ADD
double f_alpha_beta_neg_y_0 = r_ab_y_0 * scalar_mult_r_ab_0 + r_ab2_y_0 * scalar_mult_r_ab2_0;
double w_val_0;
// 4 FLOPS = 3 MULT + 1 ADD
double a_0 = (p_e_x * f_alpha_beta_neg_x_0 + p_e_y * f_alpha_beta_neg_y_0) * cos_phi_inv;
// 3 FLOPS = 2 MULT + 1 ADD
double b_sq_0 = f_alpha_beta_neg_x_0 * f_alpha_beta_neg_x_0 + f_alpha_beta_neg_y_0 * f_alpha_beta_neg_y_0;
w_val_0 = dt_neg;
// 1 FLOP = 1 MULT
if (a_0 >= 0 && (a_0 * a_0 > b_sq_0)) {
w_val_0 = c_neg_times_dt;
}
// 2 FLOPS = 1 ADD + 1 MULT
acc_0_x += f_alpha_beta_neg_x_0 * w_val_0;
// 2 FLOPS = 1 ADD + 1 MULT
acc_0_y += f_alpha_beta_neg_y_0 * w_val_0;
}
// skip i == j
// FLOPCOUNT= 66 FLOPS
if (i != j1) {
if (i < j1) {
r_ab_x_1 = r_ab_x_lut[i * n_p + j1];
r_ab_y_1 = r_ab_y_lut[i * n_p + j1];
r_ab_norm_1 = r_ab_norm_lut[i * n_p + j1];
} else {
// other direction
// I think here we can optimize for sure, currently not using LUT good
r_ab_x_1 = -1 * r_ab_x_lut[j1 * n_p + i];
r_ab_y_1 = -1 * r_ab_y_lut[j1 * n_p + i];
r_ab_norm_1 = r_ab_norm_lut[j1 * n_p + i];
}
// the math, see paper
// 1 FLOP = 1 ADD
double r_ab2_x_1 = r_ab_x_1 - pedestrians[j1].s_b_e_x;
// 1 FLOP = 1 ADD
double r_ab2_y_1 = r_ab_y_1 - pedestrians[j1].s_b_e_y;
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
double r_ab2_norm_1 = sqrt(r_ab2_x_1 * r_ab2_x_1 + r_ab2_y_1 * r_ab2_y_1);
// 1 FLOP = 1 ADD
double r_ab_norm_sum_1 = r_ab_norm_1 + r_ab2_norm_1;
// 2 FLOPS = 1 MULT + 1 ADD
double two_b_sq_1 = r_ab_norm_sum_1 * r_ab_norm_sum_1 - pedestrians[j1].s_b_sq;
// 1 FLOP = 1 SQRT
double two_b_1 = sqrt(two_b_sq_1);
// 36 FLOPS = 1 DIV + 5 MULT + 1 EXP
double scalar_mult_common_1 = (r_ab_norm_sum_1 / (two_b_1 * r_ab_norm_1 * r_ab2_norm_1)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_1 * NEG_TWO_SIGMA_INV));
double scalar_mult_r_ab_1 = scalar_mult_common_1 * r_ab2_norm_1;
double scalar_mult_r_ab2_1 = scalar_mult_common_1 * r_ab_norm_1;
// 3 FLOPS = 2 MULT + 1 ADD
double f_alpha_beta_neg_x_1 = r_ab_x_1 * scalar_mult_r_ab_1 + r_ab2_x_1 * scalar_mult_r_ab2_1;
// 3 FLOPS = 2 MULT + 1 ADD
double f_alpha_beta_neg_y_1 = r_ab_y_1 * scalar_mult_r_ab_1 + r_ab2_y_1 * scalar_mult_r_ab2_1;
double w_val_1;
// 4 FLOPS = 3 MULT + 1 ADD
double a_1 = (p_e_x * f_alpha_beta_neg_x_1 + p_e_y * f_alpha_beta_neg_y_1) * cos_phi_inv;
// 3 FLOPS = 2 MULT + 1 ADD
double b_sq_1 = f_alpha_beta_neg_x_1 * f_alpha_beta_neg_x_1 + f_alpha_beta_neg_y_1 * f_alpha_beta_neg_y_1;
w_val_1 = dt_neg;
// 1 FLOP = 1 MULT
if (a_1 >= 0 && (a_1 * a_1 > b_sq_1)) {
w_val_1 = c_neg_times_dt;
}
// 2 FLOPS = 1 MULT + 1 ADD
acc_1_x += f_alpha_beta_neg_x_1 * w_val_1;
// 2 FLOPS = 1 MULT + 1 ADD
acc_1_y += f_alpha_beta_neg_y_1 * w_val_1;
}
// skip i == j
// FLOPCOUNT = 66 FLOPS
if (i != j2) {
if (i < j2) {
r_ab_x_2 = r_ab_x_lut[i * n_p + j2];
r_ab_y_2 = r_ab_y_lut[i * n_p + j2];
r_ab_norm_2 = r_ab_norm_lut[i * n_p + j2];
} else {
// other direction
// I think here we can optimize for sure, currently not using LUT good
r_ab_x_2 = -1 * r_ab_x_lut[j2 * n_p + i];
r_ab_y_2 = -1 * r_ab_y_lut[j2 * n_p + i];
r_ab_norm_2 = r_ab_norm_lut[j2 * n_p + i];
}
// the math, see paper
double r_ab2_x_2 = r_ab_x_2 - pedestrians[j2].s_b_e_x;
double r_ab2_y_2 = r_ab_y_2 - pedestrians[j2].s_b_e_y;
double r_ab2_norm_2 = sqrt(r_ab2_x_2 * r_ab2_x_2 + r_ab2_y_2 * r_ab2_y_2);
double r_ab_norm_sum_2 = r_ab_norm_2 + r_ab2_norm_2;
double two_b_sq_2 = r_ab_norm_sum_2 * r_ab_norm_sum_2 - pedestrians[j2].s_b_sq;
double two_b_2 = sqrt(two_b_sq_2);
double scalar_mult_common_2 = (r_ab_norm_sum_2 / (two_b_2 * r_ab_norm_2 * r_ab2_norm_2)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_2 * NEG_TWO_SIGMA_INV));
double scalar_mult_r_ab_2 = scalar_mult_common_2 * r_ab2_norm_2;
double scalar_mult_r_ab2_2 = scalar_mult_common_2 * r_ab_norm_2;
double f_alpha_beta_neg_x_2 = r_ab_x_2 * scalar_mult_r_ab_2 + r_ab2_x_2 * scalar_mult_r_ab2_2;
double f_alpha_beta_neg_y_2 = r_ab_y_2 * scalar_mult_r_ab_2 + r_ab2_y_2 * scalar_mult_r_ab2_2;
double w_val_2;
double a_2 = (p_e_x * f_alpha_beta_neg_x_2 + p_e_y * f_alpha_beta_neg_y_2) * cos_phi_inv;
double b_sq_2 = f_alpha_beta_neg_x_2 * f_alpha_beta_neg_x_2 + f_alpha_beta_neg_y_2 * f_alpha_beta_neg_y_2;
w_val_2 = dt_neg;
if (a_2 >= 0 && (a_2 * a_2 > b_sq_2)) {
w_val_2 = c_neg_times_dt;
}
acc_2_x += f_alpha_beta_neg_x_2 * w_val_2;
acc_2_y += f_alpha_beta_neg_y_2 * w_val_2;
}
// skip i == j
// FLOPCOUNT = 66 FLOPS
if (i != j3) {
if (i < j3) {
r_ab_x_3 = r_ab_x_lut[i * n_p + j3];
r_ab_y_3 = r_ab_y_lut[i * n_p + j3];
r_ab_norm_3 = r_ab_norm_lut[i * n_p + j3];
} else {
// other direction
// I think here we can optimize for sure, currently not using LUT good
r_ab_x_3 = -1 * r_ab_x_lut[j3 * n_p + i];
r_ab_y_3 = -1 * r_ab_y_lut[j3 * n_p + i];
r_ab_norm_3 = r_ab_norm_lut[j3 * n_p + i];
}
// the math, see paper
double r_ab2_x_3 = r_ab_x_3 - pedestrians[j3].s_b_e_x;
double r_ab2_y_3 = r_ab_y_3 - pedestrians[j3].s_b_e_y;
double r_ab2_norm_3 = sqrt(r_ab2_x_3 * r_ab2_x_3 + r_ab2_y_3 * r_ab2_y_3);
double r_ab_norm_sum_3 = r_ab_norm_3 + r_ab2_norm_3;
double two_b_sq_3 = r_ab_norm_sum_3 * r_ab_norm_sum_3 - pedestrians[j3].s_b_sq;
double two_b_3 = sqrt(two_b_sq_3);
double scalar_mult_common_3 = (r_ab_norm_sum_3 / (two_b_3 * r_ab_norm_3 * r_ab2_norm_3)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_3 * NEG_TWO_SIGMA_INV));
double scalar_mult_r_ab_3 = scalar_mult_common_3 * r_ab2_norm_3;
double scalar_mult_r_ab2_3 = scalar_mult_common_3 * r_ab_norm_3;
double f_alpha_beta_neg_x_3 = r_ab_x_3 * scalar_mult_r_ab_3 + r_ab2_x_3 * scalar_mult_r_ab2_3;
double f_alpha_beta_neg_y_3 = r_ab_y_3 * scalar_mult_r_ab_3 + r_ab2_y_3 * scalar_mult_r_ab2_3;
double w_val_3;
double a_3 = (p_e_x * f_alpha_beta_neg_x_3 + p_e_y * f_alpha_beta_neg_y_3) * cos_phi_inv;
double b_sq_3 = f_alpha_beta_neg_x_3 * f_alpha_beta_neg_x_3 + f_alpha_beta_neg_y_3 * f_alpha_beta_neg_y_3;
w_val_3 = dt_neg;
if (a_3 >= 0 && (a_3 * a_3 > b_sq_3)) {
w_val_3 = c_neg_times_dt;
}
acc_3_x += f_alpha_beta_neg_x_3 * w_val_3;
acc_3_y += f_alpha_beta_neg_y_3 * w_val_3;
}
}
// add the result
// 4 FLOPS = 4 ADD
f_alpha_dt_x += acc_0_x + acc_1_x + acc_2_x + acc_3_x;
// 4 FLOPS = 4 ADD
f_alpha_dt_y += acc_0_y + acc_1_y + acc_2_y + acc_3_y;
// remaining
// 66 FLOPS --> see ILP loop
// 11 ADD + 22 MULT + 2 SQRT + 1 DIV + 1 EXP
for (; j < n_p; j++) {
if (i == j) continue;
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 {
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];
}
// 1 FLOP = 1 ADD
double r_ab2_x = r_ab_x - pedestrians[j].s_b_e_x;
// 1 FLOP = 1 ADD
double r_ab2_y = r_ab_y - pedestrians[j].s_b_e_y;
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
double r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
// 1 FLOP = 1 ADD
double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
// 2 FLOPS = 1 MULT + 1 ADD
double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - pedestrians[j].s_b_sq;
// 1 FLOP = 1 SQRT
double two_b = sqrt(two_b_sq);
// 36 FLOPS = 1 DIV + 5 MULT + 1 EXP
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));
// 1 FLOP = 1 MULT
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// 1 FLOP = 1 MULT
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
// 3 FLOPS = 2 MULT + 1 ADD
double f_alpha_beta_neg_x = r_ab_x * scalar_mult_r_ab + r_ab2_x * scalar_mult_r_ab2;
// 3 FLOPS = 2 MULT + 1 ADD
double f_alpha_beta_neg_y = r_ab_y * scalar_mult_r_ab + r_ab2_y * scalar_mult_r_ab2;
double w_val;
// 4 FLOPS = 3 MULT + 1 ADD
double a = (p_e_x * f_alpha_beta_neg_x + p_e_y * f_alpha_beta_neg_y) * cos_phi_inv;
// 3 FLOPS = 2 MULT + 1 ADD
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;
// 1 FLOP = 1 MULT
if (a >= 0 && (a * a > b_sq)) {
w_val = c_neg_times_dt;
}
// 2 FLOPS = 1 MULT + 1 ADD
f_alpha_dt_x = f_alpha_dt_x + f_alpha_beta_neg_x * w_val;
// 2 FLOPS = 1 MULT + 1 ADD
f_alpha_dt_y = f_alpha_dt_y + f_alpha_beta_neg_y * w_val;
}
// force between pedestrian and obstacles
// initialized with 0 obstacles, might consider deleting this?
// to sparse to use ILP
// FLOPCOUNT = n_obstacles * 63 FLOPS
// n_obstacles * (12 ADD + 19 MULT + 1 SQRT + 1 DIV + 1 EXP)
for (int k = 0; k < board.n_obstacles; k++) {
// 1 FLOP = 1 ADD
double v1_x = p.curr_pos_x - board.obstacles[k].start.x;
// 1 FLOP = 1 ADD
double v1_y = p.curr_pos_y - board.obstacles[k].start.y;
// 4 FLOPS = 3 MULT + 1 ADD
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;
// 3 FLOPS = 1 MULT + 2 ADD
double r_aB_x = p.curr_pos_x - (board.obstacles[k].start.x + board.obstacles[k].obstacle_v.x * projection);
// 3 FLOPS = 1 MULT + 2 ADD
double r_aB_y = p.curr_pos_y - (board.obstacles[k].start.y + board.obstacles[k].obstacle_v.y * projection);
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
double r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
// 33 FLOPS = 1 DIV + 2 MULT + 1 EXPT
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// 1 FLOP = 1 MULT
double f_alpha_B_neg_x = r_aB_x * scalar_mult;
// 1 FLOP = 1 MULT
double f_alpha_B_neg_y = r_aB_y * scalar_mult;
double w_val;
// 4 FLOPS = 3 MULT + 1 ADD
double a = (p.e_x * f_alpha_B_neg_x + p.e_y * f_alpha_B_neg_y) * cos_phi_inv;
// 3 FLOPS = 2 MULT + 1 ADD
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;
// 1 FLOP = 1 MULT
if (a >= 0 && (a * a > b_sq)) {
w_val = c_neg_times_dt;
}
// 1 FLOP = 1 MULT
double f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
// 1 FLOP = 1 MULT
double f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
// 1 FLOP = 1 ADD
f_alpha_dt_x += f_alpha_B_dt_x;
// 1 FLOP = 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y;
}
// 1 FLOP = 1 ADD
p.w_x += f_alpha_dt_x;
// 1 FLOP = 1 ADD
p.w_y += f_alpha_dt_y;
}
// update the positions etc.
// ILP is not possible I think
// FLOPCOUNT = n_p * 29 FLOPS
// n_p * (7 ADD + 17 MULT + 3 SQRT + 2 DIV)
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
double w_norm = sqrt(p.w_x * p.w_x + p.w_y * p.w_y);
// 1 FLOP = 1 DIV
double correction_factor = min(1.0, p.v_max / w_norm);
// 1 FLOP = 1 MULT
p.curr_vel_x = p.w_x * correction_factor;
// 1 FLOP = 1 MULT
p.curr_vel_y = p.w_y * correction_factor;
// maybe move this calculation to a separate loop to enable ILP
// 2 FLOPS = 1 MULT + 1 ADD
p.curr_pos_x = p.curr_pos_x + p.curr_vel_x * dt;
// 2 FLOPS = 1 MULT + 1 ADD
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);
// 1 FLOP = 1 ADD
double e_x = p.destination_x - p.curr_pos_x;
// 1 FLOP = 1 ADD
double e_y = p.destination_y - p.curr_pos_y;
// 5 FLOPS = 1 DIV + 1 SQRT + 2 MULT + 1 ADD
double e_norm_inv = 1.0 / sqrt(e_x * e_x + e_y * e_y);
// 1 FLOP = 1 MULT
p.e_x = e_x * e_norm_inv;
// 1 FLOP = 1 MULT
p.e_y = e_y * e_norm_inv;
// 3 FLOPS = 2 MULT + 1 ADD
double velocity_norm_sq = p.curr_vel_x * p.curr_vel_x + p.curr_vel_y * p.curr_vel_y;
// 1 FLOP = 1 SQRT
double velocity_norm = sqrt(velocity_norm_sq);
// 1 FLOP = 1 MULT
p.s_b_sq = velocity_norm_sq * delta_t_sq;
// 2 FLOPS = 2 MULT
p.s_b_e_x = p.e_x * (velocity_norm * delta_t);
// 2 FLOPS = 2 MULT
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;
}
@@ -1,753 +0,0 @@
// # ILP
// # Changes: flopcoun
// FLOPCOUNT = n_iter * [ 69 * n_p ** 2 - 16 * n_p + 63 * n_obstacles * n_p ]
// n_iter * [ n_p ** 2 * (12.5 A + 23 M + 2.5 S + 1 D + 1 E) + n_p * (14.5 A - 2 M + 0.5 S + 1 D - 1 E) + n_obstacles * n_p * (12 A + 19 M + 1 S + 1 D + 1 E) ]
// # Predecessor: variant_inlining_7_symmetry_2_ilp_2.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]
// FLOPCOUNT = n_iter * [ {0.5 * (n_p * n_p - n_p) * 6 FLOPS} + {n_p * [(24 FLOPS) + ((n_p - 1) * 66 FLOPS) + (n_obstacles * 63 FLOPS) ]} + {n_p * 29 FLOPS} ]
// FLOPCOUNT = n_iter * [ {3 * (n_p * n_p - n_p)} + {n_p * [(24) + ((n_p - 1) * 66) + (n_obstacles * 63) ]} + {n_p * 29} ]
// FLOPCOUNT = n_iter * [ {3 * n_p ** 2 - 3 * n_p} + {66 * n_p ** 2 - 42 * n_p + 63 * n_obstacles * n_p} + {29 * n_p} ]
// FLOPCOUNT = n_iter * [ {3 * n_p ** 2 + 66 * n_p ** 2} + { - 3 * n_p - 42 * n_p + 29 * n_p} + {63 * n_obstacles * n_p} ]
// FLOPCOUNT = n_iter * [ 69 * n_p ** 2 - 16 * n_p + 63 * n_obstacles * n_p ]
// n_iter * [ {0.5 * (n_p * n_p - n_p) * (3 ADD + 2 MULT + 1 SQRT)} + {n_p * [ (20 ADD + 4 MULT) + ((n_p - 1) * (11 ADD + 22 MULT + 2 SQRT + 1 DIV + 1 EXP)) + n_obstacles * (12 ADD + 19 MULT + 1 SQRT + 1 DIV + 1 EXP) ]} + {n_p * (7 ADD + 17 MULT + 3 SQRT + 2 DIV)} ]
// n_iter * [ {0.5 * n_p ** 2 * (3 A + 2 M + 1 S) - 0.5 * n_p * (3 A + 2 M + 1 S)} + {n_p * [ (20 A + 4 M) + ((n_p - 1) * (11 A + 22 M + 2 S + 1 D + 1 E)) + n_obstacles * (12 A + 19 M + 1 S + 1 D + 1 E) ]} + {n_p * (7 A + 17 M + 3 S + 2 D)} ]
// n_iter * [ 0.5 * n_p ** 2 * (3 A + 2 M + 1 S) - 0.5 * n_p * (3 A + 2 M + 1 S) + {n_p * [ (20 A + 4 M) + n_p * (11 A + 22 M + 2 S + 1 D + 1 E) - 1 * (11 A + 22 M + 2 S + 1 D + 1 E) + n_obstacles * (12 A + 19 M + 1 S + 1 D + 1 E) ]} + {n_p * (7 A + 17 M + 3 S + 2 D)} ]
// n_iter * [ 0.5 * n_p ** 2 * (3 A + 2 M + 1 S) - 0.5 * n_p * (3 A + 2 M + 1 S) + n_p ** 2 * (11 A + 22 M + 2 S + 1 D + 1 E) + n_p * (9 A - 18 M - 2 S - 1 D - 1 E) + n_obstacles * n_p * (12 A + 19 M + 1 S + 1 D + 1 E) + n_p * (7 A + 17 M + 3 S + 2 D) ]
// n_iter * [ n_p ** 2 * (1.5 A + 1 M + 0.5 S + 11 A + 22 M + 2 S + 1 D + 1 E) + n_p * (- 1.5 A - 1 M - 0.5 S + 9 A - 18 M - 2 S - 1 D - 1 E + 7 A + 17 M + 3 S + 2 D) + n_obstacles * n_p * (12 A + 19 M + 1 S + 1 D + 1 E) ]
// n_iter * [ n_p ** 2 * (12.5 A + 23 M + 2.5 S + 1 D + 1 E) + n_p * (14.5 A - 2 M + 0.5 S + 1 D - 1 E) + n_obstacles * n_p * (12 A + 19 M + 1 S + 1 D + 1 E) ]
for (int t = 0; t < n_iter; t++) {
// get all distances first
// No ILP because no accumulators
// FLOPCOUNT = 0.5 * (n_p * n_p - n_p) * 6 FLOPS
// 0.5 * (n_p * n_p - n_p) * (3 ADD + 2 MULT + 1 SQRT)
for (int i = 0; i < n_p; i++) {
// only upper triangle
for (int j = i + 1; j < n_p; j++) {
// 1 FLOP = 1 ADD
double dx = pedestrians[i].curr_pos_x - pedestrians[j].curr_pos_x;
// 1 FLOP = 1 ADD
double dy = pedestrians[i].curr_pos_y - pedestrians[j].curr_pos_y;
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
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;
}
}
// FLOPCOUNT = n_p * [(24 FLOPS) + ((n_p - 1) * 66 FLOPS) + (n_obstacles * 63 FLOPS) ]
// n_p * [ (20 ADD + 4 MULT) + ((n_p - 1) * (11 ADD + 22 MULT + 2 SQRT + 1 DIV + 1 EXP)) + n_obstacles * (12 ADD + 19 MULT + 1 SQRT + 1 DIV + 1 EXP) ]
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// 1 FLOP = 1 MULT
double desired_velocity_x = p.e_x * p.desired_v0;
// 1 FLOP = 1 MULT
double desired_velocity_y = p.e_y * p.desired_v0;
// 2 FLOPS = 1ADD + 1 MULT
double f_alpha_dt_x = (desired_velocity_x - p.curr_vel_x) * dt_times_tau_alpha_inv;
// 2 FLOPS = 1ADD + 1 MULT
double f_alpha_dt_y = (desired_velocity_y - p.curr_vel_y) * dt_times_tau_alpha_inv;
// accumulators for ILP
// 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;
double acc_0_x = 0.0, acc_1_x = 0.0, acc_2_x = 0.0, acc_3_x = 0.0, acc_4_x = 0.0, acc_5_x = 0.0, acc_6_x = 0.0, acc_7_x = 0.0;
double acc_0_y = 0.0, acc_1_y = 0.0, acc_2_y = 0.0, acc_3_y = 0.0, acc_4_y = 0.0, acc_5_y = 0.0, acc_6_y = 0.0, acc_7_y = 0.0;
const double p_e_x = p.e_x;
const double p_e_y = p.e_y;
// force between pedestrians --> no symmetry because of different accelerations
// ILP
int j = 0;
// FLOPCOUNT = (n_p - 1) * 66 FLOPS
for (; j <= n_p - 8; j+=8) {
// distance symmetry
double r_ab_x_0, r_ab_y_0, r_ab_norm_0;
double r_ab_x_1, r_ab_y_1, r_ab_norm_1;
double r_ab_x_2, r_ab_y_2, r_ab_norm_2;
double r_ab_x_3, r_ab_y_3, r_ab_norm_3;
double r_ab_x_4, r_ab_y_4, r_ab_norm_4;
double r_ab_x_5, r_ab_y_5, r_ab_norm_5;
double r_ab_x_6, r_ab_y_6, r_ab_norm_6;
double r_ab_x_7, r_ab_y_7, r_ab_norm_7;
int j0 = j, j1 = j+1, j2 = j+2, j3 = j+3, j4 = j+4, j5 = j+5, j6 = j+6, j7 = j+7;
// skip i == j
// FLOPCOUNT = 66 FLOPS
if (i != j0) {
if (i < j0) {
r_ab_x_0 = r_ab_x_lut[i * n_p + j0];
r_ab_y_0 = r_ab_y_lut[i * n_p + j0];
r_ab_norm_0 = r_ab_norm_lut[i * n_p + j0];
} else {
// other direction
// I think here we can optimize for sure, currently not using LUT good
r_ab_x_0 = -1 * r_ab_x_lut[j0 * n_p + i];
r_ab_y_0 = -1 * r_ab_y_lut[j0 * n_p + i];
r_ab_norm_0 = r_ab_norm_lut[j0 * n_p + i];
}
// the math, see paper
// 1 FLOP = 1 ADD
double r_ab2_x_0 = r_ab_x_0 - pedestrians[j0].s_b_e_x;
// 1 FLOP = 1 ADD
double r_ab2_y_0 = r_ab_y_0 - pedestrians[j0].s_b_e_y;
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
double r_ab2_norm_0 = sqrt(r_ab2_x_0 * r_ab2_x_0 + r_ab2_y_0 * r_ab2_y_0);
// 1 FLOP = 1 ADD
double r_ab_norm_sum_0 = r_ab_norm_0 + r_ab2_norm_0;
// 2 FLOPS = 1 MULT + 1 ADD
double two_b_sq_0 = r_ab_norm_sum_0 * r_ab_norm_sum_0 - pedestrians[j0].s_b_sq;
// 1 FLOP = 1 SQRT
double two_b_0 = sqrt(two_b_sq_0);
// 36 FLOPS = 1 DIV + 5 MULT + 1 EXP
double scalar_mult_common_0 = (r_ab_norm_sum_0 / (two_b_0 * r_ab_norm_0 * r_ab2_norm_0)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_0 * NEG_TWO_SIGMA_INV));
// 1 FLOP = 1 MULT
double scalar_mult_r_ab_0 = scalar_mult_common_0 * r_ab2_norm_0;
// 1 FLOP = 1 MULT
double scalar_mult_r_ab2_0 = scalar_mult_common_0 * r_ab_norm_0;
// 3 FLOPS = 1 ADD + 2 MULT
double f_alpha_beta_neg_x_0 = r_ab_x_0 * scalar_mult_r_ab_0 + r_ab2_x_0 * scalar_mult_r_ab2_0;
// 3 FLOPS = 1 ADD + 2 MULT
double f_alpha_beta_neg_y_0 = r_ab_y_0 * scalar_mult_r_ab_0 + r_ab2_y_0 * scalar_mult_r_ab2_0;
double w_val_0;
// 4 FLOPS = 1 ADD + 3 MULT
double a_0 = (p_e_x * f_alpha_beta_neg_x_0 + p_e_y * f_alpha_beta_neg_y_0) * cos_phi_inv;
// 3 FLOPS = 1 ADD + 2 MULT
double b_sq_0 = f_alpha_beta_neg_x_0 * f_alpha_beta_neg_x_0 + f_alpha_beta_neg_y_0 * f_alpha_beta_neg_y_0;
w_val_0 = dt_neg;
// 1 FLOP
if (a_0 >= 0 && (a_0 * a_0 > b_sq_0)) {
w_val_0 = c_neg_times_dt;
}
// 2 FLOPS = 1 ADD + 1 MULT
acc_0_x += f_alpha_beta_neg_x_0 * w_val_0;
// 2 FLOPS = 1 ADD + 1 MULT
acc_0_y += f_alpha_beta_neg_y_0 * w_val_0;
}
// skip i == j
// FLOPCOUNT = 66 FLOPS
if (i != j1) {
if (i < j1) {
r_ab_x_1 = r_ab_x_lut[i * n_p + j1];
r_ab_y_1 = r_ab_y_lut[i * n_p + j1];
r_ab_norm_1 = r_ab_norm_lut[i * n_p + j1];
} else {
// other direction
// I think here we can optimize for sure, currently not using LUT good
r_ab_x_1 = -1 * r_ab_x_lut[j1 * n_p + i];
r_ab_y_1 = -1 * r_ab_y_lut[j1 * n_p + i];
r_ab_norm_1 = r_ab_norm_lut[j1 * n_p + i];
}
// the math, see paper
double r_ab2_x_1 = r_ab_x_1 - pedestrians[j1].s_b_e_x;
double r_ab2_y_1 = r_ab_y_1 - pedestrians[j1].s_b_e_y;
double r_ab2_norm_1 = sqrt(r_ab2_x_1 * r_ab2_x_1 + r_ab2_y_1 * r_ab2_y_1);
double r_ab_norm_sum_1 = r_ab_norm_1 + r_ab2_norm_1;
double two_b_sq_1 = r_ab_norm_sum_1 * r_ab_norm_sum_1 - pedestrians[j1].s_b_sq;
double two_b_1 = sqrt(two_b_sq_1);
double scalar_mult_common_1 = (r_ab_norm_sum_1 / (two_b_1 * r_ab_norm_1 * r_ab2_norm_1)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_1 * NEG_TWO_SIGMA_INV));
double scalar_mult_r_ab_1 = scalar_mult_common_1 * r_ab2_norm_1;
double scalar_mult_r_ab2_1 = scalar_mult_common_1 * r_ab_norm_1;
double f_alpha_beta_neg_x_1 = r_ab_x_1 * scalar_mult_r_ab_1 + r_ab2_x_1 * scalar_mult_r_ab2_1;
double f_alpha_beta_neg_y_1 = r_ab_y_1 * scalar_mult_r_ab_1 + r_ab2_y_1 * scalar_mult_r_ab2_1;
double w_val_1;
double a_1 = (p_e_x * f_alpha_beta_neg_x_1 + p_e_y * f_alpha_beta_neg_y_1) * cos_phi_inv;
double b_sq_1 = f_alpha_beta_neg_x_1 * f_alpha_beta_neg_x_1 + f_alpha_beta_neg_y_1 * f_alpha_beta_neg_y_1;
w_val_1 = dt_neg;
if (a_1 >= 0 && (a_1 * a_1 > b_sq_1)) {
w_val_1 = c_neg_times_dt;
}
acc_1_x += f_alpha_beta_neg_x_1 * w_val_1;
acc_1_y += f_alpha_beta_neg_y_1 * w_val_1;
}
// skip i == j
// FLOPCOUNT = 66 FLOPS
if (i != j2) {
if (i < j2) {
r_ab_x_2 = r_ab_x_lut[i * n_p + j2];
r_ab_y_2 = r_ab_y_lut[i * n_p + j2];
r_ab_norm_2 = r_ab_norm_lut[i * n_p + j2];
} else {
// other direction
// I think here we can optimize for sure, currently not using LUT good
r_ab_x_2 = -1 * r_ab_x_lut[j2 * n_p + i];
r_ab_y_2 = -1 * r_ab_y_lut[j2 * n_p + i];
r_ab_norm_2 = r_ab_norm_lut[j2 * n_p + i];
}
// the math, see paper
double r_ab2_x_2 = r_ab_x_2 - pedestrians[j2].s_b_e_x;
double r_ab2_y_2 = r_ab_y_2 - pedestrians[j2].s_b_e_y;
double r_ab2_norm_2 = sqrt(r_ab2_x_2 * r_ab2_x_2 + r_ab2_y_2 * r_ab2_y_2);
double r_ab_norm_sum_2 = r_ab_norm_2 + r_ab2_norm_2;
double two_b_sq_2 = r_ab_norm_sum_2 * r_ab_norm_sum_2 - pedestrians[j2].s_b_sq;
double two_b_2 = sqrt(two_b_sq_2);
double scalar_mult_common_2 = (r_ab_norm_sum_2 / (two_b_2 * r_ab_norm_2 * r_ab2_norm_2)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_2 * NEG_TWO_SIGMA_INV));
double scalar_mult_r_ab_2 = scalar_mult_common_2 * r_ab2_norm_2;
double scalar_mult_r_ab2_2 = scalar_mult_common_2 * r_ab_norm_2;
double f_alpha_beta_neg_x_2 = r_ab_x_2 * scalar_mult_r_ab_2 + r_ab2_x_2 * scalar_mult_r_ab2_2;
double f_alpha_beta_neg_y_2 = r_ab_y_2 * scalar_mult_r_ab_2 + r_ab2_y_2 * scalar_mult_r_ab2_2;
double w_val_2;
double a_2 = (p_e_x * f_alpha_beta_neg_x_2 + p_e_y * f_alpha_beta_neg_y_2) * cos_phi_inv;
double b_sq_2 = f_alpha_beta_neg_x_2 * f_alpha_beta_neg_x_2 + f_alpha_beta_neg_y_2 * f_alpha_beta_neg_y_2;
w_val_2 = dt_neg;
if (a_2 >= 0 && (a_2 * a_2 > b_sq_2)) {
w_val_2 = c_neg_times_dt;
}
acc_2_x += f_alpha_beta_neg_x_2 * w_val_2;
acc_2_y += f_alpha_beta_neg_y_2 * w_val_2;
}
// skip i == j
// FLOPCOUNT = 66 FLOPS
if (i != j3) {
if (i < j3) {
r_ab_x_3 = r_ab_x_lut[i * n_p + j3];
r_ab_y_3 = r_ab_y_lut[i * n_p + j3];
r_ab_norm_3 = r_ab_norm_lut[i * n_p + j3];
} else {
// other direction
// I think here we can optimize for sure, currently not using LUT good
r_ab_x_3 = -1 * r_ab_x_lut[j3 * n_p + i];
r_ab_y_3 = -1 * r_ab_y_lut[j3 * n_p + i];
r_ab_norm_3 = r_ab_norm_lut[j3 * n_p + i];
}
// the math, see paper
double r_ab2_x_3 = r_ab_x_3 - pedestrians[j3].s_b_e_x;
double r_ab2_y_3 = r_ab_y_3 - pedestrians[j3].s_b_e_y;
double r_ab2_norm_3 = sqrt(r_ab2_x_3 * r_ab2_x_3 + r_ab2_y_3 * r_ab2_y_3);
double r_ab_norm_sum_3 = r_ab_norm_3 + r_ab2_norm_3;
double two_b_sq_3 = r_ab_norm_sum_3 * r_ab_norm_sum_3 - pedestrians[j3].s_b_sq;
double two_b_3 = sqrt(two_b_sq_3);
double scalar_mult_common_3 = (r_ab_norm_sum_3 / (two_b_3 * r_ab_norm_3 * r_ab2_norm_3)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_3 * NEG_TWO_SIGMA_INV));
double scalar_mult_r_ab_3 = scalar_mult_common_3 * r_ab2_norm_3;
double scalar_mult_r_ab2_3 = scalar_mult_common_3 * r_ab_norm_3;
double f_alpha_beta_neg_x_3 = r_ab_x_3 * scalar_mult_r_ab_3 + r_ab2_x_3 * scalar_mult_r_ab2_3;
double f_alpha_beta_neg_y_3 = r_ab_y_3 * scalar_mult_r_ab_3 + r_ab2_y_3 * scalar_mult_r_ab2_3;
double w_val_3;
double a_3 = (p_e_x * f_alpha_beta_neg_x_3 + p_e_y * f_alpha_beta_neg_y_3) * cos_phi_inv;
double b_sq_3 = f_alpha_beta_neg_x_3 * f_alpha_beta_neg_x_3 + f_alpha_beta_neg_y_3 * f_alpha_beta_neg_y_3;
w_val_3 = dt_neg;
if (a_3 >= 0 && (a_3 * a_3 > b_sq_3)) {
w_val_3 = c_neg_times_dt;
}
acc_3_x += f_alpha_beta_neg_x_3 * w_val_3;
acc_3_y += f_alpha_beta_neg_y_3 * w_val_3;
}
// skip i == j
// FLOPCOUNT = 66 FLOPS
if (i != j4) {
if (i < j4) {
r_ab_x_4 = r_ab_x_lut[i * n_p + j4];
r_ab_y_4 = r_ab_y_lut[i * n_p + j4];
r_ab_norm_4 = r_ab_norm_lut[i * n_p + j4];
} else {
// other direction
// I think here we can optimize for sure, currently not using LUT good
r_ab_x_4 = -1 * r_ab_x_lut[j4 * n_p + i];
r_ab_y_4 = -1 * r_ab_y_lut[j4 * n_p + i];
r_ab_norm_4 = r_ab_norm_lut[j4 * n_p + i];
}
// the math, see paper
double r_ab2_x_4 = r_ab_x_4 - pedestrians[j4].s_b_e_x;
double r_ab2_y_4 = r_ab_y_4 - pedestrians[j4].s_b_e_y;
double r_ab2_norm_4 = sqrt(r_ab2_x_4 * r_ab2_x_4 + r_ab2_y_4 * r_ab2_y_4);
double r_ab_norm_sum_4 = r_ab_norm_4 + r_ab2_norm_4;
double two_b_sq_4 = r_ab_norm_sum_4 * r_ab_norm_sum_4 - pedestrians[j4].s_b_sq;
double two_b_4 = sqrt(two_b_sq_4);
double scalar_mult_common_4 = (r_ab_norm_sum_4 / (two_b_4 * r_ab_norm_4 * r_ab2_norm_4)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_4 * NEG_TWO_SIGMA_INV));
double scalar_mult_r_ab_4 = scalar_mult_common_4 * r_ab2_norm_4;
double scalar_mult_r_ab2_4 = scalar_mult_common_4 * r_ab_norm_4;
double f_alpha_beta_neg_x_4 = r_ab_x_4 * scalar_mult_r_ab_4 + r_ab2_x_4 * scalar_mult_r_ab2_4;
double f_alpha_beta_neg_y_4 = r_ab_y_4 * scalar_mult_r_ab_4 + r_ab2_y_4 * scalar_mult_r_ab2_4;
double w_val_4;
double a_4 = (p_e_x * f_alpha_beta_neg_x_4 + p_e_y * f_alpha_beta_neg_y_4) * cos_phi_inv;
double b_sq_4 = f_alpha_beta_neg_x_4 * f_alpha_beta_neg_x_4 + f_alpha_beta_neg_y_4 * f_alpha_beta_neg_y_4;
w_val_4 = dt_neg;
if (a_4 >= 0 && (a_4 * a_4 > b_sq_4)) {
w_val_4 = c_neg_times_dt;
}
acc_4_x += f_alpha_beta_neg_x_4 * w_val_4;
acc_4_y += f_alpha_beta_neg_y_4 * w_val_4;
}
// skip i == j
// FLOPCOUNT = 66 FLOPS
if (i != j5) {
if (i < j5) {
r_ab_x_5 = r_ab_x_lut[i * n_p + j5];
r_ab_y_5 = r_ab_y_lut[i * n_p + j5];
r_ab_norm_5 = r_ab_norm_lut[i * n_p + j5];
} else {
// other direction
// I think here we can optimize for sure, currently not using LUT good
r_ab_x_5 = -1 * r_ab_x_lut[j5 * n_p + i];
r_ab_y_5 = -1 * r_ab_y_lut[j5 * n_p + i];
r_ab_norm_5 = r_ab_norm_lut[j5 * n_p + i];
}
// the math, see paper
double r_ab2_x_5 = r_ab_x_5 - pedestrians[j5].s_b_e_x;
double r_ab2_y_5 = r_ab_y_5 - pedestrians[j5].s_b_e_y;
double r_ab2_norm_5 = sqrt(r_ab2_x_5 * r_ab2_x_5 + r_ab2_y_5 * r_ab2_y_5);
double r_ab_norm_sum_5 = r_ab_norm_5 + r_ab2_norm_5;
double two_b_sq_5 = r_ab_norm_sum_5 * r_ab_norm_sum_5 - pedestrians[j5].s_b_sq;
double two_b_5 = sqrt(two_b_sq_5);
double scalar_mult_common_5 = (r_ab_norm_sum_5 / (two_b_5 * r_ab_norm_5 * r_ab2_norm_5)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_5 * NEG_TWO_SIGMA_INV));
double scalar_mult_r_ab_5 = scalar_mult_common_5 * r_ab2_norm_5;
double scalar_mult_r_ab2_5 = scalar_mult_common_5 * r_ab_norm_5;
double f_alpha_beta_neg_x_5 = r_ab_x_5 * scalar_mult_r_ab_5 + r_ab2_x_5 * scalar_mult_r_ab2_5;
double f_alpha_beta_neg_y_5 = r_ab_y_5 * scalar_mult_r_ab_5 + r_ab2_y_5 * scalar_mult_r_ab2_5;
double w_val_5;
double a_5 = (p_e_x * f_alpha_beta_neg_x_5 + p_e_y * f_alpha_beta_neg_y_5) * cos_phi_inv;
double b_sq_5 = f_alpha_beta_neg_x_5 * f_alpha_beta_neg_x_5 + f_alpha_beta_neg_y_5 * f_alpha_beta_neg_y_5;
w_val_5 = dt_neg;
if (a_5 >= 0 && (a_5 * a_5 > b_sq_5)) {
w_val_5 = c_neg_times_dt;
}
acc_5_x += f_alpha_beta_neg_x_5 * w_val_5;
acc_5_y += f_alpha_beta_neg_y_5 * w_val_5;
}
// skip i == j
// FLOPCOUNT = 66 FLOPS
if (i != j6) {
if (i < j6) {
r_ab_x_6 = r_ab_x_lut[i * n_p + j6];
r_ab_y_6 = r_ab_y_lut[i * n_p + j6];
r_ab_norm_6 = r_ab_norm_lut[i * n_p + j6];
} else {
// other direction
// I think here we can optimize for sure, currently not using LUT good
r_ab_x_6 = -1 * r_ab_x_lut[j6 * n_p + i];
r_ab_y_6 = -1 * r_ab_y_lut[j6 * n_p + i];
r_ab_norm_6 = r_ab_norm_lut[j6 * n_p + i];
}
// the math, see paper
double r_ab2_x_6 = r_ab_x_6 - pedestrians[j6].s_b_e_x;
double r_ab2_y_6 = r_ab_y_6 - pedestrians[j6].s_b_e_y;
double r_ab2_norm_6 = sqrt(r_ab2_x_6 * r_ab2_x_6 + r_ab2_y_6 * r_ab2_y_6);
double r_ab_norm_sum_6 = r_ab_norm_6 + r_ab2_norm_6;
double two_b_sq_6 = r_ab_norm_sum_6 * r_ab_norm_sum_6 - pedestrians[j6].s_b_sq;
double two_b_6 = sqrt(two_b_sq_6);
double scalar_mult_common_6 = (r_ab_norm_sum_6 / (two_b_6 * r_ab_norm_6 * r_ab2_norm_6)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_6 * NEG_TWO_SIGMA_INV));
double scalar_mult_r_ab_6 = scalar_mult_common_6 * r_ab2_norm_6;
double scalar_mult_r_ab2_6 = scalar_mult_common_6 * r_ab_norm_6;
double f_alpha_beta_neg_x_6 = r_ab_x_6 * scalar_mult_r_ab_6 + r_ab2_x_6 * scalar_mult_r_ab2_6;
double f_alpha_beta_neg_y_6 = r_ab_y_6 * scalar_mult_r_ab_6 + r_ab2_y_6 * scalar_mult_r_ab2_6;
double w_val_6;
double a_6 = (p_e_x * f_alpha_beta_neg_x_6 + p_e_y * f_alpha_beta_neg_y_6) * cos_phi_inv;
double b_sq_6 = f_alpha_beta_neg_x_6 * f_alpha_beta_neg_x_6 + f_alpha_beta_neg_y_6 * f_alpha_beta_neg_y_6;
w_val_6 = dt_neg;
if (a_6 >= 0 && (a_6 * a_6 > b_sq_6)) {
w_val_6 = c_neg_times_dt;
}
acc_6_x += f_alpha_beta_neg_x_6 * w_val_6;
acc_6_y += f_alpha_beta_neg_y_6 * w_val_6;
}
// skip i == j
// FLOPCOUNT = 66 FLOPS
if (i != j7) {
if (i < j7) {
r_ab_x_7 = r_ab_x_lut[i * n_p + j7];
r_ab_y_7 = r_ab_y_lut[i * n_p + j7];
r_ab_norm_7 = r_ab_norm_lut[i * n_p + j7];
} else {
// other direction
// I think here we can optimize for sure, currently not using LUT good
r_ab_x_7 = -1 * r_ab_x_lut[j7 * n_p + i];
r_ab_y_7 = -1 * r_ab_y_lut[j7 * n_p + i];
r_ab_norm_7 = r_ab_norm_lut[j7 * n_p + i];
}
// the math, see paper
double r_ab2_x_7 = r_ab_x_7 - pedestrians[j7].s_b_e_x;
double r_ab2_y_7 = r_ab_y_7 - pedestrians[j7].s_b_e_y;
double r_ab2_norm_7 = sqrt(r_ab2_x_7 * r_ab2_x_7 + r_ab2_y_7 * r_ab2_y_7);
double r_ab_norm_sum_7 = r_ab_norm_7 + r_ab2_norm_7;
double two_b_sq_7 = r_ab_norm_sum_7 * r_ab_norm_sum_7 - pedestrians[j7].s_b_sq;
double two_b_7 = sqrt(two_b_sq_7);
double scalar_mult_common_7 = (r_ab_norm_sum_7 / (two_b_7 * r_ab_norm_7 * r_ab2_norm_7)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b_7 * NEG_TWO_SIGMA_INV));
double scalar_mult_r_ab_7 = scalar_mult_common_7 * r_ab2_norm_7;
double scalar_mult_r_ab2_7 = scalar_mult_common_7 * r_ab_norm_7;
double f_alpha_beta_neg_x_7 = r_ab_x_7 * scalar_mult_r_ab_7 + r_ab2_x_7 * scalar_mult_r_ab2_7;
double f_alpha_beta_neg_y_7 = r_ab_y_7 * scalar_mult_r_ab_7 + r_ab2_y_7 * scalar_mult_r_ab2_7;
double w_val_7;
double a_7 = (p_e_x * f_alpha_beta_neg_x_7 + p_e_y * f_alpha_beta_neg_y_7) * cos_phi_inv;
double b_sq_7 = f_alpha_beta_neg_x_7 * f_alpha_beta_neg_x_7 + f_alpha_beta_neg_y_7 * f_alpha_beta_neg_y_7;
w_val_7 = dt_neg;
if (a_7 >= 0 && (a_7 * a_7 > b_sq_7)) {
w_val_7 = c_neg_times_dt;
}
acc_7_x += f_alpha_beta_neg_x_7 * w_val_7;
acc_7_y += f_alpha_beta_neg_y_7 * w_val_7;
}
}
// add the result
// 8 FLOPS = 8 ADD
f_alpha_dt_x += acc_0_x + acc_1_x + acc_2_x + acc_3_x + acc_4_x + acc_5_x + acc_6_x + acc_7_x;
// 8 FLOPS = 8 ADD
f_alpha_dt_y += acc_0_y + acc_1_y + acc_2_y + acc_3_y + acc_4_y + acc_5_y + acc_6_y + acc_7_y;
// remaining
// 66 FLOPS --> ignore for flop count already handled correctly with ILP loop
for (; j < n_p; j++) {
if (i == j) continue;
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 {
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];
}
// 1 FLOP = 1 ADD
double r_ab2_x = r_ab_x - pedestrians[j].s_b_e_x;
// 1 FLOP = 1 ADD
double r_ab2_y = r_ab_y - pedestrians[j].s_b_e_y;
// 4 FLOPS = 1 SQRT + 2 MULT + 1 ADD
double r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
// 1 FLOP = 1 ADD
double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
// 2 FLOPS = 1 MULT + 1 ADD
double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - pedestrians[j].s_b_sq;
// 1 FLOP = 1 SQRT
double two_b = sqrt(two_b_sq);
// 36 FLOPS = 1 DIV + 5 MULT + 1 EXP
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));
// 1 FLOP = 1 MULT
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// 1 FLOP = 1 MULT
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
// 3 FLOPS = 2 MULT + 1 ADD
double f_alpha_beta_neg_x = r_ab_x * scalar_mult_r_ab + r_ab2_x * scalar_mult_r_ab2;
// 3 FLOPS = 2 MULT + 1 ADD
double f_alpha_beta_neg_y = r_ab_y * scalar_mult_r_ab + r_ab2_y * scalar_mult_r_ab2;
double w_val;
// 4 FLOPS = 3 MULT + 1 ADD
double a = (p_e_x * f_alpha_beta_neg_x + p_e_y * f_alpha_beta_neg_y) * cos_phi_inv;
// 3 FLOPS = 2 MULT + 1 ADD
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;
// 1 FLOP = 1 MULT
if (a >= 0 && (a * a > b_sq)) {
w_val = c_neg_times_dt;
}
// 2 FLOPS = 1 ADD + 1 MULT
f_alpha_dt_x = f_alpha_dt_x + f_alpha_beta_neg_x * w_val;
// 2 FLOPS = 1 ADD + 1 MULT
f_alpha_dt_y = f_alpha_dt_y + f_alpha_beta_neg_y * w_val;
}
// force between pedestrian and obstacles
// initialized with 0 obstacles, might consider deleting this?
// to sparse to use ILP
// FLOPS = n_obstacles * 63 FLOPS
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;
}
// 1 FLOP = 1 ADD
p.w_x += f_alpha_dt_x;
// 1 FLOP = 1 ADD
p.w_y += f_alpha_dt_y;
}
// update the positions etc.
// ILP is not possible I think
// FLOPCOUNT = n_p * 29 FLOPS
// n_p * (7 ADD + 17 MULT + 3 SQRT + 2 DIV)
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;
// maybe move this calculation to a separate loop to enable ILP
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;
}
@@ -166,23 +166,42 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// # 128 MULTs, 112 ADDs
static inline __m512d _avx512_exp_pd(__m512d x) {
const __m512d log2e = _mm512_set1_pd(1.4426950408889634);
// # 8 MULTs
__m512d nf = _mm512_roundscale_pd(_mm512_mul_pd(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
const __m512d ln2 = _mm512_set1_pd(0.6931471805599453);
// # 8 MULTs, 8 ADDs
__m512d r = _mm512_fnmadd_pd(nf, ln2, x);
const __m512d c7 = _mm512_set1_pd(1.0 / 5040.0);
const __m512d c6 = _mm512_set1_pd(1.0 / 720.0);
const __m512d c5 = _mm512_set1_pd(1.0 / 120.0);
const __m512d c4 = _mm512_set1_pd(1.0 / 24.0);
const __m512d c3 = _mm512_set1_pd(1.0 / 6.0);
const __m512d c2 = _mm512_set1_pd(0.5);
const __m512d c1 = _mm512_set1_pd(1.0);
const __m512d c0 = _mm512_set1_pd(1.0);
const __m512d c13 = _mm512_set1_pd(c13_scalar);
const __m512d c12 = _mm512_set1_pd(c12_scalar);
const __m512d c11 = _mm512_set1_pd(c11_scalar);
const __m512d c10 = _mm512_set1_pd(c10_scalar);
const __m512d c9 = _mm512_set1_pd(c9_scalar);
const __m512d c8 = _mm512_set1_pd(c8_scalar);
const __m512d c7 = _mm512_set1_pd(c7_scalar);
const __m512d c6 = _mm512_set1_pd(c6_scalar);
const __m512d c5 = _mm512_set1_pd(c5_scalar);
const __m512d c4 = _mm512_set1_pd(c4_scalar);
const __m512d c3 = _mm512_set1_pd(c3_scalar);
const __m512d c2 = _mm512_set1_pd(c2_scalar);
const __m512d c1 = _mm512_set1_pd(c1_scalar);
const __m512d c0 = _mm512_set1_pd(c0_scalar);
__m512d p = c7;
__m512d p = c13;
// # 104 MULTs, 104 ADDs
p = _mm512_fmadd_pd(p, r, c12);
p = _mm512_fmadd_pd(p, r, c11);
p = _mm512_fmadd_pd(p, r, c10);
p = _mm512_fmadd_pd(p, r, c9);
p = _mm512_fmadd_pd(p, r, c8);
p = _mm512_fmadd_pd(p, r, c7);
p = _mm512_fmadd_pd(p, r, c6);
p = _mm512_fmadd_pd(p, r, c5);
p = _mm512_fmadd_pd(p, r, c4);
@@ -208,6 +227,7 @@ static inline __m512d _avx512_exp_pd(__m512d x) {
__m512d pow2n = _mm512_castsi512_pd(scaled);
// # 8 MULTs
return _mm512_mul_pd(p, pow2n);
}
@@ -261,6 +281,23 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
const __m512d v_np = _mm512_set1_pd(n_p);
// # ADDs: n_iter * n_p * 33 +
// # n_iter * n_p * (n_p_pad / 8) * 232 +
// # n_iter * n_p * board.n_obstacles * 10
// # MULTs: n_iter * n_p * 40 +
// # n_iter * n_p * (n_p_pad / 8) * 320 +
// # n_iter * n_p * board.n_obstacles * 13
// # DIVs: n_iter * n_p * (n_p_pad / 8) * 8 +
// # n_iter * n_p * board.n_obstacles
// # SQRTs: n_iter * n_p +
// # n_iter * n_p * (n_p_pad / 8) * 24 +
// # n_iter * n_p * board.n_obstacles
// # EXPs: n_iter * n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
@@ -14,6 +14,8 @@
#define SEED 42
#define BOARD_WIDTH 10.0
#define BOARD_LENGTH 50.0
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5
#define WALL_EPS 1e-3
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999
@@ -163,23 +165,42 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// # 128 MULTs, 112 ADDs
static inline __m512d _avx512_exp_pd(__m512d x) {
const __m512d log2e = _mm512_set1_pd(1.4426950408889634);
// # 8 MULTs
__m512d nf = _mm512_roundscale_pd(_mm512_mul_pd(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
const __m512d ln2 = _mm512_set1_pd(0.6931471805599453);
// # 8 MULTs, 8 ADDs
__m512d r = _mm512_fnmadd_pd(nf, ln2, x);
const __m512d c7 = _mm512_set1_pd(1.0 / 5040.0);
const __m512d c6 = _mm512_set1_pd(1.0 / 720.0);
const __m512d c5 = _mm512_set1_pd(1.0 / 120.0);
const __m512d c4 = _mm512_set1_pd(1.0 / 24.0);
const __m512d c3 = _mm512_set1_pd(1.0 / 6.0);
const __m512d c2 = _mm512_set1_pd(0.5);
const __m512d c1 = _mm512_set1_pd(1.0);
const __m512d c0 = _mm512_set1_pd(1.0);
const __m512d c13 = _mm512_set1_pd(c13_scalar);
const __m512d c12 = _mm512_set1_pd(c12_scalar);
const __m512d c11 = _mm512_set1_pd(c11_scalar);
const __m512d c10 = _mm512_set1_pd(c10_scalar);
const __m512d c9 = _mm512_set1_pd(c9_scalar);
const __m512d c8 = _mm512_set1_pd(c8_scalar);
const __m512d c7 = _mm512_set1_pd(c7_scalar);
const __m512d c6 = _mm512_set1_pd(c6_scalar);
const __m512d c5 = _mm512_set1_pd(c5_scalar);
const __m512d c4 = _mm512_set1_pd(c4_scalar);
const __m512d c3 = _mm512_set1_pd(c3_scalar);
const __m512d c2 = _mm512_set1_pd(c2_scalar);
const __m512d c1 = _mm512_set1_pd(c1_scalar);
const __m512d c0 = _mm512_set1_pd(c0_scalar);
__m512d p = c7;
__m512d p = c13;
// # 104 MULTs, 104 ADDs
p = _mm512_fmadd_pd(p, r, c12);
p = _mm512_fmadd_pd(p, r, c11);
p = _mm512_fmadd_pd(p, r, c10);
p = _mm512_fmadd_pd(p, r, c9);
p = _mm512_fmadd_pd(p, r, c8);
p = _mm512_fmadd_pd(p, r, c7);
p = _mm512_fmadd_pd(p, r, c6);
p = _mm512_fmadd_pd(p, r, c5);
p = _mm512_fmadd_pd(p, r, c4);
@@ -205,9 +226,11 @@ static inline __m512d _avx512_exp_pd(__m512d x) {
__m512d pow2n = _mm512_castsi512_pd(scaled);
// # 8 MULTs
return _mm512_mul_pd(p, pow2n);
}
// # 10 MULTs, 3 ADDs
static inline double fast_inv_sqrt(double number) {
double y = number;
@@ -258,9 +281,26 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
const __m512d v_np = _mm512_set1_pd(n_p);
for (int t = 0; t < n_iter; t++) {
// # ADDs: n_iter * n_p * 33 +
// # n_iter * n_p * (n_p_pad / 8) * 232 +
// # n_iter * n_p * board.n_obstacles * 10
// # MULTs: n_iter * n_p * 40 +
// # n_iter * n_p * (n_p_pad / 8) * 320 +
// # n_iter * n_p * board.n_obstacles * 13
// # DIVs: n_iter * n_p * (n_p_pad / 8) * 8 +
// # n_iter * n_p * board.n_obstacles
// # SQRTs: n_iter * n_p +
// # n_iter * n_p * (n_p_pad / 8) * 24 +
// # n_iter * n_p * board.n_obstacles
// # EXPs: n_iter * n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
double pos_x_i = pos_x[i];
double pos_y_i = pos_y[i];
double e_x_i = e_x[i];
@@ -269,6 +309,7 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
double curr_vel_x_i = curr_vel_x[i];
double curr_vel_y_i = curr_vel_y[i];
// # 4 MULTs, 2 ADDs
double f_alpha_dt_x = (e_x_i * desired_v0_i - curr_vel_x_i) * dt_times_tau_alpha_inv;
double f_alpha_dt_y = (e_y_i * desired_v0_i - curr_vel_y_i) * dt_times_tau_alpha_inv;
@@ -282,6 +323,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512d acc_y = _mm512_setzero_pd();
for (int j = 0; j < n_p_pad; j += 8) {
// # 8 ADDs
__m512d j_idx = _mm512_add_pd(_mm512_set1_pd((double)j), lane_off);
__mmask8 m_neq_i = _mm512_cmp_pd_mask(j_idx, v_i, _CMP_NEQ_OQ);
@@ -294,36 +337,65 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512d sbe_jy = _mm512_loadu_pd(&sb_e_y[j]);
__m512d sb_jsq = _mm512_loadu_pd(&sb_sq[j]);
// # 16 ADDs
__m512d r_ab_x = _mm512_sub_pd(pix, pjx);
__m512d r_ab_y = _mm512_sub_pd(piy, pjy);
// # 16 MULTs, 8 ADDs
__m512d r_ab_n2 = _mm512_fmadd_pd(r_ab_x, r_ab_x, _mm512_mul_pd(r_ab_y, r_ab_y));
// # 8 SQRTs
__m512d r_ab_n = _mm512_sqrt_pd(r_ab_n2);
// # 16 ADDs
__m512d r_ab2_x = _mm512_sub_pd(r_ab_x, sbe_jx);
__m512d r_ab2_y = _mm512_sub_pd(r_ab_y, sbe_jy);
// # 16 MULTs, 8 ADDs
__m512d r_ab2_n2 = _mm512_fmadd_pd(r_ab2_x, r_ab2_x, _mm512_mul_pd(r_ab2_y, r_ab2_y));
// # 8 SQRTs
__m512d r_ab2_n = _mm512_sqrt_pd(r_ab2_n2);
// # 8 ADDs
__m512d r_sum = _mm512_add_pd(r_ab_n, r_ab2_n);
// # 8 MULTs, 8 ADDs
__m512d two_b_sq = _mm512_fmsub_pd(r_sum, r_sum, sb_jsq);
// # 8 SQRTs
__m512d two_b = _mm512_sqrt_pd(two_b_sq);
// # 8 MULTs
__m512d e_arg = _mm512_mul_pd(two_b, v_neg_2s_inv);
// # 128 MULTs, 112 ADDs
__m512d e_val = _avx512_exp_pd(e_arg);
// # 32 MULTs
__m512d numer = _mm512_mul_pd(_mm512_mul_pd(r_sum, v_neg_v0_div_2s), e_val);
__m512d denom = _mm512_mul_pd(_mm512_mul_pd(two_b, r_ab_n), r_ab2_n);
__m512d denom_safe = _mm512_mask_blend_pd(active, _mm512_set1_pd(1.0), denom);
// # 8 DIVs
__m512d smc = _mm512_div_pd(numer, denom_safe);
// # 16 MULTs
__m512d sm_r_ab = _mm512_mul_pd(smc, r_ab2_n);
__m512d sm_r_ab2 = _mm512_mul_pd(smc, r_ab_n);
// # 32 MULTs, 16 ADDs
__m512d f_x = _mm512_fmadd_pd(r_ab2_x, sm_r_ab2, _mm512_mul_pd(r_ab_x, sm_r_ab));
__m512d f_y = _mm512_fmadd_pd(r_ab2_y, sm_r_ab2, _mm512_mul_pd(r_ab_y, sm_r_ab));
// # 24 MULTs, 8 ADDs
__m512d a = _mm512_mul_pd(_mm512_fmadd_pd(eix, f_x, _mm512_mul_pd(eiy, f_y)), v_cos_phi_inv);
// # 8 MULTs
__m512d a_sq = _mm512_mul_pd(a, a);
// # 16 MULTs, 8 ADDs
__m512d b_sq = _mm512_fmadd_pd(f_x, f_x, _mm512_mul_pd(f_y, f_y));
__mmask8 k_ge = _mm512_cmp_pd_mask(a, v_zero, _CMP_GE_OQ);
@@ -333,10 +405,13 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512d f_x_safe = _mm512_maskz_mov_pd(active, f_x);
__m512d f_y_safe = _mm512_maskz_mov_pd(active, f_y);
// # 16 MULTs, 16 ADDs
acc_x = _mm512_fmadd_pd(f_x_safe, w_val, acc_x);
acc_y = _mm512_fmadd_pd(f_y_safe, w_val, acc_y);
}
// # 16 ADDs
f_alpha_dt_x += _mm512_reduce_add_pd(acc_x);
f_alpha_dt_y += _mm512_reduce_add_pd(acc_y);
@@ -348,9 +423,11 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
double obs_v_y = board.obstacles[k].obstacle_v_y;
double obs_length_sq_inv = board.obstacles[k].length_sq_inv;
// # 2 ADDs
double v1_x = pos_x_i - obs_start_x;
double v1_y = pos_y_i - obs_start_y;
// # 3 MULTs, 1 ADD
double projection = (v1_x * obs_v_x + v1_y * obs_v_y) * obs_length_sq_inv;
if (projection < 0.0)
@@ -358,11 +435,17 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
else if (projection > 1.0)
projection = 1.0;
// # 2 MULTs, 4 ADDs
double r_aB_x = pos_x_i - (obs_start_x + obs_v_x * projection);
double r_aB_y = pos_y_i - (obs_start_y + obs_v_y * projection);
// # 1 SQRT, 2 MULTs, 1 ADD
double r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
// # 2 MULTs, 1 EXP, 1 DIV
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// # 2 MULTs
double f_alpha_B_neg_x = r_aB_x * scalar_mult;
double f_alpha_B_neg_y = r_aB_y * scalar_mult;
@@ -371,15 +454,18 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
if (e_dot_f < cos_phi)
w_val = c_neg_times_dt;
// # 2 MULTs, 2 ADDs
f_alpha_dt_x += f_alpha_B_neg_x * w_val;
f_alpha_dt_y += f_alpha_B_neg_y * w_val;
}
// # 2 ADDs
w_x[i] += f_alpha_dt_x;
w_y[i] += f_alpha_dt_y;
}
for (int i = 0; i < n_p; i++) {
double color_i = color[i];
double curr_vel_x_i = curr_vel_x[i];
double curr_vel_y_i = curr_vel_y[i];
@@ -394,17 +480,21 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
double destination_x_i = destination_x[i];
double destination_y_i = destination_y[i];
// # 12 MULTs, 4 ADDs
double w_norm_inv = fast_inv_sqrt(w_x_i * w_x_i + w_y_i * w_y_i);
// # 1 MULT
double correction_factor = min(1.0, v_max_i * w_norm_inv);
// # 2 MULTs
curr_vel_x_i = w_x_i * correction_factor;
curr_vel_y_i = w_y_i * correction_factor;
// # 2 MULTs, 2 ADDs
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 && pos_x_i > (BOARD_LENGTH - 1.5)) {
if (!color_i && pos_x_i > (BOARD_LENGTH_MINUS_ONE_POINT_FIVE)) {
pos_x_i = 1.0;
destination_y_i = pos_y_i;
w_x_i = desired_v0_i;
@@ -413,7 +503,7 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
curr_vel_y_i = w_y_i;
}
if (color_i && pos_x_i < 1.5) {
pos_x_i = BOARD_LENGTH - 1.0;
pos_x_i = BOARD_LENGTH_MINUS_ONE;
destination_y_i = pos_y_i;
w_x_i = -desired_v0_i;
w_y_i = 0.0;
@@ -423,16 +513,24 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
pos_y_i = clamp(pos_y_i, WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
// # 2 ADDs
double ex = destination_x_i - pos_x_i;
double ey = destination_y_i - pos_y_i;
// # 12 MULTs, 4 ADDs
double e_norm_inv = fast_inv_sqrt(ex * ex + ey * ey);
// # 2 MULTs
e_x_i = ex * e_norm_inv;
e_y_i = ey * e_norm_inv;
// # 2 MULTs, 1 ADD
double velocity_norm_sq = curr_vel_x_i * curr_vel_x_i + curr_vel_y_i * curr_vel_y_i;
// # 1 MULT
double sb_sq_i = velocity_norm_sq * delta_t_sq;
// # 1 SQRT
double velocity_norm_mul_delta_t = sqrt(sb_sq_i);
curr_vel_x[i] = curr_vel_x_i;
@@ -450,6 +548,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
destination_y[i] = destination_y_i;
sb_sq[i] = sb_sq_i;
// # 2 MULTs
sb_e_x[i] = e_x_i * velocity_norm_mul_delta_t;
sb_e_y[i] = e_y_i * velocity_norm_mul_delta_t;
@@ -13,6 +13,8 @@
#define SEED 42
#define BOARD_WIDTH 10.0
#define BOARD_LENGTH 50.0
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5
#define WALL_EPS 1e-3
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999
@@ -162,23 +164,42 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// # 128 MULTs, 112 ADDs
static inline __m512d _avx512_exp_pd(__m512d x) {
const __m512d log2e = _mm512_set1_pd(1.4426950408889634);
// # 8 MULTs
__m512d nf = _mm512_roundscale_pd(_mm512_mul_pd(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
const __m512d ln2 = _mm512_set1_pd(0.6931471805599453);
// # 8 MULTs, 8 ADDs
__m512d r = _mm512_fnmadd_pd(nf, ln2, x);
const __m512d c7 = _mm512_set1_pd(1.0 / 5040.0);
const __m512d c6 = _mm512_set1_pd(1.0 / 720.0);
const __m512d c5 = _mm512_set1_pd(1.0 / 120.0);
const __m512d c4 = _mm512_set1_pd(1.0 / 24.0);
const __m512d c3 = _mm512_set1_pd(1.0 / 6.0);
const __m512d c2 = _mm512_set1_pd(0.5);
const __m512d c1 = _mm512_set1_pd(1.0);
const __m512d c0 = _mm512_set1_pd(1.0);
const __m512d c13 = _mm512_set1_pd(c13_scalar);
const __m512d c12 = _mm512_set1_pd(c12_scalar);
const __m512d c11 = _mm512_set1_pd(c11_scalar);
const __m512d c10 = _mm512_set1_pd(c10_scalar);
const __m512d c9 = _mm512_set1_pd(c9_scalar);
const __m512d c8 = _mm512_set1_pd(c8_scalar);
const __m512d c7 = _mm512_set1_pd(c7_scalar);
const __m512d c6 = _mm512_set1_pd(c6_scalar);
const __m512d c5 = _mm512_set1_pd(c5_scalar);
const __m512d c4 = _mm512_set1_pd(c4_scalar);
const __m512d c3 = _mm512_set1_pd(c3_scalar);
const __m512d c2 = _mm512_set1_pd(c2_scalar);
const __m512d c1 = _mm512_set1_pd(c1_scalar);
const __m512d c0 = _mm512_set1_pd(c0_scalar);
__m512d p = c7;
__m512d p = c13;
// # 104 MULTs, 104 ADDs
p = _mm512_fmadd_pd(p, r, c12);
p = _mm512_fmadd_pd(p, r, c11);
p = _mm512_fmadd_pd(p, r, c10);
p = _mm512_fmadd_pd(p, r, c9);
p = _mm512_fmadd_pd(p, r, c8);
p = _mm512_fmadd_pd(p, r, c7);
p = _mm512_fmadd_pd(p, r, c6);
p = _mm512_fmadd_pd(p, r, c5);
p = _mm512_fmadd_pd(p, r, c4);
@@ -204,6 +225,7 @@ static inline __m512d _avx512_exp_pd(__m512d x) {
__m512d pow2n = _mm512_castsi512_pd(scaled);
// # 8 MULTs
return _mm512_mul_pd(p, pow2n);
}
@@ -240,6 +262,26 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
const __m512d v_np = _mm512_set1_pd(n_p);
// # ADDs: n_iter * n_p * 20 +
// # n_iter * n_p * (n_p_pad / 8) * 232 +
// # n_iter * n_p * board.n_obstacles * 10 +
// # n_iter * (n_p_pad / 8) * 56
// # MULTs: n_iter * n_p * 4 +
// # n_iter * n_p * (n_p_pad / 8) * 320 +
// # n_iter * n_p * board.n_obstacles * 13 +
// # n_iter * (n_p_pad / 8) * 120
// # DIVs: n_iter * n_p * (n_p_pad / 8) * 8 +
// # n_iter * n_p * board.n_obstacles +
// # n_iter * (n_p_pad / 8) * 16
// # SQRTs: n_iter * n_p * (n_p_pad / 8) * 24 +
// # n_iter * n_p * board.n_obstacles +
// # n_iter * (n_p_pad / 8) * 24
// # EXPs: n_iter * n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
@@ -251,6 +293,7 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
double curr_vel_x_i = curr_vel_x[i];
double curr_vel_y_i = curr_vel_y[i];
// # 4 MULTs, 2 ADDs
double f_alpha_dt_x = (e_x_i * desired_v0_i - curr_vel_x_i) * dt_times_tau_alpha_inv;
double f_alpha_dt_y = (e_y_i * desired_v0_i - curr_vel_y_i) * dt_times_tau_alpha_inv;
@@ -264,6 +307,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512d acc_y = _mm512_setzero_pd();
for (int j = 0; j < n_p_pad; j += 8) {
// # 8 ADDs
__m512d j_idx = _mm512_add_pd(_mm512_set1_pd((double)j), lane_off);
__mmask8 m_neq_i = _mm512_cmp_pd_mask(j_idx, v_i, _CMP_NEQ_OQ);
@@ -276,35 +321,63 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512d sbe_jy = _mm512_loadu_pd(&sb_e_y[j]);
__m512d sb_jsq = _mm512_loadu_pd(&sb_sq[j]);
// # 16 ADDs
__m512d r_ab_x = _mm512_sub_pd(pix, pjx);
__m512d r_ab_y = _mm512_sub_pd(piy, pjy);
// # 16 MULTs, 8 ADDs
__m512d r_ab_n2 = _mm512_fmadd_pd(r_ab_x, r_ab_x, _mm512_mul_pd(r_ab_y, r_ab_y));
// # 8 SQRTs
__m512d r_ab_n = _mm512_sqrt_pd(r_ab_n2);
// # 16 ADDs
__m512d r_ab2_x = _mm512_sub_pd(r_ab_x, sbe_jx);
__m512d r_ab2_y = _mm512_sub_pd(r_ab_y, sbe_jy);
// # 16 MULTs, 8 ADDs
__m512d r_ab2_n2 = _mm512_fmadd_pd(r_ab2_x, r_ab2_x, _mm512_mul_pd(r_ab2_y, r_ab2_y));
// # 8 SQRTs
__m512d r_ab2_n = _mm512_sqrt_pd(r_ab2_n2);
// # 8 ADDs
__m512d r_sum = _mm512_add_pd(r_ab_n, r_ab2_n);
// # 8 MULTs, 8 ADDs
__m512d two_b_sq = _mm512_fmsub_pd(r_sum, r_sum, sb_jsq);
// # 8 SQRTs
__m512d two_b = _mm512_sqrt_pd(two_b_sq);
// # 8 MULTs
__m512d e_arg = _mm512_mul_pd(two_b, v_neg_2s_inv);
// # 128 MULTs, 112 ADDs
__m512d e_val = _avx512_exp_pd(e_arg);
// # 32 MULTs
__m512d numer = _mm512_mul_pd(_mm512_mul_pd(r_sum, v_neg_v0_div_2s), e_val);
__m512d denom = _mm512_mul_pd(_mm512_mul_pd(two_b, r_ab_n), r_ab2_n);
// # 8 DIVs
__m512d smc = _mm512_div_pd(numer, denom);
// # 16 MULTs
__m512d sm_r_ab = _mm512_mul_pd(smc, r_ab2_n);
__m512d sm_r_ab2 = _mm512_mul_pd(smc, r_ab_n);
// # 32 MULTs, 16 ADDs
__m512d f_x = _mm512_fmadd_pd(r_ab2_x, sm_r_ab2, _mm512_mul_pd(r_ab_x, sm_r_ab));
__m512d f_y = _mm512_fmadd_pd(r_ab2_y, sm_r_ab2, _mm512_mul_pd(r_ab_y, sm_r_ab));
// # 24 MULTs, 8 ADDs
__m512d a = _mm512_mul_pd(_mm512_fmadd_pd(eix, f_x, _mm512_mul_pd(eiy, f_y)), v_cos_phi_inv);
// # 8 MULTs
__m512d a_sq = _mm512_mul_pd(a, a);
// # 16 MULTs, 8 ADDs
__m512d b_sq = _mm512_fmadd_pd(f_x, f_x, _mm512_mul_pd(f_y, f_y));
__mmask8 k_ge = _mm512_cmp_pd_mask(a, v_zero, _CMP_GE_OQ);
@@ -314,18 +387,23 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512d f_x_safe = _mm512_maskz_mov_pd(active, f_x);
__m512d f_y_safe = _mm512_maskz_mov_pd(active, f_y);
// # 16 MULTs, 16 ADDs
acc_x = _mm512_fmadd_pd(f_x_safe, w_val, acc_x);
acc_y = _mm512_fmadd_pd(f_y_safe, w_val, acc_y);
}
// # 16 ADDS
f_alpha_dt_x += _mm512_reduce_add_pd(acc_x);
f_alpha_dt_y += _mm512_reduce_add_pd(acc_y);
for (int k = 0; k < board.n_obstacles; k++) {
// # 2 ADDs
double v1_x = pos_x_i - board.obstacles[k].start_x;
double v1_y = pos_y_i - board.obstacles[k].start_y;
// # 3 MULTs, 1 ADD
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)
@@ -333,11 +411,17 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
else if (projection > 1.0)
projection = 1.0;
// # 4 ADDs, 2 MULTs
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);
// # 1 SQRT, 2 MULTs, 1 ADD
double r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
// # 2 MULTs, 1 EXP, 1 DIV
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// # 2 MULTs
double f_alpha_B_neg_x = r_aB_x * scalar_mult;
double f_alpha_B_neg_y = r_aB_y * scalar_mult;
@@ -346,10 +430,12 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
if (e_dot_f < cos_phi)
w_val = c_neg_times_dt;
// # 2 MULTs, 2 ADDs
f_alpha_dt_x += f_alpha_B_neg_x * w_val;
f_alpha_dt_y += f_alpha_B_neg_y * w_val;
}
// # 2 ADDs
w_x[i] += f_alpha_dt_x;
w_y[i] += f_alpha_dt_y;
}
@@ -367,19 +453,27 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m256i color_i_vec = _mm256_loadu_si256((__m256i *)&color[i]);
// # 16 MULTs, 8 ADDs
__m512d w_norm_sq = _mm512_fmadd_pd(w_x_i_vec, w_x_i_vec, _mm512_mul_pd(w_y_i_vec, w_y_i_vec));
// # 8 SQRTs
__m512d w_norm = _mm512_sqrt_pd(w_norm_sq);
// # 8 DIVs
__m512d correction_factor = _mm512_min_pd(_mm512_set1_pd(1.0), _mm512_div_pd(v_max_i_vec, w_norm));
// # 16 MULTs
__m512d curr_vel_x_i_vec = _mm512_mul_pd(w_x_i_vec, correction_factor);
__m512d curr_vel_y_i_vec = _mm512_mul_pd(w_y_i_vec, correction_factor);
__m512d dt_vec = _mm512_set1_pd(dt);
// # 16 MULTs, 16 ADDs
pos_x_i_vec = _mm512_fmadd_pd(curr_vel_x_i_vec, dt_vec, pos_x_i_vec);
pos_y_i_vec = _mm512_fmadd_pd(curr_vel_y_i_vec, dt_vec, pos_y_i_vec);
__mmask8 cond_1_left = _mm256_cmpeq_epi32_mask(color_i_vec, _mm256_setzero_si256());
__mmask8 cond_1_right = _mm512_cmp_pd_mask(pos_x_i_vec, _mm512_set1_pd(BOARD_LENGTH - 1.5), _CMP_GT_OQ);
__mmask8 cond_1_right = _mm512_cmp_pd_mask(pos_x_i_vec, _mm512_set1_pd(BOARD_LENGTH_MINUS_ONE_POINT_FIVE), _CMP_GT_OQ);
__mmask8 cond_1 = cond_1_left & cond_1_right;
pos_x_i_vec = _mm512_mask_blend_pd(cond_1, pos_x_i_vec, _mm512_set1_pd(1.0));
@@ -404,16 +498,30 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
pos_y_i_vec = _mm512_max_pd(pos_y_i_vec, _mm512_set1_pd(WALL_EPS));
pos_y_i_vec = _mm512_min_pd(pos_y_i_vec, _mm512_set1_pd(BOARD_WIDTH_MINUS_WALL_EPS));
// # 16 ADDs
__m512d ex_vec = _mm512_sub_pd(destination_x_i_vec, pos_x_i_vec);
__m512d ey_vec = _mm512_sub_pd(destination_y_i_vec, pos_y_i_vec);
// # 16 MULTs, 8 ADDs
__m512d e_norm_sq = _mm512_fmadd_pd(ex_vec, ex_vec, _mm512_mul_pd(ey_vec, ey_vec));
// # 8 SQRTs
__m512d e_norm = _mm512_sqrt_pd(e_norm_sq);
// # 8 DIVs
__m512d e_norm_inv = _mm512_div_pd(_mm512_set1_pd(1.0), e_norm);
// # 16 MULT
__m512d e_x_i_vec = _mm512_mul_pd(ex_vec, e_norm_inv);
__m512d e_y_i_vec = _mm512_mul_pd(ey_vec, e_norm_inv);
// # 16 MULTs, 8 ADDs
__m512d vel_norm_sq = _mm512_fmadd_pd(curr_vel_x_i_vec, curr_vel_x_i_vec, _mm512_mul_pd(curr_vel_y_i_vec, curr_vel_y_i_vec));
// # 8 MULTs
__m512d sb_sq_i_vec = _mm512_mul_pd(vel_norm_sq, _mm512_set1_pd(delta_t_sq));
// # 8 SQRTs
__m512d vel_norm_dt = _mm512_sqrt_pd(sb_sq_i_vec);
_mm512_storeu_pd(&curr_vel_x[i], curr_vel_x_i_vec);
@@ -426,6 +534,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
_mm512_storeu_pd(&e_y[i], e_y_i_vec);
_mm512_storeu_pd(&destination_y[i], destination_y_i_vec);
_mm512_storeu_pd(&sb_sq[i], sb_sq_i_vec);
// # 16 MULTs
_mm512_storeu_pd(&sb_e_x[i], _mm512_mul_pd(e_x_i_vec, vel_norm_dt));
_mm512_storeu_pd(&sb_e_y[i], _mm512_mul_pd(e_y_i_vec, vel_norm_dt));
}
@@ -13,6 +13,8 @@
#define SEED 42
#define BOARD_WIDTH 10.0
#define BOARD_LENGTH 50.0
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5
#define WALL_EPS 1e-3
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999
@@ -162,23 +164,42 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// # 128 MULTs, 112 ADDs
static inline __m512d _avx512_exp_pd(__m512d x) {
const __m512d log2e = _mm512_set1_pd(1.4426950408889634);
// # 8 MULTs
__m512d nf = _mm512_roundscale_pd(_mm512_mul_pd(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
const __m512d ln2 = _mm512_set1_pd(0.6931471805599453);
// # 8 MULTs, 8 ADDs
__m512d r = _mm512_fnmadd_pd(nf, ln2, x);
const __m512d c7 = _mm512_set1_pd(1.0 / 5040.0);
const __m512d c6 = _mm512_set1_pd(1.0 / 720.0);
const __m512d c5 = _mm512_set1_pd(1.0 / 120.0);
const __m512d c4 = _mm512_set1_pd(1.0 / 24.0);
const __m512d c3 = _mm512_set1_pd(1.0 / 6.0);
const __m512d c2 = _mm512_set1_pd(0.5);
const __m512d c1 = _mm512_set1_pd(1.0);
const __m512d c0 = _mm512_set1_pd(1.0);
const __m512d c13 = _mm512_set1_pd(c13_scalar);
const __m512d c12 = _mm512_set1_pd(c12_scalar);
const __m512d c11 = _mm512_set1_pd(c11_scalar);
const __m512d c10 = _mm512_set1_pd(c10_scalar);
const __m512d c9 = _mm512_set1_pd(c9_scalar);
const __m512d c8 = _mm512_set1_pd(c8_scalar);
const __m512d c7 = _mm512_set1_pd(c7_scalar);
const __m512d c6 = _mm512_set1_pd(c6_scalar);
const __m512d c5 = _mm512_set1_pd(c5_scalar);
const __m512d c4 = _mm512_set1_pd(c4_scalar);
const __m512d c3 = _mm512_set1_pd(c3_scalar);
const __m512d c2 = _mm512_set1_pd(c2_scalar);
const __m512d c1 = _mm512_set1_pd(c1_scalar);
const __m512d c0 = _mm512_set1_pd(c0_scalar);
__m512d p = c7;
__m512d p = c13;
// # 104 MULTs, 104 ADDs
p = _mm512_fmadd_pd(p, r, c12);
p = _mm512_fmadd_pd(p, r, c11);
p = _mm512_fmadd_pd(p, r, c10);
p = _mm512_fmadd_pd(p, r, c9);
p = _mm512_fmadd_pd(p, r, c8);
p = _mm512_fmadd_pd(p, r, c7);
p = _mm512_fmadd_pd(p, r, c6);
p = _mm512_fmadd_pd(p, r, c5);
p = _mm512_fmadd_pd(p, r, c4);
@@ -204,6 +225,7 @@ static inline __m512d _avx512_exp_pd(__m512d x) {
__m512d pow2n = _mm512_castsi512_pd(scaled);
// # 8 MULTs
return _mm512_mul_pd(p, pow2n);
}
@@ -242,9 +264,30 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
const __m512d v_np = _mm512_set1_pd((float)n_p);
const __m512d v_exp_min = _mm512_set1_pd(-87.0f);
// # ADDs: n_iter * n_p * 36 +
// # n_iter * n_p * (n_p_pad / 16) * 464 +
// # n_iter * n_p * board.n_obstacles * 10 +
// # n_iter * (n_p_pad / 8) * 56
// # MULTs: n_iter * n_p * 4 +
// # n_iter * n_p * (n_p_pad / 16) * 640 +
// # n_iter * n_p * board.n_obstacles * 13 +
// # n_iter * (n_p_pad / 8) * 120
// # DIVs: n_iter * n_p * (n_p_pad / 16) * 16 +
// # n_iter * n_p * board.n_obstacles +
// # n_iter * (n_p_pad / 8) * 16
// # SQRTs: n_iter * n_p * (n_p_pad / 16) * 48 +
// # n_iter * n_p * board.n_obstacles +
// # n_iter * (n_p_pad / 8) * 24
// # EXPs: n_iter * n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
double pos_x_i = pos_x[i];
double pos_y_i = pos_y[i];
double e_x_i = e_x[i];
@@ -253,6 +296,7 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
double curr_vel_x_i = curr_vel_x[i];
double curr_vel_y_i = curr_vel_y[i];
// # 4 MULTs, 2 ADDs
double f_alpha_dt_x = (e_x_i * desired_v0_i - curr_vel_x_i) * dt_times_tau_alpha_inv;
double f_alpha_dt_y = (e_y_i * desired_v0_i - curr_vel_y_i) * dt_times_tau_alpha_inv;
@@ -268,6 +312,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512d acc_y_1 = _mm512_setzero_pd();
for (int j = 0; j < n_p_pad; j += 16) {
// # 16 ADDs
__m512d j_idx_0 = _mm512_add_pd(_mm512_set1_pd((double)j), lane_off);
__m512d j_idx_1 = _mm512_add_pd(_mm512_set1_pd((double)j + 8), lane_off);
@@ -295,81 +341,105 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512d sb_jsq_0 = _mm512_loadu_pd(&sb_sq[j]);
__m512d sb_jsq_1 = _mm512_loadu_pd(&sb_sq[j + 8]);
// # 16 ADDs
__m512d r_ab_x_0 = _mm512_sub_pd(pix, pjx_0);
__m512d r_ab_x_1 = _mm512_sub_pd(pix, pjx_1);
// # 16 ADDs
__m512d r_ab_y_0 = _mm512_sub_pd(piy, pjy_0);
__m512d r_ab_y_1 = _mm512_sub_pd(piy, pjy_1);
// # 32 MULTs, 16 ADDs
__m512d r_ab_n2_0 = _mm512_fmadd_pd(r_ab_x_0, r_ab_x_0, _mm512_mul_pd(r_ab_y_0, r_ab_y_0));
__m512d r_ab_n2_1 = _mm512_fmadd_pd(r_ab_x_1, r_ab_x_1, _mm512_mul_pd(r_ab_y_1, r_ab_y_1));
// # 16 SQRTs
__m512d r_ab_n_0 = _mm512_sqrt_pd(r_ab_n2_0);
__m512d r_ab_n_1 = _mm512_sqrt_pd(r_ab_n2_1);
// # 16 ADDs
__m512d r_ab2_x_0 = _mm512_sub_pd(r_ab_x_0, sbe_jx_0);
__m512d r_ab2_x_1 = _mm512_sub_pd(r_ab_x_1, sbe_jx_1);
// # 16 ADDs
__m512d r_ab2_y_0 = _mm512_sub_pd(r_ab_y_0, sbe_jy_0);
__m512d r_ab2_y_1 = _mm512_sub_pd(r_ab_y_1, sbe_jy_1);
// # 32 MULTs, 16 ADDs
__m512d r_ab2_n2_0 = _mm512_fmadd_pd(r_ab2_x_0, r_ab2_x_0, _mm512_mul_pd(r_ab2_y_0, r_ab2_y_0));
__m512d r_ab2_n2_1 = _mm512_fmadd_pd(r_ab2_x_1, r_ab2_x_1, _mm512_mul_pd(r_ab2_y_1, r_ab2_y_1));
// # 16 SQRTs
__m512d r_ab2_n_0 = _mm512_sqrt_pd(r_ab2_n2_0);
__m512d r_ab2_n_1 = _mm512_sqrt_pd(r_ab2_n2_1);
// # 16 ADDs
__m512d r_sum_0 = _mm512_add_pd(r_ab_n_0, r_ab2_n_0);
__m512d r_sum_1 = _mm512_add_pd(r_ab_n_1, r_ab2_n_1);
// # 16 MULTs, 16 ADDs
__m512d two_b_sq_0 = _mm512_fmsub_pd(r_sum_0, r_sum_0, sb_jsq_0);
__m512d two_b_sq_1 = _mm512_fmsub_pd(r_sum_1, r_sum_1, sb_jsq_1);
// # 16 SQRTs
__m512d two_b_0 = _mm512_sqrt_pd(two_b_sq_0);
__m512d two_b_1 = _mm512_sqrt_pd(two_b_sq_1);
// # 16 MULTs
__m512d e_arg_0 = _mm512_mul_pd(two_b_0, v_neg_2s_inv);
__m512d e_arg_1 = _mm512_mul_pd(two_b_1, v_neg_2s_inv);
e_arg_0 = _mm512_max_pd(e_arg_0, v_exp_min);
e_arg_1 = _mm512_max_pd(e_arg_1, v_exp_min);
// # 256 MULTs, 224 ADDs
__m512d e_val_0 = _avx512_exp_pd(e_arg_0);
__m512d e_val_1 = _avx512_exp_pd(e_arg_1);
// # 32 MULTs
__m512d numer_0 = _mm512_mul_pd(_mm512_mul_pd(r_sum_0, v_neg_v0_div_2s), e_val_0);
__m512d numer_1 = _mm512_mul_pd(_mm512_mul_pd(r_sum_1, v_neg_v0_div_2s), e_val_1);
// # 32 MULTs
__m512d denom_0 = _mm512_mul_pd(_mm512_mul_pd(two_b_0, r_ab_n_0), r_ab2_n_0);
__m512d denom_1 = _mm512_mul_pd(_mm512_mul_pd(two_b_1, r_ab_n_1), r_ab2_n_1);
__m512d denom_safe_0 = _mm512_mask_blend_pd(active_0, v_one, denom_0);
__m512d denom_safe_1 = _mm512_mask_blend_pd(active_1, v_one, denom_1);
// # 16 DIVs
__m512d smc_0 = _mm512_div_pd(numer_0, denom_safe_0);
__m512d smc_1 = _mm512_div_pd(numer_1, denom_safe_1);
// # 16 MULTs
__m512d sm_r_ab_0 = _mm512_mul_pd(smc_0, r_ab2_n_0);
__m512d sm_r_ab_1 = _mm512_mul_pd(smc_1, r_ab2_n_1);
// # 16 MULTs
__m512d sm_r_ab2_0 = _mm512_mul_pd(smc_0, r_ab_n_0);
__m512d sm_r_ab2_1 = _mm512_mul_pd(smc_1, r_ab_n_1);
// # 32 MULTs, 16 ADDs
__m512d f_x_0 = _mm512_fmadd_pd(r_ab2_x_0, sm_r_ab2_0, _mm512_mul_pd(r_ab_x_0, sm_r_ab_0));
__m512d f_x_1 = _mm512_fmadd_pd(r_ab2_x_1, sm_r_ab2_1, _mm512_mul_pd(r_ab_x_1, sm_r_ab_1));
// # 32 MULTs, 16 ADDs
__m512d f_y_0 = _mm512_fmadd_pd(r_ab2_y_0, sm_r_ab2_0, _mm512_mul_pd(r_ab_y_0, sm_r_ab_0));
__m512d f_y_1 = _mm512_fmadd_pd(r_ab2_y_1, sm_r_ab2_1, _mm512_mul_pd(r_ab_y_1, sm_r_ab_1));
// # 32 MULTs, 16 ADDs
__m512d f_norm_sq_0 = _mm512_fmadd_pd(f_x_0, f_x_0, _mm512_mul_pd(f_y_0, f_y_0));
__m512d f_norm_sq_1 = _mm512_fmadd_pd(f_x_1, f_x_1, _mm512_mul_pd(f_y_1, f_y_1));
// # 16 MULTs
__m512d rhs_0 = _mm512_mul_pd(f_norm_sq_0, v_cos_phi_sq);
__m512d rhs_1 = _mm512_mul_pd(f_norm_sq_1, v_cos_phi_sq);
// # 32 MULTs, 16 ADDs
__m512d e_dot_f_0 = _mm512_fmadd_pd(eix, f_x_0, _mm512_mul_pd(eiy, f_y_0));
__m512d e_dot_f_1 = _mm512_fmadd_pd(eix, f_x_1, _mm512_mul_pd(eiy, f_y_1));
// # 16 MULTs
__m512d e_dot_f_sq_0 = _mm512_mul_pd(e_dot_f_0, e_dot_f_0);
__m512d e_dot_f_sq_1 = _mm512_mul_pd(e_dot_f_1, e_dot_f_1);
@@ -391,21 +461,26 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m512d f_y_safe_0 = _mm512_maskz_mov_pd(active_0, f_y_0);
__m512d f_y_safe_1 = _mm512_maskz_mov_pd(active_1, f_y_1);
// # 16 MULTs, 16 ADDs
acc_x_0 = _mm512_fmadd_pd(f_x_safe_0, w_val_0, acc_x_0);
acc_x_1 = _mm512_fmadd_pd(f_x_safe_1, w_val_1, acc_x_1);
// # 16 MULTs, 16 ADDs
acc_y_0 = _mm512_fmadd_pd(f_y_safe_0, w_val_0, acc_y_0);
acc_y_1 = _mm512_fmadd_pd(f_y_safe_1, w_val_1, acc_y_1);
}
// # 32 ADDs
f_alpha_dt_x += _mm512_reduce_add_pd(_mm512_add_pd(acc_x_0, acc_x_1));
f_alpha_dt_y += _mm512_reduce_add_pd(_mm512_add_pd(acc_y_0, acc_y_1));
for (int k = 0; k < board.n_obstacles; k++) {
// # 2 ADDs
double v1_x = pos_x_i - board.obstacles[k].start_x;
double v1_y = pos_y_i - board.obstacles[k].start_y;
// # 3 MULTs, 1 ADD
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)
@@ -413,11 +488,17 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
else if (projection > 1.0)
projection = 1.0;
// # 4 ADDs, 2 MULTs
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);
// # 1 SQRT, 2 MULTs, 1 ADD
double r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
// # 2 MULTs, 1 EXP, 1 DIV
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// # 2 MULTs
double f_alpha_B_neg_x = r_aB_x * scalar_mult;
double f_alpha_B_neg_y = r_aB_y * scalar_mult;
@@ -426,10 +507,12 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
if (e_dot_f < cos_phi)
w_val = c_neg_times_dt;
// # 2 MULTs, 2 ADDs
f_alpha_dt_x += f_alpha_B_neg_x * w_val;
f_alpha_dt_y += f_alpha_B_neg_y * w_val;
}
// # 2 ADDs
w_x[i] += f_alpha_dt_x;
w_y[i] += f_alpha_dt_y;
}
@@ -447,19 +530,27 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m256i color_i_vec = _mm256_loadu_si256((__m256i *)&color[i]);
// # 16 MULTs, 8 ADDs
__m512d w_norm_sq = _mm512_fmadd_pd(w_x_i_vec, w_x_i_vec, _mm512_mul_pd(w_y_i_vec, w_y_i_vec));
// # 8 SQRTs
__m512d w_norm = _mm512_sqrt_pd(w_norm_sq);
// # 8 DIVs
__m512d correction_factor = _mm512_min_pd(_mm512_set1_pd(1.0), _mm512_div_pd(v_max_i_vec, w_norm));
// # 16 MULTs
__m512d curr_vel_x_i_vec = _mm512_mul_pd(w_x_i_vec, correction_factor);
__m512d curr_vel_y_i_vec = _mm512_mul_pd(w_y_i_vec, correction_factor);
__m512d dt_vec = _mm512_set1_pd(dt);
// # 16 MULTs, 16 ADDs
pos_x_i_vec = _mm512_fmadd_pd(curr_vel_x_i_vec, dt_vec, pos_x_i_vec);
pos_y_i_vec = _mm512_fmadd_pd(curr_vel_y_i_vec, dt_vec, pos_y_i_vec);
__mmask8 cond_1_left = _mm256_cmpeq_epi32_mask(color_i_vec, _mm256_setzero_si256());
__mmask8 cond_1_right = _mm512_cmp_pd_mask(pos_x_i_vec, _mm512_set1_pd(BOARD_LENGTH - 1.5), _CMP_GT_OQ);
__mmask8 cond_1_right = _mm512_cmp_pd_mask(pos_x_i_vec, _mm512_set1_pd(BOARD_LENGTH_MINUS_ONE_POINT_FIVE), _CMP_GT_OQ);
__mmask8 cond_1 = cond_1_left & cond_1_right;
pos_x_i_vec = _mm512_mask_blend_pd(cond_1, pos_x_i_vec, _mm512_set1_pd(1.0));
@@ -484,16 +575,30 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
pos_y_i_vec = _mm512_max_pd(pos_y_i_vec, _mm512_set1_pd(WALL_EPS));
pos_y_i_vec = _mm512_min_pd(pos_y_i_vec, _mm512_set1_pd(BOARD_WIDTH_MINUS_WALL_EPS));
// # 16 ADDs
__m512d ex_vec = _mm512_sub_pd(destination_x_i_vec, pos_x_i_vec);
__m512d ey_vec = _mm512_sub_pd(destination_y_i_vec, pos_y_i_vec);
// # 16 MULTs, 8 ADDs
__m512d e_norm_sq = _mm512_fmadd_pd(ex_vec, ex_vec, _mm512_mul_pd(ey_vec, ey_vec));
// # 8 SQRTs
__m512d e_norm = _mm512_sqrt_pd(e_norm_sq);
// # 8 DIVs
__m512d e_norm_inv = _mm512_div_pd(_mm512_set1_pd(1.0), e_norm);
// # 16 MULT
__m512d e_x_i_vec = _mm512_mul_pd(ex_vec, e_norm_inv);
__m512d e_y_i_vec = _mm512_mul_pd(ey_vec, e_norm_inv);
// # 16 MULTs, 8 ADDs
__m512d vel_norm_sq = _mm512_fmadd_pd(curr_vel_x_i_vec, curr_vel_x_i_vec, _mm512_mul_pd(curr_vel_y_i_vec, curr_vel_y_i_vec));
// # 8 MULTs
__m512d sb_sq_i_vec = _mm512_mul_pd(vel_norm_sq, _mm512_set1_pd(delta_t_sq));
// # 8 SQRTs
__m512d vel_norm_dt = _mm512_sqrt_pd(sb_sq_i_vec);
_mm512_storeu_pd(&curr_vel_x[i], curr_vel_x_i_vec);
@@ -506,6 +611,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
_mm512_storeu_pd(&e_y[i], e_y_i_vec);
_mm512_storeu_pd(&destination_y[i], destination_y_i_vec);
_mm512_storeu_pd(&sb_sq[i], sb_sq_i_vec);
// # 16 MULTs
_mm512_storeu_pd(&sb_e_x[i], _mm512_mul_pd(e_x_i_vec, vel_norm_dt));
_mm512_storeu_pd(&sb_e_y[i], _mm512_mul_pd(e_y_i_vec, vel_norm_dt));
}
-492
View File
@@ -1,492 +0,0 @@
// This file is copied from the variant_baseline and then modified
// The goal is that we use a look up table to precompute expensive calculations
// especially to precompute the exp calls
// we call exp(two_b * NEG_TWO_SIGMA_INV)
// which is always negativ
// if a value is smaller then -40 its so small that we can ignore it
// for the other values we will create a lookup table
#include "../headers/board.hpp"
#include "../headers/constants.hpp"
#include "../headers/pedestrian.hpp"
#include "../headers/vector2d.hpp"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <math.h> // keep — forces code uses unqualified sqrt/exp
#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;
// exp lookup table
constexpr int EXP_LUT_SIZE = 69000;
constexpr double EXP_LUT_MIN = -40.0;
constexpr double EXP_LUT_MAX = 0.0;
constexpr double EXP_LUT_INV_STEP = (EXP_LUT_SIZE - 1) / (EXP_LUT_MAX - EXP_LUT_MIN);
static double exp_lut[EXP_LUT_SIZE];
struct ExpLutInit {
ExpLutInit() {
const double step = (EXP_LUT_MAX - EXP_LUT_MIN) / (EXP_LUT_SIZE - 1);
for (int i = 0; i < EXP_LUT_SIZE; i++) {
exp_lut[i] = std::exp(EXP_LUT_MIN + i * step);
}
}
};
static ExpLutInit exp_lut_init_helper;
// i used the following things to get a better understanding of how i can solve that problem
// Tang, P. T. P. (1989). "Table-driven implementation of the exponential function in IEEE floating-point arithmetic." ACM TOMS 15(2), 144-157.
// https://dl.acm.org/doi/10.1145/63522.214389
// Cubic Hermite spline. Wikipedia.
// https://en.wikipedia.org/wiki/Cubic_Hermite_spline
inline double fast_exp(double x) {
if (x <= EXP_LUT_MIN)
return 0.0;
if (x >= EXP_LUT_MAX)
return 1.0;
double pos = (x - EXP_LUT_MIN) * EXP_LUT_INV_STEP;
int idx = static_cast<int>(pos);
if (idx >= EXP_LUT_SIZE - 1)
return 1.0;
double t = pos - idx;
double y0 = exp_lut[idx];
double y1 = exp_lut[idx + 1];
// For exp(x), derivative equals function value.
// In normalized parameter space (t in [0,1] across width h),
// dy/dt = h * y. No centered differences needed - we know the exact slope.
constexpr double h_step = (EXP_LUT_MAX - EXP_LUT_MIN) / (EXP_LUT_SIZE - 1);
double t2 = t * t;
double t3 = t2 * t;
double h00 = 2.0 * t3 - 3.0 * t2 + 1.0;
double h10 = t3 - 2.0 * t2 + t;
double h01 = -2.0 * t3 + 3.0 * t2;
double h11 = t3 - t2;
return y0 * (h00 + h_step * h10) + y1 * (h01 + h_step * h11);
}
// forces.cpp
// Flop count: 2 ADDs, 5 MULTs, 1 SQRT
double w(Vector2d e, Vector2d f) {
if (e.dot(f) >= f.norm() * cos_phi)
return dt_neg;
else
return c_neg_times_dt;
}
// Flop count: 2 ADDs, 4 MULTs
Vector2d acceleration_term_dt(const Pedestrian &alpha) {
// # 2 MULTs
Vector2d desired_velocity = alpha.e * alpha.desired_v0;
Vector2d actual_velocity = alpha.curr_vel;
// # 2 ADDs, 2 MULTs
Vector2d f_alpha_0_dt = (desired_velocity - actual_velocity) * dt_times_tau_alpha_inv;
return f_alpha_0_dt;
}
// Flop count: 12 ADDs, 23 MULTs, 1 DIV, 4 SQRTs, 1 EXP
Vector2d pedestrian_repulsion_dt(const Pedestrian &alpha, const Pedestrian &beta) {
// # 2 ADDs
// $\vec{r}_{\alpha\beta}$
Vector2d r_ab = alpha.curr_pos - beta.curr_pos;
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert$
double r_ab_norm = r_ab.norm();
r_ab_norm = r_ab_norm > EPS ? r_ab_norm : EPS; // Avoid division by zero
// $v_\beta \Delta t \vec{e}_\beta$
Vector2d s_b_e = beta.s_b_e;
// # 2 ADDs
// $\vec{r}_{\alpha\beta} - \vec{v}_{\beta}\varDelta_{t}\vec{e}_{\beta}$
Vector2d r_ab2 = r_ab - s_b_e;
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
double r_ab2_norm = r_ab2.norm();
r_ab2_norm = r_ab2_norm > EPS ? r_ab2_norm : EPS; // Avoid division by zero
// # 1 ADD
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
// # 1 ADD, 1 MULTs, 1 SQRT
//
// $2b = \sqrt{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)^2 - (v_\beta \Delta t)^2}$
//
double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - beta.s_b_sq;
two_b_sq = two_b_sq > EPS_SQ ? two_b_sq : EPS_SQ; // Avoid sqrt of negative and avoid division by zero
double two_b = sqrt(two_b_sq);
// # 5 MULTs, 1 DIV, 1 EXP
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}$
//
double scalar_mult_common = (r_ab_norm_sum / (two_b * r_ab_norm * r_ab2_norm)) * (NEG_V0_AB_div_TWO_SIGMA * fast_exp(two_b * NEG_TWO_SIGMA_INV));
// # 1 MULT
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}$
//
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// # 1 MULT
//
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}$
//
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
// # 2 ADDs, 4 MULTs
//
// $\vec{-f}_{\alpha \beta}(\vec{r}_{\alpha \beta}) = \frac{-V^0_{\alpha \beta}}{2\sigma}e^{-b / \sigma} 2\grad_{\vec{r}_{\alpha \beta}}b $
//
//
// $= \frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}\,\vec{r}_{\alpha \beta} + \frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} -
// v_\beta \Delta t \vec{e}_\beta \Arrowvert}\,(\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta)$
//
//
//
// $2\grad_{\vec{r}_{\alpha \beta}}b = \frac{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)(\frac{\vec{r}_{\alpha \beta}}{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert} + \frac{\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta}{\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert})}{2b}$
//
//
// $= \frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}\,\vec{r}_{\alpha \beta} + \frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}\,(\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta)$
//
//
Vector2d f_alpha_beta_neg = r_ab * scalar_mult_r_ab + r_ab2 * scalar_mult_r_ab2;
// # 2 ADDs, 7 MULTs, 1 SQRT
//
// $\vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha \beta}).dt = w(\vec{e}_{\alpha}, -\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta})).\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta}).dt$
//
Vector2d f_alpha_beta_dt = f_alpha_beta_neg * w(alpha.e, f_alpha_beta_neg);
return f_alpha_beta_dt;
}
// Flop count: 10 ADDs, 18 MULTs, 1 DIVs, 2 SQRTs, 1 EXP
Vector2d obstacle_repulsion_dt(const Pedestrian &alpha, const Obstacle &B) {
// # 7 ADDs, 5 MULTs
// $\vec{r}_{\alpha B}$
Vector2d r_aB = alpha.curr_pos - B.find_nearest_obstacle_point(alpha.curr_pos);
// # 1 ADD, 2 MULTs, 1 SQRT
// $\Arrowvert \vec{r}_{\alpha B} \Arrowvert$
double r_aB_norm = r_aB.norm();
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS; // Avoid division by zero
// # 2 MULTs, 1 DIV, 1 EXP
//
// $\frac{-U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R}$
//
double scalar_mult = (NEG_U0_AB_div_R * fast_exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// # 2 MULTs
//
// $-\vec{F}_{\alpha B}(\vec{r}_{\alpha B}) = \frac{-U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R} \frac{\vec{r}_{\alpha B}}{\Arrowvert \vec{r}_{\alpha B} \Arrowvert}$
//
Vector2d f_alpha_B_neg = r_aB * scalar_mult;
// # 2 ADDs, 7 MULTs, 1 SQRT
//
// $\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha B}).dt = w(\vec{e}_{\alpha}, -\vec{F}_{\alpha B}(\vec{r}_{\alpha B})).\vec{F}_{\alpha B}(\vec{r}_{\alpha B}).dt$
//
Vector2d f_alpha_B_dt = f_alpha_B_neg * w(alpha.e, f_alpha_B_neg);
return f_alpha_B_dt;
}
// social_force_baseline.cpp
Pedestrian *init_pedestrians(int n_p) {
#if RANDOM_SEED
random_device rd; // Platform-provided random seed source.
mt19937 gen(rd()); // Non-reproducible initialization.
#else
mt19937 gen(SEED); // Reproducible initialization.
#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;
// Group of pedestrians walking from left to right.
for (int i = 0; i < n_p_half; i++) {
double x = x_gen_left(gen);
double y = y_gen(gen);
Vector2d start_pos = Vector2d(x, y);
Vector2d destination = Vector2d(BOARD_LENGTH + 30.0, y);
Vector2d init_e = destination - start_pos;
double init_e_norm = init_e.norm();
Vector2d init_e_unit = init_e / init_e_norm;
// Reject near-zero desired speeds to avoid pedestrians that barely move,
// and reject negative speeds because they would reverse the intended
// walking direction.
double desired_v0;
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1);
double v_max = 1.3 * desired_v0;
P[i] = Pedestrian(WHITE, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
}
// Group of pedestrians walking from right to left.
for (int i = n_p_half; i < n_p; i++) {
double x = x_gen_right(gen);
double y = y_gen(gen);
Vector2d start_pos = Vector2d(x, y);
Vector2d destination = Vector2d(-30.0, y);
Vector2d init_e = destination - start_pos;
double init_e_norm = init_e.norm();
Vector2d init_e_unit = init_e / init_e_norm;
// Reject near-zero desired speeds to avoid pedestrians that barely move,
// and reject negative speeds because they would reverse the intended
// walking direction.
double desired_v0;
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1);
double v_max = 1.3 * desired_v0;
P[i] = Pedestrian(BLACK, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
}
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
// Avoid compiler unused parameter warning
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
// Each sample stores: time step, pedestrian id, x, y, e.x, e.y, color.
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);
// Store the initial state before the first simulation step.
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
// FLOPCOUNT (everything inside the t-loop). EXP = one fast_exp() call (LUT + cubic Hermite),
// NOT a real libm exp. AoS baseline, no distance precompute, r_ab recomputed inside the j-loop.
// section costs: overhead 4 ADD 4 MULT (accel 2 ADD 4 MULT + p.w += 2 ADD) |
// ped-ped j 14 ADD 23 MULT 1 DIV 4 SQRT 1 EXP (12/23/1/4/1 + f_alpha += 2 ADD) |
// obstacle 12 ADD 18 MULT 1 DIV 2 SQRT 1 EXP (10/18/1/2/1 + f_alpha += 2 ADD) |
// update 7 ADD 16 MULT 2 DIV 3 SQRT
// # ADDs: n_iter * (
// # n_p * 4 +
// # n_p * (n_p - 1) * 14 +
// # n_p * board.n_obstacles * 12 +
// # n_p * 7
// # )
// # MULTs: n_iter * (
// # n_p * 4 +
// # n_p * (n_p - 1) * 23 +
// # n_p * board.n_obstacles * 18 +
// # n_p * 16
// # )
// # DIVs: n_iter * (
// # n_p * (n_p - 1) +
// # n_p * board.n_obstacles +
// # n_p * 2
// # )
// # SQRTs: n_iter * (
// # n_p * (n_p - 1) * 4 +
// # n_p * board.n_obstacles * 2 +
// # n_p * 3
// # )
// # EXPs (fast_exp, LUT): n_iter * (
// # n_p * (n_p - 1) +
// # n_p * board.n_obstacles
// # )
for (int t = 0; t < n_iter; t++) {
// Pass 1: Compute all forces from the current state.
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// # 2 ADDs, 4 MULTs
//
// $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$
//
Vector2d f_alpha_dt = acceleration_term_dt(p);
// # (N-1) * (12 ADDs, 23 MULTs, 1 DIV, 4 SQRTs, 1 EXP)
// + (N-1) * 2 ADDs
//
// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha},\vec{r}_{\alpha} - \vec{r}_{\beta})$
//
for (int j = 0; j < n_p; j++) {
if (i == j)
continue;
f_alpha_dt += pedestrian_repulsion_dt(p, pedestrians[j]);
}
// # board.n_obstacles * (10 ADDs, 18 MULTs, 1 DIV, 2 SQRTs, 1 EXP)
// + board.n_obstacles * 2 ADDs
//
// $\sum_{B} \vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$
//
for (int k = 0; k < board.n_obstacles; k++) {
f_alpha_dt += obstacle_repulsion_dt(p, board.obstacles[k]);
}
// # 2 ADDs
//
//
// $\frac{d\vec{w}_\alpha}{dt} = \vec{F}_{\alpha}(t)$
//
p.w += f_alpha_dt;
}
// Pass 2: Update pedestrian states after all forces have been computed.
// # (7 ADDs, 16 MULTs, 2 DIVs, 3 SQRTs) * n_p
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
// # 1 ADD, 2 MULTs, 1 SQRT
double w_norm = p.w.norm();
double w_norm2 = w_norm > EPS ? w_norm : EPS; // Avoid division by zero.
// # 1 DIV
double correction_factor = std::min(1.0, p.v_max / w_norm2);
// # 2 MULTs
p.curr_vel = p.w * correction_factor;
// # 2 ADDs, 2 MULTs
//
// $\vec{r}_i^{t+1} = \vec{r}_i^t + \vec{v}_i^t dt$
//
p.curr_pos = p.curr_pos + p.curr_vel * dt;
// When a pedestrian reaches the end of the corridor, teleport them back
// to the opposite entrance while keeping the same y-coordinate. This
// maintains a constant pedestrian flow and makes lane formation easier to
// observe.
if (p.color == WHITE && p.curr_pos.x > (BOARD_LENGTH - 1.5)) {
p.curr_pos.x = 1.0;
p.destination.y = p.curr_pos.y;
p.w = Vector2d(p.desired_v0, 0.0);
p.curr_vel = p.w;
}
if (p.color == BLACK && p.curr_pos.x < 1.5) {
p.curr_pos.x = BOARD_LENGTH - 1.0;
p.destination.y = p.curr_pos.y;
p.w = Vector2d(-p.desired_v0, 0.0);
p.curr_vel = p.w;
}
// Keep pedestrians inside the board boundaries.
p.curr_pos.y = clamp(p.curr_pos.y, WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
// # 2 ADDs
Vector2d e = p.destination - p.curr_pos;
// # 1 ADD, 2 MULTs, 1 DIV, 1 SQRT
double e_norm = e.norm();
double e_norm2 = e_norm > EPS ? e_norm : EPS; // Avoid division by zero.
double e_norm_inv = 1.0 / e_norm2;
// # 2 MULTs
p.e = e * e_norm_inv;
// # 1 ADD, 2 MULTs
double velocity_norm_sq = p.curr_vel.dot(p.curr_vel);
// # 1 SQRT
double velocity_norm = std::sqrt(velocity_norm_sq);
// # 1 MULT
p.s_b_sq = velocity_norm_sq * delta_t_sq;
// # 3 MULTs
p.s_b_e = p.e * (velocity_norm * delta_t);
#if !BENCHMARK
// Store the current pedestrian state for visualization.
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
// Export recorded simulation states to CSV.
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;
}