From fee012f56cf9a184880b258809fdee791d383fc7 Mon Sep 17 00:00:00 2001 From: Marcel Plch Date: Sat, 9 Nov 2024 21:29:02 +0100 Subject: [PATCH] More threading stuff MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit So far it doesn't do anything, but soon™ it will. --- include/cx.h | 18 ++++++++++++-- src/cx.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++---- src/main.c | 14 +++++------ src/neural.c | 15 ++++++------ src/shader.c | 2 +- src/tensor.c | 1 + 6 files changed, 93 insertions(+), 23 deletions(-) diff --git a/include/cx.h b/include/cx.h index 543c589..ed2f954 100644 --- a/include/cx.h +++ b/include/cx.h @@ -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 *); diff --git a/src/cx.c b/src/cx.c index bb13ca4..289898f 100644 --- a/src/cx.c +++ b/src/cx.c @@ -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 diff --git a/src/main.c b/src/main.c index 4704912..01ef0d8 100644 --- a/src/main.c +++ b/src/main.c @@ -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; } diff --git a/src/neural.c b/src/neural.c index c05f105..90f05e3 100644 --- a/src/neural.c +++ b/src/neural.c @@ -62,15 +62,15 @@ neural_randomize(Neural_Network *self) { for (int i = 0; i < self->layer_count; i++) { nl = self->layers[i]; - 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++) { + rand_vals = malloc(nl->layer_size_next * sizeof(uint64_t)); + fread(rand_vals, sizeof(uint64_t), + nl->layer_size_next, f); for (int k = 0; k < nl->layer_size_next; k++) { nl->neurons[j].synapses[k] = (float)rand_vals[k] / UINT64_MAX; } + free(rand_vals); } - free(rand_vals); } } @@ -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) - model_colorXYZ(model, brightness, 0, 0); + brightness = nl->neurons[i].synapses[k] * 255; + if (brightness) { + model_colorXYZ(model, brightness, 0, 0); + } modelRegistry_register(mr, model); } diff --git a/src/shader.c b/src/shader.c index 6d72a35..c2c0ba1 100644 --- a/src/shader.c +++ b/src/shader.c @@ -33,7 +33,7 @@ compile_code(const char *filepath, const char *code, GLuint ShaderID, GLint *result) { int InfoLogLength; // Compile Shader - printf("Compiling shader : %s\n", filepath); + printf("Compiling shader: %s\n", filepath); glShaderSource(ShaderID, 1, (const char **)&code, NULL); glCompileShader(ShaderID); diff --git a/src/tensor.c b/src/tensor.c index 47a3149..878986a 100644 --- a/src/tensor.c +++ b/src/tensor.c @@ -36,6 +36,7 @@ tensor_fromVertexBuffer(float *buffer, size_t bufsize) { Tensor * tensor_fromNeuralData(Neural_Data *nd) { + return NULL; } Tensor *