Merge branch 'ahmed' into 'main'

This commit is contained in:
Nguyen Quy Anh
2026-05-08 11:04:20 +02:00
parent f74fbe46d7
commit ae1c64af05
10 changed files with 1194 additions and 157 deletions
+70 -37
View File
@@ -1,8 +1,8 @@
#include "board.hpp"
#include "constants.hpp"
#include "forces.hpp"
#include "math.h"
#include "pedestrian.hpp"
#include "tsc_x86.h"
#include "vector2d.hpp"
#include <fstream>
#include <iostream>
@@ -10,6 +10,14 @@
#include <tuple>
#include <vector>
#ifdef __x86_64__
#include "tsc_x86.h"
#endif
#ifdef __aarch64__
#include "kperf.h"
#endif
#define SEED 42
#define BOARD_WIDTH 10.0
@@ -36,12 +44,12 @@ Pedestrian *init_pedestrians(int n_p) {
mt19937 gen(SEED); // Use fixed seed for reproducibility
normal_distribution<double> velocity_gen(v0_mean, v0_std_dev);
normal_distribution<double> velocity_gen(V0_MEAN, V0_STD_DEV);
uniform_real_distribution<double> x_gen_left(2, BOARD_LENGTH_HALF);
uniform_real_distribution<double> x_gen_right(BOARD_LENGTH_HALF, BOARD_LENGTH_MINUS_TWO);
uniform_real_distribution<double> y_gen(0.3, BOARD_WIDTH_MINUS_ZERO_POINT_THREE);
int n_p_half = n_p / 2;
const int n_p_half = n_p / 2;
for (int i = 0; i < n_p_half; i++) {
@@ -64,7 +72,7 @@ Pedestrian *init_pedestrians(int n_p) {
double v_max = 1.3 * desired_v0;
P[i] = Pedestrian(WHITE, init_e_unit * desired_v0, start_pos, init_e_unit, v_max, desired_v0, destination, Vector2d());
P[i] = Pedestrian(WHITE, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination, Vector2d());
}
for (int i = n_p_half; i < n_p; i++) {
@@ -86,7 +94,7 @@ Pedestrian *init_pedestrians(int n_p) {
double v_max = 1.3 * desired_v0;
P[i] = Pedestrian(BLACK, init_e_unit * desired_v0, start_pos, init_e_unit, v_max, desired_v0, destination, Vector2d(0, 0));
P[i] = Pedestrian(BLACK, Vector2d(), start_pos, init_e_unit, v_max, desired_v0, destination, Vector2d());
}
return P;
@@ -97,7 +105,7 @@ int main(int argc, const char *argv[]) {
// Ensure that arguments are passed
if (argc < 3) {
printf("Usage: ./sim <number_of_pedestrians> <number_of_iterations>\n");
printf("Each iteration takes %.2f seconds in the simulation.\n", dt);
printf("Each iteration takes %.2f seconds in the simulation.\n", DT);
return 1;
}
@@ -129,7 +137,15 @@ int main(int argc, const char *argv[]) {
Pedestrian *pedestrians = init_pedestrians(n_p);
#ifdef __x86_64__
uint64_t start = start_tsc();
#endif
#ifdef __aarch64__
kperf_init();
struct performance_counters startperf, endperf;
startperf = kperf_get_counters();
#endif
for (int t = 0; t < n_iter; t++) {
@@ -140,19 +156,19 @@ int main(int argc, const char *argv[]) {
p.f_i.reset();
// # 6 ADDs, 2 DIVs
// # 4 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)$
//
add_acceleration_term(&p);
// # 15 ADDs, 24 MULTs, 8 DIVs, 5 SQRTs, 1 EXP
// # (n_p - 1) * (14 ADDs, 23 MULTs, 1 DIV, 4 SQRTs, 1 EXP)
//
// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta})$
//
add_repulsive_effects_from_pedestrians(pedestrians, &p, n_p);
// # board.n_obstacles * (13 ADDs, 19 MULTs, 4 DIVs, 2 SQRTs, 1 EXP)
// # board.n_obstacles * (12 ADDs, 18 MULTs, 1 DIV, 2 SQRTs, 1 EXP)
//
// $\sum_{B} \vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$
//
@@ -164,30 +180,31 @@ int main(int argc, const char *argv[]) {
Pedestrian &p = pedestrians[i];
// # 2 ADDs, 2 MULTs
// # 2 ADDs
//
// $\frac{d\vec{w_\alpha}}{dt} = \vec{F}_{\alpha}(t)$
//
//
p.w = p.w + p.f_i * dt;
p.w = p.w + p.f_i;
// # 1 ADD, 2 MULTs, 1 SQRT
double w_norm = p.w.norm();
// # Assumption for calculating W(n): The "if" statement always evaluates to true.
if (w_norm > p.v_max) {
// # 2 MULTs, 1 DIV
p.w = p.w * (p.v_max / w_norm);
}
p.curr_vel = p.w;
// # 1 ADD, 4 MULTs, 1 DIV, 1 SQRT
p.curr_vel = p.w * g(p.v_max, p.w.norm());
// # Assumption for calculating W(n): The "if" statement always evaluates to true.
// # This is not true in practice, but we assume the worst-case scenario for the flop count calculation.
if (w_norm > p.v_max) {
// # 2 MULTs, 1 DIV
p.curr_vel = p.curr_vel * (p.v_max / w_norm);
}
// # 2 ADDs, 2 MULTs
//
// $\vec{r}_i^{t+1} = \vec{r}^t_i + v^t_idt$
//
p.curr_pos = p.curr_pos + p.curr_vel * dt;
p.curr_pos = p.curr_pos + p.curr_vel * DT;
// When a pedestrian reaches the end of the hallway we simply "teleport" the person back to the beginning.
// This way, we have a constant flow of pedestrians, and it is easier to see lanes forming.
@@ -208,23 +225,14 @@ int main(int argc, const char *argv[]) {
p.curr_vel = p.w;
}
// # 2 ADDs
Vector2d e = p.destination - p.curr_pos;
// # 1 ADD, 2 MULTs, 1 SQRT
double e_norm = e.norm();
// # 2 DIVs
p.e = e / e_norm;
if (p.curr_pos.y < 0.0) {
if (p.curr_pos.y <= 0.0) {
p.curr_pos.y = WALL_EPS;
if (p.curr_vel.y < 0)
p.curr_vel.y = 0;
if (p.w.y < 0)
p.w.y = 0;
}
if (p.curr_pos.y > BOARD_WIDTH) {
if (p.curr_pos.y >= BOARD_WIDTH) {
p.curr_pos.y = BOARD_WIDTH_MINUS_WALL_EPS;
if (p.curr_vel.y > 0)
p.curr_vel.y = 0;
@@ -232,6 +240,24 @@ int main(int argc, const char *argv[]) {
p.w.y = 0;
}
// # 2 ADDs
Vector2d e = p.destination - p.curr_pos;
// # 1 ADD, 2 MULTs, 1 DIV, 1 SQRT
double e_norm_inv = 1.0 / e.norm();
// # 2 MULTs
p.e = e * e_norm_inv;
// # 1 ADD, 3 MULTs, 1 SQRT
double s_b = p.curr_vel.norm() * DELTA_T;
// # 2 MULTs
p.s_b_e_b = p.e * s_b;
// # 1 MULT
p.s_b_sq = s_b * s_b;
// Save the data for visualization
if (dump_path && (t % dump_interval == 0)) {
samples.emplace_back(t, i, p.curr_pos.x, p.curr_pos.y, p.e.x, p.e.y, p.color);
@@ -239,22 +265,29 @@ int main(int argc, const char *argv[]) {
}
}
#ifdef __x86_64__
long double T = stop_tsc(start);
#endif
long long add_count_per_iter = n_p * (21 + board.n_obstacles * 13) + n_p * 9;
long long mult_count_per_iter = n_p * (24 + board.n_obstacles * 19) + n_p * 14;
long long div_count_per_iter = n_p * (10 + board.n_obstacles * 4) + n_p * 4;
long long sqrt_count_per_iter = n_p * (5 + board.n_obstacles * 2) + n_p * 3;
long long exp_count_per_iter = n_p * (1 + board.n_obstacles * 1);
#ifdef __aarch64__
endperf = kperf_get_counters();
long double T = endperf.cycles - startperf.cycles;
#endif
long double W_per_iter = add_count_per_iter * cost_add + mult_count_per_iter * cost_mult + div_count_per_iter * cost_div + sqrt_count_per_iter * cost_sqrt + exp_count_per_iter * cost_exp;
long long add_count_per_iter = n_p * (4 + 14 * (n_p - 1) + 12 * board.n_obstacles) + n_p * 9;
long long mult_count_per_iter = n_p * (4 + 23 * (n_p - 1) + 18 * board.n_obstacles) + n_p * 16;
long long div_count_per_iter = n_p * (n_p - 1 + board.n_obstacles) + n_p * 2;
long long sqrt_count_per_iter = n_p * (4 * (n_p - 1) + 2 * board.n_obstacles) + n_p * 3;
long long exp_count_per_iter = n_p * (n_p - 1 + board.n_obstacles);
long double W_per_iter = add_count_per_iter * COST_ADD + mult_count_per_iter * COST_MULT + div_count_per_iter * COST_DIV + sqrt_count_per_iter * COST_SQRT + exp_count_per_iter * COST_EXP;
long long Q_per_iter = sizeof(Pedestrian) * n_p + sizeof(Obstacle) * board.n_obstacles; // TODO: Refine this
long double I = W_per_iter / Q_per_iter;
long double W = W_per_iter * n_iter;
long double P = W / T;
printf("W(n) per iteration: %.2Lf flops\n", W_per_iter);
printf("Q(n) per iteration: %Ld bytes\n", Q_per_iter);
printf("Q(n) per iteration: %lld bytes\n", Q_per_iter);
printf("I(n): %.2Lf flops/byte\n", I);
printf("W(n): %.2Lf flops\n", W);
printf("T(n): %.2Lf cycles\n", T);