AVX-512 First Attempt

This commit is contained in:
[Quy Anh] «Elliot» Nguyen
2026-05-25 00:59:19 +02:00
committed by Linus Baumberger
parent 174d167b9a
commit 7b3d554117
5 changed files with 467 additions and 13 deletions
+12
View File
@@ -0,0 +1,12 @@
CompileFlags:
Add:
[
'-O3',
'-march=native',
'-std=c++23',
'-Wal',
'-Wextra',
'-flto',
'-lsleef',
'-I/usr/include/x86_64-linux-gnu'
]
+16 -9
View File
@@ -47,6 +47,13 @@ CXXFLAGS := -O3 -march=native -std=c++23 -Wall -Wextra -flto
VARIANT ?= baseline
# sleef is only needed for the avx512 variant (used by SLEEF intrinsics there)
ifeq ($(ARCH),x86_64)
ifneq ($(filter avx512_%,$(VARIANT)),)
CXXFLAGS += -lsleef
endif
endif
VARIANT_SRC := ./variants/variant_$(VARIANT).cpp
ifneq ($(filter float_%,$(VARIANT)),)
@@ -95,7 +102,7 @@ ROOFLINE ?= 0
CXXFLAGS += -DROOFLINE=$(ROOFLINE)
# Benchmark suite parameters.
BENCH_SIZES ?= 50 100 200 500 1000 2000 5000 10000
BENCH_SIZES ?= 50 100 200 500 1000
BENCH_ITER ?= 1000
# Verification parameters
@@ -116,7 +123,7 @@ all: main
# make main : compile the main executable with benchmarking disabled.
main: clean
$(CXX) $(CXXFLAGS) $(SRC) -o $(TARGET)
$(CXX) $(SRC) -o $(TARGET) $(CXXFLAGS)
# make run : compile the code and run the simulation without generating a video.
run: main
@@ -137,7 +144,7 @@ verify: main
# make bench : compile the main executable with benchmarking enabled.
bench: CXXFLAGS += -DBENCHMARK=1
bench: clean
$(SUDO_BENCH) $(CXX) $(CXXFLAGS) $(SRC) -o $(TARGET)
$(SUDO_BENCH) $(CXX) $(SRC) -o $(TARGET) $(CXXFLAGS)
# make run-bench : compile and run benchmark measurements.
run-bench: bench
@@ -160,7 +167,7 @@ video: run
# make test : compile and run all unit tests.
test: clean
$(CXX) $(CXXFLAGS) $(TEST_SRC) $(TEST_LIBS) -o $(TEST_TARGET)
$(CXX) $(TEST_SRC) $(TEST_LIBS) -o $(TEST_TARGET) $(CXXFLAGS)
./$(TEST_TARGET)
# make asm : compile and generate assembly files.
@@ -172,19 +179,19 @@ ASM_SRCS := $(ASM_CPP_SRCS) $(ASM_HPP_SRCS)
ASM_TARGETS := $(patsubst %.cpp,%.s,$(patsubst %.hpp,%.s,$(ASM_SRCS)))
asm: clean
$(foreach src,$(ASM_CPP_SRCS),$(CXX) -O3 -march=native -std=c++23 -Wall -Wextra -DRANDOM_SEED=$(RANDOM_SEED) -S $(src) -o $(src:.cpp=.s); )
$(foreach src,$(ASM_HPP_SRCS),$(CXX) -O3 -march=native -std=c++23 -Wall -Wextra -DRANDOM_SEED=$(RANDOM_SEED) -x c++ -S $(src) -o $(src:.hpp=.s); )
$(foreach src,$(ASM_CPP_SRCS),$(CXX) -S $(src) -o $(src:.cpp=.s) $(CXXFLAGS); )
$(foreach src,$(ASM_HPP_SRCS),$(CXX) -x c++ -S $(src) -o $(src:.hpp=.s) $(CXXFLAGS); )
%.s: %.cpp $(HEADERS)
$(CXX) -O3 -march=native -std=c++23 -Wall -Wextra -DRANDOM_SEED=$(RANDOM_SEED) -S $< -o $@
$(CXX) -S $< -o $@ $(CXXFLAGS)
# make clean : delete generated files from previous builds.
clean:
rm -f main_* $(TEST_TARGET) *.o *.s
rm -f main_* $(SIZES_TARGET) $(TEST_TARGET) *.o *.s variants/*.s variants/*.o headers/*.s headers/*.o
# make clean-all : delete all generated files from previous builds and runs.
# WARNING: this deletes all *.csv and *.mp4 files in this directory.
# Move any output files you want to keep to another directory before running it.
clean-all: clean
rm -f *.csv *.mp4
rm -f *.csv *.mp4
-3
View File
@@ -1,6 +1,3 @@
// # Replaced Vector2d with double
// # Predecessor: board.hpp
#pragma once
struct Obstacle {
+16 -1
View File
@@ -2,6 +2,10 @@
#include <cmath>
#include <new>
#ifdef __x86_64__
#include "immintrin.h"
#endif
//
// $\frac{1}{\tau_{\alpha}}$; $\tau_{\alpha} = 0.5s$
//
@@ -96,10 +100,21 @@ const double EPS = 1e-9;
// Squared numerical tolerance
const double EPS_SQ = EPS * EPS;
#ifdef __AVX512F__
const __m512d LN2_VEC = _mm512_set1_pd(log(2));
const __m512d ZERO_VEC = _mm512_setzero_pd();
const __m512d NEG_TWO_SIGMA_INV_VEC = _mm512_set1_pd(NEG_TWO_SIGMA_INV);
const __m512d NEG_V0_AB_DIV_TWO_SIGMA_VEC = _mm512_set1_pd(NEG_V0_AB_div_TWO_SIGMA);
const __m512d c_neg_times_dt_vec = _mm512_set1_pd(c_neg_times_dt);
const __m512d cos_phi_inv_vec = _mm512_set1_pd(cos_phi_inv);
const __m512d dt_neg_vec = _mm512_set1_pd(dt_neg);
#endif
// Cutoff for pedestrian-pedestrian repulsion
const double CUTOFF = 12.0;
const double CUTOFF_SQ = CUTOFF * CUTOFF;
const double cos_phi_sq = cos_phi * cos_phi;
const size_t CACHE_BLOCK_SIZE = std::hardware_destructive_interference_size;
const size_t CACHE_BLOCK_SIZE = std::hardware_destructive_interference_size;
+423
View File
@@ -0,0 +1,423 @@
// # 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 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
for (int t = 0; t < n_iter; t++) {
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
double desired_velocity_x = p.e_x * p.desired_v0;
double desired_velocity_y = p.e_y * p.desired_v0;
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;
}
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;
double r_ab_norm = sqrt(r_ab_x * r_ab_x + r_ab_y * r_ab_y);
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;
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 - pedestrians[jj].s_b_sq;
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 = (p.e_x * f_alpha_beta_neg_x + p.e_y * 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;
}
} 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);
__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);
__m512d r_ab_y_mul_r_ab_y_vec = _mm512_mul_pd(r_ab_y_vec, r_ab_y_vec);
__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);
__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);
__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);
__m512d r_ab2_y_vec = _mm512_sub_pd(r_ab_y_vec, beta_s_b_e_y_vec);
__m512d r_ab2_y_mul_r_ab2_y_vec = _mm512_mul_pd(r_ab2_y_vec, r_ab2_y_vec);
__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);
__m512d r_ab2_norm_vec = _mm512_sqrt_pd(r_ab2_norm_sq_vec);
__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);
__m512d two_b_sq_vec = _mm512_fmsub_pd(r_ab_norm_sum_vec, r_ab_norm_sum_vec, beta_s_b_sq_vec);
__m512d two_b_vec = _mm512_sqrt_pd(two_b_sq_vec);
__m512d two_b_mul_r_ab_norm_vec = _mm512_mul_pd(two_b_vec, r_ab_norm_vec);
__m512d denom_vec = _mm512_mul_pd(two_b_mul_r_ab_norm_vec, r_ab2_norm_vec);
__m512d scalar_mult_common_left_vec = _mm512_div_pd(r_ab_norm_sum_vec, denom_vec);
__m512d two_b_mul_NEG_TWO_SIGMA_INV_vec = _mm512_mul_pd(two_b_vec, NEG_TWO_SIGMA_INV_VEC);
__m512d e_vec = Sleef_expd8_u10(two_b_mul_NEG_TWO_SIGMA_INV_vec);
__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);
__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);
__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);
__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);
__m512d rgt_vec = _mm512_mul_pd(alpha_e_y_vec, f_alpha_beta_neg_y_vec);
__m512d lft_add_rgt_vec = _mm512_fmadd_pd(alpha_e_x_vec, f_alpha_beta_neg_x_vec, rgt_vec);
__m512d a_vec = _mm512_mul_pd(lft_add_rgt_vec, cos_phi_inv_vec);
__m512d f_alpha_beta_neg_y_sq_vec = _mm512_mul_pd(f_alpha_beta_neg_y_vec, f_alpha_beta_neg_y_vec);
__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);
__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);
__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);
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;
}
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;
double r_ab_norm = sqrt(r_ab_x * r_ab_x + r_ab_y * r_ab_y);
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;
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 - pedestrians[j].s_b_sq;
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 = (p.e_x * f_alpha_beta_neg_x + p.e_y * 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 = p.curr_pos_x - board.obstacles[k].start_x;
double v1_y = p.curr_pos_y - 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 = 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);
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 = (p.e_x * f_alpha_B_neg_x + p.e_y * 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;
}
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];
double w_norm = sqrt(p.w_x * p.w_x + p.w_y * p.w_y);
double correction_factor = min(1.0, p.v_max / w_norm);
p.curr_vel_x = p.w_x * correction_factor;
p.curr_vel_y = p.w_y * correction_factor;
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);
double e_x = p.destination_x - p.curr_pos_x;
double e_y = p.destination_y - p.curr_pos_y;
double e_norm_inv = 1.0 / sqrt(e_x * e_x + e_y * e_y);
p.e_x = e_x * e_norm_inv;
p.e_y = e_y * e_norm_inv;
double velocity_norm_sq = p.curr_vel_x * p.curr_vel_x + p.curr_vel_y * p.curr_vel_y;
double velocity_norm = sqrt(velocity_norm_sq);
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