More threading stuff
So far it doesn't do anything, but soon™ it will.
This commit is contained in:
parent
0b4cc27331
commit
fee012f56c
6 changed files with 93 additions and 23 deletions
18
include/cx.h
18
include/cx.h
|
@ -27,15 +27,28 @@
|
||||||
|
|
||||||
// Declare common data structures.
|
// Declare common data structures.
|
||||||
|
|
||||||
|
typedef struct _cx_thrd {
|
||||||
|
pthread_t thread;
|
||||||
|
void *ctx; // Arbitrary thread context
|
||||||
|
} CX_Thread;
|
||||||
|
|
||||||
typedef struct _cx_thrgr {
|
typedef struct _cx_thrgr {
|
||||||
pthread_t *group_manager;
|
CX_Thread *group_manager;
|
||||||
pthread_t *workers;
|
CX_Thread **workers;
|
||||||
|
size_t worker_count;
|
||||||
|
size_t worker_size;
|
||||||
} CX_ThreadGroup;
|
} CX_ThreadGroup;
|
||||||
|
|
||||||
typedef struct _cx_ctx {
|
typedef struct _cx_ctx {
|
||||||
GLFWwindow *window;
|
GLFWwindow *window;
|
||||||
Neural_Network *nn;
|
Neural_Network *nn;
|
||||||
CX_ThreadGroup **threads;
|
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;
|
} CX_Context;
|
||||||
|
|
||||||
// Declare functions
|
// Declare functions
|
||||||
|
@ -44,6 +57,7 @@ CX_Context *cx_context_new(void);
|
||||||
|
|
||||||
int cx_glinit(GLFWwindow **);
|
int cx_glinit(GLFWwindow **);
|
||||||
int cx_nninit(Neural_Network **);
|
int cx_nninit(Neural_Network **);
|
||||||
|
int cx_init(CX_Context **);
|
||||||
|
|
||||||
int cx_run(CX_Context *);
|
int cx_run(CX_Context *);
|
||||||
|
|
||||||
|
|
66
src/cx.c
66
src/cx.c
|
@ -40,7 +40,6 @@ cx_glrender(GLFWwindow *window, GLuint programID,
|
||||||
glGenBuffers(1, &colorbuffer);
|
glGenBuffers(1, &colorbuffer);
|
||||||
|
|
||||||
for (int i = 0; i < mr->model_count; i++) {
|
for (int i = 0; i < mr->model_count; i++) {
|
||||||
|
|
||||||
// Allocate the render buffer
|
// Allocate the render buffer
|
||||||
// GL uses this to feed the GPU
|
// GL uses this to feed the GPU
|
||||||
render_buffer = model_applyTransformations(mr->models[i]);
|
render_buffer = model_applyTransformations(mr->models[i]);
|
||||||
|
@ -84,6 +83,7 @@ cx_loadShaders(GLuint *VertexArrayID, GLuint *programID) {
|
||||||
int
|
int
|
||||||
cx_glinit(GLFWwindow **window) {
|
cx_glinit(GLFWwindow **window) {
|
||||||
// Initialise GLFW
|
// Initialise GLFW
|
||||||
|
printf("Initializing OpenGL.\n");
|
||||||
if(!glfwInit()) {
|
if(!glfwInit()) {
|
||||||
fprintf(stderr, "Failed to initialize GLFW\n");
|
fprintf(stderr, "Failed to initialize GLFW\n");
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -126,6 +126,7 @@ cx_glinit(GLFWwindow **window) {
|
||||||
int
|
int
|
||||||
cx_nninit(Neural_Network **nn) {
|
cx_nninit(Neural_Network **nn) {
|
||||||
// Allocate a Neural Network
|
// Allocate a Neural Network
|
||||||
|
printf("Initializing a neural network.\n");
|
||||||
*nn = neural_new(64, 4, 8);
|
*nn = neural_new(64, 4, 8);
|
||||||
if(!*nn) {
|
if(!*nn) {
|
||||||
fprintf(stderr, "Failed to initialize Neural Network.\n");
|
fprintf(stderr, "Failed to initialize Neural Network.\n");
|
||||||
|
@ -138,6 +139,27 @@ cx_nninit(Neural_Network **nn) {
|
||||||
return 0;
|
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
|
static int
|
||||||
cx_glrun() {
|
cx_glrun() {
|
||||||
|
|
||||||
|
@ -155,13 +177,47 @@ cx_nnrun(Neural_Network *nn) {
|
||||||
return 0;
|
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
|
int
|
||||||
cx_run(CX_Context *cx_ctx) {
|
cx_run(CX_Context *cx_ctx) {
|
||||||
ModelRegistry *mr;
|
ModelRegistry *mr;
|
||||||
GLuint VertexArrayID;
|
|
||||||
GLuint programID;
|
|
||||||
|
|
||||||
if (cx_loadShaders(&VertexArrayID, &programID)) {
|
if (cx_loadShaders(cx_ctx->VertexArrayIDs, cx_ctx->programIDs)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,7 +232,7 @@ cx_run(CX_Context *cx_ctx) {
|
||||||
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
cx_glrender(cx_ctx->window, programID, mr);
|
cx_glrender(cx_ctx->window, cx_ctx->programIDs[0], mr);
|
||||||
usleep(1000000/60);
|
usleep(1000000/60);
|
||||||
// Check if the ESC key was pressed or the window was closed
|
// Check if the ESC key was pressed or the window was closed
|
||||||
} while(glfwGetKey(cx_ctx->window, GLFW_KEY_ESCAPE) != GLFW_PRESS
|
} while(glfwGetKey(cx_ctx->window, GLFW_KEY_ESCAPE) != GLFW_PRESS
|
||||||
|
|
14
src/main.c
14
src/main.c
|
@ -2,22 +2,20 @@
|
||||||
|
|
||||||
int
|
int
|
||||||
main(void) {
|
main(void) {
|
||||||
|
// CX context (Window, neural network, threads.)
|
||||||
CX_Context *cx_ctx;
|
CX_Context *cx_ctx;
|
||||||
cx_ctx = calloc(1, sizeof(CX_Context));
|
|
||||||
GLFWwindow *window;
|
|
||||||
Neural_Network *nn;
|
|
||||||
|
|
||||||
int retval;
|
int retval;
|
||||||
|
|
||||||
if (cx_glinit(&(cx_ctx->window))) {
|
|
||||||
return -1;
|
if (cx_init(&cx_ctx)) {
|
||||||
}
|
|
||||||
|
|
||||||
if (cx_nninit(&(cx_ctx->nn))) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Do magic
|
||||||
retval = cx_run(cx_ctx);
|
retval = cx_run(cx_ctx);
|
||||||
|
|
||||||
|
// Complain about failure
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
15
src/neural.c
15
src/neural.c
|
@ -62,15 +62,15 @@ neural_randomize(Neural_Network *self) {
|
||||||
|
|
||||||
for (int i = 0; i < self->layer_count; i++) {
|
for (int i = 0; i < self->layer_count; i++) {
|
||||||
nl = self->layers[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++) {
|
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++) {
|
for (int k = 0; k < nl->layer_size_next; k++) {
|
||||||
nl->neurons[j].synapses[k] = (float)rand_vals[k] / UINT64_MAX;
|
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
|
.001 // girth
|
||||||
);
|
);
|
||||||
brightness = nl->neurons[i].synapses[k] <= 1.0 ? nl->neurons[i].synapses[k] * 255 : 255;
|
brightness = nl->neurons[i].synapses[k] * 255;
|
||||||
if (brightness)
|
if (brightness) {
|
||||||
model_colorXYZ(model, brightness, 0, 0);
|
model_colorXYZ(model, brightness, 0, 0);
|
||||||
|
}
|
||||||
modelRegistry_register(mr, model);
|
modelRegistry_register(mr, model);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ compile_code(const char *filepath, const char *code,
|
||||||
GLuint ShaderID, GLint *result) {
|
GLuint ShaderID, GLint *result) {
|
||||||
int InfoLogLength;
|
int InfoLogLength;
|
||||||
// Compile Shader
|
// Compile Shader
|
||||||
printf("Compiling shader : %s\n", filepath);
|
printf("Compiling shader: %s\n", filepath);
|
||||||
glShaderSource(ShaderID, 1, (const char **)&code, NULL);
|
glShaderSource(ShaderID, 1, (const char **)&code, NULL);
|
||||||
glCompileShader(ShaderID);
|
glCompileShader(ShaderID);
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,7 @@ tensor_fromVertexBuffer(float *buffer, size_t bufsize) {
|
||||||
|
|
||||||
Tensor *
|
Tensor *
|
||||||
tensor_fromNeuralData(Neural_Data *nd) {
|
tensor_fromNeuralData(Neural_Data *nd) {
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Tensor *
|
Tensor *
|
||||||
|
|
Loading…
Reference in a new issue