Added measurements

This commit is contained in:
Nguyen Quy Anh
2026-04-24 16:31:42 +02:00
parent aa1daf9149
commit 3d217e7840
7 changed files with 113 additions and 55 deletions
+13 -2
View File
@@ -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 {
+21 -1
View File
@@ -1,6 +1,7 @@
#include "forces.hpp"
#include <math.h>
// # 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;
}
}
Executable
BIN
View File
Binary file not shown.
+30
View File
@@ -2,7 +2,9 @@
#include "forces.hpp"
#include "math.h"
#include "pedestrian.hpp"
#include "tsc_x86.h"
#include "vector2d.hpp"
#include <iostream>
#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;
}
+6
View File
@@ -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.
+31 -42
View File
@@ -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
#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 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,53 +27,43 @@
* 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
}
@@ -91,4 +81,3 @@ static myInt64 stop_tsc(myInt64 start) {
CPUID();
return COUNTER_VAL(end) - start;
}
+2
View File
@@ -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;