From 11ce67f845772686e81538a4881d155da398a382 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Wed, 23 Nov 2022 10:41:19 +0100 Subject: [PATCH] Update multithreading --- src/cnn/creation.c | 4 ++-- src/cnn/train.c | 8 +++++--- src/cnn/utils.c | 14 ++++++++++++++ src/mnist/main.c | 2 +- test/cnn_neuron_io.c | 6 ++++++ test/cnn_utils.c | 3 +++ 6 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/cnn/creation.c b/src/cnn/creation.c index d068e5f..79d27e7 100644 --- a/src/cnn/creation.c +++ b/src/cnn/creation.c @@ -28,8 +28,8 @@ Network* create_network(int max_size, float learning_rate, int dropout, int init network->depth[0] = input_depth; network->kernel[0]->nn = NULL; network->kernel[0]->cnn = NULL; - create_a_cube_input_layer(network, 0, input_depth, input_dim); - network->input_z[0] = NULL; // As we don't backpropagate the input + create_a_cube_input_layer(network, 0, input_depth, input_dim); + create_a_cube_input_z_layer(network, 0, input_depth, input_dim); return network; } diff --git a/src/cnn/train.c b/src/cnn/train.c index bc4dfec..a5bcf02 100644 --- a/src/cnn/train.c +++ b/src/cnn/train.c @@ -193,13 +193,15 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di } train_parameters[k]->network = copy_network(network); train_parameters[k]->start = BATCHES*j + (nb_images_total/BATCHES)*k; - pthread_create( &tid[j], NULL, train_thread, (void*) train_parameters[k]); + pthread_create( &tid[k], NULL, train_thread, (void*) train_parameters[k]); } for (int k=0; k < nb_threads; k++) { // On attend la terminaison de chaque thread un à un - pthread_join( tid[j], NULL ); + pthread_join( tid[k], NULL ); accuracy += train_parameters[k]->accuracy / (float) nb_images_total; - // TODO patch_network(network, train_parameters[k]->network, train_parameters[k]->nb_images); + + update_weights(network, train_parameters[k]->network); + update_bias(network, train_parameters[k]->network); free_network(train_parameters[k]->network); } current_accuracy = accuracy * nb_images_total/((j+1)*BATCHES); diff --git a/src/cnn/utils.c b/src/cnn/utils.c index 257766f..9a5bf55 100644 --- a/src/cnn/utils.c +++ b/src/cnn/utils.c @@ -212,5 +212,19 @@ Network* copy_network(Network* network) { } } + network_cp->input_z = (float****)malloc(sizeof(float***)*size); + for (int i=0; i < size; i++) { // input_z[size][couche->depth][couche->dim][couche->dim] + network_cp->input_z[i] = (float***)malloc(sizeof(float**)*network->depth[i]); + for (int j=0; j < network->depth[i]; j++) { + network_cp->input_z[i][j] = (float**)malloc(sizeof(float*)*network->width[i]); + for (int k=0; k < network->width[i]; k++) { + network_cp->input_z[i][j][k] = (float*)malloc(sizeof(float)*network->width[i]); + for (int l=0; l < network->width[i]; l++) { + network_cp->input_z[i][j][k][l] = 0.; + } + } + } + } + return network_cp; } \ No newline at end of file diff --git a/src/mnist/main.c b/src/mnist/main.c index e9a1c5b..9d4cb57 100644 --- a/src/mnist/main.c +++ b/src/mnist/main.c @@ -210,7 +210,7 @@ void train(int epochs, int layers, int neurons, char* recovery, char* image_file pthread_create( &tid[j], NULL, train_thread, (void*) train_parameters[j]); } for(int j=0; j < nb_threads; j++ ) { - // On join les threads créés sur le CPU + // On récupère les threads créés sur le CPU pthread_join( tid[j], NULL ); accuracy += train_parameters[j]->accuracy / (float) nb_images_total; diff --git a/test/cnn_neuron_io.c b/test/cnn_neuron_io.c index 3588e16..8c54e9a 100644 --- a/test/cnn_neuron_io.c +++ b/test/cnn_neuron_io.c @@ -7,6 +7,7 @@ #include "../src/cnn/include/neuron_io.h" #include "../src/cnn/include/creation.h" #include "../src/cnn/include/utils.h" +#include "../src/cnn/include/free.h" #include "../src/include/colors.h" @@ -30,5 +31,10 @@ int main() { } printf(GREEN "OK\n" RESET); + printf("Libération de la mémoire\n"); + free_network(network); + free_network(network2); + printf(GREEN "OK\n" RESET); + return 0; } \ No newline at end of file diff --git a/test/cnn_utils.c b/test/cnn_utils.c index dce732d..2bc7c04 100644 --- a/test/cnn_utils.c +++ b/test/cnn_utils.c @@ -4,6 +4,7 @@ #include "../src/include/colors.h" #include "../src/cnn/include/creation.h" #include "../src/cnn/include/utils.h" +#include "../src/cnn/include/free.h" #include "../src/include/colors.h" int main() { @@ -22,5 +23,7 @@ int main() { } printf(GREEN "OK\n" RESET); + free_network(network_cp); + free_network(network); return 0; } \ No newline at end of file