reduced flop count

This commit is contained in:
Nguyen Quy Anh
2026-05-03 05:57:34 +02:00
parent 89dfdf7785
commit e9533ab925
10 changed files with 236 additions and 166 deletions
+129 -93
View File
@@ -6,172 +6,217 @@
#include "vector2d.hpp"
#include <fstream>
#include <iostream>
#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
#define BOARD_LENGTH_MINUS_ONE_POINT_FIVE BOARD_LENGTH - 1.5
#define WALL_EPS 1e-3
#define BOARD_WIDTH_MINUS_WALL_EPS BOARD_WIDTH - WALL_EPS
#define BOARD_LENGTH_HALF BOARD_LENGTH / 2
#define BOARD_LENGTH_MINUS_TWO BOARD_LENGTH - 2
#define BOARD_WIDTH_MINUS_ZERO_POINT_THREE BOARD_WIDTH - 0.3
#define BOARD_LENGTH_PLUS_THIRTY BOARD_LENGTH + 30
using namespace std;
// Numer of pedestrians
// $n_p$
// const int n_p = 100;
// Number of iterations
// $n_{iter}$
// const int n_iter = 100;
Pedestrian *init_pedestrians(int n_p) {
Pedestrian *P = new Pedestrian[n_p];
std::random_device rd; // use for truly random simulation
// std::mt19937 gen(rd()); // use rd instead of 42 as seed for real randomness
std::mt19937 gen(42); // use fixed seed for reproducibility
std::normal_distribution<double> velocity_gen(1.34, 0.26); // 1.34 and 0.26 are the mean and stdrd deviation used in paper
std::uniform_real_distribution<double> x_gen_left(2, BOARD_LENGTH / 2);
std::uniform_real_distribution<double> x_gen_right(BOARD_LENGTH / 2, BOARD_LENGTH - 2);
std::uniform_real_distribution<double> y_gen(0.3, BOARD_WIDTH - 0.3);
for (int i = 0; i < n_p / 2; i++) {
// Platform-specific non-deterministic random number generator (if available)
// random_device rd;
// mt19937 gen(rd()); // Use rd for true randomness
mt19937 gen(SEED); // Use fixed seed for reproducibility
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;
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_gen_left(gen), y);
Vector2d destination = Vector2d(BOARD_LENGTH + 30, y);
// double desired_v0 = velocity_gen(gen);
Vector2d start_pos = Vector2d(x, y);
Vector2d destination = Vector2d(BOARD_LENGTH_PLUS_THIRTY, y);
Vector2d init_e = destination - start_pos;
double init_e_norm = init_e.norm();
Vector2d init_e_unit = init_e / init_e_norm;
double desired_v0;
// Avoid values are too small since they would make the pedestrians walk backwards
do {
desired_v0 = velocity_gen(gen);
} while (desired_v0 <= 0.1); // to avoid small values (Way below the social force -> that would make the walk backwards) (we discovered some weird stuff in the simulation)
} while (desired_v0 <= 0.1);
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());
P[i] = Pedestrian(WHITE, init_e_unit * desired_v0, start_pos, init_e_unit, v_max, desired_v0, destination, Vector2d());
}
for (int i = n_p / 2; i < n_p; i++) {
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_gen_right(gen), y);
Vector2d start_pos = Vector2d(x, y);
Vector2d destination = Vector2d(-30, y);
// double desired_v0 = velocity_gen(gen);
Vector2d init_e = destination - start_pos;
double init_e_norm = init_e.norm();
Vector2d init_e_unit = init_e / 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(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));
P[i] = Pedestrian(BLACK, init_e_unit * desired_v0, start_pos, init_e_unit, v_max, desired_v0, destination, Vector2d(0, 0));
}
return P;
}
int main(int argc, const char *argv[]) {
// Check command line arguments
// Ensure that arguments are passed
if (argc < 3) {
printf("Usage: ./sim <number_of_pedestrians> <number_of_iterations>\n");
printf(" number_of_iterations: each iteration = %.2f seconds of simulation time\n", dt);
printf("Each iteration takes %.2f seconds in the simulation.\n", dt);
return 1;
}
// Parse command line arguments
// Parse command-line arguments
int n_p = atoi(argv[1]);
int n_iter = atoi(argv[2]);
if (n_p <= 0) {
printf("Error: <number_of_pedestrians> must be a positive integer, got '%s'\n", argv[1]);
printf("Error: <number_of_pedestrians> must be a positive integer, got '%s' instead\n", argv[1]);
return 1;
}
if (n_iter <= 0) {
printf("Error: <number_of_iterations> must be a positive integer, got '%s'\n", argv[2]);
printf("Error: <number_of_iterations> must be a positive integer, got '%s' instead\n", argv[2]);
return 1;
}
Obstacle obs[] = {};
Board board = Board(BOARD_LENGTH, BOARD_WIDTH, 0, obs);
// Logic to get/dump the simulation data
const int dump_interval = 1; // dump every 5 iterations -> 20 fps (since dt = 0.01)
// Logic for importing/exporting the simulation data
const int dump_interval = 1;
const char *dump_path = (argc >= 4) ? argv[3] : nullptr;
std::vector<std::tuple<int, int, double, double, double, double, int>> samples; // t, id, x, y, e.x, e.y, color
if (dump_path)
vector<tuple<int, int, double, double, double, double, int>> samples; // t, id, x, y, e.x, e.y, color
if (dump_path) {
samples.reserve((size_t)n_iter / dump_interval * n_p);
}
Pedestrian *pedestrians = init_pedestrians(n_p);
// # Start here or right after function signature
uint64_t start = start_tsc();
// # (8 + 55N - 55 + 192 + 4 + 8 + 4 + 10)*n_p*n_iter
for (int t = 0; t < n_iter; t++) {
// Pass1: conmpute all forces from the current state
// Pass 1: Conmpute all the forces given the current state
for (int i = 0; i < n_p; i++) {
Pedestrian &p = pedestrians[i];
p.f_i.reset();
// # 8 flops
// # 6 ADDs, 2 DIVs
//
// $\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)$
// $\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)$
//
compute_acceleration_term(&p);
add_acceleration_term(&p);
// # 55N - 55 flops
// # 15 ADDs, 24 MULTs, 8 DIVs, 5 SQRTs, 1 EXP
//
// $\sum_{\beta} \vec{F}_{\alpha\beta}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{\beta})$
//
compute_repulsive_effects_from_pedestrians(pedestrians, &p, n_p);
add_repulsive_effects_from_pedestrians(pedestrians, &p, n_p);
// # 192 flops
// # board.n_obstacles * (13 ADDs, 19 MULTs, 4 DIVs, 2 SQRTs, 1 EXP)
//
// $\sum_{B} \vec{F}_{\alpha B}(\vec{e}_{\alpha}, \vec{r}_{\alpha} - \vec{r}_{B})$
//
compute_repulsive_effects_from_borders(board, &p);
add_repulsive_effects_from_borders(board, &p);
}
// Pass2: integrate the forces and update the current state
// Pass 2: 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
// integrate preferred velocity: dw/dt = F (Eq. 10)
// # 2 ADDs, 2 MULTs
//
// $\frac{d\vec{w_\alpha}}{dt} = \vec{F}_{\alpha}(t)$
//
//
p.w = p.w + p.f_i * dt;
// # 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);
}
// Vector2d w_alpha = p.curr_vel + p.f_i * dt; // commented since this was using p.curr_vel instead of p.w (different from paper)
// # 8 flops = 2 MULTs, 2 DIVs, 1 SQRT, 2 MULTs, 1 ADD
// # 1 ADD, 4 MULTs, 1 DIV, 1 SQRT
p.curr_vel = p.w * g(p.v_max, p.w.norm());
// # 4 flops = 2 ADDs, 2 MULTs
// # 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;
// # 10 flops = 5 ADDs, 2 DIVs, 1 SQRT, 2 MULTs
// p.e = (p.destination - p.curr_pos) / (p.destination - p.curr_pos).norm();
// 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.
if (p.color == WHITE && p.curr_pos.x > BOARD_LENGTH_MINUS_ONE_POINT_FIVE) {
// how to handle if a pedestrian reached the end of our simulation/end of the hallway
// The option we do here is to just "teleport" the person back to the beginning
// the good thing is we will have a constant flow of pedestrians and in the simulation we should see how lines build up
if (p.color == WHITE && p.curr_pos.x > (BOARD_LENGTH - 1.5)) {
// basically just keep the y position and reset the x
// keep y, reset x
p.curr_pos.x = 1.0;
p.destination.y = p.curr_pos.y;
p.w = Vector2d(p.desired_v0, 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.curr_pos.x = BOARD_LENGTH_MINUS_ONE;
p.destination.y = p.curr_pos.y;
p.w = Vector2d(-p.desired_v0, 0);
p.curr_vel = p.w;
}
// # 10 flops = 5 ADDs, 2 DIVs, 1 SQRT, 2 MULTs
p.e = (p.destination - p.curr_pos) / (p.destination - p.curr_pos).norm();
// p.e = (p.curr_vel - p.destination) / (p.curr_vel - p.destination).norm();
// p.e = (p.destination + (p.curr_vel / p.curr_vel.norm())) / (p.destination + (p.curr_vel / p.curr_vel.norm())).norm();
const double WALL_EPS = 1e-3;
// # 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) {
p.curr_pos.y = WALL_EPS;
if (p.curr_vel.y < 0)
@@ -180,53 +225,44 @@ int main(int argc, const char *argv[]) {
p.w.y = 0;
}
if (p.curr_pos.y > BOARD_WIDTH) {
p.curr_pos.y = BOARD_WIDTH - WALL_EPS;
p.curr_pos.y = BOARD_WIDTH_MINUS_WALL_EPS;
if (p.curr_vel.y > 0)
p.curr_vel.y = 0;
if (p.w.y > 0)
p.w.y = 0;
}
// if we have a dump path/file we wanna save the data for visualisation
// 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);
}
}
}
long long W = (long long)(8 + 55 * n_p - 55 + 192 + 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";
long double T = stop_tsc(start);
double P = W / T;
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);
std::cout << "P(n): ";
std::cout << P;
std::cout << " flops/cycle\n";
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;
long long W_per_iter = (long long)(8 + 55 * n_p - 55 + 192 + 4 + 8 + 4 + 10) * n_p;
std::cout << "W(n) per iteration: ";
std::cout << W_per_iter;
std::cout << " flops\n";
printf("W(n) per iteration: %.2Lf flops\n", W_per_iter);
printf("Q(n) per iteration: %Ld 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);
printf("P(n): %.2Lf flops/cycle\n", P);
long long Q = 104 * n_p + 32 * 4 + 80;
std::cout << "Q(n) per iteration: ";
std::cout << Q;
std::cout << " bytes\n";
long long I = (long long)(8 + 55 * n_p - 55 + 192 + 4 + 8 + 4 + 10) * n_p / Q;
std::cout << "I(n): ";
std::cout << I;
std::cout << " flops/byte\n";
// now we wanna save the data to a csv
// Export the data to a .csv file
if (dump_path) {
std::ofstream out(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';