diff --git a/variants/variant_branchless_flopcount.cpp b/variants/variant_branchless_flopcount.cpp new file mode 100644 index 0000000..d903659 --- /dev/null +++ b/variants/variant_branchless_flopcount.cpp @@ -0,0 +1,426 @@ +// 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_.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 +#include +#include +#include +#include // keep — forces code uses unqualified sqrt/exp +#include +#include +#include + +#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 velocity_gen(v0_mean, v0_std_dev); + uniform_real_distribution y_gen(0.3, BOARD_WIDTH - 0.3); + uniform_real_distribution x_gen_left(2.0, BOARD_LENGTH / 2.0); + uniform_real_distribution 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> samples; + + if (dump_path) { + samples.reserve(static_cast(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; +} \ No newline at end of file diff --git a/variants/variant_branchless_sqrtfree_flopcount.cpp b/variants/variant_branchless_sqrtfree_flopcount.cpp new file mode 100644 index 0000000..ed3ff6b --- /dev/null +++ b/variants/variant_branchless_sqrtfree_flopcount.cpp @@ -0,0 +1,428 @@ +// 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_.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 +#include +#include +#include +#include // keep — forces code uses unqualified sqrt/exp +#include +#include +#include + +#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 velocity_gen(v0_mean, v0_std_dev); + uniform_real_distribution y_gen(0.3, BOARD_WIDTH - 0.3); + uniform_real_distribution x_gen_left(2.0, BOARD_LENGTH / 2.0); + uniform_real_distribution 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> samples; + + if (dump_path) { + samples.reserve(static_cast(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; +} \ No newline at end of file diff --git a/variants/variant_cutoff_callsite_flopcount.cpp b/variants/variant_cutoff_callsite_flopcount.cpp new file mode 100644 index 0000000..969f59c --- /dev/null +++ b/variants/variant_cutoff_callsite_flopcount.cpp @@ -0,0 +1,436 @@ +// 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 +#include +#include +#include +#include // keep — forces code uses unqualified sqrt/exp +#include +#include +#include + +#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 velocity_gen(v0_mean, v0_std_dev); + uniform_real_distribution y_gen(0.3, BOARD_WIDTH - 0.3); + uniform_real_distribution x_gen_left(2.0, BOARD_LENGTH / 2.0); + uniform_real_distribution 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> samples; + + if (dump_path) { + samples.reserve(static_cast(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; +} \ No newline at end of file diff --git a/variants/variant_cutoff_flopcount.cpp b/variants/variant_cutoff_flopcount.cpp new file mode 100644 index 0000000..bffd1a9 --- /dev/null +++ b/variants/variant_cutoff_flopcount.cpp @@ -0,0 +1,434 @@ +// 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 +#include +#include +#include +#include // keep — forces code uses unqualified sqrt/exp +#include +#include +#include + +#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 velocity_gen(v0_mean, v0_std_dev); + uniform_real_distribution y_gen(0.3, BOARD_WIDTH - 0.3); + uniform_real_distribution x_gen_left(2.0, BOARD_LENGTH / 2.0); + uniform_real_distribution 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> samples; + + if (dump_path) { + samples.reserve(static_cast(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; +} \ No newline at end of file