Merge branch 'elliot/flop-count' into 'main'
Flop Count Calculations + x86 Measurements + c13 Fix See merge request course-asl-fastcode26/team70!60
This commit is contained in:
@@ -0,0 +1,568 @@
|
||||
// # This file attempts to use AVX-512 intrinsics to speed up computation.
|
||||
// # Predecessor: variant_inlining_7.cpp
|
||||
#ifdef __AVX512F__
|
||||
|
||||
#include "../headers/board_reduced.hpp"
|
||||
#include "../headers/constants.hpp"
|
||||
#include "../headers/pedestrian_reduced.hpp"
|
||||
|
||||
#include "immintrin.h"
|
||||
#include "sleef.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#include <immintrin.h>
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <random>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#define SEED 42
|
||||
#define BOARD_WIDTH 10.0
|
||||
#define BOARD_LENGTH 50.0
|
||||
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
|
||||
#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<double> velocity_gen(v0_mean, v0_std_dev);
|
||||
uniform_real_distribution<double> y_gen(0.3, BOARD_WIDTH - 0.3);
|
||||
uniform_real_distribution<double> x_gen_left(2.0, BOARD_LENGTH / 2.0);
|
||||
uniform_real_distribution<double> 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<tuple<int, int, double, double, double, double, 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++) {
|
||||
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
|
||||
|
||||
// # ADDs: n_iter * n_p * 4 +
|
||||
// # n_iter * n_p * ceil(((double) (n_p - 7)) / 8) * (1 / (ceil(((double) (n_p - 7)) / 8))) * 7 * 14 +
|
||||
// # n_iter * n_p * ceil(((double) (n_p - 7)) / 8) * (1 - 1 / (ceil(((double) (n_p - 7)) / 8))) * 224 +
|
||||
// # n_iter * n_p * (n_p - floor((double) n_p / 8) * 8) * 14 +
|
||||
// # n_iter * n_p * board.n_obstacles * 12 +
|
||||
// # n_iter * n_p * 7
|
||||
|
||||
// # MULTs: n_iter * n_p * 4 +
|
||||
// # n_iter * n_p * ceil(((double) (n_p - 7)) / 8) * (1 / (ceil(((double) (n_p - 7)) / 8))) * 7 * 24 +
|
||||
// # n_iter * n_p * ceil(((double) (n_p - 7)) / 8) * (1 - 1 / (ceil(((double) (n_p - 7)) / 8))) * 352 +
|
||||
// # n_iter * n_p * (n_p - floor((double) n_p / 8) * 8) * 24 +
|
||||
// # n_iter * n_p * board.n_obstacles * 19 +
|
||||
// # n_iter * n_p * 17
|
||||
|
||||
// # DIVs: n_iter * n_p * ceil(((double) (n_p - 7)) / 8) * (1 / (ceil(((double) (n_p - 7)) / 8))) * 7 +
|
||||
// # n_iter * n_p * ceil(((double) (n_p - 7)) / 8) * (1 - 1 / (ceil(((double) (n_p - 7)) / 8))) * 8 +
|
||||
// # n_iter * n_p * (n_p - floor((double) n_p / 8) * 8) +
|
||||
// # n_iter * n_p * board.n_obstacles +
|
||||
// # n_iter * n_p * 2
|
||||
|
||||
// # SQRTs: n_iter * n_p * ceil(((double) (n_p - 7)) / 8) * (1 / (ceil(((double) (n_p - 7)) / 8))) * 7 * 3 +
|
||||
// # n_iter * n_p * ceil(((double) (n_p - 7)) / 8) * (1 - 1 / (ceil(((double) (n_p - 7)) / 8))) * 24 +
|
||||
// # n_iter * n_p * (n_p - floor((double) n_p / 8) * 8) * 3 +
|
||||
// # n_iter * n_p * board.n_obstacles +
|
||||
// # n_iter * n_p * 3
|
||||
|
||||
// # EXPs: n_iter * n_p * ceil(((double) (n_p - 7)) / 8) * (1 / (ceil(((double) (n_p - 7)) / 8))) * 7 +
|
||||
// # n_iter * n_p * (n_p - floor((double) n_p / 8) * 8) +
|
||||
// # n_iter * n_p * board.n_obstacles
|
||||
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
Pedestrian &p = pedestrians[i];
|
||||
|
||||
// # 2 MULTs
|
||||
double desired_velocity_x = p.e_x * p.desired_v0;
|
||||
double desired_velocity_y = p.e_y * p.desired_v0;
|
||||
|
||||
// # 2 ADDs, 2 MULTs
|
||||
double f_alpha_dt_x = (desired_velocity_x - p.curr_vel_x) * dt_times_tau_alpha_inv;
|
||||
double f_alpha_dt_y = (desired_velocity_y - p.curr_vel_y) * dt_times_tau_alpha_inv;
|
||||
|
||||
__m512d desired_velocity_x_vec = _mm512_set1_pd(desired_velocity_x);
|
||||
__m512d desired_velocity_y_vec = _mm512_set1_pd(desired_velocity_y);
|
||||
__m512d f_alpha_dt_x_vec = _mm512_set1_pd(f_alpha_dt_x);
|
||||
__m512d f_alpha_dt_y_vec = _mm512_set1_pd(f_alpha_dt_y);
|
||||
|
||||
__m512d alpha_curr_pos_x_vec = _mm512_set1_pd(p.curr_pos_x);
|
||||
__m512d alpha_curr_pos_y_vec = _mm512_set1_pd(p.curr_pos_y);
|
||||
__m512d alpha_e_x_vec = _mm512_set1_pd(p.e_x);
|
||||
__m512d alpha_e_y_vec = _mm512_set1_pd(p.e_y);
|
||||
|
||||
int j;
|
||||
|
||||
// # ############################################################################################################################################################################################
|
||||
|
||||
for (j = 0; j < n_p - 7; j += 8) {
|
||||
|
||||
if (j <= i && i <= j + 7) {
|
||||
|
||||
for (int jj = j; jj < j + 8; jj++) {
|
||||
|
||||
if (jj == i) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
double r_ab_x = p.curr_pos_x - pedestrians[jj].curr_pos_x;
|
||||
double r_ab_y = p.curr_pos_y - pedestrians[jj].curr_pos_y;
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double r_ab_norm = sqrt(r_ab_x * r_ab_x + r_ab_y * r_ab_y);
|
||||
|
||||
// # 2 ADDs
|
||||
double r_ab2_x = r_ab_x - pedestrians[jj].s_b_e_x;
|
||||
double r_ab2_y = r_ab_y - pedestrians[jj].s_b_e_y;
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
|
||||
|
||||
// # 2 ADDs, 1 MULT
|
||||
double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
|
||||
double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - pedestrians[jj].s_b_sq;
|
||||
|
||||
// # 1 SQRT
|
||||
double two_b = sqrt(two_b_sq);
|
||||
|
||||
// # 5 MULTs, 1 DIV, 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));
|
||||
|
||||
// # 2 MULTs
|
||||
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
|
||||
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
|
||||
|
||||
// # 4 MULTs, 2 ADDs
|
||||
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;
|
||||
|
||||
double w_val;
|
||||
|
||||
// # 3 MULTs, 1 ADD
|
||||
double a = (p.e_x * f_alpha_beta_neg_x + p.e_y * f_alpha_beta_neg_y) * cos_phi_inv;
|
||||
|
||||
// # 2 MULTs, 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 MULT
|
||||
if (a >= 0 && (a * a > b_sq)) {
|
||||
w_val = c_neg_times_dt;
|
||||
}
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
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;
|
||||
}
|
||||
|
||||
} else {
|
||||
__m512d beta_curr_pos_x_vec = _mm512_setr_pd(pedestrians[j].curr_pos_x, pedestrians[j + 1].curr_pos_x, pedestrians[j + 2].curr_pos_x, pedestrians[j + 3].curr_pos_x, pedestrians[j + 4].curr_pos_x, pedestrians[j + 5].curr_pos_x, pedestrians[j + 6].curr_pos_x, pedestrians[j + 7].curr_pos_x);
|
||||
__m512d beta_curr_pos_y_vec = _mm512_setr_pd(pedestrians[j].curr_pos_y, pedestrians[j + 1].curr_pos_y, pedestrians[j + 2].curr_pos_y, pedestrians[j + 3].curr_pos_y, pedestrians[j + 4].curr_pos_y, pedestrians[j + 5].curr_pos_y, pedestrians[j + 6].curr_pos_y, pedestrians[j + 7].curr_pos_y);
|
||||
|
||||
// # 16 ADDs
|
||||
__m512d r_ab_x_vec = _mm512_sub_pd(alpha_curr_pos_x_vec, beta_curr_pos_x_vec);
|
||||
__m512d r_ab_y_vec = _mm512_sub_pd(alpha_curr_pos_y_vec, beta_curr_pos_y_vec);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d r_ab_y_mul_r_ab_y_vec = _mm512_mul_pd(r_ab_y_vec, r_ab_y_vec);
|
||||
|
||||
// # 8 MULTs, 8 ADDs
|
||||
__m512d r_ab_norm_sq_vec = _mm512_fmadd_pd(r_ab_x_vec, r_ab_x_vec, r_ab_y_mul_r_ab_y_vec);
|
||||
|
||||
// # 8 SQRTs
|
||||
__m512d r_ab_norm_vec = _mm512_sqrt_pd(r_ab_norm_sq_vec);
|
||||
|
||||
__m512d beta_s_b_e_x_vec = _mm512_setr_pd(pedestrians[j].s_b_e_x, pedestrians[j + 1].s_b_e_x, pedestrians[j + 2].s_b_e_x, pedestrians[j + 3].s_b_e_x, pedestrians[j + 4].s_b_e_x, pedestrians[j + 5].s_b_e_x, pedestrians[j + 6].s_b_e_x, pedestrians[j + 7].s_b_e_x);
|
||||
|
||||
// # 8 ADDs
|
||||
__m512d r_ab2_x_vec = _mm512_sub_pd(r_ab_x_vec, beta_s_b_e_x_vec);
|
||||
|
||||
__m512d beta_s_b_e_y_vec = _mm512_setr_pd(pedestrians[j].s_b_e_y, pedestrians[j + 1].s_b_e_y, pedestrians[j + 2].s_b_e_y, pedestrians[j + 3].s_b_e_y, pedestrians[j + 4].s_b_e_y, pedestrians[j + 5].s_b_e_y, pedestrians[j + 6].s_b_e_y, pedestrians[j + 7].s_b_e_y);
|
||||
|
||||
// # 8 ADDs
|
||||
__m512d r_ab2_y_vec = _mm512_sub_pd(r_ab_y_vec, beta_s_b_e_y_vec);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d r_ab2_y_mul_r_ab2_y_vec = _mm512_mul_pd(r_ab2_y_vec, r_ab2_y_vec);
|
||||
|
||||
// # 8 MULTs, 8 ADDs
|
||||
__m512d r_ab2_norm_sq_vec = _mm512_fmadd_pd(r_ab2_x_vec, r_ab2_x_vec, r_ab2_y_mul_r_ab2_y_vec);
|
||||
|
||||
// # 8 SQRTs
|
||||
__m512d r_ab2_norm_vec = _mm512_sqrt_pd(r_ab2_norm_sq_vec);
|
||||
|
||||
// # 8 ADDs
|
||||
__m512d r_ab_norm_sum_vec = _mm512_add_pd(r_ab_norm_vec, r_ab2_norm_vec);
|
||||
|
||||
__m512d beta_s_b_sq_vec = _mm512_setr_pd(pedestrians[j].s_b_sq, pedestrians[j + 1].s_b_sq, pedestrians[j + 2].s_b_sq, pedestrians[j + 3].s_b_sq, pedestrians[j + 4].s_b_sq, pedestrians[j + 5].s_b_sq, pedestrians[j + 6].s_b_sq, pedestrians[j + 7].s_b_sq);
|
||||
|
||||
// # 8 MULTs, 8 ADDs
|
||||
__m512d two_b_sq_vec = _mm512_fmsub_pd(r_ab_norm_sum_vec, r_ab_norm_sum_vec, beta_s_b_sq_vec);
|
||||
|
||||
// # 8 SQRTs
|
||||
__m512d two_b_vec = _mm512_sqrt_pd(two_b_sq_vec);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d two_b_mul_r_ab_norm_vec = _mm512_mul_pd(two_b_vec, r_ab_norm_vec);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d denom_vec = _mm512_mul_pd(two_b_mul_r_ab_norm_vec, r_ab2_norm_vec);
|
||||
|
||||
// # 8 DIVs
|
||||
__m512d scalar_mult_common_left_vec = _mm512_div_pd(r_ab_norm_sum_vec, denom_vec);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d two_b_mul_NEG_TWO_SIGMA_INV_vec = _mm512_mul_pd(two_b_vec, NEG_TWO_SIGMA_INV_VEC);
|
||||
|
||||
// # 160 MULTs, 112 ADDs
|
||||
// objdump -d --demangle /usr/lib/x86_64-linux-gnu/libsleef.so | grep -A 67 "Sleef_expd8_u10@@Base"
|
||||
__m512d e_vec = Sleef_expd8_u10(two_b_mul_NEG_TWO_SIGMA_INV_vec);
|
||||
|
||||
// # 16 MULTs
|
||||
__m512d scalar_mult_common_right_vec = _mm512_mul_pd(NEG_V0_AB_DIV_TWO_SIGMA_VEC, e_vec);
|
||||
__m512d scalar_mult_common_vec = _mm512_mul_pd(scalar_mult_common_left_vec, scalar_mult_common_right_vec);
|
||||
|
||||
// # 16 MULTs
|
||||
__m512d scalar_mult_r_ab_vec = _mm512_mul_pd(scalar_mult_common_vec, r_ab2_norm_vec);
|
||||
__m512d scalar_mult_r_ab2_vec = _mm512_mul_pd(scalar_mult_common_vec, r_ab_norm_vec);
|
||||
|
||||
// # 16 MULTs
|
||||
__m512d r_ab2_x_mul_scalar_mult_r_ab2_vec = _mm512_mul_pd(r_ab2_x_vec, scalar_mult_r_ab2_vec);
|
||||
__m512d r_ab2_y_mul_scalar_mult_r_ab2_vec = _mm512_mul_pd(r_ab2_y_vec, scalar_mult_r_ab2_vec);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
__m512d f_alpha_beta_neg_x_vec = _mm512_fmadd_pd(r_ab_x_vec, scalar_mult_r_ab_vec, r_ab2_x_mul_scalar_mult_r_ab2_vec);
|
||||
__m512d f_alpha_beta_neg_y_vec = _mm512_fmadd_pd(r_ab_y_vec, scalar_mult_r_ab_vec, r_ab2_y_mul_scalar_mult_r_ab2_vec);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d rgt_vec = _mm512_mul_pd(alpha_e_y_vec, f_alpha_beta_neg_y_vec);
|
||||
|
||||
// # 8 MULTs, 8 ADDs
|
||||
__m512d lft_add_rgt_vec = _mm512_fmadd_pd(alpha_e_x_vec, f_alpha_beta_neg_x_vec, rgt_vec);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d a_vec = _mm512_mul_pd(lft_add_rgt_vec, cos_phi_inv_vec);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d f_alpha_beta_neg_y_sq_vec = _mm512_mul_pd(f_alpha_beta_neg_y_vec, f_alpha_beta_neg_y_vec);
|
||||
|
||||
// # 8 MULTs, 8 ADDs
|
||||
__m512d b_sq_vec = _mm512_fmadd_pd(f_alpha_beta_neg_x_vec, f_alpha_beta_neg_x_vec, f_alpha_beta_neg_y_sq_vec);
|
||||
|
||||
__m512d w_val_vec = dt_neg_vec;
|
||||
__mmask8 a_ge_zero_mask = _mm512_cmp_pd_mask(a_vec, ZERO_VEC, _CMP_GE_OQ);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d a_sq_vec = _mm512_mul_pd(a_vec, a_vec);
|
||||
|
||||
__mmask8 a_sq_gt_b_sq_mask = _mm512_cmp_pd_mask(a_sq_vec, b_sq_vec, _CMP_GT_OQ);
|
||||
__mmask8 cond_mask = _kand_mask8(a_ge_zero_mask, a_sq_gt_b_sq_mask);
|
||||
w_val_vec = _mm512_mask_mov_pd(w_val_vec, cond_mask, c_neg_times_dt_vec);
|
||||
|
||||
// # 16 MULTs
|
||||
__m512d x_term_vec = _mm512_mul_pd(f_alpha_beta_neg_x_vec, w_val_vec);
|
||||
__m512d y_term_vec = _mm512_mul_pd(f_alpha_beta_neg_y_vec, w_val_vec);
|
||||
|
||||
// # 16 ADDs
|
||||
f_alpha_dt_x += _mm512_reduce_add_pd(x_term_vec);
|
||||
f_alpha_dt_y += _mm512_reduce_add_pd(y_term_vec);
|
||||
}
|
||||
}
|
||||
|
||||
// # ############################################################################################################################################################################################
|
||||
|
||||
for (; j < n_p; j++) {
|
||||
|
||||
if (j == i) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
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;
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double r_ab_norm = sqrt(r_ab_x * r_ab_x + r_ab_y * r_ab_y);
|
||||
|
||||
// # 2 ADDs
|
||||
double r_ab2_x = r_ab_x - pedestrians[j].s_b_e_x;
|
||||
double r_ab2_y = r_ab_y - pedestrians[j].s_b_e_y;
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
|
||||
|
||||
// # 1 ADD
|
||||
double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
|
||||
|
||||
// # 1 MULT, 1 ADD
|
||||
double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - pedestrians[j].s_b_sq;
|
||||
|
||||
// # 1 SQRT
|
||||
double two_b = sqrt(two_b_sq);
|
||||
|
||||
// # 5 MULTs, 1 DIV, 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));
|
||||
|
||||
// # 2 MULTs
|
||||
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
|
||||
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
|
||||
|
||||
// # 4 MULTs, 2 ADDs
|
||||
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;
|
||||
|
||||
double w_val;
|
||||
|
||||
// # 3 MULTs, 1 ADD
|
||||
double a = (p.e_x * f_alpha_beta_neg_x + p.e_y * f_alpha_beta_neg_y) * cos_phi_inv;
|
||||
|
||||
// # 2 MULTs, 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 MULT
|
||||
if (a >= 0 && (a * a > b_sq)) {
|
||||
w_val = c_neg_times_dt;
|
||||
}
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
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;
|
||||
}
|
||||
|
||||
// # ############################################################################################################################################################################################
|
||||
|
||||
for (int k = 0; k < board.n_obstacles; k++) {
|
||||
|
||||
// # 2 ADDs
|
||||
double v1_x = p.curr_pos_x - board.obstacles[k].start_x;
|
||||
double v1_y = p.curr_pos_y - board.obstacles[k].start_y;
|
||||
|
||||
// # 3 MULTs, 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;
|
||||
|
||||
// # 2 MULTs, 4 ADDs
|
||||
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);
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
|
||||
|
||||
// # 2 MULTs, 1 EXP, 1 DIV
|
||||
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
|
||||
|
||||
// # 2 MULTs
|
||||
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;
|
||||
|
||||
// # 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;
|
||||
|
||||
// # 2 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;
|
||||
|
||||
w_val = dt_neg;
|
||||
|
||||
// # 1 MULT
|
||||
if (a >= 0 && (a * a > b_sq)) {
|
||||
w_val = c_neg_times_dt;
|
||||
}
|
||||
|
||||
// # 2 MULTs
|
||||
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;
|
||||
|
||||
// # 2 ADDs
|
||||
f_alpha_dt_x += f_alpha_B_dt_x;
|
||||
f_alpha_dt_y += f_alpha_B_dt_y;
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
p.w_x += f_alpha_dt_x;
|
||||
p.w_y += f_alpha_dt_y;
|
||||
}
|
||||
|
||||
// # ############################################################################################################################################################################################
|
||||
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
Pedestrian &p = pedestrians[i];
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double w_norm = sqrt(p.w_x * p.w_x + p.w_y * p.w_y);
|
||||
|
||||
// # 1 DIV
|
||||
double correction_factor = min(1.0, p.v_max / w_norm);
|
||||
|
||||
// # 2 MULTs
|
||||
p.curr_vel_x = p.w_x * correction_factor;
|
||||
p.curr_vel_y = p.w_y * correction_factor;
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
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_MINUS_ONE_POINT_FIVE) {
|
||||
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_MINUS_ONE;
|
||||
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);
|
||||
|
||||
// # 2 ADDs
|
||||
double e_x = p.destination_x - p.curr_pos_x;
|
||||
double e_y = p.destination_y - p.curr_pos_y;
|
||||
|
||||
// # 1 DIV, 1 SQRT, 2 MULTs, 1 ADD
|
||||
double e_norm_inv = 1.0 / sqrt(e_x * e_x + e_y * e_y);
|
||||
|
||||
// # 2 MULTs
|
||||
p.e_x = e_x * e_norm_inv;
|
||||
p.e_y = e_y * e_norm_inv;
|
||||
|
||||
// # 2 MULTs, 1 ADD
|
||||
double velocity_norm_sq = p.curr_vel_x * p.curr_vel_x + p.curr_vel_y * p.curr_vel_y;
|
||||
|
||||
// # 1 SQRT
|
||||
double velocity_norm = sqrt(velocity_norm_sq);
|
||||
|
||||
// # 5 MULTs
|
||||
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;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,440 @@
|
||||
// This file is merged from the forces.cpp and social_forces_baseline.cpp
|
||||
// The goal is that we from now on only have one file as our baseline
|
||||
// This should make it more easy in the future to implement optimizations
|
||||
// Our naming convention from now on is: variant_<variant_name>.cpp (Please make sure to keep that consistent, otherwise our Makefile wont work anymore)
|
||||
|
||||
#include "../headers/board.hpp"
|
||||
#include "../headers/constants.hpp"
|
||||
#include "../headers/pedestrian.hpp"
|
||||
#include "../headers/vector2d.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <math.h> // keep — forces code uses unqualified sqrt/exp
|
||||
#include <random>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#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;
|
||||
|
||||
// forces.cpp
|
||||
|
||||
// Flop count: 2 ADDs, 6 MULTs
|
||||
double w_ped(Vector2d e, Vector2d f) {
|
||||
double f_norm_sq = f.dot(f);
|
||||
double rhs = f_norm_sq * cos_phi_sq;
|
||||
double e_dot_f = e.dot(f);
|
||||
double e_dot_f_sq = e_dot_f * e_dot_f;
|
||||
if (e_dot_f >= 0.0 || e_dot_f_sq <= rhs)
|
||||
return dt_neg;
|
||||
else
|
||||
return c_neg_times_dt;
|
||||
}
|
||||
|
||||
double w_obstacle(Vector2d e, Vector2d f) {
|
||||
double e_dot_f = (f.y == 0) ? 0 : (f.y < 0 ? -e.y : e.y);
|
||||
if (e_dot_f >= cos_phi)
|
||||
return dt_neg;
|
||||
else
|
||||
return c_neg_times_dt;
|
||||
}
|
||||
|
||||
// Flop count: 2 ADDs, 4 MULTs
|
||||
Vector2d acceleration_term_dt(const Pedestrian &alpha) {
|
||||
|
||||
// # 2 MULTs
|
||||
Vector2d desired_velocity = alpha.e * alpha.desired_v0;
|
||||
|
||||
Vector2d actual_velocity = alpha.curr_vel;
|
||||
|
||||
// # 2 ADDs, 2 MULTs
|
||||
Vector2d f_alpha_0_dt = (desired_velocity - actual_velocity) * dt_times_tau_alpha_inv;
|
||||
|
||||
return f_alpha_0_dt;
|
||||
}
|
||||
|
||||
// Flop count: 12 ADDs, 24 MULTs, 1 DIV, 3 SQRTs, 1 EXP
|
||||
Vector2d pedestrian_repulsion_dt(const Pedestrian &alpha, const Pedestrian &beta) {
|
||||
|
||||
// # 2 ADDs
|
||||
// $\vec{r}_{\alpha\beta}$
|
||||
Vector2d r_ab = alpha.curr_pos - beta.curr_pos;
|
||||
|
||||
// # 1 ADD, 2 MULTs, 1 SQRT
|
||||
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert$
|
||||
double r_ab_norm = r_ab.norm();
|
||||
r_ab_norm = r_ab_norm > EPS ? r_ab_norm : EPS; // Avoid division by zero
|
||||
|
||||
// $v_\beta \Delta t \vec{e}_\beta$
|
||||
Vector2d s_b_e = beta.s_b_e;
|
||||
|
||||
// # 2 ADDs
|
||||
// $\vec{r}_{\alpha\beta} - \vec{v}_{\beta}\varDelta_{t}\vec{e}_{\beta}$
|
||||
Vector2d r_ab2 = r_ab - s_b_e;
|
||||
|
||||
// # 1 ADD, 2 MULTs, 1 SQRT
|
||||
// $\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
|
||||
double r_ab2_norm = r_ab2.norm();
|
||||
r_ab2_norm = r_ab2_norm > EPS ? r_ab2_norm : EPS; // Avoid division by zero
|
||||
|
||||
// # 1 ADD
|
||||
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
|
||||
double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
|
||||
|
||||
// # 1 ADD, 1 MULTs, 1 SQRT
|
||||
//
|
||||
// $2b = \sqrt{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)^2 - (v_\beta \Delta t)^2}$
|
||||
//
|
||||
double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - beta.s_b_sq;
|
||||
two_b_sq = two_b_sq > EPS_SQ ? two_b_sq : EPS_SQ; // Avoid sqrt of negative and avoid division by zero
|
||||
double two_b = sqrt(two_b_sq);
|
||||
|
||||
// # 5 MULTs, 1 DIV, 1 EXP
|
||||
//
|
||||
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}$
|
||||
//
|
||||
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 MULT
|
||||
//
|
||||
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}$
|
||||
//
|
||||
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
|
||||
|
||||
// # 1 MULT
|
||||
//
|
||||
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}$
|
||||
//
|
||||
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
|
||||
|
||||
// # 2 ADDs, 4 MULTs
|
||||
//
|
||||
// $\vec{-f}_{\alpha \beta}(\vec{r}_{\alpha \beta}) = \frac{-V^0_{\alpha \beta}}{2\sigma}e^{-b / \sigma} 2\grad_{\vec{r}_{\alpha \beta}}b $
|
||||
//
|
||||
//
|
||||
// $= \frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}\,\vec{r}_{\alpha \beta} + \frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} -
|
||||
// v_\beta \Delta t \vec{e}_\beta \Arrowvert}\,(\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta)$
|
||||
//
|
||||
//
|
||||
//
|
||||
// $2\grad_{\vec{r}_{\alpha \beta}}b = \frac{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)(\frac{\vec{r}_{\alpha \beta}}{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert} + \frac{\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta}{\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert})}{2b}$
|
||||
//
|
||||
//
|
||||
// $= \frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}\,\vec{r}_{\alpha \beta} + \frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}\,(\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta)$
|
||||
//
|
||||
//
|
||||
Vector2d f_alpha_beta_neg = r_ab * scalar_mult_r_ab + r_ab2 * scalar_mult_r_ab2;
|
||||
|
||||
// # 2 ADDs, 8 MULTs
|
||||
//
|
||||
// $\vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha \beta}).dt = w(\vec{e}_{\alpha}, -\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta})).\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta}).dt$
|
||||
//
|
||||
Vector2d f_alpha_beta_dt = f_alpha_beta_neg * w_ped(alpha.e, f_alpha_beta_neg);
|
||||
|
||||
return f_alpha_beta_dt;
|
||||
}
|
||||
|
||||
// Flop count: 8 ADDs, 13 MULTs, 1 DIV, 1 SQRT, 1 EXP
|
||||
Vector2d obstacle_repulsion_dt(const Pedestrian &alpha, const Obstacle &B) {
|
||||
|
||||
// # 7 ADDs, 5 MULTs
|
||||
// $\vec{r}_{\alpha B}$
|
||||
Vector2d r_aB = alpha.curr_pos - B.find_nearest_obstacle_point(alpha.curr_pos);
|
||||
|
||||
// # 1 ADD, 2 MULTs, 1 SQRT
|
||||
// $\Arrowvert \vec{r}_{\alpha B} \Arrowvert$
|
||||
double r_aB_norm = r_aB.norm();
|
||||
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS; // Avoid division by zero
|
||||
|
||||
// # 2 MULTs, 1 DIV, 1 EXP
|
||||
//
|
||||
// $\frac{-U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R}$
|
||||
//
|
||||
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
|
||||
|
||||
// # 2 MULTs
|
||||
//
|
||||
// $-\vec{F}_{\alpha B}(\vec{r}_{\alpha B}) = \frac{-U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R} \frac{\vec{r}_{\alpha B}}{\Arrowvert \vec{r}_{\alpha B} \Arrowvert}$
|
||||
//
|
||||
Vector2d f_alpha_B_neg = r_aB * scalar_mult;
|
||||
|
||||
// # 2 MULTs
|
||||
//
|
||||
// $\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha B}).dt = w(\vec{e}_{\alpha}, -\vec{F}_{\alpha B}(\vec{r}_{\alpha B})).\vec{F}_{\alpha B}(\vec{r}_{\alpha B}).dt$
|
||||
//
|
||||
Vector2d f_alpha_B_dt = f_alpha_B_neg * w_obstacle(alpha.e, f_alpha_B_neg);
|
||||
|
||||
return f_alpha_B_dt;
|
||||
}
|
||||
|
||||
// social_force_baseline.cpp
|
||||
|
||||
Pedestrian *init_pedestrians(int n_p) {
|
||||
#if RANDOM_SEED
|
||||
random_device rd; // Platform-provided random seed source.
|
||||
mt19937 gen(rd()); // Non-reproducible initialization.
|
||||
#else
|
||||
mt19937 gen(SEED); // Reproducible initialization.
|
||||
#endif
|
||||
|
||||
normal_distribution<double> velocity_gen(v0_mean, v0_std_dev);
|
||||
uniform_real_distribution<double> y_gen(0.3, BOARD_WIDTH - 0.3);
|
||||
uniform_real_distribution<double> x_gen_left(2.0, BOARD_LENGTH / 2.0);
|
||||
uniform_real_distribution<double> x_gen_right(BOARD_LENGTH / 2.0, BOARD_LENGTH - 2.0);
|
||||
|
||||
Pedestrian *P = new Pedestrian[n_p];
|
||||
|
||||
int n_p_half = n_p / 2;
|
||||
|
||||
// Group of pedestrians walking from left to right.
|
||||
for (int i = 0; i < n_p_half; i++) {
|
||||
double x = x_gen_left(gen);
|
||||
double y = y_gen(gen);
|
||||
|
||||
Vector2d start_pos = Vector2d(x, y);
|
||||
Vector2d destination = Vector2d(BOARD_LENGTH + 30.0, y);
|
||||
|
||||
Vector2d init_e = destination - start_pos;
|
||||
double init_e_norm = init_e.norm();
|
||||
Vector2d init_e_unit = init_e / init_e_norm;
|
||||
|
||||
// Reject near-zero desired speeds to avoid pedestrians that barely move,
|
||||
// and reject negative speeds because they would reverse the intended
|
||||
// walking direction.
|
||||
double desired_v0;
|
||||
do {
|
||||
desired_v0 = velocity_gen(gen);
|
||||
} while (desired_v0 <= 0.1);
|
||||
|
||||
double v_max = 1.3 * desired_v0;
|
||||
|
||||
P[i] = Pedestrian(WHITE, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
|
||||
}
|
||||
|
||||
// Group of pedestrians walking from right to left.
|
||||
for (int i = n_p_half; i < n_p; i++) {
|
||||
double x = x_gen_right(gen);
|
||||
double y = y_gen(gen);
|
||||
|
||||
Vector2d start_pos = Vector2d(x, y);
|
||||
Vector2d destination = Vector2d(-30.0, y);
|
||||
|
||||
Vector2d init_e = destination - start_pos;
|
||||
double init_e_norm = init_e.norm();
|
||||
Vector2d init_e_unit = init_e / init_e_norm;
|
||||
|
||||
// Reject near-zero desired speeds to avoid pedestrians that barely move,
|
||||
// and reject negative speeds because they would reverse the intended
|
||||
// walking direction.
|
||||
double desired_v0;
|
||||
do {
|
||||
desired_v0 = velocity_gen(gen);
|
||||
} while (desired_v0 <= 0.1);
|
||||
|
||||
double v_max = 1.3 * desired_v0;
|
||||
|
||||
P[i] = Pedestrian(BLACK, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
|
||||
}
|
||||
|
||||
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
|
||||
// Avoid compiler unused parameter warning
|
||||
(void)dump_path;
|
||||
#endif
|
||||
|
||||
#if !BENCHMARK
|
||||
const int dump_interval = 1;
|
||||
|
||||
// Each sample stores: time step, pedestrian id, x, y, e.x, e.y, color.
|
||||
vector<tuple<int, int, double, double, double, double, int>> samples;
|
||||
|
||||
if (dump_path) {
|
||||
samples.reserve(static_cast<size_t>(n_iter / dump_interval + 1) * n_p);
|
||||
|
||||
// Store the initial state before the first simulation step.
|
||||
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
|
||||
|
||||
// # ADDs: n_iter * (
|
||||
// # n_p * 4 +
|
||||
// # n_p * (n_p - 1) * 14 +
|
||||
// # n_p * board.n_obstacles * 10 +
|
||||
// # n_p * 7
|
||||
// # )
|
||||
|
||||
// # MULTs: n_iter * (
|
||||
// # n_p * 4
|
||||
// # n_p * (n_p - 1) * 24 +
|
||||
// # n_p * board.n_obstacles * 13 +
|
||||
// # n_p * 16
|
||||
// # )
|
||||
|
||||
// # DIVs: n_iter * (
|
||||
// # n_p * (n_p - 1) +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p * 2
|
||||
// # )
|
||||
|
||||
// # SQRTs: n_iter * (
|
||||
// # n_p * (n_p - 1) * 3 +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p * 3
|
||||
// # )
|
||||
|
||||
// # EXPs: n_iter * (
|
||||
// # n_p * (n_p - 1) +
|
||||
// # n_p * board.n_obstacles
|
||||
// # )
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
// Pass 1: Compute all forces from the current state.
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
Pedestrian &p = pedestrians[i];
|
||||
|
||||
// # 2 ADDs, 4 MULTs
|
||||
//
|
||||
// $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$
|
||||
//
|
||||
Vector2d f_alpha_dt = acceleration_term_dt(p);
|
||||
|
||||
// # (N-1) * (14 ADDs, 24 MULTs, 1 DIV, 3 SQRTs, 1 EXP)
|
||||
//
|
||||
// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha},\vec{r}_{\alpha} - \vec{r}_{\beta})$
|
||||
//
|
||||
for (int j = 0; j < n_p; j++) {
|
||||
if (i == j)
|
||||
continue;
|
||||
|
||||
f_alpha_dt += pedestrian_repulsion_dt(p, pedestrians[j]);
|
||||
}
|
||||
|
||||
// # board.n_obstacles * (10 ADDs, 13 MULTs, 1 DIV, 1 SQRT, 1 EXP)
|
||||
//
|
||||
// $\sum_{B} \vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$
|
||||
//
|
||||
for (int k = 0; k < board.n_obstacles; k++) {
|
||||
f_alpha_dt += obstacle_repulsion_dt(p, board.obstacles[k]);
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
//
|
||||
//
|
||||
// $\frac{d\vec{w}_\alpha}{dt} = \vec{F}_{\alpha}(t)$
|
||||
//
|
||||
p.w += f_alpha_dt;
|
||||
}
|
||||
|
||||
// Pass 2: Update pedestrian states after all forces have been computed.
|
||||
// # (7 ADDs, 16 MULTs, 2 DIVs, 3 SQRTs) * n_p
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
Pedestrian &p = pedestrians[i];
|
||||
|
||||
// # 1 ADD, 2 MULTs, 1 SQRT
|
||||
double w_norm = p.w.norm();
|
||||
|
||||
double w_norm2 = w_norm > EPS ? w_norm : EPS; // Avoid division by zero.
|
||||
|
||||
// # 1 DIV
|
||||
double correction_factor = std::min(1.0, p.v_max / w_norm2);
|
||||
|
||||
// # 2 MULTs
|
||||
p.curr_vel = p.w * correction_factor;
|
||||
|
||||
// # 2 ADDs, 2 MULTs
|
||||
//
|
||||
// $\vec{r}_i^{t+1} = \vec{r}_i^t + \vec{v}_i^t dt$
|
||||
//
|
||||
p.curr_pos = p.curr_pos + p.curr_vel * dt;
|
||||
|
||||
// When a pedestrian reaches the end of the corridor, teleport them back
|
||||
// to the opposite entrance while keeping the same y-coordinate. This
|
||||
// maintains a constant pedestrian flow and makes lane formation easier to
|
||||
// observe.
|
||||
if (p.color == WHITE && p.curr_pos.x > (BOARD_LENGTH - 1.5)) {
|
||||
p.curr_pos.x = 1.0;
|
||||
p.destination.y = p.curr_pos.y;
|
||||
p.w = Vector2d(p.desired_v0, 0.0);
|
||||
p.curr_vel = p.w;
|
||||
}
|
||||
if (p.color == BLACK && p.curr_pos.x < 1.5) {
|
||||
p.curr_pos.x = BOARD_LENGTH - 1.0;
|
||||
p.destination.y = p.curr_pos.y;
|
||||
p.w = Vector2d(-p.desired_v0, 0.0);
|
||||
p.curr_vel = p.w;
|
||||
}
|
||||
|
||||
// Keep pedestrians inside the board boundaries.
|
||||
p.curr_pos.y = clamp(p.curr_pos.y, WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
|
||||
|
||||
// # 2 ADDs
|
||||
Vector2d e = p.destination - p.curr_pos;
|
||||
|
||||
// # 1 ADD, 2 MULTs, 1 DIV, 1 SQRT
|
||||
double e_norm = e.norm();
|
||||
double e_norm2 = e_norm > EPS ? e_norm : EPS; // Avoid division by zero.
|
||||
double e_norm_inv = 1.0 / e_norm2;
|
||||
|
||||
// # 2 MULTs
|
||||
p.e = e * e_norm_inv;
|
||||
|
||||
// # 1 ADD, 2 MULTs
|
||||
double velocity_norm_sq = p.curr_vel.dot(p.curr_vel);
|
||||
|
||||
// # 1 SQRT
|
||||
double velocity_norm = std::sqrt(velocity_norm_sq);
|
||||
|
||||
// # 1 MULT
|
||||
p.s_b_sq = velocity_norm_sq * delta_t_sq;
|
||||
|
||||
// # 3 MULTs
|
||||
p.s_b_e = p.e * (velocity_norm * delta_t);
|
||||
|
||||
#if !BENCHMARK
|
||||
// Store the current pedestrian state for visualization.
|
||||
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
|
||||
// Export recorded simulation states to CSV.
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,438 @@
|
||||
// # Changes: Added per-instruction flop count
|
||||
// # Predecessor: variant_float_baseline.cpp
|
||||
|
||||
#include "../headers/board_float.hpp"
|
||||
#include "../headers/constants_float.hpp"
|
||||
#include "../headers/pedestrian_float.hpp"
|
||||
#include "../headers/vector2d_float.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <math.h> // keep — forces code uses unqualified sqrt/exp
|
||||
#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;
|
||||
|
||||
// forces.cpp
|
||||
|
||||
// Flop count: 2 ADDs, 6 MULTs
|
||||
float w_ped(Vector2d e, Vector2d f) {
|
||||
float f_norm_sq = f.dot(f);
|
||||
float rhs = f_norm_sq * cos_phi_sq;
|
||||
float e_dot_f = e.dot(f);
|
||||
float e_dot_f_sq = e_dot_f * e_dot_f;
|
||||
if (e_dot_f >= 0.0 || e_dot_f_sq <= rhs)
|
||||
return dt_neg;
|
||||
else
|
||||
return c_neg_times_dt;
|
||||
}
|
||||
|
||||
float w_obstacle(Vector2d e, Vector2d f) {
|
||||
float e_dot_f = (f.y == 0) ? 0 : (f.y < 0 ? -e.y : e.y);
|
||||
if (e_dot_f >= cos_phi)
|
||||
return dt_neg;
|
||||
else
|
||||
return c_neg_times_dt;
|
||||
}
|
||||
|
||||
// Flop count: 2 ADDs, 4 MULTs
|
||||
Vector2d acceleration_term_dt(const Pedestrian &alpha) {
|
||||
|
||||
// # 2 MULTs
|
||||
Vector2d desired_velocity = alpha.e * alpha.desired_v0;
|
||||
|
||||
Vector2d actual_velocity = alpha.curr_vel;
|
||||
|
||||
// # 2 ADDs, 2 MULTs
|
||||
Vector2d f_alpha_0_dt = (desired_velocity - actual_velocity) * dt_times_tau_alpha_inv;
|
||||
|
||||
return f_alpha_0_dt;
|
||||
}
|
||||
|
||||
// Flop count: 12 ADDs, 24 MULTs, 1 DIV, 3 SQRTs, 1 EXP
|
||||
Vector2d pedestrian_repulsion_dt(const Pedestrian &alpha, const Pedestrian &beta) {
|
||||
|
||||
// # 2 ADDs
|
||||
// $\vec{r}_{\alpha\beta}$
|
||||
Vector2d r_ab = alpha.curr_pos - beta.curr_pos;
|
||||
|
||||
// # 1 ADD, 2 MULTs, 1 SQRT
|
||||
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert$
|
||||
float r_ab_norm = r_ab.norm();
|
||||
r_ab_norm = r_ab_norm > EPS ? r_ab_norm : EPS; // Avoid division by zero
|
||||
|
||||
// $v_\beta \Delta t \vec{e}_\beta$
|
||||
Vector2d s_b_e = beta.s_b_e;
|
||||
|
||||
// # 2 ADDs
|
||||
// $\vec{r}_{\alpha\beta} - \vec{v}_{\beta}\varDelta_{t}\vec{e}_{\beta}$
|
||||
Vector2d r_ab2 = r_ab - s_b_e;
|
||||
|
||||
// # 1 ADD, 2 MULTs, 1 SQRT
|
||||
// $\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
|
||||
float r_ab2_norm = r_ab2.norm();
|
||||
r_ab2_norm = r_ab2_norm > EPS ? r_ab2_norm : EPS; // Avoid division by zero
|
||||
|
||||
// # 1 ADD
|
||||
// $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$
|
||||
float r_ab_norm_sum = r_ab_norm + r_ab2_norm;
|
||||
|
||||
// # 1 ADD, 1 MULTs, 1 SQRT
|
||||
//
|
||||
// $2b = \sqrt{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)^2 - (v_\beta \Delta t)^2}$
|
||||
//
|
||||
float two_b_sq = r_ab_norm_sum * r_ab_norm_sum - beta.s_b_sq;
|
||||
two_b_sq = two_b_sq > EPS_SQ ? two_b_sq : EPS_SQ; // Avoid sqrt of negative and avoid division by zero
|
||||
float two_b = sqrt(two_b_sq);
|
||||
|
||||
// # 5 MULTs, 1 DIV, 1 EXP
|
||||
//
|
||||
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}$
|
||||
//
|
||||
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
|
||||
//
|
||||
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}$
|
||||
//
|
||||
float scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
|
||||
|
||||
// # 1 MULT
|
||||
//
|
||||
// $\frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}$
|
||||
//
|
||||
float scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
|
||||
|
||||
// # 2 ADDs, 4 MULTs
|
||||
//
|
||||
// $\vec{-f}_{\alpha \beta}(\vec{r}_{\alpha \beta}) = \frac{-V^0_{\alpha \beta}}{2\sigma}e^{-b / \sigma} 2\grad_{\vec{r}_{\alpha \beta}}b $
|
||||
//
|
||||
//
|
||||
// $= \frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}\,\vec{r}_{\alpha \beta} + \frac{-V^0_{\alpha\beta}}{2\sigma}\,e^{-b/\sigma}\,\frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} -
|
||||
// v_\beta \Delta t \vec{e}_\beta \Arrowvert}\,(\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta)$
|
||||
//
|
||||
//
|
||||
//
|
||||
// $2\grad_{\vec{r}_{\alpha \beta}}b = \frac{(\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert)(\frac{\vec{r}_{\alpha \beta}}{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert} + \frac{\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta}{\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert})}{2b}$
|
||||
//
|
||||
//
|
||||
// $= \frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert}\,\vec{r}_{\alpha \beta} + \frac{\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}{2b\,\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert}\,(\vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta)$
|
||||
//
|
||||
//
|
||||
Vector2d f_alpha_beta_neg = r_ab * scalar_mult_r_ab + r_ab2 * scalar_mult_r_ab2;
|
||||
|
||||
// # 2 ADDs, 8 MULTs
|
||||
//
|
||||
// $\vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha \beta}).dt = w(\vec{e}_{\alpha}, -\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta})).\vec{f}_{\alpha\beta}(\vec{r}_{\alpha \beta}).dt$
|
||||
//
|
||||
Vector2d f_alpha_beta_dt = f_alpha_beta_neg * w_ped(alpha.e, f_alpha_beta_neg);
|
||||
|
||||
return f_alpha_beta_dt;
|
||||
}
|
||||
|
||||
// Flop count: 8 ADDs, 13 MULTs, 1 DIV, 1 SQRT, 1 EXP
|
||||
Vector2d obstacle_repulsion_dt(const Pedestrian &alpha, const Obstacle &B) {
|
||||
|
||||
// # 7 ADDs, 5 MULTs
|
||||
// $\vec{r}_{\alpha B}$
|
||||
Vector2d r_aB = alpha.curr_pos - B.find_nearest_obstacle_point(alpha.curr_pos);
|
||||
|
||||
// # 1 ADD, 2 MULTs, 1 SQRT
|
||||
// $\Arrowvert \vec{r}_{\alpha B} \Arrowvert$
|
||||
float r_aB_norm = r_aB.norm();
|
||||
r_aB_norm = r_aB_norm > EPS ? r_aB_norm : EPS; // Avoid division by zero
|
||||
|
||||
// # 2 MULTs, 1 DIV, 1 EXP
|
||||
//
|
||||
// $\frac{-U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R}$
|
||||
//
|
||||
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
|
||||
|
||||
// # 2 MULTs
|
||||
//
|
||||
// $-\vec{F}_{\alpha B}(\vec{r}_{\alpha B}) = \frac{-U^0_{\alpha B}}{R} e^{- \Arrowvert \vec{r}_{\alpha B} \Arrowvert / R} \frac{\vec{r}_{\alpha B}}{\Arrowvert \vec{r}_{\alpha B} \Arrowvert}$
|
||||
//
|
||||
Vector2d f_alpha_B_neg = r_aB * scalar_mult;
|
||||
|
||||
// # 2 MULTs
|
||||
//
|
||||
// $\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha B}).dt = w(\vec{e}_{\alpha}, -\vec{F}_{\alpha B}(\vec{r}_{\alpha B})).\vec{F}_{\alpha B}(\vec{r}_{\alpha B}).dt$
|
||||
//
|
||||
Vector2d f_alpha_B_dt = f_alpha_B_neg * w_obstacle(alpha.e, f_alpha_B_neg);
|
||||
|
||||
return f_alpha_B_dt;
|
||||
}
|
||||
|
||||
// social_force_baseline.cpp
|
||||
|
||||
Pedestrian *init_pedestrians(int n_p) {
|
||||
#if RANDOM_SEED
|
||||
random_device rd; // Platform-provided random seed source.
|
||||
mt19937 gen(rd()); // Non-reproducible initialization.
|
||||
#else
|
||||
mt19937 gen(SEED); // Reproducible initialization.
|
||||
#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);
|
||||
|
||||
Pedestrian *P = new Pedestrian[n_p];
|
||||
|
||||
int n_p_half = n_p / 2;
|
||||
|
||||
// Group of pedestrians walking from left to right.
|
||||
for (int i = 0; i < n_p_half; i++) {
|
||||
float x = x_gen_left(gen);
|
||||
float y = y_gen(gen);
|
||||
|
||||
Vector2d start_pos = Vector2d(x, y);
|
||||
Vector2d destination = Vector2d(BOARD_LENGTH + 30.0, y);
|
||||
|
||||
Vector2d init_e = destination - start_pos;
|
||||
float init_e_norm = init_e.norm();
|
||||
Vector2d init_e_unit = init_e / init_e_norm;
|
||||
|
||||
// Reject near-zero desired speeds to avoid pedestrians that barely move,
|
||||
// and reject negative speeds because they would reverse the intended
|
||||
// walking direction.
|
||||
float desired_v0;
|
||||
do {
|
||||
desired_v0 = velocity_gen(gen);
|
||||
} while (desired_v0 <= 0.1);
|
||||
|
||||
float v_max = 1.3 * desired_v0;
|
||||
|
||||
P[i] = Pedestrian(WHITE, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
|
||||
}
|
||||
|
||||
// Group of pedestrians walking from right to left.
|
||||
for (int i = n_p_half; i < n_p; i++) {
|
||||
float x = x_gen_right(gen);
|
||||
float y = y_gen(gen);
|
||||
|
||||
Vector2d start_pos = Vector2d(x, y);
|
||||
Vector2d destination = Vector2d(-30.0, y);
|
||||
|
||||
Vector2d init_e = destination - start_pos;
|
||||
float init_e_norm = init_e.norm();
|
||||
Vector2d init_e_unit = init_e / init_e_norm;
|
||||
|
||||
// Reject near-zero desired speeds to avoid pedestrians that barely move,
|
||||
// and reject negative speeds because they would reverse the intended
|
||||
// walking direction.
|
||||
float desired_v0;
|
||||
do {
|
||||
desired_v0 = velocity_gen(gen);
|
||||
} while (desired_v0 <= 0.1);
|
||||
|
||||
float v_max = 1.3 * desired_v0;
|
||||
|
||||
P[i] = Pedestrian(BLACK, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination);
|
||||
}
|
||||
|
||||
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
|
||||
// Avoid compiler unused parameter warning
|
||||
(void)dump_path;
|
||||
#endif
|
||||
|
||||
#if !BENCHMARK
|
||||
const int dump_interval = 1;
|
||||
|
||||
// Each sample stores: time step, pedestrian id, x, y, e.x, e.y, color.
|
||||
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);
|
||||
|
||||
// Store the initial state before the first simulation step.
|
||||
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
|
||||
|
||||
// # ADDs: n_iter * (
|
||||
// # n_p * 4 +
|
||||
// # n_p * (n_p - 1) * 14 +
|
||||
// # n_p * board.n_obstacles * 10 +
|
||||
// # n_p * 7
|
||||
// # )
|
||||
|
||||
// # MULTs: n_iter * (
|
||||
// # n_p * 4
|
||||
// # n_p * (n_p - 1) * 24 +
|
||||
// # n_p * board.n_obstacles * 13 +
|
||||
// # n_p * 16
|
||||
// # )
|
||||
|
||||
// # DIVs: n_iter * (
|
||||
// # n_p * (n_p - 1) +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p * 2
|
||||
// # )
|
||||
|
||||
// # SQRTs: n_iter * (
|
||||
// # n_p * (n_p - 1) * 3 +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p * 3
|
||||
// # )
|
||||
|
||||
// # EXPs: n_iter * (
|
||||
// # n_p * (n_p - 1) +
|
||||
// # n_p * board.n_obstacles
|
||||
// # )
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
// Pass 1: Compute all forces from the current state.
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
Pedestrian &p = pedestrians[i];
|
||||
|
||||
// # 2 ADDs, 4 MULTs
|
||||
//
|
||||
// $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$
|
||||
//
|
||||
Vector2d f_alpha_dt = acceleration_term_dt(p);
|
||||
|
||||
// # (N-1) * (14 ADDs, 24 MULTs, 1 DIV, 3 SQRTs, 1 EXP)
|
||||
//
|
||||
// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha},\vec{r}_{\alpha} - \vec{r}_{\beta})$
|
||||
//
|
||||
for (int j = 0; j < n_p; j++) {
|
||||
if (i == j)
|
||||
continue;
|
||||
|
||||
f_alpha_dt += pedestrian_repulsion_dt(p, pedestrians[j]);
|
||||
}
|
||||
|
||||
// # board.n_obstacles * (10 ADDs, 13 MULTs, 1 DIV, 1 SQRT, 1 EXP)
|
||||
//
|
||||
// $\sum_{B} \vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$
|
||||
//
|
||||
for (int k = 0; k < board.n_obstacles; k++) {
|
||||
f_alpha_dt += obstacle_repulsion_dt(p, board.obstacles[k]);
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
//
|
||||
//
|
||||
// $\frac{d\vec{w}_\alpha}{dt} = \vec{F}_{\alpha}(t)$
|
||||
//
|
||||
p.w += f_alpha_dt;
|
||||
}
|
||||
|
||||
// Pass 2: Update pedestrian states after all forces have been computed.
|
||||
// # (7 ADDs, 16 MULTs, 2 DIVs, 3 SQRTs) * n_p
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
Pedestrian &p = pedestrians[i];
|
||||
|
||||
// # 1 ADD, 2 MULTs, 1 SQRT
|
||||
float w_norm = p.w.norm();
|
||||
|
||||
float w_norm2 = w_norm > EPS ? w_norm : EPS; // Avoid division by zero.
|
||||
|
||||
// # 1 DIV
|
||||
float correction_factor = std::min(1.0f, p.v_max / w_norm2);
|
||||
|
||||
// # 2 MULTs
|
||||
p.curr_vel = p.w * correction_factor;
|
||||
|
||||
// # 2 ADDs, 2 MULTs
|
||||
//
|
||||
// $\vec{r}_i^{t+1} = \vec{r}_i^t + \vec{v}_i^t dt$
|
||||
//
|
||||
p.curr_pos = p.curr_pos + p.curr_vel * dt;
|
||||
|
||||
// When a pedestrian reaches the end of the corridor, teleport them back
|
||||
// to the opposite entrance while keeping the same y-coordinate. This
|
||||
// maintains a constant pedestrian flow and makes lane formation easier to
|
||||
// observe.
|
||||
if (p.color == WHITE && p.curr_pos.x > (BOARD_LENGTH - 1.5)) {
|
||||
p.curr_pos.x = 1.0;
|
||||
p.destination.y = p.curr_pos.y;
|
||||
p.w = Vector2d(p.desired_v0, 0.0);
|
||||
p.curr_vel = p.w;
|
||||
}
|
||||
if (p.color == BLACK && p.curr_pos.x < 1.5) {
|
||||
p.curr_pos.x = BOARD_LENGTH - 1.0;
|
||||
p.destination.y = p.curr_pos.y;
|
||||
p.w = Vector2d(-p.desired_v0, 0.0);
|
||||
p.curr_vel = p.w;
|
||||
}
|
||||
|
||||
// Keep pedestrians inside the board boundaries.
|
||||
p.curr_pos.y = clamp(p.curr_pos.y, WALL_EPS, BOARD_WIDTH_MINUS_WALL_EPS);
|
||||
|
||||
// # 2 ADDs
|
||||
Vector2d e = p.destination - p.curr_pos;
|
||||
|
||||
// # 1 ADD, 2 MULTs, 1 DIV, 1 SQRT
|
||||
float e_norm = e.norm();
|
||||
float e_norm2 = e_norm > EPS ? e_norm : EPS; // Avoid division by zero.
|
||||
float e_norm_inv = 1.0 / e_norm2;
|
||||
|
||||
// # 2 MULTs
|
||||
p.e = e * e_norm_inv;
|
||||
|
||||
// # 1 ADD, 2 MULTs
|
||||
float velocity_norm_sq = p.curr_vel.dot(p.curr_vel);
|
||||
|
||||
// # 1 SQRT
|
||||
float velocity_norm = std::sqrt(velocity_norm_sq);
|
||||
|
||||
// # 1 MULT
|
||||
p.s_b_sq = velocity_norm_sq * delta_t_sq;
|
||||
|
||||
// # 3 MULTs
|
||||
p.s_b_e = p.e * (velocity_norm * delta_t);
|
||||
|
||||
#if !BENCHMARK
|
||||
// Store the current pedestrian state for visualization.
|
||||
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
|
||||
// Export recorded simulation states to CSV.
|
||||
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;
|
||||
}
|
||||
@@ -180,6 +180,7 @@ static inline __m256 exp256_ps(__m256 x) {
|
||||
const __m256 ln2 = _mm256_set1_ps(0.6931471805599453f);
|
||||
__m256 r = _mm256_fnmadd_ps(nf, ln2, x); // # 8 MULTs, 8 ADDs
|
||||
|
||||
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);
|
||||
@@ -194,7 +195,7 @@ static inline __m256 exp256_ps(__m256 x) {
|
||||
const __m256 c1 = _mm256_set1_ps(c1_scalar);
|
||||
const __m256 c0 = _mm256_set1_ps(c0_scalar);
|
||||
|
||||
__m256 p = c12;
|
||||
__m256 p = c13;
|
||||
|
||||
// # 13 * (8 MULTs, 8 ADDs)
|
||||
// # = 104 MULTs, 104 ADDs
|
||||
|
||||
@@ -197,6 +197,7 @@ static inline __m256 exp256_ps(__m256 x) {
|
||||
const __m256 ln2 = _mm256_set1_ps(0.6931471805599453f);
|
||||
__m256 r = _mm256_fnmadd_ps(nf, ln2, x); // # 8 MULTs, 8 ADDs
|
||||
|
||||
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);
|
||||
@@ -211,7 +212,7 @@ static inline __m256 exp256_ps(__m256 x) {
|
||||
const __m256 c1 = _mm256_set1_ps(c1_scalar);
|
||||
const __m256 c0 = _mm256_set1_ps(c0_scalar);
|
||||
|
||||
__m256 p = c12;
|
||||
__m256 p = c13;
|
||||
|
||||
// # 13 * (8 MULTs, 8 ADDs)
|
||||
// # = 104 MULTs, 104 ADDs
|
||||
|
||||
@@ -197,6 +197,7 @@ static inline __m256 exp256_ps(__m256 x) {
|
||||
const __m256 ln2 = _mm256_set1_ps(0.6931471805599453f);
|
||||
__m256 r = _mm256_fnmadd_ps(nf, ln2, x); // # 8 MULTs, 8 ADDs
|
||||
|
||||
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);
|
||||
@@ -211,7 +212,7 @@ static inline __m256 exp256_ps(__m256 x) {
|
||||
const __m256 c1 = _mm256_set1_ps(c1_scalar);
|
||||
const __m256 c0 = _mm256_set1_ps(c0_scalar);
|
||||
|
||||
__m256 p = c12;
|
||||
__m256 p = c13;
|
||||
|
||||
// # 13 * (8 MULTs, 8 ADDs)
|
||||
// # = 104 MULTs, 104 ADDs
|
||||
|
||||
@@ -0,0 +1,540 @@
|
||||
// # Changes: Ported AVX2 to AVX512
|
||||
// # Predecessor: variant_float_inlining_SoA_AVX2_polyexp.cpp
|
||||
|
||||
#include "../headers/board_reduced_float.hpp"
|
||||
#include "../headers/constants_float.hpp"
|
||||
#include "../headers/pedestrian_reduced_float.hpp"
|
||||
|
||||
#include <immintrin.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
|
||||
#define SEED 42
|
||||
#define BOARD_WIDTH 10.0f
|
||||
#define BOARD_LENGTH 50.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
|
||||
#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<float> pos_x, pos_y;
|
||||
alignas(64) vector<float> sb_sq;
|
||||
alignas(64) vector<float> sb_e_x, sb_e_y;
|
||||
alignas(64) vector<float> e_x, e_y;
|
||||
alignas(64) vector<float> curr_vel_x, curr_vel_y;
|
||||
alignas(64) vector<float> w_x, w_y;
|
||||
alignas(64) vector<float> desired_v0;
|
||||
alignas(64) vector<float> v_max;
|
||||
alignas(64) vector<float> destination_x, destination_y;
|
||||
alignas(64) 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);
|
||||
|
||||
const int n_p_pad = (n_p + 15) & ~15;
|
||||
|
||||
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); }
|
||||
|
||||
// # 256 MULTs, 224 ADDs
|
||||
static inline __m512 _avx512_exp_ps(__m512 x) {
|
||||
|
||||
const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
|
||||
|
||||
// # 16 MULTs
|
||||
__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);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
|
||||
|
||||
const __m512 c13 = _mm512_set1_ps(c13_scalar);
|
||||
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);
|
||||
|
||||
__m512 p = c13;
|
||||
|
||||
// # 13 * (16 MULTs, 16 ADDs)
|
||||
// # = 208 MULTs, 208 ADDs
|
||||
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);
|
||||
|
||||
__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);
|
||||
|
||||
// # 16 MULTs
|
||||
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<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
|
||||
|
||||
const int n_p_pad = (n_p + 15) & ~15;
|
||||
|
||||
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);
|
||||
|
||||
// # ADDs: n_iter * (
|
||||
// # n_p * 36 +
|
||||
// # n_p * n_p_pad * 29 +
|
||||
// # n_p * board.n_obstacles * 10 +
|
||||
// # n_p * 7
|
||||
// # )
|
||||
|
||||
// # MULTs: n_iter * (
|
||||
// # n_p * 4 +
|
||||
// # n_p * n_p_pad * 40 +
|
||||
// # n_p * board.n_obstacles * 13 +
|
||||
// # n_p * 17
|
||||
// # )
|
||||
|
||||
// # DIVs: n_iter * (
|
||||
// # n_p * n_p_pad +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p * 2
|
||||
// # )
|
||||
|
||||
// # SQRTs: n_iter * (
|
||||
// # n_p * n_p_pad * 3 +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p * 3
|
||||
// # )
|
||||
|
||||
// # EXPs: n_iter * n_p * board.n_obstacles
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
|
||||
// # 2 MULTs
|
||||
float desired_velocity_x = e_x[i] * desired_v0[i];
|
||||
float desired_velocity_y = e_y[i] * desired_v0[i];
|
||||
|
||||
// # 2 ADDs, 2 MULTs
|
||||
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;
|
||||
|
||||
const __m512 pix = _mm512_set1_ps(pos_x[i]);
|
||||
const __m512 piy = _mm512_set1_ps(pos_y[i]);
|
||||
const __m512 eix = _mm512_set1_ps(e_x[i]);
|
||||
const __m512 eiy = _mm512_set1_ps(e_y[i]);
|
||||
const __m512 v_i = _mm512_set1_ps((float)i);
|
||||
|
||||
__m512 acc_x = _mm512_setzero_ps();
|
||||
__m512 acc_y = _mm512_setzero_ps();
|
||||
|
||||
for (int j = 0; j < n_p_pad; j += 16) {
|
||||
|
||||
// # 16 ADDs
|
||||
__m512 j_idx = _mm512_add_ps(_mm512_set1_ps((float)j), lane_off);
|
||||
|
||||
__mmask16 m_neq_i = _mm512_cmp_ps_mask(j_idx, v_i, _CMP_NEQ_OQ);
|
||||
__mmask16 m_in_range = _mm512_cmp_ps_mask(j_idx, v_np, _CMP_LT_OQ);
|
||||
__mmask16 active = m_neq_i & m_in_range;
|
||||
|
||||
__m512 pjx = _mm512_loadu_ps(&pos_x[j]);
|
||||
__m512 pjy = _mm512_loadu_ps(&pos_y[j]);
|
||||
__m512 sbe_jx = _mm512_loadu_ps(&sb_e_x[j]);
|
||||
__m512 sbe_jy = _mm512_loadu_ps(&sb_e_y[j]);
|
||||
__m512 sb_jsq = _mm512_loadu_ps(&sb_sq[j]);
|
||||
|
||||
// # 32 ADDs
|
||||
__m512 r_ab_x = _mm512_sub_ps(pix, pjx);
|
||||
__m512 r_ab_y = _mm512_sub_ps(piy, pjy);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 r_ab_n2 = _mm512_fmadd_ps(r_ab_x, r_ab_x, _mm512_mul_ps(r_ab_y, r_ab_y));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 r_ab_n = _mm512_sqrt_ps(r_ab_n2);
|
||||
|
||||
r_ab_n = _mm512_max_ps(r_ab_n, v_eps);
|
||||
|
||||
// # 32 ADDs
|
||||
__m512 r_ab2_x = _mm512_sub_ps(r_ab_x, sbe_jx);
|
||||
__m512 r_ab2_y = _mm512_sub_ps(r_ab_y, sbe_jy);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 r_ab2_n2 = _mm512_fmadd_ps(r_ab2_x, r_ab2_x, _mm512_mul_ps(r_ab2_y, r_ab2_y));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 r_ab2_n = _mm512_sqrt_ps(r_ab2_n2);
|
||||
|
||||
r_ab2_n = _mm512_max_ps(r_ab2_n, v_eps);
|
||||
|
||||
// # 16 ADDs
|
||||
__m512 r_sum = _mm512_add_ps(r_ab_n, r_ab2_n);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
__m512 two_b_sq = _mm512_fmsub_ps(r_sum, r_sum, sb_jsq);
|
||||
|
||||
two_b_sq = _mm512_max_ps(two_b_sq, v_eps_sq);
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 two_b = _mm512_sqrt_ps(two_b_sq);
|
||||
|
||||
// # 16 MULTs
|
||||
__m512 e_arg = _mm512_mul_ps(two_b, v_neg_2s_inv);
|
||||
|
||||
e_arg = _mm512_max_ps(e_arg, v_exp_min);
|
||||
|
||||
// # 256 MULTs, 224 ADDs
|
||||
__m512 e_val = _avx512_exp_ps(e_arg);
|
||||
|
||||
// # 64 MULTs
|
||||
__m512 numer = _mm512_mul_ps(_mm512_mul_ps(r_sum, v_neg_v0_div_2s), e_val);
|
||||
__m512 denom = _mm512_mul_ps(_mm512_mul_ps(two_b, r_ab_n), r_ab2_n);
|
||||
|
||||
__m512 denom_safe = _mm512_mask_blend_ps(active, v_one, denom);
|
||||
|
||||
// # 16 DIVs
|
||||
__m512 smc = _mm512_div_ps(numer, denom_safe);
|
||||
|
||||
// # 32 MULTs
|
||||
__m512 sm_r_ab = _mm512_mul_ps(smc, r_ab2_n);
|
||||
__m512 sm_r_ab2 = _mm512_mul_ps(smc, r_ab_n);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__m512 f_x = _mm512_fmadd_ps(r_ab2_x, sm_r_ab2, _mm512_mul_ps(r_ab_x, sm_r_ab));
|
||||
__m512 f_y = _mm512_fmadd_ps(r_ab2_y, sm_r_ab2, _mm512_mul_ps(r_ab_y, sm_r_ab));
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 f_norm_sq = _mm512_fmadd_ps(f_x, f_x, _mm512_mul_ps(f_y, f_y));
|
||||
|
||||
// # 16 MULTs
|
||||
__m512 rhs = _mm512_mul_ps(f_norm_sq, v_cos_phi_sq);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 e_dot_f = _mm512_fmadd_ps(eix, f_x, _mm512_mul_ps(eiy, f_y));
|
||||
|
||||
// # 16 MULTs
|
||||
__m512 e_dot_f_sq = _mm512_mul_ps(e_dot_f, e_dot_f);
|
||||
|
||||
__mmask16 c1 = _mm512_cmp_ps_mask(e_dot_f, v_zero, _CMP_LT_OQ);
|
||||
__mmask16 c2 = _mm512_cmp_ps_mask(e_dot_f_sq, rhs, _CMP_GT_OQ);
|
||||
__mmask16 cond = c1 & c2;
|
||||
|
||||
__m512 w_val = _mm512_mask_blend_ps(cond, v_dt_neg, v_c_neg_dt);
|
||||
|
||||
__m512 f_x_safe = _mm512_maskz_mov_ps(active, f_x);
|
||||
__m512 f_y_safe = _mm512_maskz_mov_ps(active, f_y);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
acc_x = _mm512_fmadd_ps(f_x_safe, w_val, acc_x);
|
||||
acc_y = _mm512_fmadd_ps(f_y_safe, w_val, acc_y);
|
||||
}
|
||||
|
||||
// # 32 ADDs
|
||||
f_alpha_dt_x += _mm512_reduce_add_ps(acc_x);
|
||||
f_alpha_dt_y += _mm512_reduce_add_ps(acc_y);
|
||||
|
||||
for (int k = 0; k < board.n_obstacles; k++) {
|
||||
|
||||
// # 2 ADDs
|
||||
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;
|
||||
|
||||
// # 3 MULTs, 1 ADD
|
||||
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;
|
||||
|
||||
// # 4 ADDs, 2 MULTs
|
||||
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);
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
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;
|
||||
|
||||
// # 4 MULTs, 1 EXP, 1 DIV
|
||||
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;
|
||||
|
||||
// # 2 MULTS
|
||||
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;
|
||||
|
||||
// # 2 ADDs
|
||||
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;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
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);
|
||||
|
||||
// # 2 MULTs
|
||||
curr_vel_x[i] = w_x[i] * correction_factor;
|
||||
curr_vel_y[i] = w_y[i] * correction_factor;
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt;
|
||||
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt;
|
||||
|
||||
if (color[i] == 0 && pos_x[i] > BOARD_LENGTH_MINUS_ONE_POINT_FIVE) {
|
||||
pos_x[i] = 1.0f;
|
||||
destination_y[i] = pos_y[i];
|
||||
w_x[i] = desired_v0[i];
|
||||
w_y[i] = 0.0f;
|
||||
curr_vel_x[i] = w_x[i];
|
||||
curr_vel_y[i] = w_y[i];
|
||||
}
|
||||
if (color[i] == 1 && pos_x[i] < 1.5f) {
|
||||
pos_x[i] = BOARD_LENGTH_MINUS_ONE;
|
||||
destination_y[i] = pos_y[i];
|
||||
w_x[i] = -desired_v0[i];
|
||||
w_y[i] = 0.0f;
|
||||
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);
|
||||
|
||||
// # 2 ADDs
|
||||
float ex = destination_x[i] - pos_x[i];
|
||||
float ey = destination_y[i] - pos_y[i];
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
float e_norm = sqrt(ex * ex + ey * ey);
|
||||
|
||||
float e_norm2 = e_norm > EPS ? e_norm : EPS;
|
||||
|
||||
// # 1 DIV
|
||||
float e_norm_inv = 1.0f / e_norm2;
|
||||
|
||||
// # 2 MULTs
|
||||
e_x[i] = ex * e_norm_inv;
|
||||
e_y[i] = ey * e_norm_inv;
|
||||
|
||||
// # 2 MULTs, 1 ADD
|
||||
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);
|
||||
|
||||
// # 5 MULTs
|
||||
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);
|
||||
|
||||
#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;
|
||||
}
|
||||
@@ -0,0 +1,557 @@
|
||||
// # Changes: Added more correction rounds to _avx512_exp_ps to do 30 flops per scalar
|
||||
// # Predecessor: variant_float_inlining_SoA_AVX512_polyexp_fastinvsqrt.cpp
|
||||
|
||||
#include "../headers/board_reduced_float.hpp"
|
||||
#include "../headers/constants_float.hpp"
|
||||
#include "../headers/pedestrian_reduced_float.hpp"
|
||||
|
||||
#include <immintrin.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
|
||||
#define SEED 42
|
||||
#define BOARD_WIDTH 10.0f
|
||||
#define BOARD_LENGTH 50.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
|
||||
#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<float> pos_x, pos_y;
|
||||
alignas(64) vector<float> sb_sq;
|
||||
alignas(64) vector<float> sb_e_x, sb_e_y;
|
||||
alignas(64) vector<float> e_x, e_y;
|
||||
alignas(64) vector<float> curr_vel_x, curr_vel_y;
|
||||
alignas(64) vector<float> w_x, w_y;
|
||||
alignas(64) vector<float> desired_v0;
|
||||
alignas(64) vector<float> v_max;
|
||||
alignas(64) vector<float> destination_x, destination_y;
|
||||
alignas(64) 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);
|
||||
|
||||
const int n_p_pad = (n_p + 15) & ~15;
|
||||
|
||||
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); }
|
||||
|
||||
// # 256 MULTs, 224 ADDs
|
||||
static inline __m512 _avx512_exp_ps(__m512 x) {
|
||||
|
||||
const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
|
||||
|
||||
// # 16 MULTs
|
||||
__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);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
|
||||
|
||||
const __m512 c13 = _mm512_set1_ps(c13_scalar);
|
||||
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);
|
||||
|
||||
__m512 p = c13;
|
||||
|
||||
// # 13 * (16 MULTs, 16 ADDs)
|
||||
// # = 208 MULTs, 208 ADDs
|
||||
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);
|
||||
|
||||
__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);
|
||||
|
||||
// # 16 MULTs
|
||||
return _mm512_mul_ps(p, pow2n);
|
||||
}
|
||||
|
||||
// # 7 MULTs, 2 ADDs
|
||||
static inline float fast_inv_sqrt(float number) {
|
||||
float y = number;
|
||||
|
||||
uint32_t i = bit_cast<uint32_t>(y);
|
||||
|
||||
i = 0x5f3759df - (i >> 1);
|
||||
|
||||
y = bit_cast<float>(i);
|
||||
|
||||
// # 1 MULT
|
||||
float x2 = number * 0.5;
|
||||
|
||||
// # 6 MULTs, 2 ADDs
|
||||
y = y * (1.5 - (x2 * y * y));
|
||||
y = y * (1.5 - (x2 * y * y));
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
const int n_p_pad = (n_p + 15) & ~15;
|
||||
|
||||
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);
|
||||
|
||||
// # ADDs: n_iter * (
|
||||
// # n_p * 36 +
|
||||
// # n_p * n_p_pad * 29 +
|
||||
// # n_p * board.n_obstacles * 10 +
|
||||
// # n_p * 11
|
||||
// # )
|
||||
|
||||
// # MULTs: n_iter * (
|
||||
// # n_p * 4 +
|
||||
// # n_p * n_p_pad * 40 +
|
||||
// # n_p * board.n_obstacles * 13 +
|
||||
// # n_p * 30
|
||||
// # )
|
||||
|
||||
// # DIVs: n_iter * (
|
||||
// # n_p * n_p_pad +
|
||||
// # n_p * board.n_obstacles
|
||||
// # )
|
||||
|
||||
// # SQRTs: n_iter * (
|
||||
// # n_p * n_p_pad * 3 +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p
|
||||
// # )
|
||||
|
||||
// # EXPs: n_iter * n_p * board.n_obstacles
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
|
||||
// # 2 MULTs
|
||||
float desired_velocity_x = e_x[i] * desired_v0[i];
|
||||
float desired_velocity_y = e_y[i] * desired_v0[i];
|
||||
|
||||
// # 2 ADDs, 2 MULTs
|
||||
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;
|
||||
|
||||
const __m512 pix = _mm512_set1_ps(pos_x[i]);
|
||||
const __m512 piy = _mm512_set1_ps(pos_y[i]);
|
||||
const __m512 eix = _mm512_set1_ps(e_x[i]);
|
||||
const __m512 eiy = _mm512_set1_ps(e_y[i]);
|
||||
const __m512 v_i = _mm512_set1_ps((float)i);
|
||||
|
||||
__m512 acc_x = _mm512_setzero_ps();
|
||||
__m512 acc_y = _mm512_setzero_ps();
|
||||
|
||||
for (int j = 0; j < n_p_pad; j += 16) {
|
||||
|
||||
// # 16 ADDs
|
||||
__m512 j_idx = _mm512_add_ps(_mm512_set1_ps((float)j), lane_off);
|
||||
|
||||
__mmask16 m_neq_i = _mm512_cmp_ps_mask(j_idx, v_i, _CMP_NEQ_OQ);
|
||||
__mmask16 m_in_range = _mm512_cmp_ps_mask(j_idx, v_np, _CMP_LT_OQ);
|
||||
__mmask16 active = m_neq_i & m_in_range;
|
||||
|
||||
__m512 pjx = _mm512_loadu_ps(&pos_x[j]);
|
||||
__m512 pjy = _mm512_loadu_ps(&pos_y[j]);
|
||||
__m512 sbe_jx = _mm512_loadu_ps(&sb_e_x[j]);
|
||||
__m512 sbe_jy = _mm512_loadu_ps(&sb_e_y[j]);
|
||||
__m512 sb_jsq = _mm512_loadu_ps(&sb_sq[j]);
|
||||
|
||||
// # 32 ADDs
|
||||
__m512 r_ab_x = _mm512_sub_ps(pix, pjx);
|
||||
__m512 r_ab_y = _mm512_sub_ps(piy, pjy);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 r_ab_n2 = _mm512_fmadd_ps(r_ab_x, r_ab_x, _mm512_mul_ps(r_ab_y, r_ab_y));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 r_ab_n = _mm512_sqrt_ps(r_ab_n2);
|
||||
|
||||
r_ab_n = _mm512_max_ps(r_ab_n, v_eps);
|
||||
|
||||
// # 32 ADDs
|
||||
__m512 r_ab2_x = _mm512_sub_ps(r_ab_x, sbe_jx);
|
||||
__m512 r_ab2_y = _mm512_sub_ps(r_ab_y, sbe_jy);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 r_ab2_n2 = _mm512_fmadd_ps(r_ab2_x, r_ab2_x, _mm512_mul_ps(r_ab2_y, r_ab2_y));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 r_ab2_n = _mm512_sqrt_ps(r_ab2_n2);
|
||||
|
||||
r_ab2_n = _mm512_max_ps(r_ab2_n, v_eps);
|
||||
|
||||
// # 16 ADDs
|
||||
__m512 r_sum = _mm512_add_ps(r_ab_n, r_ab2_n);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
__m512 two_b_sq = _mm512_fmsub_ps(r_sum, r_sum, sb_jsq);
|
||||
|
||||
two_b_sq = _mm512_max_ps(two_b_sq, v_eps_sq);
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 two_b = _mm512_sqrt_ps(two_b_sq);
|
||||
|
||||
// # 16 MULTs
|
||||
__m512 e_arg = _mm512_mul_ps(two_b, v_neg_2s_inv);
|
||||
|
||||
e_arg = _mm512_max_ps(e_arg, v_exp_min);
|
||||
|
||||
// # 256 MULTs, 224 ADDs
|
||||
__m512 e_val = _avx512_exp_ps(e_arg);
|
||||
|
||||
// # 64 MULTs
|
||||
__m512 numer = _mm512_mul_ps(_mm512_mul_ps(r_sum, v_neg_v0_div_2s), e_val);
|
||||
__m512 denom = _mm512_mul_ps(_mm512_mul_ps(two_b, r_ab_n), r_ab2_n);
|
||||
|
||||
__m512 denom_safe = _mm512_mask_blend_ps(active, v_one, denom);
|
||||
|
||||
// # 16 DIVs
|
||||
__m512 smc = _mm512_div_ps(numer, denom_safe);
|
||||
|
||||
// # 32 MULTs
|
||||
__m512 sm_r_ab = _mm512_mul_ps(smc, r_ab2_n);
|
||||
__m512 sm_r_ab2 = _mm512_mul_ps(smc, r_ab_n);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__m512 f_x = _mm512_fmadd_ps(r_ab2_x, sm_r_ab2, _mm512_mul_ps(r_ab_x, sm_r_ab));
|
||||
__m512 f_y = _mm512_fmadd_ps(r_ab2_y, sm_r_ab2, _mm512_mul_ps(r_ab_y, sm_r_ab));
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 f_norm_sq = _mm512_fmadd_ps(f_x, f_x, _mm512_mul_ps(f_y, f_y));
|
||||
|
||||
// # 16 MULTs
|
||||
__m512 rhs = _mm512_mul_ps(f_norm_sq, v_cos_phi_sq);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 e_dot_f = _mm512_fmadd_ps(eix, f_x, _mm512_mul_ps(eiy, f_y));
|
||||
|
||||
// # 16 MULTs
|
||||
__m512 e_dot_f_sq = _mm512_mul_ps(e_dot_f, e_dot_f);
|
||||
|
||||
__mmask16 c1 = _mm512_cmp_ps_mask(e_dot_f, v_zero, _CMP_LT_OQ);
|
||||
__mmask16 c2 = _mm512_cmp_ps_mask(e_dot_f_sq, rhs, _CMP_GT_OQ);
|
||||
__mmask16 cond = c1 & c2;
|
||||
|
||||
__m512 w_val = _mm512_mask_blend_ps(cond, v_dt_neg, v_c_neg_dt);
|
||||
|
||||
__m512 f_x_safe = _mm512_maskz_mov_ps(active, f_x);
|
||||
__m512 f_y_safe = _mm512_maskz_mov_ps(active, f_y);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
acc_x = _mm512_fmadd_ps(f_x_safe, w_val, acc_x);
|
||||
acc_y = _mm512_fmadd_ps(f_y_safe, w_val, acc_y);
|
||||
}
|
||||
|
||||
// # 32 ADDs
|
||||
f_alpha_dt_x += _mm512_reduce_add_ps(acc_x);
|
||||
f_alpha_dt_y += _mm512_reduce_add_ps(acc_y);
|
||||
|
||||
for (int k = 0; k < board.n_obstacles; k++) {
|
||||
|
||||
// # 2 ADDs
|
||||
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;
|
||||
|
||||
// # 3 MULTs, 1 ADD
|
||||
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;
|
||||
|
||||
// # 4 ADDs, 2 MULTs
|
||||
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);
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
float r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
|
||||
|
||||
// # 2 MULTs, 1 EXP, 1 DIV
|
||||
float scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
|
||||
|
||||
// # 2 MULTs
|
||||
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;
|
||||
|
||||
// # 2 MULTs
|
||||
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;
|
||||
|
||||
// # 2 ADDs
|
||||
f_alpha_dt_x += f_alpha_B_dt_x;
|
||||
f_alpha_dt_y += f_alpha_B_dt_y;
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
w_x[i] += f_alpha_dt_x;
|
||||
w_y[i] += f_alpha_dt_y;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
// # 9 MULTs, 3 ADDs
|
||||
float w_norm_inv = fast_inv_sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]);
|
||||
|
||||
// # 1 MULT
|
||||
float correction_factor = min(1.0f, v_max[i] * w_norm_inv);
|
||||
|
||||
// # 2 MULTs
|
||||
curr_vel_x[i] = w_x[i] * correction_factor;
|
||||
curr_vel_y[i] = w_y[i] * correction_factor;
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt;
|
||||
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt;
|
||||
|
||||
if (color[i] == 0 && pos_x[i] > (BOARD_LENGTH_MINUS_ONE_POINT_FIVE)) {
|
||||
pos_x[i] = 1.0f;
|
||||
destination_y[i] = pos_y[i];
|
||||
w_x[i] = desired_v0[i];
|
||||
w_y[i] = 0.0f;
|
||||
curr_vel_x[i] = w_x[i];
|
||||
curr_vel_y[i] = w_y[i];
|
||||
}
|
||||
if (color[i] == 1 && pos_x[i] < 1.5f) {
|
||||
pos_x[i] = BOARD_LENGTH_MINUS_ONE;
|
||||
destination_y[i] = pos_y[i];
|
||||
w_x[i] = -desired_v0[i];
|
||||
w_y[i] = 0.0f;
|
||||
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);
|
||||
|
||||
// # 2 ADDs
|
||||
float ex = destination_x[i] - pos_x[i];
|
||||
float ey = destination_y[i] - pos_y[i];
|
||||
|
||||
// # 9 MULTs, 3 ADDs
|
||||
float e_norm_inv = fast_inv_sqrt(ex * ex + ey * ey);
|
||||
|
||||
// # 2 MULTs
|
||||
e_x[i] = ex * e_norm_inv;
|
||||
e_y[i] = ey * e_norm_inv;
|
||||
|
||||
// # 2 MULTs, 1 ADD
|
||||
double velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i];
|
||||
|
||||
// # 1 MULT
|
||||
double sb_sq_i = velocity_norm_sq * delta_t_sq;
|
||||
|
||||
// # 1 SQRT
|
||||
double velocity_norm_mul_delta_t = sqrt(sb_sq_i);
|
||||
|
||||
sb_sq[i] = sb_sq_i;
|
||||
|
||||
// # 2 MULTs
|
||||
sb_e_x[i] = e_x[i] * velocity_norm_mul_delta_t;
|
||||
sb_e_y[i] = e_y[i] * velocity_norm_mul_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;
|
||||
}
|
||||
@@ -0,0 +1,577 @@
|
||||
// # Changes: Added more correction rounds to _avx512_exp_ps to do 30 flops per scalar
|
||||
// # Predecessor: variant_float_inlining_SoA_AVX512_polyexp_moreavx.cpp
|
||||
|
||||
#include "../headers/board_reduced_float.hpp"
|
||||
#include "../headers/constants_float.hpp"
|
||||
#include "../headers/pedestrian_reduced_float.hpp"
|
||||
|
||||
#include <immintrin.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
|
||||
#define SEED 42
|
||||
#define BOARD_WIDTH 10.0f
|
||||
#define BOARD_LENGTH 50.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
|
||||
#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<float> pos_x, pos_y;
|
||||
alignas(64) vector<float> sb_sq;
|
||||
alignas(64) vector<float> sb_e_x, sb_e_y;
|
||||
alignas(64) vector<float> e_x, e_y;
|
||||
alignas(64) vector<float> curr_vel_x, curr_vel_y;
|
||||
alignas(64) vector<float> w_x, w_y;
|
||||
alignas(64) vector<float> desired_v0;
|
||||
alignas(64) vector<float> v_max;
|
||||
alignas(64) vector<float> destination_x, destination_y;
|
||||
alignas(64) 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);
|
||||
|
||||
const int n_p_pad = (n_p + 15) & ~15;
|
||||
|
||||
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); }
|
||||
|
||||
// # 256 MULTs, 224 ADDs
|
||||
static inline __m512 _avx512_exp_ps(__m512 x) {
|
||||
|
||||
const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
|
||||
|
||||
// # 16 MULTs
|
||||
__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);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
|
||||
|
||||
const __m512 c13 = _mm512_set1_ps(c13_scalar);
|
||||
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);
|
||||
|
||||
__m512 p = c13;
|
||||
|
||||
// # 13 * (16 MULTs, 16 ADDs)
|
||||
// # = 208 MULTs, 208 ADDs
|
||||
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);
|
||||
|
||||
__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);
|
||||
|
||||
// # 16 MULTs
|
||||
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<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
|
||||
|
||||
const int n_p_pad = (n_p + 15) & ~15;
|
||||
|
||||
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);
|
||||
|
||||
// # ADDs: n_iter * (
|
||||
// # n_p * 36 +
|
||||
// # n_p * n_p_pad * 29 +
|
||||
// # n_p * board.n_obstacles * 10 +
|
||||
// # n_p_pad * 7
|
||||
// # )
|
||||
|
||||
// # MULTs: n_iter * (
|
||||
// # n_p * 4 +
|
||||
// # n_p * n_p_pad * 40 +
|
||||
// # n_p * board.n_obstacles * 13 +
|
||||
// # n_p_pad * 15
|
||||
// # )
|
||||
|
||||
// # DIVs: n_iter * (
|
||||
// # n_p * n_p_pad +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p_pad * 2
|
||||
// # )
|
||||
|
||||
// # SQRTs: n_iter * (
|
||||
// # n_p * n_p_pad * 3 +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p_pad * 3
|
||||
// # )
|
||||
|
||||
// # EXPs: n_iter * n_p * board.n_obstacles
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
|
||||
// # 2 MULTs
|
||||
float desired_velocity_x = e_x[i] * desired_v0[i];
|
||||
float desired_velocity_y = e_y[i] * desired_v0[i];
|
||||
|
||||
// # 2 ADDs, 2 MULTs
|
||||
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;
|
||||
|
||||
const __m512 pix = _mm512_set1_ps(pos_x[i]);
|
||||
const __m512 piy = _mm512_set1_ps(pos_y[i]);
|
||||
const __m512 eix = _mm512_set1_ps(e_x[i]);
|
||||
const __m512 eiy = _mm512_set1_ps(e_y[i]);
|
||||
const __m512 v_i = _mm512_set1_ps((float)i);
|
||||
|
||||
__m512 acc_x = _mm512_setzero_ps();
|
||||
__m512 acc_y = _mm512_setzero_ps();
|
||||
|
||||
for (int j = 0; j < n_p_pad; j += 16) {
|
||||
|
||||
// # 16 ADDs
|
||||
__m512 j_idx = _mm512_add_ps(_mm512_set1_ps((float)j), lane_off);
|
||||
|
||||
__mmask16 m_neq_i = _mm512_cmp_ps_mask(j_idx, v_i, _CMP_NEQ_OQ);
|
||||
__mmask16 m_in_range = _mm512_cmp_ps_mask(j_idx, v_np, _CMP_LT_OQ);
|
||||
__mmask16 active = m_neq_i & m_in_range;
|
||||
|
||||
__m512 pjx = _mm512_loadu_ps(&pos_x[j]);
|
||||
__m512 pjy = _mm512_loadu_ps(&pos_y[j]);
|
||||
__m512 sbe_jx = _mm512_loadu_ps(&sb_e_x[j]);
|
||||
__m512 sbe_jy = _mm512_loadu_ps(&sb_e_y[j]);
|
||||
__m512 sb_jsq = _mm512_loadu_ps(&sb_sq[j]);
|
||||
|
||||
// # 32 ADDs
|
||||
__m512 r_ab_x = _mm512_sub_ps(pix, pjx);
|
||||
__m512 r_ab_y = _mm512_sub_ps(piy, pjy);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 r_ab_n2 = _mm512_fmadd_ps(r_ab_x, r_ab_x, _mm512_mul_ps(r_ab_y, r_ab_y));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 r_ab_n = _mm512_sqrt_ps(r_ab_n2);
|
||||
|
||||
r_ab_n = _mm512_max_ps(r_ab_n, v_eps);
|
||||
|
||||
// # 32 ADDs
|
||||
__m512 r_ab2_x = _mm512_sub_ps(r_ab_x, sbe_jx);
|
||||
__m512 r_ab2_y = _mm512_sub_ps(r_ab_y, sbe_jy);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 r_ab2_n2 = _mm512_fmadd_ps(r_ab2_x, r_ab2_x, _mm512_mul_ps(r_ab2_y, r_ab2_y));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 r_ab2_n = _mm512_sqrt_ps(r_ab2_n2);
|
||||
|
||||
r_ab2_n = _mm512_max_ps(r_ab2_n, v_eps);
|
||||
|
||||
// # 16 ADDs
|
||||
__m512 r_sum = _mm512_add_ps(r_ab_n, r_ab2_n);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
__m512 two_b_sq = _mm512_fmsub_ps(r_sum, r_sum, sb_jsq);
|
||||
|
||||
two_b_sq = _mm512_max_ps(two_b_sq, v_eps_sq);
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 two_b = _mm512_sqrt_ps(two_b_sq);
|
||||
|
||||
// # 16 MULTs
|
||||
__m512 e_arg = _mm512_mul_ps(two_b, v_neg_2s_inv);
|
||||
|
||||
e_arg = _mm512_max_ps(e_arg, v_exp_min);
|
||||
|
||||
// # 256 MULTs, 224 ADDs
|
||||
__m512 e_val = _avx512_exp_ps(e_arg);
|
||||
|
||||
// # 64 MULTs
|
||||
__m512 numer = _mm512_mul_ps(_mm512_mul_ps(r_sum, v_neg_v0_div_2s), e_val);
|
||||
__m512 denom = _mm512_mul_ps(_mm512_mul_ps(two_b, r_ab_n), r_ab2_n);
|
||||
__m512 denom_safe = _mm512_mask_blend_ps(active, v_one, denom);
|
||||
|
||||
// # 16 DIVs
|
||||
__m512 smc = _mm512_div_ps(numer, denom_safe);
|
||||
|
||||
// # 32 MULTs
|
||||
__m512 sm_r_ab = _mm512_mul_ps(smc, r_ab2_n);
|
||||
__m512 sm_r_ab2 = _mm512_mul_ps(smc, r_ab_n);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__m512 f_x = _mm512_fmadd_ps(r_ab2_x, sm_r_ab2, _mm512_mul_ps(r_ab_x, sm_r_ab));
|
||||
__m512 f_y = _mm512_fmadd_ps(r_ab2_y, sm_r_ab2, _mm512_mul_ps(r_ab_y, sm_r_ab));
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 f_norm_sq = _mm512_fmadd_ps(f_x, f_x, _mm512_mul_ps(f_y, f_y));
|
||||
|
||||
// # 16 MULTs
|
||||
__m512 rhs = _mm512_mul_ps(f_norm_sq, v_cos_phi_sq);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 e_dot_f = _mm512_fmadd_ps(eix, f_x, _mm512_mul_ps(eiy, f_y));
|
||||
|
||||
// # 16 MULTs
|
||||
__m512 e_dot_f_sq = _mm512_mul_ps(e_dot_f, e_dot_f);
|
||||
|
||||
__mmask16 c1 = _mm512_cmp_ps_mask(e_dot_f, v_zero, _CMP_LT_OQ);
|
||||
__mmask16 c2 = _mm512_cmp_ps_mask(e_dot_f_sq, rhs, _CMP_GT_OQ);
|
||||
__mmask16 cond = c1 & c2;
|
||||
|
||||
__m512 w_val = _mm512_mask_blend_ps(cond, v_dt_neg, v_c_neg_dt);
|
||||
|
||||
__m512 f_x_safe = _mm512_maskz_mov_ps(active, f_x);
|
||||
__m512 f_y_safe = _mm512_maskz_mov_ps(active, f_y);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
acc_x = _mm512_fmadd_ps(f_x_safe, w_val, acc_x);
|
||||
acc_y = _mm512_fmadd_ps(f_y_safe, w_val, acc_y);
|
||||
}
|
||||
|
||||
// # 32 ADDs
|
||||
f_alpha_dt_x += _mm512_reduce_add_ps(acc_x);
|
||||
f_alpha_dt_y += _mm512_reduce_add_ps(acc_y);
|
||||
|
||||
for (int k = 0; k < board.n_obstacles; k++) {
|
||||
|
||||
// # 2 ADDs
|
||||
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;
|
||||
|
||||
// # 3 MULTs, 1 ADD
|
||||
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;
|
||||
|
||||
// # 4 ADDs, 2 MULTs
|
||||
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);
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
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;
|
||||
|
||||
// # 4 MULTs, 1 EXP, 1 DIV
|
||||
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;
|
||||
|
||||
// # 2 MULTS
|
||||
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;
|
||||
|
||||
// # 2 ADDs
|
||||
f_alpha_dt_x += f_alpha_B_dt_x;
|
||||
f_alpha_dt_y += f_alpha_B_dt_y;
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
w_x[i] += f_alpha_dt_x;
|
||||
w_y[i] += f_alpha_dt_y;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n_p_pad; i += 16) {
|
||||
__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]);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__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));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 w_norm = _mm512_sqrt_ps(w_norm_sq);
|
||||
__m512 w_norm_safe = _mm512_max_ps(w_norm, _mm512_set1_ps(EPS));
|
||||
|
||||
// # 16 DIVs
|
||||
__m512 correction_factor = _mm512_min_ps(_mm512_set1_ps(1.0f), _mm512_div_ps(v_max_i_vec, w_norm_safe));
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
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);
|
||||
|
||||
__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_MINUS_ONE_POINT_FIVE), _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_MINUS_ONE));
|
||||
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));
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 e_norm_sq = _mm512_fmadd_ps(ex_vec, ex_vec, _mm512_mul_ps(ey_vec, ey_vec));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 e_norm = _mm512_sqrt_ps(e_norm_sq);
|
||||
|
||||
// # 16 DIVs
|
||||
__m512 e_norm_inv = _mm512_div_ps(_mm512_set1_ps(1.0f), e_norm);
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__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));
|
||||
|
||||
// # 16 MULTs
|
||||
__m512 sb_sq_i_vec = _mm512_mul_ps(vel_norm_sq, _mm512_set1_ps(delta_t_sq));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 vel_norm_dt = _mm512_sqrt_ps(sb_sq_i_vec);
|
||||
|
||||
_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);
|
||||
|
||||
// # 32 MULTs
|
||||
_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;
|
||||
}
|
||||
@@ -0,0 +1,638 @@
|
||||
// # Changes: Added ILP with k = 2 to j-loop, removed EPS checks where possible
|
||||
// # Predecessor: variant_float_inlining_SoA_AVX512_polyexp_moreavx.cpp
|
||||
|
||||
#include "../headers/board_reduced_float.hpp"
|
||||
#include "../headers/constants_float.hpp"
|
||||
#include "../headers/pedestrian_reduced_float.hpp"
|
||||
|
||||
#include <immintrin.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
|
||||
#define SEED 42
|
||||
#define BOARD_WIDTH 10.0f
|
||||
#define BOARD_LENGTH 50.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
|
||||
#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<float> pos_x, pos_y;
|
||||
alignas(64) vector<float> sb_sq;
|
||||
alignas(64) vector<float> sb_e_x, sb_e_y;
|
||||
alignas(64) vector<float> e_x, e_y;
|
||||
alignas(64) vector<float> curr_vel_x, curr_vel_y;
|
||||
alignas(64) vector<float> w_x, w_y;
|
||||
alignas(64) vector<float> desired_v0;
|
||||
alignas(64) vector<float> v_max;
|
||||
alignas(64) vector<float> destination_x, destination_y;
|
||||
alignas(64) 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);
|
||||
|
||||
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); }
|
||||
|
||||
// # 256 MULTs, 224 ADDs
|
||||
static inline __m512 _avx512_exp_ps(__m512 x) {
|
||||
|
||||
const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
|
||||
|
||||
// # 16 MULTs
|
||||
__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);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
|
||||
|
||||
const __m512 c13 = _mm512_set1_ps(c13_scalar);
|
||||
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);
|
||||
|
||||
__m512 p = c13;
|
||||
|
||||
// # 13 * (16 MULTs, 16 ADDs)
|
||||
// # = 208 MULTs, 208 ADDs
|
||||
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);
|
||||
|
||||
__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);
|
||||
|
||||
// # 16 MULTs
|
||||
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<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
|
||||
|
||||
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);
|
||||
|
||||
// # ADDs: n_iter * (
|
||||
// # n_p * 68 +
|
||||
// # n_p * n_p_pad * 29 +
|
||||
// # n_p * board.n_obstacles * 10 +
|
||||
// # n_p_pad * 7
|
||||
// # )
|
||||
|
||||
// # MULTs: n_iter * (
|
||||
// # n_p * 4 +
|
||||
// # n_p * n_p_pad * 40 +
|
||||
// # n_p * board.n_obstacles * 13 +
|
||||
// # n_p_pad * 15
|
||||
// # )
|
||||
|
||||
// # SQRTs: n_iter * (
|
||||
// # n_p * n_p_pad * 3 +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p_pad * 3
|
||||
// # )
|
||||
|
||||
// # DIVs: n_iter * (
|
||||
// # n_p * n_p_pad +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p_pad * 2
|
||||
// # )
|
||||
|
||||
// # EXPs: n_iter * n_p * board.n_obstacles
|
||||
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
|
||||
// # 2 MULTs
|
||||
float desired_velocity_x = e_x[i] * desired_v0[i];
|
||||
float desired_velocity_y = e_y[i] * desired_v0[i];
|
||||
|
||||
// # 2 ADDs, 2 MULTs
|
||||
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;
|
||||
|
||||
const __m512 pix = _mm512_set1_ps(pos_x[i]);
|
||||
const __m512 piy = _mm512_set1_ps(pos_y[i]);
|
||||
const __m512 eix = _mm512_set1_ps(e_x[i]);
|
||||
const __m512 eiy = _mm512_set1_ps(e_y[i]);
|
||||
const __m512 v_i = _mm512_set1_ps((float)i);
|
||||
|
||||
__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();
|
||||
|
||||
for (int j = 0; j < n_p_pad; j += 32) {
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
__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);
|
||||
|
||||
__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);
|
||||
|
||||
__mmask16 active_0 = m_neq_i_0 & m_in_range_0;
|
||||
__mmask16 active_1 = m_neq_i_1 & m_in_range_1;
|
||||
|
||||
__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]);
|
||||
|
||||
__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]);
|
||||
|
||||
__m512 sb_jsq_0 = _mm512_loadu_ps(&sb_sq[j]);
|
||||
__m512 sb_jsq_1 = _mm512_loadu_ps(&sb_sq[j + 16]);
|
||||
|
||||
// # 32 ADDs
|
||||
__m512 r_ab_x_0 = _mm512_sub_ps(pix, pjx_0);
|
||||
__m512 r_ab_x_1 = _mm512_sub_ps(pix, pjx_1);
|
||||
|
||||
// # 32 ADDs
|
||||
__m512 r_ab_y_0 = _mm512_sub_ps(piy, pjy_0);
|
||||
__m512 r_ab_y_1 = _mm512_sub_ps(piy, pjy_1);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 32 SQRTs
|
||||
__m512 r_ab_n_0 = _mm512_sqrt_ps(r_ab_n2_0);
|
||||
__m512 r_ab_n_1 = _mm512_sqrt_ps(r_ab_n2_1);
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 32 SQRTs
|
||||
__m512 r_ab2_n_0 = _mm512_sqrt_ps(r_ab2_n2_0);
|
||||
__m512 r_ab2_n_1 = _mm512_sqrt_ps(r_ab2_n2_1);
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 32 SQRTs
|
||||
__m512 two_b_0 = _mm512_sqrt_ps(two_b_sq_0);
|
||||
__m512 two_b_1 = _mm512_sqrt_ps(two_b_sq_1);
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
e_arg_0 = _mm512_max_ps(e_arg_0, v_exp_min);
|
||||
e_arg_1 = _mm512_max_ps(e_arg_1, v_exp_min);
|
||||
|
||||
// # 512 MULTs, 448 ADDs
|
||||
__m512 e_val_0 = _avx512_exp_ps(e_arg_0);
|
||||
__m512 e_val_1 = _avx512_exp_ps(e_arg_1);
|
||||
|
||||
// # 64 MULTs
|
||||
__m512 numer_0 = _mm512_mul_ps(_mm512_mul_ps(r_sum_0, v_neg_v0_div_2s), e_val_0);
|
||||
__m512 numer_1 = _mm512_mul_ps(_mm512_mul_ps(r_sum_1, v_neg_v0_div_2s), e_val_1);
|
||||
|
||||
// # 64 MULTs
|
||||
__m512 denom_0 = _mm512_mul_ps(_mm512_mul_ps(two_b_0, r_ab_n_0), r_ab2_n_0);
|
||||
__m512 denom_1 = _mm512_mul_ps(_mm512_mul_ps(two_b_1, r_ab_n_1), r_ab2_n_1);
|
||||
|
||||
__m512 denom_safe_0 = _mm512_mask_blend_ps(active_0, v_one, denom_0);
|
||||
__m512 denom_safe_1 = _mm512_mask_blend_ps(active_1, v_one, denom_1);
|
||||
|
||||
// # 32 DIVs
|
||||
__m512 smc_0 = _mm512_div_ps(numer_0, denom_safe_0);
|
||||
__m512 smc_1 = _mm512_div_ps(numer_1, denom_safe_1);
|
||||
|
||||
// # 32 MULTs
|
||||
__m512 sm_r_ab_0 = _mm512_mul_ps(smc_0, r_ab2_n_0);
|
||||
__m512 sm_r_ab_1 = _mm512_mul_ps(smc_1, r_ab2_n_1);
|
||||
|
||||
// # 32 MULTs
|
||||
__m512 sm_r_ab2_0 = _mm512_mul_ps(smc_0, r_ab_n_0);
|
||||
__m512 sm_r_ab2_1 = _mm512_mul_ps(smc_1, r_ab_n_1);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
__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);
|
||||
|
||||
__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);
|
||||
|
||||
__mmask16 cond_0 = c1_0 & c2_0;
|
||||
__mmask16 cond_1 = c1_1 & c2_1;
|
||||
|
||||
__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);
|
||||
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
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);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
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);
|
||||
}
|
||||
|
||||
// # 64 ADDs
|
||||
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));
|
||||
|
||||
for (int k = 0; k < board.n_obstacles; k++) {
|
||||
|
||||
// # 2 ADDs
|
||||
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;
|
||||
|
||||
// # 3 MULTs, 1 ADD
|
||||
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;
|
||||
|
||||
// # 4 ADDs, 2 MULTs
|
||||
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);
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
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 EXP, 4 MULTs, 1 DIV
|
||||
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;
|
||||
|
||||
// # 2 MULTs
|
||||
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;
|
||||
|
||||
// # 2 ADDs
|
||||
f_alpha_dt_x += f_alpha_B_dt_x;
|
||||
f_alpha_dt_y += f_alpha_B_dt_y;
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
w_x[i] += f_alpha_dt_x;
|
||||
w_y[i] += f_alpha_dt_y;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n_p_pad; i += 16) {
|
||||
|
||||
__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]);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__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));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 w_norm = _mm512_sqrt_ps(w_norm_sq);
|
||||
|
||||
// # 16 DIVs
|
||||
__m512 correction_factor = _mm512_min_ps(_mm512_set1_ps(1.0f), _mm512_div_ps(v_max_i_vec, w_norm));
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
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);
|
||||
|
||||
__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_MINUS_ONE_POINT_FIVE), _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_MINUS_ONE));
|
||||
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));
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 e_norm_sq = _mm512_fmadd_ps(ex_vec, ex_vec, _mm512_mul_ps(ey_vec, ey_vec));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 e_norm = _mm512_sqrt_ps(e_norm_sq);
|
||||
|
||||
// # 16 DIVs
|
||||
__m512 e_norm_inv = _mm512_div_ps(_mm512_set1_ps(1.0f), e_norm);
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__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));
|
||||
|
||||
// # 16 MULTs
|
||||
__m512 sb_sq_i_vec = _mm512_mul_ps(vel_norm_sq, _mm512_set1_ps(delta_t_sq));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 vel_norm_dt = _mm512_sqrt_ps(sb_sq_i_vec);
|
||||
|
||||
_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);
|
||||
|
||||
// # 32 MULTs
|
||||
_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;
|
||||
}
|
||||
@@ -0,0 +1,735 @@
|
||||
// # Changes: Added ILP with k = 2 to second pass
|
||||
// # Predecessor: variant_float_inlining_SoA_AVX512_polyexp_moreavx_ilp_2.cpp
|
||||
|
||||
#include "../headers/board_reduced_float.hpp"
|
||||
#include "../headers/constants_float.hpp"
|
||||
#include "../headers/pedestrian_reduced_float.hpp"
|
||||
|
||||
#include <immintrin.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
|
||||
#define SEED 42
|
||||
#define BOARD_WIDTH 10.0f
|
||||
#define BOARD_LENGTH 50.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
|
||||
#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<float> pos_x, pos_y;
|
||||
alignas(64) vector<float> sb_sq;
|
||||
alignas(64) vector<float> sb_e_x, sb_e_y;
|
||||
alignas(64) vector<float> e_x, e_y;
|
||||
alignas(64) vector<float> curr_vel_x, curr_vel_y;
|
||||
alignas(64) vector<float> w_x, w_y;
|
||||
alignas(64) vector<float> desired_v0;
|
||||
alignas(64) vector<float> v_max;
|
||||
alignas(64) vector<float> destination_x, destination_y;
|
||||
alignas(64) 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);
|
||||
|
||||
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); }
|
||||
|
||||
// # 256 MULTs, 224 ADDs
|
||||
static inline __m512 _avx512_exp_ps(__m512 x) {
|
||||
|
||||
const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
|
||||
|
||||
// # 16 MULTs
|
||||
__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);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
|
||||
|
||||
const __m512 c13 = _mm512_set1_ps(c13_scalar);
|
||||
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);
|
||||
|
||||
__m512 p = c13;
|
||||
|
||||
// # 13 * (16 MULTs, 16 ADDs)
|
||||
// # = 208 MULTs, 208 ADDs
|
||||
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);
|
||||
|
||||
__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);
|
||||
|
||||
// # 16 MULTs
|
||||
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<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
|
||||
|
||||
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);
|
||||
|
||||
// # ADDs: n_iter * (
|
||||
// # n_p * 68 +
|
||||
// # n_p * n_p_pad * 29 +
|
||||
// # n_p * board.n_obstacles * 10 +
|
||||
// # n_p_pad * 7
|
||||
// # )
|
||||
|
||||
// # MULTs: n_iter * (
|
||||
// # n_p * 4 +
|
||||
// # n_p * n_p_pad * 40 +
|
||||
// # n_p * board.n_obstacles * 13 +
|
||||
// # n_p_pad * 15
|
||||
// # )
|
||||
|
||||
// # DIVs: n_iter * (
|
||||
// # n_p * n_p_pad +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p_pad * 2
|
||||
// # )
|
||||
|
||||
// # SQRTs: n_iter * (
|
||||
// # n_p * n_p_pad * 3 +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p_pad * 3
|
||||
// # )
|
||||
|
||||
// # EXPs: n_iter * n_p * board.n_obstacles
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
|
||||
// # 2 MULTs
|
||||
float desired_velocity_x = e_x[i] * desired_v0[i];
|
||||
float desired_velocity_y = e_y[i] * desired_v0[i];
|
||||
|
||||
// # 2 ADDs, 2 MULTs
|
||||
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;
|
||||
|
||||
const __m512 pix = _mm512_set1_ps(pos_x[i]);
|
||||
const __m512 piy = _mm512_set1_ps(pos_y[i]);
|
||||
const __m512 eix = _mm512_set1_ps(e_x[i]);
|
||||
const __m512 eiy = _mm512_set1_ps(e_y[i]);
|
||||
const __m512 v_i = _mm512_set1_ps((float)i);
|
||||
|
||||
__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();
|
||||
|
||||
for (int j = 0; j < n_p_pad; j += 32) {
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
__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);
|
||||
|
||||
__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);
|
||||
|
||||
__mmask16 active_0 = m_neq_i_0 & m_in_range_0;
|
||||
__mmask16 active_1 = m_neq_i_1 & m_in_range_1;
|
||||
|
||||
__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]);
|
||||
|
||||
__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]);
|
||||
|
||||
__m512 sb_jsq_0 = _mm512_loadu_ps(&sb_sq[j]);
|
||||
__m512 sb_jsq_1 = _mm512_loadu_ps(&sb_sq[j + 16]);
|
||||
|
||||
// # 32 ADDs
|
||||
__m512 r_ab_x_0 = _mm512_sub_ps(pix, pjx_0);
|
||||
__m512 r_ab_x_1 = _mm512_sub_ps(pix, pjx_1);
|
||||
|
||||
// # 32 ADDs
|
||||
__m512 r_ab_y_0 = _mm512_sub_ps(piy, pjy_0);
|
||||
__m512 r_ab_y_1 = _mm512_sub_ps(piy, pjy_1);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 32 SQRTs
|
||||
__m512 r_ab_n_0 = _mm512_sqrt_ps(r_ab_n2_0);
|
||||
__m512 r_ab_n_1 = _mm512_sqrt_ps(r_ab_n2_1);
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 32 SQRTs
|
||||
__m512 r_ab2_n_0 = _mm512_sqrt_ps(r_ab2_n2_0);
|
||||
__m512 r_ab2_n_1 = _mm512_sqrt_ps(r_ab2_n2_1);
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 32 SQRTs
|
||||
__m512 two_b_0 = _mm512_sqrt_ps(two_b_sq_0);
|
||||
__m512 two_b_1 = _mm512_sqrt_ps(two_b_sq_1);
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
e_arg_0 = _mm512_max_ps(e_arg_0, v_exp_min);
|
||||
e_arg_1 = _mm512_max_ps(e_arg_1, v_exp_min);
|
||||
|
||||
// # 512 MULTs, 448 ADDs
|
||||
__m512 e_val_0 = _avx512_exp_ps(e_arg_0);
|
||||
__m512 e_val_1 = _avx512_exp_ps(e_arg_1);
|
||||
|
||||
// # 64 MULTs
|
||||
__m512 numer_0 = _mm512_mul_ps(_mm512_mul_ps(r_sum_0, v_neg_v0_div_2s), e_val_0);
|
||||
__m512 numer_1 = _mm512_mul_ps(_mm512_mul_ps(r_sum_1, v_neg_v0_div_2s), e_val_1);
|
||||
|
||||
// # 64 MULTs
|
||||
__m512 denom_0 = _mm512_mul_ps(_mm512_mul_ps(two_b_0, r_ab_n_0), r_ab2_n_0);
|
||||
__m512 denom_1 = _mm512_mul_ps(_mm512_mul_ps(two_b_1, r_ab_n_1), r_ab2_n_1);
|
||||
|
||||
__m512 denom_safe_0 = _mm512_mask_blend_ps(active_0, v_one, denom_0);
|
||||
__m512 denom_safe_1 = _mm512_mask_blend_ps(active_1, v_one, denom_1);
|
||||
|
||||
// # 32 DIVs
|
||||
__m512 smc_0 = _mm512_div_ps(numer_0, denom_safe_0);
|
||||
__m512 smc_1 = _mm512_div_ps(numer_1, denom_safe_1);
|
||||
|
||||
// # 32 MULTs
|
||||
__m512 sm_r_ab_0 = _mm512_mul_ps(smc_0, r_ab2_n_0);
|
||||
__m512 sm_r_ab_1 = _mm512_mul_ps(smc_1, r_ab2_n_1);
|
||||
|
||||
// # 32 MULTs
|
||||
__m512 sm_r_ab2_0 = _mm512_mul_ps(smc_0, r_ab_n_0);
|
||||
__m512 sm_r_ab2_1 = _mm512_mul_ps(smc_1, r_ab_n_1);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
__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);
|
||||
|
||||
__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);
|
||||
|
||||
__mmask16 cond_0 = c1_0 & c2_0;
|
||||
__mmask16 cond_1 = c1_1 & c2_1;
|
||||
|
||||
__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);
|
||||
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
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);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
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);
|
||||
}
|
||||
|
||||
// # 64 ADDs
|
||||
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));
|
||||
|
||||
for (int k = 0; k < board.n_obstacles; k++) {
|
||||
|
||||
// # 2 ADDs
|
||||
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;
|
||||
|
||||
// # 3 MULTs, 1 ADD
|
||||
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;
|
||||
|
||||
// # 4 ADDs, 2 MULTs
|
||||
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);
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
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;
|
||||
|
||||
// # 4 MULTs, 1 EXP, 1 DIV
|
||||
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;
|
||||
|
||||
// # 2 MULTS
|
||||
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;
|
||||
|
||||
// # 2 ADDs
|
||||
f_alpha_dt_x += f_alpha_B_dt_x;
|
||||
f_alpha_dt_y += f_alpha_B_dt_y;
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
w_x[i] += f_alpha_dt_x;
|
||||
w_y[i] += f_alpha_dt_y;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n_p_pad; i += 32) {
|
||||
|
||||
__m512 w_x_i_vec_0 = _mm512_loadu_ps(&w_x[i]);
|
||||
__m512 w_x_i_vec_1 = _mm512_loadu_ps(&w_x[i + 16]);
|
||||
|
||||
__m512 w_y_i_vec_0 = _mm512_loadu_ps(&w_y[i]);
|
||||
__m512 w_y_i_vec_1 = _mm512_loadu_ps(&w_y[i + 16]);
|
||||
|
||||
__m512 v_max_i_vec_0 = _mm512_loadu_ps(&v_max[i]);
|
||||
__m512 v_max_i_vec_1 = _mm512_loadu_ps(&v_max[i + 16]);
|
||||
|
||||
__m512 pos_x_i_vec_0 = _mm512_loadu_ps(&pos_x[i]);
|
||||
__m512 pos_x_i_vec_1 = _mm512_loadu_ps(&pos_x[i + 16]);
|
||||
|
||||
__m512 pos_y_i_vec_0 = _mm512_loadu_ps(&pos_y[i]);
|
||||
__m512 pos_y_i_vec_1 = _mm512_loadu_ps(&pos_y[i + 16]);
|
||||
|
||||
__m512 destination_y_i_vec_0 = _mm512_loadu_ps(&destination_y[i]);
|
||||
__m512 destination_y_i_vec_1 = _mm512_loadu_ps(&destination_y[i + 16]);
|
||||
|
||||
__m512 destination_x_i_vec_0 = _mm512_loadu_ps(&destination_x[i]);
|
||||
__m512 destination_x_i_vec_1 = _mm512_loadu_ps(&destination_x[i + 16]);
|
||||
|
||||
__m512 desired_v0_i_vec_0 = _mm512_loadu_ps(&desired_v0[i]);
|
||||
__m512 desired_v0_i_vec_1 = _mm512_loadu_ps(&desired_v0[i + 16]);
|
||||
|
||||
__m512i color_i_vec_0 = _mm512_loadu_si512((__m512i *)&color[i]);
|
||||
__m512i color_i_vec_1 = _mm512_loadu_si512((__m512i *)&color[i + 16]);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__m512 w_norm_sq_0 = _mm512_fmadd_ps(w_x_i_vec_0, w_x_i_vec_0, _mm512_mul_ps(w_y_i_vec_0, w_y_i_vec_0));
|
||||
__m512 w_norm_sq_1 = _mm512_fmadd_ps(w_x_i_vec_1, w_x_i_vec_1, _mm512_mul_ps(w_y_i_vec_1, w_y_i_vec_1));
|
||||
|
||||
// # 32 SQRTs
|
||||
__m512 w_norm_0 = _mm512_sqrt_ps(w_norm_sq_0);
|
||||
__m512 w_norm_1 = _mm512_sqrt_ps(w_norm_sq_1);
|
||||
|
||||
// # 32 DIVs
|
||||
__m512 correction_factor_0 = _mm512_min_ps(_mm512_set1_ps(1.0f), _mm512_div_ps(v_max_i_vec_0, w_norm_0));
|
||||
__m512 correction_factor_1 = _mm512_min_ps(_mm512_set1_ps(1.0f), _mm512_div_ps(v_max_i_vec_1, w_norm_1));
|
||||
|
||||
// # 32 MULTs
|
||||
__m512 curr_vel_x_i_vec_0 = _mm512_mul_ps(w_x_i_vec_0, correction_factor_0);
|
||||
__m512 curr_vel_x_i_vec_1 = _mm512_mul_ps(w_x_i_vec_1, correction_factor_1);
|
||||
|
||||
// # 32 MULTs
|
||||
__m512 curr_vel_y_i_vec_0 = _mm512_mul_ps(w_y_i_vec_0, correction_factor_0);
|
||||
__m512 curr_vel_y_i_vec_1 = _mm512_mul_ps(w_y_i_vec_1, correction_factor_1);
|
||||
|
||||
__m512 dt_vec = _mm512_set1_ps(dt);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
pos_x_i_vec_0 = _mm512_fmadd_ps(curr_vel_x_i_vec_0, dt_vec, pos_x_i_vec_0);
|
||||
pos_x_i_vec_1 = _mm512_fmadd_ps(curr_vel_x_i_vec_1, dt_vec, pos_x_i_vec_1);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
pos_y_i_vec_0 = _mm512_fmadd_ps(curr_vel_y_i_vec_0, dt_vec, pos_y_i_vec_0);
|
||||
pos_y_i_vec_1 = _mm512_fmadd_ps(curr_vel_y_i_vec_1, dt_vec, pos_y_i_vec_1);
|
||||
|
||||
__mmask16 cond_1_left_0 = _mm512_cmpeq_epi32_mask(color_i_vec_0, _mm512_setzero_si512());
|
||||
__mmask16 cond_1_left_1 = _mm512_cmpeq_epi32_mask(color_i_vec_1, _mm512_setzero_si512());
|
||||
|
||||
__mmask16 cond_1_right_0 = _mm512_cmp_ps_mask(pos_x_i_vec_0, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE_POINT_FIVE), _CMP_GT_OQ);
|
||||
__mmask16 cond_1_right_1 = _mm512_cmp_ps_mask(pos_x_i_vec_1, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE_POINT_FIVE), _CMP_GT_OQ);
|
||||
|
||||
__mmask16 cond_1_0 = cond_1_left_0 & cond_1_right_0;
|
||||
__mmask16 cond_1_1 = cond_1_left_1 & cond_1_right_1;
|
||||
|
||||
pos_x_i_vec_0 = _mm512_mask_blend_ps(cond_1_0, pos_x_i_vec_0, _mm512_set1_ps(1.0f));
|
||||
pos_x_i_vec_1 = _mm512_mask_blend_ps(cond_1_1, pos_x_i_vec_1, _mm512_set1_ps(1.0f));
|
||||
|
||||
destination_y_i_vec_0 = _mm512_mask_blend_ps(cond_1_0, destination_y_i_vec_0, pos_y_i_vec_0);
|
||||
destination_y_i_vec_1 = _mm512_mask_blend_ps(cond_1_1, destination_y_i_vec_1, pos_y_i_vec_1);
|
||||
|
||||
w_x_i_vec_0 = _mm512_mask_blend_ps(cond_1_0, w_x_i_vec_0, desired_v0_i_vec_0);
|
||||
w_x_i_vec_1 = _mm512_mask_blend_ps(cond_1_1, w_x_i_vec_1, desired_v0_i_vec_1);
|
||||
|
||||
w_y_i_vec_0 = _mm512_mask_blend_ps(cond_1_0, w_y_i_vec_0, _mm512_setzero_ps());
|
||||
w_y_i_vec_1 = _mm512_mask_blend_ps(cond_1_1, w_y_i_vec_1, _mm512_setzero_ps());
|
||||
|
||||
curr_vel_x_i_vec_0 = _mm512_mask_blend_ps(cond_1_0, curr_vel_x_i_vec_0, w_x_i_vec_0);
|
||||
curr_vel_x_i_vec_1 = _mm512_mask_blend_ps(cond_1_1, curr_vel_x_i_vec_1, w_x_i_vec_1);
|
||||
|
||||
curr_vel_y_i_vec_0 = _mm512_mask_blend_ps(cond_1_0, curr_vel_y_i_vec_0, w_y_i_vec_0);
|
||||
curr_vel_y_i_vec_1 = _mm512_mask_blend_ps(cond_1_1, curr_vel_y_i_vec_1, w_y_i_vec_1);
|
||||
|
||||
__mmask16 cond_2_left_0 = _mm512_cmpeq_epi32_mask(color_i_vec_0, _mm512_set1_epi32(1));
|
||||
__mmask16 cond_2_left_1 = _mm512_cmpeq_epi32_mask(color_i_vec_1, _mm512_set1_epi32(1));
|
||||
|
||||
__mmask16 cond_2_right_0 = _mm512_cmp_ps_mask(pos_x_i_vec_0, _mm512_set1_ps(1.5f), _CMP_LT_OQ);
|
||||
__mmask16 cond_2_right_1 = _mm512_cmp_ps_mask(pos_x_i_vec_1, _mm512_set1_ps(1.5f), _CMP_LT_OQ);
|
||||
|
||||
__mmask16 cond_2_0 = cond_2_left_0 & cond_2_right_0;
|
||||
__mmask16 cond_2_1 = cond_2_left_1 & cond_2_right_1;
|
||||
|
||||
pos_x_i_vec_0 = _mm512_mask_blend_ps(cond_2_0, pos_x_i_vec_0, _mm512_set1_ps(BOARD_LENGTH - 1.0f));
|
||||
pos_x_i_vec_1 = _mm512_mask_blend_ps(cond_2_1, pos_x_i_vec_1, _mm512_set1_ps(BOARD_LENGTH - 1.0f));
|
||||
|
||||
destination_y_i_vec_0 = _mm512_mask_blend_ps(cond_2_0, destination_y_i_vec_0, pos_y_i_vec_0);
|
||||
destination_y_i_vec_1 = _mm512_mask_blend_ps(cond_2_1, destination_y_i_vec_1, pos_y_i_vec_1);
|
||||
|
||||
__m512 neg_desired_v0_0 = _mm512_xor_ps(desired_v0_i_vec_0, _mm512_set1_ps(-0.0f));
|
||||
__m512 neg_desired_v0_1 = _mm512_xor_ps(desired_v0_i_vec_1, _mm512_set1_ps(-0.0f));
|
||||
|
||||
w_x_i_vec_0 = _mm512_mask_blend_ps(cond_2_0, w_x_i_vec_0, neg_desired_v0_0);
|
||||
w_x_i_vec_1 = _mm512_mask_blend_ps(cond_2_1, w_x_i_vec_1, neg_desired_v0_1);
|
||||
|
||||
w_y_i_vec_0 = _mm512_mask_blend_ps(cond_2_0, w_y_i_vec_0, _mm512_setzero_ps());
|
||||
w_y_i_vec_1 = _mm512_mask_blend_ps(cond_2_1, w_y_i_vec_1, _mm512_setzero_ps());
|
||||
|
||||
curr_vel_x_i_vec_0 = _mm512_mask_blend_ps(cond_2_0, curr_vel_x_i_vec_0, w_x_i_vec_0);
|
||||
curr_vel_y_i_vec_1 = _mm512_mask_blend_ps(cond_2_1, curr_vel_y_i_vec_1, w_y_i_vec_1);
|
||||
|
||||
pos_y_i_vec_0 = _mm512_max_ps(pos_y_i_vec_0, _mm512_set1_ps(WALL_EPS));
|
||||
pos_y_i_vec_1 = _mm512_max_ps(pos_y_i_vec_1, _mm512_set1_ps(WALL_EPS));
|
||||
|
||||
pos_y_i_vec_0 = _mm512_min_ps(pos_y_i_vec_0, _mm512_set1_ps(BOARD_WIDTH_MINUS_WALL_EPS));
|
||||
pos_y_i_vec_1 = _mm512_min_ps(pos_y_i_vec_1, _mm512_set1_ps(BOARD_WIDTH_MINUS_WALL_EPS));
|
||||
|
||||
// # 32 ADDs
|
||||
__m512 ex_vec_0 = _mm512_sub_ps(destination_x_i_vec_0, pos_x_i_vec_0);
|
||||
__m512 ex_vec_1 = _mm512_sub_ps(destination_x_i_vec_1, pos_x_i_vec_1);
|
||||
|
||||
// # 32 ADDs
|
||||
__m512 ey_vec_0 = _mm512_sub_ps(destination_y_i_vec_0, pos_y_i_vec_0);
|
||||
__m512 ey_vec_1 = _mm512_sub_ps(destination_y_i_vec_1, pos_y_i_vec_1);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__m512 e_norm_sq_0 = _mm512_fmadd_ps(ex_vec_0, ex_vec_0, _mm512_mul_ps(ey_vec_0, ey_vec_0));
|
||||
__m512 e_norm_sq_1 = _mm512_fmadd_ps(ex_vec_1, ex_vec_1, _mm512_mul_ps(ey_vec_1, ey_vec_1));
|
||||
|
||||
// # 32 SQRTs
|
||||
__m512 e_norm_0 = _mm512_sqrt_ps(e_norm_sq_0);
|
||||
__m512 e_norm_1 = _mm512_sqrt_ps(e_norm_sq_1);
|
||||
|
||||
// # 32 DIVs
|
||||
__m512 e_norm_inv_0 = _mm512_div_ps(_mm512_set1_ps(1.0f), e_norm_0);
|
||||
__m512 e_norm_inv_1 = _mm512_div_ps(_mm512_set1_ps(1.0f), e_norm_1);
|
||||
|
||||
// # 32 MULTs
|
||||
__m512 e_x_i_vec_0 = _mm512_mul_ps(ex_vec_0, e_norm_inv_0);
|
||||
__m512 e_x_i_vec_1 = _mm512_mul_ps(ex_vec_1, e_norm_inv_1);
|
||||
|
||||
// # 32 MULTs
|
||||
__m512 e_y_i_vec_0 = _mm512_mul_ps(ey_vec_0, e_norm_inv_0);
|
||||
__m512 e_y_i_vec_1 = _mm512_mul_ps(ey_vec_1, e_norm_inv_1);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__m512 vel_norm_sq_0 = _mm512_fmadd_ps(curr_vel_x_i_vec_0, curr_vel_x_i_vec_0, _mm512_mul_ps(curr_vel_y_i_vec_0, curr_vel_y_i_vec_0));
|
||||
__m512 vel_norm_sq_1 = _mm512_fmadd_ps(curr_vel_x_i_vec_1, curr_vel_x_i_vec_1, _mm512_mul_ps(curr_vel_y_i_vec_1, curr_vel_y_i_vec_1));
|
||||
|
||||
// # 32 MULTs
|
||||
__m512 sb_sq_i_vec_0 = _mm512_mul_ps(vel_norm_sq_0, _mm512_set1_ps(delta_t_sq));
|
||||
__m512 sb_sq_i_vec_1 = _mm512_mul_ps(vel_norm_sq_1, _mm512_set1_ps(delta_t_sq));
|
||||
|
||||
// # 32 SQRTs
|
||||
__m512 vel_norm_dt_0 = _mm512_sqrt_ps(sb_sq_i_vec_0);
|
||||
__m512 vel_norm_dt_1 = _mm512_sqrt_ps(sb_sq_i_vec_1);
|
||||
|
||||
_mm512_storeu_ps(&curr_vel_x[i], curr_vel_x_i_vec_0);
|
||||
_mm512_storeu_ps(&curr_vel_x[i + 16], curr_vel_x_i_vec_1);
|
||||
|
||||
_mm512_storeu_ps(&curr_vel_y[i], curr_vel_y_i_vec_0);
|
||||
_mm512_storeu_ps(&curr_vel_y[i + 16], curr_vel_y_i_vec_1);
|
||||
|
||||
_mm512_storeu_ps(&w_x[i], w_x_i_vec_0);
|
||||
_mm512_storeu_ps(&w_x[i + 16], w_x_i_vec_1);
|
||||
|
||||
_mm512_storeu_ps(&w_y[i], w_y_i_vec_0);
|
||||
_mm512_storeu_ps(&w_y[i + 16], w_y_i_vec_1);
|
||||
|
||||
_mm512_storeu_ps(&pos_x[i], pos_x_i_vec_0);
|
||||
_mm512_storeu_ps(&pos_x[i + 16], pos_x_i_vec_1);
|
||||
|
||||
_mm512_storeu_ps(&pos_y[i], pos_y_i_vec_0);
|
||||
_mm512_storeu_ps(&pos_y[i + 16], pos_y_i_vec_1);
|
||||
|
||||
_mm512_storeu_ps(&e_x[i], e_x_i_vec_0);
|
||||
_mm512_storeu_ps(&e_x[i + 16], e_x_i_vec_1);
|
||||
|
||||
_mm512_storeu_ps(&e_y[i], e_y_i_vec_0);
|
||||
_mm512_storeu_ps(&e_y[i + 16], e_y_i_vec_1);
|
||||
|
||||
_mm512_storeu_ps(&destination_y[i], destination_y_i_vec_0);
|
||||
_mm512_storeu_ps(&destination_y[i + 16], destination_y_i_vec_1);
|
||||
|
||||
_mm512_storeu_ps(&sb_sq[i], sb_sq_i_vec_0);
|
||||
_mm512_storeu_ps(&sb_sq[i + 16], sb_sq_i_vec_1);
|
||||
|
||||
// # 32 MULTs
|
||||
_mm512_storeu_ps(&sb_e_x[i], _mm512_mul_ps(e_x_i_vec_0, vel_norm_dt_0));
|
||||
_mm512_storeu_ps(&sb_e_x[i + 16], _mm512_mul_ps(e_x_i_vec_1, vel_norm_dt_1));
|
||||
|
||||
// # 32 MULTs
|
||||
_mm512_storeu_ps(&sb_e_y[i], _mm512_mul_ps(e_y_i_vec_0, vel_norm_dt_0));
|
||||
_mm512_storeu_ps(&sb_e_y[i + 16], _mm512_mul_ps(e_y_i_vec_1, vel_norm_dt_1));
|
||||
}
|
||||
|
||||
#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;
|
||||
}
|
||||
@@ -0,0 +1,634 @@
|
||||
// # Changes: Used _mm512_rsqrt14_ps() instead of _mm512_sqrt_ps() and _mm512_div_ps()
|
||||
// # Predecessor: variant_float_inlining_SoA_AVX512_polyexp_moreavx_ilp_2.cpp
|
||||
|
||||
#include "../headers/board_reduced_float.hpp"
|
||||
#include "../headers/constants_float.hpp"
|
||||
#include "../headers/pedestrian_reduced_float.hpp"
|
||||
|
||||
#include <immintrin.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
|
||||
#define SEED 42
|
||||
#define BOARD_WIDTH 10.0f
|
||||
#define BOARD_LENGTH 50.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
|
||||
#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<float> pos_x, pos_y;
|
||||
alignas(64) vector<float> sb_sq;
|
||||
alignas(64) vector<float> sb_e_x, sb_e_y;
|
||||
alignas(64) vector<float> e_x, e_y;
|
||||
alignas(64) vector<float> curr_vel_x, curr_vel_y;
|
||||
alignas(64) vector<float> w_x, w_y;
|
||||
alignas(64) vector<float> desired_v0;
|
||||
alignas(64) vector<float> v_max;
|
||||
alignas(64) vector<float> destination_x, destination_y;
|
||||
alignas(64) 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);
|
||||
|
||||
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); }
|
||||
|
||||
// # 256 MULTs, 224 ADDs
|
||||
static inline __m512 _avx512_exp_ps(__m512 x) {
|
||||
|
||||
const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
|
||||
|
||||
// # 16 MULTs
|
||||
__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);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
|
||||
|
||||
const __m512 c13 = _mm512_set1_ps(c13_scalar);
|
||||
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);
|
||||
|
||||
__m512 p = c13;
|
||||
|
||||
// # 13 * (16 MULTs, 16 ADDs)
|
||||
// # = 208 MULTs, 208 ADDs
|
||||
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);
|
||||
|
||||
__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);
|
||||
|
||||
// # 16 MULTs
|
||||
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<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
|
||||
|
||||
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);
|
||||
|
||||
// # ADDs: n_iter * (
|
||||
// # n_p * 68 +
|
||||
// # n_p * n_p_pad * 29
|
||||
// # n_p * board.n_obstacles * 10
|
||||
// # n_p_pad * 9
|
||||
// # )
|
||||
|
||||
// # MULTs: n_iter * (
|
||||
// # n_p * 4 +
|
||||
// # n_p * n_p_pad * 40
|
||||
// # n_p * board.n_obstacles * 13
|
||||
// # n_p_pad * 17
|
||||
// # )
|
||||
|
||||
// # DIVs: n_iter * (
|
||||
// # n_p * n_p_pad
|
||||
// # n_p * board.n_obstacles
|
||||
// # n_p_pad * 2
|
||||
// # )
|
||||
|
||||
// # SQRTs: n_iter * (
|
||||
// # n_p * n_p_pad * 3
|
||||
// # n_p * board.n_obstacles
|
||||
// # n_p_pad * 3
|
||||
// # )
|
||||
|
||||
// # EXPs: n_iter * n_p * board.n_obstacles
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
|
||||
// # 2 MULTs
|
||||
float desired_velocity_x = e_x[i] * desired_v0[i];
|
||||
float desired_velocity_y = e_y[i] * desired_v0[i];
|
||||
|
||||
// # 2 ADDs, 2 MULTs
|
||||
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;
|
||||
|
||||
const __m512 pix = _mm512_set1_ps(pos_x[i]);
|
||||
const __m512 piy = _mm512_set1_ps(pos_y[i]);
|
||||
const __m512 eix = _mm512_set1_ps(e_x[i]);
|
||||
const __m512 eiy = _mm512_set1_ps(e_y[i]);
|
||||
const __m512 v_i = _mm512_set1_ps((float)i);
|
||||
|
||||
__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();
|
||||
|
||||
for (int j = 0; j < n_p_pad; j += 32) {
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
__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);
|
||||
|
||||
__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);
|
||||
|
||||
__mmask16 active_0 = m_neq_i_0 & m_in_range_0;
|
||||
__mmask16 active_1 = m_neq_i_1 & m_in_range_1;
|
||||
|
||||
__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]);
|
||||
|
||||
__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]);
|
||||
|
||||
__m512 sb_jsq_0 = _mm512_loadu_ps(&sb_sq[j]);
|
||||
__m512 sb_jsq_1 = _mm512_loadu_ps(&sb_sq[j + 16]);
|
||||
|
||||
// # 32 ADDs
|
||||
__m512 r_ab_x_0 = _mm512_sub_ps(pix, pjx_0);
|
||||
__m512 r_ab_x_1 = _mm512_sub_ps(pix, pjx_1);
|
||||
|
||||
// # 32 ADDs
|
||||
__m512 r_ab_y_0 = _mm512_sub_ps(piy, pjy_0);
|
||||
__m512 r_ab_y_1 = _mm512_sub_ps(piy, pjy_1);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 32 SQRTs
|
||||
__m512 r_ab_n_0 = _mm512_sqrt_ps(r_ab_n2_0);
|
||||
__m512 r_ab_n_1 = _mm512_sqrt_ps(r_ab_n2_1);
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 32 SQRTs
|
||||
__m512 r_ab2_n_0 = _mm512_sqrt_ps(r_ab2_n2_0);
|
||||
__m512 r_ab2_n_1 = _mm512_sqrt_ps(r_ab2_n2_1);
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 32 SQRTs
|
||||
__m512 two_b_0 = _mm512_sqrt_ps(two_b_sq_0);
|
||||
__m512 two_b_1 = _mm512_sqrt_ps(two_b_sq_1);
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
e_arg_0 = _mm512_max_ps(e_arg_0, v_exp_min);
|
||||
e_arg_1 = _mm512_max_ps(e_arg_1, v_exp_min);
|
||||
|
||||
// # 512 MULTs, 448 ADDs
|
||||
__m512 e_val_0 = _avx512_exp_ps(e_arg_0);
|
||||
__m512 e_val_1 = _avx512_exp_ps(e_arg_1);
|
||||
|
||||
// # 64 MULTs
|
||||
__m512 numer_0 = _mm512_mul_ps(_mm512_mul_ps(r_sum_0, v_neg_v0_div_2s), e_val_0);
|
||||
__m512 numer_1 = _mm512_mul_ps(_mm512_mul_ps(r_sum_1, v_neg_v0_div_2s), e_val_1);
|
||||
|
||||
// # 64 MULTs
|
||||
__m512 denom_0 = _mm512_mul_ps(_mm512_mul_ps(two_b_0, r_ab_n_0), r_ab2_n_0);
|
||||
__m512 denom_1 = _mm512_mul_ps(_mm512_mul_ps(two_b_1, r_ab_n_1), r_ab2_n_1);
|
||||
|
||||
__m512 denom_safe_0 = _mm512_mask_blend_ps(active_0, v_one, denom_0);
|
||||
__m512 denom_safe_1 = _mm512_mask_blend_ps(active_1, v_one, denom_1);
|
||||
|
||||
// # 32 DIVs
|
||||
__m512 smc_0 = _mm512_div_ps(numer_0, denom_safe_0);
|
||||
__m512 smc_1 = _mm512_div_ps(numer_1, denom_safe_1);
|
||||
|
||||
// # 32 MULTs
|
||||
__m512 sm_r_ab_0 = _mm512_mul_ps(smc_0, r_ab2_n_0);
|
||||
__m512 sm_r_ab_1 = _mm512_mul_ps(smc_1, r_ab2_n_1);
|
||||
|
||||
// # 32 MULTs
|
||||
__m512 sm_r_ab2_0 = _mm512_mul_ps(smc_0, r_ab_n_0);
|
||||
__m512 sm_r_ab2_1 = _mm512_mul_ps(smc_1, r_ab_n_1);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
__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);
|
||||
|
||||
__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);
|
||||
|
||||
__mmask16 cond_0 = c1_0 & c2_0;
|
||||
__mmask16 cond_1 = c1_1 & c2_1;
|
||||
|
||||
__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);
|
||||
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
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);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
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);
|
||||
}
|
||||
|
||||
// # 64 ADDs
|
||||
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));
|
||||
|
||||
for (int k = 0; k < board.n_obstacles; k++) {
|
||||
|
||||
// # 2 ADDs
|
||||
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;
|
||||
|
||||
// # 3 MULTs, 1 ADD
|
||||
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;
|
||||
|
||||
// # 4 ADDs, 2 MULTs
|
||||
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);
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
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;
|
||||
|
||||
// # 4 MULTs, 1 EXP, 1 DIV
|
||||
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;
|
||||
|
||||
// # 2 MULTS
|
||||
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;
|
||||
|
||||
// # 2 ADDs
|
||||
f_alpha_dt_x += f_alpha_B_dt_x;
|
||||
f_alpha_dt_y += f_alpha_B_dt_y;
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
w_x[i] += f_alpha_dt_x;
|
||||
w_y[i] += f_alpha_dt_y;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n_p_pad; i += 16) {
|
||||
|
||||
__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]);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__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));
|
||||
|
||||
// # 16 SQRTs, 16 DIVs
|
||||
__m512 w_norm_inv = _mm512_rsqrt14_ps(w_norm_sq);
|
||||
|
||||
// # 16 MULTs
|
||||
__m512 correction_factor = _mm512_min_ps(_mm512_set1_ps(1.0f), _mm512_mul_ps(v_max_i_vec, w_norm_inv));
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
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);
|
||||
|
||||
__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_MINUS_ONE_POINT_FIVE), _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_MINUS_ONE));
|
||||
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));
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 e_norm_sq = _mm512_fmadd_ps(ex_vec, ex_vec, _mm512_mul_ps(ey_vec, ey_vec));
|
||||
|
||||
// # 16 DIVs, 16 SQRTs
|
||||
__m512 e_norm_inv = _mm512_rsqrt14_ps(e_norm_sq);
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__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));
|
||||
|
||||
// # 16 MULTs
|
||||
__m512 sb_sq_i_vec = _mm512_mul_ps(vel_norm_sq, _mm512_set1_ps(delta_t_sq));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 vel_norm_dt = _mm512_sqrt_ps(sb_sq_i_vec);
|
||||
|
||||
_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);
|
||||
|
||||
// # 32 MULTs
|
||||
_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;
|
||||
}
|
||||
+103
-103
@@ -1,5 +1,5 @@
|
||||
// # 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
|
||||
// # Changes: Added more correction rounds to _avx512_exp_ps to do 30 flops per scalar
|
||||
// # Predecessor: variant_float_inlining_SoA_AVX512_polyexp_moreavx_ilp_2_rsqrt_morersqrt.cpp
|
||||
|
||||
#include "../headers/board_reduced_float.hpp"
|
||||
#include "../headers/constants_float.hpp"
|
||||
@@ -13,6 +13,8 @@
|
||||
#define SEED 42
|
||||
#define BOARD_WIDTH 10.0f
|
||||
#define BOARD_LENGTH 50.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
|
||||
#define WALL_EPS 1e-3f
|
||||
#define BOARD_WIDTH_MINUS_WALL_EPS 9.999f
|
||||
|
||||
@@ -160,19 +162,20 @@ Pedestrian *init_pedestrians(int n_p) {
|
||||
|
||||
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
|
||||
|
||||
// # 256 MULTs, 224 ADDs
|
||||
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);
|
||||
|
||||
// # 16 MULTs
|
||||
__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)
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
|
||||
|
||||
// approximation of e^r using taylor polynomial
|
||||
// cNUM --> coefficients
|
||||
const __m512 c13 = _mm512_set1_ps(c13_scalar);
|
||||
const __m512 c12 = _mm512_set1_ps(c12_scalar);
|
||||
const __m512 c11 = _mm512_set1_ps(c11_scalar);
|
||||
const __m512 c10 = _mm512_set1_ps(c10_scalar);
|
||||
@@ -187,8 +190,10 @@ static inline __m512 _avx512_exp_ps(__m512 x) {
|
||||
const __m512 c1 = _mm512_set1_ps(c1_scalar);
|
||||
const __m512 c0 = _mm512_set1_ps(c0_scalar);
|
||||
|
||||
// apply coefficients
|
||||
__m512 p = c12;
|
||||
__m512 p = c13;
|
||||
|
||||
// # 13 * (16 MULTs, 16 ADDs)
|
||||
// # = 208 MULTs, 208 ADDs
|
||||
p = _mm512_fmadd_ps(p, r, c12);
|
||||
p = _mm512_fmadd_ps(p, r, c11);
|
||||
p = _mm512_fmadd_ps(p, r, c10);
|
||||
@@ -203,13 +208,12 @@ static inline __m512 _avx512_exp_ps(__m512 x) {
|
||||
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
|
||||
// # 16 MULTs
|
||||
return _mm512_mul_ps(p, pow2n);
|
||||
}
|
||||
|
||||
@@ -248,254 +252,227 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
|
||||
const __m512 v_np = _mm512_set1_ps((float)n_p);
|
||||
const __m512 v_exp_min = _mm512_set1_ps(-87.0f);
|
||||
|
||||
// # ADDs: n_iter * (
|
||||
// # n_p * 68 +
|
||||
// # n_p * n_p_pad * 29 +
|
||||
// # n_p * board.n_obstacles * 10 +
|
||||
// # n_p_pad * 7
|
||||
// # )
|
||||
|
||||
// # MULTs: n_iter * (
|
||||
// # n_p * 4 +
|
||||
// # n_p * n_p_pad * 42 +
|
||||
// # n_p * board.n_obstacles * 13 +
|
||||
// # n_p_pad * 16
|
||||
// # )
|
||||
|
||||
// # DIVs: n_iter * (
|
||||
// # n_p * n_p_pad * 3 +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p_pad * 2
|
||||
// # )
|
||||
|
||||
// # SQRTs: n_iter * (
|
||||
// # n_p * n_p_pad * 3 +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p_pad * 3
|
||||
// # )
|
||||
|
||||
// # EXPs: n_iter * n_p * board.n_obstacles
|
||||
|
||||
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
|
||||
// # 2 MULTs
|
||||
float desired_velocity_x = e_x[i] * desired_v0[i];
|
||||
float desired_velocity_y = e_y[i] * desired_v0[i];
|
||||
|
||||
// velocity change accumulator
|
||||
// # 2 ADDs, 2 MULTs
|
||||
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]
|
||||
// # 32 ADDs
|
||||
__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, ...]
|
||||
// # 64 ADDs
|
||||
__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
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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, ...]
|
||||
// # 32 DIVs, 32 SQRTs
|
||||
__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, ...]
|
||||
// # 32 MULTs
|
||||
__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
|
||||
// # 64 ADDs
|
||||
__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)
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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|, ...]
|
||||
// # 32 DIVs, 32 SQRTs
|
||||
__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|
|
||||
// # 32 MULTs
|
||||
__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]
|
||||
// # 32 ADDs
|
||||
__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
|
||||
// # 32 MULTs, 32 ADDs
|
||||
__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
|
||||
// # 32 DIVs, 32 SQRTs
|
||||
__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
|
||||
// # 32 MULTs
|
||||
__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
|
||||
// # 32 MULTs
|
||||
__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
|
||||
// # 512 MULTs, 448 ADDs
|
||||
__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
|
||||
// # 96 MULTs
|
||||
__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|
|
||||
// # 32 MULTs
|
||||
__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|
|
||||
// # 32 MULTs
|
||||
__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
|
||||
// # 128 MULTs, 64 ADDs
|
||||
__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;
|
||||
//
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
// # 64 MULTs, 32 ADDs
|
||||
__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));
|
||||
|
||||
// # 32 MULTs
|
||||
__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
|
||||
// # 64 MULTs, 64 ADDs
|
||||
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
|
||||
// # 64 ADDs
|
||||
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++) {
|
||||
|
||||
// # 2 ADDs
|
||||
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;
|
||||
|
||||
// # 3 MULTs, 1 ADD
|
||||
float projection = (v1_x * v2_x + v1_y * v2_y) * board.obstacles[k].length_sq_inv;
|
||||
|
||||
if (projection < 0.0f)
|
||||
@@ -503,12 +480,15 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
|
||||
else if (projection > 1.0f)
|
||||
projection = 1.0f;
|
||||
|
||||
// # 4 ADDs, 2 MULTs
|
||||
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);
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
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;
|
||||
|
||||
// # 4 MULTs, 1 EXP, 1 DIV
|
||||
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;
|
||||
@@ -519,21 +499,21 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
|
||||
if (e_dot_f < cos_phi)
|
||||
w_val = c_neg_times_dt;
|
||||
|
||||
// # 2 MULTS
|
||||
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;
|
||||
|
||||
// # 2 ADDs
|
||||
f_alpha_dt_x += f_alpha_B_dt_x;
|
||||
f_alpha_dt_y += f_alpha_B_dt_y;
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
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]);
|
||||
@@ -545,20 +525,27 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
|
||||
|
||||
__m512i color_i_vec = _mm512_loadu_si512((__m512i *)&color[i]);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__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));
|
||||
|
||||
// # 16 SQRTs, 16 DIVs
|
||||
__m512 w_norm_inv = _mm512_rsqrt14_ps(w_norm_sq);
|
||||
|
||||
// # 16 MULTs
|
||||
__m512 correction_factor = _mm512_min_ps(_mm512_set1_ps(1.0f), _mm512_mul_ps(v_max_i_vec, w_norm_inv));
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
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_right = _mm512_cmp_ps_mask(pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE_POINT_FIVE), _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));
|
||||
@@ -572,7 +559,7 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
|
||||
__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));
|
||||
pos_x_i_vec = _mm512_mask_blend_ps(cond_2, pos_x_i_vec, _mm512_set1_ps(BOARD_LENGTH_MINUS_ONE));
|
||||
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);
|
||||
@@ -583,18 +570,29 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
|
||||
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));
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 e_norm_sq = _mm512_fmadd_ps(ex_vec, ex_vec, _mm512_mul_ps(ey_vec, ey_vec));
|
||||
|
||||
// # 16 SQRTs, 16 DIVs
|
||||
__m512 e_norm_inv = _mm512_rsqrt14_ps(e_norm_sq);
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__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));
|
||||
|
||||
// # 16 MULTs
|
||||
__m512 sb_sq_i_vec = _mm512_mul_ps(vel_norm_sq, _mm512_set1_ps(delta_t_sq));
|
||||
|
||||
// # 16 SQRTs
|
||||
__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);
|
||||
@@ -605,6 +603,8 @@ int run_simulation(Pedestrian *pedestrians, Board &board, int n_p, int n_iter, c
|
||||
_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);
|
||||
|
||||
// # 32 MULTs
|
||||
_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));
|
||||
}
|
||||
|
||||
+2
-1
@@ -176,6 +176,7 @@ static inline __m512 _avx512_exp_ps(__m512 x) {
|
||||
// # 16 MULTs, 16 ADDs
|
||||
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
|
||||
|
||||
const __m512 c13 = _mm512_set1_ps(c13_scalar);
|
||||
const __m512 c12 = _mm512_set1_ps(c12_scalar);
|
||||
const __m512 c11 = _mm512_set1_ps(c11_scalar);
|
||||
const __m512 c10 = _mm512_set1_ps(c10_scalar);
|
||||
@@ -190,7 +191,7 @@ static inline __m512 _avx512_exp_ps(__m512 x) {
|
||||
const __m512 c1 = _mm512_set1_ps(c1_scalar);
|
||||
const __m512 c0 = _mm512_set1_ps(c0_scalar);
|
||||
|
||||
__m512 p = c12;
|
||||
__m512 p = c13;
|
||||
|
||||
// # 13 * (16 MULTs, 16 ADDs)
|
||||
// # = 208 MULTs, 208 ADDs
|
||||
|
||||
+2
-1
@@ -174,6 +174,7 @@ static inline __m512 _avx512_exp_ps(__m512 x) {
|
||||
// # 16 MULTs, 16 ADDs
|
||||
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
|
||||
|
||||
const __m512 c13 = _mm512_set1_ps(c13_scalar);
|
||||
const __m512 c12 = _mm512_set1_ps(c12_scalar);
|
||||
const __m512 c11 = _mm512_set1_ps(c11_scalar);
|
||||
const __m512 c10 = _mm512_set1_ps(c10_scalar);
|
||||
@@ -188,7 +189,7 @@ static inline __m512 _avx512_exp_ps(__m512 x) {
|
||||
const __m512 c1 = _mm512_set1_ps(c1_scalar);
|
||||
const __m512 c0 = _mm512_set1_ps(c0_scalar);
|
||||
|
||||
__m512 p = c12;
|
||||
__m512 p = c13;
|
||||
|
||||
// # 13 * (16 MULTs, 16 ADDs)
|
||||
// # = 208 MULTs, 208 ADDs
|
||||
|
||||
@@ -0,0 +1,727 @@
|
||||
// # Changes: Added ILP with k = 4 to j-loop
|
||||
// # Predecessor: variant_float_inlining_SoA_AVX512_polyexp_moreavx_ilp_2.cpp
|
||||
|
||||
#include "../headers/board_reduced_float.hpp"
|
||||
#include "../headers/constants_float.hpp"
|
||||
#include "../headers/pedestrian_reduced_float.hpp"
|
||||
|
||||
#include <immintrin.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
|
||||
#define SEED 42
|
||||
#define BOARD_WIDTH 10.0f
|
||||
#define BOARD_LENGTH 50.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0f
|
||||
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5f
|
||||
#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<float> pos_x, pos_y;
|
||||
alignas(64) vector<float> sb_sq;
|
||||
alignas(64) vector<float> sb_e_x, sb_e_y;
|
||||
alignas(64) vector<float> e_x, e_y;
|
||||
alignas(64) vector<float> curr_vel_x, curr_vel_y;
|
||||
alignas(64) vector<float> w_x, w_y;
|
||||
alignas(64) vector<float> desired_v0;
|
||||
alignas(64) vector<float> v_max;
|
||||
alignas(64) vector<float> destination_x, destination_y;
|
||||
alignas(64) 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);
|
||||
|
||||
const int n_p_pad = (n_p + 63) & ~63;
|
||||
|
||||
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); }
|
||||
|
||||
// # 256 MULTs, 224 ADDs
|
||||
static inline __m512 _avx512_exp_ps(__m512 x) {
|
||||
|
||||
const __m512 log2e = _mm512_set1_ps(1.4426950408889634f);
|
||||
|
||||
// # 16 MULTs
|
||||
__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);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
__m512 r = _mm512_fnmadd_ps(nf, ln2, x);
|
||||
|
||||
const __m512 c13 = _mm512_set1_ps(c13_scalar);
|
||||
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);
|
||||
|
||||
__m512 p = c13;
|
||||
|
||||
// # 13 * (16 MULTs, 16 ADDs)
|
||||
// # = 208 MULTs, 208 ADDs
|
||||
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);
|
||||
|
||||
__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);
|
||||
|
||||
// # 16 MULTs
|
||||
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<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
|
||||
|
||||
const int n_p_pad = (n_p + 63) & ~63;
|
||||
|
||||
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);
|
||||
|
||||
// # ADDs: n_iter * (
|
||||
// # n_p * 132 +
|
||||
// # n_p * n_p_pad * 29 +
|
||||
// # n_p * board.n_obstacles * 10 +
|
||||
// # n_p_pad * 7
|
||||
// # )
|
||||
|
||||
// # MULTs: n_iter * (
|
||||
// # n_p * 4 +
|
||||
// # n_p * n_p_pad * 40 +
|
||||
// # n_p * board.n_obstacles * 13 +
|
||||
// # n_p_pad * 15
|
||||
// # )
|
||||
|
||||
// # DIVs: n_iter * (
|
||||
// # n_p * n_p_pad +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p_pad * 2
|
||||
// # )
|
||||
|
||||
// # SQRTs: n_iter * (
|
||||
// # n_p * n_p_pad * 3 +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p_pad * 3
|
||||
// # )
|
||||
|
||||
// # EXPs: n_iter * n_p * board.n_obstacles
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
|
||||
// # 2 MULTs
|
||||
float desired_velocity_x = e_x[i] * desired_v0[i];
|
||||
float desired_velocity_y = e_y[i] * desired_v0[i];
|
||||
|
||||
// # 2 ADDs, 2 MULTs
|
||||
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;
|
||||
|
||||
const __m512 pix = _mm512_set1_ps(pos_x[i]);
|
||||
const __m512 piy = _mm512_set1_ps(pos_y[i]);
|
||||
const __m512 eix = _mm512_set1_ps(e_x[i]);
|
||||
const __m512 eiy = _mm512_set1_ps(e_y[i]);
|
||||
const __m512 v_i = _mm512_set1_ps((float)i);
|
||||
|
||||
__m512 acc_x_0 = _mm512_setzero_ps();
|
||||
__m512 acc_x_1 = _mm512_setzero_ps();
|
||||
__m512 acc_x_2 = _mm512_setzero_ps();
|
||||
__m512 acc_x_3 = _mm512_setzero_ps();
|
||||
|
||||
__m512 acc_y_0 = _mm512_setzero_ps();
|
||||
__m512 acc_y_1 = _mm512_setzero_ps();
|
||||
__m512 acc_y_2 = _mm512_setzero_ps();
|
||||
__m512 acc_y_3 = _mm512_setzero_ps();
|
||||
|
||||
for (int j = 0; j < n_p_pad; j += 64) {
|
||||
|
||||
// # 64 ADDs
|
||||
__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);
|
||||
__m512 j_idx_2 = _mm512_add_ps(_mm512_set1_ps((float)j + 32), lane_off);
|
||||
__m512 j_idx_3 = _mm512_add_ps(_mm512_set1_ps((float)j + 48), lane_off);
|
||||
|
||||
__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);
|
||||
__mmask16 m_neq_i_2 = _mm512_cmp_ps_mask(j_idx_2, v_i, _CMP_NEQ_OQ);
|
||||
__mmask16 m_neq_i_3 = _mm512_cmp_ps_mask(j_idx_3, v_i, _CMP_NEQ_OQ);
|
||||
|
||||
__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);
|
||||
__mmask16 m_in_range_2 = _mm512_cmp_ps_mask(j_idx_2, v_np, _CMP_LT_OQ);
|
||||
__mmask16 m_in_range_3 = _mm512_cmp_ps_mask(j_idx_3, v_np, _CMP_LT_OQ);
|
||||
|
||||
__mmask16 active_0 = m_neq_i_0 & m_in_range_0;
|
||||
__mmask16 active_1 = m_neq_i_1 & m_in_range_1;
|
||||
__mmask16 active_2 = m_neq_i_2 & m_in_range_2;
|
||||
__mmask16 active_3 = m_neq_i_3 & m_in_range_3;
|
||||
|
||||
__m512 pjx_0 = _mm512_loadu_ps(&pos_x[j]);
|
||||
__m512 pjx_1 = _mm512_loadu_ps(&pos_x[j + 16]);
|
||||
__m512 pjx_2 = _mm512_loadu_ps(&pos_x[j + 32]);
|
||||
__m512 pjx_3 = _mm512_loadu_ps(&pos_x[j + 48]);
|
||||
|
||||
__m512 pjy_0 = _mm512_loadu_ps(&pos_y[j]);
|
||||
__m512 pjy_1 = _mm512_loadu_ps(&pos_y[j + 16]);
|
||||
__m512 pjy_2 = _mm512_loadu_ps(&pos_y[j + 32]);
|
||||
__m512 pjy_3 = _mm512_loadu_ps(&pos_y[j + 48]);
|
||||
|
||||
__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_jx_2 = _mm512_loadu_ps(&sb_e_x[j + 32]);
|
||||
__m512 sbe_jx_3 = _mm512_loadu_ps(&sb_e_x[j + 48]);
|
||||
|
||||
__m512 sbe_jy_0 = _mm512_loadu_ps(&sb_e_y[j]);
|
||||
__m512 sbe_jy_1 = _mm512_loadu_ps(&sb_e_y[j + 16]);
|
||||
__m512 sbe_jy_2 = _mm512_loadu_ps(&sb_e_y[j + 32]);
|
||||
__m512 sbe_jy_3 = _mm512_loadu_ps(&sb_e_y[j + 48]);
|
||||
|
||||
__m512 sb_jsq_0 = _mm512_loadu_ps(&sb_sq[j]);
|
||||
__m512 sb_jsq_1 = _mm512_loadu_ps(&sb_sq[j + 16]);
|
||||
__m512 sb_jsq_2 = _mm512_loadu_ps(&sb_sq[j + 32]);
|
||||
__m512 sb_jsq_3 = _mm512_loadu_ps(&sb_sq[j + 48]);
|
||||
|
||||
// # 64 ADDs
|
||||
__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_x_2 = _mm512_sub_ps(pix, pjx_2);
|
||||
__m512 r_ab_x_3 = _mm512_sub_ps(pix, pjx_3);
|
||||
|
||||
// # 64 ADDs
|
||||
__m512 r_ab_y_0 = _mm512_sub_ps(piy, pjy_0);
|
||||
__m512 r_ab_y_1 = _mm512_sub_ps(piy, pjy_1);
|
||||
__m512 r_ab_y_2 = _mm512_sub_ps(piy, pjy_2);
|
||||
__m512 r_ab_y_3 = _mm512_sub_ps(piy, pjy_3);
|
||||
|
||||
// # 128 MULTs, 64 ADDs
|
||||
__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));
|
||||
__m512 r_ab_n2_2 = _mm512_fmadd_ps(r_ab_x_2, r_ab_x_2, _mm512_mul_ps(r_ab_y_2, r_ab_y_2));
|
||||
__m512 r_ab_n2_3 = _mm512_fmadd_ps(r_ab_x_3, r_ab_x_3, _mm512_mul_ps(r_ab_y_3, r_ab_y_3));
|
||||
|
||||
// # 64 SQRTs
|
||||
__m512 r_ab_n_0 = _mm512_sqrt_ps(r_ab_n2_0);
|
||||
__m512 r_ab_n_1 = _mm512_sqrt_ps(r_ab_n2_1);
|
||||
__m512 r_ab_n_2 = _mm512_sqrt_ps(r_ab_n2_2);
|
||||
__m512 r_ab_n_3 = _mm512_sqrt_ps(r_ab_n2_3);
|
||||
|
||||
// # 64 ADDs
|
||||
__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_x_2 = _mm512_sub_ps(r_ab_x_2, sbe_jx_2);
|
||||
__m512 r_ab2_x_3 = _mm512_sub_ps(r_ab_x_3, sbe_jx_3);
|
||||
|
||||
// # 64 ADDs
|
||||
__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);
|
||||
__m512 r_ab2_y_2 = _mm512_sub_ps(r_ab_y_2, sbe_jy_2);
|
||||
__m512 r_ab2_y_3 = _mm512_sub_ps(r_ab_y_3, sbe_jy_3);
|
||||
|
||||
// # 128 MULTs, 64 ADDs
|
||||
__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));
|
||||
__m512 r_ab2_n2_2 = _mm512_fmadd_ps(r_ab2_x_2, r_ab2_x_2, _mm512_mul_ps(r_ab2_y_2, r_ab2_y_2));
|
||||
__m512 r_ab2_n2_3 = _mm512_fmadd_ps(r_ab2_x_3, r_ab2_x_3, _mm512_mul_ps(r_ab2_y_3, r_ab2_y_3));
|
||||
|
||||
// # 64 SQRTs
|
||||
__m512 r_ab2_n_0 = _mm512_sqrt_ps(r_ab2_n2_0);
|
||||
__m512 r_ab2_n_1 = _mm512_sqrt_ps(r_ab2_n2_1);
|
||||
__m512 r_ab2_n_2 = _mm512_sqrt_ps(r_ab2_n2_2);
|
||||
__m512 r_ab2_n_3 = _mm512_sqrt_ps(r_ab2_n2_3);
|
||||
|
||||
// # 64 ADDs
|
||||
__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);
|
||||
__m512 r_sum_2 = _mm512_add_ps(r_ab_n_2, r_ab2_n_2);
|
||||
__m512 r_sum_3 = _mm512_add_ps(r_ab_n_3, r_ab2_n_3);
|
||||
|
||||
// # 64 MULTs, 64 ADDs
|
||||
__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);
|
||||
__m512 two_b_sq_2 = _mm512_fmsub_ps(r_sum_2, r_sum_2, sb_jsq_2);
|
||||
__m512 two_b_sq_3 = _mm512_fmsub_ps(r_sum_3, r_sum_3, sb_jsq_3);
|
||||
|
||||
// # 64 SQRTs
|
||||
__m512 two_b_0 = _mm512_sqrt_ps(two_b_sq_0);
|
||||
__m512 two_b_1 = _mm512_sqrt_ps(two_b_sq_1);
|
||||
__m512 two_b_2 = _mm512_sqrt_ps(two_b_sq_2);
|
||||
__m512 two_b_3 = _mm512_sqrt_ps(two_b_sq_3);
|
||||
|
||||
// # 64 MULTs
|
||||
__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);
|
||||
__m512 e_arg_2 = _mm512_mul_ps(two_b_2, v_neg_2s_inv);
|
||||
__m512 e_arg_3 = _mm512_mul_ps(two_b_3, v_neg_2s_inv);
|
||||
|
||||
e_arg_0 = _mm512_max_ps(e_arg_0, v_exp_min);
|
||||
e_arg_1 = _mm512_max_ps(e_arg_1, v_exp_min);
|
||||
e_arg_2 = _mm512_max_ps(e_arg_2, v_exp_min);
|
||||
e_arg_3 = _mm512_max_ps(e_arg_3, v_exp_min);
|
||||
|
||||
// # 1024 MULTs, 896 ADDs
|
||||
__m512 e_val_0 = _avx512_exp_ps(e_arg_0);
|
||||
__m512 e_val_1 = _avx512_exp_ps(e_arg_1);
|
||||
__m512 e_val_2 = _avx512_exp_ps(e_arg_2);
|
||||
__m512 e_val_3 = _avx512_exp_ps(e_arg_3);
|
||||
|
||||
// # 128 MULTs
|
||||
__m512 numer_0 = _mm512_mul_ps(_mm512_mul_ps(r_sum_0, v_neg_v0_div_2s), e_val_0);
|
||||
__m512 numer_1 = _mm512_mul_ps(_mm512_mul_ps(r_sum_1, v_neg_v0_div_2s), e_val_1);
|
||||
__m512 numer_2 = _mm512_mul_ps(_mm512_mul_ps(r_sum_2, v_neg_v0_div_2s), e_val_2);
|
||||
__m512 numer_3 = _mm512_mul_ps(_mm512_mul_ps(r_sum_3, v_neg_v0_div_2s), e_val_3);
|
||||
|
||||
// # 128 MULTs
|
||||
__m512 denom_0 = _mm512_mul_ps(_mm512_mul_ps(two_b_0, r_ab_n_0), r_ab2_n_0);
|
||||
__m512 denom_1 = _mm512_mul_ps(_mm512_mul_ps(two_b_1, r_ab_n_1), r_ab2_n_1);
|
||||
__m512 denom_2 = _mm512_mul_ps(_mm512_mul_ps(two_b_2, r_ab_n_2), r_ab2_n_2);
|
||||
__m512 denom_3 = _mm512_mul_ps(_mm512_mul_ps(two_b_3, r_ab_n_3), r_ab2_n_3);
|
||||
|
||||
__m512 denom_safe_0 = _mm512_mask_blend_ps(active_0, v_one, denom_0);
|
||||
__m512 denom_safe_1 = _mm512_mask_blend_ps(active_1, v_one, denom_1);
|
||||
__m512 denom_safe_2 = _mm512_mask_blend_ps(active_2, v_one, denom_2);
|
||||
__m512 denom_safe_3 = _mm512_mask_blend_ps(active_3, v_one, denom_3);
|
||||
|
||||
// # 64 DIVs
|
||||
__m512 smc_0 = _mm512_div_ps(numer_0, denom_safe_0);
|
||||
__m512 smc_1 = _mm512_div_ps(numer_1, denom_safe_1);
|
||||
__m512 smc_2 = _mm512_div_ps(numer_2, denom_safe_2);
|
||||
__m512 smc_3 = _mm512_div_ps(numer_3, denom_safe_3);
|
||||
|
||||
// # 64 MULTs
|
||||
__m512 sm_r_ab_0 = _mm512_mul_ps(smc_0, r_ab2_n_0);
|
||||
__m512 sm_r_ab_1 = _mm512_mul_ps(smc_1, r_ab2_n_1);
|
||||
__m512 sm_r_ab_2 = _mm512_mul_ps(smc_2, r_ab2_n_2);
|
||||
__m512 sm_r_ab_3 = _mm512_mul_ps(smc_3, r_ab2_n_3);
|
||||
|
||||
// # 64 MULTs
|
||||
__m512 sm_r_ab2_0 = _mm512_mul_ps(smc_0, r_ab_n_0);
|
||||
__m512 sm_r_ab2_1 = _mm512_mul_ps(smc_1, r_ab_n_1);
|
||||
__m512 sm_r_ab2_2 = _mm512_mul_ps(smc_2, r_ab_n_2);
|
||||
__m512 sm_r_ab2_3 = _mm512_mul_ps(smc_3, r_ab_n_3);
|
||||
|
||||
// # 128 MULTs, 64 ADDs
|
||||
__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_x_2 = _mm512_fmadd_ps(r_ab2_x_2, sm_r_ab2_2, _mm512_mul_ps(r_ab_x_2, sm_r_ab_2));
|
||||
__m512 f_x_3 = _mm512_fmadd_ps(r_ab2_x_3, sm_r_ab2_3, _mm512_mul_ps(r_ab_x_3, sm_r_ab_3));
|
||||
|
||||
// # 128 MULTs, 64 ADDs
|
||||
__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));
|
||||
__m512 f_y_2 = _mm512_fmadd_ps(r_ab2_y_2, sm_r_ab2_2, _mm512_mul_ps(r_ab_y_2, sm_r_ab_2));
|
||||
__m512 f_y_3 = _mm512_fmadd_ps(r_ab2_y_3, sm_r_ab2_3, _mm512_mul_ps(r_ab_y_3, sm_r_ab_3));
|
||||
|
||||
// # 128 MULTs, 64 ADDs
|
||||
__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 f_norm_sq_2 = _mm512_fmadd_ps(f_x_2, f_x_2, _mm512_mul_ps(f_y_2, f_y_2));
|
||||
__m512 f_norm_sq_3 = _mm512_fmadd_ps(f_x_3, f_x_3, _mm512_mul_ps(f_y_3, f_y_3));
|
||||
|
||||
// # 64 MULTs
|
||||
__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 rhs_2 = _mm512_mul_ps(f_norm_sq_2, v_cos_phi_sq);
|
||||
__m512 rhs_3 = _mm512_mul_ps(f_norm_sq_3, v_cos_phi_sq);
|
||||
|
||||
// # 128 MULTs, 64 ADDs
|
||||
__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_2 = _mm512_fmadd_ps(eix, f_x_2, _mm512_mul_ps(eiy, f_y_2));
|
||||
__m512 e_dot_f_3 = _mm512_fmadd_ps(eix, f_x_3, _mm512_mul_ps(eiy, f_y_3));
|
||||
|
||||
// # 64 MULTs
|
||||
__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);
|
||||
__m512 e_dot_f_sq_2 = _mm512_mul_ps(e_dot_f_2, e_dot_f_2);
|
||||
__m512 e_dot_f_sq_3 = _mm512_mul_ps(e_dot_f_3, e_dot_f_3);
|
||||
|
||||
__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);
|
||||
__mmask16 c1_2 = _mm512_cmp_ps_mask(e_dot_f_2, v_zero, _CMP_LT_OQ);
|
||||
__mmask16 c1_3 = _mm512_cmp_ps_mask(e_dot_f_3, v_zero, _CMP_LT_OQ);
|
||||
|
||||
__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);
|
||||
__mmask16 c2_2 = _mm512_cmp_ps_mask(e_dot_f_sq_2, rhs_2, _CMP_GT_OQ);
|
||||
__mmask16 c2_3 = _mm512_cmp_ps_mask(e_dot_f_sq_3, rhs_3, _CMP_GT_OQ);
|
||||
|
||||
__mmask16 cond_0 = c1_0 & c2_0;
|
||||
__mmask16 cond_1 = c1_1 & c2_1;
|
||||
__mmask16 cond_2 = c1_2 & c2_2;
|
||||
__mmask16 cond_3 = c1_3 & c2_3;
|
||||
|
||||
__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);
|
||||
__m512 w_val_2 = _mm512_mask_blend_ps(cond_2, v_dt_neg, v_c_neg_dt);
|
||||
__m512 w_val_3 = _mm512_mask_blend_ps(cond_3, v_dt_neg, v_c_neg_dt);
|
||||
|
||||
__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_x_safe_2 = _mm512_maskz_mov_ps(active_2, f_x_2);
|
||||
__m512 f_x_safe_3 = _mm512_maskz_mov_ps(active_3, f_x_3);
|
||||
|
||||
__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);
|
||||
__m512 f_y_safe_2 = _mm512_maskz_mov_ps(active_2, f_y_2);
|
||||
__m512 f_y_safe_3 = _mm512_maskz_mov_ps(active_3, f_y_3);
|
||||
|
||||
// # 64 MULTs, 64 ADDs
|
||||
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_x_2 = _mm512_fmadd_ps(f_x_safe_2, w_val_2, acc_x_2);
|
||||
acc_x_3 = _mm512_fmadd_ps(f_x_safe_3, w_val_3, acc_x_3);
|
||||
|
||||
// # 64 MULTs, 64 ADDs
|
||||
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);
|
||||
acc_y_2 = _mm512_fmadd_ps(f_y_safe_2, w_val_2, acc_y_2);
|
||||
acc_y_3 = _mm512_fmadd_ps(f_y_safe_3, w_val_3, acc_y_3);
|
||||
}
|
||||
// # 128 ADDs
|
||||
f_alpha_dt_x += _mm512_reduce_add_ps(_mm512_add_ps(_mm512_add_ps(acc_x_0, acc_x_1), _mm512_add_ps(acc_x_2, acc_x_3)));
|
||||
f_alpha_dt_y += _mm512_reduce_add_ps(_mm512_add_ps(_mm512_add_ps(acc_y_0, acc_y_1), _mm512_add_ps(acc_y_2, acc_y_3)));
|
||||
|
||||
for (int k = 0; k < board.n_obstacles; k++) {
|
||||
|
||||
// # 2 ADDs
|
||||
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;
|
||||
|
||||
// # 3 MULTs, 1 ADD
|
||||
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;
|
||||
|
||||
// # 4 ADDs, 2 MULTs
|
||||
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);
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
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 EXP, 4 MULTs, 1 DIV
|
||||
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;
|
||||
|
||||
// # 2 MULTs
|
||||
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;
|
||||
|
||||
// # 2 ADDs
|
||||
f_alpha_dt_x += f_alpha_B_dt_x;
|
||||
f_alpha_dt_y += f_alpha_B_dt_y;
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
w_x[i] += f_alpha_dt_x;
|
||||
w_y[i] += f_alpha_dt_y;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n_p_pad; i += 16) {
|
||||
|
||||
__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]);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__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));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 w_norm = _mm512_sqrt_ps(w_norm_sq);
|
||||
|
||||
// # 16 DIVs
|
||||
__m512 correction_factor = _mm512_min_ps(_mm512_set1_ps(1.0f), _mm512_div_ps(v_max_i_vec, w_norm));
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 32 ADDs
|
||||
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);
|
||||
|
||||
__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_MINUS_ONE_POINT_FIVE), _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_MINUS_ONE));
|
||||
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));
|
||||
|
||||
// # 32 ADDs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512 e_norm_sq = _mm512_fmadd_ps(ex_vec, ex_vec, _mm512_mul_ps(ey_vec, ey_vec));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 e_norm = _mm512_sqrt_ps(e_norm_sq);
|
||||
|
||||
// # 16 DIVs
|
||||
__m512 e_norm_inv = _mm512_div_ps(_mm512_set1_ps(1.0f), e_norm);
|
||||
|
||||
// # 32 MULTs
|
||||
__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);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__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));
|
||||
|
||||
// # 16 MULTs
|
||||
__m512 sb_sq_i_vec = _mm512_mul_ps(vel_norm_sq, _mm512_set1_ps(delta_t_sq));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512 vel_norm_dt = _mm512_sqrt_ps(sb_sq_i_vec);
|
||||
|
||||
_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);
|
||||
|
||||
// # 32 MULTs
|
||||
_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;
|
||||
}
|
||||
@@ -0,0 +1,380 @@
|
||||
// # Changes: SoA layout for pedestrians (file-scope arrays instead of struct fields)
|
||||
// # 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
|
||||
|
||||
// # ADDs: n_iter * (
|
||||
// # n_p * 4 +
|
||||
// # n_p * (n_p - 1) * 14 +
|
||||
// # n_p * board.n_obstacles * 10 +
|
||||
// # n_p * 7
|
||||
// # )
|
||||
|
||||
// # MULTs: n_iter * (
|
||||
// # n_p * 4
|
||||
// # n_p * (n_p - 1) * 24 +
|
||||
// # 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 * (
|
||||
// # n_p * (n_p - 1) * 3 +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p * 3
|
||||
// # )
|
||||
|
||||
// # EXPs: n_iter * (
|
||||
// # n_p * (n_p - 1) +
|
||||
// # 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 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;
|
||||
|
||||
for (int j = 0; j < n_p; j++) {
|
||||
if (i == j)
|
||||
continue;
|
||||
|
||||
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;
|
||||
|
||||
float r_ab2_x = r_ab_x - sb_e_x[j];
|
||||
float r_ab2_y = r_ab_y - sb_e_y[j];
|
||||
|
||||
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;
|
||||
|
||||
float r_ab_norm_sum = r_ab_norm + r_ab2_norm;
|
||||
|
||||
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;
|
||||
float two_b = sqrt(two_b_sq);
|
||||
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));
|
||||
float scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
|
||||
float scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
|
||||
|
||||
float f_alpha_beta_neg_x = r_ab_x * scalar_mult_r_ab + r_ab2_x * scalar_mult_r_ab2;
|
||||
float f_alpha_beta_neg_y = r_ab_y * scalar_mult_r_ab + r_ab2_y * scalar_mult_r_ab2;
|
||||
|
||||
float w_val;
|
||||
|
||||
float f_norm_sq = f_alpha_beta_neg_x * f_alpha_beta_neg_x + f_alpha_beta_neg_y * f_alpha_beta_neg_y;
|
||||
float rhs = f_norm_sq * cos_phi_sq;
|
||||
float e_dot_f = e_x[i] * f_alpha_beta_neg_x + e_y[i] * f_alpha_beta_neg_y;
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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.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_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;
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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_norm2 = w_norm > EPS ? w_norm : EPS;
|
||||
|
||||
float correction_factor = min(1.0f, v_max[i] / w_norm2);
|
||||
|
||||
curr_vel_x[i] = w_x[i] * correction_factor;
|
||||
curr_vel_y[i] = w_y[i] * correction_factor;
|
||||
|
||||
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt;
|
||||
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);
|
||||
|
||||
float ex = destination_x[i] - pos_x[i];
|
||||
float ey = destination_y[i] - pos_y[i];
|
||||
|
||||
float e_norm = sqrt(ex * ex + ey * ey);
|
||||
float e_norm2 = e_norm > EPS ? e_norm : EPS;
|
||||
float e_norm_inv = 1.0 / e_norm2;
|
||||
|
||||
e_x[i] = ex * e_norm_inv;
|
||||
e_y[i] = ey * e_norm_inv;
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
#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;
|
||||
}
|
||||
@@ -0,0 +1,366 @@
|
||||
// # Changes: Replaced «double» with «float» in variable declarations
|
||||
// # Predecessor: variant_float_baseline.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;
|
||||
|
||||
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);
|
||||
|
||||
Pedestrian *P = new Pedestrian[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 start_pos_x = x;
|
||||
float start_pos_y = y;
|
||||
|
||||
float destination_x = BOARD_LENGTH + 30.0;
|
||||
float destination_y = y;
|
||||
|
||||
float init_e_x = destination_x - start_pos_x;
|
||||
float init_e_y = destination_y - start_pos_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 desired_v0;
|
||||
do {
|
||||
desired_v0 = velocity_gen(gen);
|
||||
} while (desired_v0 <= 0.1);
|
||||
|
||||
float 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++) {
|
||||
float x = x_gen_right(gen);
|
||||
float y = y_gen(gen);
|
||||
|
||||
float start_pos_x = x;
|
||||
float start_pos_y = y;
|
||||
float destination_x = -30;
|
||||
float destination_y = y;
|
||||
|
||||
float init_e_x = destination_x - start_pos_x;
|
||||
float init_e_y = destination_y - start_pos_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 desired_v0;
|
||||
do {
|
||||
desired_v0 = velocity_gen(gen);
|
||||
} while (desired_v0 <= 0.1);
|
||||
|
||||
float 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<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++) {
|
||||
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
|
||||
|
||||
// # ADDs: n_iter * (
|
||||
// # n_p * 4 +
|
||||
// # n_p * (n_p - 1) * 14 +
|
||||
// # n_p * board.n_obstacles * 10 +
|
||||
// # n_p * 7
|
||||
// # )
|
||||
|
||||
// # MULTs: n_iter * (
|
||||
// # n_p * 4
|
||||
// # n_p * (n_p - 1) * 24 +
|
||||
// # 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 * (
|
||||
// # n_p * (n_p - 1) * 3 +
|
||||
// # n_p * board.n_obstacles +
|
||||
// # n_p * 3
|
||||
// # )
|
||||
|
||||
// # EXPs: n_iter * (
|
||||
// # n_p * (n_p - 1) +
|
||||
// # n_p * board.n_obstacles
|
||||
// # )
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
Pedestrian &p = pedestrians[i];
|
||||
|
||||
// # 2 MULTs
|
||||
float desired_velocity_x = p.e_x * p.desired_v0;
|
||||
float desired_velocity_y = p.e_y * p.desired_v0;
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
float f_alpha_dt_x = (desired_velocity_x - p.curr_vel_x) * dt_times_tau_alpha_inv;
|
||||
float f_alpha_dt_y = (desired_velocity_y - p.curr_vel_y) * dt_times_tau_alpha_inv;
|
||||
|
||||
for (int j = 0; j < n_p; j++) {
|
||||
if (i == j)
|
||||
continue;
|
||||
|
||||
// # 2 ADDs
|
||||
float r_ab_x = p.curr_pos_x - pedestrians[j].curr_pos_x;
|
||||
float r_ab_y = p.curr_pos_y - pedestrians[j].curr_pos_y;
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
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 ADDs
|
||||
float r_ab2_x = r_ab_x - pedestrians[j].s_b_e_x;
|
||||
float r_ab2_y = r_ab_y - pedestrians[j].s_b_e_y;
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
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 MULT 1 ADD
|
||||
float two_b_sq = r_ab_norm_sum * r_ab_norm_sum - pedestrians[j].s_b_sq;
|
||||
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));
|
||||
|
||||
// # 2 MULTs
|
||||
float scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
|
||||
float scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
|
||||
|
||||
// # 4 MULTs, 2 ADDs
|
||||
float f_alpha_beta_neg_x = r_ab_x * scalar_mult_r_ab + r_ab2_x * scalar_mult_r_ab2;
|
||||
float f_alpha_beta_neg_y = r_ab_y * scalar_mult_r_ab + r_ab2_y * scalar_mult_r_ab2;
|
||||
|
||||
float w_val;
|
||||
|
||||
// # 6 MULTs, 2 ADDs
|
||||
float f_norm_sq = f_alpha_beta_neg_x * f_alpha_beta_neg_x + f_alpha_beta_neg_y * f_alpha_beta_neg_y;
|
||||
float rhs = f_norm_sq * cos_phi_sq;
|
||||
float e_dot_f = p.e_x * f_alpha_beta_neg_x + p.e_y * f_alpha_beta_neg_y;
|
||||
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;
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
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;
|
||||
}
|
||||
|
||||
for (int k = 0; k < board.n_obstacles; k++) {
|
||||
|
||||
// # 2 ADDs
|
||||
float v1_x = p.curr_pos_x - board.obstacles[k].start_x;
|
||||
float v1_y = p.curr_pos_y - board.obstacles[k].start_y;
|
||||
float v2_x = board.obstacles[k].obstacle_v_x;
|
||||
float v2_y = board.obstacles[k].obstacle_v_y;
|
||||
|
||||
// # 3 MULTs, 1 ADD
|
||||
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;
|
||||
|
||||
// # 4 ADDs, 2 MULTs
|
||||
float r_aB_x = p.curr_pos_x - (board.obstacles[k].start_x + board.obstacles[k].obstacle_v_x * projection);
|
||||
float r_aB_y = p.curr_pos_y - (board.obstacles[k].start_y + board.obstacles[k].obstacle_v_y * projection);
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
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;
|
||||
|
||||
// # 4 MULTs, 1 EXP, 1 DIV
|
||||
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;
|
||||
|
||||
float e_dot_f = (f_alpha_B_neg_y == 0) ? 0 : (f_alpha_B_neg_y < 0 ? -p.e_y : p.e_y);
|
||||
|
||||
if (e_dot_f >= cos_phi)
|
||||
w_val = dt_neg;
|
||||
else
|
||||
w_val = c_neg_times_dt;
|
||||
|
||||
// # 2 MULTS
|
||||
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;
|
||||
|
||||
// # 2 ADDs
|
||||
f_alpha_dt_x += f_alpha_B_dt_x;
|
||||
f_alpha_dt_y += f_alpha_B_dt_y;
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
p.w_x += f_alpha_dt_x;
|
||||
p.w_y += f_alpha_dt_y;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
Pedestrian &p = pedestrians[i];
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
float w_norm = sqrt(p.w_x * p.w_x + p.w_y * p.w_y);
|
||||
|
||||
float w_norm2 = w_norm > EPS ? w_norm : EPS;
|
||||
|
||||
// # 1 DIV
|
||||
float correction_factor = min(1.0f, p.v_max / w_norm2);
|
||||
|
||||
// # 2 MULTs
|
||||
p.curr_vel_x = p.w_x * correction_factor;
|
||||
p.curr_vel_y = p.w_y * correction_factor;
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
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);
|
||||
|
||||
// # 2 ADDs
|
||||
float e_x = p.destination_x - p.curr_pos_x;
|
||||
float e_y = p.destination_y - p.curr_pos_y;
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
float e_norm = sqrt(e_x * e_x + e_y * e_y);
|
||||
|
||||
float e_norm2 = e_norm > EPS ? e_norm : EPS;
|
||||
|
||||
// # 1 DIV
|
||||
float e_norm_inv = 1.0 / e_norm2;
|
||||
|
||||
// # 2 MULTs
|
||||
p.e_x = e_x * e_norm_inv;
|
||||
p.e_y = e_y * e_norm_inv;
|
||||
|
||||
// # 2 MULTs, 1 ADD
|
||||
float velocity_norm_sq = p.curr_vel_x * p.curr_vel_x + p.curr_vel_y * p.curr_vel_y;
|
||||
|
||||
// # 1 SQRT
|
||||
float velocity_norm = sqrt(velocity_norm_sq);
|
||||
|
||||
// # 5 MULTs
|
||||
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;
|
||||
}
|
||||
@@ -190,21 +190,23 @@ static inline __m256d exp256_pd(__m256d x) {
|
||||
const __m256d ln2 = _mm256_set1_pd(0.6931471805599453);
|
||||
__m256d r = _mm256_fnmadd_pd(nf, ln2, x); // # 4 MULTs, 4 ADDs
|
||||
|
||||
const __m256d c12 = _mm256_set1_pd(1.0 / 479001600.0);
|
||||
const __m256d c11 = _mm256_set1_pd(1.0 / 39916800.0);
|
||||
const __m256d c10 = _mm256_set1_pd(1.0 / 3628800.0);
|
||||
const __m256d c9 = _mm256_set1_pd(1.0 / 362880.0);
|
||||
const __m256d c8 = _mm256_set1_pd(1.0 / 40320.0);
|
||||
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 = c13;
|
||||
|
||||
__m256d p = c12;
|
||||
// # 13 * (4 MULTs, 4 ADDs)
|
||||
// # = 52 MULTs, 52 ADDs
|
||||
p = _mm256_fmadd_pd(p, r, c12);
|
||||
|
||||
@@ -0,0 +1,362 @@
|
||||
// # This file uses avx2
|
||||
// # This is the first variant and should serve as a base for further avx2 optimizations
|
||||
|
||||
// Okay our first step is to use a SoA instead of an AoS
|
||||
// We only do SoA for the pedestrian struct, there we wanna optimize such that we access for the ped-ped forces adjacent pedestrians (in the arrays)
|
||||
|
||||
#include "../headers/board.hpp"
|
||||
#include "../headers/constants.hpp"
|
||||
#include "../headers/pedestrian_reduced.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.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;
|
||||
|
||||
// we wanna use arrays so we can implement SOA, we create arrays for all the data we need and access
|
||||
vector<double> pos_x, pos_y;
|
||||
vector<double> sb_sq;
|
||||
vector<double> sb_e_x, sb_e_y;
|
||||
vector<double> e_x, e_y;
|
||||
vector<double> curr_vel_x, curr_vel_y;
|
||||
vector<double> w_x, w_y;
|
||||
vector<double> desired_v0;
|
||||
vector<double> v_max;
|
||||
vector<double> 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<double> velocity_gen(v0_mean, v0_std_dev);
|
||||
uniform_real_distribution<double> y_gen(0.3, BOARD_WIDTH - 0.3);
|
||||
uniform_real_distribution<double> x_gen_left(2.0, BOARD_LENGTH / 2.0);
|
||||
uniform_real_distribution<double> x_gen_right(BOARD_LENGTH / 2.0, BOARD_LENGTH - 2.0);
|
||||
|
||||
// resize all our 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++) {
|
||||
double x = x_gen_left(gen);
|
||||
double y = y_gen(gen);
|
||||
|
||||
double dest_x = BOARD_LENGTH + 30.0; // renamed to avoid shadowing destination_x[]
|
||||
double dest_y = y; // same idea for destination_y[]
|
||||
|
||||
double init_e_x = dest_x - x;
|
||||
double init_e_y = dest_y - 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 d_v0;
|
||||
do {
|
||||
d_v0 = velocity_gen(gen);
|
||||
} while (d_v0 <= 0.1);
|
||||
|
||||
double 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:
|
||||
w_x[i] = 0.0;
|
||||
w_y[i] = 0.0;
|
||||
curr_vel_x[i] = 0.0;
|
||||
curr_vel_y[i] = 0.0;
|
||||
sb_sq[i] = 0.0;
|
||||
sb_e_x[i] = 0.0;
|
||||
sb_e_y[i] = 0.0;
|
||||
}
|
||||
|
||||
for (int i = n_p_half; i < n_p; i++) {
|
||||
double x = x_gen_right(gen);
|
||||
double y = y_gen(gen);
|
||||
|
||||
double dest_x = -30;
|
||||
double dest_y = y;
|
||||
|
||||
double init_e_x = dest_x - x;
|
||||
double init_e_y = dest_y - 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 d_v0;
|
||||
do {
|
||||
d_v0 = velocity_gen(gen);
|
||||
} while (d_v0 <= 0.1);
|
||||
|
||||
double 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;
|
||||
|
||||
// Fields the Pedestrian constructor would have zero-initialized:
|
||||
w_x[i] = 0.0;
|
||||
w_y[i] = 0.0;
|
||||
curr_vel_x[i] = 0.0;
|
||||
curr_vel_y[i] = 0.0;
|
||||
sb_sq[i] = 0.0;
|
||||
sb_e_x[i] = 0.0;
|
||||
sb_e_y[i] = 0.0;
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
#if BENCHMARK
|
||||
(void)dump_path;
|
||||
#endif
|
||||
|
||||
#if !BENCHMARK
|
||||
const int dump_interval = 1;
|
||||
|
||||
vector<tuple<int, int, double, double, double, double, 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++) {
|
||||
// Pedestrian &p = pedestrians[i];
|
||||
samples.emplace_back(0, i, pos_x[i], pos_y[i], e_x[i], e_y[i], color[i]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// # ADDs: n_iter * n_p * 11 +
|
||||
// # n_iter * n_p * (n_p - 1) * 14 +
|
||||
// # n_iter * n_p * board.n_obstacles * 12
|
||||
|
||||
// # MULTs: n_iter * n_p * 21 +
|
||||
// # n_iter * n_p * (n_p - 1) * 24 +
|
||||
// # n_iter * n_p * board.n_obstacles * 19
|
||||
|
||||
// # DIVs: n_iter * n_p * 2 +
|
||||
// # n_iter * n_p * (n_p - 1) +
|
||||
// # n_iter * n_p * board.n_obstacles
|
||||
|
||||
// # SQRTs: n_iter * n_p * 3 +
|
||||
// # n_iter * n_p * (n_p - 1) * 3 +
|
||||
// # n_iter * n_p * board.n_obstacles
|
||||
|
||||
// # EXPs: n_iter * n_p * (n_p - 1) +
|
||||
// # n_iter * 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 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;
|
||||
|
||||
for (int j = 0; j < n_p; j++) {
|
||||
|
||||
if (i == j)
|
||||
continue;
|
||||
|
||||
double r_ab_x = pos_x[i] - pos_x[j];
|
||||
double r_ab_y = pos_y[i] - pos_y[j];
|
||||
|
||||
double r_ab_norm = sqrt(r_ab_x * r_ab_x + r_ab_y * r_ab_y);
|
||||
|
||||
double r_ab2_x = r_ab_x - sb_e_x[j];
|
||||
double r_ab2_y = r_ab_y - sb_e_y[j];
|
||||
|
||||
double r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
|
||||
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);
|
||||
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));
|
||||
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
|
||||
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
|
||||
|
||||
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;
|
||||
|
||||
double w_val;
|
||||
double a = (e_x[i] * f_alpha_beta_neg_x + e_y[i] * f_alpha_beta_neg_y) * cos_phi_inv;
|
||||
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;
|
||||
if (a >= 0 && (a * a > b_sq)) {
|
||||
w_val = c_neg_times_dt;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 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 = 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_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 = (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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
w_x[i] += f_alpha_dt_x;
|
||||
w_y[i] += f_alpha_dt_y;
|
||||
}
|
||||
|
||||
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 correction_factor = min(1.0, v_max[i] / w_norm);
|
||||
|
||||
curr_vel_x[i] = w_x[i] * correction_factor;
|
||||
curr_vel_y[i] = w_y[i] * correction_factor;
|
||||
|
||||
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt;
|
||||
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);
|
||||
|
||||
double ex = destination_x[i] - pos_x[i];
|
||||
double ey = destination_y[i] - pos_y[i];
|
||||
|
||||
double e_norm_inv = 1.0 / sqrt(ex * ex + ey * ey);
|
||||
|
||||
e_x[i] = ex * e_norm_inv;
|
||||
e_y[i] = ey * e_norm_inv;
|
||||
|
||||
double velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i];
|
||||
|
||||
double velocity_norm = sqrt(velocity_norm_sq);
|
||||
|
||||
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);
|
||||
|
||||
#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;
|
||||
}
|
||||
@@ -0,0 +1,352 @@
|
||||
// # This file performs in-lining for run_simulation.
|
||||
// # Changes: Removed 2 sqrt() calls from the loop bodies
|
||||
// # Predecessor: variant_inlining_6.cpp
|
||||
|
||||
#include "../headers/board.hpp"
|
||||
#include "../headers/constants.hpp"
|
||||
#include "../headers/pedestrian_reduced.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.0
|
||||
#define BOARD_LENGTH 50.0
|
||||
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0
|
||||
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5
|
||||
#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<double> velocity_gen(v0_mean, v0_std_dev);
|
||||
uniform_real_distribution<double> y_gen(0.3, BOARD_WIDTH - 0.3);
|
||||
uniform_real_distribution<double> x_gen_left(2.0, BOARD_LENGTH / 2.0);
|
||||
uniform_real_distribution<double> 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<tuple<int, int, double, double, double, double, 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++) {
|
||||
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
|
||||
|
||||
// # ADDs: n_iter * n_p * 11 +
|
||||
// # n_iter * n_p * (n_p - 1) * 14 +
|
||||
// # n_iter * n_p * board.n_obstacles * 12
|
||||
|
||||
// # MULTs: n_iter * n_p * 21 +
|
||||
// # n_iter * n_p * (n_p - 1) * 24 +
|
||||
// # n_iter * n_p * board.n_obstacles * 19
|
||||
|
||||
// # DIVs: n_iter * n_p * 2 +
|
||||
// # n_iter * n_p * (n_p - 1) +
|
||||
// # n_iter * n_p * board.n_obstacles
|
||||
|
||||
// # SQRTs: n_iter * n_p * 3 +
|
||||
// # n_iter * n_p * (n_p - 1) * 3 +
|
||||
// # n_iter * n_p * board.n_obstacles
|
||||
|
||||
// # EXPs: n_iter * n_p * (n_p - 1) +
|
||||
// # n_iter * n_p * board.n_obstacles
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
Pedestrian &p = pedestrians[i];
|
||||
|
||||
// # 2 MULTs
|
||||
double desired_velocity_x = p.e_x * p.desired_v0;
|
||||
double desired_velocity_y = p.e_y * p.desired_v0;
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
double f_alpha_dt_x = (desired_velocity_x - p.curr_vel_x) * dt_times_tau_alpha_inv;
|
||||
double f_alpha_dt_y = (desired_velocity_y - p.curr_vel_y) * dt_times_tau_alpha_inv;
|
||||
|
||||
for (int j = 0; j < n_p; j++) {
|
||||
if (i == j)
|
||||
continue;
|
||||
|
||||
// # 2 ADDs
|
||||
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;
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double r_ab_norm = sqrt(r_ab_x * r_ab_x + r_ab_y * r_ab_y);
|
||||
|
||||
// # 2 ADDs
|
||||
double r_ab2_x = r_ab_x - pedestrians[j].s_b_e_x;
|
||||
double r_ab2_y = r_ab_y - pedestrians[j].s_b_e_y;
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
|
||||
|
||||
// # 1ADD
|
||||
double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
|
||||
|
||||
// # 1 MULT, 1 ADD
|
||||
double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - pedestrians[j].s_b_sq;
|
||||
|
||||
// # 1 SQRT
|
||||
double two_b = sqrt(two_b_sq);
|
||||
|
||||
// # 5 MULTs, 1 EXP, 1 DIV
|
||||
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));
|
||||
|
||||
// # 2 MULTs
|
||||
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
|
||||
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
|
||||
|
||||
// # 4 MULTs, 2 ADDs
|
||||
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;
|
||||
|
||||
double w_val;
|
||||
|
||||
// # 3 MULTs, 1 ADD
|
||||
double a = (p.e_x * f_alpha_beta_neg_x + p.e_y * f_alpha_beta_neg_y) * cos_phi_inv;
|
||||
|
||||
// # 2 MULTs, 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 MULT
|
||||
if (a >= 0 && (a * a > b_sq)) {
|
||||
w_val = c_neg_times_dt;
|
||||
}
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
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;
|
||||
}
|
||||
|
||||
for (int k = 0; k < board.n_obstacles; k++) {
|
||||
|
||||
// # 2 ADDs
|
||||
double v1_x = p.curr_pos_x - board.obstacles[k].start.x;
|
||||
double v1_y = p.curr_pos_y - board.obstacles[k].start.y;
|
||||
|
||||
// # 3 MULTs, 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;
|
||||
|
||||
// # 4 ADDs, 2 MULTs
|
||||
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);
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
|
||||
|
||||
// # 4 MULTs, 1 EXP, 1 DIV
|
||||
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;
|
||||
|
||||
// # 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;
|
||||
|
||||
// # 2 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;
|
||||
|
||||
w_val = dt_neg;
|
||||
|
||||
// # 1 MULT
|
||||
if (a >= 0 && (a * a > b_sq)) {
|
||||
w_val = c_neg_times_dt;
|
||||
}
|
||||
|
||||
// # 2 MULTs
|
||||
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;
|
||||
|
||||
// # 2 ADDs
|
||||
f_alpha_dt_x += f_alpha_B_dt_x;
|
||||
f_alpha_dt_y += f_alpha_B_dt_y;
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
p.w_x += f_alpha_dt_x;
|
||||
p.w_y += f_alpha_dt_y;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
Pedestrian &p = pedestrians[i];
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double w_norm = sqrt(p.w_x * p.w_x + p.w_y * p.w_y);
|
||||
|
||||
// # 1 DIV
|
||||
double correction_factor = min(1.0, p.v_max / w_norm);
|
||||
|
||||
// # 2 MULTs
|
||||
p.curr_vel_x = p.w_x * correction_factor;
|
||||
p.curr_vel_y = p.w_y * correction_factor;
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
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_MINUS_ONE_POINT_FIVE) {
|
||||
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_MINUS_ONE;
|
||||
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);
|
||||
|
||||
// # 2 ADDs
|
||||
double e_x = p.destination_x - p.curr_pos_x;
|
||||
double e_y = p.destination_y - p.curr_pos_y;
|
||||
|
||||
// # 1 DIV, 1 SQRT, 2 MULTs, 1 ADD
|
||||
double e_norm_inv = 1.0 / sqrt(e_x * e_x + e_y * e_y);
|
||||
|
||||
// # 2 MULTs
|
||||
p.e_x = e_x * e_norm_inv;
|
||||
p.e_y = e_y * e_norm_inv;
|
||||
|
||||
// # 2 MULTs, 1 ADD
|
||||
double velocity_norm_sq = p.curr_vel_x * p.curr_vel_x + p.curr_vel_y * p.curr_vel_y;
|
||||
|
||||
// # 1 SQRT
|
||||
double velocity_norm = sqrt(velocity_norm_sq);
|
||||
|
||||
// # 5 MULTs
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,483 @@
|
||||
// # Changes:
|
||||
// # - Ported AVX2 to AVX-512
|
||||
// # - Replaced Obstacle with the version that contains no Vector2d
|
||||
// # - Implemented Fast Inverse Square Root
|
||||
// # Predecessor: variant_inlining_7_SoA_AVX2_polyexp.cpp
|
||||
|
||||
#include "../headers/board_reduced.hpp"
|
||||
#include "../headers/constants.hpp"
|
||||
#include "../headers/pedestrian_reduced.hpp"
|
||||
|
||||
#include <immintrin.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
#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;
|
||||
|
||||
alignas(64) vector<double> pos_x, pos_y;
|
||||
alignas(64) vector<double> sb_sq;
|
||||
alignas(64) vector<double> sb_e_x, sb_e_y;
|
||||
alignas(64) vector<double> e_x, e_y;
|
||||
alignas(64) vector<double> curr_vel_x, curr_vel_y;
|
||||
alignas(64) vector<double> w_x, w_y;
|
||||
alignas(64) vector<double> desired_v0;
|
||||
alignas(64) vector<double> v_max;
|
||||
alignas(64) vector<double> destination_x, destination_y;
|
||||
alignas(64) 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<double> velocity_gen(v0_mean, v0_std_dev);
|
||||
uniform_real_distribution<double> y_gen(0.3, BOARD_WIDTH - 0.3);
|
||||
uniform_real_distribution<double> x_gen_left(2.0, BOARD_LENGTH / 2.0);
|
||||
uniform_real_distribution<double> 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);
|
||||
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++) {
|
||||
double x = x_gen_left(gen);
|
||||
double y = y_gen(gen);
|
||||
|
||||
double dest_x = BOARD_LENGTH + 30.0;
|
||||
double dest_y = y;
|
||||
|
||||
double init_e_x = dest_x - x;
|
||||
double init_e_y = dest_y - 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 d_v0;
|
||||
do {
|
||||
d_v0 = velocity_gen(gen);
|
||||
} while (d_v0 <= 0.1);
|
||||
|
||||
double 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;
|
||||
|
||||
w_x[i] = 0.0;
|
||||
w_y[i] = 0.0;
|
||||
curr_vel_x[i] = 0.0;
|
||||
curr_vel_y[i] = 0.0;
|
||||
sb_sq[i] = 0.0;
|
||||
sb_e_x[i] = 0.0;
|
||||
sb_e_y[i] = 0.0;
|
||||
}
|
||||
|
||||
for (int i = n_p_half; i < n_p; i++) {
|
||||
double x = x_gen_right(gen);
|
||||
double y = y_gen(gen);
|
||||
|
||||
double dest_x = -30;
|
||||
double dest_y = y;
|
||||
|
||||
double init_e_x = dest_x - x;
|
||||
double init_e_y = dest_y - 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 d_v0;
|
||||
do {
|
||||
d_v0 = velocity_gen(gen);
|
||||
} while (d_v0 <= 0.1);
|
||||
|
||||
double 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.0;
|
||||
w_y[i] = 0.0;
|
||||
curr_vel_x[i] = 0.0;
|
||||
curr_vel_y[i] = 0.0;
|
||||
sb_sq[i] = 0.0;
|
||||
sb_e_x[i] = 0.0;
|
||||
sb_e_y[i] = 0.0;
|
||||
}
|
||||
|
||||
for (int j = n_p; j < n_p_pad; j++) {
|
||||
pos_x[j] = 1e9;
|
||||
pos_y[j] = 1e9;
|
||||
sb_sq[j] = 0.0;
|
||||
sb_e_x[j] = 0.0;
|
||||
sb_e_y[j] = 0.0;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
|
||||
|
||||
// # 128 MULTs, 112 ADDs
|
||||
static inline __m512d _avx512_exp_pd(__m512d x) {
|
||||
|
||||
const __m512d log2e = _mm512_set1_pd(1.4426950408889634);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d nf = _mm512_roundscale_pd(_mm512_mul_pd(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
|
||||
const __m512d ln2 = _mm512_set1_pd(0.6931471805599453);
|
||||
|
||||
// # 8 MULTs, 8 ADDs
|
||||
__m512d r = _mm512_fnmadd_pd(nf, ln2, x);
|
||||
|
||||
const __m512d c13 = _mm512_set1_pd(c13_scalar);
|
||||
const __m512d c12 = _mm512_set1_pd(c12_scalar);
|
||||
const __m512d c11 = _mm512_set1_pd(c11_scalar);
|
||||
const __m512d c10 = _mm512_set1_pd(c10_scalar);
|
||||
const __m512d c9 = _mm512_set1_pd(c9_scalar);
|
||||
const __m512d c8 = _mm512_set1_pd(c8_scalar);
|
||||
const __m512d c7 = _mm512_set1_pd(c7_scalar);
|
||||
const __m512d c6 = _mm512_set1_pd(c6_scalar);
|
||||
const __m512d c5 = _mm512_set1_pd(c5_scalar);
|
||||
const __m512d c4 = _mm512_set1_pd(c4_scalar);
|
||||
const __m512d c3 = _mm512_set1_pd(c3_scalar);
|
||||
const __m512d c2 = _mm512_set1_pd(c2_scalar);
|
||||
const __m512d c1 = _mm512_set1_pd(c1_scalar);
|
||||
const __m512d c0 = _mm512_set1_pd(c0_scalar);
|
||||
|
||||
__m512d p = c13;
|
||||
|
||||
// # 104 MULTs, 104 ADDs
|
||||
p = _mm512_fmadd_pd(p, r, c12);
|
||||
p = _mm512_fmadd_pd(p, r, c11);
|
||||
p = _mm512_fmadd_pd(p, r, c10);
|
||||
p = _mm512_fmadd_pd(p, r, c9);
|
||||
p = _mm512_fmadd_pd(p, r, c8);
|
||||
p = _mm512_fmadd_pd(p, r, c7);
|
||||
p = _mm512_fmadd_pd(p, r, c6);
|
||||
p = _mm512_fmadd_pd(p, r, c5);
|
||||
p = _mm512_fmadd_pd(p, r, c4);
|
||||
p = _mm512_fmadd_pd(p, r, c3);
|
||||
p = _mm512_fmadd_pd(p, r, c2);
|
||||
p = _mm512_fmadd_pd(p, r, c1);
|
||||
p = _mm512_fmadd_pd(p, r, c0);
|
||||
|
||||
__m256d nf_lo = _mm512_castpd512_pd256(nf);
|
||||
__m256d nf_hi = _mm512_extractf64x4_pd(nf, 1);
|
||||
|
||||
__m128i ni32_lo = _mm256_cvtpd_epi32(nf_lo);
|
||||
__m128i ni32_hi = _mm256_cvtpd_epi32(nf_hi);
|
||||
|
||||
__m256i ni64_lo = _mm256_cvtepi32_epi64(ni32_lo);
|
||||
__m256i ni64_hi = _mm256_cvtepi32_epi64(ni32_hi);
|
||||
|
||||
__m512i ni64 = _mm512_inserti64x4(_mm512_castsi256_si512(ni64_lo), ni64_hi, 1);
|
||||
|
||||
__m512i biased = _mm512_add_epi64(ni64, _mm512_set1_epi64(1023));
|
||||
|
||||
__m512i scaled = _mm512_slli_epi64(biased, 52);
|
||||
|
||||
__m512d pow2n = _mm512_castsi512_pd(scaled);
|
||||
|
||||
// # 8 MULTs
|
||||
return _mm512_mul_pd(p, pow2n);
|
||||
}
|
||||
|
||||
static inline double fast_inv_sqrt(double number) {
|
||||
double y = number;
|
||||
|
||||
uint64_t i = bit_cast<uint64_t>(y);
|
||||
|
||||
i = 0x5fe6eb50c7b537a9ULL - (i >> 1);
|
||||
|
||||
y = bit_cast<double>(i);
|
||||
|
||||
double x2 = number * 0.5;
|
||||
y = y * (1.5 - (x2 * y * y));
|
||||
y = y * (1.5 - (x2 * y * y));
|
||||
y = y * (1.5 - (x2 * y * y));
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
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<tuple<int, int, double, double, double, double, 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
|
||||
|
||||
const int n_p_pad = (n_p + 7) & ~7;
|
||||
|
||||
alignas(64) constexpr double lane_offset_arr[8] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0};
|
||||
const __m512d lane_off = _mm512_load_pd(lane_offset_arr);
|
||||
const __m512d v_neg_v0_div_2s = _mm512_set1_pd(NEG_V0_AB_div_TWO_SIGMA);
|
||||
const __m512d v_neg_2s_inv = _mm512_set1_pd(NEG_TWO_SIGMA_INV);
|
||||
const __m512d v_cos_phi_inv = _mm512_set1_pd(cos_phi_inv);
|
||||
const __m512d v_dt_neg = _mm512_set1_pd(dt_neg);
|
||||
const __m512d v_c_neg_dt = _mm512_set1_pd(c_neg_times_dt);
|
||||
const __m512d v_zero = _mm512_setzero_pd();
|
||||
|
||||
const __m512d v_np = _mm512_set1_pd(n_p);
|
||||
|
||||
// # ADDs: n_iter * n_p * 33 +
|
||||
// # n_iter * n_p * (n_p_pad / 8) * 232 +
|
||||
// # n_iter * n_p * board.n_obstacles * 10
|
||||
|
||||
// # MULTs: n_iter * n_p * 40 +
|
||||
// # n_iter * n_p * (n_p_pad / 8) * 320 +
|
||||
// # n_iter * n_p * board.n_obstacles * 13
|
||||
|
||||
// # DIVs: n_iter * n_p * (n_p_pad / 8) * 8 +
|
||||
// # n_iter * n_p * board.n_obstacles
|
||||
|
||||
// # SQRTs: n_iter * n_p +
|
||||
// # n_iter * n_p * (n_p_pad / 8) * 24 +
|
||||
// # n_iter * n_p * board.n_obstacles
|
||||
|
||||
// # EXPs: n_iter * n_p * board.n_obstacles
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
double pos_x_i = pos_x[i];
|
||||
double pos_y_i = pos_y[i];
|
||||
double e_x_i = e_x[i];
|
||||
double e_y_i = e_y[i];
|
||||
double desired_v0_i = desired_v0[i];
|
||||
double curr_vel_x_i = curr_vel_x[i];
|
||||
double curr_vel_y_i = curr_vel_y[i];
|
||||
|
||||
double f_alpha_dt_x = (e_x_i * desired_v0_i - curr_vel_x_i) * dt_times_tau_alpha_inv;
|
||||
double f_alpha_dt_y = (e_y_i * desired_v0_i - curr_vel_y_i) * dt_times_tau_alpha_inv;
|
||||
|
||||
const __m512d pix = _mm512_set1_pd(pos_x_i);
|
||||
const __m512d piy = _mm512_set1_pd(pos_y_i);
|
||||
const __m512d eix = _mm512_set1_pd(e_x_i);
|
||||
const __m512d eiy = _mm512_set1_pd(e_y_i);
|
||||
const __m512d v_i = _mm512_set1_pd((double)i);
|
||||
|
||||
__m512d acc_x = _mm512_setzero_pd();
|
||||
__m512d acc_y = _mm512_setzero_pd();
|
||||
|
||||
for (int j = 0; j < n_p_pad; j += 8) {
|
||||
__m512d j_idx = _mm512_add_pd(_mm512_set1_pd((double)j), lane_off);
|
||||
|
||||
__mmask8 m_neq_i = _mm512_cmp_pd_mask(j_idx, v_i, _CMP_NEQ_OQ);
|
||||
__mmask8 m_in_range = _mm512_cmp_pd_mask(j_idx, v_np, _CMP_LT_OQ);
|
||||
__mmask8 active = m_neq_i & m_in_range;
|
||||
|
||||
__m512d pjx = _mm512_loadu_pd(&pos_x[j]);
|
||||
__m512d pjy = _mm512_loadu_pd(&pos_y[j]);
|
||||
__m512d sbe_jx = _mm512_loadu_pd(&sb_e_x[j]);
|
||||
__m512d sbe_jy = _mm512_loadu_pd(&sb_e_y[j]);
|
||||
__m512d sb_jsq = _mm512_loadu_pd(&sb_sq[j]);
|
||||
|
||||
__m512d r_ab_x = _mm512_sub_pd(pix, pjx);
|
||||
__m512d r_ab_y = _mm512_sub_pd(piy, pjy);
|
||||
__m512d r_ab_n2 = _mm512_fmadd_pd(r_ab_x, r_ab_x, _mm512_mul_pd(r_ab_y, r_ab_y));
|
||||
__m512d r_ab_n = _mm512_sqrt_pd(r_ab_n2);
|
||||
|
||||
__m512d r_ab2_x = _mm512_sub_pd(r_ab_x, sbe_jx);
|
||||
__m512d r_ab2_y = _mm512_sub_pd(r_ab_y, sbe_jy);
|
||||
__m512d r_ab2_n2 = _mm512_fmadd_pd(r_ab2_x, r_ab2_x, _mm512_mul_pd(r_ab2_y, r_ab2_y));
|
||||
__m512d r_ab2_n = _mm512_sqrt_pd(r_ab2_n2);
|
||||
|
||||
__m512d r_sum = _mm512_add_pd(r_ab_n, r_ab2_n);
|
||||
__m512d two_b_sq = _mm512_fmsub_pd(r_sum, r_sum, sb_jsq);
|
||||
__m512d two_b = _mm512_sqrt_pd(two_b_sq);
|
||||
|
||||
__m512d e_arg = _mm512_mul_pd(two_b, v_neg_2s_inv);
|
||||
__m512d e_val = _avx512_exp_pd(e_arg);
|
||||
|
||||
__m512d numer = _mm512_mul_pd(_mm512_mul_pd(r_sum, v_neg_v0_div_2s), e_val);
|
||||
__m512d denom = _mm512_mul_pd(_mm512_mul_pd(two_b, r_ab_n), r_ab2_n);
|
||||
__m512d denom_safe = _mm512_mask_blend_pd(active, _mm512_set1_pd(1.0), denom);
|
||||
__m512d smc = _mm512_div_pd(numer, denom_safe);
|
||||
|
||||
__m512d sm_r_ab = _mm512_mul_pd(smc, r_ab2_n);
|
||||
__m512d sm_r_ab2 = _mm512_mul_pd(smc, r_ab_n);
|
||||
|
||||
__m512d f_x = _mm512_fmadd_pd(r_ab2_x, sm_r_ab2, _mm512_mul_pd(r_ab_x, sm_r_ab));
|
||||
__m512d f_y = _mm512_fmadd_pd(r_ab2_y, sm_r_ab2, _mm512_mul_pd(r_ab_y, sm_r_ab));
|
||||
|
||||
__m512d a = _mm512_mul_pd(_mm512_fmadd_pd(eix, f_x, _mm512_mul_pd(eiy, f_y)), v_cos_phi_inv);
|
||||
__m512d a_sq = _mm512_mul_pd(a, a);
|
||||
__m512d b_sq = _mm512_fmadd_pd(f_x, f_x, _mm512_mul_pd(f_y, f_y));
|
||||
|
||||
__mmask8 k_ge = _mm512_cmp_pd_mask(a, v_zero, _CMP_GE_OQ);
|
||||
__mmask8 k_gt = _mm512_cmp_pd_mask(a_sq, b_sq, _CMP_GT_OQ);
|
||||
__mmask8 k_cond = _mm512_kand(k_ge, k_gt);
|
||||
__m512d w_val = _mm512_mask_blend_pd(k_cond, v_dt_neg, v_c_neg_dt);
|
||||
|
||||
__m512d f_x_safe = _mm512_maskz_mov_pd(active, f_x);
|
||||
__m512d f_y_safe = _mm512_maskz_mov_pd(active, f_y);
|
||||
acc_x = _mm512_fmadd_pd(f_x_safe, w_val, acc_x);
|
||||
acc_y = _mm512_fmadd_pd(f_y_safe, w_val, acc_y);
|
||||
}
|
||||
|
||||
f_alpha_dt_x += _mm512_reduce_add_pd(acc_x);
|
||||
f_alpha_dt_y += _mm512_reduce_add_pd(acc_y);
|
||||
|
||||
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 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 = 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_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 = dt_neg;
|
||||
double 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;
|
||||
|
||||
f_alpha_dt_x += f_alpha_B_neg_x * w_val;
|
||||
f_alpha_dt_y += f_alpha_B_neg_y * w_val;
|
||||
}
|
||||
|
||||
w_x[i] += f_alpha_dt_x;
|
||||
w_y[i] += f_alpha_dt_y;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
double w_norm_inv = fast_inv_sqrt(w_x[i] * w_x[i] + w_y[i] * w_y[i]);
|
||||
|
||||
double correction_factor = min(1.0, v_max[i] * w_norm_inv);
|
||||
|
||||
curr_vel_x[i] = w_x[i] * correction_factor;
|
||||
curr_vel_y[i] = w_y[i] * correction_factor;
|
||||
|
||||
pos_x[i] = pos_x[i] + curr_vel_x[i] * dt;
|
||||
pos_y[i] = pos_y[i] + curr_vel_y[i] * dt;
|
||||
|
||||
if (!color[i] && 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] && 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);
|
||||
|
||||
double ex = destination_x[i] - pos_x[i];
|
||||
double ey = destination_y[i] - pos_y[i];
|
||||
|
||||
double e_norm_inv = fast_inv_sqrt(ex * ex + ey * ey);
|
||||
|
||||
e_x[i] = ex * e_norm_inv;
|
||||
e_y[i] = ey * e_norm_inv;
|
||||
|
||||
double velocity_norm_sq = curr_vel_x[i] * curr_vel_x[i] + curr_vel_y[i] * curr_vel_y[i];
|
||||
double sb_sq_i = velocity_norm_sq * delta_t_sq;
|
||||
double velocity_norm_mul_delta_t = sqrt(sb_sq_i);
|
||||
|
||||
sb_sq[i] = sb_sq_i;
|
||||
|
||||
sb_e_x[i] = e_x[i] * velocity_norm_mul_delta_t;
|
||||
sb_e_y[i] = e_y[i] * velocity_norm_mul_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;
|
||||
}
|
||||
@@ -0,0 +1,576 @@
|
||||
// # Changes: Performed Scalar Replacements
|
||||
// # Predecessor: variant_inlining_8_SoA_AVX512_polyexp_fastinvsqrt.cpp
|
||||
|
||||
#include "../headers/board_reduced.hpp"
|
||||
#include "../headers/constants.hpp"
|
||||
#include "../headers/pedestrian_reduced.hpp"
|
||||
|
||||
#include <immintrin.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
#define SEED 42
|
||||
#define BOARD_WIDTH 10.0
|
||||
#define BOARD_LENGTH 50.0
|
||||
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0
|
||||
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5
|
||||
#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;
|
||||
|
||||
alignas(64) vector<double> pos_x, pos_y;
|
||||
alignas(64) vector<double> sb_sq;
|
||||
alignas(64) vector<double> sb_e_x, sb_e_y;
|
||||
alignas(64) vector<double> e_x, e_y;
|
||||
alignas(64) vector<double> curr_vel_x, curr_vel_y;
|
||||
alignas(64) vector<double> w_x, w_y;
|
||||
alignas(64) vector<double> desired_v0;
|
||||
alignas(64) vector<double> v_max;
|
||||
alignas(64) vector<double> destination_x, destination_y;
|
||||
alignas(64) 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<double> velocity_gen(v0_mean, v0_std_dev);
|
||||
uniform_real_distribution<double> y_gen(0.3, BOARD_WIDTH - 0.3);
|
||||
uniform_real_distribution<double> x_gen_left(2.0, BOARD_LENGTH / 2.0);
|
||||
uniform_real_distribution<double> 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);
|
||||
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++) {
|
||||
double x = x_gen_left(gen);
|
||||
double y = y_gen(gen);
|
||||
|
||||
double dest_x = BOARD_LENGTH + 30.0;
|
||||
double dest_y = y;
|
||||
|
||||
double init_e_x = dest_x - x;
|
||||
double init_e_y = dest_y - 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 d_v0;
|
||||
do {
|
||||
d_v0 = velocity_gen(gen);
|
||||
} while (d_v0 <= 0.1);
|
||||
|
||||
double 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;
|
||||
|
||||
w_x[i] = 0.0;
|
||||
w_y[i] = 0.0;
|
||||
curr_vel_x[i] = 0.0;
|
||||
curr_vel_y[i] = 0.0;
|
||||
sb_sq[i] = 0.0;
|
||||
sb_e_x[i] = 0.0;
|
||||
sb_e_y[i] = 0.0;
|
||||
}
|
||||
|
||||
for (int i = n_p_half; i < n_p; i++) {
|
||||
double x = x_gen_right(gen);
|
||||
double y = y_gen(gen);
|
||||
|
||||
double dest_x = -30;
|
||||
double dest_y = y;
|
||||
|
||||
double init_e_x = dest_x - x;
|
||||
double init_e_y = dest_y - 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 d_v0;
|
||||
do {
|
||||
d_v0 = velocity_gen(gen);
|
||||
} while (d_v0 <= 0.1);
|
||||
|
||||
double 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.0;
|
||||
w_y[i] = 0.0;
|
||||
curr_vel_x[i] = 0.0;
|
||||
curr_vel_y[i] = 0.0;
|
||||
sb_sq[i] = 0.0;
|
||||
sb_e_x[i] = 0.0;
|
||||
sb_e_y[i] = 0.0;
|
||||
}
|
||||
|
||||
for (int j = n_p; j < n_p_pad; j++) {
|
||||
pos_x[j] = 1e9;
|
||||
pos_y[j] = 1e9;
|
||||
sb_sq[j] = 0.0;
|
||||
sb_e_x[j] = 0.0;
|
||||
sb_e_y[j] = 0.0;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
|
||||
|
||||
// # 128 MULTs, 112 ADDs
|
||||
static inline __m512d _avx512_exp_pd(__m512d x) {
|
||||
|
||||
const __m512d log2e = _mm512_set1_pd(1.4426950408889634);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d nf = _mm512_roundscale_pd(_mm512_mul_pd(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
|
||||
const __m512d ln2 = _mm512_set1_pd(0.6931471805599453);
|
||||
|
||||
// # 8 MULTs, 8 ADDs
|
||||
__m512d r = _mm512_fnmadd_pd(nf, ln2, x);
|
||||
|
||||
const __m512d c13 = _mm512_set1_pd(c13_scalar);
|
||||
const __m512d c12 = _mm512_set1_pd(c12_scalar);
|
||||
const __m512d c11 = _mm512_set1_pd(c11_scalar);
|
||||
const __m512d c10 = _mm512_set1_pd(c10_scalar);
|
||||
const __m512d c9 = _mm512_set1_pd(c9_scalar);
|
||||
const __m512d c8 = _mm512_set1_pd(c8_scalar);
|
||||
const __m512d c7 = _mm512_set1_pd(c7_scalar);
|
||||
const __m512d c6 = _mm512_set1_pd(c6_scalar);
|
||||
const __m512d c5 = _mm512_set1_pd(c5_scalar);
|
||||
const __m512d c4 = _mm512_set1_pd(c4_scalar);
|
||||
const __m512d c3 = _mm512_set1_pd(c3_scalar);
|
||||
const __m512d c2 = _mm512_set1_pd(c2_scalar);
|
||||
const __m512d c1 = _mm512_set1_pd(c1_scalar);
|
||||
const __m512d c0 = _mm512_set1_pd(c0_scalar);
|
||||
|
||||
__m512d p = c13;
|
||||
|
||||
// # 104 MULTs, 104 ADDs
|
||||
p = _mm512_fmadd_pd(p, r, c12);
|
||||
p = _mm512_fmadd_pd(p, r, c11);
|
||||
p = _mm512_fmadd_pd(p, r, c10);
|
||||
p = _mm512_fmadd_pd(p, r, c9);
|
||||
p = _mm512_fmadd_pd(p, r, c8);
|
||||
p = _mm512_fmadd_pd(p, r, c7);
|
||||
p = _mm512_fmadd_pd(p, r, c6);
|
||||
p = _mm512_fmadd_pd(p, r, c5);
|
||||
p = _mm512_fmadd_pd(p, r, c4);
|
||||
p = _mm512_fmadd_pd(p, r, c3);
|
||||
p = _mm512_fmadd_pd(p, r, c2);
|
||||
p = _mm512_fmadd_pd(p, r, c1);
|
||||
p = _mm512_fmadd_pd(p, r, c0);
|
||||
|
||||
__m256d nf_lo = _mm512_castpd512_pd256(nf);
|
||||
__m256d nf_hi = _mm512_extractf64x4_pd(nf, 1);
|
||||
|
||||
__m128i ni32_lo = _mm256_cvtpd_epi32(nf_lo);
|
||||
__m128i ni32_hi = _mm256_cvtpd_epi32(nf_hi);
|
||||
|
||||
__m256i ni64_lo = _mm256_cvtepi32_epi64(ni32_lo);
|
||||
__m256i ni64_hi = _mm256_cvtepi32_epi64(ni32_hi);
|
||||
|
||||
__m512i ni64 = _mm512_inserti64x4(_mm512_castsi256_si512(ni64_lo), ni64_hi, 1);
|
||||
|
||||
__m512i biased = _mm512_add_epi64(ni64, _mm512_set1_epi64(1023));
|
||||
|
||||
__m512i scaled = _mm512_slli_epi64(biased, 52);
|
||||
|
||||
__m512d pow2n = _mm512_castsi512_pd(scaled);
|
||||
|
||||
// # 8 MULTs
|
||||
return _mm512_mul_pd(p, pow2n);
|
||||
}
|
||||
|
||||
// # 10 MULTs, 3 ADDs
|
||||
static inline double fast_inv_sqrt(double number) {
|
||||
double y = number;
|
||||
|
||||
uint64_t i = bit_cast<uint64_t>(y);
|
||||
|
||||
i = 0x5fe6eb50c7b537a9ULL - (i >> 1);
|
||||
|
||||
y = bit_cast<double>(i);
|
||||
|
||||
double x2 = number * 0.5;
|
||||
y = y * (1.5 - (x2 * y * y));
|
||||
y = y * (1.5 - (x2 * y * y));
|
||||
y = y * (1.5 - (x2 * y * y));
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
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<tuple<int, int, double, double, double, double, 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
|
||||
|
||||
const int n_p_pad = (n_p + 7) & ~7;
|
||||
|
||||
alignas(64) constexpr double lane_offset_arr[8] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0};
|
||||
const __m512d lane_off = _mm512_load_pd(lane_offset_arr);
|
||||
const __m512d v_neg_v0_div_2s = _mm512_set1_pd(NEG_V0_AB_div_TWO_SIGMA);
|
||||
const __m512d v_neg_2s_inv = _mm512_set1_pd(NEG_TWO_SIGMA_INV);
|
||||
const __m512d v_cos_phi_inv = _mm512_set1_pd(cos_phi_inv);
|
||||
const __m512d v_dt_neg = _mm512_set1_pd(dt_neg);
|
||||
const __m512d v_c_neg_dt = _mm512_set1_pd(c_neg_times_dt);
|
||||
const __m512d v_zero = _mm512_setzero_pd();
|
||||
|
||||
const __m512d v_np = _mm512_set1_pd(n_p);
|
||||
|
||||
// # ADDs: n_iter * n_p * 33 +
|
||||
// # n_iter * n_p * (n_p_pad / 8) * 232 +
|
||||
// # n_iter * n_p * board.n_obstacles * 10
|
||||
|
||||
// # MULTs: n_iter * n_p * 40 +
|
||||
// # n_iter * n_p * (n_p_pad / 8) * 320 +
|
||||
// # n_iter * n_p * board.n_obstacles * 13
|
||||
|
||||
// # DIVs: n_iter * n_p * (n_p_pad / 8) * 8 +
|
||||
// # n_iter * n_p * board.n_obstacles
|
||||
|
||||
// # SQRTs: n_iter * n_p +
|
||||
// # n_iter * n_p * (n_p_pad / 8) * 24 +
|
||||
// # n_iter * n_p * board.n_obstacles
|
||||
|
||||
// # EXPs: n_iter * n_p * board.n_obstacles
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
|
||||
double pos_x_i = pos_x[i];
|
||||
double pos_y_i = pos_y[i];
|
||||
double e_x_i = e_x[i];
|
||||
double e_y_i = e_y[i];
|
||||
double desired_v0_i = desired_v0[i];
|
||||
double curr_vel_x_i = curr_vel_x[i];
|
||||
double curr_vel_y_i = curr_vel_y[i];
|
||||
|
||||
// # 4 MULTs, 2 ADDs
|
||||
double f_alpha_dt_x = (e_x_i * desired_v0_i - curr_vel_x_i) * dt_times_tau_alpha_inv;
|
||||
double f_alpha_dt_y = (e_y_i * desired_v0_i - curr_vel_y_i) * dt_times_tau_alpha_inv;
|
||||
|
||||
const __m512d pix = _mm512_set1_pd(pos_x_i);
|
||||
const __m512d piy = _mm512_set1_pd(pos_y_i);
|
||||
const __m512d eix = _mm512_set1_pd(e_x_i);
|
||||
const __m512d eiy = _mm512_set1_pd(e_y_i);
|
||||
const __m512d v_i = _mm512_set1_pd((double)i);
|
||||
|
||||
__m512d acc_x = _mm512_setzero_pd();
|
||||
__m512d acc_y = _mm512_setzero_pd();
|
||||
|
||||
for (int j = 0; j < n_p_pad; j += 8) {
|
||||
|
||||
// # 8 ADDs
|
||||
__m512d j_idx = _mm512_add_pd(_mm512_set1_pd((double)j), lane_off);
|
||||
|
||||
__mmask8 m_neq_i = _mm512_cmp_pd_mask(j_idx, v_i, _CMP_NEQ_OQ);
|
||||
__mmask8 m_in_range = _mm512_cmp_pd_mask(j_idx, v_np, _CMP_LT_OQ);
|
||||
__mmask8 active = m_neq_i & m_in_range;
|
||||
|
||||
__m512d pjx = _mm512_loadu_pd(&pos_x[j]);
|
||||
__m512d pjy = _mm512_loadu_pd(&pos_y[j]);
|
||||
__m512d sbe_jx = _mm512_loadu_pd(&sb_e_x[j]);
|
||||
__m512d sbe_jy = _mm512_loadu_pd(&sb_e_y[j]);
|
||||
__m512d sb_jsq = _mm512_loadu_pd(&sb_sq[j]);
|
||||
|
||||
// # 16 ADDs
|
||||
__m512d r_ab_x = _mm512_sub_pd(pix, pjx);
|
||||
__m512d r_ab_y = _mm512_sub_pd(piy, pjy);
|
||||
|
||||
// # 16 MULTs, 8 ADDs
|
||||
__m512d r_ab_n2 = _mm512_fmadd_pd(r_ab_x, r_ab_x, _mm512_mul_pd(r_ab_y, r_ab_y));
|
||||
|
||||
// # 8 SQRTs
|
||||
__m512d r_ab_n = _mm512_sqrt_pd(r_ab_n2);
|
||||
|
||||
// # 16 ADDs
|
||||
__m512d r_ab2_x = _mm512_sub_pd(r_ab_x, sbe_jx);
|
||||
__m512d r_ab2_y = _mm512_sub_pd(r_ab_y, sbe_jy);
|
||||
|
||||
// # 16 MULTs, 8 ADDs
|
||||
__m512d r_ab2_n2 = _mm512_fmadd_pd(r_ab2_x, r_ab2_x, _mm512_mul_pd(r_ab2_y, r_ab2_y));
|
||||
|
||||
// # 8 SQRTs
|
||||
__m512d r_ab2_n = _mm512_sqrt_pd(r_ab2_n2);
|
||||
|
||||
// # 8 ADDs
|
||||
__m512d r_sum = _mm512_add_pd(r_ab_n, r_ab2_n);
|
||||
|
||||
// # 8 MULTs, 8 ADDs
|
||||
__m512d two_b_sq = _mm512_fmsub_pd(r_sum, r_sum, sb_jsq);
|
||||
|
||||
// # 8 SQRTs
|
||||
__m512d two_b = _mm512_sqrt_pd(two_b_sq);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d e_arg = _mm512_mul_pd(two_b, v_neg_2s_inv);
|
||||
|
||||
// # 128 MULTs, 112 ADDs
|
||||
__m512d e_val = _avx512_exp_pd(e_arg);
|
||||
|
||||
// # 32 MULTs
|
||||
__m512d numer = _mm512_mul_pd(_mm512_mul_pd(r_sum, v_neg_v0_div_2s), e_val);
|
||||
__m512d denom = _mm512_mul_pd(_mm512_mul_pd(two_b, r_ab_n), r_ab2_n);
|
||||
|
||||
__m512d denom_safe = _mm512_mask_blend_pd(active, _mm512_set1_pd(1.0), denom);
|
||||
|
||||
// # 8 DIVs
|
||||
__m512d smc = _mm512_div_pd(numer, denom_safe);
|
||||
|
||||
// # 16 MULTs
|
||||
__m512d sm_r_ab = _mm512_mul_pd(smc, r_ab2_n);
|
||||
__m512d sm_r_ab2 = _mm512_mul_pd(smc, r_ab_n);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512d f_x = _mm512_fmadd_pd(r_ab2_x, sm_r_ab2, _mm512_mul_pd(r_ab_x, sm_r_ab));
|
||||
__m512d f_y = _mm512_fmadd_pd(r_ab2_y, sm_r_ab2, _mm512_mul_pd(r_ab_y, sm_r_ab));
|
||||
|
||||
// # 24 MULTs, 8 ADDs
|
||||
__m512d a = _mm512_mul_pd(_mm512_fmadd_pd(eix, f_x, _mm512_mul_pd(eiy, f_y)), v_cos_phi_inv);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d a_sq = _mm512_mul_pd(a, a);
|
||||
|
||||
// # 16 MULTs, 8 ADDs
|
||||
__m512d b_sq = _mm512_fmadd_pd(f_x, f_x, _mm512_mul_pd(f_y, f_y));
|
||||
|
||||
__mmask8 k_ge = _mm512_cmp_pd_mask(a, v_zero, _CMP_GE_OQ);
|
||||
__mmask8 k_gt = _mm512_cmp_pd_mask(a_sq, b_sq, _CMP_GT_OQ);
|
||||
__mmask8 k_cond = _mm512_kand(k_ge, k_gt);
|
||||
__m512d w_val = _mm512_mask_blend_pd(k_cond, v_dt_neg, v_c_neg_dt);
|
||||
|
||||
__m512d f_x_safe = _mm512_maskz_mov_pd(active, f_x);
|
||||
__m512d f_y_safe = _mm512_maskz_mov_pd(active, f_y);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
acc_x = _mm512_fmadd_pd(f_x_safe, w_val, acc_x);
|
||||
acc_y = _mm512_fmadd_pd(f_y_safe, w_val, acc_y);
|
||||
}
|
||||
|
||||
// # 16 ADDs
|
||||
f_alpha_dt_x += _mm512_reduce_add_pd(acc_x);
|
||||
f_alpha_dt_y += _mm512_reduce_add_pd(acc_y);
|
||||
|
||||
for (int k = 0; k < board.n_obstacles; k++) {
|
||||
|
||||
double obs_start_x = board.obstacles[k].start_x;
|
||||
double obs_start_y = board.obstacles[k].start_y;
|
||||
double obs_v_x = board.obstacles[k].obstacle_v_x;
|
||||
double obs_v_y = board.obstacles[k].obstacle_v_y;
|
||||
double obs_length_sq_inv = board.obstacles[k].length_sq_inv;
|
||||
|
||||
// # 2 ADDs
|
||||
double v1_x = pos_x_i - obs_start_x;
|
||||
double v1_y = pos_y_i - obs_start_y;
|
||||
|
||||
// # 3 MULTs, 1 ADD
|
||||
double projection = (v1_x * obs_v_x + v1_y * obs_v_y) * obs_length_sq_inv;
|
||||
|
||||
if (projection < 0.0)
|
||||
projection = 0.0;
|
||||
else if (projection > 1.0)
|
||||
projection = 1.0;
|
||||
|
||||
// # 2 MULTs, 4 ADDs
|
||||
double r_aB_x = pos_x_i - (obs_start_x + obs_v_x * projection);
|
||||
double r_aB_y = pos_y_i - (obs_start_y + obs_v_y * projection);
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
|
||||
|
||||
// # 2 MULTs, 1 EXP, 1 DIV
|
||||
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
|
||||
|
||||
// # 2 MULTs
|
||||
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 = dt_neg;
|
||||
double 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;
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
f_alpha_dt_x += f_alpha_B_neg_x * w_val;
|
||||
f_alpha_dt_y += f_alpha_B_neg_y * w_val;
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
w_x[i] += f_alpha_dt_x;
|
||||
w_y[i] += f_alpha_dt_y;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
|
||||
double color_i = color[i];
|
||||
double curr_vel_x_i = curr_vel_x[i];
|
||||
double curr_vel_y_i = curr_vel_y[i];
|
||||
double w_x_i = w_x[i];
|
||||
double w_y_i = w_y[i];
|
||||
double pos_x_i = pos_x[i];
|
||||
double pos_y_i = pos_y[i];
|
||||
double e_x_i = e_x[i];
|
||||
double e_y_i = e_y[i];
|
||||
double v_max_i = v_max[i];
|
||||
double desired_v0_i = desired_v0[i];
|
||||
double destination_x_i = destination_x[i];
|
||||
double destination_y_i = destination_y[i];
|
||||
|
||||
// # 12 MULTs, 4 ADDs
|
||||
double w_norm_inv = fast_inv_sqrt(w_x_i * w_x_i + w_y_i * w_y_i);
|
||||
|
||||
// # 1 MULT
|
||||
double correction_factor = min(1.0, v_max_i * w_norm_inv);
|
||||
|
||||
// # 2 MULTs
|
||||
curr_vel_x_i = w_x_i * correction_factor;
|
||||
curr_vel_y_i = w_y_i * correction_factor;
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
pos_x_i = pos_x_i + curr_vel_x_i * dt;
|
||||
pos_y_i = pos_y_i + curr_vel_y_i * dt;
|
||||
|
||||
if (!color_i && pos_x_i > (BOARD_LENGTH_MINUS_ONE_POINT_FIVE)) {
|
||||
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 && pos_x_i < 1.5) {
|
||||
pos_x_i = BOARD_LENGTH_MINUS_ONE;
|
||||
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);
|
||||
|
||||
// # 2 ADDs
|
||||
double ex = destination_x_i - pos_x_i;
|
||||
double ey = destination_y_i - pos_y_i;
|
||||
|
||||
// # 12 MULTs, 4 ADDs
|
||||
double e_norm_inv = fast_inv_sqrt(ex * ex + ey * ey);
|
||||
|
||||
// # 2 MULTs
|
||||
e_x_i = ex * e_norm_inv;
|
||||
e_y_i = ey * e_norm_inv;
|
||||
|
||||
// # 2 MULTs, 1 ADD
|
||||
double velocity_norm_sq = curr_vel_x_i * curr_vel_x_i + curr_vel_y_i * curr_vel_y_i;
|
||||
|
||||
// # 1 MULT
|
||||
double sb_sq_i = velocity_norm_sq * delta_t_sq;
|
||||
|
||||
// # 1 SQRT
|
||||
double velocity_norm_mul_delta_t = sqrt(sb_sq_i);
|
||||
|
||||
curr_vel_x[i] = curr_vel_x_i;
|
||||
curr_vel_y[i] = curr_vel_y_i;
|
||||
|
||||
w_x[i] = w_x_i;
|
||||
w_y[i] = w_y_i;
|
||||
|
||||
pos_x[i] = pos_x_i;
|
||||
pos_y[i] = pos_y_i;
|
||||
|
||||
e_x[i] = e_x_i;
|
||||
e_y[i] = e_y_i;
|
||||
|
||||
destination_y[i] = destination_y_i;
|
||||
|
||||
sb_sq[i] = sb_sq_i;
|
||||
|
||||
// # 2 MULTs
|
||||
sb_e_x[i] = e_x_i * velocity_norm_mul_delta_t;
|
||||
sb_e_y[i] = e_y_i * velocity_norm_mul_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;
|
||||
}
|
||||
@@ -0,0 +1,563 @@
|
||||
// # Changes: Vectorised second pass with AVX-512
|
||||
// # Predecessor: variant_inlining_8_SoA_AVX512_polyexp_fastinvsqrt.cpp
|
||||
|
||||
#include "../headers/board_reduced.hpp"
|
||||
#include "../headers/constants.hpp"
|
||||
#include "../headers/pedestrian_reduced.hpp"
|
||||
|
||||
#include <immintrin.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
|
||||
#define SEED 42
|
||||
#define BOARD_WIDTH 10.0
|
||||
#define BOARD_LENGTH 50.0
|
||||
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0
|
||||
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5
|
||||
#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;
|
||||
|
||||
alignas(64) vector<double> pos_x, pos_y;
|
||||
alignas(64) vector<double> sb_sq;
|
||||
alignas(64) vector<double> sb_e_x, sb_e_y;
|
||||
alignas(64) vector<double> e_x, e_y;
|
||||
alignas(64) vector<double> curr_vel_x, curr_vel_y;
|
||||
alignas(64) vector<double> w_x, w_y;
|
||||
alignas(64) vector<double> desired_v0;
|
||||
alignas(64) vector<double> v_max;
|
||||
alignas(64) vector<double> destination_x, destination_y;
|
||||
alignas(64) 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<double> velocity_gen(v0_mean, v0_std_dev);
|
||||
uniform_real_distribution<double> y_gen(0.3, BOARD_WIDTH - 0.3);
|
||||
uniform_real_distribution<double> x_gen_left(2.0, BOARD_LENGTH / 2.0);
|
||||
uniform_real_distribution<double> 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);
|
||||
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++) {
|
||||
double x = x_gen_left(gen);
|
||||
double y = y_gen(gen);
|
||||
|
||||
double dest_x = BOARD_LENGTH + 30.0;
|
||||
double dest_y = y;
|
||||
|
||||
double init_e_x = dest_x - x;
|
||||
double init_e_y = dest_y - 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 d_v0;
|
||||
do {
|
||||
d_v0 = velocity_gen(gen);
|
||||
} while (d_v0 <= 0.1);
|
||||
|
||||
double 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;
|
||||
|
||||
w_x[i] = 0.0;
|
||||
w_y[i] = 0.0;
|
||||
curr_vel_x[i] = 0.0;
|
||||
curr_vel_y[i] = 0.0;
|
||||
sb_sq[i] = 0.0;
|
||||
sb_e_x[i] = 0.0;
|
||||
sb_e_y[i] = 0.0;
|
||||
}
|
||||
|
||||
for (int i = n_p_half; i < n_p; i++) {
|
||||
double x = x_gen_right(gen);
|
||||
double y = y_gen(gen);
|
||||
|
||||
double dest_x = -30;
|
||||
double dest_y = y;
|
||||
|
||||
double init_e_x = dest_x - x;
|
||||
double init_e_y = dest_y - 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 d_v0;
|
||||
do {
|
||||
d_v0 = velocity_gen(gen);
|
||||
} while (d_v0 <= 0.1);
|
||||
|
||||
double 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.0;
|
||||
w_y[i] = 0.0;
|
||||
curr_vel_x[i] = 0.0;
|
||||
curr_vel_y[i] = 0.0;
|
||||
sb_sq[i] = 0.0;
|
||||
sb_e_x[i] = 0.0;
|
||||
sb_e_y[i] = 0.0;
|
||||
}
|
||||
|
||||
for (int j = n_p; j < n_p_pad; j++) {
|
||||
pos_x[j] = 1e9;
|
||||
pos_y[j] = 1e9;
|
||||
sb_sq[j] = 0.0;
|
||||
sb_e_x[j] = 0.0;
|
||||
sb_e_y[j] = 0.0;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
|
||||
|
||||
// # 128 MULTs, 112 ADDs
|
||||
static inline __m512d _avx512_exp_pd(__m512d x) {
|
||||
|
||||
const __m512d log2e = _mm512_set1_pd(1.4426950408889634);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d nf = _mm512_roundscale_pd(_mm512_mul_pd(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
|
||||
const __m512d ln2 = _mm512_set1_pd(0.6931471805599453);
|
||||
|
||||
// # 8 MULTs, 8 ADDs
|
||||
__m512d r = _mm512_fnmadd_pd(nf, ln2, x);
|
||||
|
||||
const __m512d c13 = _mm512_set1_pd(c13_scalar);
|
||||
const __m512d c12 = _mm512_set1_pd(c12_scalar);
|
||||
const __m512d c11 = _mm512_set1_pd(c11_scalar);
|
||||
const __m512d c10 = _mm512_set1_pd(c10_scalar);
|
||||
const __m512d c9 = _mm512_set1_pd(c9_scalar);
|
||||
const __m512d c8 = _mm512_set1_pd(c8_scalar);
|
||||
const __m512d c7 = _mm512_set1_pd(c7_scalar);
|
||||
const __m512d c6 = _mm512_set1_pd(c6_scalar);
|
||||
const __m512d c5 = _mm512_set1_pd(c5_scalar);
|
||||
const __m512d c4 = _mm512_set1_pd(c4_scalar);
|
||||
const __m512d c3 = _mm512_set1_pd(c3_scalar);
|
||||
const __m512d c2 = _mm512_set1_pd(c2_scalar);
|
||||
const __m512d c1 = _mm512_set1_pd(c1_scalar);
|
||||
const __m512d c0 = _mm512_set1_pd(c0_scalar);
|
||||
|
||||
__m512d p = c13;
|
||||
|
||||
// # 104 MULTs, 104 ADDs
|
||||
p = _mm512_fmadd_pd(p, r, c12);
|
||||
p = _mm512_fmadd_pd(p, r, c11);
|
||||
p = _mm512_fmadd_pd(p, r, c10);
|
||||
p = _mm512_fmadd_pd(p, r, c9);
|
||||
p = _mm512_fmadd_pd(p, r, c8);
|
||||
p = _mm512_fmadd_pd(p, r, c7);
|
||||
p = _mm512_fmadd_pd(p, r, c6);
|
||||
p = _mm512_fmadd_pd(p, r, c5);
|
||||
p = _mm512_fmadd_pd(p, r, c4);
|
||||
p = _mm512_fmadd_pd(p, r, c3);
|
||||
p = _mm512_fmadd_pd(p, r, c2);
|
||||
p = _mm512_fmadd_pd(p, r, c1);
|
||||
p = _mm512_fmadd_pd(p, r, c0);
|
||||
|
||||
__m256d nf_lo = _mm512_castpd512_pd256(nf);
|
||||
__m256d nf_hi = _mm512_extractf64x4_pd(nf, 1);
|
||||
|
||||
__m128i ni32_lo = _mm256_cvtpd_epi32(nf_lo);
|
||||
__m128i ni32_hi = _mm256_cvtpd_epi32(nf_hi);
|
||||
|
||||
__m256i ni64_lo = _mm256_cvtepi32_epi64(ni32_lo);
|
||||
__m256i ni64_hi = _mm256_cvtepi32_epi64(ni32_hi);
|
||||
|
||||
__m512i ni64 = _mm512_inserti64x4(_mm512_castsi256_si512(ni64_lo), ni64_hi, 1);
|
||||
|
||||
__m512i biased = _mm512_add_epi64(ni64, _mm512_set1_epi64(1023));
|
||||
|
||||
__m512i scaled = _mm512_slli_epi64(biased, 52);
|
||||
|
||||
__m512d pow2n = _mm512_castsi512_pd(scaled);
|
||||
|
||||
// # 8 MULTs
|
||||
return _mm512_mul_pd(p, pow2n);
|
||||
}
|
||||
|
||||
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<tuple<int, int, double, double, double, double, 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
|
||||
|
||||
const int n_p_pad = (n_p + 7) & ~7;
|
||||
|
||||
alignas(64) constexpr double lane_offset_arr[8] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0};
|
||||
const __m512d lane_off = _mm512_load_pd(lane_offset_arr);
|
||||
const __m512d v_neg_v0_div_2s = _mm512_set1_pd(NEG_V0_AB_div_TWO_SIGMA);
|
||||
const __m512d v_neg_2s_inv = _mm512_set1_pd(NEG_TWO_SIGMA_INV);
|
||||
const __m512d v_cos_phi_inv = _mm512_set1_pd(cos_phi_inv);
|
||||
const __m512d v_dt_neg = _mm512_set1_pd(dt_neg);
|
||||
const __m512d v_c_neg_dt = _mm512_set1_pd(c_neg_times_dt);
|
||||
const __m512d v_zero = _mm512_setzero_pd();
|
||||
|
||||
const __m512d v_np = _mm512_set1_pd(n_p);
|
||||
|
||||
// # ADDs: n_iter * n_p * 20 +
|
||||
// # n_iter * n_p * (n_p_pad / 8) * 232 +
|
||||
// # n_iter * n_p * board.n_obstacles * 10 +
|
||||
// # n_iter * (n_p_pad / 8) * 56
|
||||
|
||||
// # MULTs: n_iter * n_p * 4 +
|
||||
// # n_iter * n_p * (n_p_pad / 8) * 320 +
|
||||
// # n_iter * n_p * board.n_obstacles * 13 +
|
||||
// # n_iter * (n_p_pad / 8) * 120
|
||||
|
||||
// # DIVs: n_iter * n_p * (n_p_pad / 8) * 8 +
|
||||
// # n_iter * n_p * board.n_obstacles +
|
||||
// # n_iter * (n_p_pad / 8) * 16
|
||||
|
||||
// # SQRTs: n_iter * n_p * (n_p_pad / 8) * 24 +
|
||||
// # n_iter * n_p * board.n_obstacles +
|
||||
// # n_iter * (n_p_pad / 8) * 24
|
||||
|
||||
// # EXPs: n_iter * n_p * board.n_obstacles
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
double pos_x_i = pos_x[i];
|
||||
double pos_y_i = pos_y[i];
|
||||
double e_x_i = e_x[i];
|
||||
double e_y_i = e_y[i];
|
||||
double desired_v0_i = desired_v0[i];
|
||||
double curr_vel_x_i = curr_vel_x[i];
|
||||
double curr_vel_y_i = curr_vel_y[i];
|
||||
|
||||
// # 4 MULTs, 2 ADDs
|
||||
double f_alpha_dt_x = (e_x_i * desired_v0_i - curr_vel_x_i) * dt_times_tau_alpha_inv;
|
||||
double f_alpha_dt_y = (e_y_i * desired_v0_i - curr_vel_y_i) * dt_times_tau_alpha_inv;
|
||||
|
||||
const __m512d pix = _mm512_set1_pd(pos_x_i);
|
||||
const __m512d piy = _mm512_set1_pd(pos_y_i);
|
||||
const __m512d eix = _mm512_set1_pd(e_x_i);
|
||||
const __m512d eiy = _mm512_set1_pd(e_y_i);
|
||||
const __m512d v_i = _mm512_set1_pd((double)i);
|
||||
|
||||
__m512d acc_x = _mm512_setzero_pd();
|
||||
__m512d acc_y = _mm512_setzero_pd();
|
||||
|
||||
for (int j = 0; j < n_p_pad; j += 8) {
|
||||
|
||||
// # 8 ADDs
|
||||
__m512d j_idx = _mm512_add_pd(_mm512_set1_pd((double)j), lane_off);
|
||||
|
||||
__mmask8 m_neq_i = _mm512_cmp_pd_mask(j_idx, v_i, _CMP_NEQ_OQ);
|
||||
__mmask8 m_in_range = _mm512_cmp_pd_mask(j_idx, v_np, _CMP_LT_OQ);
|
||||
__mmask8 active = m_neq_i & m_in_range;
|
||||
|
||||
__m512d pjx = _mm512_loadu_pd(&pos_x[j]);
|
||||
__m512d pjy = _mm512_loadu_pd(&pos_y[j]);
|
||||
__m512d sbe_jx = _mm512_loadu_pd(&sb_e_x[j]);
|
||||
__m512d sbe_jy = _mm512_loadu_pd(&sb_e_y[j]);
|
||||
__m512d sb_jsq = _mm512_loadu_pd(&sb_sq[j]);
|
||||
|
||||
// # 16 ADDs
|
||||
__m512d r_ab_x = _mm512_sub_pd(pix, pjx);
|
||||
__m512d r_ab_y = _mm512_sub_pd(piy, pjy);
|
||||
|
||||
// # 16 MULTs, 8 ADDs
|
||||
__m512d r_ab_n2 = _mm512_fmadd_pd(r_ab_x, r_ab_x, _mm512_mul_pd(r_ab_y, r_ab_y));
|
||||
|
||||
// # 8 SQRTs
|
||||
__m512d r_ab_n = _mm512_sqrt_pd(r_ab_n2);
|
||||
|
||||
// # 16 ADDs
|
||||
__m512d r_ab2_x = _mm512_sub_pd(r_ab_x, sbe_jx);
|
||||
__m512d r_ab2_y = _mm512_sub_pd(r_ab_y, sbe_jy);
|
||||
|
||||
// # 16 MULTs, 8 ADDs
|
||||
__m512d r_ab2_n2 = _mm512_fmadd_pd(r_ab2_x, r_ab2_x, _mm512_mul_pd(r_ab2_y, r_ab2_y));
|
||||
|
||||
// # 8 SQRTs
|
||||
__m512d r_ab2_n = _mm512_sqrt_pd(r_ab2_n2);
|
||||
|
||||
// # 8 ADDs
|
||||
__m512d r_sum = _mm512_add_pd(r_ab_n, r_ab2_n);
|
||||
|
||||
// # 8 MULTs, 8 ADDs
|
||||
__m512d two_b_sq = _mm512_fmsub_pd(r_sum, r_sum, sb_jsq);
|
||||
|
||||
// # 8 SQRTs
|
||||
__m512d two_b = _mm512_sqrt_pd(two_b_sq);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d e_arg = _mm512_mul_pd(two_b, v_neg_2s_inv);
|
||||
|
||||
// # 128 MULTs, 112 ADDs
|
||||
__m512d e_val = _avx512_exp_pd(e_arg);
|
||||
|
||||
// # 32 MULTs
|
||||
__m512d numer = _mm512_mul_pd(_mm512_mul_pd(r_sum, v_neg_v0_div_2s), e_val);
|
||||
__m512d denom = _mm512_mul_pd(_mm512_mul_pd(two_b, r_ab_n), r_ab2_n);
|
||||
|
||||
// # 8 DIVs
|
||||
__m512d smc = _mm512_div_pd(numer, denom);
|
||||
|
||||
// # 16 MULTs
|
||||
__m512d sm_r_ab = _mm512_mul_pd(smc, r_ab2_n);
|
||||
__m512d sm_r_ab2 = _mm512_mul_pd(smc, r_ab_n);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512d f_x = _mm512_fmadd_pd(r_ab2_x, sm_r_ab2, _mm512_mul_pd(r_ab_x, sm_r_ab));
|
||||
__m512d f_y = _mm512_fmadd_pd(r_ab2_y, sm_r_ab2, _mm512_mul_pd(r_ab_y, sm_r_ab));
|
||||
|
||||
// # 24 MULTs, 8 ADDs
|
||||
__m512d a = _mm512_mul_pd(_mm512_fmadd_pd(eix, f_x, _mm512_mul_pd(eiy, f_y)), v_cos_phi_inv);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d a_sq = _mm512_mul_pd(a, a);
|
||||
|
||||
// # 16 MULTs, 8 ADDs
|
||||
__m512d b_sq = _mm512_fmadd_pd(f_x, f_x, _mm512_mul_pd(f_y, f_y));
|
||||
|
||||
__mmask8 k_ge = _mm512_cmp_pd_mask(a, v_zero, _CMP_GE_OQ);
|
||||
__mmask8 k_gt = _mm512_cmp_pd_mask(a_sq, b_sq, _CMP_GT_OQ);
|
||||
__mmask8 k_cond = _mm512_kand(k_ge, k_gt);
|
||||
__m512d w_val = _mm512_mask_blend_pd(k_cond, v_dt_neg, v_c_neg_dt);
|
||||
|
||||
__m512d f_x_safe = _mm512_maskz_mov_pd(active, f_x);
|
||||
__m512d f_y_safe = _mm512_maskz_mov_pd(active, f_y);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
acc_x = _mm512_fmadd_pd(f_x_safe, w_val, acc_x);
|
||||
acc_y = _mm512_fmadd_pd(f_y_safe, w_val, acc_y);
|
||||
}
|
||||
|
||||
// # 16 ADDS
|
||||
f_alpha_dt_x += _mm512_reduce_add_pd(acc_x);
|
||||
f_alpha_dt_y += _mm512_reduce_add_pd(acc_y);
|
||||
|
||||
for (int k = 0; k < board.n_obstacles; k++) {
|
||||
|
||||
// # 2 ADDs
|
||||
double v1_x = pos_x_i - board.obstacles[k].start_x;
|
||||
double v1_y = pos_y_i - board.obstacles[k].start_y;
|
||||
|
||||
// # 3 MULTs, 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;
|
||||
|
||||
// # 4 ADDs, 2 MULTs
|
||||
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);
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
|
||||
|
||||
// # 2 MULTs, 1 EXP, 1 DIV
|
||||
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
|
||||
|
||||
// # 2 MULTs
|
||||
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 = dt_neg;
|
||||
double 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;
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
f_alpha_dt_x += f_alpha_B_neg_x * w_val;
|
||||
f_alpha_dt_y += f_alpha_B_neg_y * w_val;
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
w_x[i] += f_alpha_dt_x;
|
||||
w_y[i] += f_alpha_dt_y;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n_p_pad; i += 8) {
|
||||
|
||||
__m512d w_x_i_vec = _mm512_loadu_pd(&w_x[i]);
|
||||
__m512d w_y_i_vec = _mm512_loadu_pd(&w_y[i]);
|
||||
__m512d v_max_i_vec = _mm512_loadu_pd(&v_max[i]);
|
||||
__m512d pos_x_i_vec = _mm512_loadu_pd(&pos_x[i]);
|
||||
__m512d pos_y_i_vec = _mm512_loadu_pd(&pos_y[i]);
|
||||
__m512d destination_y_i_vec = _mm512_loadu_pd(&destination_y[i]);
|
||||
__m512d destination_x_i_vec = _mm512_loadu_pd(&destination_x[i]);
|
||||
__m512d desired_v0_i_vec = _mm512_loadu_pd(&desired_v0[i]);
|
||||
|
||||
__m256i color_i_vec = _mm256_loadu_si256((__m256i *)&color[i]);
|
||||
|
||||
// # 16 MULTs, 8 ADDs
|
||||
__m512d w_norm_sq = _mm512_fmadd_pd(w_x_i_vec, w_x_i_vec, _mm512_mul_pd(w_y_i_vec, w_y_i_vec));
|
||||
|
||||
// # 8 SQRTs
|
||||
__m512d w_norm = _mm512_sqrt_pd(w_norm_sq);
|
||||
|
||||
// # 8 DIVs
|
||||
__m512d correction_factor = _mm512_min_pd(_mm512_set1_pd(1.0), _mm512_div_pd(v_max_i_vec, w_norm));
|
||||
|
||||
// # 16 MULTs
|
||||
__m512d curr_vel_x_i_vec = _mm512_mul_pd(w_x_i_vec, correction_factor);
|
||||
__m512d curr_vel_y_i_vec = _mm512_mul_pd(w_y_i_vec, correction_factor);
|
||||
|
||||
__m512d dt_vec = _mm512_set1_pd(dt);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
pos_x_i_vec = _mm512_fmadd_pd(curr_vel_x_i_vec, dt_vec, pos_x_i_vec);
|
||||
pos_y_i_vec = _mm512_fmadd_pd(curr_vel_y_i_vec, dt_vec, pos_y_i_vec);
|
||||
|
||||
__mmask8 cond_1_left = _mm256_cmpeq_epi32_mask(color_i_vec, _mm256_setzero_si256());
|
||||
__mmask8 cond_1_right = _mm512_cmp_pd_mask(pos_x_i_vec, _mm512_set1_pd(BOARD_LENGTH_MINUS_ONE_POINT_FIVE), _CMP_GT_OQ);
|
||||
__mmask8 cond_1 = cond_1_left & cond_1_right;
|
||||
|
||||
pos_x_i_vec = _mm512_mask_blend_pd(cond_1, pos_x_i_vec, _mm512_set1_pd(1.0));
|
||||
destination_y_i_vec = _mm512_mask_blend_pd(cond_1, destination_y_i_vec, pos_y_i_vec);
|
||||
w_x_i_vec = _mm512_mask_blend_pd(cond_1, w_x_i_vec, desired_v0_i_vec);
|
||||
w_y_i_vec = _mm512_mask_blend_pd(cond_1, w_y_i_vec, _mm512_setzero_pd());
|
||||
curr_vel_x_i_vec = _mm512_mask_blend_pd(cond_1, curr_vel_x_i_vec, w_x_i_vec);
|
||||
curr_vel_y_i_vec = _mm512_mask_blend_pd(cond_1, curr_vel_y_i_vec, w_y_i_vec);
|
||||
|
||||
__mmask8 cond_2_left = _mm256_cmpeq_epi32_mask(color_i_vec, _mm256_set1_epi32(1));
|
||||
__mmask8 cond_2_right = _mm512_cmp_pd_mask(pos_x_i_vec, _mm512_set1_pd(1.5), _CMP_LT_OQ);
|
||||
__mmask8 cond_2 = cond_2_left & cond_2_right;
|
||||
|
||||
pos_x_i_vec = _mm512_mask_blend_pd(cond_2, pos_x_i_vec, _mm512_set1_pd(BOARD_LENGTH - 1.0));
|
||||
destination_y_i_vec = _mm512_mask_blend_pd(cond_2, destination_y_i_vec, pos_y_i_vec);
|
||||
__m512d neg_desired_v0 = _mm512_xor_pd(desired_v0_i_vec, _mm512_set1_pd(-0.0));
|
||||
w_x_i_vec = _mm512_mask_blend_pd(cond_2, w_x_i_vec, neg_desired_v0);
|
||||
w_y_i_vec = _mm512_mask_blend_pd(cond_2, w_y_i_vec, _mm512_setzero_pd());
|
||||
curr_vel_x_i_vec = _mm512_mask_blend_pd(cond_2, curr_vel_x_i_vec, w_x_i_vec);
|
||||
curr_vel_y_i_vec = _mm512_mask_blend_pd(cond_2, curr_vel_y_i_vec, w_y_i_vec);
|
||||
|
||||
pos_y_i_vec = _mm512_max_pd(pos_y_i_vec, _mm512_set1_pd(WALL_EPS));
|
||||
pos_y_i_vec = _mm512_min_pd(pos_y_i_vec, _mm512_set1_pd(BOARD_WIDTH_MINUS_WALL_EPS));
|
||||
|
||||
// # 16 ADDs
|
||||
__m512d ex_vec = _mm512_sub_pd(destination_x_i_vec, pos_x_i_vec);
|
||||
__m512d ey_vec = _mm512_sub_pd(destination_y_i_vec, pos_y_i_vec);
|
||||
|
||||
// # 16 MULTs, 8 ADDs
|
||||
__m512d e_norm_sq = _mm512_fmadd_pd(ex_vec, ex_vec, _mm512_mul_pd(ey_vec, ey_vec));
|
||||
|
||||
// # 8 SQRTs
|
||||
__m512d e_norm = _mm512_sqrt_pd(e_norm_sq);
|
||||
|
||||
// # 8 DIVs
|
||||
__m512d e_norm_inv = _mm512_div_pd(_mm512_set1_pd(1.0), e_norm);
|
||||
|
||||
// # 16 MULT
|
||||
__m512d e_x_i_vec = _mm512_mul_pd(ex_vec, e_norm_inv);
|
||||
__m512d e_y_i_vec = _mm512_mul_pd(ey_vec, e_norm_inv);
|
||||
|
||||
// # 16 MULTs, 8 ADDs
|
||||
__m512d vel_norm_sq = _mm512_fmadd_pd(curr_vel_x_i_vec, curr_vel_x_i_vec, _mm512_mul_pd(curr_vel_y_i_vec, curr_vel_y_i_vec));
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d sb_sq_i_vec = _mm512_mul_pd(vel_norm_sq, _mm512_set1_pd(delta_t_sq));
|
||||
|
||||
// # 8 SQRTs
|
||||
__m512d vel_norm_dt = _mm512_sqrt_pd(sb_sq_i_vec);
|
||||
|
||||
_mm512_storeu_pd(&curr_vel_x[i], curr_vel_x_i_vec);
|
||||
_mm512_storeu_pd(&curr_vel_y[i], curr_vel_y_i_vec);
|
||||
_mm512_storeu_pd(&w_x[i], w_x_i_vec);
|
||||
_mm512_storeu_pd(&w_y[i], w_y_i_vec);
|
||||
_mm512_storeu_pd(&pos_x[i], pos_x_i_vec);
|
||||
_mm512_storeu_pd(&pos_y[i], pos_y_i_vec);
|
||||
_mm512_storeu_pd(&e_x[i], e_x_i_vec);
|
||||
_mm512_storeu_pd(&e_y[i], e_y_i_vec);
|
||||
_mm512_storeu_pd(&destination_y[i], destination_y_i_vec);
|
||||
_mm512_storeu_pd(&sb_sq[i], sb_sq_i_vec);
|
||||
|
||||
// # 16 MULTs
|
||||
_mm512_storeu_pd(&sb_e_x[i], _mm512_mul_pd(e_x_i_vec, vel_norm_dt));
|
||||
_mm512_storeu_pd(&sb_e_y[i], _mm512_mul_pd(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;
|
||||
}
|
||||
@@ -0,0 +1,640 @@
|
||||
// # Changes: Added ILP with k = 2 to j-loop
|
||||
// # Predecessor: variant_inlining_8_SoA_AVX512_polyexp_moreavx.cpp
|
||||
|
||||
#include "../headers/board_reduced.hpp"
|
||||
#include "../headers/constants.hpp"
|
||||
#include "../headers/pedestrian_reduced.hpp"
|
||||
|
||||
#include <immintrin.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
|
||||
#define SEED 42
|
||||
#define BOARD_WIDTH 10.0
|
||||
#define BOARD_LENGTH 50.0
|
||||
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0
|
||||
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5
|
||||
#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;
|
||||
|
||||
alignas(64) vector<double> pos_x, pos_y;
|
||||
alignas(64) vector<double> sb_sq;
|
||||
alignas(64) vector<double> sb_e_x, sb_e_y;
|
||||
alignas(64) vector<double> e_x, e_y;
|
||||
alignas(64) vector<double> curr_vel_x, curr_vel_y;
|
||||
alignas(64) vector<double> w_x, w_y;
|
||||
alignas(64) vector<double> desired_v0;
|
||||
alignas(64) vector<double> v_max;
|
||||
alignas(64) vector<double> destination_x, destination_y;
|
||||
alignas(64) 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<double> velocity_gen(v0_mean, v0_std_dev);
|
||||
uniform_real_distribution<double> y_gen(0.3, BOARD_WIDTH - 0.3);
|
||||
uniform_real_distribution<double> x_gen_left(2.0, BOARD_LENGTH / 2.0);
|
||||
uniform_real_distribution<double> x_gen_right(BOARD_LENGTH / 2.0, BOARD_LENGTH - 2.0);
|
||||
|
||||
const int n_p_pad = (n_p + 15) & ~15;
|
||||
|
||||
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++) {
|
||||
double x = x_gen_left(gen);
|
||||
double y = y_gen(gen);
|
||||
|
||||
double dest_x = BOARD_LENGTH + 30.0;
|
||||
double dest_y = y;
|
||||
|
||||
double init_e_x = dest_x - x;
|
||||
double init_e_y = dest_y - 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 d_v0;
|
||||
do {
|
||||
d_v0 = velocity_gen(gen);
|
||||
} while (d_v0 <= 0.1);
|
||||
|
||||
double 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;
|
||||
|
||||
w_x[i] = 0.0;
|
||||
w_y[i] = 0.0;
|
||||
curr_vel_x[i] = 0.0;
|
||||
curr_vel_y[i] = 0.0;
|
||||
sb_sq[i] = 0.0;
|
||||
sb_e_x[i] = 0.0;
|
||||
sb_e_y[i] = 0.0;
|
||||
}
|
||||
|
||||
for (int i = n_p_half; i < n_p; i++) {
|
||||
double x = x_gen_right(gen);
|
||||
double y = y_gen(gen);
|
||||
|
||||
double dest_x = -30;
|
||||
double dest_y = y;
|
||||
|
||||
double init_e_x = dest_x - x;
|
||||
double init_e_y = dest_y - 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 d_v0;
|
||||
do {
|
||||
d_v0 = velocity_gen(gen);
|
||||
} while (d_v0 <= 0.1);
|
||||
|
||||
double 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.0;
|
||||
w_y[i] = 0.0;
|
||||
curr_vel_x[i] = 0.0;
|
||||
curr_vel_y[i] = 0.0;
|
||||
sb_sq[i] = 0.0;
|
||||
sb_e_x[i] = 0.0;
|
||||
sb_e_y[i] = 0.0;
|
||||
}
|
||||
|
||||
for (int j = n_p; j < n_p_pad; j++) {
|
||||
pos_x[j] = 1e9;
|
||||
pos_y[j] = 1e9;
|
||||
sb_sq[j] = 0.0;
|
||||
sb_e_x[j] = 0.0;
|
||||
sb_e_y[j] = 0.0;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Board init_board_without_obstacles() { return Board(BOARD_LENGTH, BOARD_WIDTH, 0, nullptr); }
|
||||
|
||||
// # 128 MULTs, 112 ADDs
|
||||
static inline __m512d _avx512_exp_pd(__m512d x) {
|
||||
|
||||
const __m512d log2e = _mm512_set1_pd(1.4426950408889634);
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d nf = _mm512_roundscale_pd(_mm512_mul_pd(x, log2e), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
|
||||
const __m512d ln2 = _mm512_set1_pd(0.6931471805599453);
|
||||
|
||||
// # 8 MULTs, 8 ADDs
|
||||
__m512d r = _mm512_fnmadd_pd(nf, ln2, x);
|
||||
|
||||
const __m512d c13 = _mm512_set1_pd(c13_scalar);
|
||||
const __m512d c12 = _mm512_set1_pd(c12_scalar);
|
||||
const __m512d c11 = _mm512_set1_pd(c11_scalar);
|
||||
const __m512d c10 = _mm512_set1_pd(c10_scalar);
|
||||
const __m512d c9 = _mm512_set1_pd(c9_scalar);
|
||||
const __m512d c8 = _mm512_set1_pd(c8_scalar);
|
||||
const __m512d c7 = _mm512_set1_pd(c7_scalar);
|
||||
const __m512d c6 = _mm512_set1_pd(c6_scalar);
|
||||
const __m512d c5 = _mm512_set1_pd(c5_scalar);
|
||||
const __m512d c4 = _mm512_set1_pd(c4_scalar);
|
||||
const __m512d c3 = _mm512_set1_pd(c3_scalar);
|
||||
const __m512d c2 = _mm512_set1_pd(c2_scalar);
|
||||
const __m512d c1 = _mm512_set1_pd(c1_scalar);
|
||||
const __m512d c0 = _mm512_set1_pd(c0_scalar);
|
||||
|
||||
__m512d p = c13;
|
||||
|
||||
// # 104 MULTs, 104 ADDs
|
||||
p = _mm512_fmadd_pd(p, r, c12);
|
||||
p = _mm512_fmadd_pd(p, r, c11);
|
||||
p = _mm512_fmadd_pd(p, r, c10);
|
||||
p = _mm512_fmadd_pd(p, r, c9);
|
||||
p = _mm512_fmadd_pd(p, r, c8);
|
||||
p = _mm512_fmadd_pd(p, r, c7);
|
||||
p = _mm512_fmadd_pd(p, r, c6);
|
||||
p = _mm512_fmadd_pd(p, r, c5);
|
||||
p = _mm512_fmadd_pd(p, r, c4);
|
||||
p = _mm512_fmadd_pd(p, r, c3);
|
||||
p = _mm512_fmadd_pd(p, r, c2);
|
||||
p = _mm512_fmadd_pd(p, r, c1);
|
||||
p = _mm512_fmadd_pd(p, r, c0);
|
||||
|
||||
__m256d nf_lo = _mm512_castpd512_pd256(nf);
|
||||
__m256d nf_hi = _mm512_extractf64x4_pd(nf, 1);
|
||||
|
||||
__m128i ni32_lo = _mm256_cvtpd_epi32(nf_lo);
|
||||
__m128i ni32_hi = _mm256_cvtpd_epi32(nf_hi);
|
||||
|
||||
__m256i ni64_lo = _mm256_cvtepi32_epi64(ni32_lo);
|
||||
__m256i ni64_hi = _mm256_cvtepi32_epi64(ni32_hi);
|
||||
|
||||
__m512i ni64 = _mm512_inserti64x4(_mm512_castsi256_si512(ni64_lo), ni64_hi, 1);
|
||||
|
||||
__m512i biased = _mm512_add_epi64(ni64, _mm512_set1_epi64(1023));
|
||||
|
||||
__m512i scaled = _mm512_slli_epi64(biased, 52);
|
||||
|
||||
__m512d pow2n = _mm512_castsi512_pd(scaled);
|
||||
|
||||
// # 8 MULTs
|
||||
return _mm512_mul_pd(p, pow2n);
|
||||
}
|
||||
|
||||
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<tuple<int, int, double, double, double, double, 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
|
||||
|
||||
const int n_p_pad = (n_p + 15) & ~15;
|
||||
|
||||
alignas(64) constexpr double lane_offset_arr[8] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0};
|
||||
const __m512d lane_off = _mm512_load_pd(lane_offset_arr);
|
||||
|
||||
const __m512d v_neg_v0_div_2s = _mm512_set1_pd(NEG_V0_AB_div_TWO_SIGMA);
|
||||
const __m512d v_neg_2s_inv = _mm512_set1_pd(NEG_TWO_SIGMA_INV);
|
||||
const __m512d v_cos_phi_sq = _mm512_set1_pd(cos_phi_sq);
|
||||
const __m512d v_dt_neg = _mm512_set1_pd(dt_neg);
|
||||
const __m512d v_c_neg_dt = _mm512_set1_pd(c_neg_times_dt);
|
||||
const __m512d v_zero = _mm512_setzero_pd();
|
||||
const __m512d v_one = _mm512_set1_pd(1.0f);
|
||||
const __m512d v_np = _mm512_set1_pd((float)n_p);
|
||||
const __m512d v_exp_min = _mm512_set1_pd(-87.0f);
|
||||
|
||||
// # ADDs: n_iter * n_p * 36 +
|
||||
// # n_iter * n_p * (n_p_pad / 16) * 464 +
|
||||
// # n_iter * n_p * board.n_obstacles * 10 +
|
||||
// # n_iter * (n_p_pad / 8) * 56
|
||||
|
||||
// # MULTs: n_iter * n_p * 4 +
|
||||
// # n_iter * n_p * (n_p_pad / 16) * 640 +
|
||||
// # n_iter * n_p * board.n_obstacles * 13 +
|
||||
// # n_iter * (n_p_pad / 8) * 120
|
||||
|
||||
// # DIVs: n_iter * n_p * (n_p_pad / 16) * 16 +
|
||||
// # n_iter * n_p * board.n_obstacles +
|
||||
// # n_iter * (n_p_pad / 8) * 16
|
||||
|
||||
// # SQRTs: n_iter * n_p * (n_p_pad / 16) * 48 +
|
||||
// # n_iter * n_p * board.n_obstacles +
|
||||
// # n_iter * (n_p_pad / 8) * 24
|
||||
|
||||
// # EXPs: n_iter * n_p * board.n_obstacles
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
|
||||
double pos_x_i = pos_x[i];
|
||||
double pos_y_i = pos_y[i];
|
||||
double e_x_i = e_x[i];
|
||||
double e_y_i = e_y[i];
|
||||
double desired_v0_i = desired_v0[i];
|
||||
double curr_vel_x_i = curr_vel_x[i];
|
||||
double curr_vel_y_i = curr_vel_y[i];
|
||||
|
||||
// # 4 MULTs, 2 ADDs
|
||||
double f_alpha_dt_x = (e_x_i * desired_v0_i - curr_vel_x_i) * dt_times_tau_alpha_inv;
|
||||
double f_alpha_dt_y = (e_y_i * desired_v0_i - curr_vel_y_i) * dt_times_tau_alpha_inv;
|
||||
|
||||
const __m512d pix = _mm512_set1_pd(pos_x_i);
|
||||
const __m512d piy = _mm512_set1_pd(pos_y_i);
|
||||
const __m512d eix = _mm512_set1_pd(e_x_i);
|
||||
const __m512d eiy = _mm512_set1_pd(e_y_i);
|
||||
const __m512d v_i = _mm512_set1_pd((double)i);
|
||||
|
||||
__m512d acc_x_0 = _mm512_setzero_pd();
|
||||
__m512d acc_y_0 = _mm512_setzero_pd();
|
||||
__m512d acc_x_1 = _mm512_setzero_pd();
|
||||
__m512d acc_y_1 = _mm512_setzero_pd();
|
||||
|
||||
for (int j = 0; j < n_p_pad; j += 16) {
|
||||
|
||||
// # 16 ADDs
|
||||
__m512d j_idx_0 = _mm512_add_pd(_mm512_set1_pd((double)j), lane_off);
|
||||
__m512d j_idx_1 = _mm512_add_pd(_mm512_set1_pd((double)j + 8), lane_off);
|
||||
|
||||
__mmask8 m_neq_i_0 = _mm512_cmp_pd_mask(j_idx_0, v_i, _CMP_NEQ_OQ);
|
||||
__mmask8 m_neq_i_1 = _mm512_cmp_pd_mask(j_idx_1, v_i, _CMP_NEQ_OQ);
|
||||
|
||||
__mmask8 m_in_range_0 = _mm512_cmp_pd_mask(j_idx_0, v_np, _CMP_LT_OQ);
|
||||
__mmask8 m_in_range_1 = _mm512_cmp_pd_mask(j_idx_1, v_np, _CMP_LT_OQ);
|
||||
|
||||
__mmask8 active_0 = m_neq_i_0 & m_in_range_0;
|
||||
__mmask8 active_1 = m_neq_i_1 & m_in_range_1;
|
||||
|
||||
__m512d pjx_0 = _mm512_loadu_pd(&pos_x[j]);
|
||||
__m512d pjx_1 = _mm512_loadu_pd(&pos_x[j + 8]);
|
||||
|
||||
__m512d pjy_0 = _mm512_loadu_pd(&pos_y[j]);
|
||||
__m512d pjy_1 = _mm512_loadu_pd(&pos_y[j + 8]);
|
||||
|
||||
__m512d sbe_jx_0 = _mm512_loadu_pd(&sb_e_x[j]);
|
||||
__m512d sbe_jx_1 = _mm512_loadu_pd(&sb_e_x[j + 8]);
|
||||
|
||||
__m512d sbe_jy_0 = _mm512_loadu_pd(&sb_e_y[j]);
|
||||
__m512d sbe_jy_1 = _mm512_loadu_pd(&sb_e_y[j + 8]);
|
||||
|
||||
__m512d sb_jsq_0 = _mm512_loadu_pd(&sb_sq[j]);
|
||||
__m512d sb_jsq_1 = _mm512_loadu_pd(&sb_sq[j + 8]);
|
||||
|
||||
// # 16 ADDs
|
||||
__m512d r_ab_x_0 = _mm512_sub_pd(pix, pjx_0);
|
||||
__m512d r_ab_x_1 = _mm512_sub_pd(pix, pjx_1);
|
||||
|
||||
// # 16 ADDs
|
||||
__m512d r_ab_y_0 = _mm512_sub_pd(piy, pjy_0);
|
||||
__m512d r_ab_y_1 = _mm512_sub_pd(piy, pjy_1);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512d r_ab_n2_0 = _mm512_fmadd_pd(r_ab_x_0, r_ab_x_0, _mm512_mul_pd(r_ab_y_0, r_ab_y_0));
|
||||
__m512d r_ab_n2_1 = _mm512_fmadd_pd(r_ab_x_1, r_ab_x_1, _mm512_mul_pd(r_ab_y_1, r_ab_y_1));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512d r_ab_n_0 = _mm512_sqrt_pd(r_ab_n2_0);
|
||||
__m512d r_ab_n_1 = _mm512_sqrt_pd(r_ab_n2_1);
|
||||
|
||||
// # 16 ADDs
|
||||
__m512d r_ab2_x_0 = _mm512_sub_pd(r_ab_x_0, sbe_jx_0);
|
||||
__m512d r_ab2_x_1 = _mm512_sub_pd(r_ab_x_1, sbe_jx_1);
|
||||
|
||||
// # 16 ADDs
|
||||
__m512d r_ab2_y_0 = _mm512_sub_pd(r_ab_y_0, sbe_jy_0);
|
||||
__m512d r_ab2_y_1 = _mm512_sub_pd(r_ab_y_1, sbe_jy_1);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512d r_ab2_n2_0 = _mm512_fmadd_pd(r_ab2_x_0, r_ab2_x_0, _mm512_mul_pd(r_ab2_y_0, r_ab2_y_0));
|
||||
__m512d r_ab2_n2_1 = _mm512_fmadd_pd(r_ab2_x_1, r_ab2_x_1, _mm512_mul_pd(r_ab2_y_1, r_ab2_y_1));
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512d r_ab2_n_0 = _mm512_sqrt_pd(r_ab2_n2_0);
|
||||
__m512d r_ab2_n_1 = _mm512_sqrt_pd(r_ab2_n2_1);
|
||||
|
||||
// # 16 ADDs
|
||||
__m512d r_sum_0 = _mm512_add_pd(r_ab_n_0, r_ab2_n_0);
|
||||
__m512d r_sum_1 = _mm512_add_pd(r_ab_n_1, r_ab2_n_1);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
__m512d two_b_sq_0 = _mm512_fmsub_pd(r_sum_0, r_sum_0, sb_jsq_0);
|
||||
__m512d two_b_sq_1 = _mm512_fmsub_pd(r_sum_1, r_sum_1, sb_jsq_1);
|
||||
|
||||
// # 16 SQRTs
|
||||
__m512d two_b_0 = _mm512_sqrt_pd(two_b_sq_0);
|
||||
__m512d two_b_1 = _mm512_sqrt_pd(two_b_sq_1);
|
||||
|
||||
// # 16 MULTs
|
||||
__m512d e_arg_0 = _mm512_mul_pd(two_b_0, v_neg_2s_inv);
|
||||
__m512d e_arg_1 = _mm512_mul_pd(two_b_1, v_neg_2s_inv);
|
||||
|
||||
e_arg_0 = _mm512_max_pd(e_arg_0, v_exp_min);
|
||||
e_arg_1 = _mm512_max_pd(e_arg_1, v_exp_min);
|
||||
|
||||
// # 256 MULTs, 224 ADDs
|
||||
__m512d e_val_0 = _avx512_exp_pd(e_arg_0);
|
||||
__m512d e_val_1 = _avx512_exp_pd(e_arg_1);
|
||||
|
||||
// # 32 MULTs
|
||||
__m512d numer_0 = _mm512_mul_pd(_mm512_mul_pd(r_sum_0, v_neg_v0_div_2s), e_val_0);
|
||||
__m512d numer_1 = _mm512_mul_pd(_mm512_mul_pd(r_sum_1, v_neg_v0_div_2s), e_val_1);
|
||||
|
||||
// # 32 MULTs
|
||||
__m512d denom_0 = _mm512_mul_pd(_mm512_mul_pd(two_b_0, r_ab_n_0), r_ab2_n_0);
|
||||
__m512d denom_1 = _mm512_mul_pd(_mm512_mul_pd(two_b_1, r_ab_n_1), r_ab2_n_1);
|
||||
|
||||
__m512d denom_safe_0 = _mm512_mask_blend_pd(active_0, v_one, denom_0);
|
||||
__m512d denom_safe_1 = _mm512_mask_blend_pd(active_1, v_one, denom_1);
|
||||
|
||||
// # 16 DIVs
|
||||
__m512d smc_0 = _mm512_div_pd(numer_0, denom_safe_0);
|
||||
__m512d smc_1 = _mm512_div_pd(numer_1, denom_safe_1);
|
||||
|
||||
// # 16 MULTs
|
||||
__m512d sm_r_ab_0 = _mm512_mul_pd(smc_0, r_ab2_n_0);
|
||||
__m512d sm_r_ab_1 = _mm512_mul_pd(smc_1, r_ab2_n_1);
|
||||
|
||||
// # 16 MULTs
|
||||
__m512d sm_r_ab2_0 = _mm512_mul_pd(smc_0, r_ab_n_0);
|
||||
__m512d sm_r_ab2_1 = _mm512_mul_pd(smc_1, r_ab_n_1);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512d f_x_0 = _mm512_fmadd_pd(r_ab2_x_0, sm_r_ab2_0, _mm512_mul_pd(r_ab_x_0, sm_r_ab_0));
|
||||
__m512d f_x_1 = _mm512_fmadd_pd(r_ab2_x_1, sm_r_ab2_1, _mm512_mul_pd(r_ab_x_1, sm_r_ab_1));
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512d f_y_0 = _mm512_fmadd_pd(r_ab2_y_0, sm_r_ab2_0, _mm512_mul_pd(r_ab_y_0, sm_r_ab_0));
|
||||
__m512d f_y_1 = _mm512_fmadd_pd(r_ab2_y_1, sm_r_ab2_1, _mm512_mul_pd(r_ab_y_1, sm_r_ab_1));
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512d f_norm_sq_0 = _mm512_fmadd_pd(f_x_0, f_x_0, _mm512_mul_pd(f_y_0, f_y_0));
|
||||
__m512d f_norm_sq_1 = _mm512_fmadd_pd(f_x_1, f_x_1, _mm512_mul_pd(f_y_1, f_y_1));
|
||||
|
||||
// # 16 MULTs
|
||||
__m512d rhs_0 = _mm512_mul_pd(f_norm_sq_0, v_cos_phi_sq);
|
||||
__m512d rhs_1 = _mm512_mul_pd(f_norm_sq_1, v_cos_phi_sq);
|
||||
|
||||
// # 32 MULTs, 16 ADDs
|
||||
__m512d e_dot_f_0 = _mm512_fmadd_pd(eix, f_x_0, _mm512_mul_pd(eiy, f_y_0));
|
||||
__m512d e_dot_f_1 = _mm512_fmadd_pd(eix, f_x_1, _mm512_mul_pd(eiy, f_y_1));
|
||||
|
||||
// # 16 MULTs
|
||||
__m512d e_dot_f_sq_0 = _mm512_mul_pd(e_dot_f_0, e_dot_f_0);
|
||||
__m512d e_dot_f_sq_1 = _mm512_mul_pd(e_dot_f_1, e_dot_f_1);
|
||||
|
||||
__mmask8 c1_0 = _mm512_cmp_pd_mask(e_dot_f_0, v_zero, _CMP_LT_OQ);
|
||||
__mmask8 c1_1 = _mm512_cmp_pd_mask(e_dot_f_1, v_zero, _CMP_LT_OQ);
|
||||
|
||||
__mmask8 c2_0 = _mm512_cmp_pd_mask(e_dot_f_sq_0, rhs_0, _CMP_GT_OQ);
|
||||
__mmask8 c2_1 = _mm512_cmp_pd_mask(e_dot_f_sq_1, rhs_1, _CMP_GT_OQ);
|
||||
|
||||
__mmask8 cond_0 = c1_0 & c2_0;
|
||||
__mmask8 cond_1 = c1_1 & c2_1;
|
||||
|
||||
__m512d w_val_0 = _mm512_mask_blend_pd(cond_0, v_dt_neg, v_c_neg_dt);
|
||||
__m512d w_val_1 = _mm512_mask_blend_pd(cond_1, v_dt_neg, v_c_neg_dt);
|
||||
|
||||
__m512d f_x_safe_0 = _mm512_maskz_mov_pd(active_0, f_x_0);
|
||||
__m512d f_x_safe_1 = _mm512_maskz_mov_pd(active_1, f_x_1);
|
||||
|
||||
__m512d f_y_safe_0 = _mm512_maskz_mov_pd(active_0, f_y_0);
|
||||
__m512d f_y_safe_1 = _mm512_maskz_mov_pd(active_1, f_y_1);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
acc_x_0 = _mm512_fmadd_pd(f_x_safe_0, w_val_0, acc_x_0);
|
||||
acc_x_1 = _mm512_fmadd_pd(f_x_safe_1, w_val_1, acc_x_1);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
acc_y_0 = _mm512_fmadd_pd(f_y_safe_0, w_val_0, acc_y_0);
|
||||
acc_y_1 = _mm512_fmadd_pd(f_y_safe_1, w_val_1, acc_y_1);
|
||||
}
|
||||
|
||||
// # 32 ADDs
|
||||
f_alpha_dt_x += _mm512_reduce_add_pd(_mm512_add_pd(acc_x_0, acc_x_1));
|
||||
f_alpha_dt_y += _mm512_reduce_add_pd(_mm512_add_pd(acc_y_0, acc_y_1));
|
||||
|
||||
for (int k = 0; k < board.n_obstacles; k++) {
|
||||
|
||||
// # 2 ADDs
|
||||
double v1_x = pos_x_i - board.obstacles[k].start_x;
|
||||
double v1_y = pos_y_i - board.obstacles[k].start_y;
|
||||
|
||||
// # 3 MULTs, 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;
|
||||
|
||||
// # 4 ADDs, 2 MULTs
|
||||
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);
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
|
||||
|
||||
// # 2 MULTs, 1 EXP, 1 DIV
|
||||
double scalar_mult = (NEG_U0_AB_div_R * exp(r_aB_norm * NEG_R_INV)) / r_aB_norm;
|
||||
|
||||
// # 2 MULTs
|
||||
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 = dt_neg;
|
||||
double 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;
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
f_alpha_dt_x += f_alpha_B_neg_x * w_val;
|
||||
f_alpha_dt_y += f_alpha_B_neg_y * w_val;
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
w_x[i] += f_alpha_dt_x;
|
||||
w_y[i] += f_alpha_dt_y;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n_p_pad; i += 8) {
|
||||
|
||||
__m512d w_x_i_vec = _mm512_loadu_pd(&w_x[i]);
|
||||
__m512d w_y_i_vec = _mm512_loadu_pd(&w_y[i]);
|
||||
__m512d v_max_i_vec = _mm512_loadu_pd(&v_max[i]);
|
||||
__m512d pos_x_i_vec = _mm512_loadu_pd(&pos_x[i]);
|
||||
__m512d pos_y_i_vec = _mm512_loadu_pd(&pos_y[i]);
|
||||
__m512d destination_y_i_vec = _mm512_loadu_pd(&destination_y[i]);
|
||||
__m512d destination_x_i_vec = _mm512_loadu_pd(&destination_x[i]);
|
||||
__m512d desired_v0_i_vec = _mm512_loadu_pd(&desired_v0[i]);
|
||||
|
||||
__m256i color_i_vec = _mm256_loadu_si256((__m256i *)&color[i]);
|
||||
|
||||
// # 16 MULTs, 8 ADDs
|
||||
__m512d w_norm_sq = _mm512_fmadd_pd(w_x_i_vec, w_x_i_vec, _mm512_mul_pd(w_y_i_vec, w_y_i_vec));
|
||||
|
||||
// # 8 SQRTs
|
||||
__m512d w_norm = _mm512_sqrt_pd(w_norm_sq);
|
||||
|
||||
// # 8 DIVs
|
||||
__m512d correction_factor = _mm512_min_pd(_mm512_set1_pd(1.0), _mm512_div_pd(v_max_i_vec, w_norm));
|
||||
|
||||
// # 16 MULTs
|
||||
__m512d curr_vel_x_i_vec = _mm512_mul_pd(w_x_i_vec, correction_factor);
|
||||
__m512d curr_vel_y_i_vec = _mm512_mul_pd(w_y_i_vec, correction_factor);
|
||||
|
||||
__m512d dt_vec = _mm512_set1_pd(dt);
|
||||
|
||||
// # 16 MULTs, 16 ADDs
|
||||
pos_x_i_vec = _mm512_fmadd_pd(curr_vel_x_i_vec, dt_vec, pos_x_i_vec);
|
||||
pos_y_i_vec = _mm512_fmadd_pd(curr_vel_y_i_vec, dt_vec, pos_y_i_vec);
|
||||
|
||||
__mmask8 cond_1_left = _mm256_cmpeq_epi32_mask(color_i_vec, _mm256_setzero_si256());
|
||||
__mmask8 cond_1_right = _mm512_cmp_pd_mask(pos_x_i_vec, _mm512_set1_pd(BOARD_LENGTH_MINUS_ONE_POINT_FIVE), _CMP_GT_OQ);
|
||||
__mmask8 cond_1 = cond_1_left & cond_1_right;
|
||||
|
||||
pos_x_i_vec = _mm512_mask_blend_pd(cond_1, pos_x_i_vec, _mm512_set1_pd(1.0));
|
||||
destination_y_i_vec = _mm512_mask_blend_pd(cond_1, destination_y_i_vec, pos_y_i_vec);
|
||||
w_x_i_vec = _mm512_mask_blend_pd(cond_1, w_x_i_vec, desired_v0_i_vec);
|
||||
w_y_i_vec = _mm512_mask_blend_pd(cond_1, w_y_i_vec, _mm512_setzero_pd());
|
||||
curr_vel_x_i_vec = _mm512_mask_blend_pd(cond_1, curr_vel_x_i_vec, w_x_i_vec);
|
||||
curr_vel_y_i_vec = _mm512_mask_blend_pd(cond_1, curr_vel_y_i_vec, w_y_i_vec);
|
||||
|
||||
__mmask8 cond_2_left = _mm256_cmpeq_epi32_mask(color_i_vec, _mm256_set1_epi32(1));
|
||||
__mmask8 cond_2_right = _mm512_cmp_pd_mask(pos_x_i_vec, _mm512_set1_pd(1.5), _CMP_LT_OQ);
|
||||
__mmask8 cond_2 = cond_2_left & cond_2_right;
|
||||
|
||||
pos_x_i_vec = _mm512_mask_blend_pd(cond_2, pos_x_i_vec, _mm512_set1_pd(BOARD_LENGTH - 1.0));
|
||||
destination_y_i_vec = _mm512_mask_blend_pd(cond_2, destination_y_i_vec, pos_y_i_vec);
|
||||
__m512d neg_desired_v0 = _mm512_xor_pd(desired_v0_i_vec, _mm512_set1_pd(-0.0));
|
||||
w_x_i_vec = _mm512_mask_blend_pd(cond_2, w_x_i_vec, neg_desired_v0);
|
||||
w_y_i_vec = _mm512_mask_blend_pd(cond_2, w_y_i_vec, _mm512_setzero_pd());
|
||||
curr_vel_x_i_vec = _mm512_mask_blend_pd(cond_2, curr_vel_x_i_vec, w_x_i_vec);
|
||||
curr_vel_y_i_vec = _mm512_mask_blend_pd(cond_2, curr_vel_y_i_vec, w_y_i_vec);
|
||||
|
||||
pos_y_i_vec = _mm512_max_pd(pos_y_i_vec, _mm512_set1_pd(WALL_EPS));
|
||||
pos_y_i_vec = _mm512_min_pd(pos_y_i_vec, _mm512_set1_pd(BOARD_WIDTH_MINUS_WALL_EPS));
|
||||
|
||||
// # 16 ADDs
|
||||
__m512d ex_vec = _mm512_sub_pd(destination_x_i_vec, pos_x_i_vec);
|
||||
__m512d ey_vec = _mm512_sub_pd(destination_y_i_vec, pos_y_i_vec);
|
||||
|
||||
// # 16 MULTs, 8 ADDs
|
||||
__m512d e_norm_sq = _mm512_fmadd_pd(ex_vec, ex_vec, _mm512_mul_pd(ey_vec, ey_vec));
|
||||
|
||||
// # 8 SQRTs
|
||||
__m512d e_norm = _mm512_sqrt_pd(e_norm_sq);
|
||||
|
||||
// # 8 DIVs
|
||||
__m512d e_norm_inv = _mm512_div_pd(_mm512_set1_pd(1.0), e_norm);
|
||||
|
||||
// # 16 MULT
|
||||
__m512d e_x_i_vec = _mm512_mul_pd(ex_vec, e_norm_inv);
|
||||
__m512d e_y_i_vec = _mm512_mul_pd(ey_vec, e_norm_inv);
|
||||
|
||||
// # 16 MULTs, 8 ADDs
|
||||
__m512d vel_norm_sq = _mm512_fmadd_pd(curr_vel_x_i_vec, curr_vel_x_i_vec, _mm512_mul_pd(curr_vel_y_i_vec, curr_vel_y_i_vec));
|
||||
|
||||
// # 8 MULTs
|
||||
__m512d sb_sq_i_vec = _mm512_mul_pd(vel_norm_sq, _mm512_set1_pd(delta_t_sq));
|
||||
|
||||
// # 8 SQRTs
|
||||
__m512d vel_norm_dt = _mm512_sqrt_pd(sb_sq_i_vec);
|
||||
|
||||
_mm512_storeu_pd(&curr_vel_x[i], curr_vel_x_i_vec);
|
||||
_mm512_storeu_pd(&curr_vel_y[i], curr_vel_y_i_vec);
|
||||
_mm512_storeu_pd(&w_x[i], w_x_i_vec);
|
||||
_mm512_storeu_pd(&w_y[i], w_y_i_vec);
|
||||
_mm512_storeu_pd(&pos_x[i], pos_x_i_vec);
|
||||
_mm512_storeu_pd(&pos_y[i], pos_y_i_vec);
|
||||
_mm512_storeu_pd(&e_x[i], e_x_i_vec);
|
||||
_mm512_storeu_pd(&e_y[i], e_y_i_vec);
|
||||
_mm512_storeu_pd(&destination_y[i], destination_y_i_vec);
|
||||
_mm512_storeu_pd(&sb_sq[i], sb_sq_i_vec);
|
||||
|
||||
// # 16 MULTs
|
||||
_mm512_storeu_pd(&sb_e_x[i], _mm512_mul_pd(e_x_i_vec, vel_norm_dt));
|
||||
_mm512_storeu_pd(&sb_e_y[i], _mm512_mul_pd(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;
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
// # This file performs in-lining for run_simulation.
|
||||
// # Changes: Replaced references to Vector2d structs inside each Obstacle of Board.obstacles with references to doubles
|
||||
// # Predecessor: variant_inlining_7.cpp
|
||||
|
||||
#include "../headers/board_reduced.hpp"
|
||||
#include "../headers/constants.hpp"
|
||||
#include "../headers/pedestrian_reduced.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.0
|
||||
#define BOARD_LENGTH 50.0
|
||||
#define BOARD_LENGTH_MINUS_ONE BOARD_LENGTH - 1.0
|
||||
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5
|
||||
#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<double> velocity_gen(v0_mean, v0_std_dev);
|
||||
uniform_real_distribution<double> y_gen(0.3, BOARD_WIDTH - 0.3);
|
||||
uniform_real_distribution<double> x_gen_left(2.0, BOARD_LENGTH / 2.0);
|
||||
uniform_real_distribution<double> 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<tuple<int, int, double, double, double, double, 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++) {
|
||||
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
|
||||
|
||||
// # ADDs: n_iter * n_p * 11 +
|
||||
// # n_iter * n_p * (n_p - 1) * 14 +
|
||||
// # n_iter * n_p * board.n_obstacles * 10
|
||||
|
||||
// # MULTs: n_iter * n_p * 21 +
|
||||
// # n_iter * n_p * (n_p - 1) * 24 +
|
||||
// # n_iter * n_p * board.n_obstacles * 13
|
||||
|
||||
// # DIVs: n_iter * n_p * 2 +
|
||||
// # n_iter * n_p * (n_p - 1) +
|
||||
// # n_iter * n_p * board.n_obstacles
|
||||
|
||||
// # SQRTs: n_iter * n_p * 3 +
|
||||
// # n_iter * n_p * (n_p - 1) * 3 +
|
||||
// # n_iter * n_p * board.n_obstacles
|
||||
|
||||
// # EXPs: n_iter * n_p * (n_p - 1) +
|
||||
// # n_iter * n_p * board.n_obstacles
|
||||
|
||||
for (int t = 0; t < n_iter; t++) {
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
Pedestrian &p = pedestrians[i];
|
||||
|
||||
// # 2 MULTs
|
||||
double desired_velocity_x = p.e_x * p.desired_v0;
|
||||
double desired_velocity_y = p.e_y * p.desired_v0;
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
double f_alpha_dt_x = (desired_velocity_x - p.curr_vel_x) * dt_times_tau_alpha_inv;
|
||||
double f_alpha_dt_y = (desired_velocity_y - p.curr_vel_y) * dt_times_tau_alpha_inv;
|
||||
|
||||
for (int j = 0; j < n_p; j++) {
|
||||
if (i == j)
|
||||
continue;
|
||||
|
||||
// # 2 ADDs
|
||||
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;
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double r_ab_norm = sqrt(r_ab_x * r_ab_x + r_ab_y * r_ab_y);
|
||||
|
||||
// # 2 ADDs
|
||||
double r_ab2_x = r_ab_x - pedestrians[j].s_b_e_x;
|
||||
double r_ab2_y = r_ab_y - pedestrians[j].s_b_e_y;
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double r_ab2_norm = sqrt(r_ab2_x * r_ab2_x + r_ab2_y * r_ab2_y);
|
||||
|
||||
// # 1ADD
|
||||
double r_ab_norm_sum = r_ab_norm + r_ab2_norm;
|
||||
|
||||
// # 1 MULT, 1 ADD
|
||||
double two_b_sq = r_ab_norm_sum * r_ab_norm_sum - pedestrians[j].s_b_sq;
|
||||
|
||||
// # 1 SQRT
|
||||
double two_b = sqrt(two_b_sq);
|
||||
|
||||
// # 5 MULTs, 1 EXP, 1 DIV
|
||||
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));
|
||||
|
||||
// # 2 MULTs
|
||||
double scalar_mult_r_ab = scalar_mult_common * r_ab2_norm;
|
||||
double scalar_mult_r_ab2 = scalar_mult_common * r_ab_norm;
|
||||
|
||||
// # 4 MULTs, 2 ADDs
|
||||
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;
|
||||
|
||||
double w_val;
|
||||
|
||||
// # 3 MULTs, 1 ADD
|
||||
double a = (p.e_x * f_alpha_beta_neg_x + p.e_y * f_alpha_beta_neg_y) * cos_phi_inv;
|
||||
|
||||
// # 2 MULTs, 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 MULT
|
||||
if (a >= 0 && (a * a > b_sq)) {
|
||||
w_val = c_neg_times_dt;
|
||||
}
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
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;
|
||||
}
|
||||
|
||||
for (int k = 0; k < board.n_obstacles; k++) {
|
||||
|
||||
// # 2 ADDs
|
||||
double v1_x = p.curr_pos_x - board.obstacles[k].start_x;
|
||||
double v1_y = p.curr_pos_y - board.obstacles[k].start_y;
|
||||
|
||||
// # 3 MULTs, 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;
|
||||
|
||||
// # 4 ADDs, 2 MULTs
|
||||
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);
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double r_aB_norm = sqrt(r_aB_x * r_aB_x + r_aB_y * r_aB_y);
|
||||
// # 4 MULTs, 1 EXP, 1 DIV
|
||||
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 e_dot_f = (f_alpha_B_neg_y == 0) ? 0 : (f_alpha_B_neg_y < 0 ? -p.e_y : p.e_y);
|
||||
if (e_dot_f >= cos_phi)
|
||||
w_val = dt_neg;
|
||||
else
|
||||
w_val = c_neg_times_dt;
|
||||
|
||||
// # 2 MULTs
|
||||
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;
|
||||
|
||||
// # 2 ADDs
|
||||
f_alpha_dt_x += f_alpha_B_dt_x;
|
||||
f_alpha_dt_y += f_alpha_B_dt_y;
|
||||
}
|
||||
|
||||
// # 2 ADDs
|
||||
p.w_x += f_alpha_dt_x;
|
||||
p.w_y += f_alpha_dt_y;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n_p; i++) {
|
||||
Pedestrian &p = pedestrians[i];
|
||||
|
||||
// # 1 SQRT, 2 MULTs, 1 ADD
|
||||
double w_norm = sqrt(p.w_x * p.w_x + p.w_y * p.w_y);
|
||||
|
||||
// # 1 DIV
|
||||
double correction_factor = min(1.0, p.v_max / w_norm);
|
||||
|
||||
// # 2 MULTs
|
||||
p.curr_vel_x = p.w_x * correction_factor;
|
||||
p.curr_vel_y = p.w_y * correction_factor;
|
||||
|
||||
// # 2 MULTs, 2 ADDs
|
||||
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_MINUS_ONE_POINT_FIVE) {
|
||||
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_MINUS_ONE;
|
||||
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);
|
||||
|
||||
// # 2 ADDs
|
||||
double e_x = p.destination_x - p.curr_pos_x;
|
||||
double e_y = p.destination_y - p.curr_pos_y;
|
||||
|
||||
// # 1 DIV, 1 SQRT, 2 MULTs, 1 ADD
|
||||
double e_norm_inv = 1.0 / sqrt(e_x * e_x + e_y * e_y);
|
||||
|
||||
// # 2 MULTs
|
||||
p.e_x = e_x * e_norm_inv;
|
||||
p.e_y = e_y * e_norm_inv;
|
||||
|
||||
// # 2 MULTs, 1 ADD
|
||||
double velocity_norm_sq = p.curr_vel_x * p.curr_vel_x + p.curr_vel_y * p.curr_vel_y;
|
||||
|
||||
// # 1 SQRT
|
||||
double velocity_norm = sqrt(velocity_norm_sq);
|
||||
|
||||
// # 5 MULTs
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user