flopcount soa_symmetry

This commit is contained in:
lbaumberger
2026-06-14 15:21:19 +02:00
parent a87863096f
commit 1ebbf35296
@@ -0,0 +1,489 @@
// # Changes: SoA layout for pedestrians (file-scope arrays instead of struct fields)
// # added flopcount
// FLOPCOUNT = n_iter * ( 69 * n_p ** 2 - 32 * n_p + 55 * n_obstacles * n_p )
// # Predecessor: variant_float_inlining.cpp
#include "../headers/board_reduced_float.hpp"
#include "../headers/constants_float.hpp"
#include "../headers/pedestrian_reduced_float.hpp"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <math.h>
#include <random>
#include <tuple>
#include <vector>
#define SEED 42
#define BOARD_WIDTH 10.0f
#define BOARD_LENGTH 50.0f
#define WALL_EPS 1e-3f
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
#ifndef RANDOM_SEED
#define RANDOM_SEED 0
#endif
#ifndef BENCHMARK
#define BENCHMARK 0
#endif
using namespace std;
// we wanna use arrays so we can implement SoA, one array per pedestrian field
vector<float> pos_x, pos_y;
vector<float> sb_sq;
vector<float> sb_e_x, sb_e_y;
vector<float> e_x, e_y;
vector<float> curr_vel_x, curr_vel_y;
vector<float> w_x, w_y;
vector<float> desired_v0;
vector<float> v_max;
vector<float> destination_x, destination_y;
vector<int> color;
Pedestrian *init_pedestrians(int n_p) {
#if RANDOM_SEED
random_device rd;
mt19937 gen(rd());
#else
mt19937 gen(SEED);
#endif
normal_distribution<float> velocity_gen(v0_mean, v0_std_dev);
uniform_real_distribution<float> y_gen(0.3, BOARD_WIDTH - 0.3);
uniform_real_distribution<float> x_gen_left(2.0, BOARD_LENGTH / 2.0);
uniform_real_distribution<float> x_gen_right(BOARD_LENGTH / 2.0, BOARD_LENGTH - 2.0);
// resize all arrays for this run
pos_x.resize(n_p);
pos_y.resize(n_p);
sb_sq.resize(n_p);
sb_e_x.resize(n_p);
sb_e_y.resize(n_p);
e_x.resize(n_p);
e_y.resize(n_p);
curr_vel_x.resize(n_p);
curr_vel_y.resize(n_p);
w_x.resize(n_p);
w_y.resize(n_p);
desired_v0.resize(n_p);
v_max.resize(n_p);
destination_x.resize(n_p);
destination_y.resize(n_p);
color.resize(n_p);
int n_p_half = n_p / 2;
for (int i = 0; i < n_p_half; i++) {
float x = x_gen_left(gen);
float y = y_gen(gen);
float dest_x = BOARD_LENGTH + 30.0; // renamed to avoid shadowing destination_x[]
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1);
float v_m = 1.3 * d_v0;
color[i] = 0;
pos_x[i] = x;
pos_y[i] = y;
e_x[i] = init_e_unit_x;
e_y[i] = init_e_unit_y;
v_max[i] = v_m;
desired_v0[i] = d_v0;
destination_x[i] = dest_x;
destination_y[i] = dest_y;
// Fields the Pedestrian constructor would have zero-initialized (c_v_x=c_v_y=0):
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
for (int i = n_p_half; i < n_p; i++) {
float x = x_gen_right(gen);
float y = y_gen(gen);
float dest_x = -30;
float dest_y = y;
float init_e_x = dest_x - x;
float init_e_y = dest_y - y;
float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y);
float init_e_unit_x = init_e_x / init_e_norm;
float init_e_unit_y = init_e_y / init_e_norm;
float d_v0;
do {
d_v0 = velocity_gen(gen);
} while (d_v0 <= 0.1);
float v_m = 1.3 * d_v0;
color[i] = 1;
pos_x[i] = x;
pos_y[i] = y;
e_x[i] = init_e_unit_x;
e_y[i] = init_e_unit_y;
v_max[i] = v_m;
desired_v0[i] = d_v0;
destination_x[i] = dest_x;
destination_y[i] = dest_y;
w_x[i] = 0.0f;
w_y[i] = 0.0f;
curr_vel_x[i] = 0.0f;
curr_vel_y[i] = 0.0f;
sb_sq[i] = 0.0f;
sb_e_x[i] = 0.0f;
sb_e_y[i] = 0.0f;
}
return nullptr;
}
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, const char *dump_path) {
(void)pedestrians;
#if BENCHMARK
(void)dump_path;
#endif
#if !BENCHMARK
const int dump_interval = 1;
vector<tuple<int, int, float, float, float, float, int>> samples;
if (dump_path) {
samples.reserve(static_cast<size_t>(n_iter / dump_interval + 1) * n_p);
for (int i = 0; i < n_p; i++) {
samples.emplace_back(0, i, pos_x[i], pos_y[i], e_x[i], e_y[i], color[i]);
}
}
#endif
// here we have real (simple) symmetry
std::vector<float> r_ab_x_lut(n_p * n_p, 0.0);
std::vector<float> r_ab_y_lut(n_p * n_p, 0.0);
std::vector<float> r_ab_norm_lut(n_p * n_p, 0.0);
// use NAME[i * n_p + j]
// FLOPCOUNT (everything inside the t-loop). EXP kept as its own op (one real exp() per call).
// section costs: overhead 4 ADD 4 MULT | distance pair 3 ADD 2 MULT 1 SQRT |
// ped-ped j 11 ADD 22 MULT 2 SQRT 1 DIV 1 EXP |
// obstacle 10 ADD 13 MULT 1 SQRT 1 DIV 1 EXP | update 7 ADD 17 MULT 3 SQRT 2 DIV
// # ADDs: n_iter * (
// # n_p * 4 +
// # 0.5 * (n_p*n_p - n_p) * 3 +
// # n_p * (n_p - 1) * 11 +
// # n_p * board.n_obstacles * 10 +
// # n_p * 7
// # )
// # MULTs: n_iter * (
// # n_p * 4 +
// # 0.5 * (n_p*n_p - n_p) * 2 +
// # n_p * (n_p - 1) * 22 +
// # n_p * board.n_obstacles * 13 +
// # n_p * 17
// # )
// # DIVs: n_iter * (
// # n_p * (n_p - 1) +
// # n_p * board.n_obstacles +
// # n_p * 2
// # )
// # SQRTs: n_iter * (
// # 0.5 * (n_p*n_p - n_p) +
// # n_p * (n_p - 1) * 2 +
// # n_p * board.n_obstacles +
// # n_p * 3
// # )
// # EXPs: n_iter * (
// # n_p * (n_p - 1) +
// # n_p * board.n_obstacles
// # )
// total with EXP = 30: n_iter * ( 69 * n_p ** 2 - 32 * n_p + 55 * n_obstacles * n_p )
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
// # 1 MULT
float desired_velocity_x = e_x[i] * desired_v0[i];
// # 1 MULT
float desired_velocity_y = e_y[i] * desired_v0[i];
// # 1 ADD, 1 MULT
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
// # 1 ADD, 1 MULT
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
// lut for distances
// get all distances first
// # each pair: 6 FLOPS, total over all i = 0.5*(n_p*n_p - n_p) * 6
for (int j = i + 1; j < n_p; j++) {
// # 1 ADD
float dx = pos_x[i] - pos_x[j];
// # 1 ADD
float dy = pos_y[i] - pos_y[j];
// # 1 ADD, 2 MULTs, 1 SQRT
float norm = sqrt(dx * dx + dy * dy);
int idx = i * n_p + j;
r_ab_x_lut[idx] = dx;
r_ab_y_lut[idx] = dy;
r_ab_norm_lut[idx] = norm;
}
// force between pedestrians
// # one j (the i==j case is skipped) = 66 FLOPS, runs (n_p-1) times per i
for (int j = 0; j < n_p; j++) {
if (i == j) continue;
// symmetry: lut lookup is for free, the -1* sign flip is not counted
float r_ab_x, r_ab_y, r_ab_norm;
if (i < j) {
r_ab_x = r_ab_x_lut[i * n_p + j];
r_ab_y = r_ab_y_lut[i * n_p + j];
r_ab_norm = r_ab_norm_lut[i * n_p + j];
} else {
// other direction
r_ab_x = -1 * r_ab_x_lut[j * n_p + i];
r_ab_y = -1 * r_ab_y_lut[j * n_p + i];
r_ab_norm = r_ab_norm_lut[j * n_p + i];
}
//float r_ab_x = pos_x[i] - pos_x[j];
//float r_ab_y = pos_y[i] - pos_y[j];
//float r_ab_norm = sqrt(r_ab_x * r_ab_x + r_ab_y * r_ab_y);
r_ab_norm = r_ab_norm > EPS ? r_ab_norm : EPS;
// # 1 ADD
float r_ab2_x = r_ab_x - sb_e_x[j];
// # 1 ADD
float r_ab2_y = r_ab_y - sb_e_y[j];
// # 1 ADD, 2 MULTs, 1 SQRT
float r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
r_ab2_norm = r_ab2_norm > EPS ? r_ab2_norm : EPS;
// # 1 ADD
float r_ab_norm_sum = r_ab_norm + r_ab2_norm;
// # 1 ADD, 1 MULT
float two_b_sq = r_ab_norm_sum * r_ab_norm_sum - sb_sq[j];
two_b_sq = two_b_sq > EPS_SQ ? two_b_sq : EPS_SQ;
// # 1 SQRT
float two_b = sqrt(two_b_sq);
// # 5 MULTs, 1 DIV, 1 EXP
float scalar_mult_common = (r_ab_norm_sum / (two_b * r_ab_norm * r_ab2_norm)) * (NEG_V0_AB_div_TWO_SIGMA * exp(two_b * NEG_TWO_SIGMA_INV));
// # 1 MULT
float scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
// # 1 MULT
float scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
// # 1 ADD, 2 MULTs
float f_alpha_beta_neg_x = r_ab_x * scalar_mult_r_ab + r_ab2_x * scalar_mult_r_ab2;
// # 1 ADD, 2 MULTs
float f_alpha_beta_neg_y = r_ab_y * scalar_mult_r_ab + r_ab2_y * scalar_mult_r_ab2;
float w_val;
// # 1 ADD, 2 MULTs
float f_norm_sq = f_alpha_beta_neg_x * f_alpha_beta_neg_x + f_alpha_beta_neg_y * f_alpha_beta_neg_y;
// # 1 MULT
float rhs = f_norm_sq * cos_phi_sq;
// # 1 ADD, 2 MULTs
float e_dot_f = e_x[i] * f_alpha_beta_neg_x + e_y[i] * f_alpha_beta_neg_y;
// # 1 MULT
float e_dot_f_sq = e_dot_f * e_dot_f;
if (e_dot_f >= 0.0 || e_dot_f_sq <= rhs)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
// # 1 ADD, 1 MULT
f_alpha_dt_x = f_alpha_dt_x + f_alpha_beta_neg_x * w_val;
// # 1 ADD, 1 MULT
f_alpha_dt_y = f_alpha_dt_y + f_alpha_beta_neg_y * w_val;
}
// force between pedestrian and obstacles
// # one obstacle = 55 FLOPS (w_val here is sign-based, no a*a / b_sq)
for (int k = 0; k < board.n_obstacles; k++) {
// # 1 ADD
float v1_x = pos_x[i] - board.obstacles[k].start_x;
// # 1 ADD
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
// # 1 ADD, 3 MULTs
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
if (projection < 0.0)
projection = 0.0;
else if (projection > 1.0)
projection = 1.0;
// # 2 ADDs, 1 MULT
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
// # 2 ADDs, 1 MULT
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
// # 1 ADD, 2 MULTs, 1 SQRT
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
// # 2 MULTs, 1 DIV, 1 EXP
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
// # 1 MULT
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
// # 1 MULT
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
float w_val;
// sign select only, no flops
float e_dot_f = (f_alpha_B_neg_y == 0) ? 0 : (f_alpha_B_neg_y < 0 ? -e_y[i] : e_y[i]);
if (e_dot_f >= cos_phi)
w_val = dt_neg;
else
w_val = c_neg_times_dt;
// # 1 MULT
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
// # 1 MULT
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
// # 1 ADD
f_alpha_dt_x += f_alpha_B_dt_x;
// # 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y;
}
// # 1 ADD
w_x[i] += f_alpha_dt_x;
// # 1 ADD
w_y[i] += f_alpha_dt_y;
}
// update each pedestrian
// # 29 FLOPS per pedestrian
for (int i = 0; i < n_p; i++) {
// # 1 ADD, 2 MULTs, 1 SQRT
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]);
float w_norm2 = w_norm > EPS ? w_norm : EPS;
// # 1 DIV
float correction_factor = min(1.0f, v_max[i] / w_norm2);
// # 1 MULT
curr_vel_x[i] = w_x[i] * correction_factor;
// # 1 MULT
curr_vel_y[i] = w_y[i] * correction_factor;
// # 1 ADD, 1 MULT
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt;
// # 1 ADD, 1 MULT
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt;
if (color[i] == 0 && pos_x[i] > (BOARD_LENGTH - 1.5)) {
pos_x[i] = 1.0;
destination_y[i] = pos_y[i];
w_x[i] = desired_v0[i];
w_y[i] = 0.0;
curr_vel_x[i] = w_x[i];
curr_vel_y[i] = w_y[i];
}
if (color[i] == 1 && pos_x[i] < 1.5) {
pos_x[i] = BOARD_LENGTH - 1.0;
destination_y[i] = pos_y[i];
w_x[i] = -desired_v0[i];
w_y[i] = 0.0;
curr_vel_x[i] = w_x[i];
curr_vel_y[i] = w_y[i];
}
pos_y[i] = clamp(pos_y[i], WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
// # 1 ADD
float ex = destination_x[i] - pos_x[i];
// # 1 ADD
float ey = destination_y[i] - pos_y[i];
// # 1 ADD, 2 MULTs, 1 SQRT
float e_norm = sqrt(ex * ex + ey * ey);
float e_norm2 = e_norm > EPS ? e_norm : EPS;
// # 1 DIV
float e_norm_inv = 1.0 / e_norm2;
// # 1 MULT
e_x[i] = ex * e_norm_inv;
// # 1 MULT
e_y[i] = ey * e_norm_inv;
// # 1 ADD, 2 MULTs
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i];
// # 1 SQRT
float velocity_norm = sqrt(velocity_norm_sq);
// # 1 MULT
sb_sq[i] = velocity_norm_sq * delta_t_sq;
// # 2 MULTs (velocity_norm*delta_t written on both lines, counted as is)
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t);
// # 2 MULTs
sb_e_y[i] = e_y[i] * (velocity_norm * delta_t);
#if !BENCHMARK
if (dump_path && (t % dump_interval == 0)) {
samples.emplace_back(t + 1, i, pos_x[i], pos_y[i], e_x[i], e_y[i], color[i]);
}
#endif
}
}
#if !BENCHMARK
if (dump_path) {
ofstream out(dump_path);
out << "time,id,x,y,p.e.x,p.e.y,color\n";
for (auto &[t, id, x, y, e_x, e_y, c] : samples) {
out << t << ',' << id << ',' << x << ',' << y << ',' << e_x << ',' << e_y << ',' << c << '\n';
}
}
#endif
return 0;
}