More threading stuff

So far it doesn't do anything,
but soon™ it will.
This commit is contained in:
Marcel Plch 2024-11-09 21:29:02 +01:00
parent 0b4cc27331
commit fee012f56c
Signed by: dormouse
GPG key ID: 2CA77596BC4BDFFE
6 changed files with 93 additions and 23 deletions

View file

@ -27,15 +27,28 @@
// Declare common data structures.
typedef struct _cx_thrd {
pthread_t thread;
void *ctx; // Arbitrary thread context
} CX_Thread;
typedef struct _cx_thrgr {
pthread_t *group_manager;
pthread_t *workers;
CX_Thread *group_manager;
CX_Thread **workers;
size_t worker_count;
size_t worker_size;
} CX_ThreadGroup;
typedef struct _cx_ctx {
GLFWwindow *window;
Neural_Network *nn;
CX_ThreadGroup **threads;
GLuint *VertexArrayIDs;
size_t VertexArray_count;
size_t VertexArray_size;
GLuint *programIDs;
size_t ProgramID_count;
size_t ProgramID_size;
} CX_Context;
// Declare functions
@ -44,6 +57,7 @@ CX_Context *cx_context_new(void);
int cx_glinit(GLFWwindow **);
int cx_nninit(Neural_Network **);
int cx_init(CX_Context **);
int cx_run(CX_Context *);

View file

@ -40,7 +40,6 @@ cx_glrender(GLFWwindow *window, GLuint programID,
glGenBuffers(1, &colorbuffer);
for (int i = 0; i < mr->model_count; i++) {
// Allocate the render buffer
// GL uses this to feed the GPU
render_buffer = model_applyTransformations(mr->models[i]);
@ -84,6 +83,7 @@ cx_loadShaders(GLuint *VertexArrayID, GLuint *programID) {
int
cx_glinit(GLFWwindow **window) {
// Initialise GLFW
printf("Initializing OpenGL.\n");
if(!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
@ -126,6 +126,7 @@ cx_glinit(GLFWwindow **window) {
int
cx_nninit(Neural_Network **nn) {
// Allocate a Neural Network
printf("Initializing a neural network.\n");
*nn = neural_new(64, 4, 8);
if(!*nn) {
fprintf(stderr, "Failed to initialize Neural Network.\n");
@ -138,6 +139,27 @@ cx_nninit(Neural_Network **nn) {
return 0;
}
int
cx_init(CX_Context **cx_ctx) {
*cx_ctx = calloc(1, sizeof(CX_Context));
(*cx_ctx)->VertexArrayIDs = calloc(1, sizeof(GLuint));
(*cx_ctx)->VertexArray_count = 0;
(*cx_ctx)->VertexArray_size = 1;
(*cx_ctx)->programIDs = calloc(1, sizeof(GLuint));
(*cx_ctx)->ProgramID_count = 0;
(*cx_ctx)->ProgramID_size = 1;
if (cx_glinit(&(*cx_ctx)->window)) {
return -1;
}
if (cx_nninit(&(*cx_ctx)->nn)) {
return -1;
}
return 0;
}
static int
cx_glrun() {
@ -155,13 +177,47 @@ cx_nnrun(Neural_Network *nn) {
return 0;
}
static CX_Thread *
cx_thread_new(void *(*target)(void *),
void *ctx) {
CX_Thread *self;
int err;
self = malloc(sizeof(CX_Thread));
if (!self) {
goto err;
}
err = pthread_create(&self->thread, NULL, target, ctx);
if (err) {
goto err;
}
self->ctx = ctx;
err:
free(self);
return NULL;
}
static CX_ThreadGroup *
cx_threadGroup_new(void *(*target)(void *),
void *ctx) {
CX_ThreadGroup *self;
self = malloc(sizeof(CX_ThreadGroup));
self->group_manager = cx_thread_new(target, ctx);
self->workers = malloc(8 * sizeof(CX_Thread *));
self->worker_count = 0;
self->worker_size = 8;
return self;
}
int
cx_run(CX_Context *cx_ctx) {
ModelRegistry *mr;
GLuint VertexArrayID;
GLuint programID;
if (cx_loadShaders(&VertexArrayID, &programID)) {
if (cx_loadShaders(cx_ctx->VertexArrayIDs, cx_ctx->programIDs)) {
return -1;
}
@ -176,7 +232,7 @@ cx_run(CX_Context *cx_ctx) {
do {
cx_glrender(cx_ctx->window, programID, mr);
cx_glrender(cx_ctx->window, cx_ctx->programIDs[0], mr);
usleep(1000000/60);
// Check if the ESC key was pressed or the window was closed
} while(glfwGetKey(cx_ctx->window, GLFW_KEY_ESCAPE) != GLFW_PRESS

View file

@ -2,22 +2,20 @@
int
main(void) {
// CX context (Window, neural network, threads.)
CX_Context *cx_ctx;
cx_ctx = calloc(1, sizeof(CX_Context));
GLFWwindow *window;
Neural_Network *nn;
int retval;
if (cx_glinit(&(cx_ctx->window))) {
return -1;
}
if (cx_nninit(&(cx_ctx->nn))) {
if (cx_init(&cx_ctx)) {
return -1;
}
// Do magic
retval = cx_run(cx_ctx);
// Complain about failure
return retval;
}

View file

@ -62,17 +62,17 @@ neural_randomize(Neural_Network *self) {
for (int i = 0; i < self->layer_count; i++) {
nl = self->layers[i];
for (int j = 0; j < nl->layer_size; j++) {
rand_vals = malloc(nl->layer_size_next * sizeof(uint64_t));
fread(rand_vals, sizeof(uint64_t),
nl->layer_size_next, f);
for (int j = 0; j < nl->layer_size; j++) {
for (int k = 0; k < nl->layer_size_next; k++) {
nl->neurons[j].synapses[k] = (float)rand_vals[k] / UINT64_MAX;
}
}
free(rand_vals);
}
}
}
float *
neural_loadData(Neural_Network *self, const char *filename) {
@ -219,9 +219,10 @@ neural_getMesh(Neural_Network *nn, ModelRegistry *mr) {
.001 // girth
);
brightness = nl->neurons[i].synapses[k] <= 1.0 ? nl->neurons[i].synapses[k] * 255 : 255;
if (brightness)
brightness = nl->neurons[i].synapses[k] * 255;
if (brightness) {
model_colorXYZ(model, brightness, 0, 0);
}
modelRegistry_register(mr, model);
}

View file

@ -36,6 +36,7 @@ tensor_fromVertexBuffer(float *buffer, size_t bufsize) {
Tensor *
tensor_fromNeuralData(Neural_Data *nd) {
return NULL;
}
Tensor *