From 0b4cc273314efeb71e70e8a94cd5757ea66e85e6 Mon Sep 17 00:00:00 2001 From: Marcel Plch Date: Thu, 7 Nov 2024 22:07:33 +0100 Subject: [PATCH] Implement basis for thread management. --- CMakeLists.txt | 3 ++- include/cx.h | 17 ++++++++++++++++- src/cx.c | 12 ++++++------ src/main.c | 16 ++++++---------- src/model.c | 6 +++--- src/neural.c | 1 + src/tensor.c | 4 ++++ 7 files changed, 38 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 802a49e..b97f9b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ # CMake entry point -cmake_minimum_required (VERSION 3.27.1) +cmake_minimum_required (VERSION 3.30.5) project(CX C) cmake_policy(SET CMP0072 NEW) @@ -17,6 +17,7 @@ set(ALL_LIBS glfw GLEW m + pthread ) set(CMAKE_C_FLAGS "-O0 -ggdb -Wall") diff --git a/include/cx.h b/include/cx.h index cbcdf10..543c589 100644 --- a/include/cx.h +++ b/include/cx.h @@ -9,6 +9,7 @@ #include #include #include +#include // Include GLEW #include @@ -24,13 +25,27 @@ #include #include +// Declare common data structures. + +typedef struct _cx_thrgr { + pthread_t *group_manager; + pthread_t *workers; +} CX_ThreadGroup; + +typedef struct _cx_ctx { + GLFWwindow *window; + Neural_Network *nn; + CX_ThreadGroup **threads; +} CX_Context; + // Declare functions +CX_Context *cx_context_new(void); int cx_glinit(GLFWwindow **); int cx_nninit(Neural_Network **); -int cx_run(GLFWwindow *, Neural_Network *); +int cx_run(CX_Context *); #endif diff --git a/src/cx.c b/src/cx.c index c17c6f0..bb13ca4 100644 --- a/src/cx.c +++ b/src/cx.c @@ -156,7 +156,7 @@ cx_nnrun(Neural_Network *nn) { } int -cx_run(GLFWwindow *window, Neural_Network *nn) { +cx_run(CX_Context *cx_ctx) { ModelRegistry *mr; GLuint VertexArrayID; GLuint programID; @@ -168,19 +168,19 @@ cx_run(GLFWwindow *window, Neural_Network *nn) { // Establish a model registry mr = modelRegistry_new(); // Fill the model registry with mesh models - neural_getMesh(nn, mr); + neural_getMesh(cx_ctx->nn, mr); // Remainder from cursor experiments, might be useful later double xpos, ypos; - glfwGetCursorPos(window, &xpos, &ypos); + glfwGetCursorPos(cx_ctx->window, &xpos, &ypos); do { - cx_glrender(window, programID, mr); + cx_glrender(cx_ctx->window, programID, mr); usleep(1000000/60); // Check if the ESC key was pressed or the window was closed - } while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS - && !glfwWindowShouldClose(window)); + } while(glfwGetKey(cx_ctx->window, GLFW_KEY_ESCAPE) != GLFW_PRESS + && !glfwWindowShouldClose(cx_ctx->window)); // Close OpenGL window and terminate GLFW glfwTerminate(); diff --git a/src/main.c b/src/main.c index 773058d..4704912 100644 --- a/src/main.c +++ b/src/main.c @@ -1,27 +1,23 @@ -// Include standard headers -#include -#include - - -// Include project headers #include - int main(void) { + CX_Context *cx_ctx; + cx_ctx = calloc(1, sizeof(CX_Context)); GLFWwindow *window; Neural_Network *nn; + int retval; - if (cx_glinit(&window)) { + if (cx_glinit(&(cx_ctx->window))) { return -1; } - if (cx_nninit(&nn)) { + if (cx_nninit(&(cx_ctx->nn))) { return -1; } - retval = cx_run(window, nn); + retval = cx_run(cx_ctx); return retval; } diff --git a/src/model.c b/src/model.c index cc1a2e0..c870c50 100644 --- a/src/model.c +++ b/src/model.c @@ -3,10 +3,10 @@ Model * model_new(size_t size) { Model *self = calloc(1, sizeof(Model)); - self->object = calloc((size ? size : 1) *4 , sizeof(GLfloat)); - self->colors = calloc((size ? size : 1) *3 , sizeof(GLfloat)); + self->object = calloc((size ? size : 1) * 4 , sizeof(GLfloat)); + self->colors = calloc((size ? size : 1) * 3 , sizeof(GLfloat)); self->bufsize = size; - self->transformations = calloc(8 , sizeof(Tensor *)); + self->transformations = calloc(8, sizeof(Tensor *)); self->transformation_size = 8; self->transformation_count = 0; return self; diff --git a/src/neural.c b/src/neural.c index 22246dd..c05f105 100644 --- a/src/neural.c +++ b/src/neural.c @@ -28,6 +28,7 @@ neural_new(size_t input_size, size_t output_size, size_t layer_count) { // Failed to allocate. return NULL; } + // The difference between layer sizes, hidden layers step between the two // sizes in linear fashion. ssize_t layer_diff; diff --git a/src/tensor.c b/src/tensor.c index 2073c25..47a3149 100644 --- a/src/tensor.c +++ b/src/tensor.c @@ -34,6 +34,10 @@ tensor_fromVertexBuffer(float *buffer, size_t bufsize) { return mat; } +Tensor * +tensor_fromNeuralData(Neural_Data *nd) { +} + Tensor * tensor_multip(Tensor *mat2, Tensor *mat1) { Tensor *result;