main: threaded simulation

The actual computation is embarrassingly parallelised - the only part
that requires a mutex is the picking of any two programs due to the
fact that both programs may be affected following a compute block.
This is why we do a uniqueness check in thread_pick.
This commit is contained in:
2026-03-10 22:19:34 +00:00
parent 30f8e65cc2
commit fabd593eff
3 changed files with 115 additions and 39 deletions

View File

@@ -94,18 +94,19 @@ void simulation_draw(struct Simulation *sim)
DrawRectangle(x * CELL_WIDTH, y * CELL_WIDTH, CELL_WIDTH, CELL_WIDTH, DrawRectangle(x * CELL_WIDTH, y * CELL_WIDTH, CELL_WIDTH, CELL_WIDTH,
color); color);
if (i == sim->p1) sv = sv_chop_left(sv, 64);
{
DrawRectangleLines(x * CELL_WIDTH, y * CELL_WIDTH, CELL_WIDTH, CELL_WIDTH,
BLUE);
}
if (i == sim->p1)
{
DrawRectangleLines(x * CELL_WIDTH, y * CELL_WIDTH, CELL_WIDTH, CELL_WIDTH,
RED);
} }
sv = sv_chop_left(sv, 64); for (u64 i = 0; i < THREAD_POOL; ++i)
{
u64 p1 = sim->states[i].p1;
u64 p2 = sim->states[i].p2;
DrawRectangleLines((p1 / GRID_WIDTH) * CELL_WIDTH,
(p1 % GRID_WIDTH) * CELL_WIDTH, CELL_WIDTH, CELL_WIDTH,
BLUE);
DrawRectangleLines((p2 / GRID_WIDTH) * CELL_WIDTH,
(p2 % GRID_WIDTH) * CELL_WIDTH, CELL_WIDTH, CELL_WIDTH,
RED);
} }
} }
@@ -114,16 +115,15 @@ int main(void)
srand(time(NULL)); srand(time(NULL));
struct Simulation sim = {0}; struct Simulation sim = {0};
simulation_init(&sim); simulation_start(&sim);
bool paused = false;
InitWindow(WIDTH, HEIGHT, "CompLife"); InitWindow(WIDTH, HEIGHT, "CompLife");
SetTargetFPS(60); SetTargetFPS(60);
for (size_t ticks = 0; !WindowShouldClose(); ++ticks) for (size_t ticks = 0; !WindowShouldClose(); ++ticks)
{ {
if (IsKeyPressed(KEY_SPACE)) if (IsKeyPressed(KEY_SPACE))
{ {
paused = !paused; sim.paused = !sim.paused;
} }
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{ {
@@ -143,15 +143,12 @@ int main(void)
} }
printf("\n"); printf("\n");
} }
if (!paused)
{
simulation_update(&sim);
}
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK); ClearBackground(BLACK);
simulation_draw(&sim); simulation_draw(&sim);
EndDrawing(); EndDrawing();
} }
simulation_stop(&sim);
CloseWindow(); CloseWindow();
return 0; return 0;
} }

View File

