From 3b99d3c4d186c3dcc3617a4e31eef16dcac24398 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Wed, 1 Jun 2022 21:31:32 +0200 Subject: [PATCH] Fix headers --- src/mnist/cuda_utils.cu | 29 ++++++++++++++++++++--------- src/mnist/include/cuda_utils.h | 1 + src/mnist/include/main.h | 27 +++++++++++++++++++++++++++ src/mnist/include/preview.h | 6 ++---- src/mnist/main.c | 17 +++++------------ src/mnist/mnist.c | 1 + src/mnist/neural_network.c | 18 ++++++++++-------- src/mnist/neuron_io.c | 1 + src/mnist/preview.c | 5 +++-- 9 files changed, 70 insertions(+), 35 deletions(-) create mode 100644 src/mnist/include/main.h diff --git a/src/mnist/cuda_utils.cu b/src/mnist/cuda_utils.cu index b1503c3..b074efa 100644 --- a/src/mnist/cuda_utils.cu +++ b/src/mnist/cuda_utils.cu @@ -1,29 +1,40 @@ #include #include +#include "include/cuda_utils.h" int*** copy_images_cuda(int*** images, int nb_images, int width, int height) { int*** images_cuda; - cudaMalloc((int****)&images_cuda, sizeof(int**)*nb_images); - cudaMemcpy((int****)&images_cuda, sizeof(int**)*nb_images, images); + cudaMalloc(&images_cuda, (size_t)sizeof(int**)*nb_images); + cudaMemcpy(images_cuda, &images, (size_t)sizeof(int**)*nb_images, cudaMemcpyHostToDevice); for (int i=0; i < nb_images; i++) { - cudaMalloc((int***)&images_cuda[i], sizeof(int**)*nb_images); - cudaMemcpy((int***)&images_cuda[i], sizeof(int**)*nb_images, images[i]); + cudaMalloc(&images_cuda[i], sizeof(int**)*nb_images); + cudaMemcpy(images_cuda[i], &images[i], sizeof(int**)*nb_images, cudaMemcpyHostToDevice); for (int j=0; j < height; j++) { cudaMalloc((int**)&images_cuda[i][j], sizeof(int*)*width); - cudaMemcpy((int**)&images_cuda[i][j], sizeof(int*)*width, images[i][j]); + cudaMemcpy(images_cuda[i][j], &images[i][j], sizeof(int*)*width, cudaMemcpyHostToDevice); } } return images_cuda; } - - unsigned int* copy_labels_cuda(unsigned int* labels) { unsigned int* labels_cuda; - cudaMalloc((unsigned int**)&labels_cuda, sizeof(labels)); - cudaMemcpy((unsigned int**)&labels_cuda, sizeof(labels), labels); + cudaMalloc(&labels_cuda, (size_t)sizeof(labels)); + cudaMemcpy(labels_cuda, &labels, sizeof(labels), cudaMemcpyHostToDevice); return labels_cuda; +} + + +void check_cuda_compatibility() { + int nDevices; + cudaError_t err = cudaGetDeviceCount(&nDevices); + if (err != cudaSuccess) { + printf("%s\n", cudaGetErrorString(err)); + exit(1); + } else { + printf("CUDA-capable device is detected\n"); + } } \ No newline at end of file diff --git a/src/mnist/include/cuda_utils.h b/src/mnist/include/cuda_utils.h index cce1786..0dd038a 100644 --- a/src/mnist/include/cuda_utils.h +++ b/src/mnist/include/cuda_utils.h @@ -6,5 +6,6 @@ int*** copy_images_cuda(int*** images, int nb_images, int width, int height); unsigned int* copy_labels_cuda(unsigned int* labels); +void check_cuda_compatibility(); #endif \ No newline at end of file diff --git a/src/mnist/include/main.h b/src/mnist/include/main.h new file mode 100644 index 0000000..6a35049 --- /dev/null +++ b/src/mnist/include/main.h @@ -0,0 +1,27 @@ +#ifndef DEF_MAIN_H +#define DEF_MAIN_H + +typedef struct TrainParameters { + Network* network; + int*** images; + int* labels; + int start; + int nb_images; + int height; + int width; + float accuracy; +} TrainParameters; + + +void print_image(unsigned int width, unsigned int height, int** image, float* previsions); +int indice_max(float* tab, int n); +void help(char* call); +void write_image_in_network(int** image, Network* network, int height, int width); +void* train_images(void* parameters); +void train(int epochs, int layers, int neurons, char* recovery, char* image_file, char* label_file, char* out, char* delta, int nb_images_to_process, int start); +float** recognize(char* modele, char* entree); +void print_recognize(char* modele, char* entree, char* sortie); +void test(char* modele, char* fichier_images, char* fichier_labels, bool preview_fails); +int main(int argc, char* argv[]); + +#endif \ No newline at end of file diff --git a/src/mnist/include/preview.h b/src/mnist/include/preview.h index b7be4a3..32f91a8 100644 --- a/src/mnist/include/preview.h +++ b/src/mnist/include/preview.h @@ -6,10 +6,8 @@ #ifndef DEF_PREVIEW_H #define DEF_PREVIEW_H -uint32_t swap_endian(uint32_t val); -void print_image(unsigned int width, unsigned int height, FILE* ptr, int start); -void read_mnist_images(char* filename, unsigned int* labels); -unsigned int* read_mnist_labels(char* filename); +void print_image(unsigned int width, unsigned int height, int** image); +void preview_images(char* images_file, char* labels_file); #endif diff --git a/src/mnist/main.c b/src/mnist/main.c index 7228b25..632386c 100644 --- a/src/mnist/main.c +++ b/src/mnist/main.c @@ -9,25 +9,17 @@ #include "neuron_io.c" #include "mnist.c" +#include "include/main.h" + #define EPOCHS 10 #define BATCHES 100 #ifdef __CUDACC__ +# warning compiling for CUDA compatible device only # include "cuda_utils.cu" # define MAX_CUDA_THREADS 1024 // from NVIDIA documentation #endif -typedef struct TrainParameters { - Network* network; - int*** images; - int* labels; - int start; - int nb_images; - int height; - int width; - float accuracy; -} TrainParameters; - void print_image(unsigned int width, unsigned int height, int** image, float* previsions) { char tab[] = {' ', '.', ':', '%', '#', '\0'}; @@ -140,7 +132,8 @@ void train(int epochs, int layers, int neurons, char* recovery, char* image_file float accuracy; #ifdef __CUDACC__ - printf("Utilisation du GPU\n"); + printf("Testing compatibility...\n"); + check_cuda_compatibility(); int nb_threads = MAX_CUDA_THREADS; #else printf("Pas d'utilisation du GPU\n"); diff --git a/src/mnist/mnist.c b/src/mnist/mnist.c index 8edabb8..34768f3 100644 --- a/src/mnist/mnist.c +++ b/src/mnist/mnist.c @@ -3,6 +3,7 @@ #include #include +#include "include/mnist.h" uint32_t swap_endian(uint32_t val) { val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF); diff --git a/src/mnist/neural_network.c b/src/mnist/neural_network.c index fa9ea2d..9f9024d 100644 --- a/src/mnist/neural_network.c +++ b/src/mnist/neural_network.c @@ -7,6 +7,7 @@ #include #include "include/neuron.h" +#include "include/neural_network.h" // Définit le taux d'apprentissage du réseau neuronal, donc la rapidité d'adaptation du modèle (compris entre 0 et 1) // Cette valeur peut évoluer au fur et à mesure des époques (linéaire c'est mieux) @@ -404,16 +405,16 @@ Network* copy_network_cuda(Network* network) { Neuron* neuron1; Neuron* neuron; - cudaMalloc((void**)&network2, sizeof(Network)); + cudaMalloc(&network2, (size_t)sizeof(Network)); network2->nb_layers = network->nb_layers; - cudaMalloc((void***)&network2->layers, sizeof(Layer*)*network->nb_layers); + cudaMalloc(&network2->layers, (size_t)sizeof(Layer*)*network->nb_layers); for (int i=0; i < network2->nb_layers; i++) { - cudaMalloc((void**)&layer, sizeof(Layer)); + cudaMalloc(&layer, (size_t)sizeof(Layer)); layer->nb_neurons = network->layers[i]->nb_neurons; - cudaMalloc((void***)&layer->neurons, sizeof(Neuron*)*layer->nb_neurons); + cudaMalloc(&layer->neurons, (size_t)sizeof(Neuron*)*layer->nb_neurons); for (int j=0; j < layer->nb_neurons; j++) { - cudaMalloc((void**)neuron, sizeof(Neuron)); + cudaMalloc(&neuron, (size_t)sizeof(Neuron)); neuron1 = network->layers[i]->neurons[j]; neuron->bias = neuron1->bias; @@ -422,9 +423,10 @@ Network* copy_network_cuda(Network* network) { neuron->last_back_bias = neuron1->last_back_bias; if (i != network2->nb_layers-1) { (void)network2->layers[i+1]->nb_neurons; - cudaMalloc((float**)&neuron->weights, sizeof(float)*network->layers[i+1]->nb_neurons); - cudaMalloc((float**)&neuron->back_weights, sizeof(float)*network->layers[i+1]->nb_neurons); - cudaMalloc((float**)&neuron->last_back_weights, sizeof(float)*network->layers[i+1]->nb_neurons); + cudaMalloc(&neuron->weights, (size_t)sizeof(float)*network->layers[i+1]->nb_neurons); + cudaMalloc(&neuron->back_weights, (size_t)sizeof(float)*network->layers[i+1]->nb_neurons); + cudaMalloc(&neuron->last_back_weights, (size_t)sizeof(float)*network->layers[i+1]->nb_neurons); + for (int k=0; k < network->layers[i+1]->nb_neurons; k++) { neuron->weights[k] = neuron1->weights[k]; neuron->back_weights[k] = neuron1->back_weights[k]; diff --git a/src/mnist/neuron_io.c b/src/mnist/neuron_io.c index bce26a2..74a3166 100644 --- a/src/mnist/neuron_io.c +++ b/src/mnist/neuron_io.c @@ -4,6 +4,7 @@ #include #include "include/neuron.h" +#include "include/neuron_io.h" #define MAGIC_NUMBER 2023 #define DELTA_MAGIC_NUMBER 2024 diff --git a/src/mnist/preview.c b/src/mnist/preview.c index 9799ac1..5f5a041 100644 --- a/src/mnist/preview.c +++ b/src/mnist/preview.c @@ -4,6 +4,7 @@ #include #include "mnist.c" +#include "include/preview.h" // Prévisualise un chiffre écrit à la main @@ -11,8 +12,8 @@ void print_image(unsigned int width, unsigned int height, int** image) { char tab[] = {' ', '.', ':', '%', '#', '\0'}; - for (int i=0; i < height; i++) { - for (int j=0; j < width; j++) { + for (int i=0; i < (int)height; i++) { + for (int j=0; j < (int)width; j++) { printf("%c", tab[image[i][j]/52]); } printf("\n");