tipe/make.sh

206 lines
5.1 KiB
Bash
Raw Normal View History

2022-03-21 17:18:49 +01:00
#!/bin/bash
2022-06-01 14:40:36 +02:00
# C compiler can be defined with the $CC environment variable
2022-04-08 19:34:54 +02:00
OUT="out"
2022-03-25 19:19:57 +01:00
2022-05-14 16:40:54 +02:00
set -e
2022-05-14 15:56:13 +02:00
2022-06-01 17:13:03 +02:00
compile_cuda () {
# nvcc will compile .c files as if they did not have
# CUDA program parts so we need to copy them to .cu
2022-06-01 21:30:34 +02:00
mv $1 "$1"u
echo "" > "$1" # If we compile file.cu, file.c needs to exist to, even if it is empty
2022-06-01 17:13:03 +02:00
nvcc "$1"u ${*:1}
2022-06-01 21:30:34 +02:00
mv "$1"u "$1"
2022-06-01 17:13:03 +02:00
}
2022-06-01 14:40:36 +02:00
build () {
2022-04-22 15:22:51 +02:00
mkdir -p "$OUT"
[[ $1 ]] || set "mnist-main"
case $1 in
"mnist-main")
echo "Compilation de src/mnist/main.c";
$CC src/mnist/main.c -o "$OUT/mnist_main" $FLAGS;
echo "Fait.";
return 0;;
"mnist-preview")
echo "Compilation de src/mnist/preview.c";
$CC src/mnist/preview.c -o "$OUT/mnist_preview" $FLAGS;
echo "Fait.";
return 0;;
"mnist-utils")
echo "Compilation de src/mnist/utils.c";
$CC "src/mnist/utils.c" -o "$OUT/mnist_utils" $FLAGS;
echo "Fait.";
return 0;;
"mnist")
build mnist-main;
build mnist-preview;
build mnist-utils;;
"cnn-main")
echo "Compilation de src/cnn/main.c";
$CC "src/cnn/main.c" -o "$OUT/cnn_main" $FLAGS;
echo "Fait.";;
"cnn")
build cnn-main;;
"test")
rm "$OUT/test_"* || true;
for i in "test/"*".c"; do
echo "Compilation de $i";
$CC "$i" -o "$OUT/test_$(echo $i | awk -F. '{print $1}' | awk -F/ '{print $NF}')" $FLAGS;
echo "Fait.";
done;
if ! command -v nvcc &> /dev/null; then
echo "Tests CUDA évités";
elif [[ $SKIP_CUDA == 1 ]]; then
echo "Tests CUDA évités";
else
for i in "test/"*".cu"; do
echo "Compilation de $i";
nvcc "$i" -o "$OUT/test_$(echo $i | awk -F. '{print $1}' | awk -F/ '{print $NF}')";
echo "Fait.";
done;
fi;
return 0;;
*)
echo -e "\033[1m\033[34m### Building mnist ###\033[0m";
build mnist-main;
build mnist-preview;
build mnist-utils;
echo -e "\033[1m\033[34m### Building cnn ###\033[0m";
build cnn;
echo -e "\033[1m\033[34m### Building tests ###\033[0m";
build test;
return 0;;
esac
2022-06-01 14:40:36 +02:00
}
2022-04-22 15:22:51 +02:00
2022-06-01 14:40:36 +02:00
preview () {
case $1 in
"train")
[[ -f "$OUT/mnist_preview" ]] || $0 build mnist-preview;
"$OUT/mnist_preview" data/mnist/train-images-idx3-ubyte data/mnist/train-labels-idx1-ubyte;
return 0;;
"t10k")
[[ -f "$OUT/mnist_preview" ]] || $0 build mnist-preview;
"$OUT/mnist_preview" data/mnist/t10k-images-idx3-ubyte data/mnist/t10k-labels-idx1-ubyte;
return 0;;
*)
build mnist-preview;
return 0;;
esac
2022-06-01 14:40:36 +02:00
}
2022-03-27 14:42:00 +02:00
2022-06-01 14:40:36 +02:00
test () {
case $1 in
"run")
build test;
mkdir -p .test-cache;
for i in "$OUT/test_"*; do
echo "--- $i ---";
$i;
done
for i in "test/"*".sh"; do
echo "--- $i ---";
chmod +x "$i";
"$i" "$OUT" "$0";
done;
return 0;;
*)
build test;
return 0;;
esac
2022-06-01 14:40:36 +02:00
}
2022-03-27 15:03:13 +02:00
2022-06-01 14:40:36 +02:00
train () {
[[ -f "$OUT/mnist_main" ]] || build mnist-main
2022-06-01 14:40:36 +02:00
[[ $1 ]] || set -- "train"
[[ $2 == "-r" || $2 == "--recover" ]] && RECOVER="-r .cache/reseau.bin"
2022-04-08 19:34:54 +02:00
mkdir -p .cache
"$OUT/mnist_main" train \
2022-06-01 14:40:36 +02:00
--images "data/mnist/$1-images-idx3-ubyte" \
--labels "data/mnist/$1-labels-idx1-ubyte" \
2022-04-19 17:21:37 +02:00
--out ".cache/reseau.bin" \
$RECOVER
2022-06-01 14:40:36 +02:00
return 0
}
2022-04-08 19:34:54 +02:00
2022-06-01 14:40:36 +02:00
test_reseau () {
[[ -f "$OUT/mnist_main" ]] || build mnist-main
2022-06-01 14:40:36 +02:00
[[ $1 ]] || set -- "train"
[[ -f ".cache/reseau.bin" ]] || train train
"$OUT/mnist_main" test \
2022-06-01 14:40:36 +02:00
--images "data/mnist/$1-images-idx3-ubyte" \
--labels "data/mnist/$1-labels-idx1-ubyte" \
2022-05-03 10:02:47 +02:00
--modele ".cache/reseau.bin"
2022-06-01 14:40:36 +02:00
return 0
}
2022-05-03 10:02:47 +02:00
2022-06-01 14:40:36 +02:00
recognize () {
if [[ $1 ]]; then
[[ $2 ]] || set -- "$2" "text"
[[ -f "$OUT/mnist_main" ]] || build mnist-main
2022-06-01 14:40:36 +02:00
[[ -f ".cache/reseau.bin" ]] || train train
"$OUT/mnist_main" recognize \
2022-04-08 19:34:54 +02:00
--modele ".cache/reseau.bin" \
2022-06-01 14:40:36 +02:00
--in "$1" \
--out "$2"
return 0
2022-04-08 19:34:54 +02:00
else
echo "Pas de fichier d'entrée spécifié. Abandon"
2022-06-01 14:40:36 +02:00
return 1
2022-04-08 19:34:54 +02:00
fi
2022-06-01 14:40:36 +02:00
}
2022-04-08 19:34:54 +02:00
2022-06-01 14:40:36 +02:00
utils () {
[[ -f "$OUT/mnist_utils" ]] || build mnist-utils
"$OUT/mnist_utils" ${*:1}
2022-06-01 14:40:36 +02:00
return 0
}
2022-04-22 15:22:51 +02:00
2022-06-01 14:40:36 +02:00
webserver () {
[[ -f "$OUT/mnist_main" ]] || build mnist-main
2022-06-01 14:40:36 +02:00
[[ -f ".cache/reseau.bin" ]] || train train
2022-04-11 18:04:10 +02:00
FLASK_APP="src/webserver/app.py" flask run
2022-06-01 14:40:36 +02:00
return 0
}
usage () {
echo "Usage:"
echo -e "\t$0 build ( test | all | ... )"
echo -e "\t\t\tmnist: mnist"
echo -e "\t\t\t\tmnist-main"
echo -e "\t\t\t\tmnist-preview"
echo -e "\t\t\t\tmnist-utils"
echo -e "\t\t\tcnn: cnn"
echo -e "\t\t\t\tcnn-main\n"
2022-06-01 14:40:36 +02:00
echo -e "\t$0 train ( train | t10k ) ( -r | --recover )"
echo -e "\t$0 preview ( train | t10k )"
echo -e "\t$0 test_reseau ( train | t10k )\n"
echo -e "\t$0 recognize [FILENAME] ( text | json )"
echo -e "\t$0 utils ( help )\n"
echo -e "\t$0 test ( run )"
echo -e "\t$0 webserver\n"
echo -e "Les fichiers de test sont recompilés à chaque exécution,\nles autres programmes sont compilés automatiquement si manquants\n"
echo -e "La plupart des options listées ici sont juste faites pour une utilisation plus rapide des commandes fréquentes,"
echo -e "d'autres options sont uniquement disponibles via les fichiers binaires dans '$OUT'"
}
[[ $CC ]] || CC=gcc
if [[ "$CC" == "gcc" ]]; then
2022-06-01 21:30:34 +02:00
FLAGS="-std=c99 -lm -lpthread -Wall -Wextra" # GCC flags
2022-06-01 14:40:36 +02:00
elif [[ "$CC" == "nvcc" ]]; then
2022-06-01 17:13:03 +02:00
CC=compile_cuda
2022-06-01 14:40:36 +02:00
FLAGS="" # NVCC flags
else
FLAGS=""
2022-04-11 18:04:10 +02:00
fi
2022-06-01 14:45:39 +02:00
if [[ $1 && $(type "$1") = *"is a"*"function"* || $(type "$1") == *"est une fonction"* ]]; then
2022-06-01 14:40:36 +02:00
$1 ${*:2} # Call the function
else
usage
exit 1
fi;