@@ -5,38 +5,103 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "simulation.h" #include "simulation.h"
void simulation_init(struct Simulation *sim) bool any_threads_using(const struct ThreadState *const states, u64 n)
{ {
for (u64 i = 0; i < SIMULATION_SIZE / sizeof(u16); ++i) for (u64 i = 0; i < THREAD_POOL; ++i)
{ {
((u16 *)(sim->buffer))[i] = rand() % UINT16_MAX; if (states[i].p1 == n || states[i].p2 == n)
{
return true;
}
}
return false;
}
void thread_pick(struct ThreadState *state)
{
struct Simulation *sim = state->sim;
while (true)
{
u64 p1 = rand() % (SIMULATION_SIZE / SIZEOF_PROGRAM);
if (any_threads_using(sim->states, p1))
{
continue;
}
u64 p2 = rand() % (SIMULATION_SIZE / SIZEOF_PROGRAM);
while (p1 * 8 <= ((p2 * 8) + SIZEOF_PROGRAM) &&
p2 * 8 <= ((p1 * 8) + SIZEOF_PROGRAM))
{
p2 = rand() % (SIMULATION_SIZE / SIZEOF_PROGRAM);
}
if (any_threads_using(sim->states, p2))
{
continue;
}
state->p1 = p1;
state->p2 = p2;
break;
} }
} }
void simulation_pick(struct Simulation *sim) const struct timespec THREAD_DEFAULT_SLEEP = {.tv_sec = 1};
{
sim->p1 = rand() % (SIMULATION_SIZE / SIZEOF_PROGRAM);
sim->p2 = rand() % (SIMULATION_SIZE / SIZEOF_PROGRAM);
while (sim->p1 * 8 <= ((sim->p2 * 8) + SIZEOF_PROGRAM) &&
sim->p2 * 8 <= ((sim->p1 * 8) + SIZEOF_PROGRAM))
{
sim->p2 = rand() % (SIMULATION_SIZE / SIZEOF_PROGRAM);
}
}
void simulation_update(struct Simulation *sim) int thread_update(void *rawptr)
{ {
simulation_pick(sim); struct ThreadState *state = rawptr;
sv_t a = SV(sim->buffer + (sim->p1 * SIZEOF_PROGRAM), 64); struct Simulation *simulation = state->sim;
sv_t b = SV(sim->buffer + (sim->p2 * SIZEOF_PROGRAM), 64);
while (!simulation->stopped)
{
while (simulation->paused)
{
thrd_sleep(&THREAD_DEFAULT_SLEEP, NULL);
}
// We only need to lock when picking the programs
mtx_lock(&simulation->mutex);
thread_pick(state);
mtx_unlock(&simulation->mutex);
sv_t a = SV(simulation->buffer + (state->p1 * SIZEOF_PROGRAM), 64);
sv_t b = SV(simulation->buffer + (state->p2 * SIZEOF_PROGRAM), 64);
struct ProgramConcat prog_concat = {0}; struct ProgramConcat prog_concat = {0};
program_concat(&prog_concat, a, b); program_concat(&prog_concat, a, b);
program_execute(&prog_concat); program_execute(&prog_concat);
program_split(&prog_concat); program_split(&prog_concat);
}
return 0;
}
void simulation_start(struct Simulation *sim)
{
for (u64 i = 0; i < SIMULATION_SIZE / sizeof(u16); ++i)
{
((u16 *)(sim->buffer))[i] = rand() % UINT16_MAX;
}
sim->stopped = false;
sim->paused = true;
mtx_init(&sim->mutex, mtx_plain);
for (u64 i = 0; i < THREAD_POOL; ++i)
{
memset(&sim->states[i], 0, sizeof(sim->states[i]));
sim->states[i].sim = sim;
thrd_create(&sim->threads[i], thread_update, &sim->states[i]);
}
}
void simulation_stop(struct Simulation *sim)
{
sim->stopped = true;
for (u64 i = 0; i < THREAD_POOL; ++i)
{
thrd_join(sim->threads[i], NULL);
}
} }
/* Copyright (C) 2026 Aryadev Chavali /* Copyright (C) 2026 Aryadev Chavali

View File

@@ -9,21 +9,35 @@
#define SIMULATION_H #define SIMULATION_H
#include "program_iter.h" #include "program_iter.h"
#include <stdatomic.h>
#include <threads.h>
#define NUM_PROGRAMS_POW_2 10 #define NUM_PROGRAMS_POW_2 10
#define NUM_PROGRAMS (1LU << NUM_PROGRAMS_POW_2) #define NUM_PROGRAMS (1LU << NUM_PROGRAMS_POW_2)
#define SIMULATION_SIZE (SIZEOF_PROGRAM * NUM_PROGRAMS) #define SIMULATION_SIZE (SIZEOF_PROGRAM * NUM_PROGRAMS)
#define THREAD_POOL 8
struct ThreadState
{
u64 p1, p2;
void *sim;
};
// Simulation is simply a massive tape. We pick two 64 byte portions of this // Simulation is simply a massive tape. We pick two 64 byte portions of this
// buffer for our update procedure, which is based on program_iter. // buffer for our update procedure, which is based on program_iter.
struct Simulation struct Simulation
{ {
char buffer[SIMULATION_SIZE]; char buffer[SIMULATION_SIZE];
u64 p1, p2;
bool paused, stopped;
mtx_t mutex;
thrd_t threads[THREAD_POOL];
struct ThreadState states[THREAD_POOL];
}; };
void simulation_init(struct Simulation *sim); void simulation_start(struct Simulation *sim);
void simulation_update(struct Simulation *sim); void simulation_stop(struct Simulation *sim);
#endif #endif