From d8d075f1d7da9a150f7c685468c842254c9f4306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=5BQuy=20Anh=5D=20=C2=ABElliot=C2=BB=20Nguyen?= <23906-nguyenquy@users.noreply.gitlab.inf.ethz.ch> Date: Tue, 9 Jun 2026 00:01:08 +0200 Subject: [PATCH] Flop Count Correction --- headers/constants_float.hpp | 14 + ...p_moreavx_ilp_2_rsqrt_morersqrt_expfix.cpp | 632 ++++++++++++++++++ ...N_polyexp_moreavx_ilp_2_rsqrt_morrsqrt.cpp | 168 ++--- 3 files changed, 720 insertions(+), 94 deletions(-) create mode 100644 variants/variant_float_inlining_SoA_AVX512_polyexp_moreavx_ilp_2_rsqrt_morersqrt_expfix.cpp diff --git a/headers/constants_float.hpp b/headers/constants_float.hpp index 2db5ff6..4f42c62 100644 --- a/headers/constants_float.hpp +++ b/headers/constants_float.hpp @@ -103,3 +103,17 @@ const float CUTOFF_SQ = CUTOFF * CUTOFF; const float cos_phi_sq = cos_phi * cos_phi; const size_t CACHE_BLOCK_SIZE = std::hardware_destructive_interference_size; + +const double c0_scalar = 1.0 / 1; +const double c1_scalar = 1.0 / 1; +const double c2_scalar = 1.0 / 2; +const double c3_scalar = 1.0 / 6; +const double c4_scalar = 1.0 / 24; +const double c5_scalar = 1.0 / 120; +const double c6_scalar = 1.0 / 720; +const double c7_scalar = 1.0 / 5040; +const double c8_scalar = 1.0 / 40320; +const double c9_scalar = 1.0 / 362880; +const double c10_scalar = 1.0 / 3628800; +const double c11_scalar = 1.0 / 39916800; +const double c12_scalar = 1.0 / 479001600; diff --git a/variants/variant_float_inlining_SoA_AVX512_polyexp_moreavx_ilp_2_rsqrt_morersqrt_expfix.cpp b/variants/variant_float_inlining_SoA_AVX512_polyexp_moreavx_ilp_2_rsqrt_morersqrt_expfix.cpp new file mode 100644 index 0000000..d682289 --- /dev/null +++ b/variants/variant_float_inlining_SoA_AVX512_polyexp_moreavx_ilp_2_rsqrt_morersqrt_expfix.cpp @@ -0,0 +1,632 @@ +// # Changes: Replaced sqrt_ps with rsqrt14_ps in the pedestrian loop, similar to @lbaumberger in variant_float_inlining_SoA_AVX2_polyexp_rsqrt.cpp +// # Predecessor: variant_float_inlining_SoA_AVX512_polyexp_moreavx_ilp_2_rsqrt.cpp + +#include "../headers/board_reduced_float.hpp" +#include "../headers/constants_float.hpp" +#include "../headers/pedestrian_reduced_float.hpp" + +#include + +#include +#include + +#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; +alignas(64) vector pos_x, pos_y; +alignas(64) vector sb_sq; +alignas(64) vector sb_e_x, sb_e_y; +alignas(64) vector e_x, e_y; +alignas(64) vector curr_vel_x, curr_vel_y; +alignas(64) vector w_x, w_y; +alignas(64) vector desired_v0; +alignas(64) vector v_max; +alignas(64) vector destination_x, destination_y; +alignas(64) vector color; + +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); + + const int n_p_pad = (n_p + 31) & ~31; + + pos_x.resize(n_p_pad); + pos_y.resize(n_p_pad); + sb_sq.resize(n_p_pad); + sb_e_x.resize(n_p_pad); + sb_e_y.resize(n_p_pad); + e_x.resize(n_p_pad); + e_y.resize(n_p_pad); + curr_vel_x.resize(n_p_pad); + curr_vel_y.resize(n_p_pad); + w_x.resize(n_p_pad); + w_y.resize(n_p_pad); + desired_v0.resize(n_p_pad); + v_max.resize(n_p_pad); + destination_x.resize(n_p_pad); + destination_y.resize(n_p_pad); + color.resize(n_p_pad); + + int n_p_half = n_p / 2; + + for (int i = 0; i < n_p_half; i++) { + float x = x_gen_left(gen); + float y = y_gen(gen); + + float dest_x = BOARD_LENGTH + 30.0f; + float dest_y = y; + + float init_e_x = dest_x - x; + float init_e_y = dest_y - y; + float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y); + float init_e_unit_x = init_e_x / init_e_norm; + float init_e_unit_y = init_e_y / init_e_norm; + + float d_v0; + do { + d_v0 = velocity_gen(gen); + } while (d_v0 <= 0.1f); + + float v_m = 1.3f * d_v0; + + color[i] = 0; + pos_x[i] = x; + pos_y[i] = y; + e_x[i] = init_e_unit_x; + e_y[i] = init_e_unit_y; + v_max[i] = v_m; + desired_v0[i] = d_v0; + destination_x[i] = dest_x; + destination_y[i] = dest_y; + + w_x[i] = 0.0f; + w_y[i] = 0.0f; + curr_vel_x[i] = 0.0f; + curr_vel_y[i] = 0.0f; + sb_sq[i] = 0.0f; + sb_e_x[i] = 0.0f; + sb_e_y[i] = 0.0f; + } + + for (int i = n_p_half; i < n_p; i++) { + float x = x_gen_right(gen); + float y = y_gen(gen); + + float dest_x = -30.0f; + float dest_y = y; + + float init_e_x = dest_x - x; + float init_e_y = dest_y - y; + float init_e_norm = sqrt(init_e_x * init_e_x + init_e_y * init_e_y); + float init_e_unit_x = init_e_x / init_e_norm; + float init_e_unit_y = init_e_y / init_e_norm; + + float d_v0; + do { + d_v0 = velocity_gen(gen); + } while (d_v0 <= 0.1f); + + float v_m = 1.3f * d_v0; + + color[i] = 1; + pos_x[i] = x; + pos_y[i] = y; + e_x[i] = init_e_unit_x; + e_y[i] = init_e_unit_y; + v_max[i] = v_m; + desired_v0[i] = d_v0; + destination_x[i] = dest_x; + destination_y[i] = dest_y; + + w_x[i] = 0.0f; + w_y[i] = 0.0f; + curr_vel_x[i] = 0.0f; + curr_vel_y[i] = 0.0f; + sb_sq[i] = 0.0f; + sb_e_x[i] = 0.0f; + sb_e_y[i] = 0.0f; + } + + for (int j = n_p; j < n_p_pad; j++) { + pos_x[j] = 1e9f; + pos_y[j] = 1e9f; + sb_sq[j] = 0.0f; + sb_e_x[j] = 0.0f; + sb_e_y[j] = 0.0f; + } + return nullptr; +} + +Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); } + +static inline __m512 _avx512_exp_ps(__m512 x) { + // define own inlined intrinsic function for approximating the exponent function + // e^x = 2^n * e^r --> x = n * ln(2) + r + + const __m512 log2e = _mm512_set1_ps(1.4426950408889634f); + __m512 nf = _mm512_roundscale_ps(_mm512_mul_ps(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC); + + const __m512 ln2 = _mm512_set1_ps(0.6931471805599453f); + // r = x - n * ln(2) + __m512 r = _mm512_fnmadd_ps(nf, ln2, x); + + // approximation of e^r using taylor polynomial + // cNUM --> coefficients + const __m512 c12 = _mm512_set1_ps(c12_scalar); + const __m512 c11 = _mm512_set1_ps(c11_scalar); + const __m512 c10 = _mm512_set1_ps(c10_scalar); + const __m512 c9 = _mm512_set1_ps(c9_scalar); + const __m512 c8 = _mm512_set1_ps(c8_scalar); + const __m512 c7 = _mm512_set1_ps(c7_scalar); + const __m512 c6 = _mm512_set1_ps(c6_scalar); + const __m512 c5 = _mm512_set1_ps(c5_scalar); + const __m512 c4 = _mm512_set1_ps(c4_scalar); + const __m512 c3 = _mm512_set1_ps(c3_scalar); + const __m512 c2 = _mm512_set1_ps(c2_scalar); + const __m512 c1 = _mm512_set1_ps(c1_scalar); + const __m512 c0 = _mm512_set1_ps(c0_scalar); + + // apply coefficients + __m512 p = c12; + p = _mm512_fmadd_ps(p, r, c12); + p = _mm512_fmadd_ps(p, r, c11); + p = _mm512_fmadd_ps(p, r, c10); + p = _mm512_fmadd_ps(p, r, c9); + p = _mm512_fmadd_ps(p, r, c8); + p = _mm512_fmadd_ps(p, r, c7); + p = _mm512_fmadd_ps(p, r, c6); + p = _mm512_fmadd_ps(p, r, c5); + p = _mm512_fmadd_ps(p, r, c4); + p = _mm512_fmadd_ps(p, r, c3); + p = _mm512_fmadd_ps(p, r, c2); + p = _mm512_fmadd_ps(p, r, c1); + p = _mm512_fmadd_ps(p, r, c0); + + // use bit shifting for 2^n and bias because float have 23 bit mantissa + __m512i ni = _mm512_cvtps_epi32(nf); + __m512i biased = _mm512_add_epi32(ni, _mm512_set1_epi32(127)); + __m512i scaled = _mm512_slli_epi32(biased, 23); + __m512 pow2n = _mm512_castsi512_ps(scaled); + + // e^x + return _mm512_mul_ps(p, pow2n); +} + +int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, const char *dump_path) { + (void)pedestrians; +#if BENCHMARK + (void)dump_path; +#endif + +#if !BENCHMARK + const int dump_interval = 1; + vector> samples; + + if (dump_path) { + samples.reserve(static_cast(n_iter / dump_interval + 1) * n_p); + for (int i = 0; i < n_p; i++) { + samples.emplace_back(0, i, pos_x[i], pos_y[i], e_x[i], e_y[i], color[i]); + } + } +#endif + + const int n_p_pad = (n_p + 31) & ~31; + + alignas(64) const float lane_offset_arr[16] = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f}; + const __m512 lane_off = _mm512_load_ps(lane_offset_arr); + + const __m512 v_neg_v0_div_2s = _mm512_set1_ps(NEG_V0_AB_div_TWO_SIGMA); + const __m512 v_neg_2s_inv = _mm512_set1_ps(NEG_TWO_SIGMA_INV); + const __m512 v_cos_phi_sq = _mm512_set1_ps(cos_phi_sq); + const __m512 v_dt_neg = _mm512_set1_ps(dt_neg); + const __m512 v_c_neg_dt = _mm512_set1_ps(c_neg_times_dt); + const __m512 v_zero = _mm512_setzero_ps(); + const __m512 v_one = _mm512_set1_ps(1.0f); + const __m512 v_eps = _mm512_set1_ps(EPS); + const __m512 v_eps_sq = _mm512_set1_ps(EPS_SQ); + const __m512 v_np = _mm512_set1_ps((float)n_p); + const __m512 v_exp_min = _mm512_set1_ps(-87.0f); + + for (int t = 0; t < n_iter; t++) { + for (int i = 0; i < n_p; i++) { + // set the vector for the current pedestrian + + // get desired velocity + float desired_velocity_x = e_x[i] * desired_v0[i]; + float desired_velocity_y = e_y[i] * desired_v0[i]; + + // velocity change accumulator + float f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv; + float f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv; + + // pos + const __m512 pix = _mm512_set1_ps(pos_x[i]); + const __m512 piy = _mm512_set1_ps(pos_y[i]); + // desired direction + const __m512 eix = _mm512_set1_ps(e_x[i]); + const __m512 eiy = _mm512_set1_ps(e_y[i]); + // broadcast index used by mask to prevent situation pedestrian[i] == pedestrian[j], see m_neq_i_0 and m_neq_i_1 + const __m512 v_i = _mm512_set1_ps((float)i); + + // accumulators --> 16 pedestrians per accumulator + __m512 acc_x_0 = _mm512_setzero_ps(); + __m512 acc_y_0 = _mm512_setzero_ps(); + __m512 acc_x_1 = _mm512_setzero_ps(); + __m512 acc_y_1 = _mm512_setzero_ps(); + + // force between pedestrians + // loop unroll factor 32 with 2 vectors + // --> each vector has 16 lanes + for (int j = 0; j < n_p_pad; j += 32) { + + // [j, j+1, j+2, ..., j+15] and [j+16, j+17, j+18, ..., j+31] + __m512 j_idx_0 = _mm512_add_ps(_mm512_set1_ps((float)j), lane_off); + __m512 j_idx_1 = _mm512_add_ps(_mm512_set1_ps((float)j + 16), lane_off); + + // lane is active if j != i + // [1,1,1,0,1,1,1,...] 0 --> i == j, 1 --> i != j + __mmask16 m_neq_i_0 = _mm512_cmp_ps_mask(j_idx_0, v_i, _CMP_NEQ_OQ); + __mmask16 m_neq_i_1 = _mm512_cmp_ps_mask(j_idx_1, v_i, _CMP_NEQ_OQ); + + // lane is active if j < n_p --> valid range + // [1,1,1,0,1,1,1,...] 0 --> j >= n_p, 1 --> j < n_p + __mmask16 m_in_range_0 = _mm512_cmp_ps_mask(j_idx_0, v_np, _CMP_LT_OQ); + __mmask16 m_in_range_1 = _mm512_cmp_ps_mask(j_idx_1, v_np, _CMP_LT_OQ); + + // bitwise comparison if not valid --> 0 + // finally check if lane is valid or not --> prevents i == j, and + // j >= n_p because of loop unrolling --> we can therefore omit a remainings for loop + __mmask16 active_0 = m_neq_i_0 & m_in_range_0; + __mmask16 active_1 = m_neq_i_1 & m_in_range_1; + + // --- + + // load the positions of the other pedestrians x value + // [pos[j], pos[j+1], ..., pos[j+15]] and [pos[16], pos[j+17], ..., pos[j+31]] for x and y + __m512 pjx_0 = _mm512_loadu_ps(&pos_x[j]); + __m512 pjx_1 = _mm512_loadu_ps(&pos_x[j + 16]); + __m512 pjy_0 = _mm512_loadu_ps(&pos_y[j]); + __m512 pjy_1 = _mm512_loadu_ps(&pos_y[j + 16]); + + // --- + + // step vector of j + // [(v_j * delta_t * e_j).x, (v_j+1 * delta_t * e_j+1).x, ...] + __m512 sbe_jx_0 = _mm512_loadu_ps(&sb_e_x[j]); + __m512 sbe_jx_1 = _mm512_loadu_ps(&sb_e_x[j + 16]); + __m512 sbe_jy_0 = _mm512_loadu_ps(&sb_e_y[j]); + __m512 sbe_jy_1 = _mm512_loadu_ps(&sb_e_y[j + 16]); + + // --- + + // squared step length + // [j, j+1, ...] + __m512 sb_jsq_0 = _mm512_loadu_ps(&sb_sq[j]); + __m512 sb_jsq_1 = _mm512_loadu_ps(&sb_sq[j + 16]); + + // --- + + // distance vector + // [j, j+1, ...] + __m512 r_ab_x_0 = _mm512_sub_ps(pix, pjx_0); + __m512 r_ab_x_1 = _mm512_sub_ps(pix, pjx_1); + __m512 r_ab_y_0 = _mm512_sub_ps(piy, pjy_0); + __m512 r_ab_y_1 = _mm512_sub_ps(piy, pjy_1); + + // |r_ab|^2 needed for rsqrt to prevent 0 values --> x^2 + y^2 + __m512 r_ab_n2_0 = _mm512_fmadd_ps(r_ab_x_0, r_ab_x_0, _mm512_mul_ps(r_ab_y_0, r_ab_y_0)); + __m512 r_ab_n2_1 = _mm512_fmadd_ps(r_ab_x_1, r_ab_x_1, _mm512_mul_ps(r_ab_y_1, r_ab_y_1)); + + // prevent cases where distance = 0 (=pedestrian on top of each other) + // [dist_i_j, dist_i_j+1, ...] + r_ab_n2_0 = _mm512_max_ps(r_ab_n2_0, v_eps_sq); + r_ab_n2_1 = _mm512_max_ps(r_ab_n2_1, v_eps_sq); + + // --- + + // 1 / |r_ab| --> 14 bit rsqrt approx. rsqrt --> 1 / x^2 --> rsqrt(x^2) = 1/x + // [1 / dist_i_j, 1 / dist_i_j+1, ...] + __m512 inv_r_ab_n_0 = _mm512_rsqrt14_ps(r_ab_n2_0); + __m512 inv_r_ab_n_1 = _mm512_rsqrt14_ps(r_ab_n2_1); + + // r_ab^2 * 1 / r_ab = |r_ab| --> normed + // [r_i_j, r_i_j+1, ...] + __m512 r_ab_n_0 = _mm512_mul_ps(r_ab_n2_0, inv_r_ab_n_0); + __m512 r_ab_n_1 = _mm512_mul_ps(r_ab_n2_1, inv_r_ab_n_1); + + // r_ab - step[j] + // j next pos + __m512 r_ab2_x_0 = _mm512_sub_ps(r_ab_x_0, sbe_jx_0); + __m512 r_ab2_x_1 = _mm512_sub_ps(r_ab_x_1, sbe_jx_1); + __m512 r_ab2_y_0 = _mm512_sub_ps(r_ab_y_0, sbe_jy_0); + __m512 r_ab2_y_1 = _mm512_sub_ps(r_ab_y_1, sbe_jy_1); + + // |r_ab_2| --> needed for rsqrt trick (r_ab - step) + __m512 r_ab2_n2_0 = _mm512_fmadd_ps(r_ab2_x_0, r_ab2_x_0, _mm512_mul_ps(r_ab2_y_0, r_ab2_y_0)); + __m512 r_ab2_n2_1 = _mm512_fmadd_ps(r_ab2_x_1, r_ab2_x_1, _mm512_mul_ps(r_ab2_y_1, r_ab2_y_1)); + + // prevent 0 case --> infinity + r_ab2_n2_0 = _mm512_max_ps(r_ab2_n2_0, v_eps_sq); + r_ab2_n2_1 = _mm512_max_ps(r_ab2_n2_1, v_eps_sq); + + // same as before use rsqrt with 14bit approx + // [1 / |r_ab_2|, 1 / |r_ab_2|, ...] + __m512 inv_r_ab2_n_0 = _mm512_rsqrt14_ps(r_ab2_n2_0); + __m512 inv_r_ab2_n_1 = _mm512_rsqrt14_ps(r_ab2_n2_1); + + // |r_ab_2_second_distance_vector| * 1 / |r_ab_2_first_distance_vector| + __m512 r_ab2_n_0 = _mm512_mul_ps(r_ab2_n2_0, inv_r_ab2_n_0); + __m512 r_ab2_n_1 = _mm512_mul_ps(r_ab2_n2_1, inv_r_ab2_n_1); + + // r_sum = |r_ab| + |r_ab - step[j]| --> [w_0, w_1, w_2, ..., w_15] and [w_16, ..., w_31] + __m512 r_sum_0 = _mm512_add_ps(r_ab_n_0, r_ab2_n_0); + __m512 r_sum_1 = _mm512_add_ps(r_ab_n_1, r_ab2_n_1); + + // 2_b^2 = r_sum^2 - step[j]^2 + __m512 two_b_sq_0 = _mm512_fmsub_ps(r_sum_0, r_sum_0, sb_jsq_0); + __m512 two_b_sq_1 = _mm512_fmsub_ps(r_sum_1, r_sum_1, sb_jsq_1); + + // prevent 0 case + two_b_sq_0 = _mm512_max_ps(two_b_sq_0, v_eps_sq); + two_b_sq_1 = _mm512_max_ps(two_b_sq_1, v_eps_sq); + + // 2_b + // 1 / 2_b^(2*0.5) --> 1 / 2_b + __m512 inv_two_b_0 = _mm512_rsqrt14_ps(two_b_sq_0); + __m512 inv_two_b_1 = _mm512_rsqrt14_ps(two_b_sq_1); + + // 2_b = 2_b^2 * 1 / 2_b + __m512 two_b_0 = _mm512_mul_ps(two_b_sq_0, inv_two_b_0); + __m512 two_b_1 = _mm512_mul_ps(two_b_sq_1, inv_two_b_1); + + // --- + + // two_b * -1 / (2*sigma) --> exp + __m512 e_arg_0 = _mm512_mul_ps(two_b_0, v_neg_2s_inv); + __m512 e_arg_1 = _mm512_mul_ps(two_b_1, v_neg_2s_inv); + + // exp(x) + // when x is small just assume constant value --> faster exp + e_arg_0 = _mm512_max_ps(e_arg_0, v_exp_min); + e_arg_1 = _mm512_max_ps(e_arg_1, v_exp_min); + + // custom fast exponent intrinsic using polynomial approximations and some precomputed constants + __m512 e_val_0 = _avx512_exp_ps(e_arg_0); + __m512 e_val_1 = _avx512_exp_ps(e_arg_1); + + // [−(V0/σ) * e^(−b/σ) * w/(4b), ...] --> scalar + __m512 numer_inv_two_b_0 = _mm512_mul_ps(_mm512_mul_ps(r_sum_0, v_neg_v0_div_2s), _mm512_mul_ps(e_val_0, inv_two_b_0)); + __m512 numer_inv_two_b_1 = _mm512_mul_ps(_mm512_mul_ps(r_sum_1, v_neg_v0_div_2s), _mm512_mul_ps(e_val_1, inv_two_b_1)); + + // scalar * 1 / |r_ab| + __m512 sm_r_ab_0 = _mm512_mul_ps(numer_inv_two_b_0, inv_r_ab_n_0); + __m512 sm_r_ab_1 = _mm512_mul_ps(numer_inv_two_b_1, inv_r_ab_n_1); + + // scalar * 1 / |r_ab_2| + __m512 sm_r_ab2_0 = _mm512_mul_ps(numer_inv_two_b_0, inv_r_ab2_n_0); + __m512 sm_r_ab2_1 = _mm512_mul_ps(numer_inv_two_b_1, inv_r_ab2_n_1); + + // summed force + __m512 f_x_0 = _mm512_fmadd_ps(r_ab2_x_0, sm_r_ab2_0, _mm512_mul_ps(r_ab_x_0, sm_r_ab_0)); + __m512 f_x_1 = _mm512_fmadd_ps(r_ab2_x_1, sm_r_ab2_1, _mm512_mul_ps(r_ab_x_1, sm_r_ab_1)); + __m512 f_y_0 = _mm512_fmadd_ps(r_ab2_y_0, sm_r_ab2_0, _mm512_mul_ps(r_ab_y_0, sm_r_ab_0)); + __m512 f_y_1 = _mm512_fmadd_ps(r_ab2_y_1, sm_r_ab2_1, _mm512_mul_ps(r_ab_y_1, sm_r_ab_1)); + + // using masking to perform --> field of view formula + // if (e_dot_f >= 0.0 || e_dot_f_sq <= rhs) + // return dt_neg; + // else + // return c_neg_times_dt; + // + __m512 f_norm_sq_0 = _mm512_fmadd_ps(f_x_0, f_x_0, _mm512_mul_ps(f_y_0, f_y_0)); + __m512 f_norm_sq_1 = _mm512_fmadd_ps(f_x_1, f_x_1, _mm512_mul_ps(f_y_1, f_y_1)); + + __m512 rhs_0 = _mm512_mul_ps(f_norm_sq_0, v_cos_phi_sq); + __m512 rhs_1 = _mm512_mul_ps(f_norm_sq_1, v_cos_phi_sq); + + __m512 e_dot_f_0 = _mm512_fmadd_ps(eix, f_x_0, _mm512_mul_ps(eiy, f_y_0)); + __m512 e_dot_f_1 = _mm512_fmadd_ps(eix, f_x_1, _mm512_mul_ps(eiy, f_y_1)); + + __m512 e_dot_f_sq_0 = _mm512_mul_ps(e_dot_f_0, e_dot_f_0); + __m512 e_dot_f_sq_1 = _mm512_mul_ps(e_dot_f_1, e_dot_f_1); + + // e_i * f < 0 --> behind + __mmask16 c1_0 = _mm512_cmp_ps_mask(e_dot_f_0, v_zero, _CMP_LT_OQ); + __mmask16 c1_1 = _mm512_cmp_ps_mask(e_dot_f_1, v_zero, _CMP_LT_OQ); + + // (e_i * f)^2 > cos(phi) --> out of view + __mmask16 c2_0 = _mm512_cmp_ps_mask(e_dot_f_sq_0, rhs_0, _CMP_GT_OQ); + __mmask16 c2_1 = _mm512_cmp_ps_mask(e_dot_f_sq_1, rhs_1, _CMP_GT_OQ); + + // get the case (in field of view or not) + // 1 = reduced weight, 0 = regular + __mmask16 cond_0 = c1_0 & c2_0; + __mmask16 cond_1 = c1_1 & c2_1; + + // apply the correct weight + __m512 w_val_0 = _mm512_mask_blend_ps(cond_0, v_dt_neg, v_c_neg_dt); + __m512 w_val_1 = _mm512_mask_blend_ps(cond_1, v_dt_neg, v_c_neg_dt); + + // take care of i == j case by zeroing it out + // see active_0 and active_1 at the beginning of this loop + // [f.x or 0, ...] + __m512 f_x_safe_0 = _mm512_maskz_mov_ps(active_0, f_x_0); + __m512 f_x_safe_1 = _mm512_maskz_mov_ps(active_1, f_x_1); + __m512 f_y_safe_0 = _mm512_maskz_mov_ps(active_0, f_y_0); + __m512 f_y_safe_1 = _mm512_maskz_mov_ps(active_1, f_y_1); + + // accumulate force with weight + // force_P_total += weight * force_P_other + acc_x_0 = _mm512_fmadd_ps(f_x_safe_0, w_val_0, acc_x_0); + acc_x_1 = _mm512_fmadd_ps(f_x_safe_1, w_val_1, acc_x_1); + acc_y_0 = _mm512_fmadd_ps(f_y_safe_0, w_val_0, acc_y_0); + acc_y_1 = _mm512_fmadd_ps(f_y_safe_1, w_val_1, acc_y_1); + } + + // scalar force accumulator + f_alpha_dt_x += _mm512_reduce_add_ps(_mm512_add_ps(acc_x_0, acc_x_1)); + f_alpha_dt_y += _mm512_reduce_add_ps(_mm512_add_ps(acc_y_0, acc_y_1)); + + // force between pedestrian and obstacles + // no optimizations, because force between pedestrian is the bottleneck of the computation + for (int k = 0; k < board.n_obstacles; k++) { + float v1_x = pos_x[i] - board.obstacles[k].start_x; + float v1_y = pos_y[i] - board.obstacles[k].start_y; + float v2_x = board.obstacles[k].obstacle_v_x; + float v2_y = board.obstacles[k].obstacle_v_y; + + float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv; + + if (projection < 0.0f) + projection = 0.0f; + else if (projection > 1.0f) + projection = 1.0f; + + 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_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; + + float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm; + float f_alpha_B_neg_x = r_aB_x * scalar_mult; + float f_alpha_B_neg_y = r_aB_y * scalar_mult; + + float w_val = dt_neg; + float e_dot_f = (f_alpha_B_neg_y == 0) ? 0 : (f_alpha_B_neg_y < 0 ? -e_y[i] : e_y[i]); + + if (e_dot_f < cos_phi) + w_val = c_neg_times_dt; + + 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; + + f_alpha_dt_x += f_alpha_B_dt_x; + f_alpha_dt_y += f_alpha_B_dt_y; + } + + w_x[i] += f_alpha_dt_x; + w_y[i] += f_alpha_dt_y; + } + + // update each pedestrian + for (int i = 0; i < n_p_pad; i += 16) { + // load 16 pedestrians per iteration with their data + // SoA in vectors + __m512 w_x_i_vec = _mm512_loadu_ps(&w_x[i]); + __m512 w_y_i_vec = _mm512_loadu_ps(&w_y[i]); + __m512 v_max_i_vec = _mm512_loadu_ps(&v_max[i]); + __m512 pos_x_i_vec = _mm512_loadu_ps(&pos_x[i]); + __m512 pos_y_i_vec = _mm512_loadu_ps(&pos_y[i]); + __m512 destination_y_i_vec = _mm512_loadu_ps(&destination_y[i]); + __m512 destination_x_i_vec = _mm512_loadu_ps(&destination_x[i]); + __m512 desired_v0_i_vec = _mm512_loadu_ps(&desired_v0[i]); + + __m512i color_i_vec = _mm512_loadu_si512((__m512i *)&color[i]); + + __m512 w_norm_sq = _mm512_fmadd_ps(w_x_i_vec, w_x_i_vec, _mm512_mul_ps(w_y_i_vec, w_y_i_vec)); + __m512 w_norm_inv = _mm512_rsqrt14_ps(w_norm_sq); + __m512 correction_factor = _mm512_min_ps(_mm512_set1_ps(1.0f), _mm512_mul_ps(v_max_i_vec, w_norm_inv)); + + __m512 curr_vel_x_i_vec = _mm512_mul_ps(w_x_i_vec, correction_factor); + __m512 curr_vel_y_i_vec = _mm512_mul_ps(w_y_i_vec, correction_factor); + + __m512 dt_vec = _mm512_set1_ps(dt); + pos_x_i_vec = _mm512_fmadd_ps(curr_vel_x_i_vec, dt_vec, pos_x_i_vec); + pos_y_i_vec = _mm512_fmadd_ps(curr_vel_y_i_vec, dt_vec, pos_y_i_vec); + + // differentiate whether pedestrian came from left or right using masking + __mmask16 cond_1_left = _mm512_cmpeq_epi32_mask(color_i_vec, _mm512_setzero_si512()); + __mmask16 cond_1_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH - 1.5f), _CMP_GT_OQ); + __mmask16 cond_1 = cond_1_left & cond_1_right; + + pos_x_i_vec = _mm512_mask_blend_ps(cond_1, pos_x_i_vec, _mm512_set1_ps(1.0f)); + destination_y_i_vec = _mm512_mask_blend_ps(cond_1, destination_y_i_vec, pos_y_i_vec); + w_x_i_vec = _mm512_mask_blend_ps(cond_1, w_x_i_vec, desired_v0_i_vec); + w_y_i_vec = _mm512_mask_blend_ps(cond_1, w_y_i_vec, _mm512_setzero_ps()); + curr_vel_x_i_vec = _mm512_mask_blend_ps(cond_1, curr_vel_x_i_vec, w_x_i_vec); + curr_vel_y_i_vec = _mm512_mask_blend_ps(cond_1, curr_vel_y_i_vec, w_y_i_vec); + + __mmask16 cond_2_left = _mm512_cmpeq_epi32_mask(color_i_vec, _mm512_set1_epi32(1)); + __mmask16 cond_2_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(1.5f), _CMP_LT_OQ); + __mmask16 cond_2 = cond_2_left & cond_2_right; + + pos_x_i_vec = _mm512_mask_blend_ps(cond_2, pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH - 1.0f)); + destination_y_i_vec = _mm512_mask_blend_ps(cond_2, destination_y_i_vec, pos_y_i_vec); + __m512 neg_desired_v0 = _mm512_xor_ps(desired_v0_i_vec, _mm512_set1_ps(-0.0f)); + w_x_i_vec = _mm512_mask_blend_ps(cond_2, w_x_i_vec, neg_desired_v0); + w_y_i_vec = _mm512_mask_blend_ps(cond_2, w_y_i_vec, _mm512_setzero_ps()); + curr_vel_x_i_vec = _mm512_mask_blend_ps(cond_2, curr_vel_x_i_vec, w_x_i_vec); + curr_vel_y_i_vec = _mm512_mask_blend_ps(cond_2, curr_vel_y_i_vec, w_y_i_vec); + + pos_y_i_vec = _mm512_max_ps(pos_y_i_vec, _mm512_set1_ps(WALL_EPS)); + pos_y_i_vec = _mm512_min_ps(pos_y_i_vec, _mm512_set1_ps(BOARD_WIDTH_MINUS_WALL_EPS)); + + __m512 ex_vec = _mm512_sub_ps(destination_x_i_vec, pos_x_i_vec); + __m512 ey_vec = _mm512_sub_ps(destination_y_i_vec, pos_y_i_vec); + __m512 e_norm_sq = _mm512_fmadd_ps(ex_vec, ex_vec, _mm512_mul_ps(ey_vec, ey_vec)); + __m512 e_norm_inv = _mm512_rsqrt14_ps(e_norm_sq); + __m512 e_x_i_vec = _mm512_mul_ps(ex_vec, e_norm_inv); + __m512 e_y_i_vec = _mm512_mul_ps(ey_vec, e_norm_inv); + + __m512 vel_norm_sq = _mm512_fmadd_ps(curr_vel_x_i_vec, curr_vel_x_i_vec, _mm512_mul_ps(curr_vel_y_i_vec, curr_vel_y_i_vec)); + __m512 sb_sq_i_vec = _mm512_mul_ps(vel_norm_sq, _mm512_set1_ps(delta_t_sq)); + __m512 vel_norm_dt = _mm512_sqrt_ps(sb_sq_i_vec); + + // write the data + _mm512_storeu_ps(&curr_vel_x[i], curr_vel_x_i_vec); + _mm512_storeu_ps(&curr_vel_y[i], curr_vel_y_i_vec); + _mm512_storeu_ps(&w_x[i], w_x_i_vec); + _mm512_storeu_ps(&w_y[i], w_y_i_vec); + _mm512_storeu_ps(&pos_x[i], pos_x_i_vec); + _mm512_storeu_ps(&pos_y[i], pos_y_i_vec); + _mm512_storeu_ps(&e_x[i], e_x_i_vec); + _mm512_storeu_ps(&e_y[i], e_y_i_vec); + _mm512_storeu_ps(&destination_y[i], destination_y_i_vec); + _mm512_storeu_ps(&sb_sq[i], sb_sq_i_vec); + _mm512_storeu_ps(&sb_e_x[i], _mm512_mul_ps(e_x_i_vec, vel_norm_dt)); + _mm512_storeu_ps(&sb_e_y[i], _mm512_mul_ps(e_y_i_vec, vel_norm_dt)); + } + +#if !BENCHMARK + for (int i = 0; i < n_p; i++) { + if (dump_path && (t % dump_interval == 0)) { + samples.emplace_back(t + 1, i, pos_x[i], pos_y[i], e_x[i], e_y[i], color[i]); + } + } +#endif + } + +#if !BENCHMARK + if (dump_path) { + ofstream out(dump_path); + out << "time,id,x,y,p.e.x,p.e.y,color\n"; + for (auto &[t, id, x, y, e_x, e_y, c] : samples) { + out << t << ',' << id << ',' << x << ',' << y << ',' << e_x << ',' << e_y << ',' << c << '\n'; + } + } +#endif + + return 0; +} diff --git a/variants/variant_float_inlining_SoA_NEON_polyexp_moreavx_ilp_2_rsqrt_morrsqrt.cpp b/variants/variant_float_inlining_SoA_NEON_polyexp_moreavx_ilp_2_rsqrt_morrsqrt.cpp index 46c0088..7ec0af2 100644 --- a/variants/variant_float_inlining_SoA_NEON_polyexp_moreavx_ilp_2_rsqrt_morrsqrt.cpp +++ b/variants/variant_float_inlining_SoA_NEON_polyexp_moreavx_ilp_2_rsqrt_morrsqrt.cpp @@ -9,7 +9,14 @@ #include "../headers/constants_float.hpp" #include "../headers/pedestrian_reduced_float.hpp" +#ifdef __aarch64__ #include +#endif + +#ifdef __x86_64__ +#define SIMDE_ENABLE_NATIVE_ALIASES +#include +#endif #include #include @@ -33,7 +40,6 @@ using namespace std; - alignas(64) vector pos_x, pos_y; alignas(64) vector sb_sq; alignas(64) vector sb_e_x, sb_e_y; @@ -58,7 +64,6 @@ Pedestrian *init_pedestrians(int n_p) { 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); - const int n_p_pad = (n_p + 7) & ~7; pos_x.resize(n_p_pad); @@ -170,13 +175,12 @@ Pedestrian *init_pedestrians(int n_p) { Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); } - static inline float32x4_t _neon_exp_ps(float32x4_t x) { const float32x4_t log2e = vdupq_n_f32(1.4426950408889634f); float32x4_t nf = vrndnq_f32(vmulq_f32(x, log2e)); const float32x4_t ln2 = vdupq_n_f32(0.6931471805599453f); - float32x4_t r = vfmsq_f32(x, nf, ln2); // r = x - nf*ln2 + float32x4_t r = vfmsq_f32(x, nf, ln2); // r = x - nf*ln2 const float32x4_t c5 = vdupq_n_f32(1.0f / 120.0f); const float32x4_t c4 = vdupq_n_f32(1.0f / 24.0f); @@ -192,7 +196,7 @@ static inline float32x4_t _neon_exp_ps(float32x4_t x) { p = vfmaq_f32(c1, p, r); p = vfmaq_f32(c0, p, r); - int32x4_t ni = vcvtq_s32_f32(nf); + int32x4_t ni = vcvtq_s32_f32(nf); int32x4_t biased = vaddq_s32(ni, vdupq_n_s32(127)); int32x4_t scaled = vshlq_n_s32(biased, 23); float32x4_t pow2n = vreinterpretq_f32_s32(scaled); @@ -230,19 +234,19 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c const float32x4_t lane_off = {0.0f, 1.0f, 2.0f, 3.0f}; const float32x4_t v_neg_v0_div_2s = vdupq_n_f32(NEG_V0_AB_div_TWO_SIGMA); - const float32x4_t v_neg_2s_inv = vdupq_n_f32(NEG_TWO_SIGMA_INV); - const float32x4_t v_cos_phi_sq = vdupq_n_f32(cos_phi_sq); - const float32x4_t v_dt_neg = vdupq_n_f32(dt_neg); - const float32x4_t v_c_neg_dt = vdupq_n_f32(c_neg_times_dt); - const float32x4_t v_zero = vdupq_n_f32(0.0f); - const float32x4_t v_one = vdupq_n_f32(1.0f); - const float32x4_t v_eps_sq = vdupq_n_f32(EPS_SQ); - const float32x4_t v_np = vdupq_n_f32((float)n_p); - const float32x4_t v_exp_min = vdupq_n_f32(-87.0f); + const float32x4_t v_neg_2s_inv = vdupq_n_f32(NEG_TWO_SIGMA_INV); + const float32x4_t v_cos_phi_sq = vdupq_n_f32(cos_phi_sq); + const float32x4_t v_dt_neg = vdupq_n_f32(dt_neg); + const float32x4_t v_c_neg_dt = vdupq_n_f32(c_neg_times_dt); + const float32x4_t v_zero = vdupq_n_f32(0.0f); + const float32x4_t v_one = vdupq_n_f32(1.0f); + const float32x4_t v_eps_sq = vdupq_n_f32(EPS_SQ); + const float32x4_t v_np = vdupq_n_f32((float)n_p); + const float32x4_t v_exp_min = vdupq_n_f32(-87.0f); 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]; @@ -255,28 +259,27 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c const float32x4_t eiy = vdupq_n_f32(e_y[i]); const float32x4_t v_i = vdupq_n_f32((float)i); - float32x4_t acc_x_0 = vdupq_n_f32(0.0f); float32x4_t acc_y_0 = vdupq_n_f32(0.0f); float32x4_t acc_x_1 = vdupq_n_f32(0.0f); float32x4_t acc_y_1 = vdupq_n_f32(0.0f); for (int j = 0; j < n_p_pad; j += 8) { - - float32x4_t j_idx_0 = vaddq_f32(vdupq_n_f32((float)j), lane_off); + + float32x4_t j_idx_0 = vaddq_f32(vdupq_n_f32((float)j), lane_off); float32x4_t j_idx_1 = vaddq_f32(vdupq_n_f32((float)(j + 4)), lane_off); - uint32x4_t m_neq_i_0 = vmvnq_u32(vceqq_f32(j_idx_0, v_i)); - uint32x4_t m_neq_i_1 = vmvnq_u32(vceqq_f32(j_idx_1, v_i)); + uint32x4_t m_neq_i_0 = vmvnq_u32(vceqq_f32(j_idx_0, v_i)); + uint32x4_t m_neq_i_1 = vmvnq_u32(vceqq_f32(j_idx_1, v_i)); uint32x4_t m_in_range_0 = vcltq_f32(j_idx_0, v_np); uint32x4_t m_in_range_1 = vcltq_f32(j_idx_1, v_np); - uint32x4_t active_0 = vandq_u32(m_neq_i_0, m_in_range_0); - uint32x4_t active_1 = vandq_u32(m_neq_i_1, m_in_range_1); + uint32x4_t active_0 = vandq_u32(m_neq_i_0, m_in_range_0); + uint32x4_t active_1 = vandq_u32(m_neq_i_1, m_in_range_1); - float32x4_t pjx_0 = vld1q_f32(&pos_x[j]); - float32x4_t pjx_1 = vld1q_f32(&pos_x[j + 4]); - float32x4_t pjy_0 = vld1q_f32(&pos_y[j]); - float32x4_t pjy_1 = vld1q_f32(&pos_y[j + 4]); + float32x4_t pjx_0 = vld1q_f32(&pos_x[j]); + float32x4_t pjx_1 = vld1q_f32(&pos_x[j + 4]); + float32x4_t pjy_0 = vld1q_f32(&pos_y[j]); + float32x4_t pjy_1 = vld1q_f32(&pos_y[j + 4]); float32x4_t sbe_jx_0 = vld1q_f32(&sb_e_x[j]); float32x4_t sbe_jx_1 = vld1q_f32(&sb_e_x[j + 4]); float32x4_t sbe_jy_0 = vld1q_f32(&sb_e_y[j]); @@ -284,7 +287,6 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c float32x4_t sb_jsq_0 = vld1q_f32(&sb_sq[j]); float32x4_t sb_jsq_1 = vld1q_f32(&sb_sq[j + 4]); - float32x4_t r_ab_x_0 = vsubq_f32(pix, pjx_0); float32x4_t r_ab_x_1 = vsubq_f32(pix, pjx_1); float32x4_t r_ab_y_0 = vsubq_f32(piy, pjy_0); @@ -302,7 +304,6 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c float32x4_t r_ab_n_0 = vmulq_f32(r_ab_n2_0, inv_r_ab_n_0); float32x4_t r_ab_n_1 = vmulq_f32(r_ab_n2_1, inv_r_ab_n_1); - float32x4_t r_ab2_x_0 = vsubq_f32(r_ab_x_0, sbe_jx_0); float32x4_t r_ab2_x_1 = vsubq_f32(r_ab_x_1, sbe_jx_1); float32x4_t r_ab2_y_0 = vsubq_f32(r_ab_y_0, sbe_jy_0); @@ -323,7 +324,6 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c float32x4_t r_sum_0 = vaddq_f32(r_ab_n_0, r_ab2_n_0); float32x4_t r_sum_1 = vaddq_f32(r_ab_n_1, r_ab2_n_1); - float32x4_t two_b_sq_0 = vsubq_f32(vmulq_f32(r_sum_0, r_sum_0), sb_jsq_0); float32x4_t two_b_sq_1 = vsubq_f32(vmulq_f32(r_sum_1, r_sum_1), sb_jsq_1); @@ -339,33 +339,25 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c float32x4_t e_arg_0 = vmulq_f32(two_b_0, v_neg_2s_inv); float32x4_t e_arg_1 = vmulq_f32(two_b_1, v_neg_2s_inv); - e_arg_0 = vmaxq_f32(e_arg_0, v_exp_min); e_arg_1 = vmaxq_f32(e_arg_1, v_exp_min); float32x4_t e_val_0 = _neon_exp_ps(e_arg_0); float32x4_t e_val_1 = _neon_exp_ps(e_arg_1); - - float32x4_t numer_inv_two_b_0 = vmulq_f32( - vmulq_f32(r_sum_0, v_neg_v0_div_2s), - vmulq_f32(e_val_0, inv_two_b_0)); - float32x4_t numer_inv_two_b_1 = vmulq_f32( - vmulq_f32(r_sum_1, v_neg_v0_div_2s), - vmulq_f32(e_val_1, inv_two_b_1)); + float32x4_t numer_inv_two_b_0 = vmulq_f32(vmulq_f32(r_sum_0, v_neg_v0_div_2s), vmulq_f32(e_val_0, inv_two_b_0)); + float32x4_t numer_inv_two_b_1 = vmulq_f32(vmulq_f32(r_sum_1, v_neg_v0_div_2s), vmulq_f32(e_val_1, inv_two_b_1)); - float32x4_t sm_r_ab_0 = vmulq_f32(numer_inv_two_b_0, inv_r_ab_n_0); - float32x4_t sm_r_ab_1 = vmulq_f32(numer_inv_two_b_1, inv_r_ab_n_1); + float32x4_t sm_r_ab_0 = vmulq_f32(numer_inv_two_b_0, inv_r_ab_n_0); + float32x4_t sm_r_ab_1 = vmulq_f32(numer_inv_two_b_1, inv_r_ab_n_1); float32x4_t sm_r_ab2_0 = vmulq_f32(numer_inv_two_b_0, inv_r_ab2_n_0); float32x4_t sm_r_ab2_1 = vmulq_f32(numer_inv_two_b_1, inv_r_ab2_n_1); - float32x4_t f_x_0 = vfmaq_f32(vmulq_f32(r_ab_x_0, sm_r_ab_0), r_ab2_x_0, sm_r_ab2_0); float32x4_t f_x_1 = vfmaq_f32(vmulq_f32(r_ab_x_1, sm_r_ab_1), r_ab2_x_1, sm_r_ab2_1); float32x4_t f_y_0 = vfmaq_f32(vmulq_f32(r_ab_y_0, sm_r_ab_0), r_ab2_y_0, sm_r_ab2_0); float32x4_t f_y_1 = vfmaq_f32(vmulq_f32(r_ab_y_1, sm_r_ab_1), r_ab2_y_1, sm_r_ab2_1); - float32x4_t f_norm_sq_0 = vfmaq_f32(vmulq_f32(f_y_0, f_y_0), f_x_0, f_x_0); float32x4_t f_norm_sq_1 = vfmaq_f32(vmulq_f32(f_y_1, f_y_1), f_x_1, f_x_1); @@ -389,7 +381,6 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c float32x4_t w_val_0 = vbslq_f32(cond_0, v_c_neg_dt, v_dt_neg); float32x4_t w_val_1 = vbslq_f32(cond_1, v_c_neg_dt, v_dt_neg); - float32x4_t f_x_safe_0 = vbslq_f32(active_0, f_x_0, v_zero); float32x4_t f_x_safe_1 = vbslq_f32(active_1, f_x_1, v_zero); float32x4_t f_y_safe_0 = vbslq_f32(active_0, f_y_0, v_zero); @@ -401,11 +392,9 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c acc_y_1 = vfmaq_f32(acc_y_1, f_y_safe_1, w_val_1); } - f_alpha_dt_x += vaddvq_f32(vaddq_f32(acc_x_0, acc_x_1)); f_alpha_dt_y += vaddvq_f32(vaddq_f32(acc_y_0, acc_y_1)); - 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; @@ -446,21 +435,19 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c w_y[i] += f_alpha_dt_y; } - for (int i = 0; i < n_p_pad; i += 4) { - float32x4_t w_x_i_vec = vld1q_f32(&w_x[i]); - float32x4_t w_y_i_vec = vld1q_f32(&w_y[i]); - float32x4_t v_max_i_vec = vld1q_f32(&v_max[i]); - float32x4_t pos_x_i_vec = vld1q_f32(&pos_x[i]); - float32x4_t pos_y_i_vec = vld1q_f32(&pos_y[i]); + float32x4_t w_x_i_vec = vld1q_f32(&w_x[i]); + float32x4_t w_y_i_vec = vld1q_f32(&w_y[i]); + float32x4_t v_max_i_vec = vld1q_f32(&v_max[i]); + float32x4_t pos_x_i_vec = vld1q_f32(&pos_x[i]); + float32x4_t pos_y_i_vec = vld1q_f32(&pos_y[i]); float32x4_t destination_y_i_vec = vld1q_f32(&destination_y[i]); float32x4_t destination_x_i_vec = vld1q_f32(&destination_x[i]); - float32x4_t desired_v0_i_vec = vld1q_f32(&desired_v0[i]); - int32x4_t color_i_vec = vld1q_s32(&color[i]); + float32x4_t desired_v0_i_vec = vld1q_f32(&desired_v0[i]); + int32x4_t color_i_vec = vld1q_s32(&color[i]); - - float32x4_t w_norm_sq = vfmaq_f32(vmulq_f32(w_y_i_vec, w_y_i_vec), w_x_i_vec, w_x_i_vec); + float32x4_t w_norm_sq = vfmaq_f32(vmulq_f32(w_y_i_vec, w_y_i_vec), w_x_i_vec, w_x_i_vec); float32x4_t w_norm_inv = _neon_rsqrt_ps(w_norm_sq); float32x4_t correction_factor = vminq_f32(v_one, vmulq_f32(v_max_i_vec, w_norm_inv)); @@ -471,63 +458,56 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c pos_x_i_vec = vfmaq_f32(pos_x_i_vec, curr_vel_x_i_vec, dt_vec); pos_y_i_vec = vfmaq_f32(pos_y_i_vec, curr_vel_y_i_vec, dt_vec); - - uint32x4_t cond_1_left = vceqq_s32(color_i_vec, vdupq_n_s32(0)); + uint32x4_t cond_1_left = vceqq_s32(color_i_vec, vdupq_n_s32(0)); uint32x4_t cond_1_right = vcgtq_f32(pos_x_i_vec, vdupq_n_f32(BOARD_LENGTH - 1.5f)); - uint32x4_t cond_1 = vandq_u32(cond_1_left, cond_1_right); + uint32x4_t cond_1 = vandq_u32(cond_1_left, cond_1_right); - - pos_x_i_vec = vbslq_f32(cond_1, v_one, pos_x_i_vec); - destination_y_i_vec = vbslq_f32(cond_1, pos_y_i_vec, destination_y_i_vec); - w_x_i_vec = vbslq_f32(cond_1, desired_v0_i_vec, w_x_i_vec); - w_y_i_vec = vbslq_f32(cond_1, v_zero, w_y_i_vec); - curr_vel_x_i_vec = vbslq_f32(cond_1, w_x_i_vec, curr_vel_x_i_vec); - curr_vel_y_i_vec = vbslq_f32(cond_1, w_y_i_vec, curr_vel_y_i_vec); + pos_x_i_vec = vbslq_f32(cond_1, v_one, pos_x_i_vec); + destination_y_i_vec = vbslq_f32(cond_1, pos_y_i_vec, destination_y_i_vec); + w_x_i_vec = vbslq_f32(cond_1, desired_v0_i_vec, w_x_i_vec); + w_y_i_vec = vbslq_f32(cond_1, v_zero, w_y_i_vec); + curr_vel_x_i_vec = vbslq_f32(cond_1, w_x_i_vec, curr_vel_x_i_vec); + curr_vel_y_i_vec = vbslq_f32(cond_1, w_y_i_vec, curr_vel_y_i_vec); - - uint32x4_t cond_2_left = vceqq_s32(color_i_vec, vdupq_n_s32(1)); + uint32x4_t cond_2_left = vceqq_s32(color_i_vec, vdupq_n_s32(1)); uint32x4_t cond_2_right = vcltq_f32(pos_x_i_vec, vdupq_n_f32(1.5f)); - uint32x4_t cond_2 = vandq_u32(cond_2_left, cond_2_right); + uint32x4_t cond_2 = vandq_u32(cond_2_left, cond_2_right); - pos_x_i_vec = vbslq_f32(cond_2, vdupq_n_f32(BOARD_LENGTH - 1.0f), pos_x_i_vec); - destination_y_i_vec = vbslq_f32(cond_2, pos_y_i_vec, destination_y_i_vec); + pos_x_i_vec = vbslq_f32(cond_2, vdupq_n_f32(BOARD_LENGTH - 1.0f), pos_x_i_vec); + destination_y_i_vec = vbslq_f32(cond_2, pos_y_i_vec, destination_y_i_vec); float32x4_t neg_desired_v0 = vnegq_f32(desired_v0_i_vec); - w_x_i_vec = vbslq_f32(cond_2, neg_desired_v0, w_x_i_vec); - w_y_i_vec = vbslq_f32(cond_2, v_zero, w_y_i_vec); - curr_vel_x_i_vec = vbslq_f32(cond_2, w_x_i_vec, curr_vel_x_i_vec); - curr_vel_y_i_vec = vbslq_f32(cond_2, w_y_i_vec, curr_vel_y_i_vec); + w_x_i_vec = vbslq_f32(cond_2, neg_desired_v0, w_x_i_vec); + w_y_i_vec = vbslq_f32(cond_2, v_zero, w_y_i_vec); + curr_vel_x_i_vec = vbslq_f32(cond_2, w_x_i_vec, curr_vel_x_i_vec); + curr_vel_y_i_vec = vbslq_f32(cond_2, w_y_i_vec, curr_vel_y_i_vec); - pos_y_i_vec = vmaxq_f32(pos_y_i_vec, vdupq_n_f32(WALL_EPS)); pos_y_i_vec = vminq_f32(pos_y_i_vec, vdupq_n_f32(BOARD_WIDTH_MINUS_WALL_EPS)); - float32x4_t ex_vec = vsubq_f32(destination_x_i_vec, pos_x_i_vec); float32x4_t ey_vec = vsubq_f32(destination_y_i_vec, pos_y_i_vec); - float32x4_t e_norm_sq = vfmaq_f32(vmulq_f32(ey_vec, ey_vec), ex_vec, ex_vec); + float32x4_t e_norm_sq = vfmaq_f32(vmulq_f32(ey_vec, ey_vec), ex_vec, ex_vec); float32x4_t e_norm_inv = _neon_rsqrt_ps(e_norm_sq); - float32x4_t e_x_i_vec = vmulq_f32(ex_vec, e_norm_inv); - float32x4_t e_y_i_vec = vmulq_f32(ey_vec, e_norm_inv); + float32x4_t e_x_i_vec = vmulq_f32(ex_vec, e_norm_inv); + float32x4_t e_y_i_vec = vmulq_f32(ey_vec, e_norm_inv); - - float32x4_t vel_norm_sq = vfmaq_f32(vmulq_f32(curr_vel_y_i_vec, curr_vel_y_i_vec), - curr_vel_x_i_vec, curr_vel_x_i_vec); + float32x4_t vel_norm_sq = vfmaq_f32(vmulq_f32(curr_vel_y_i_vec, curr_vel_y_i_vec), curr_vel_x_i_vec, curr_vel_x_i_vec); float32x4_t sb_sq_i_vec = vmulq_f32(vel_norm_sq, vdupq_n_f32(delta_t_sq)); - float32x4_t vel_norm_dt = vsqrtq_f32(sb_sq_i_vec); // full-precision sqrt, matches AVX-512 + float32x4_t vel_norm_dt = vsqrtq_f32(sb_sq_i_vec); // full-precision sqrt, matches AVX-512 - vst1q_f32(&curr_vel_x[i], curr_vel_x_i_vec); - vst1q_f32(&curr_vel_y[i], curr_vel_y_i_vec); - vst1q_f32(&w_x[i], w_x_i_vec); - vst1q_f32(&w_y[i], w_y_i_vec); - vst1q_f32(&pos_x[i], pos_x_i_vec); - vst1q_f32(&pos_y[i], pos_y_i_vec); - vst1q_f32(&e_x[i], e_x_i_vec); - vst1q_f32(&e_y[i], e_y_i_vec); + vst1q_f32(&curr_vel_x[i], curr_vel_x_i_vec); + vst1q_f32(&curr_vel_y[i], curr_vel_y_i_vec); + vst1q_f32(&w_x[i], w_x_i_vec); + vst1q_f32(&w_y[i], w_y_i_vec); + vst1q_f32(&pos_x[i], pos_x_i_vec); + vst1q_f32(&pos_y[i], pos_y_i_vec); + vst1q_f32(&e_x[i], e_x_i_vec); + vst1q_f32(&e_y[i], e_y_i_vec); vst1q_f32(&destination_y[i], destination_y_i_vec); - vst1q_f32(&sb_sq[i], sb_sq_i_vec); - vst1q_f32(&sb_e_x[i], vmulq_f32(e_x_i_vec, vel_norm_dt)); - vst1q_f32(&sb_e_y[i], vmulq_f32(e_y_i_vec, vel_norm_dt)); + vst1q_f32(&sb_sq[i], sb_sq_i_vec); + vst1q_f32(&sb_e_x[i], vmulq_f32(e_x_i_vec, vel_norm_dt)); + vst1q_f32(&sb_e_y[i], vmulq_f32(e_y_i_vec, vel_norm_dt)); } #if !BENCHMARK @@ -550,4 +530,4 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c #endif return 0; -} \ No newline at end of file +}