time-to-botec/C-optimized/makefile

83 lines
3.3 KiB
Makefile
Raw Normal View History

2023-05-29 21:17:07 +00:00
# Interface:
# make
# make build
# make format
# make run
# Compiler
2023-05-29 22:48:25 +00:00
CC=gcc
2023-05-29 21:17:07 +00:00
# CC=tcc # <= faster compilation
# Main file
SRC=samples.c
2023-05-29 21:50:00 +00:00
OUTPUT=out/samples
2023-05-29 21:17:07 +00:00
2023-05-29 23:55:57 +00:00
SRC_ONE_THREAD=./samples-one-thread.c
OUTPUT_ONE_THREAD=out/samples-one-thread
2023-05-29 21:17:07 +00:00
## Dependencies
# Has no dependencies
2023-05-29 21:59:17 +00:00
MATH=-lm
2023-05-29 21:17:07 +00:00
## Flags
DEBUG= #'-g'
STANDARD=-std=c99
WARNINGS=-Wall
OPTIMIZED=-O3 #-O3 actually gives better performance than -Ofast, at least for this version
OPENMP=-fopenmp
2023-05-29 21:59:17 +00:00
2023-05-29 21:17:07 +00:00
## Formatter
STYLE_BLUEPRINT=webkit
FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT)
## make build
build: $(SRC)
2023-05-29 21:59:17 +00:00
$(CC) $(OPTIMIZED) $(DEBUG) $(SRC) $(OPENMP) $(MATH) -o $(OUTPUT)
2023-05-29 23:55:57 +00:00
$(CC) $(OPTIMIZED) $(DEBUG) $(SRC_ONE_THREAD) $(OPENMP) $(MATH) -o $(OUTPUT_ONE_THREAD)
2023-05-29 21:17:07 +00:00
format: $(SRC)
$(FORMATTER) $(SRC)
run: $(SRC) $(OUTPUT)
2023-05-30 00:05:18 +00:00
OMP_NUM_THREADS=1 ./$(OUTPUT) && echo
2023-05-29 23:55:57 +00:00
./$(OUTPUT_ONE_THREAD)
2023-05-29 22:48:25 +00:00
multi:
OMP_NUM_THREADS=1 ./$(OUTPUT) && echo
OMP_NUM_THREADS=2 ./$(OUTPUT) && echo
OMP_NUM_THREADS=4 ./$(OUTPUT) && echo
OMP_NUM_THREADS=8 ./$(OUTPUT) && echo
OMP_NUM_THREADS=16 ./$(OUTPUT) && echo
./$(OUTPUT_ONE_THREAD) && echo
2023-06-02 21:44:52 +00:00
time-linux-simple:
@echo "Requires /bin/time, found on GNU/Linux systems" && echo
2023-05-29 23:40:03 +00:00
OMP_NUM_THREADS=1 /bin/time -f "Time: %es" ./$(OUTPUT) && echo
OMP_NUM_THREADS=2 /bin/time -f "Time: %es" ./$(OUTPUT) && echo
OMP_NUM_THREADS=4 /bin/time -f "Time: %es" ./$(OUTPUT) && echo
OMP_NUM_THREADS=8 /bin/time -f "Time: %es" ./$(OUTPUT) && echo
OMP_NUM_THREADS=16 /bin/time -f "Time: %es" ./$(OUTPUT) && echo
2023-05-29 23:55:57 +00:00
/bin/time -f "Time: %es" ./$(OUTPUT_ONE_THREAD) && echo
2023-05-29 21:17:07 +00:00
2023-06-02 21:44:52 +00:00
time-linux:
@echo "Requires /bin/time, found on GNU/Linux systems" && echo
@echo "Running 100x and taking avg time: OMP_NUM_THREADS=1 $(OUTPUT)"
@t=$$(/usr/bin/time -f "%e" -p bash -c 'for i in {1..100}; do OMP_NUM_THREADS=1 $(OUTPUT); done' 2>&1 >/dev/null | grep real | awk '{print $$2}' ); echo "scale=2; 1000 * $$t / 100" | bc | sed "s|^|Time using 1 thread: |" | sed 's|$$|ms|' && echo
@echo "Running 100x and taking avg time: OMP_NUM_THREADS=2 $(OUTPUT)"
@t=$$(/usr/bin/time -f "%e" -p bash -c 'for i in {1..100}; do OMP_NUM_THREADS=2 $(OUTPUT); done' 2>&1 >/dev/null | grep real | awk '{print $$2}' ); echo "scale=2; 1000 * $$t / 100" | bc | sed "s|^|Time using 2 threads: |" | sed 's|$$|ms|' && echo
@echo "Running 100x and taking avg time: OMP_NUM_THREADS=4 $(OUTPUT)"
@t=$$(/usr/bin/time -f "%e" -p bash -c 'for i in {1..100}; do OMP_NUM_THREADS=4 $(OUTPUT); done' 2>&1 >/dev/null | grep real | awk '{print $$2}' ); echo "scale=2; 1000 * $$t / 100" | bc | sed "s|^|Time for 4 threads: |" | sed 's|$$|ms|' && echo
@echo "Running 100x and taking avg time: OMP_NUM_THREADS=8 $(OUTPUT)"
@t=$$(/usr/bin/time -f "%e" -p bash -c 'for i in {1..100}; do OMP_NUM_THREADS=8 $(OUTPUT); done' 2>&1 >/dev/null | grep real | awk '{print $$2}' ); echo "scale=2; 1000 * $$t / 100" | bc | sed "s|^|Time using 8 threads: |" | sed 's|$$|ms|' && echo
@echo "Running 100x and taking avg time: OMP_NUM_THREADS=16 $(OUTPUT)"
@t=$$(/usr/bin/time -f "%e" -p bash -c 'for i in {1..100}; do OMP_NUM_THREADS=16 $(OUTPUT); done' 2>&1 >/dev/null | grep real | awk '{print $$2}' ); echo "scale=2; 1000 * $$t / 100" | bc | sed "s|^|Time using 16 threads: |" | sed 's|$$|ms|' && echo
2023-06-02 19:56:50 +00:00
debian-install-dependencies:
2023-05-29 21:47:52 +00:00
sudo apt-get install libomp-dev
2023-06-02 19:56:50 +00:00