From d347534ff28c9c12e5fc1f657a0c610c4e2bce49 Mon Sep 17 00:00:00 2001 From: Nguyen Quy Anh <23906-nguyenquy@users.noreply.gitlab.inf.ethz.ch> Date: Fri, 27 Mar 2026 17:20:55 +0100 Subject: [PATCH 1/4] Baseline --- .clang-format | 1 + .clangd | 2 + board.hpp | 9 ++++ forces.cpp | 120 +++++++++++++++++++++++++++++++++++++++++ forces.hpp | 50 +++++++++++++++++ main.cpp | 144 ++++++++++++++++++++++--------------------------- pedestrian.hpp | 85 +++++++++++++++++++++++++++++ tsc_x86.h | 94 ++++++++++++++++++++++++++++++++ vector2d.hpp | 24 +++++++++ 9 files changed, 450 insertions(+), 79 deletions(-) create mode 100644 .clang-format create mode 100644 .clangd create mode 100644 board.hpp create mode 100644 forces.cpp create mode 100644 forces.hpp create mode 100644 pedestrian.hpp create mode 100644 tsc_x86.h create mode 100644 vector2d.hpp diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..5b02c5e --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +ColumnLimit: 1000 diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..edfd275 --- /dev/null +++ b/.clangd @@ -0,0 +1,2 @@ +CompileFlags: + Add: [-std=c++23] \ No newline at end of file diff --git a/board.hpp b/board.hpp new file mode 100644 index 0000000..6ee78b4 --- /dev/null +++ b/board.hpp @@ -0,0 +1,9 @@ +#pragma once +#include "vector2d.hpp" + +struct Board { + double length, width; + Vector2d *borders; // array containing the position of each obstacle + int n_borders; // number of obstacles + Board(double l, double w, int n, Vector2d *border) : length(l), width(w), n_borders(n), borders(border) {} +}; diff --git a/forces.cpp b/forces.cpp new file mode 100644 index 0000000..b48d0d0 --- /dev/null +++ b/forces.cpp @@ -0,0 +1,120 @@ +#include "forces.hpp" +#include + +// # Done +double w(Vector2d e, Vector2d f) { + if (e.dot(f) >= f.norm() * cos_phi) + return 1.0; + return c; +} + +// # Done +// +// $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) \vec{e}_\alpha(t) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$ +// +void compute_acceleration_term(Pedestrian *p) { + + // $v_{\alpha}^{0}\vec{e}_{\alpha}$ + Vector2d desired_velocity = p->e * p->desired_v0; + + // $\vec{v}_{\alpha}(t)$ + Vector2d actual_velocity = p->curr_vel; + + double tau_alpha_inv = 1 / tau_alpha; + + p->F_i += (desired_velocity - actual_velocity) * tau_alpha_inv; +} + +// # Done +// +// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta})$ +// + +void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestrian *p, int N) { + + for (int i = 0; i < N; i++) { + + if (pedestrians + i == p) + continue; + + // # Optional: Do not consider far away pedestrians from current pedestrian + // # Might get better speed up, remove if not + // if ((pedestrians[i].curr_pos - p->curr_pos).norm() > DISTANCE) { + // continue; + // } + + // $\vec{r}_{\alpha\beta}$ + Vector2d r_ab = p->curr_pos - pedestrians[i].curr_pos; + + // $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert$ + double r_ab_norm = r_ab.norm(); + + // $\vec{e}_{\beta}$ + Vector2d e_beta = pedestrians[i].e; + + // $v_\beta \Delta t$ + double v_beta_delta_t = pedestrians[i].desired_v0 * delta_t; + + // $\vec{r}_{\alpha\beta} - \vec{v}_{\beta}\varDelta_{t}\vec{e}_{\beta}$ + Vector2d r_ab_minus_v_beta_delta_t_e_beta = r_ab - (e_beta * v_beta_delta_t); + + // $\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$ + double r_ab_minus_v_beta_delta_t_e_beta_norm = r_ab_minus_v_beta_delta_t_e_beta.norm(); + + // (4): + // + // $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 b = sqrt((r_ab_norm + r_ab_minus_v_beta_delta_t_e_beta_norm) * (r_ab_norm + r_ab_minus_v_beta_delta_t_e_beta_norm) - (v_beta_delta_t) * (v_beta_delta_t)) / 2; + + // (7): + // + // $\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})}{4b}$ + // + // + Vector2d delta_r_alpha_beta_b = (((r_ab / r_ab_norm) + (r_ab_minus_v_beta_delta_t_e_beta / r_ab_minus_v_beta_delta_t_e_beta_norm)) * (r_ab_norm + r_ab_minus_v_beta_delta_t_e_beta_norm)) / (4 * b); + + // (5): + // + // $\vec{f}_{\alpha \beta}(\vec{r}_{\alpha \beta}) = \frac{V^0_{\alpha \beta}}{\sigma}e^{-b / \sigma} \grad_{\vec{r}_{\alpha \beta}}b$ + // + // + Vector2d f_alpha_beta_r_alpha_beta = delta_r_alpha_beta_b * ((V0_AB / SIGMA) * (exp(-b / SIGMA))); + + // (11): + // + // $\vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta}) = w(\vec{e}_{\alpha}, -\vec{f}_{\alpha\beta})\vec{f}_{\alpha\beta}(\vec{r}_{\alpha} - \vec{r}_{\beta})$ + // + // Add repulsive effects from other pedestrians to current pedestrian's social force + p->F_i += f_alpha_beta_r_alpha_beta * w(p->e, f_alpha_beta_r_alpha_beta * -1); + } +} + +// # Done +void compute_repulsive_effects_from_borders(Board &B, Pedestrian *p) { + + Vector2d *borders = B.borders; + int n_borders = B.n_borders; + + for (int i = 0; i < n_borders; i++) { + + Vector2d border = borders[i]; + + // $\vec{r}_{\alpha B}$ + Vector2d r_alpha_B = p->curr_pos - border; + + // + // $\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 = (r_alpha_B / r_alpha_B.norm()) * (U0_AB / R) * std::exp(-r_alpha_B.norm() / R); + + // (12): + // + // $\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{ B}) = w(\vec{e}_{\alpha}, -\vec{F}_{\alpha B})\vec{F}_{\alpha B}(\vec{r}_{\alpha} - \vec{r}_{ B})$ + // + + p->F_i += F_alpha_B * w(p->e, F_alpha_B * (-1)); + } +} diff --git a/forces.hpp b/forces.hpp new file mode 100644 index 0000000..27cc042 --- /dev/null +++ b/forces.hpp @@ -0,0 +1,50 @@ +#pragma once +#include "board.hpp" +#include "pedestrian.hpp" +#include + +// $\tau_{\alpha}$ +// https://arxiv.org/pdf/cond-mat/9805244, page 8 +const double tau_alpha = 0.5; + +// $V^0_{\alpha \beta}$ +// https://arxiv.org/pdf/cond-mat/9805244, page 8 +const double V0_AB = 2.1; + +// $\sigma$ +// https://arxiv.org/pdf/cond-mat/9805244, page 8 +const double SIGMA = 0.3; + +// $U^0_{\alpha B}$ +// https://arxiv.org/pdf/cond-mat/9805244, page 8 +const double U0_AB = 10.0; + +// $R$ +// https://arxiv.org/pdf/cond-mat/9805244, page 8 +const double R = 0.2; + +// $c$ +// https://arxiv.org/pdf/cond-mat/9805244, page 8 +const double c = 0.5; + +// $\varphi$ +// https://arxiv.org/pdf/cond-mat/9805244, page 8 +const double phi = 100.0; + +const double cos_phi = cos(phi); + +// $\varDelta_{t}$ +// step size = 0.2 seconds +// https://arxiv.org/pdf/cond-mat/9805244, page 8 +const double delta_t = 2; + +// # Optional (forces.cpp: compute_repulsive_effects_from_pedestrians) +// const double DISTANCE = 2.0; + +double w(Vector2d e, Vector2d f); + +void compute_acceleration_term(Pedestrian *p); + +void compute_repulsive_effects_from_pedestrians(Pedestrian *P, Pedestrian *p, int N); + +void compute_repulsive_effects_from_borders(Board &B, Pedestrian *p); diff --git a/main.cpp b/main.cpp index 248d4f6..2e6035b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,103 +1,89 @@ -#include -#include -#include +#include "board.hpp" +#include "forces.hpp" +#include "math.h" +#include "pedestrian.hpp" +#include "vector2d.hpp" + +double g(double v_max_alpha, double w_alpha_norm) { + if (w_alpha_norm <= v_max_alpha) { + return 1; + } + return v_max_alpha / w_alpha_norm; +} using namespace std; -struct Vector2d { - double x, y; - Vector2d(double a, double b) : x(a), y(b) {} - Vector2d() : x(.0), y(.0) {} - Vector2d operator+(Vector2d v) const { return Vector2d(v.x + x, v.y + y); } - Vector2d operator-(Vector2d v) const { return Vector2d(x - v.x, y - v.y); } - Vector2d operator-(double a) const { return Vector2d(x - a, y - a); } - Vector2d operator+(double a) const { return Vector2d(x + a, y + a); } - Vector2d operator/(double a) const { return Vector2d(x / a, y / a); } - Vector2d operator*(double a) const { return Vector2d(x * a, y * a); } - Vector2d operator+=(const Vector2d &v) { - *this = *this + v; - return *this; - } - double norm() { return sqrt(x * x + y * y); } - double dot(Vector2d v) { return x * v.x + y * v.y; } - void reset() { - x = 0; - y = 0; - } -}; - // Numer of pedestrians // $n_p$ -const int n_p = 100; +// const int n_p = 100; // Number of iterations // $n_{iter}$ -const int n_iter = 100; +// const int n_iter = 100; -// TODO: Change this -// $\tau$ -const double tau = 0.12345; - -void compute_acceleration_term(Vector2d *F_i) {} -void compute_repulsive_effects_from_pedestrians(Vector2d *F_i) {} -void compute_repulsive_effects_from_borders(Vector2d *F_i) {} - -// Return an array of doubles -double *w(Vector2d e, Vector2d f) { - - // TODO: - // $cos(60\degree)$ - // if (e * f > f.norm() * (0.5)) { - // } - // return 0; +Pedestrian *init_pedestrians(int n_p) { + Pedestrian *P = new Pedestrian[n_p]; + for (int i = 0; i < n_p; i++) { + P[i] = Pedestrian(); + } + return P; } -int main() { +Board init_board(int width, int length) { + Vector2d obs[] = {Vector2d(2.0, 1.0), Vector2d(4.0, 1.0), Vector2d(6.0, 2.0), Vector2d(6.0, 4.0)}; + return Board(width, length, 4, obs); +} - // ! DONT TOUCH +int main(int argc, const char* argv[]) { - // $\vec{v}^0_i$ - // Velocity at iteration $t$ for pedestrian $i$ - double v[n_iter][n_p]; + Board B = init_board(10, 15); - // $\vec{r}^0_i$ - // Intended destination at iteration $t$ for pedestrian $i$ - Vector2d r[n_iter][n_p]; + int n_p = atoi(argv[1]); + int n_iter = atoi(argv[2]); - // $\vec{e}_i$ - // Desired direction to the next way point at iteration $t$ for pedestrian $i$ - Vector2d e[n_iter][n_p]; - - // $v_i^{max}$ - // Maximum allowed speed for pedestrian $i$ - double v_max[n_p]; - - // $v_i^{0}$ - // Desired speed for pedestrian $i$ - double v_0[n_p]; - - // $\vec{F}_i$ - // Total social force for pedestrian $i$ - Vector2d F[n_p][n_p]; - - // $\vec{r}_0$ - // Desired destination of pedestrian $n_p$ - Vector2d r_0[n_p]; + Pedestrian *pedestrians = init_pedestrians(n_p); for (int t = 0; t < n_iter; t++) { + for (int i = 0; i < n_p; i++) { - for (int itr = 0; itr < n_p; itr++) { - F[i][itr].reset(); - } - compute_acceleration_term(F[i]); - compute_repulsive_effects_from_pedestrians(F[i]); - compute_repulsive_effects_from_borders(F[i]); - // TODO: Update $\vec{w}_i$ + Pedestrian &p = pedestrians[i]; - // TODO: Update $\vec{v}_i^{t+1}$ + p.F_i.reset(); - // TODO: pdate $\vec{r}_i^{t+1}$ + // + // $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) \vec{e}_\alpha(t) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$ + // + compute_acceleration_term(&p); + + // + // $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta})$ + // + compute_repulsive_effects_from_pedestrians(pedestrians, &p, n_p); + + // + // $\sum_{B} \vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$ + // + compute_repulsive_effects_from_borders(B, &p); + + // + // $\vec{w}_i = \vec{v}_i + \vec{F}_idt$ + // + p.w_alpha = p.curr_vel + p.F_i * delta_t; + + // + // $\vec{v}_i^{t+1} = \vec{w}_ig(\frac{v^{max}_i}{\Arrowvert \vec{w}_i \Arrowvert}) $ + // + p.next_vel = p.w_alpha * g(p.v_max, p.w_alpha.norm()); + + // + // $\vec{r}_i^{t+1} += \vec{r}^t_i + v^t_idt$ + // + p.curr_pos = p.curr_pos + p.curr_vel * delta_t; + } + + for (int i = 0; i < n_p; i++) { + pedestrians[i].update(); } } diff --git a/pedestrian.hpp b/pedestrian.hpp new file mode 100644 index 0000000..8cbef8b --- /dev/null +++ b/pedestrian.hpp @@ -0,0 +1,85 @@ +#pragma once +#include "vector2d.hpp" +#include "random" + + +struct Pedestrian { + + // TODO: Update initial values + Pedestrian() : + curr_vel(), + curr_pos(), + e(), + v_max(1.34 * 1.3), + desired_v0(1.34), + destination(), + F_i(), + next_pos(), + next_vel(), + w_alpha() + {} + + Pedestrian( + Vector2d c_v, + Vector2d c_p, + Vector2d e, + Vector2d d, + Vector2d f, + Vector2d n_p, + Vector2d n_v, + Vector2d w_alpha + ) : + curr_vel(c_v), + curr_pos(c_p), + e(e), + v_max(), + desired_v0(), + destination(d), + F_i(f), + next_pos(n_p), + next_vel(n_v), + w_alpha(w_alpha) + {} + + // $\vec{v}_{\alpha}(t)$ + Vector2d curr_vel; + + // $\vec{r}_\alpha(t)$ + Vector2d curr_pos; + + // $\vec{e}_{\alpha}(t)$ + Vector2d e; + + // $v_{\alpha}^{max} = 1.3 v^{0}_{\alpha}$ + double v_max; + + // $v_\alpha^0$ + double desired_v0; + + // # ============================================================= + + // $\vec{r}^0_\alpha$ + Vector2d destination; + + // social force + Vector2d F_i; + + // # ============================================================= + + // $\vec{r}_\alpha(t+1)$ + Vector2d next_pos; + + // $\vec{v}_{\alpha}(t + 1)$ + Vector2d next_vel; + + // $\vec{w}_{\alpha}$ + Vector2d w_alpha; + + // # ============================================================= + + void update() { + curr_pos = next_pos; + curr_vel = next_vel; + e = (destination - curr_pos) / (destination - curr_pos).norm(); + } +}; diff --git a/tsc_x86.h b/tsc_x86.h new file mode 100644 index 0000000..4e4baf4 --- /dev/null +++ b/tsc_x86.h @@ -0,0 +1,94 @@ +/* ==================== GNU C and possibly other UNIX compilers ===================== */ + #ifndef _WIN32 + + #if defined(__GNUC__) || defined(__linux__) + #define VOLATILE __volatile__ + #define ASM __asm__ + #else + /* if we're neither compiling with gcc or under linux, we can hope + * the following lines work, they probably won't */ + #define ASM asm + #define VOLATILE + #endif + + #define myInt64 unsigned long long + #define INT32 unsigned int + +/* ======================== WIN32 ======================= */ +#else + + #define myInt64 signed __int64 + #define INT32 unsigned __int32 + +#endif + +/* This is the RDTSC timer. + * RDTSC is an instruction on several Intel and compatible CPUs that Reads the + * Time Stamp Counter. The Intel manuals contain more information. + */ + + +#define COUNTER_LO(a) ((a).int32.lo) +#define COUNTER_HI(a) ((a).int32.hi) +#define COUNTER_VAL(a) ((a).int64) + +#define COUNTER(a) \ + ((unsigned long long)COUNTER_VAL(a)) + +#define COUNTER_DIFF(a,b) \ + (COUNTER(a)-COUNTER(b)) + +/* ==================== GNU C and possibly other UNIX compilers ===================== */ +#ifndef _WIN32 + + typedef union + { myInt64 int64; + struct {INT32 lo, hi;} int32; + } tsc_counter; + + #define RDTSC(cpu_c) \ + ASM VOLATILE ("rdtsc" : "=a" ((cpu_c).int32.lo), "=d"((cpu_c).int32.hi)) + #define CPUID() \ + ASM VOLATILE ("cpuid" : : "a" (0) : "bx", "cx", "dx" ) + +/* ======================== WIN32 ======================= */ +#else + + typedef union + { myInt64 int64; + struct {INT32 lo, hi;} int32; + } tsc_counter; + + #define RDTSC(cpu_c) \ + { __asm rdtsc \ + __asm mov (cpu_c).int32.lo,eax \ + __asm mov (cpu_c).int32.hi,edx \ + } + + #define CPUID() \ + { \ + __asm mov eax, 0 \ + __asm cpuid \ + } + +#endif + + +static void init_tsc() { + ; // no need to initialize anything for x86 +} + +static myInt64 start_tsc(void) { + tsc_counter start; + CPUID(); + RDTSC(start); + return COUNTER_VAL(start); +} + +static myInt64 stop_tsc(myInt64 start) { + tsc_counter end; + RDTSC(end); + CPUID(); + return COUNTER_VAL(end) - start; +} + diff --git a/vector2d.hpp b/vector2d.hpp new file mode 100644 index 0000000..b4eef41 --- /dev/null +++ b/vector2d.hpp @@ -0,0 +1,24 @@ +#pragma once +#include + +struct Vector2d { + double x, y; + Vector2d(double a, double b) : x(a), y(b) {} + Vector2d() : x(.0), y(.0) {} + Vector2d operator+(Vector2d v) const { return Vector2d(v.x + x, v.y + y); } + Vector2d operator-(Vector2d v) const { return Vector2d(x - v.x, y - v.y); } + Vector2d operator-(double a) const { return Vector2d(x - a, y - a); } + Vector2d operator+(double a) const { return Vector2d(x + a, y + a); } + Vector2d operator/(double a) const { return Vector2d(x / a, y / a); } + Vector2d operator*(double a) const { return Vector2d(x * a, y * a); } + Vector2d operator+=(const Vector2d &v) { + *this = *this + v; + return *this; + } + double norm() { return sqrt(x * x + y * y); } + double dot(Vector2d v) { return x * v.x + y * v.y; } + void reset() { + x = 0; + y = 0; + } +}; From aa1daf9149f9a9c222c23582b44aaf3583842f0d Mon Sep 17 00:00:00 2001 From: AKallala Date: Thu, 23 Apr 2026 20:14:24 +0200 Subject: [PATCH 2/4] Update board.hpp, forces.hpp, forces.cpp, main.cpp, pedestrian.hpp: correct some bugs in implementation --- board.hpp | 38 ++++++++++++++++++-- forces.cpp | 96 +++++++++++++------------------------------------- forces.hpp | 23 +++--------- main.cpp | 73 ++++++++++++++++---------------------- pedestrian.hpp | 78 ++++------------------------------------ 5 files changed, 102 insertions(+), 206 deletions(-) diff --git a/board.hpp b/board.hpp index 6ee78b4..e76b5ea 100644 --- a/board.hpp +++ b/board.hpp @@ -1,9 +1,41 @@ #pragma once #include "vector2d.hpp" +struct Obstacle { + Vector2d start; + Vector2d end; + + Obstacle() : start(0, 0), end(0, 0) {} + Obstacle(Vector2d s, Vector2d e) : start(s), end(e) {} + // find the point of the obstacle that is nearest to p + Vector2d find_nearest_obstacle_point(Vector2d p) { + Vector2d obstacle_v = end - start; + double projection = (p - start).dot(obstacle_v) / obstacle_v.dot(obstacle_v); + if (projection < 0.0) + projection = 0.0; + if (projection > 1.0) + projection = 1.0; + return start + obstacle_v * projection; + } +}; + struct Board { double length, width; - Vector2d *borders; // array containing the position of each obstacle - int n_borders; // number of obstacles - Board(double l, double w, int n, Vector2d *border) : length(l), width(w), n_borders(n), borders(border) {} + Obstacle *obstacles; // array of obstacles (represented as lines) that are present on the board + int n_obstacles; // number of obstacles + + Board(double l, double w, int n, Obstacle *o) : length(l), width(w), n_obstacles(n + 4) { + obstacles = new Obstacle[n + 4]; + for (int i = 0; i < n; i++) { + obstacles[i] = o[i]; + } + + // borders of the board: bottom, right, top, left + obstacles[n] = Obstacle(Vector2d(0, 0), Vector2d(l, 0)); + obstacles[n + 1] = Obstacle(Vector2d(l, 0), Vector2d(l, w)); + obstacles[n + 2] = Obstacle(Vector2d(l, w), Vector2d(0, w)); + obstacles[n + 3] = Obstacle(Vector2d(0, w), Vector2d(0, 0)); + } + + ~Board() { delete[] obstacles; } }; diff --git a/forces.cpp b/forces.cpp index b48d0d0..24dfa5f 100644 --- a/forces.cpp +++ b/forces.cpp @@ -1,35 +1,26 @@ #include "forces.hpp" #include -// # Done double w(Vector2d e, Vector2d f) { if (e.dot(f) >= f.norm() * cos_phi) return 1.0; - return c; + else + return c; +} + +double g(double v_max_alpha, double w_alpha_norm) { + if (w_alpha_norm <= v_max_alpha) { + return 1; + } + return v_max_alpha / w_alpha_norm; } -// # Done -// -// $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) \vec{e}_\alpha(t) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$ -// void compute_acceleration_term(Pedestrian *p) { - - // $v_{\alpha}^{0}\vec{e}_{\alpha}$ Vector2d desired_velocity = p->e * p->desired_v0; - - // $\vec{v}_{\alpha}(t)$ Vector2d actual_velocity = p->curr_vel; - - double tau_alpha_inv = 1 / tau_alpha; - - p->F_i += (desired_velocity - actual_velocity) * tau_alpha_inv; + p->f_i += (desired_velocity - actual_velocity) / tau_alpha; } -// # Done -// -// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta})$ -// - void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestrian *p, int N) { for (int i = 0; i < N; i++) { @@ -43,78 +34,41 @@ void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestr // continue; // } - // $\vec{r}_{\alpha\beta}$ Vector2d r_ab = p->curr_pos - pedestrians[i].curr_pos; - // $\Arrowvert \vec{r}_{\alpha \beta} \Arrowvert$ double r_ab_norm = r_ab.norm(); - // $\vec{e}_{\beta}$ - Vector2d e_beta = pedestrians[i].e; + Vector2d e_b = pedestrians[i].e; - // $v_\beta \Delta t$ - double v_beta_delta_t = pedestrians[i].desired_v0 * delta_t; + double s_b = pedestrians[i].desired_v0 * delta_t; - // $\vec{r}_{\alpha\beta} - \vec{v}_{\beta}\varDelta_{t}\vec{e}_{\beta}$ - Vector2d r_ab_minus_v_beta_delta_t_e_beta = r_ab - (e_beta * v_beta_delta_t); + Vector2d r_ab2 = r_ab - (e_b * s_b); - // $\Arrowvert \vec{r}_{\alpha \beta} - v_\beta \Delta t \vec{e}_\beta \Arrowvert$ - double r_ab_minus_v_beta_delta_t_e_beta_norm = r_ab_minus_v_beta_delta_t_e_beta.norm(); + double r_ab2_norm = r_ab2.norm(); - // (4): - // - // $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 b = sqrt(pow(r_ab_norm + r_ab2_norm, 2) - pow(s_b, 2)) / 2; - double b = sqrt((r_ab_norm + r_ab_minus_v_beta_delta_t_e_beta_norm) * (r_ab_norm + r_ab_minus_v_beta_delta_t_e_beta_norm) - (v_beta_delta_t) * (v_beta_delta_t)) / 2; + Vector2d b_grad = (r_ab / r_ab_norm + r_ab2 / r_ab2_norm) * (r_ab_norm + r_ab2_norm) / (4 * b); - // (7): - // - // $\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})}{4b}$ - // - // - Vector2d delta_r_alpha_beta_b = (((r_ab / r_ab_norm) + (r_ab_minus_v_beta_delta_t_e_beta / r_ab_minus_v_beta_delta_t_e_beta_norm)) * (r_ab_norm + r_ab_minus_v_beta_delta_t_e_beta_norm)) / (4 * b); + Vector2d f_ped = b_grad * ((V0_AB / SIGMA) * (exp(-b / SIGMA))); - // (5): - // - // $\vec{f}_{\alpha \beta}(\vec{r}_{\alpha \beta}) = \frac{V^0_{\alpha \beta}}{\sigma}e^{-b / \sigma} \grad_{\vec{r}_{\alpha \beta}}b$ - // - // - Vector2d f_alpha_beta_r_alpha_beta = delta_r_alpha_beta_b * ((V0_AB / SIGMA) * (exp(-b / SIGMA))); - - // (11): - // - // $\vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta}) = w(\vec{e}_{\alpha}, -\vec{f}_{\alpha\beta})\vec{f}_{\alpha\beta}(\vec{r}_{\alpha} - \vec{r}_{\beta})$ - // - // Add repulsive effects from other pedestrians to current pedestrian's social force - p->F_i += f_alpha_beta_r_alpha_beta * w(p->e, f_alpha_beta_r_alpha_beta * -1); + p->f_i += f_ped * w(p->e, f_ped * -1); } } -// # Done -void compute_repulsive_effects_from_borders(Board &B, Pedestrian *p) { +void compute_repulsive_effects_from_borders(Board &board, Pedestrian *p) { - Vector2d *borders = B.borders; - int n_borders = B.n_borders; + Obstacle *obstacles = board.obstacles; + int n_borders = board.n_obstacles; for (int i = 0; i < n_borders; i++) { - Vector2d border = borders[i]; + Obstacle obstacle = obstacles[i]; - // $\vec{r}_{\alpha B}$ - Vector2d r_alpha_B = p->curr_pos - border; + Vector2d r_aB = p->curr_pos - obstacle.find_nearest_obstacle_point(p->curr_pos); - // - // $\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_obst = (r_aB / r_aB.norm()) * (U0_AB / R) * std::exp(-r_aB.norm() / R); - Vector2d F_alpha_B = (r_alpha_B / r_alpha_B.norm()) * (U0_AB / R) * std::exp(-r_alpha_B.norm() / R); - - // (12): - // - // $\vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{ B}) = w(\vec{e}_{\alpha}, -\vec{F}_{\alpha B})\vec{F}_{\alpha B}(\vec{r}_{\alpha} - \vec{r}_{ B})$ - // - - p->F_i += F_alpha_B * w(p->e, F_alpha_B * (-1)); + p->f_i += f_obst; } } diff --git a/forces.hpp b/forces.hpp index 27cc042..3d2bd4a 100644 --- a/forces.hpp +++ b/forces.hpp @@ -3,46 +3,33 @@ #include "pedestrian.hpp" #include -// $\tau_{\alpha}$ -// https://arxiv.org/pdf/cond-mat/9805244, page 8 const double tau_alpha = 0.5; -// $V^0_{\alpha \beta}$ -// https://arxiv.org/pdf/cond-mat/9805244, page 8 const double V0_AB = 2.1; -// $\sigma$ -// https://arxiv.org/pdf/cond-mat/9805244, page 8 const double SIGMA = 0.3; -// $U^0_{\alpha B}$ -// https://arxiv.org/pdf/cond-mat/9805244, page 8 const double U0_AB = 10.0; -// $R$ -// https://arxiv.org/pdf/cond-mat/9805244, page 8 const double R = 0.2; -// $c$ -// https://arxiv.org/pdf/cond-mat/9805244, page 8 const double c = 0.5; -// $\varphi$ -// https://arxiv.org/pdf/cond-mat/9805244, page 8 -const double phi = 100.0; +const double phi = 100.0 / 180.0 * M_PI; const double cos_phi = cos(phi); -// $\varDelta_{t}$ -// step size = 0.2 seconds -// https://arxiv.org/pdf/cond-mat/9805244, page 8 const double delta_t = 2; +const double dt = 0.01; + // # Optional (forces.cpp: compute_repulsive_effects_from_pedestrians) // const double DISTANCE = 2.0; double w(Vector2d e, Vector2d f); +double g(double v_max_alpha, double w_alpha_norm); + void compute_acceleration_term(Pedestrian *p); void compute_repulsive_effects_from_pedestrians(Pedestrian *P, Pedestrian *p, int N); diff --git a/main.cpp b/main.cpp index 2e6035b..10ee86e 100644 --- a/main.cpp +++ b/main.cpp @@ -4,12 +4,8 @@ #include "pedestrian.hpp" #include "vector2d.hpp" -double g(double v_max_alpha, double w_alpha_norm) { - if (w_alpha_norm <= v_max_alpha) { - return 1; - } - return v_max_alpha / w_alpha_norm; -} +#define BOARD_WIDTH 10.0 +#define BOARD_LENGTH 50.0 using namespace std; @@ -23,67 +19,58 @@ using namespace std; Pedestrian *init_pedestrians(int n_p) { Pedestrian *P = new Pedestrian[n_p]; - for (int i = 0; i < n_p; i++) { - P[i] = Pedestrian(); + std::random_device rd; // use for truly random simulation + std::mt19937 gen(rd()); // use rd instead of 42 as seed for real randomness + std::normal_distribution velocity_gen(1.34, 0.26); // 1.34 and 0.26 are the mean and stdrd deviation used in paper + std::uniform_real_distribution x_gen(2, BOARD_LENGTH - 2); + std::uniform_real_distribution y_gen(2, BOARD_WIDTH - 2); + for (int i = 0; i < n_p / 2; i++) { + Vector2d start_pos = Vector2d(1, y_gen(gen)); + Vector2d destination = Vector2d(BOARD_LENGTH - 1, y_gen(gen)); + double desired_v0 = velocity_gen(gen); + double v_max = 1.3 * desired_v0; + P[i] = Pedestrian(WHITE, (destination - start_pos) / (destination - start_pos).norm() * desired_v0, start_pos, (destination - start_pos) / (destination - start_pos).norm(), v_max, desired_v0, destination, Vector2d()); + } + + for (int i = n_p / 2; i < n_p; i++) { + Vector2d start_pos = Vector2d(BOARD_LENGTH - 1, y_gen(gen)); + Vector2d destination = Vector2d(1, y_gen(gen)); + double desired_v0 = velocity_gen(gen); + double v_max = 1.3 * desired_v0; + P[i] = Pedestrian(BLACK, (destination - start_pos) / (destination - start_pos).norm() * desired_v0, start_pos, (destination - start_pos) / (destination - start_pos).norm(), v_max, desired_v0, destination, Vector2d(0, 0)); } return P; } -Board init_board(int width, int length) { - Vector2d obs[] = {Vector2d(2.0, 1.0), Vector2d(4.0, 1.0), Vector2d(6.0, 2.0), Vector2d(6.0, 4.0)}; - return Board(width, length, 4, obs); -} +int main(int argc, const char *argv[]) { -int main(int argc, const char* argv[]) { - - Board B = init_board(10, 15); + Obstacle obs[] = {}; + Board board = Board(BOARD_LENGTH, BOARD_WIDTH, 0, obs); int n_p = atoi(argv[1]); int n_iter = atoi(argv[2]); - Pedestrian *pedestrians = init_pedestrians(n_p); - for (int t = 0; t < n_iter; t++) { for (int i = 0; i < n_p; i++) { Pedestrian &p = pedestrians[i]; - p.F_i.reset(); + p.f_i.reset(); - // - // $\vec{F}_\alpha(\vec{v}_\alpha, v^0_\alpha \vec{e}_\alpha) \vec{e}_\alpha(t) = \frac{1}{\tau_\alpha}(v^0_\alpha \vec{e}_\alpha - \vec{v}_\alpha)$ - // compute_acceleration_term(&p); - // - // $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta})$ - // compute_repulsive_effects_from_pedestrians(pedestrians, &p, n_p); - // - // $\sum_{B} \vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$ - // - compute_repulsive_effects_from_borders(B, &p); + compute_repulsive_effects_from_borders(board, &p); - // - // $\vec{w}_i = \vec{v}_i + \vec{F}_idt$ - // - p.w_alpha = p.curr_vel + p.F_i * delta_t; + Vector2d w_alpha = p.curr_vel + p.f_i * dt; - // - // $\vec{v}_i^{t+1} = \vec{w}_ig(\frac{v^{max}_i}{\Arrowvert \vec{w}_i \Arrowvert}) $ - // - p.next_vel = p.w_alpha * g(p.v_max, p.w_alpha.norm()); + p.curr_vel = w_alpha * g(p.v_max, w_alpha.norm()); - // - // $\vec{r}_i^{t+1} += \vec{r}^t_i + v^t_idt$ - // - p.curr_pos = p.curr_pos + p.curr_vel * delta_t; - } + p.curr_pos = p.curr_pos + p.curr_vel * dt; - for (int i = 0; i < n_p; i++) { - pedestrians[i].update(); + p.e = (p.destination - p.curr_pos) / (p.destination - p.curr_pos).norm(); } } diff --git a/pedestrian.hpp b/pedestrian.hpp index 8cbef8b..54b3abe 100644 --- a/pedestrian.hpp +++ b/pedestrian.hpp @@ -1,85 +1,21 @@ #pragma once -#include "vector2d.hpp" #include "random" +#include "vector2d.hpp" +// For visual simulation +enum Color { BLACK, WHITE }; struct Pedestrian { - - // TODO: Update initial values - Pedestrian() : - curr_vel(), - curr_pos(), - e(), - v_max(1.34 * 1.3), - desired_v0(1.34), - destination(), - F_i(), - next_pos(), - next_vel(), - w_alpha() - {} - - Pedestrian( - Vector2d c_v, - Vector2d c_p, - Vector2d e, - Vector2d d, - Vector2d f, - Vector2d n_p, - Vector2d n_v, - Vector2d w_alpha - ) : - curr_vel(c_v), - curr_pos(c_p), - e(e), - v_max(), - desired_v0(), - destination(d), - F_i(f), - next_pos(n_p), - next_vel(n_v), - w_alpha(w_alpha) - {} - - // $\vec{v}_{\alpha}(t)$ + Color color; Vector2d curr_vel; - - // $\vec{r}_\alpha(t)$ Vector2d curr_pos; - - // $\vec{e}_{\alpha}(t)$ Vector2d e; - - // $v_{\alpha}^{max} = 1.3 v^{0}_{\alpha}$ double v_max; - - // $v_\alpha^0$ double desired_v0; - - // # ============================================================= - - // $\vec{r}^0_\alpha$ Vector2d destination; + Vector2d f_i; - // social force - Vector2d F_i; + Pedestrian() : color(WHITE), curr_vel(0.0, 0.0), curr_pos(0.0, 0.0), e(0.0, 0.0), v_max(0.0), desired_v0(0.0), destination(0.0, 0.0), f_i(0.0, 0.0) {} - // # ============================================================= - - // $\vec{r}_\alpha(t+1)$ - Vector2d next_pos; - - // $\vec{v}_{\alpha}(t + 1)$ - Vector2d next_vel; - - // $\vec{w}_{\alpha}$ - Vector2d w_alpha; - - // # ============================================================= - - void update() { - curr_pos = next_pos; - curr_vel = next_vel; - e = (destination - curr_pos) / (destination - curr_pos).norm(); - } + Pedestrian(Color clr, Vector2d c_v, Vector2d c_p, Vector2d e, double v_max, double desired_v0, Vector2d d, Vector2d f) : color(clr), curr_vel(c_v), curr_pos(c_p), e(e), v_max(v_max), desired_v0(desired_v0), destination(d), f_i(f) {} }; From 3d217e784060de58d1dc570cca767c59f6a87867 Mon Sep 17 00:00:00 2001 From: Nguyen Quy Anh <23906-nguyenquy@users.noreply.gitlab.inf.ethz.ch> Date: Fri, 24 Apr 2026 16:31:42 +0200 Subject: [PATCH 3/4] Added measurements --- board.hpp | 15 +++++++-- forces.cpp | 22 +++++++++++- main | Bin 0 -> 31912 bytes main.cpp | 30 +++++++++++++++++ questions.md | 6 ++++ tsc_x86.h | 93 +++++++++++++++++++++++---------------------------- vector2d.hpp | 2 ++ 7 files changed, 113 insertions(+), 55 deletions(-) create mode 100755 main create mode 100644 questions.md diff --git a/board.hpp b/board.hpp index e76b5ea..33da163 100644 --- a/board.hpp +++ b/board.hpp @@ -7,16 +7,27 @@ struct Obstacle { Obstacle() : start(0, 0), end(0, 0) {} Obstacle(Vector2d s, Vector2d e) : start(s), end(e) {} - // find the point of the obstacle that is nearest to p + + // Find the point of the obstacle that is nearest to p + // # 15 flops Vector2d find_nearest_obstacle_point(Vector2d p) { + + // # 2 ADDs Vector2d obstacle_v = end - start; + + // # 9 flops = 2 ADDs, 2 MULTs, 1 ADD, 1 DIV, 2 MULTs, 1 ADD double projection = (p - start).dot(obstacle_v) / obstacle_v.dot(obstacle_v); + if (projection < 0.0) projection = 0.0; - if (projection > 1.0) + else if (projection > 1.0) projection = 1.0; + + // # 4 flops = 2 ADDs, 2 MULTs return start + obstacle_v * projection; + } + }; struct Board { diff --git a/forces.cpp b/forces.cpp index 24dfa5f..5911909 100644 --- a/forces.cpp +++ b/forces.cpp @@ -1,6 +1,7 @@ #include "forces.hpp" #include +// # 8 flops = 5 MULTs, 2 ADD, 1 SQRT double w(Vector2d e, Vector2d f) { if (e.dot(f) >= f.norm() * cos_phi) return 1.0; @@ -8,6 +9,7 @@ double w(Vector2d e, Vector2d f) { return c; } +// # 2 DIVs double g(double v_max_alpha, double w_alpha_norm) { if (w_alpha_norm <= v_max_alpha) { return 1; @@ -15,12 +17,16 @@ double g(double v_max_alpha, double w_alpha_norm) { return v_max_alpha / w_alpha_norm; } +// # 8 flops void compute_acceleration_term(Pedestrian *p) { + // # 2 flops Vector2d desired_velocity = p->e * p->desired_v0; Vector2d actual_velocity = p->curr_vel; + // # 6 flops = 4 ADDs, 2 DIVs p->f_i += (desired_velocity - actual_velocity) / tau_alpha; } +// # 55(N - 1) = 55N - 55 flops void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestrian *p, int N) { for (int i = 0; i < N; i++) { @@ -34,28 +40,39 @@ void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestr // continue; // } + // # 2 ADDs Vector2d r_ab = p->curr_pos - pedestrians[i].curr_pos; + // # 4 flops = 1 SQRT, 2 MULTs, 1 ADD double r_ab_norm = r_ab.norm(); Vector2d e_b = pedestrians[i].e; + // # 2 MULTs double s_b = pedestrians[i].desired_v0 * delta_t; + // # 4 flops = 2 ADDs, 2 MULTs Vector2d r_ab2 = r_ab - (e_b * s_b); + // # 4 flops = 1 SQRT, 2 MULTs, 1 ADD double r_ab2_norm = r_ab2.norm(); - double b = sqrt(pow(r_ab_norm + r_ab2_norm, 2) - pow(s_b, 2)) / 2; + // # 7 flops = 1 SQRT, 3 ADDs, 2 MULTs, 1 DIV + double b = sqrt((r_ab_norm + r_ab2_norm) * (r_ab_norm + r_ab2_norm) - s_b * s_b) / 2; + // # 12 flops = 6 DIVs, 3 ADDs, 3 MULTs Vector2d b_grad = (r_ab / r_ab_norm + r_ab2 / r_ab2_norm) * (r_ab_norm + r_ab2_norm) / (4 * b); + // # 6 flops = 3 MULTs, 2 DIVs, 1 EXP Vector2d f_ped = b_grad * ((V0_AB / SIGMA) * (exp(-b / SIGMA))); + // # 14 flops: 9 MULTs, 4 ADDs, 1 SQRT p->f_i += f_ped * w(p->e, f_ped * -1); } } +// # Assume that the four borders are also 4 obstacles +// # 36 n_obstacles flops = 144 flops void compute_repulsive_effects_from_borders(Board &board, Pedestrian *p) { Obstacle *obstacles = board.obstacles; @@ -65,10 +82,13 @@ void compute_repulsive_effects_from_borders(Board &board, Pedestrian *p) { Obstacle obstacle = obstacles[i]; + // # 17 flops Vector2d r_aB = p->curr_pos - obstacle.find_nearest_obstacle_point(p->curr_pos); + // # 17 flops = 2 DIVs, 1 SQRT, 2 MULTs, 1 ADD, 2 MULTs, 1 DIV, 2 MULTs, 1 EXP, 1 SQRT, 2 MULTs, 1 ADD, 1 DIV Vector2d f_obst = (r_aB / r_aB.norm()) * (U0_AB / R) * std::exp(-r_aB.norm() / R); + // # 2 ADDs p->f_i += f_obst; } } diff --git a/main b/main new file mode 100755 index 0000000000000000000000000000000000000000..6df50769a8b160144d31a3d4aa10357823305fe1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#|wV3k1Kq!kLzE5qWlXv|5Pqe>?0FD?S+f< z1%DZElts322cI2!`}ktNnNZ|JKD3Y|Edk)mA7RpByuM$B^Js4}KlS5J;)^cfi~Nf8 zL|FJg_v07)Fv8!FC^$7=q%W{<`0fggWNx<)7b)j}|rBkGL@rT_Bd7yCEDFB62^_fP)- z@V@%T|Gw1-KYsu8Tm1ONzMJs<`Tf&B5B^a6xZ3mXV~P7s{o7llFZ?Uu#qNujU+kN0 zpn(l1=(Nzau)x!YjcDwM^u>OYxaZcy>z~FyIl(9L^$Kv*{(@iJ2e-{e)Y`jC8l*i1 zzwnJ<_T?A*SM8jCgD*2+!6*E1KYp=)v2vcqDD;F7Bltx44)7CB)V8K7t4=?zI{{znNM<%t-(=h&W1)re*;Sl_zvNehy3qtPu^G_dwe<|mm z^%wY`8iM~I=g<2K{Mnq}znqg5jj?kGey}lDFa&>Oju!t5PA5FUC;X+HU&IT48|N4E zr@#w75neq6|5?uOKmYjiQ`k?%G0}!xErmsVV!IbX>dT@(ihfP{Jy0VZ@GRpEzmoHd WNdNR{x*Ck%@Vv&c-jBgADE(g?lkXJ( literal 0 HcmV?d00001 diff --git a/main.cpp b/main.cpp index 10ee86e..ffbc725 100644 --- a/main.cpp +++ b/main.cpp @@ -2,7 +2,9 @@ #include "forces.hpp" #include "math.h" #include "pedestrian.hpp" +#include "tsc_x86.h" #include "vector2d.hpp" +#include #define BOARD_WIDTH 10.0 #define BOARD_LENGTH 50.0 @@ -50,6 +52,11 @@ int main(int argc, const char *argv[]) { int n_p = atoi(argv[1]); int n_iter = atoi(argv[2]); Pedestrian *pedestrians = init_pedestrians(n_p); + + // # Start here or right after function signature + uint64_t start = start_tsc(); + + // # (8 + 55N - 55 + 144 + 4 + 8 + 4 + 10)*n_p*n_iter for (int t = 0; t < n_iter; t++) { for (int i = 0; i < n_p; i++) { @@ -58,21 +65,44 @@ int main(int argc, const char *argv[]) { p.f_i.reset(); + // # 8 flops compute_acceleration_term(&p); + // # 55N - 55 flops compute_repulsive_effects_from_pedestrians(pedestrians, &p, n_p); + // # 144 flops compute_repulsive_effects_from_borders(board, &p); + // # 4 flops = 2 ADDs, 2 MULTs Vector2d w_alpha = p.curr_vel + p.f_i * dt; + // # 8 flops = 2 MULTs, 2 DIVs, 1 SQRT, 2 MULTs, 1 ADD p.curr_vel = w_alpha * g(p.v_max, w_alpha.norm()); + // # 4 flops = 2 ADDs, 2 MULTs p.curr_pos = p.curr_pos + p.curr_vel * dt; + // # 10 flops = 5 ADDs, 2 DIVs, 1 SQRT, 2 MULTs p.e = (p.destination - p.curr_pos) / (p.destination - p.curr_pos).norm(); } } + int W = (8 + 55 * n_p - 55 + 144 + 4 + 8 + 4 + 10) * n_p * n_iter; + std::cout << "W(n): "; + std::cout << W; + std::cout << " flops\n"; + + double T = stop_tsc(start); + std::cout << "T(n): "; + std::cout << T; + std::cout << " cycles\n"; + double P = W/T; + + std::cout << "P(n): "; + std::cout << P; + std::cout << " flops/cycle\n"; + + delete []pedestrians; return 0; } diff --git a/questions.md b/questions.md new file mode 100644 index 0000000..6de7008 --- /dev/null +++ b/questions.md @@ -0,0 +1,6 @@ +- [ ] SQRT == 1 flop? EXP == 1 flop? +- [ ] Where to start measurements, do we include measurements? +- [ ] Can Vector2d be reused? +- [ ] How precise do we have to be? How do we define "correctness"? +- [ ] Do borders count as obstacles? +- [ ] Do we use a fixed machine, frequency scaling, etc. diff --git a/tsc_x86.h b/tsc_x86.h index 4e4baf4..be144a3 100644 --- a/tsc_x86.h +++ b/tsc_x86.h @@ -1,24 +1,24 @@ /* ==================== GNU C and possibly other UNIX compilers ===================== */ - #ifndef _WIN32 +#ifndef _WIN32 - #if defined(__GNUC__) || defined(__linux__) - #define VOLATILE __volatile__ - #define ASM __asm__ - #else - /* if we're neither compiling with gcc or under linux, we can hope - * the following lines work, they probably won't */ - #define ASM asm - #define VOLATILE - #endif +#if defined(__GNUC__) || defined(__linux__) +#define VOLATILE __volatile__ +#define ASM __asm__ +#else +/* if we're neither compiling with gcc or under linux, we can hope + * the following lines work, they probably won't */ +#define ASM asm +#define VOLATILE +#endif - #define myInt64 unsigned long long - #define INT32 unsigned int +#define myInt64 unsigned long long +#define INT32 unsigned int /* ======================== WIN32 ======================= */ #else - #define myInt64 signed __int64 - #define INT32 unsigned __int32 +#define myInt64 signed __int64 +#define INT32 unsigned __int32 #endif @@ -27,68 +27,57 @@ * Time Stamp Counter. The Intel manuals contain more information. */ - #define COUNTER_LO(a) ((a).int32.lo) #define COUNTER_HI(a) ((a).int32.hi) #define COUNTER_VAL(a) ((a).int64) -#define COUNTER(a) \ - ((unsigned long long)COUNTER_VAL(a)) +#define COUNTER(a) ((unsigned long long)COUNTER_VAL(a)) -#define COUNTER_DIFF(a,b) \ - (COUNTER(a)-COUNTER(b)) +#define COUNTER_DIFF(a, b) (COUNTER(a) - COUNTER(b)) /* ==================== GNU C and possibly other UNIX compilers ===================== */ #ifndef _WIN32 - typedef union - { myInt64 int64; - struct {INT32 lo, hi;} int32; - } tsc_counter; +typedef union { + myInt64 int64; + struct { + INT32 lo, hi; + } int32; +} tsc_counter; - #define RDTSC(cpu_c) \ - ASM VOLATILE ("rdtsc" : "=a" ((cpu_c).int32.lo), "=d"((cpu_c).int32.hi)) - #define CPUID() \ - ASM VOLATILE ("cpuid" : : "a" (0) : "bx", "cx", "dx" ) +#define RDTSC(cpu_c) ASM VOLATILE("rdtsc" : "=a"((cpu_c).int32.lo), "=d"((cpu_c).int32.hi)) +#define CPUID() ASM VOLATILE("cpuid" : : "a"(0) : "bx", "cx", "dx") /* ======================== WIN32 ======================= */ #else - typedef union - { myInt64 int64; - struct {INT32 lo, hi;} int32; - } tsc_counter; +typedef union { + myInt64 int64; + struct { + INT32 lo, hi; + } int32; +} tsc_counter; - #define RDTSC(cpu_c) \ - { __asm rdtsc \ - __asm mov (cpu_c).int32.lo,eax \ - __asm mov (cpu_c).int32.hi,edx \ - } +#define RDTSC(cpu_c) {__asm rdtsc __asm mov(cpu_c).int32.lo, eax __asm mov(cpu_c).int32.hi, edx} - #define CPUID() \ - { \ - __asm mov eax, 0 \ - __asm cpuid \ - } +#define CPUID() {__asm mov eax, 0 __asm cpuid} #endif - static void init_tsc() { - ; // no need to initialize anything for x86 + ; // no need to initialize anything for x86 } static myInt64 start_tsc(void) { - tsc_counter start; - CPUID(); - RDTSC(start); - return COUNTER_VAL(start); + tsc_counter start; + CPUID(); + RDTSC(start); + return COUNTER_VAL(start); } static myInt64 stop_tsc(myInt64 start) { - tsc_counter end; - RDTSC(end); - CPUID(); - return COUNTER_VAL(end) - start; + tsc_counter end; + RDTSC(end); + CPUID(); + return COUNTER_VAL(end) - start; } - diff --git a/vector2d.hpp b/vector2d.hpp index b4eef41..b9bf1f0 100644 --- a/vector2d.hpp +++ b/vector2d.hpp @@ -15,7 +15,9 @@ struct Vector2d { *this = *this + v; return *this; } + // # 4 flops = 1 SQRT, 2 MULTs, 1 ADD double norm() { return sqrt(x * x + y * y); } + // # 3 flops = 2 MULTs, 1 ADD double dot(Vector2d v) { return x * v.x + y * v.y; } void reset() { x = 0; From 49e96e85a1a71e110f3cf8a7e7b18a05d82597af Mon Sep 17 00:00:00 2001 From: lbaumberger Date: Sat, 25 Apr 2026 20:57:50 +0200 Subject: [PATCH 4/4] created two passes Pass1: compute all forces Pass2: integrate the forces and do the updates If we dont separate it, we will have some inter pedestrian forces calculated on a mix of new and updated values --- main.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index ffbc725..6e00475 100644 --- a/main.cpp +++ b/main.cpp @@ -58,7 +58,7 @@ int main(int argc, const char *argv[]) { // # (8 + 55N - 55 + 144 + 4 + 8 + 4 + 10)*n_p*n_iter for (int t = 0; t < n_iter; t++) { - + // Pass1: conmpute all forces from the current state for (int i = 0; i < n_p; i++) { Pedestrian &p = pedestrians[i]; @@ -73,7 +73,10 @@ int main(int argc, const char *argv[]) { // # 144 flops compute_repulsive_effects_from_borders(board, &p); - + } + // Pass2: integrate the forces and update the current state + for (int i = 0; i < n_p; i++) { + Pedestrian &p = pedestrians[i]; // # 4 flops = 2 ADDs, 2 MULTs Vector2d w_alpha = p.curr_vel + p.f_i * dt; @@ -91,18 +94,18 @@ int main(int argc, const char *argv[]) { std::cout << "W(n): "; std::cout << W; std::cout << " flops\n"; - + double T = stop_tsc(start); std::cout << "T(n): "; std::cout << T; std::cout << " cycles\n"; - double P = W/T; + double P = W / T; std::cout << "P(n): "; std::cout << P; std::cout << " flops/cycle\n"; - delete []pedestrians; + delete[] pedestrians; return 0; }