tipe/Makefile

299 lines
8.9 KiB
Makefile
Raw Normal View History

2023-06-26 18:09:21 +02:00
OS := $(shell uname)
2022-10-24 12:54:51 +02:00
BUILDDIR := ./build
SRCDIR := ./src
2023-02-19 10:21:26 +01:00
CACHE_DIR := ./.cache
NVCC := nvcc
2023-02-07 15:44:52 +01:00
CUDA_INCLUDE := /opt/cuda/include # Default installation path for ArchLinux, may be different
2022-10-24 12:54:51 +02:00
NVCC_INSTALLED := $(shell command -v $(NVCC) 2> /dev/null)
2022-10-24 12:54:51 +02:00
2023-05-12 16:16:34 +02:00
COMMON_SRCDIR := $(SRCDIR)/common
DENSE_SRCDIR := $(SRCDIR)/dense
CNN_SRCDIR := $(SRCDIR)/cnn
2022-10-24 12:54:51 +02:00
DENSE_SRC := $(wildcard $(DENSE_SRCDIR)/*.c)
2022-10-24 12:54:51 +02:00
CNN_SRC := $(wildcard $(CNN_SRCDIR)/*.c)
CNN_SRC_CUDA := $(wildcard $(CNN_SRCDIR)/*.cu)
DENSE_OBJ = $(filter-out $(BUILDDIR)/dense_main.o $(BUILDDIR)/dense_utils.o $(BUILDDIR)/dense_preview.o, $(DENSE_SRC:$(DENSE_SRCDIR)/%.c=$(BUILDDIR)/dense_%.o))
CNN_OBJ = $(filter-out $(BUILDDIR)/cnn_main.o $(BUILDDIR)/cnn_preview.o $(BUILDDIR)/cnn_export.o, $(CNN_SRC:$(CNN_SRCDIR)/%.c=$(BUILDDIR)/cnn_%.o))
2022-10-24 12:54:51 +02:00
CNN_OBJ_CUDA = $(CNN_SRC:$(CNN_SRCDIR)/%.cu=$(BUILDDIR)/cnn_%.o)
TEST_SRCDIR := test
2023-02-07 15:44:52 +01:00
TESTS_SRC = $(wildcard $(TEST_SRCDIR)/*.c)
TESTS_SRC_CU += $(wildcard $(TEST_SRCDIR)/*.cu)
2022-10-24 12:54:51 +02:00
2023-02-07 15:44:52 +01:00
TESTS_OBJ = $(TESTS_SRC:$(TEST_SRCDIR)/%.c=$(BUILDDIR)/$(TEST_SRCDIR)-%) $(TESTS_SRC_CU:$(TEST_SRCDIR)/%.cu=$(BUILDDIR)/$(TEST_SRCDIR)-%)
2022-10-24 12:54:51 +02:00
2023-02-01 11:46:08 +01:00
# Linker only flags
LD_CFLAGS = -lm -lpthread -ljpeg
LD_NVCCFLAGS = -ljpeg
2022-10-24 12:54:51 +02:00
2023-02-01 11:46:08 +01:00
# Compilation flag
2023-02-18 13:03:08 +01:00
CFLAGS = -Wall -Wextra -std=gnu99 -g -O3
2023-02-05 16:21:41 +01:00
NVCCFLAGS = -g
2022-10-24 12:54:51 +02:00
# Remove warnings about unused variables, functions, ...
# -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable
# Compile with debug
# -g
2023-01-14 14:30:34 +01:00
# See memory leaks and Incorrect Read/Write
# -fsanitize=address -lasan
2023-02-15 11:15:48 +01:00
#! WARNING: test/cnn-neuron_io fails with this option enabled
2022-10-24 12:54:51 +02:00
2023-06-26 18:09:21 +02:00
# Specify library path of libjpeg on MacOS
ifeq ($(OS),Darwin)
LD_CFLAGS += -I/opt/homebrew/Cellar/jpeg/9e/include/ -L/opt/homebrew/Cellar/jpeg/9e/lib/
LD_NVCCFLAGS += -L/opt/homebrew/Cellar/jpeg/9e/lib/
CFLAGS += -I/opt/homebrew/Cellar/jpeg/9e/include/
endif
2023-03-13 13:55:09 +01:00
all: dense cnn;
2022-10-24 12:54:51 +02:00
#
2023-03-13 13:55:09 +01:00
# Build dense
2022-10-24 12:54:51 +02:00
#
# Executables
2023-03-13 13:55:09 +01:00
dense: $(BUILDDIR)/dense-main $(BUILDDIR)/dense-utils $(BUILDDIR)/dense-preview;
2022-10-24 12:54:51 +02:00
2023-03-13 13:55:09 +01:00
$(BUILDDIR)/dense-main: $(DENSE_SRCDIR)/main.c $(BUILDDIR)/mnist.o $(BUILDDIR)/dense_neuron_io.o $(BUILDDIR)/dense_neural_network.o
2023-02-01 11:49:21 +01:00
$(CC) $^ -o $@ $(CFLAGS) $(LD_CFLAGS)
2022-10-24 12:54:51 +02:00
2023-03-13 13:55:09 +01:00
$(BUILDDIR)/dense-utils: $(DENSE_SRCDIR)/utils.c $(BUILDDIR)/dense_neural_network.o $(BUILDDIR)/dense_neuron_io.o $(BUILDDIR)/mnist.o
2023-02-01 11:49:21 +01:00
$(CC) $^ -o $@ $(CFLAGS) $(LD_CFLAGS)
2022-10-24 12:54:51 +02:00
2023-03-13 13:55:09 +01:00
$(BUILDDIR)/dense-preview: $(DENSE_SRCDIR)/preview.c $(BUILDDIR)/mnist.o
$(CC) $^ -o $@ $(CFLAGS) $(LD_CFLAGS)
2023-01-28 22:04:38 +01:00
2023-03-13 13:55:09 +01:00
$(BUILDDIR)/dense_%.o: $(DENSE_SRCDIR)/%.c $(DENSE_SRCDIR)/include/%.h
2022-11-04 12:43:47 +01:00
$(CC) -c $< -o $@ $(CFLAGS)
2022-10-24 12:54:51 +02:00
2022-10-24 12:54:51 +02:00
#
# Build cnn
#
2023-05-27 22:23:50 +02:00
cnn: $(BUILDDIR)/cnn-main $(BUILDDIR)/cnn-main-cuda $(BUILDDIR)/cnn-preview $(BUILDDIR)/cnn-export $(BUILDDIR)/cnn-export-cuda;
2022-10-24 12:54:51 +02:00
2023-01-28 22:04:38 +01:00
$(BUILDDIR)/cnn-main: $(CNN_SRCDIR)/main.c \
$(BUILDDIR)/cnn_backpropagation.o \
2023-01-28 22:04:38 +01:00
$(BUILDDIR)/cnn_initialisation.o \
$(BUILDDIR)/cnn_test_network.o \
$(BUILDDIR)/cnn_convolution.o \
2023-01-28 22:04:38 +01:00
$(BUILDDIR)/cnn_neuron_io.o \
$(BUILDDIR)/cnn_function.o \
$(BUILDDIR)/cnn_creation.o \
$(BUILDDIR)/cnn_models.o \
2023-01-28 22:04:38 +01:00
$(BUILDDIR)/cnn_update.o \
$(BUILDDIR)/cnn_train.o \
$(BUILDDIR)/cnn_utils.o \
$(BUILDDIR)/cnn_make.o \
2023-01-28 22:04:38 +01:00
$(BUILDDIR)/cnn_free.o \
$(BUILDDIR)/cnn_jpeg.o \
$(BUILDDIR)/cnn_cnn.o \
\
2023-02-18 13:03:08 +01:00
$(BUILDDIR)/memory_management.o \
2023-01-28 22:04:38 +01:00
$(BUILDDIR)/colors.o \
$(BUILDDIR)/mnist.o \
$(BUILDDIR)/utils.o
2023-02-01 11:50:35 +01:00
$(CC) $^ -o $@ $(CFLAGS) $(LD_CFLAGS)
2022-10-24 12:54:51 +02:00
ifdef NVCC_INSTALLED
2023-01-28 22:04:38 +01:00
$(BUILDDIR)/cnn-main-cuda: $(BUILDDIR)/cnn_main.cuda.o \
2023-02-15 11:22:51 +01:00
$(BUILDDIR)/cnn_initialisation.cuda.o \
$(BUILDDIR)/cnn_test_network.cuda.o \
2023-01-28 22:04:38 +01:00
$(BUILDDIR)/cnn_neuron_io.cuda.o \
$(BUILDDIR)/cnn_creation.cuda.o \
$(BUILDDIR)/cnn_models.cuda.o \
2023-02-15 11:22:51 +01:00
$(BUILDDIR)/cnn_update.cuda.o \
$(BUILDDIR)/cnn_train.cuda.o \
$(BUILDDIR)/cnn_utils.cuda.o \
2023-01-28 22:04:38 +01:00
$(BUILDDIR)/cnn_free.cuda.o \
$(BUILDDIR)/cnn_jpeg.cuda.o \
$(BUILDDIR)/cnn_cnn.cuda.o \
\
$(BUILDDIR)/cnn_cuda_backpropagation.o \
$(BUILDDIR)/cnn_cuda_convolution.o \
$(BUILDDIR)/cnn_cuda_function.o \
$(BUILDDIR)/cnn_cuda_make.o \
\
2023-02-18 13:03:08 +01:00
$(BUILDDIR)/cuda_memory_management.o \
$(BUILDDIR)/colors.cuda.o \
2023-01-28 22:04:38 +01:00
$(BUILDDIR)/mnist.cuda.o \
$(BUILDDIR)/cuda_utils.o
2023-02-01 11:46:08 +01:00
$(NVCC) $(LD_NVCCFLAGS) $(NVCCFLAGS) $^ -o $@
else
$(BUILDDIR)/cnn-main-cuda:
@echo "$(NVCC) not found, skipping"
2022-11-19 16:11:24 +01:00
endif
2022-11-01 17:24:29 +01:00
2023-01-28 22:04:38 +01:00
$(BUILDDIR)/cnn-preview: $(CNN_SRCDIR)/preview.c $(BUILDDIR)/cnn_jpeg.o $(BUILDDIR)/colors.o $(BUILDDIR)/utils.o
2023-02-01 11:49:21 +01:00
$(CC) $^ -o $@ $(CFLAGS) $(LD_CFLAGS)
2022-11-19 17:39:56 +01:00
2023-05-08 18:58:33 +02:00
$(BUILDDIR)/cnn-export: $(CNN_SRCDIR)/export.c \
$(BUILDDIR)/cnn_backpropagation.o \
$(BUILDDIR)/cnn_convolution.o \
$(BUILDDIR)/cnn_neuron_io.o \
2023-05-08 18:58:33 +02:00
$(BUILDDIR)/cnn_function.o \
$(BUILDDIR)/cnn_free.o \
$(BUILDDIR)/cnn_make.o \
$(BUILDDIR)/cnn_cnn.o \
$(BUILDDIR)/cnn_jpeg.o \
\
$(BUILDDIR)/memory_management.o \
2023-05-08 18:58:33 +02:00
$(BUILDDIR)/colors.o \
$(BUILDDIR)/mnist.o \
$(BUILDDIR)/utils.o
2023-02-05 16:21:41 +01:00
$(CC) $^ -o $@ $(CFLAGS) $(LD_CFLAGS)
ifdef NVCC_INSTALLED
2023-05-27 22:23:50 +02:00
$(BUILDDIR)/cnn-export-cuda: $(CNN_SRCDIR)/export.c \
$(BUILDDIR)/cnn_cuda_backpropagation.o \
$(BUILDDIR)/cnn_cuda_convolution.o \
$(BUILDDIR)/cnn_neuron_io.cuda.o \
$(BUILDDIR)/cnn_cuda_function.o \
$(BUILDDIR)/cnn_free.cuda.o \
$(BUILDDIR)/cnn_cuda_make.o \
$(BUILDDIR)/cnn_cnn.cuda.o \
$(BUILDDIR)/cnn_utils.cuda.o \
$(BUILDDIR)/cnn_jpeg.cuda.o \
\
$(BUILDDIR)/cuda_memory_management.o \
$(BUILDDIR)/colors.cuda.o \
$(BUILDDIR)/mnist.o \
$(BUILDDIR)/cuda_utils.o
$(NVCC) $^ -o $@ $(NVCCFLAGS) $(LD_NVCCFLAGS)
else
$(BUILDDIR)/cnn-export-cuda: $(CNN_SRCDIR)/export.c
@echo "$(NVCC) not found, skipping"
endif
2023-05-27 22:23:50 +02:00
2022-10-24 12:54:51 +02:00
$(BUILDDIR)/cnn_%.o: $(CNN_SRCDIR)/%.c $(CNN_SRCDIR)/include/%.h
2022-11-04 12:43:47 +01:00
$(CC) -c $< -o $@ $(CFLAGS)
2022-10-24 12:54:51 +02:00
2023-01-28 22:04:38 +01:00
$(BUILDDIR)/cnn_%.cuda.o: $(CNN_SRCDIR)/%.c $(CNN_SRCDIR)/include/%.h
2023-01-29 09:40:55 +01:00
$(CC) -c $< -o $@ $(CFLAGS) -DUSE_CUDA -lcuda -I$(CUDA_INCLUDE)
2023-01-28 22:04:38 +01:00
ifdef NVCC_INSTALLED
2022-11-01 17:24:29 +01:00
$(BUILDDIR)/cnn_cuda_%.o: $(CNN_SRCDIR)/%.cu $(CNN_SRCDIR)/include/%.h
2023-03-31 14:17:41 +02:00
$(NVCC) $(NVCCFLAGS) -c -dc $< -o $@
else
$(BUILDDIR)/cnn_cuda_%.o: $(CNN_SRCDIR)/%.cu $(CNN_SRCDIR)/include/%.h
@echo "$(NVCC) not found, skipping"
2022-10-24 14:09:10 +02:00
endif
2022-10-24 12:54:51 +02:00
#
# Build general files
#
2023-05-12 16:16:34 +02:00
$(BUILDDIR)/%.o: $(COMMON_SRCDIR)/%.c $(COMMON_SRCDIR)/include/%.h
2022-11-04 12:45:05 +01:00
$(CC) -c $< -o $@ $(CFLAGS)
2022-10-24 12:54:51 +02:00
2023-05-12 16:16:34 +02:00
$(BUILDDIR)/%.cuda.o: $(COMMON_SRCDIR)/%.c $(COMMON_SRCDIR)/include/%.h
2023-01-29 09:40:55 +01:00
$(CC) -c $< -o $@ $(CFLAGS) -DUSE_CUDA -lcuda -I$(CUDA_INCLUDE)
2023-01-28 22:04:38 +01:00
ifdef NVCC_INSTALLED
2023-05-12 16:16:34 +02:00
$(BUILDDIR)/cuda_%.o: $(COMMON_SRCDIR)/%.cu $(COMMON_SRCDIR)/include/%.h
2023-03-31 14:17:41 +02:00
$(NVCC) $(NVCCFLAGS) -c -dc $< -o $@
else
@echo "$(NVCC) not found, skipping"
endif
2022-10-24 12:54:51 +02:00
#
# Tests
#
run-tests: build-tests
$(foreach file, $(wildcard $(TEST_SRCDIR)/*.sh), $(file);)
2023-11-10 23:09:48 +01:00
@echo "$$(for file in build/test-*; do $$file || exit 1; done)"
2022-10-24 12:54:51 +02:00
2023-02-19 10:31:19 +01:00
build-tests: prepare-tests $(TESTS_OBJ) $(BUILDDIR)/test-cnn_matrix_multiplication $(BUILDDIR)/test-cnn_convolution $(BUILDDIR)/test-cuda_memory_management
2022-10-24 12:54:51 +02:00
prepare-tests:
@rm -f $(BUILDDIR)/test-*
2023-02-19 10:31:19 +01:00
$(BUILDDIR)/test-cnn_%: $(TEST_SRCDIR)/cnn_%.c $(CNN_OBJ) $(BUILDDIR)/colors.o $(BUILDDIR)/mnist.o $(BUILDDIR)/utils.o $(BUILDDIR)/memory_management.o
2023-02-01 11:49:21 +01:00
$(CC) $^ -o $@ $(CFLAGS) $(LD_CFLAGS)
2022-10-24 12:54:51 +02:00
$(BUILDDIR)/test-dense_%: $(TEST_SRCDIR)/dense_%.c $(DENSE_OBJ) $(BUILDDIR)/colors.o $(BUILDDIR)/mnist.o
2023-02-01 11:49:21 +01:00
$(CC) $^ -o $@ $(CFLAGS) $(LD_CFLAGS)
2022-10-24 12:54:51 +02:00
2023-02-19 10:31:19 +01:00
$(BUILDDIR)/test-memory_management: $(TEST_SRCDIR)/memory_management.c $(BUILDDIR)/colors.o $(BUILDDIR)/utils.o $(BUILDDIR)/test_memory_management.o
2023-02-18 13:03:08 +01:00
$(CC) $^ -o $@ $(CFLAGS) $(LD_CFLAGS)
2023-05-12 16:16:34 +02:00
$(BUILDDIR)/test_memory_management.o: $(COMMON_SRCDIR)/memory_management.c $(COMMON_SRCDIR)/include/memory_management.h
2023-02-18 13:03:08 +01:00
$(CC) -c $< -o $@ $(CFLAGS) -DTEST_MEMORY_MANAGEMENT
2023-02-19 10:31:19 +01:00
ifdef NVCC_INSTALLED
$(BUILDDIR)/test-cuda_memory_management: $(TEST_SRCDIR)/memory_management.cu $(BUILDDIR)/colors.cuda.o $(BUILDDIR)/cuda_utils.o $(BUILDDIR)/cuda_memory_management.o
$(NVCC) $(LD_NVCCFLAGS) $(NVCCFLAGS) $^ -o $@
else
$(BUILDDIR)/test-cuda_memory_management:
@echo "$(NVCC) not found, skipping"
endif
ifdef NVCC_INSTALLED
2023-02-07 15:44:52 +01:00
$(BUILDDIR)/test-cnn_%: $(TEST_SRCDIR)/cnn_%.cu \
2023-01-28 22:04:38 +01:00
$(BUILDDIR)/cnn_cuda_%.o \
$(BUILDDIR)/cuda_utils.o \
2023-01-29 09:40:55 +01:00
$(BUILDDIR)/colors.o \
2023-02-18 13:03:08 +01:00
$(BUILDDIR)/mnist.cuda.o \
$(BUILDDIR)/cuda_memory_management.o
2023-02-01 11:46:08 +01:00
$(NVCC) $(LD_NVCCFLAGS) $(NVCCFLAGS) $^ -o $@
else
2023-02-07 15:44:52 +01:00
$(BUILDDIR)/test-cnn_%: $(TEST_SRCDIR)/cnn_%.cu
@echo "$(NVCC) not found, skipping"
2022-10-24 14:09:10 +02:00
endif
2022-10-24 12:54:51 +02:00
2022-10-24 12:54:51 +02:00
#
# Utils
#
2023-02-19 10:21:26 +01:00
webserver: $(CACHE_DIR)/mnist-reseau-fully-connected.bin $(CACHE_DIR)/mnist-reseau-cnn.bin
2022-10-24 12:54:51 +02:00
FLASK_APP="src/webserver/app.py" flask run
2023-03-13 13:55:09 +01:00
$(CACHE_DIR)/mnist-reseau-fully-connected.bin: $(BUILDDIR)/dense-main
2022-10-24 12:54:51 +02:00
@mkdir -p $(CACHE_DIR)
2023-03-13 13:55:09 +01:00
$(BUILDDIR)/dense-main train \
2022-10-24 12:54:51 +02:00
--images "data/mnist/train-images-idx3-ubyte" \
--labels "data/mnist/train-labels-idx1-ubyte" \
2023-01-23 21:16:36 +01:00
--out "$(CACHE_DIR)/mnist-reseau-fully-connected.bin"
$(CACHE_DIR)/mnist-reseau-cnn.bin: $(BUILDDIR)/cnn-main
@mkdir -p $(CACHE_DIR)
$(BUILDDIR)/cnn-main train \
--dataset mnist \
--images data/mnist/train-images-idx3-ubyte \
--labels data/mnist/train-labels-idx1-ubyte \
--epochs 10 \
--out $(CACHE_DIR)/mnist-reseau-cnn.bin
2022-10-24 12:54:51 +02:00
2022-10-24 12:54:51 +02:00
#
# Clean project
#
clean:
rm -rf $(BUILDDIR)/*
2023-05-08 18:58:33 +02:00
#rm -f $(CACHE_DIR)/*