Move to Makefile

This commit is contained in:
augustin64 2022-10-24 12:54:51 +02:00
parent 35ac91585f
commit cf050de1d6
27 changed files with 241 additions and 66 deletions

View File

@ -13,13 +13,11 @@ jobs:
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: compile all - name: run-tests
run: ./make.sh build all run: make run-tests
- name: run tests - name: mnist main train
run: ./make.sh test run run: build/mnist-main train
- name: mnist main test
run: out/mnist_main train
--epochs 1 --epochs 1
--images data/mnist/t10k-images-idx3-ubyte --images data/mnist/t10k-images-idx3-ubyte
--labels data/mnist/t10k-labels-idx1-ubyte --labels data/mnist/t10k-labels-idx1-ubyte
--out reseau.bin --out mnist-reseau.bin

4
.gitignore vendored
View File

@ -1,10 +1,10 @@
**/out
**/a.out **/a.out
**/__pycache__ **/__pycache__
.cache .cache
.test-cache .test-cache
.vscode .vscode
build/*
*.bin *.bin
app-secret
data/50States10K data/50States10K

126
Makefile Normal file
View File

@ -0,0 +1,126 @@
BUILDDIR := ./build
SRCDIR := ./src
CACHE_DIR := ./cache
NVCC := nvcc
CC := gcc
MNIST_SRCDIR := $(SRCDIR)/mnist
CNN_SRCDIR := $(SRCDIR)/cnn
MNIST_SRC := $(wildcard $(MNIST_SRCDIR)/*.c)
CNN_SRC := $(wildcard $(CNN_SRCDIR)/*.c)
CNN_SRC_CUDA := $(wildcard $(CNN_SRCDIR)/*.cu)
MNIST_OBJ = $(filter-out $(BUILDDIR)/mnist_main.o $(BUILDDIR)/mnist_utils.o $(BUILDDIR)/mnist_preview.o, $(MNIST_SRC:$(MNIST_SRCDIR)/%.c=$(BUILDDIR)/mnist_%.o))
CNN_OBJ = $(filter-out $(BUILDDIR)/cnn_main.o, $(CNN_SRC:$(CNN_SRCDIR)/%.c=$(BUILDDIR)/cnn_%.o))
CNN_OBJ_CUDA = $(CNN_SRC:$(CNN_SRCDIR)/%.cu=$(BUILDDIR)/cnn_%.o)
TEST_SRCDIR := test
TESTS_SRC = $(wildcard test/*.c)
TESTS_SRC_CU += $(wildcard test/*.cu)
TESTS_OBJ = $(TESTS_SRC:test/%.c=$(BUILDDIR)/test-%) $(TESTS_SRC_CU:test/%.cu=$(BUILDDIR)/test-%)
# Compile flags
CFLAGS = -std=c99 -lm -lpthread
NVCCFLAGS =
# Additional warning rules
CFLAGS += -Wall -Wextra
NVCCFLAGS +=
# Remove warnings about unused variables, functions, ...
# -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable
# Compile with debug
# -g
all: mnist cnn;
#
# Build mnist
#
# Executables
mnist: $(BUILDDIR)/mnist-main $(BUILDDIR)/mnist-utils $(BUILDDIR)/mnist-preview;
$(BUILDDIR)/mnist-main: $(MNIST_SRCDIR)/main.c $(BUILDDIR)/mnist.o $(BUILDDIR)/mnist_neuron_io.o $(BUILDDIR)/mnist_neural_network.o
$(CC) $(CFLAGS) $(MNIST_SRCDIR)/main.c $(BUILDDIR)/mnist.o $(BUILDDIR)/mnist_neuron_io.o $(BUILDDIR)/mnist_neural_network.o -o $(BUILDDIR)/mnist-main
$(BUILDDIR)/mnist-utils: $(MNIST_SRCDIR)/utils.c $(BUILDDIR)/mnist_neural_network.o $(BUILDDIR)/mnist_neuron_io.o $(BUILDDIR)/mnist.o
$(CC) $(CFLAGS) $^ -o $@
$(BUILDDIR)/mnist-preview: $(MNIST_SRCDIR)/preview.c $(BUILDDIR)/mnist.o
$(CC) $(CFLAGS) $^ -o $@
# .o files
$(BUILDDIR)/mnist.o: $(MNIST_SRCDIR)/mnist.c $(MNIST_SRCDIR)/include/mnist.h
$(CC) $(CFLAGS) -c $< -o $@
$(BUILDDIR)/mnist_%.o: $(MNIST_SRCDIR)/%.c $(MNIST_SRCDIR)/include/%.h
$(CC) $(CFLAGS) -c $< -o $@
#
# Build cnn
#
cnn: $(BUILDDIR)/cnn-main;
$(BUILDDIR)/cnn-main: $(CNN_SRCDIR)/main.c $(BUILDDIR)/cnn_train.o $(BUILDDIR)/cnn_cnn.o $(BUILDDIR)/cnn_creation.o $(BUILDDIR)/cnn_initialisation.o $(BUILDDIR)/cnn_make.o $(BUILDDIR)/cnn_neuron_io.o $(BUILDDIR)/cnn_function.o $(BUILDDIR)/cnn_utils.o $(BUILDDIR)/cnn_free.o $(BUILDDIR)/colors.o $(BUILDDIR)/mnist.o
$(CC) $(CFLAGS) $^ -o $@
$(BUILDDIR)/cnn_%.o: $(CNN_SRCDIR)/%.c $(CNN_SRCDIR)/include/%.h
$(CC) $(CFLAGS) -c $< -o $@
$(BUILDDIR)/cnn_%.o: $(CNN_SRCDIR)/%.cu $(CNN_SRCDIR)/include/%.h
$(NVCC) $(NVCCFLAGS) -c $< -o $@
#
# Build general files
#
$(BUILDDIR)/%.o: $(SRCDIR)/%.c $(SRCDIR)/include/%.h
$(CC) $(CFLAGS) -c $< -o $@
#
# Tests
#
run-tests: build-tests
$(foreach file, $(wildcard $(BUILDDIR)/test-*), $(file);)
$(foreach file, $(wildcard $(TEST_SRCDIR)/*.sh), $(file);)
build-tests: prepare-tests $(TESTS_OBJ)
prepare-tests:
@rm -f $(BUILDDIR)/test-*
build/test-cnn_%: test/cnn_%.c $(CNN_OBJ) $(BUILDDIR)/colors.o $(BUILDDIR)/mnist.o
$(CC) $(CFLAGS) $^ -o $@
# mnist.o est déjà inclus en tant que mnist_mnist.o
build/test-mnist_%: test/mnist_%.c $(MNIST_OBJ) $(BUILDDIR)/colors.o
$(CC) $(CFLAGS) $^ -o $@
$(BUILDDIR)/test-cnn_matrix_multiplication: test/cnn_matrix_multiplication.cu $(BUILDDIR)/cnn_matrix_multiplication.o $(BUILDDIR)/colors.o $(BUILDDIR)/mnist.o
$(NVCC) $(NVCCFLAGS) $^ -o $@
#
# Utils
#
webserver: $(CACHE_DIR)/mnist-reseau.bin
FLASK_APP="src/webserver/app.py" flask run
$(CACHE_DIR)/mnist-reseau.bin: $(BUILDDIR)/mnist-main
@mkdir -p $(CACHE_DIR)
$(BUILDDIR)/mnist-main train \
--images "data/mnist/train-images-idx3-ubyte" \
--labels "data/mnist/train-labels-idx1-ubyte" \
--out "$(CACHE_DIR)/mnist-reseau.bin"
#
# Clean project
#
clean:
rm -rf $(BUILDDIR)/*
rm -f $(CACHE_DIR)/*

View File

@ -3,12 +3,12 @@
#include <math.h> #include <math.h>
#include <float.h> // Is it used ? #include <float.h> // Is it used ?
#include "../colors.h"
#include "include/initialisation.h" #include "include/initialisation.h"
#include "function.c" #include "include/function.h"
#include "creation.c" #include "include/creation.h"
#include "make.c" #include "include/make.h"
#include "../include/colors.h"
#include "include/cnn.h" #include "include/cnn.h"
// Augmente les dimensions de l'image d'entrée // Augmente les dimensions de l'image d'entrée

View File

@ -1,8 +1,10 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "include/creation.h"
#include "include/initialisation.h"
#include "include/function.h" #include "include/function.h"
#include "initialisation.c"
#include "include/creation.h"
Network* create_network(int max_size, int learning_rate, int dropout, int initialisation, int input_dim, int input_depth) { Network* create_network(int max_size, int learning_rate, int dropout, int initialisation, int input_dim, int input_depth) {
if (dropout < 0 || dropout > 100) { if (dropout < 0 || dropout > 100) {

View File

@ -1,5 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "include/free.h" #include "include/free.h"
void free_a_cube_input_layer(Network* network, int pos, int depth, int dim) { void free_a_cube_input_layer(Network* network, int pos, int depth, int dim) {

View File

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include "include/function.h" #include "include/function.h"
float max(float a, float b) { float max(float a, float b) {

View File

@ -0,0 +1,40 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#ifndef DEF_MATRIX_MULTIPLICATION_H
#define DEF_MATRIX_MULTIPLICATION_H
#ifdef __CUDACC__
/*
* Partie entière supérieure de a/b
*/
int i_div_up(int a, int b);
/*
* Fonction exécutée par chaque thread lancé dans `matrix_multiplication_device`
*/
__global__ void matrix_mul_kernel(float* Md, float* Nd, float* Pd, int n, int p, int q, size_t pitch_m, size_t pitch_n, size_t pitch_p);
/*
* Multiplication de deux matrices sur le GPU
*/
void matrix_multiplication_device(float** m1, float** m2, float** result, int n, int p, int q);
#endif
/*
* Vérification de la compatibilité CUDA
*/
bool check_cuda_compatibility();
/*
* Multiplication naïve de matrices sur le CPU (1 seul coeur)
*/
void matrix_multiplication_host(float** m1, float** m2, float** result, int n, int p, int q);
/*
* Multiplication de matrices (décide si il faut la faire sur CPU ou GPU)
*/
void matrix_multiplication(float** m1, float** m2, float** result, int n, int p, int q, bool use_cuda);
#endif

