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:
101
src/simulation.c
101
src/simulation.c
@@ -5,38 +5,103 @@
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.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 < THREAD_POOL; ++i)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
const struct timespec THREAD_DEFAULT_SLEEP = {.tv_sec = 1};
|
||||
|
||||
int thread_update(void *rawptr)
|
||||
{
|
||||
struct ThreadState *state = rawptr;
|
||||
struct Simulation *simulation = state->sim;
|
||||
|
||||
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};
|
||||
program_concat(&prog_concat, a, b);
|
||||
program_execute(&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;
|
||||
}
|
||||
}
|
||||
|
||||
void simulation_pick(struct Simulation *sim)
|
||||
{
|
||||
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->stopped = false;
|
||||
sim->paused = true;
|
||||
mtx_init(&sim->mutex, mtx_plain);
|
||||
for (u64 i = 0; i < THREAD_POOL; ++i)
|
||||
{
|
||||
sim->p2 = rand() % (SIMULATION_SIZE / SIZEOF_PROGRAM);
|
||||
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_update(struct Simulation *sim)
|
||||
void simulation_stop(struct Simulation *sim)
|
||||
{
|
||||
simulation_pick(sim);
|
||||
sv_t a = SV(sim->buffer + (sim->p1 * SIZEOF_PROGRAM), 64);
|
||||
sv_t b = SV(sim->buffer + (sim->p2 * SIZEOF_PROGRAM), 64);
|
||||
|
||||
struct ProgramConcat prog_concat = {0};
|
||||
program_concat(&prog_concat, a, b);
|
||||
program_execute(&prog_concat);
|
||||
program_split(&prog_concat);
|
||||
sim->stopped = true;
|
||||
for (u64 i = 0; i < THREAD_POOL; ++i)
|
||||
{
|
||||
thrd_join(sim->threads[i], NULL);
|
||||
}
|
||||
}
|
||||
|
||||
/* Copyright (C) 2026 Aryadev Chavali
|
||||
|
||||
Reference in New Issue
Block a user