Fix headers

This commit is contained in:
augustin64 2022-06-01 21:31:32 +02:00
parent bae59ceef0
commit 3b99d3c4d1
9 changed files with 70 additions and 35 deletions

View File

@ -1,29 +1,40 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "include/cuda_utils.h"
int*** copy_images_cuda(int*** images, int nb_images, int width, int height) { int*** copy_images_cuda(int*** images, int nb_images, int width, int height) {
int*** images_cuda; int*** images_cuda;
cudaMalloc((int****)&images_cuda, sizeof(int**)*nb_images); cudaMalloc(&images_cuda, (size_t)sizeof(int**)*nb_images);
cudaMemcpy((int****)&images_cuda, sizeof(int**)*nb_images, images); cudaMemcpy(images_cuda, &images, (size_t)sizeof(int**)*nb_images, cudaMemcpyHostToDevice);
for (int i=0; i < nb_images; i++) { for (int i=0; i < nb_images; i++) {
cudaMalloc((int***)&images_cuda[i], sizeof(int**)*nb_images); cudaMalloc(&images_cuda[i], sizeof(int**)*nb_images);
cudaMemcpy((int***)&images_cuda[i], sizeof(int**)*nb_images, images[i]); cudaMemcpy(images_cuda[i], &images[i], sizeof(int**)*nb_images, cudaMemcpyHostToDevice);
for (int j=0; j < height; j++) { for (int j=0; j < height; j++) {
cudaMalloc((int**)&images_cuda[i][j], sizeof(int*)*width); 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; return images_cuda;
} }
unsigned int* copy_labels_cuda(unsigned int* labels) { unsigned int* copy_labels_cuda(unsigned int* labels) {
unsigned int* labels_cuda; unsigned int* labels_cuda;
cudaMalloc((unsigned int**)&labels_cuda, sizeof(labels)); cudaMalloc(&labels_cuda, (size_t)sizeof(labels));
cudaMemcpy((unsigned int**)&labels_cuda, sizeof(labels), labels); cudaMemcpy(labels_cuda, &labels, sizeof(labels), cudaMemcpyHostToDevice);
return labels_cuda; 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");
}
} }

View File

@ -6,5 +6,6 @@
int*** copy_images_cuda(int*** images, int nb_images, int width, int height); int*** copy_images_cuda(int*** images, int nb_images, int width, int height);
unsigned int* copy_labels_cuda(unsigned int* labels); unsigned int* copy_labels_cuda(unsigned int* labels);
void check_cuda_compatibility();
#endif #endif

27
src/mnist/include/main.h Normal file
View File

@ -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

View File

@ -6,10 +6,8 @@
#ifndef DEF_PREVIEW_H #ifndef DEF_PREVIEW_H
#define DEF_PREVIEW_H #define DEF_PREVIEW_H
uint32_t swap_endian(uint32_t val); void print_image(unsigned int width, unsigned int height, int** image);
void print_image(unsigned int width, unsigned int height, FILE* ptr, int start); void preview_images(char* images_file, char* labels_file);
void read_mnist_images(char* filename, unsigned int* labels);
unsigned int* read_mnist_labels(char* filename);
#endif #endif

View File

@ -9,25 +9,17 @@
#include "neuron_io.c" #include "neuron_io.c"
#include "mnist.c" #include "mnist.c"
#include "include/main.h"
#define EPOCHS 10 #define EPOCHS 10
#define BATCHES 100 #define BATCHES 100
#ifdef __CUDACC__ #ifdef __CUDACC__
# warning compiling for CUDA compatible device only
# include "cuda_utils.cu" # include "cuda_utils.cu"
# define MAX_CUDA_THREADS 1024 // from NVIDIA documentation # define MAX_CUDA_THREADS 1024 // from NVIDIA documentation
#endif #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) { void print_image(unsigned int width, unsigned int height, int** image, float* previsions) {
char tab[] = {' ', '.', ':', '%', '#', '\0'}; char tab[] = {' ', '.', ':', '%', '#', '\0'};
@ -140,7 +132,8 @@ void train(int epochs, int layers, int neurons, char* recovery, char* image_file
float accuracy; float accuracy;
#ifdef __CUDACC__ #ifdef __CUDACC__
printf("Utilisation du GPU\n"); printf("Testing compatibility...\n");
check_cuda_compatibility();
int nb_threads = MAX_CUDA_THREADS; int nb_threads = MAX_CUDA_THREADS;
#else #else
printf("Pas d'utilisation du GPU\n"); printf("Pas d'utilisation du GPU\n");

View File

@ -3,6 +3,7 @@
#include <stdint.h> #include <stdint.h>
#include <inttypes.h> #include <inttypes.h>
#include "include/mnist.h"
uint32_t swap_endian(uint32_t val) { uint32_t swap_endian(uint32_t val) {
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF); val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);

View File

@ -7,6 +7,7 @@
#include <time.h> #include <time.h>
#include "include/neuron.h" #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) // 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) // 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* neuron1;
Neuron* neuron; Neuron* neuron;
cudaMalloc((void**)&network2, sizeof(Network)); cudaMalloc(&network2, (size_t)sizeof(Network));
network2->nb_layers = network->nb_layers; 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++) { 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; 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++) { 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]; neuron1 = network->layers[i]->neurons[j];
neuron->bias = neuron1->bias; neuron->bias = neuron1->bias;
@ -422,9 +423,10 @@ Network* copy_network_cuda(Network* network) {
neuron->last_back_bias = neuron1->last_back_bias; neuron->last_back_bias = neuron1->last_back_bias;
if (i != network2->nb_layers-1) { if (i != network2->nb_layers-1) {
(void)network2->layers[i+1]->nb_neurons; (void)network2->layers[i+1]->nb_neurons;
cudaMalloc((float**)&neuron->weights, sizeof(float)*network->layers[i+1]->nb_neurons); cudaMalloc(&neuron->weights, (size_t)sizeof(float)*network->layers[i+1]->nb_neurons);
cudaMalloc((float**)&neuron->back_weights, sizeof(float)*network->layers[i+1]->nb_neurons); cudaMalloc(&neuron->back_weights, (size_t)sizeof(float)*network->layers[i+1]->nb_neurons);
cudaMalloc((float**)&neuron->last_back_weights, 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++) { for (int k=0; k < network->layers[i+1]->nb_neurons; k++) {
neuron->weights[k] = neuron1->weights[k]; neuron->weights[k] = neuron1->weights[k];
neuron->back_weights[k] = neuron1->back_weights[k]; neuron->back_weights[k] = neuron1->back_weights[k];

View File

@ -4,6 +4,7 @@
#include <inttypes.h> #include <inttypes.h>
#include "include/neuron.h" #include "include/neuron.h"
#include "include/neuron_io.h"
#define MAGIC_NUMBER 2023 #define MAGIC_NUMBER 2023
#define DELTA_MAGIC_NUMBER 2024 #define DELTA_MAGIC_NUMBER 2024

View File

@ -4,6 +4,7 @@
#include <inttypes.h> #include <inttypes.h>
#include "mnist.c" #include "mnist.c"
#include "include/preview.h"
// Prévisualise un chiffre écrit à la main // Prévisualise un chiffre écrit à la main
@ -11,8 +12,8 @@
void print_image(unsigned int width, unsigned int height, int** image) { void print_image(unsigned int width, unsigned int height, int** image) {
char tab[] = {' ', '.', ':', '%', '#', '\0'}; char tab[] = {' ', '.', ':', '%', '#', '\0'};
for (int i=0; i < height; i++) { for (int i=0; i < (int)height; i++) {
for (int j=0; j < width; j++) { for (int j=0; j < (int)width; j++) {
printf("%c", tab[image[i][j]/52]); printf("%c", tab[image[i][j]/52]);
} }
printf("\n"); printf("\n");