From 4d95b4b51970662057dd8bae94f3d45983c0f60c Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Fri, 30 Oct 2020 09:18:53 -0500 Subject: [PATCH] performance improvements; utilizing more of the threads time --- include/utils.h | 2 +- src/sim.cpp | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/include/utils.h b/include/utils.h index 604edbf..6481b97 100644 --- a/include/utils.h +++ b/include/utils.h @@ -80,7 +80,7 @@ struct AABB bool intersects(AABB other) const; }; -#define QUAD_TREE_POINTS_PER_NODE 4 +#define QUAD_TREE_POINTS_PER_NODE 8 template struct QuadTree { diff --git a/src/sim.cpp b/src/sim.cpp index 00b4f1a..f718c3a 100644 --- a/src/sim.cpp +++ b/src/sim.cpp @@ -27,10 +27,10 @@ #define WINDOW_TITLE "N-Body Simulation" // :ArbitraryConstant -#define PARTICLE_COUNT 3000 +#define PARTICLE_COUNT 2500 // :ArbitraryConstant -#define NUM_THREADS 2 +#define NUM_THREADS 4 // TODO(Brendan): Maybe this can be removed because it isn't really necessary? internal void @@ -170,6 +170,12 @@ thread_start(void* data) pthread_barrier_wait(&thread_sync_barrier); update_bodies_partial(state, thread_id + 1); pthread_barrier_wait(&thread_sync_barrier); + if (thread_id == 0) + { + state->qt_bodies.init(AABB { -2000, -2000, 4000, 4000 }); + state->qt_body_allocator.reset(); + For (state->bodies) state->qt_bodies.insert(&it, &state->qt_body_allocator); + } pthread_barrier_wait(&thread_sync_barrier); } @@ -186,7 +192,7 @@ sim_state_init(SimState* state) foreach (i, 0, PARTICLE_COUNT) { Body tmp_body; - tmp_body.pos = V2f{ randf(0, 1200), randf(0, 1200) }; + tmp_body.pos = V2f{ randf(-1000, 1000), randf(-1000, 1000) }; tmp_body.vel = V2f{ 0.0f, 0.0f }; tmp_body.mass = randf(3.0f, 6.0f); tmp_body.body_type = static_cast ((rand() % 4)); @@ -194,6 +200,7 @@ sim_state_init(SimState* state) } state->qt_body_allocator.init(PARTICLE_COUNT); + state->qt_bodies.init(AABB { -2000, -2000, 4000, 4000 }); state->camera.scale = 1.0f; @@ -219,12 +226,9 @@ update(SimState* state, f64 dt) if (glfwGetKey(window, GLFW_KEY_DOWN)) state->camera.offset.y += camera_move_speed; if (glfwGetKey(window, GLFW_KEY_LEFT)) state->camera.offset.x -= camera_move_speed; if (glfwGetKey(window, GLFW_KEY_RIGHT)) state->camera.offset.x += camera_move_speed; - if (glfwGetKey(window, GLFW_KEY_Q)) state->camera.scale *= 1.01f; - if (glfwGetKey(window, GLFW_KEY_A)) state->camera.scale /= 1.01f; + if (glfwGetKey(window, GLFW_KEY_Q)) state->camera.scale *= 1.02f; + if (glfwGetKey(window, GLFW_KEY_A)) state->camera.scale /= 1.02f; - state->qt_bodies.init(AABB { -2000, -2000, 4000, 4000 }); - state->qt_body_allocator.reset(); - For (state->bodies) state->qt_bodies.insert(&it, &state->qt_body_allocator); pthread_barrier_wait(&thread_sync_barrier); update_bodies_partial(state, 0); -- 2.25.1