From 26e4e5270561cfea4fdd9b5741474348c24e4a34 Mon Sep 17 00:00:00 2001 From: salijagic Date: Sun, 14 Jun 2026 16:17:27 +0200 Subject: [PATCH] flopcounts, more is coming --- ...ariant_inlining_7_symmetry_1_flopcount.cpp | 423 ++++++++++ ...ariant_inlining_7_symmetry_2_flopcount.cpp | 385 +++++++++ ..._inlining_7_symmetry_2_ilp_1_flopcount.cpp | 633 +++++++++++++++ ..._inlining_7_symmetry_2_ilp_2_flopcount.cpp | 753 ++++++++++++++++++ 4 files changed, 2194 insertions(+) create mode 100644 variants/variant_inlining_7_symmetry_1_flopcount.cpp create mode 100644 variants/variant_inlining_7_symmetry_2_flopcount.cpp create mode 100644 variants/variant_inlining_7_symmetry_2_ilp_1_flopcount.cpp create mode 100644 variants/variant_inlining_7_symmetry_2_ilp_2_flopcount.cpp diff --git a/variants/variant_inlining_7_symmetry_1_flopcount.cpp b/variants/variant_inlining_7_symmetry_1_flopcount.cpp new file mode 100644 index 0000000..b0888a8 --- /dev/null +++ b/variants/variant_inlining_7_symmetry_1_flopcount.cpp @@ -0,0 +1,423 @@ +// # 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 +#include +#include +#include +#include +#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; + +Pedestrian *init_pedestrians(int n_p) { +#if RANDOM_SEED + random_device rd; + mt19937 gen(rd()); +#else + mt19937 gen(SEED); +#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; + + 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> samples; + + if (dump_path) { + samples.reserve(static_cast(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 r_ab_x_lut(n_p * n_p, 0.0); + std::vector r_ab_y_lut(n_p * n_p, 0.0); + std::vector 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; +} diff --git a/variants/variant_inlining_7_symmetry_2_flopcount.cpp b/variants/variant_inlining_7_symmetry_2_flopcount.cpp new file mode 100644 index 0000000..a6740c9 --- /dev/null +++ b/variants/variant_inlining_7_symmetry_2_flopcount.cpp @@ -0,0 +1,385 @@ +// # 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 +#include +#include +#include +#include +#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; + +Pedestrian *init_pedestrians(int n_p) { +#if RANDOM_SEED + random_device rd; + mt19937 gen(rd()); +#else + mt19937 gen(SEED); +#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; + + 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> samples; + + if (dump_path) { + samples.reserve(static_cast(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 r_ab_x_lut(n_p * n_p, 0.0); + std::vector r_ab_y_lut(n_p * n_p, 0.0); + std::vector 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; +} diff --git a/variants/variant_inlining_7_symmetry_2_ilp_1_flopcount.cpp b/variants/variant_inlining_7_symmetry_2_ilp_1_flopcount.cpp new file mode 100644 index 0000000..2e85f45 --- /dev/null +++ b/variants/variant_inlining_7_symmetry_2_ilp_1_flopcount.cpp @@ -0,0 +1,633 @@ +// # 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 +#include +#include +#include +#include +#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; + +Pedestrian *init_pedestrians(int n_p) { +#if RANDOM_SEED + random_device rd; + mt19937 gen(rd()); +#else + mt19937 gen(SEED); +#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; + + 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> samples; + + if (dump_path) { + samples.reserve(static_cast(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 r_ab_x_lut(n_p * n_p, 0.0); + std::vector r_ab_y_lut(n_p * n_p, 0.0); + std::vector 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; +} diff --git a/variants/variant_inlining_7_symmetry_2_ilp_2_flopcount.cpp b/variants/variant_inlining_7_symmetry_2_ilp_2_flopcount.cpp new file mode 100644 index 0000000..372c69c --- /dev/null +++ b/variants/variant_inlining_7_symmetry_2_ilp_2_flopcount.cpp @@ -0,0 +1,753 @@ +// # 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 +#include +#include +#include +#include +#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; + +Pedestrian *init_pedestrians(int n_p) { +#if RANDOM_SEED + random_device rd; + mt19937 gen(rd()); +#else + mt19937 gen(SEED); +#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; + + 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> samples; + + if (dump_path) { + samples.reserve(static_cast(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 r_ab_x_lut(n_p * n_p, 0.0); + std::vector r_ab_y_lut(n_p * n_p, 0.0); + std::vector 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; +}