Perf plot
This commit is contained in:
committed by
Linus Baumberger
parent
ca969564a6
commit
e7af3103e8
@@ -180,35 +180,41 @@ Pedestrian *init_pedestrians(int n_p) {
|
||||
|
||||
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
|
||||
|
||||
// vectorized exp for a __m256d, no libm call.
|
||||
// idea: exp(x) = 2^n * exp(r), where n = round(x / ln2) and r = x - n*ln2.
|
||||
// after the range reduce, r lives in [-ln(2)/2, ln(2)/2] ~ [-0.347, 0.347], small enough
|
||||
// that degree-7 taylor is accurate to ~1e-9 absolute, way under the 1e-3 verify.py tolerance.
|
||||
// the 2^n part is free: in ieee 754 doubles, 2^n is just (n + 1023) << 52 in the bit pattern.
|
||||
// Schraudolph 1999 for the 2^n bit trick.
|
||||
// ieee 754 double layout (bias 1023, exp slot at bit 52): https://en.wikipedia.org/wiki/Double-precision_floating-point_format
|
||||
// Horner's method for the poly eval: https://en.wikipedia.org/wiki/Horner%27s_method
|
||||
// simd expd, degree-12 taylor on the [-ln2/2, ln2/2] reduced range,
|
||||
// schraudolph 2^n bit trick on the exponent slot.
|
||||
// # 64 MULTs, 56 ADDs
|
||||
static inline __m256d exp256_pd(__m256d x) {
|
||||
// n = round(x / ln(2))
|
||||
const __m256d log2e = _mm256_set1_pd(1.4426950408889634); // 1/ln(2)
|
||||
__m256d nf = _mm256_round_pd(_mm256_mul_pd(x, log2e),
|
||||
_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
|
||||
const __m256d log2e = _mm256_set1_pd(1.4426950408889634);
|
||||
__m256d nf = _mm256_round_pd(_mm256_mul_pd(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC); // # 4 MULTs
|
||||
|
||||
// r = x - n * ln(2)
|
||||
const __m256d ln2 = _mm256_set1_pd(0.6931471805599453);
|
||||
__m256d r = _mm256_fnmadd_pd(nf, ln2, x); // r = x - nf*ln2
|
||||
__m256d r = _mm256_fnmadd_pd(nf, ln2, x); // # 4 MULTs, 4 ADDs
|
||||
|
||||
// poly(r) ~ exp(r). degree-7 taylor, horner from highest coeff downwards.
|
||||
const __m256d c7 = _mm256_set1_pd(1.0 / 5040.0);
|
||||
const __m256d c6 = _mm256_set1_pd(1.0 / 720.0);
|
||||
const __m256d c5 = _mm256_set1_pd(1.0 / 120.0);
|
||||
const __m256d c4 = _mm256_set1_pd(1.0 / 24.0);
|
||||
const __m256d c3 = _mm256_set1_pd(1.0 / 6.0);
|
||||
const __m256d c2 = _mm256_set1_pd(0.5);
|
||||
const __m256d c1 = _mm256_set1_pd(1.0);
|
||||
const __m256d c0 = _mm256_set1_pd(1.0);
|
||||
const __m256d c13 = _mm256_set1_pd(c13_scalar);
|
||||
const __m256d c12 = _mm256_set1_pd(c12_scalar);
|
||||
const __m256d c11 = _mm256_set1_pd(c11_scalar);
|
||||
const __m256d c10 = _mm256_set1_pd(c10_scalar);
|
||||
const __m256d c9 = _mm256_set1_pd(c9_scalar);
|
||||
const __m256d c8 = _mm256_set1_pd(c8_scalar);
|
||||
const __m256d c7 = _mm256_set1_pd(c7_scalar);
|
||||
const __m256d c6 = _mm256_set1_pd(c6_scalar);
|
||||
const __m256d c5 = _mm256_set1_pd(c5_scalar);
|
||||
const __m256d c4 = _mm256_set1_pd(c4_scalar);
|
||||
const __m256d c3 = _mm256_set1_pd(c3_scalar);
|
||||
const __m256d c2 = _mm256_set1_pd(c2_scalar);
|
||||
const __m256d c1 = _mm256_set1_pd(c1_scalar);
|
||||
const __m256d c0 = _mm256_set1_pd(c0_scalar);
|
||||
|
||||
__m256d p = c7;
|
||||
__m256d p = c13;
|
||||
|
||||
// # 13 * (4 MULTs, 4 ADDs)
|
||||
// # = 52 MULTs, 52 ADDs
|
||||
p = _mm256_fmadd_pd(p, r, c12);
|
||||
p = _mm256_fmadd_pd(p, r, c11);
|
||||
p = _mm256_fmadd_pd(p, r, c10);
|
||||
p = _mm256_fmadd_pd(p, r, c9);
|
||||
p = _mm256_fmadd_pd(p, r, c8);
|
||||
p = _mm256_fmadd_pd(p, r, c7);
|
||||
p = _mm256_fmadd_pd(p, r, c6);
|
||||
p = _mm256_fmadd_pd(p, r, c5);
|
||||
p = _mm256_fmadd_pd(p, r, c4);
|
||||
@@ -217,18 +223,13 @@ static inline __m256d exp256_pd(__m256d x) {
|
||||
p = _mm256_fmadd_pd(p, r, c1);
|
||||
p = _mm256_fmadd_pd(p, r, c0);
|
||||
|
||||
// scale by 2^n via bit trick on the exponent field.
|
||||
// nf is already integer-valued from the round above, cvt to int32, sign-extend to int64,
|
||||
// add ieee bias 1023, shift left 52 to land in the exponent slot of a double.
|
||||
// note: for padding lanes (sb_e=0, pos=1e9) n can overflow int32, the resulting pow2n is garbage
|
||||
// but the active mask in the j loop zeroes that lane out before accumulation so it doesn't matter.
|
||||
__m128i ni32 = _mm256_cvtpd_epi32(nf);
|
||||
__m256i ni64 = _mm256_cvtepi32_epi64(ni32);
|
||||
__m128i ni32 = _mm256_cvtpd_epi32(nf);
|
||||
__m256i ni64 = _mm256_cvtepi32_epi64(ni32);
|
||||
__m256i biased = _mm256_add_epi64(ni64, _mm256_set1_epi64x(1023));
|
||||
__m256i scaled = _mm256_slli_epi64(biased, 52);
|
||||
__m256d pow2n = _mm256_castsi256_pd(scaled);
|
||||
__m256d pow2n = _mm256_castsi256_pd(scaled);
|
||||
|
||||
return _mm256_mul_pd(p, pow2n);
|
||||
return _mm256_mul_pd(p, pow2n); // # 4 MULTs
|
||||
}
|
||||
|
||||
int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, const char *dump_path) {
|
||||
@@ -267,15 +268,21 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
|
||||
const __m256d v_zero = _mm256_setzero_pd();
|
||||
const __m256d v_np = _mm256_set1_pd((double)n_p);
|
||||
|
||||
// # flops per iteration (one pass of the outer time loop):
|
||||
// # ADDs: n_p * 12 + n_p * n_p_pad * 29 + n_p * board.n_obstacles * 12 + n_p * 7
|
||||
// # MULTs: n_p * 4 + n_p * n_p_pad * 40 + n_p * board.n_obstacles * 19 + n_p * 16
|
||||
// # DIVs: n_p * n_p_pad * 1 + 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++) {
|
||||
|
||||
double desired_velocity_x = e_x[i] * desired_v0[i];
|
||||
double desired_velocity_y = e_y[i] * desired_v0[i];
|
||||
double desired_velocity_x = e_x[i] * desired_v0[i]; // # 1 MULT
|
||||
double desired_velocity_y = e_y[i] * desired_v0[i]; // # 1 MULT
|
||||
|
||||
double f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv;
|
||||
double f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv;
|
||||
double f_alpha_dt_x = (desired_velocity_x - curr_vel_x[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
|
||||
double f_alpha_dt_y = (desired_velocity_y - curr_vel_y[i]) * dt_times_tau_alpha_inv; // # 1 MULT, 1 ADD
|
||||
|
||||
// since i is fixed for the innermost loops we now wanna set all those variables
|
||||
const __m256d pix = _mm256_set1_pd(pos_x[i]);
|
||||
@@ -289,16 +296,16 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
|
||||
|
||||
for (int j = 0; j < n_p_pad; j += 4) {
|
||||
// 4 lanes at once, lane k maps to pedestrian j+k
|
||||
__m256d j_idx = _mm256_add_pd(_mm256_set1_pd((double)j), lane_off);
|
||||
__m256d j_idx = _mm256_add_pd(_mm256_set1_pd((double)j), lane_off); // # 4 ADDs
|
||||
|
||||
// active = (j+k != i) AND (j+k < n_p), zero in lanes we wanna skip
|
||||
__m256d m_neq_i = _mm256_cmp_pd(j_idx, v_i, _CMP_NEQ_OQ);
|
||||
__m256d m_neq_i = _mm256_cmp_pd(j_idx, v_i, _CMP_NEQ_OQ);
|
||||
__m256d m_in_range = _mm256_cmp_pd(j_idx, v_np, _CMP_LT_OQ);
|
||||
__m256d active = _mm256_and_pd(m_neq_i, m_in_range);
|
||||
__m256d active = _mm256_and_pd(m_neq_i, m_in_range);
|
||||
|
||||
// j-side loads, SoA contiguous. loadu because std::vector only guarantees 16B align.
|
||||
__m256d pjx = _mm256_loadu_pd(&pos_x[j]);
|
||||
__m256d pjy = _mm256_loadu_pd(&pos_y[j]);
|
||||
__m256d pjx = _mm256_loadu_pd(&pos_x[j]);
|
||||
__m256d pjy = _mm256_loadu_pd(&pos_y[j]);
|
||||
__m256d sbe_jx = _mm256_loadu_pd(&sb_e_x[j]);
|
||||
__m256d sbe_jy = _mm256_loadu_pd(&sb_e_y[j]);
|
||||
__m256d sb_jsq = _mm256_loadu_pd(&sb_sq[j]);
|
||||
@@ -309,10 +316,10 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
|
||||
// double r_ab_norm = sqrt(r_ab_x * r_ab_x + r_ab_y * r_ab_y);
|
||||
|
||||
// r_ab = pos_i - pos_j ; |r_ab|
|
||||
__m256d r_ab_x = _mm256_sub_pd(pix, pjx);
|
||||
__m256d r_ab_y = _mm256_sub_pd(piy, pjy);
|
||||
__m256d r_ab_n2 = _mm256_fmadd_pd(r_ab_x, r_ab_x, _mm256_mul_pd(r_ab_y, r_ab_y));
|
||||
__m256d r_ab_n = _mm256_sqrt_pd(r_ab_n2);
|
||||
__m256d r_ab_x = _mm256_sub_pd(pix, pjx); // # 4 ADDs
|
||||
__m256d r_ab_y = _mm256_sub_pd(piy, pjy); // # 4 ADDs
|
||||
__m256d r_ab_n2 = _mm256_fmadd_pd(r_ab_x, r_ab_x, _mm256_mul_pd(r_ab_y, r_ab_y)); // # 8 MULTs, 4 ADDs
|
||||
__m256d r_ab_n = _mm256_sqrt_pd(r_ab_n2); // # 4 SQRTs
|
||||
|
||||
// double r_ab2_x = r_ab_x - sb_e_x[j];
|
||||
// double r_ab2_y = r_ab_y - sb_e_y[j];
|
||||
@@ -320,21 +327,21 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
|
||||
// double r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
|
||||
|
||||
// r_ab2 = r_ab - sb_e_j ; |r_ab2|
|
||||
__m256d r_ab2_x = _mm256_sub_pd(r_ab_x, sbe_jx);
|
||||
__m256d r_ab2_y = _mm256_sub_pd(r_ab_y, sbe_jy);
|
||||
__m256d r_ab2_n2 = _mm256_fmadd_pd(r_ab2_x, r_ab2_x, _mm256_mul_pd(r_ab2_y, r_ab2_y));
|
||||
__m256d r_ab2_n = _mm256_sqrt_pd(r_ab2_n2);
|
||||
__m256d r_ab2_x = _mm256_sub_pd(r_ab_x, sbe_jx); // # 4 ADDs
|
||||
__m256d r_ab2_y = _mm256_sub_pd(r_ab_y, sbe_jy); // # 4 ADDs
|
||||
__m256d r_ab2_n2 = _mm256_fmadd_pd(r_ab2_x, r_ab2_x, _mm256_mul_pd(r_ab2_y, r_ab2_y)); // # 8 MULTs, 4 ADDs
|
||||
__m256d r_ab2_n = _mm256_sqrt_pd(r_ab2_n2); // # 4 SQRTs
|
||||
|
||||
// double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
|
||||
// double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - sb_sq[j];
|
||||
// double two_b = sqrt(two_b_sq);
|
||||
|
||||
// 2b = sqrt((|r_ab| + |r_ab2|)^2 - sb_sq_j)
|
||||
__m256d r_sum = _mm256_add_pd(r_ab_n, r_ab2_n);
|
||||
__m256d two_b_sq = _mm256_fmsub_pd(r_sum, r_sum, sb_jsq);
|
||||
__m256d two_b = _mm256_sqrt_pd(two_b_sq);
|
||||
__m256d r_sum = _mm256_add_pd(r_ab_n, r_ab2_n); // # 4 ADDs
|
||||
__m256d two_b_sq = _mm256_fmsub_pd(r_sum, r_sum, sb_jsq); // # 4 MULTs, 4 ADDs
|
||||
__m256d two_b = _mm256_sqrt_pd(two_b_sq); // # 4 SQRTs
|
||||
|
||||
__m256d e_arg = _mm256_mul_pd(two_b, v_neg_2s_inv);
|
||||
__m256d e_arg = _mm256_mul_pd(two_b, v_neg_2s_inv); // # 4 MULTs
|
||||
// we used to call libm exp() 4 times into a scratch array here, now do it in simd
|
||||
// alignas(32) double tmp_arg[4], tmp_val[4];
|
||||
// _mm256_store_pd(tmp_arg, e_arg);
|
||||
@@ -343,109 +350,109 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
|
||||
// tmp_val[2] = exp(tmp_arg[2]);
|
||||
// tmp_val[3] = exp(tmp_arg[3]);
|
||||
// __m256d e_val = _mm256_load_pd(tmp_val);
|
||||
__m256d e_val = exp256_pd(e_arg);
|
||||
__m256d e_val = exp256_pd(e_arg); // # 64 MULTs, 56 ADDs
|
||||
|
||||
// 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));
|
||||
|
||||
// smc = (r_sum * NEG_V0_AB_div_TWO_SIGMA * e_val) / (two_b * r_ab_n * r_ab2_n)
|
||||
// the i==j lane collapses denom to 0, so we swap in 1.0 there to keep smc finite.
|
||||
// the AND with active zeroes the contribution from that lane anyway.
|
||||
__m256d numer = _mm256_mul_pd(_mm256_mul_pd(r_sum, v_neg_v0_div_2s), e_val);
|
||||
__m256d denom = _mm256_mul_pd(_mm256_mul_pd(two_b, r_ab_n), r_ab2_n);
|
||||
__m256d denom_safe = _mm256_blendv_pd(_mm256_set1_pd(1.0), denom, active);
|
||||
__m256d smc = _mm256_div_pd(numer, denom_safe);
|
||||
__m256d numer = _mm256_mul_pd(_mm256_mul_pd(r_sum, v_neg_v0_div_2s), e_val); // # 8 MULTs
|
||||
__m256d denom = _mm256_mul_pd(_mm256_mul_pd(two_b, r_ab_n), r_ab2_n); // # 8 MULTs
|
||||
__m256d denom_safe = _mm256_blendv_pd(_mm256_set1_pd(1.0), denom, active); // bitwise, no flop
|
||||
__m256d smc = _mm256_div_pd(numer, denom_safe); // # 4 DIVs
|
||||
|
||||
// double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
|
||||
// double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
|
||||
__m256d sm_r_ab = _mm256_mul_pd(smc, r_ab2_n);
|
||||
__m256d sm_r_ab2 = _mm256_mul_pd(smc, r_ab_n);
|
||||
__m256d sm_r_ab = _mm256_mul_pd(smc, r_ab2_n); // # 4 MULTs
|
||||
__m256d sm_r_ab2 = _mm256_mul_pd(smc, r_ab_n); // # 4 MULTs
|
||||
|
||||
// double f_alpha_beta_neg_x = r_ab_x * scalar_mult_r_ab + r_ab2_x * scalar_mult_r_ab2;
|
||||
// double f_alpha_beta_neg_y = r_ab_y * scalar_mult_r_ab + r_ab2_y * scalar_mult_r_ab2;
|
||||
|
||||
// scalar wants r_ab * sm_r_ab + r_ab2 * sm_r_ab2, the previous fma had the layout wrong
|
||||
__m256d f_x = _mm256_fmadd_pd(r_ab2_x, sm_r_ab2, _mm256_mul_pd(r_ab_x, sm_r_ab));
|
||||
__m256d f_y = _mm256_fmadd_pd(r_ab2_y, sm_r_ab2, _mm256_mul_pd(r_ab_y, sm_r_ab));
|
||||
__m256d f_x = _mm256_fmadd_pd(r_ab2_x, sm_r_ab2, _mm256_mul_pd(r_ab_x, sm_r_ab)); // # 8 MULTs, 4 ADDs
|
||||
__m256d f_y = _mm256_fmadd_pd(r_ab2_y, sm_r_ab2, _mm256_mul_pd(r_ab_y, sm_r_ab)); // # 8 MULTs, 4 ADDs
|
||||
|
||||
// w_val = dt_neg by default, c_neg_times_dt when (a >= 0 && a*a > b_sq)
|
||||
__m256d a = _mm256_mul_pd(_mm256_fmadd_pd(eix, f_x, _mm256_mul_pd(eiy, f_y)), v_cos_phi_inv);
|
||||
__m256d b_sq = _mm256_fmadd_pd(f_x, f_x, _mm256_mul_pd(f_y, f_y));
|
||||
__m256d a_sq = _mm256_mul_pd(a, a);
|
||||
__m256d a = _mm256_mul_pd(_mm256_fmadd_pd(eix, f_x, _mm256_mul_pd(eiy, f_y)), v_cos_phi_inv); // # 12 MULTs, 4 ADDs
|
||||
__m256d b_sq = _mm256_fmadd_pd(f_x, f_x, _mm256_mul_pd(f_y, f_y)); // # 8 MULTs, 4 ADDs
|
||||
__m256d a_sq = _mm256_mul_pd(a, a); // # 4 MULTs
|
||||
|
||||
__m256d c1 = _mm256_cmp_pd(a, v_zero, _CMP_GE_OQ);
|
||||
__m256d c2 = _mm256_cmp_pd(a_sq, b_sq, _CMP_GT_OQ);
|
||||
__m256d cond = _mm256_and_pd(c1, c2);
|
||||
__m256d c1 = _mm256_cmp_pd(a, v_zero, _CMP_GE_OQ);
|
||||
__m256d c2 = _mm256_cmp_pd(a_sq, b_sq, _CMP_GT_OQ);
|
||||
__m256d cond = _mm256_and_pd(c1, c2);
|
||||
__m256d w_val = _mm256_blendv_pd(v_dt_neg, v_c_neg_dt, cond);
|
||||
|
||||
// zero out inactive lanes (i==j and padding) before accumulating.
|
||||
// AND with all-zero mask = 0, AND with all-one mask passes through. cheaper than blendv here.
|
||||
__m256d f_x_safe = _mm256_and_pd(f_x, active);
|
||||
__m256d f_y_safe = _mm256_and_pd(f_y, active);
|
||||
acc_x = _mm256_fmadd_pd(f_x_safe, w_val, acc_x);
|
||||
acc_y = _mm256_fmadd_pd(f_y_safe, w_val, acc_y);
|
||||
__m256d f_x_safe = _mm256_and_pd(f_x, active); // bitwise, no flop
|
||||
__m256d f_y_safe = _mm256_and_pd(f_y, active); // bitwise, no flop
|
||||
acc_x = _mm256_fmadd_pd(f_x_safe, w_val, acc_x); // # 4 MULTs, 4 ADDs
|
||||
acc_y = _mm256_fmadd_pd(f_y_safe, w_val, acc_y); // # 4 MULTs, 4 ADDs
|
||||
}
|
||||
|
||||
// horizontal sum acc_x, acc_y into the scalar f_alpha_dt
|
||||
__m256d hsum_xy = _mm256_hadd_pd(acc_x, acc_y); // [x0+x1, y0+y1, x2+x3, y2+y3]
|
||||
__m128d lo = _mm256_castpd256_pd128(hsum_xy);
|
||||
__m128d hi = _mm256_extractf128_pd(hsum_xy, 1);
|
||||
__m128d sum_xy = _mm_add_pd(lo, hi); // [x_total, y_total]
|
||||
__m256d hsum_xy = _mm256_hadd_pd(acc_x, acc_y); // [x0+x1, y0+y1, x2+x3, y2+y3] // # 4 ADDs
|
||||
__m128d lo = _mm256_castpd256_pd128(hsum_xy);
|
||||
__m128d hi = _mm256_extractf128_pd(hsum_xy, 1);
|
||||
__m128d sum_xy = _mm_add_pd(lo, hi); // [x_total, y_total] // # 2 ADDs
|
||||
alignas(16) double sum_out[2];
|
||||
_mm_store_pd(sum_out, sum_xy);
|
||||
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++) {
|
||||
|
||||
double v1_x = pos_x[i] - board.obstacles[k].start.x;
|
||||
double v1_y = pos_y[i] - board.obstacles[k].start.y;
|
||||
double v1_x = pos_x[i] - board.obstacles[k].start.x; // # 1 ADD
|
||||
double v1_y = pos_y[i] - board.obstacles[k].start.y; // # 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;
|
||||
double projection = (v1_x * board.obstacles[k].obstacle_v.x + v1_y * board.obstacles[k].obstacle_v.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;
|
||||
|
||||
double r_aB_x = pos_x[i] - (board.obstacles[k].start.x + board.obstacles[k].obstacle_v.x * projection);
|
||||
double r_aB_y = pos_y[i] - (board.obstacles[k].start.y + board.obstacles[k].obstacle_v.y * projection);
|
||||
double r_aB_x = pos_x[i] - (board.obstacles[k].start.x + board.obstacles[k].obstacle_v.x * projection); // # 1 MULT, 2 ADDs
|
||||
double r_aB_y = pos_y[i] - (board.obstacles[k].start.y + board.obstacles[k].obstacle_v.y * projection); // # 1 MULT, 2 ADDs
|
||||
|
||||
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 r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y); // # 2 MULTs, 1 ADD, 1 SQRT
|
||||
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm; // # 2 MULTs, 1 EXP, 1 DIV
|
||||
double f_alpha_B_neg_x = r_aB_x * scalar_mult; // # 1 MULT
|
||||
double f_alpha_B_neg_y = r_aB_y * scalar_mult; // # 1 MULT
|
||||
|
||||
double w_val;
|
||||
double a = (e_x[i] * f_alpha_B_neg_x + e_y[i] * 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;
|
||||
double a = (e_x[i] * f_alpha_B_neg_x + e_y[i] * f_alpha_B_neg_y) * cos_phi_inv; // # 3 MULTs, 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; // # 2 MULTs, 1 ADD
|
||||
|
||||
w_val = dt_neg;
|
||||
if (a >= 0 && (a * a > b_sq)) {
|
||||
if (a >= 0 && (a * a > b_sq)) { // # 1 MULT for a*a
|
||||
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;
|
||||
double f_alpha_B_dt_x = f_alpha_B_neg_x * w_val; // # 1 MULT
|
||||
double 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++) {
|
||||
|
||||
double w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]);
|
||||
double w_norm = sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]); // # 2 MULTs, 1 ADD, 1 SQRT
|
||||
|
||||
double correction_factor = min(1.0, v_max[i] / w_norm);
|
||||
double correction_factor = min(1.0, v_max[i] / w_norm); // # 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;
|
||||
@@ -466,22 +473,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);
|
||||
|
||||
double ex = destination_x[i] - pos_x[i];
|
||||
double ey = destination_y[i] - pos_y[i];
|
||||
double ex = destination_x[i] - pos_x[i]; // # 1 ADD
|
||||
double ey = destination_y[i] - pos_y[i]; // # 1 ADD
|
||||
|
||||
double e_norm_inv = 1.0 / sqrt(ex * ex + ey * ey);
|
||||
double e_norm_inv = 1.0 / sqrt(ex * ex + ey * ey); // # 2 MULTs, 1 ADD, 1 SQRT, 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
|
||||
|
||||
double velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i];
|
||||
double velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i]; // # 2 MULTs, 1 ADD
|
||||
|
||||
double velocity_norm = sqrt(velocity_norm_sq);
|
||||
double velocity_norm = sqrt(velocity_norm_sq); // # 1 SQRT
|
||||
|
||||
sb_sq[i] = velocity_norm_sq * delta_t_sq;
|
||||
sb_sq[i] = velocity_norm_sq * delta_t_sq; // # 1 MULT
|
||||
|
||||
sb_e_x[i] = e_x[i] * (velocity_norm * delta_t);
|
||||
sb_e_y[i] = e_y[i] * (velocity_norm * delta_t);
|
||||
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)) {
|
||||
|
||||
Reference in New Issue
Block a user