Perf plot

This commit is contained in:
[Quy Anh] «Elliot» Nguyen
2026-06-18 11:59:50 +02:00
committed by Linus Baumberger
parent ca969564a6
commit e7af3103e8
153 changed files with 22042 additions and 24220 deletions
@@ -187,23 +187,43 @@ Pedestrian *init_pedestrians(int n_p) {
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
// simd expf, same as the predecessor. degree-5 taylor on the [-ln2/2, ln2/2] reduced range,
// simd expf, degree-12 taylor on the [-ln2/2, ln2/2] reduced range,
// schraudolph 2^n bit trick on the exponent slot.
// # 128 MULTs, 112 ADDs
static inline __m256 exp256_ps(__m256 x) {
const __m256 log2e = _mm256_set1_ps(1.4426950408889634f);
__m256 nf = _mm256_round_ps(_mm256_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
__m256 nf = _mm256_round_ps(_mm256_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC); // # 8 MULTs
const __m256 ln2 = _mm256_set1_ps(0.6931471805599453f);
__m256 r = _mm256_fnmadd_ps(nf, ln2, x);
__m256 r = _mm256_fnmadd_ps(nf, ln2, x); // # 8 MULTs, 8 ADDs
const __m256 c5 = _mm256_set1_ps(1.0f / 120.0f);
const __m256 c4 = _mm256_set1_ps(1.0f / 24.0f);
const __m256 c3 = _mm256_set1_ps(1.0f / 6.0f);
const __m256 c2 = _mm256_set1_ps(0.5f);
const __m256 c1 = _mm256_set1_ps(1.0f);
const __m256 c0 = _mm256_set1_ps(1.0f);
const __m256 c13 = _mm256_set1_ps(c13_scalar);
const __m256 c12 = _mm256_set1_ps(c12_scalar);
const __m256 c11 = _mm256_set1_ps(c11_scalar);
const __m256 c10 = _mm256_set1_ps(c10_scalar);
const __m256 c9 = _mm256_set1_ps(c9_scalar);
const __m256 c8 = _mm256_set1_ps(c8_scalar);
const __m256 c7 = _mm256_set1_ps(c7_scalar);
const __m256 c6 = _mm256_set1_ps(c6_scalar);
const __m256 c5 = _mm256_set1_ps(c5_scalar);
const __m256 c4 = _mm256_set1_ps(c4_scalar);
const __m256 c3 = _mm256_set1_ps(c3_scalar);
const __m256 c2 = _mm256_set1_ps(c2_scalar);
const __m256 c1 = _mm256_set1_ps(c1_scalar);
const __m256 c0 = _mm256_set1_ps(c0_scalar);
__m256 p = c5;
__m256 p = c13;
// # 13 * (8 MULTs, 8 ADDs)
// # = 104 MULTs, 104 ADDs
p = _mm256_fmadd_ps(p, r, c12);
p = _mm256_fmadd_ps(p, r, c11);
p = _mm256_fmadd_ps(p, r, c10);
p = _mm256_fmadd_ps(p, r, c9);
p = _mm256_fmadd_ps(p, r, c8);
p = _mm256_fmadd_ps(p, r, c7);
p = _mm256_fmadd_ps(p, r, c6);
p = _mm256_fmadd_ps(p, r, c5);
p = _mm256_fmadd_ps(p, r, c4);
p = _mm256_fmadd_ps(p, r, c3);
p = _mm256_fmadd_ps(p, r, c2);
@@ -215,7 +235,7 @@ static inline __m256 exp256_ps(__m256 x) {
__m256i scaled = _mm256_slli_epi32(biased, 23);
__m256 pow2n = _mm256_castsi256_ps(scaled);
return _mm256_mul_ps(p, pow2n);
return _mm256_mul_ps(p, pow2n); // # 8 MULTs
}
int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, const char *dump_path) {
@@ -253,15 +273,21 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
// const __m256 v_np = _mm256_set1_ps((float)n_p);
const __m256 v_exp_min = _mm256_set1_ps(-87.0f);
// # flops per iteration (one pass of the outer time loop):
// # ADDs: n_p * 22 + n_p * n_p_pad * 28 + n_p * board.n_obstacles * 10 + n_p * 7
// # MULTs: n_p * 4 + n_p * n_p_pad * 42 + n_p * board.n_obstacles * 13 + n_p * 16
// # DIVs: n_p * n_p_pad * 3 + n_p * board.n_obstacles + n_p * 2
// # SQRTs: n_p * n_p_pad * 3 + n_p * board.n_obstacles + n_p * 3
// # EXPs: n_p * board.n_obstacles
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
float desired_velocity_x = e_x[i] * desired_v0[i];
float desired_velocity_y = e_y[i] * desired_v0[i];
float desired_velocity_x = e_x[i] * desired_v0[i]; // # 1 MULT
float desired_velocity_y = e_y[i] * desired_v0[i]; // # 1 MULT
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
// ghost-self trick: park pedestrian i at 1e9 for its own j-loop, broadcasting i's
// real position from a saved copy. then the j==i lane reads 1e9 like a padding lane
@@ -291,51 +317,51 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
__m256 sbe_jy = _mm256_loadu_ps(&sb_e_y[j]);
__m256 sb_jsq = _mm256_loadu_ps(&sb_sq[j]);
__m256 r_ab_x = _mm256_sub_ps(pix, pjx);
__m256 r_ab_y = _mm256_sub_ps(piy, pjy);
__m256 r_ab_n2 = _mm256_fmadd_ps(r_ab_x, r_ab_x, _mm256_mul_ps(r_ab_y, r_ab_y));
__m256 r_ab_x = _mm256_sub_ps(pix, pjx); // # 8 ADDs
__m256 r_ab_y = _mm256_sub_ps(piy, pjy); // # 8 ADDs
__m256 r_ab_n2 = _mm256_fmadd_ps(r_ab_x, r_ab_x, _mm256_mul_ps(r_ab_y, r_ab_y)); // # 16 MULTs, 8 ADDs
// clamp before rsqrt: rsqrt(0) = inf, then x*inf = 0*inf = NaN downstream. EPS_SQ
// means the inv_sqrt tops out at 1/EPS and r_ab_n = EPS_SQ/EPS = EPS, matches the predecessor.
r_ab_n2 = _mm256_max_ps(r_ab_n2, v_eps_sq);
__m256 inv_r_ab_n = _mm256_rsqrt_ps(r_ab_n2);
__m256 inv_r_ab_n = _mm256_rsqrt_ps(r_ab_n2); // # 8 SQRTs, 8 DIVs
//__m256 r_ab_n = _mm256_mul_ps(r_ab_n2, inv_r_ab_n);
__m256 r_ab2_x = _mm256_sub_ps(r_ab_x, sbe_jx);
__m256 r_ab2_y = _mm256_sub_ps(r_ab_y, sbe_jy);
__m256 r_ab2_n2 = _mm256_fmadd_ps(r_ab2_x, r_ab2_x, _mm256_mul_ps(r_ab2_y, r_ab2_y));
__m256 r_ab2_x = _mm256_sub_ps(r_ab_x, sbe_jx); // # 8 ADDs
__m256 r_ab2_y = _mm256_sub_ps(r_ab_y, sbe_jy); // # 8 ADDs
__m256 r_ab2_n2 = _mm256_fmadd_ps(r_ab2_x, r_ab2_x, _mm256_mul_ps(r_ab2_y, r_ab2_y)); // # 16 MULTs, 8 ADDs
r_ab2_n2 = _mm256_max_ps(r_ab2_n2, v_eps_sq);
__m256 inv_r_ab2_n = _mm256_rsqrt_ps(r_ab2_n2);
__m256 inv_r_ab2_n = _mm256_rsqrt_ps(r_ab2_n2); // # 8 SQRTs, 8 DIVs
//__m256 r_ab2_n = _mm256_mul_ps(r_ab2_n2, inv_r_ab2_n);
// r_sum = |r_ab| + |r_ab2|, fused. sqrt(x) = x * rsqrt(x)
// the two norms are used only here (later code uses inv_r_ab_n / inv_r_ab2_n), so fold the two
// recovery muls + add into one mul + one fmadd. -1 FP op, -2 live regs.
__m256 r_sum = _mm256_fmadd_ps(r_ab2_n2, inv_r_ab2_n, _mm256_mul_ps(r_ab_n2, inv_r_ab_n));
__m256 two_b_sq = _mm256_fmsub_ps(r_sum, r_sum, sb_jsq);
__m256 r_sum = _mm256_fmadd_ps(r_ab2_n2, inv_r_ab2_n, _mm256_mul_ps(r_ab_n2, inv_r_ab_n)); // # 16 MULTs, 8 ADDs
__m256 two_b_sq = _mm256_fmsub_ps(r_sum, r_sum, sb_jsq); // # 8 MULTs, 8 ADDs
two_b_sq = _mm256_max_ps(two_b_sq, v_eps_sq);
__m256 inv_two_b = _mm256_rsqrt_ps(two_b_sq);
__m256 two_b = _mm256_mul_ps(two_b_sq, inv_two_b);
__m256 inv_two_b = _mm256_rsqrt_ps(two_b_sq); // # 8 SQRTs, 8 DIVs
__m256 two_b = _mm256_mul_ps(two_b_sq, inv_two_b); // # 8 MULTs
__m256 e_arg = _mm256_mul_ps(two_b, v_neg_2s_inv);
__m256 e_arg = _mm256_mul_ps(two_b, v_neg_2s_inv); // # 8 MULTs
e_arg = _mm256_max_ps(e_arg, v_exp_min);
__m256 e_val = exp256_ps(e_arg);
__m256 e_val = exp256_ps(e_arg); // # 128 MULTs, 112 ADDs
// smc = numer/denom in the predecessor. with inv-sqrts in hand the divide is free:
// sm_r_ab = smc * r_ab2_n = numer / (two_b * r_ab_n) = numer * inv_two_b * inv_r_ab_n
// sm_r_ab2 = smc * r_ab_n = numer / (two_b * r_ab2_n) = numer * inv_two_b * inv_r_ab2_n
// for the i==j and padding lanes inv_two_b*inv_r_ab_n is huge, but the active-mask AND on
// f_x/f_y below zeros the lane out before accumulation so it cant pollute anything.
__m256 numer_inv_two_b = _mm256_mul_ps(_mm256_mul_ps(r_sum, v_neg_v0_div_2s), _mm256_mul_ps(e_val, inv_two_b));
__m256 sm_r_ab = _mm256_mul_ps(numer_inv_two_b, inv_r_ab_n);
__m256 sm_r_ab2 = _mm256_mul_ps(numer_inv_two_b, inv_r_ab2_n);
__m256 numer_inv_two_b = _mm256_mul_ps(_mm256_mul_ps(r_sum, v_neg_v0_div_2s), _mm256_mul_ps(e_val, inv_two_b)); // # 24 MULTs
__m256 sm_r_ab = _mm256_mul_ps(numer_inv_two_b, inv_r_ab_n); // # 8 MULTs
__m256 sm_r_ab2 = _mm256_mul_ps(numer_inv_two_b, inv_r_ab2_n); // # 8 MULTs
__m256 f_x = _mm256_fmadd_ps(r_ab2_x, sm_r_ab2, _mm256_mul_ps(r_ab_x, sm_r_ab));
__m256 f_y = _mm256_fmadd_ps(r_ab2_y, sm_r_ab2, _mm256_mul_ps(r_ab_y, sm_r_ab));
__m256 f_x = _mm256_fmadd_ps(r_ab2_x, sm_r_ab2, _mm256_mul_ps(r_ab_x, sm_r_ab)); // # 16 MULTs, 8 ADDs
__m256 f_y = _mm256_fmadd_ps(r_ab2_y, sm_r_ab2, _mm256_mul_ps(r_ab_y, sm_r_ab)); // # 16 MULTs, 8 ADDs
__m256 f_norm_sq = _mm256_fmadd_ps(f_x, f_x, _mm256_mul_ps(f_y, f_y));
__m256 rhs = _mm256_mul_ps(f_norm_sq, v_cos_phi_sq);
__m256 e_dot_f = _mm256_fmadd_ps(eix, f_x, _mm256_mul_ps(eiy, f_y));
__m256 e_dot_f_sq = _mm256_mul_ps(e_dot_f, e_dot_f);
__m256 f_norm_sq = _mm256_fmadd_ps(f_x, f_x, _mm256_mul_ps(f_y, f_y)); // # 16 MULTs, 8 ADDs
__m256 rhs = _mm256_mul_ps(f_norm_sq, v_cos_phi_sq); // # 8 MULTs
__m256 e_dot_f = _mm256_fmadd_ps(eix, f_x, _mm256_mul_ps(eiy, f_y)); // # 16 MULTs, 8 ADDs
__m256 e_dot_f_sq = _mm256_mul_ps(e_dot_f, e_dot_f); // # 8 MULTs
__m256 c1 = _mm256_cmp_ps(e_dot_f, v_zero, _CMP_LT_OQ);
__m256 c2 = _mm256_cmp_ps(e_dot_f_sq, rhs, _CMP_GT_OQ);
@@ -344,47 +370,47 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
//__m256 f_x_safe = _mm256_and_ps(f_x, active);
//__m256 f_y_safe = _mm256_and_ps(f_y, active);
acc_x = _mm256_fmadd_ps(f_x, w_val, acc_x);
acc_y = _mm256_fmadd_ps(f_y, w_val, acc_y);
acc_x = _mm256_fmadd_ps(f_x, w_val, acc_x); // # 8 MULTs, 8 ADDs
acc_y = _mm256_fmadd_ps(f_y, w_val, acc_y); // # 8 MULTs, 8 ADDs
}
// restore the values
pos_x[i] = saved_pix;
pos_y[i] = saved_piy;
__m256 hsum = _mm256_hadd_ps(acc_x, acc_y);
__m256 hsum = _mm256_hadd_ps(acc_x, acc_y); // # 8 ADDs (x and y interleaved)
__m128 hsum_lo = _mm256_castps256_ps128(hsum);
__m128 hsum_hi = _mm256_extractf128_ps(hsum, 1);
__m128 sum4 = _mm_add_ps(hsum_lo, hsum_hi);
sum4 = _mm_hadd_ps(sum4, sum4);
__m128 sum4 = _mm_add_ps(hsum_lo, hsum_hi); // # 4 ADDs
sum4 = _mm_hadd_ps(sum4, sum4); // # 4 ADDs (total hadd: 16 ADDs for x+y)
alignas(16) float sum_out[4];
_mm_store_ps(sum_out, sum4);
f_alpha_dt_x += sum_out[0];
f_alpha_dt_y += sum_out[1];
f_alpha_dt_x += sum_out[0]; // # 1 ADD
f_alpha_dt_y += sum_out[1]; // # 1 ADD
for (int k = 0; k < board.n_obstacles; k++) {
float v1_x = pos_x[i] - board.obstacles[k].start_x;
float v1_y = pos_y[i] - board.obstacles[k].start_y;
float v1_x = pos_x[i] - board.obstacles[k].start_x; // # 1 ADD
float v1_y = pos_y[i] - board.obstacles[k].start_y; // # 1 ADD
float v2_x = board.obstacles[k].obstacle_v_x;
float v2_y = board.obstacles[k].obstacle_v_y;
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv; // # 3 MULTs, 1 ADD
if (projection < 0.0)
projection = 0.0;
else if (projection > 1.0)
projection = 1.0;
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
float r_aB_x = pos_x[i] - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection); // # 1 MULT, 2 ADDs
float r_aB_y = pos_y[i] - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection); // # 1 MULT, 2 ADDs
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y); // # 2 MULTs, 1 ADD, 1 SQRT
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS;
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
float f_alpha_B_neg_x = r_aB_x * scalar_mult;
float f_alpha_B_neg_y = r_aB_y * scalar_mult;
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm; // # 2 MULTs, 1 EXP, 1 DIV
float f_alpha_B_neg_x = r_aB_x * scalar_mult; // # 1 MULT
float f_alpha_B_neg_y = r_aB_y * scalar_mult; // # 1 MULT
float w_val;
@@ -395,28 +421,28 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
else
w_val = c_neg_times_dt;
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val;
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val;
float f_alpha_B_dt_x = f_alpha_B_neg_x * w_val; // # 1 MULT
float f_alpha_B_dt_y = f_alpha_B_neg_y * w_val; // # 1 MULT
f_alpha_dt_x += f_alpha_B_dt_x;
f_alpha_dt_y += f_alpha_B_dt_y;
f_alpha_dt_x += f_alpha_B_dt_x; // # 1 ADD
f_alpha_dt_y += f_alpha_B_dt_y; // # 1 ADD
}
w_x[i] += f_alpha_dt_x;
w_y[i] += f_alpha_dt_y;
w_x[i] += f_alpha_dt_x; // # 1 ADD
w_y[i] += f_alpha_dt_y; // # 1 ADD
}
for (int i = 0; i < n_p; i++) {
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]);
float w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]); // # 2 MULTs, 1 ADD, 1 SQRT
float w_norm2 = w_norm > EPS ? w_norm : EPS;
float correction_factor = min(1.0f, v_max[i] / w_norm2);
float correction_factor = min(1.0f, v_max[i] / w_norm2); // # 1 DIV
curr_vel_x[i] = w_x[i] * correction_factor;
curr_vel_y[i] = w_y[i] * correction_factor;
curr_vel_x[i] = w_x[i] * correction_factor; // # 1 MULT
curr_vel_y[i] = w_y[i] * correction_factor; // # 1 MULT
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt;
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt;
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt; // # 1 MULT, 1 ADD
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt; // # 1 MULT, 1 ADD
if (color[i] == 0 && pos_x[i] > (BOARD_LENGTH - 1.5)) {
pos_x[i] = 1.0;
@@ -437,22 +463,22 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
pos_y[i] = clamp(pos_y[i], WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
float ex = destination_x[i] - pos_x[i];
float ey = destination_y[i] - pos_y[i];
float ex = destination_x[i] - pos_x[i]; // # 1 ADD
float ey = destination_y[i] - pos_y[i]; // # 1 ADD
float e_norm = sqrt(ex * ex + ey * ey);
float e_norm = sqrt(ex * ex + ey * ey); // # 2 MULTs, 1 ADD, 1 SQRT
float e_norm2 = e_norm > EPS ? e_norm : EPS;
float e_norm_inv = 1.0 / e_norm2;
float e_norm_inv = 1.0 / e_norm2; // # 1 DIV
e_x[i] = ex * e_norm_inv;
e_y[i] = ey * e_norm_inv;
e_x[i] = ex * e_norm_inv; // # 1 MULT
e_y[i] = ey * e_norm_inv; // # 1 MULT
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i];
float velocity_norm = sqrt(velocity_norm_sq);
float velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i]; // # 2 MULTs, 1 ADD
float velocity_norm = sqrt(velocity_norm_sq); // # 1 SQRT
sb_sq[i] = velocity_norm_sq * delta_t_sq;
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t);
sb_e_y[i] = e_y[i] * (velocity_norm * delta_t);
sb_sq[i] = velocity_norm_sq * delta_t_sq; // # 1 MULT
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t); // # 2 MULTs (vel_norm*delta_t shared with next line)
sb_e_y[i] = e_y[i] * (velocity_norm * delta_t); // # 1 MULT
#if !BENCHMARK
if (dump_path && (t % dump_interval == 0)) {