View File

@ -3,7 +3,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include "../../colors.h"
#include "struct.h" #include "struct.h"
#ifndef DEF_UTILS_H #ifndef DEF_UTILS_H

View File

@ -1,7 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include "../colors.h" #include "../include/colors.h"
#include "include/initialisation.h" #include "include/initialisation.h"

View File

@ -4,9 +4,12 @@
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include "train.c" #include "include/initialisation.h"
#include "../include/colors.h"
#include "include/function.h"
#include "include/creation.h"
#include "include/train.h"
#include "include/cnn.h" #include "include/cnn.h"
#include "../colors.h"
#include "include/main.h" #include "include/main.h"

View File

@ -1,6 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include "../colors.h" #include "../include/colors.h"
#include "include/make.h" #include "include/make.h"
void make_convolution(Kernel_cnn* kernel, float*** input, float*** output, int output_dim) { void make_convolution(Kernel_cnn* kernel, float*** input, float*** output, int output_dim) {

View File

@ -3,7 +3,7 @@
#include <stdint.h> #include <stdint.h>
#include <inttypes.h> #include <inttypes.h>
#include "../colors.h" #include "../include/colors.h"
#include "include/neuron_io.h" #include "include/neuron_io.h"
#include "include/struct.h" #include "include/struct.h"
@ -170,7 +170,7 @@ Network* read_network(char* filename) {
} }
network->input = (float****)malloc(sizeof(float***)*size); network->input = (float****)malloc(sizeof(float***)*size);
for (int i=0; i < size; i++) { // input[size][couche->depth][couche->dim][couche->dim] for (int i=0; i < (int)size; i++) { // input[size][couche->depth][couche->dim][couche->dim]
network->input[i] = (float***)malloc(sizeof(float**)*network->depth[i]); network->input[i] = (float***)malloc(sizeof(float**)*network->depth[i]);
for (int j=0; j < network->depth[i]; j++) { for (int j=0; j < network->depth[i]; j++) {
network->input[i][j] = (float**)malloc(sizeof(float*)*network->width[i]); network->input[i][j] = (float**)malloc(sizeof(float*)*network->width[i]);

View File

@ -4,12 +4,15 @@
#include <pthread.h> #include <pthread.h>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include "../mnist/mnist.c" #include "../mnist/include/mnist.h"
#include "../colors.h" #include "include/initialisation.h"
#include "neuron_io.c" #include "include/neuron_io.h"
#include "utils.c" #include "../include/colors.h"
#include "free.c" #include "include/function.h"
#include "cnn.c" #include "include/creation.h"
#include "include/utils.h"
#include "include/free.h"
#include "include/cnn.h"
#include "include/train.h" #include "include/train.h"
@ -19,7 +22,7 @@ void* train_thread(void* parameters) {
Network* network = param->network; Network* network = param->network;
int*** images = param->images; int*** images = param->images;
int* labels = param->labels; int* labels = (int*)param->labels;
int width = param->width; int width = param->width;
int height = param->height; int height = param->height;
@ -32,7 +35,7 @@ void* train_thread(void* parameters) {
if (dataset_type == 0) { if (dataset_type == 0) {
write_image_in_network_32(images[i], height, width, network->input[0][0]); write_image_in_network_32(images[i], height, width, network->input[0][0]);
forward_propagation(network); forward_propagation(network);
//backward_propagation(network, labels[i]); backward_propagation(network, labels[i]);
// TODO get_indice_max(network last layer) // TODO get_indice_max(network last layer)
// TODO if indice_max == labels[i] then accuracy += 1. // TODO if indice_max == labels[i] then accuracy += 1.

View File

@ -3,7 +3,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include "../colors.h" #include "../include/colors.h"
#include "include/struct.h" #include "include/struct.h"
#define copyVar(var) network_cp->var = network->var #define copyVar(var) network_cp->var = network->var

15
src/colors.c Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
#include "include/colors.h"
void printf_error(char* string) {
printf(BOLDRED "[ ERROR ]" RESET " %s", string);
}
void printf_warning(char* string) {
printf(BOLDYELLOW "[WARNING]" RESET " %s", string);
}
void printf_info(char* string) {
printf(BOLDBLUE "[ INFO ]" RESET " %s", string);
}

View File

@ -1,3 +1,5 @@
#include <stdio.h>
#ifndef DEF_COLORS_H #ifndef DEF_COLORS_H
#define DEF_COLORS_H #define DEF_COLORS_H
@ -19,16 +21,8 @@
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */ #define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */ #define BOLDWHITE "\033[1m\033[37m" /* Bold White */
void printf_error(char* string) { void printf_error(char* string);
printf(BOLDRED "[ ERROR ]" RESET " %s", string); void printf_warning(char* string);
} void printf_info(char* string);
void printf_warning(char* string) {
printf(BOLDYELLOW "[WARNING]" RESET " %s", string);
}
void printf_info(char* string) {
printf(BOLDBLUE "[ INFO ]" RESET " %s", string);
}
#endif #endif

View File

@ -1,8 +1,6 @@
#ifndef DEF_MAIN_H #ifndef DEF_MAIN_H
#define DEF_MAIN_H #define DEF_MAIN_H
#include "../main.c"
/* /*
* Affiche une image ainsi que les prévisions faites par le réseau de neurones sur sa valeur * Affiche une image ainsi que les prévisions faites par le réseau de neurones sur sa valeur
* width, height: dimensions de l'image * width, height: dimensions de l'image

View File

@ -6,7 +6,6 @@
#ifndef DEF_MNIST_H #ifndef DEF_MNIST_H
#define DEF_MNIST_H #define DEF_MNIST_H
#include "../mnist.c"
uint32_t swap_endian(uint32_t val); uint32_t swap_endian(uint32_t val);

View File

@ -10,7 +10,6 @@
#ifndef DEF_NEURAL_NETWORK_H #ifndef DEF_NEURAL_NETWORK_H
#define DEF_NEURAL_NETWORK_H #define DEF_NEURAL_NETWORK_H
#include "../neural_network.c"
/* /*
* Fonction max pour les floats * Fonction max pour les floats

View File

@ -8,7 +8,6 @@
#ifndef DEF_NEURON_IO_H #ifndef DEF_NEURON_IO_H
#define DEF_NEURON_IO_H #define DEF_NEURON_IO_H
#include "../neuron_io.c"
// Lecture d'un réseau neuronal // Lecture d'un réseau neuronal

View File

@ -6,7 +6,6 @@
#ifndef DEF_PREVIEW_H #ifndef DEF_PREVIEW_H
#define DEF_PREVIEW_H #define DEF_PREVIEW_H
#include "../preview.c"
/* /*
* Affiche un chiffre de taille width x height * Affiche un chiffre de taille width x height

View File

@ -38,9 +38,9 @@ def recognize_mnist(image):
try: try:
output = subprocess.check_output([ output = subprocess.check_output([
'out/mnist_main', 'build/mnist-main',
'recognize', 'recognize',
'--modele', '.cache/reseau.bin', '--modele', '.cache/mnist-reseau.bin',
'--in', '.cache/image-idx3-ubyte', '--in', '.cache/image-idx3-ubyte',
'--out', 'json' '--out', 'json'
]).decode("utf-8") ]).decode("utf-8")

View File

@ -4,7 +4,7 @@
#include <math.h> #include <math.h>
#include <time.h> #include <time.h>
#include "../src/cnn/matrix_multiplication.cu" #include "../src/cnn/include/matrix_multiplication.h"
float random_float(float low, float high) { float random_float(float low, float high) {

View File

@ -3,11 +3,10 @@
#include <stdint.h> #include <stdint.h>
#include <inttypes.h> #include <inttypes.h>
#include "../src/colors.h" #include "../src/include/colors.h"
#include "../src/cnn/neuron_io.c" #include "../src/cnn/include/neuron_io.h"
#include "../src/cnn/creation.c" #include "../src/cnn/include/creation.h"
#include "../src/cnn/utils.c" #include "../src/cnn/include/utils.h"
int main() { int main() {

View File

@ -1,9 +1,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "../src/colors.h" #include "../src/include/colors.h"
#include "../src/cnn/creation.c" #include "../src/cnn/include/creation.h"
#include "../src/cnn/utils.c" #include "../src/cnn/include/utils.h"
int main() { int main() {
printf("Création du réseau\n"); printf("Création du réseau\n");

View File

@ -2,21 +2,21 @@
set -e set -e
OUT="$1" OUT="build"
[[ -f "$OUT/mnist_utils" ]] || "$2" build mnist-utils [[ -f "$OUT/mnist-utils" ]] || make $OUT/mnist-utils
echo "Compte des labels" echo "Compte des labels"
"$OUT/mnist_utils" count-labels -l data/mnist/t10k-labels-idx1-ubyte > /dev/null "$OUT/mnist-utils" count-labels -l data/mnist/t10k-labels-idx1-ubyte > /dev/null
echo "OK" echo "OK"
echo "Création du réseau" echo "Création du réseau"
"$OUT/mnist_utils" creer-reseau -n 3 -o .test-cache/reseau.bin > /dev/null "$OUT/mnist-utils" creer-reseau -n 3 -o .test-cache/reseau.bin > /dev/null
echo "OK" echo "OK"
echo "Affichage poids" echo "Affichage poids"
"$OUT/mnist_utils" print-poids -r .test-cache/reseau.bin > /dev/null "$OUT/mnist-utils" print-poids -r .test-cache/reseau.bin > /dev/null
echo "OK" echo "OK"
echo "Affichage biais" echo "Affichage biais"
"$OUT/mnist_utils" print-biais -r .test-cache/reseau.bin > /dev/null "$OUT/mnist-utils" print-biais -r .test-cache/reseau.bin > /dev/null
echo "OK" echo "OK"