makefile (4137B)
1 # Interface: 2 # make 3 # make build 4 # make format 5 # make run 6 7 # Compiler 8 CC=gcc 9 # CC=tcc # <= faster compilation 10 11 # Main file 12 SRC=samples.c 13 OUTPUT=out/samples 14 15 SRC_ONE_THREAD=./samples-one-thread.c 16 OUTPUT_ONE_THREAD=out/samples-one-thread 17 18 ## Dependencies 19 # Has no dependencies 20 MATH=-lm 21 22 ## Flags 23 DEBUG= #'-g' 24 STANDARD=-std=c99 25 WARNINGS=-Wall 26 OPTIMIZED=-O3 #-O3 actually gives better performance than -Ofast, at least for this version 27 OPENMP=-fopenmp 28 29 ## Formatter 30 STYLE_BLUEPRINT=webkit 31 FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) 32 33 ## make build 34 build: $(SRC) 35 $(CC) $(OPTIMIZED) $(DEBUG) $(SRC) $(OPENMP) $(MATH) -o $(OUTPUT) 36 37 static: 38 $(CC) $(OPTIMIZED) $(DEBUG) $(SRC) $(OPENMP) $(MATH) -o $(OUTPUT) 39 40 format: $(SRC) 41 $(FORMATTER) $(SRC) 42 43 run: $(SRC) $(OUTPUT) 44 OMP_NUM_THREADS=1 ./$(OUTPUT) && echo 45 46 multi: 47 OMP_NUM_THREADS=1 ./$(OUTPUT) && echo 48 OMP_NUM_THREADS=2 ./$(OUTPUT) && echo 49 OMP_NUM_THREADS=4 ./$(OUTPUT) && echo 50 OMP_NUM_THREADS=8 ./$(OUTPUT) && echo 51 OMP_NUM_THREADS=16 ./$(OUTPUT) && echo 52 53 ## Timing 54 55 time-linux: 56 @echo "Requires /bin/time, found on GNU/Linux systems" && echo 57 58 @echo "Running 100x and taking avg time: OMP_NUM_THREADS=1 $(OUTPUT)" 59 @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 60 61 @echo "Running 100x and taking avg time: OMP_NUM_THREADS=2 $(OUTPUT)" 62 @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 63 64 @echo "Running 100x and taking avg time: OMP_NUM_THREADS=4 $(OUTPUT)" 65 @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 66 67 @echo "Running 100x and taking avg time: OMP_NUM_THREADS=8 $(OUTPUT)" 68 @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 69 70 @echo "Running 100x and taking avg time: OMP_NUM_THREADS=16 $(OUTPUT)" 71 @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 72 73 time-linux-fastest: 74 @echo "Running 100x and taking avg time: OMP_NUM_THREADS=32 $(OUTPUT)" 75 @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 76 77 time-linux-simple: 78 @echo "Requires /bin/time, found on GNU/Linux systems" && echo 79 OMP_NUM_THREADS=1 /bin/time -f "Time: %es" ./$(OUTPUT) && echo 80 OMP_NUM_THREADS=2 /bin/time -f "Time: %es" ./$(OUTPUT) && echo 81 OMP_NUM_THREADS=4 /bin/time -f "Time: %es" ./$(OUTPUT) && echo 82 OMP_NUM_THREADS=8 /bin/time -f "Time: %es" ./$(OUTPUT) && echo 83 OMP_NUM_THREADS=16 /bin/time -f "Time: %es" ./$(OUTPUT) && echo 84 OMP_NUM_THREADS=32 /bin/time -f "Time: %es" ./$(OUTPUT) && echo 85 86 ## Profiling 87 88 profile-linux: 89 echo "Requires perf, which depends on the kernel version, and might be in linux-tools package or similar" 90 echo "Must be run as sudo" 91 $(CC) $(SRC) $(OPENMP) $(MATH) -o $(OUTPUT) 92 # ./$(OUTPUT) 93 # gprof: 94 # gprof $(OUTPUT) gmon.out > analysis.txt 95 # rm gmon.out 96 # vim analysis.txt 97 # rm analysis.txt 98 # perf: 99 OMP_NUM_THREADS=16 sudo perf record $(OUTPUT) 100 sudo perf report 101 rm perf.data 102 103 104 ## Install 105 debian-install-dependencies: 106 sudo apt-get install libomp-dev 107