performance improvements; utilizing more of the threads time
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 30 Oct 2020 14:18:53 +0000 (09:18 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 30 Oct 2020 14:18:53 +0000 (09:18 -0500)
include/utils.h
src/sim.cpp

index 604edbf4541e3248d561c2050e733af7b1e11128..6481b97222cec7caefaaff9591027cd4767cb4bf 100644 (file)
@@ -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 <typename T>
 struct QuadTree
 {
index 00b4f1a39202f3152da290bfcb5c57908077fbf5..f718c3ab85461644111555133334c92b219de32b 100644 (file)
 #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<BodyType> ((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);