tipe/make.sh

169 lines
4.0 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
cp $1 "$1"u
nvcc "$1"u ${*:1}
rm "$1"u
}
2022-06-01 14:40:36 +02:00
build () {
2022-04-22 15:22:51 +02:00
mkdir -p "$OUT"
2022-06-01 14:40:36 +02:00
[[ $1 ]] || set "main"
if [[ $1 == "main" ]]; then
2022-04-22 15:22:51 +02:00
echo "Compilation de src/mnist/main.c"
2022-06-01 14:40:36 +02:00
$CC src/mnist/main.c -o "$OUT/main" $FLAGS
2022-04-22 15:22:51 +02:00
echo "Fait."
2022-06-01 14:40:36 +02:00
return 0
elif [[ $1 == "preview" ]]; then
2022-03-27 15:03:13 +02:00
echo "Compilation de src/mnist/preview.c"
2022-06-01 14:40:36 +02:00
$CC src/mnist/preview.c -o "$OUT/preview_mnist" $FLAGS
2022-03-27 15:03:13 +02:00
echo "Fait."
2022-06-01 14:40:36 +02:00
return 0
elif [[ $1 == "test" ]]; then
2022-05-16 17:26:04 +02:00
for i in "test/"*".c"; do
echo "Compilation de $i"
2022-06-01 14:40:36 +02:00
$CC "$i" -o "$OUT/test_$(echo $i | awk -F. '{print $1}' | awk -F/ '{print $NF}')" $FLAGS
2022-04-22 15:22:51 +02:00
echo "Fait."
done
2022-06-01 14:40:36 +02:00
return 0
elif [[ $1 == "utils" ]]; then
2022-05-16 17:26:04 +02:00
echo "Compilation de src/mnist/utils.c"
2022-06-01 14:40:36 +02:00
$CC "src/mnist/utils.c" -o "$OUT/utils" $FLAGS
2022-05-16 17:26:04 +02:00
echo "Fait."
2022-06-01 14:40:36 +02:00
return 0
2022-04-22 15:22:51 +02:00
else
2022-06-01 14:40:36 +02:00
build main
build preview
build test
build utils
return 0
2022-04-22 15:22:51 +02:00
fi
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 () {
if [[ ! $1 ]]; then
build preview
return 0
elif [[ $1 == "train" ]]; then
2022-04-22 15:22:51 +02:00
[[ -f "$OUT/preview_mnist" ]] || $0 build preview
2022-04-08 19:34:54 +02:00
"$OUT/preview_mnist" data/mnist/train-images-idx3-ubyte data/mnist/train-labels-idx1-ubyte
2022-06-01 14:40:36 +02:00
return 0
elif [[ $1 == "t10k" ]]; then
2022-04-22 15:22:51 +02:00
[[ -f "$OUT/preview_mnist" ]] || $0 build preview
2022-04-08 19:34:54 +02:00
"$OUT/preview_mnist" data/mnist/t10k-images-idx3-ubyte data/mnist/t10k-labels-idx1-ubyte
2022-06-01 14:40:36 +02:00
return 0
2022-03-21 17:18:49 +01:00
fi
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 () {
if [[ ! $1 ]]; then
build test
return 0
elif [[ $1 == "run" ]]; then
build test
2022-03-27 14:54:20 +02:00
mkdir -p .test-cache
2022-05-14 17:05:13 +02:00
for i in "$OUT/test_"*; do
2022-03-27 14:42:00 +02:00
echo "--- $i ---"
$i
done
2022-05-16 17:26:04 +02:00
for i in "test/"*".sh"; do
echo "--- $i ---"
chmod +x "$i"
"$i" "$OUT" "$0"
done
2022-06-01 14:40:36 +02:00
return 0
2022-03-27 14:42:00 +02:00
fi
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/main" ]] || build main
[[ $1 ]] || set -- "train"
[[ $2 == "-r" || $2 == "--recover" ]] && RECOVER="-r .cache/reseau.bin"
2022-04-08 19:34:54 +02:00
mkdir -p .cache
"$OUT/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/main" ]] || build main
[[ $1 ]] || set -- "train"
[[ -f ".cache/reseau.bin" ]] || train train
2022-05-03 10:02:47 +02:00
"$OUT/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/main" ]] || build main
[[ -f ".cache/reseau.bin" ]] || train train
2022-04-08 19:34:54 +02:00
"$OUT/main" recognize \
--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/utils" ]] || build utils
"$OUT/utils" ${*:1}
return 0
}
2022-04-22 15:22:51 +02:00
2022-06-01 14:40:36 +02:00
webserver () {
[[ -f "$OUT/main" ]] || build main
[[ -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 ( main | preview | train | utils | all )\n"
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
FLAGS="-std=c99 -lm -lpthread" # GCC flags
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
echo $(type "$1")
exit 1
fi;