makefile (4083B)
1 # Interface: 2 # make 3 # make build 4 # make format 5 # make run 6 7 # Compiler 8 CC=gcc 9 # CC=clang 10 # CC=tcc # <= faster compilation 11 12 # Main file 13 SRC=samples.c 14 OUTPUT=out/samples 15 16 SRC_ONE_THREAD=./samples-one-thread.c 17 OUTPUT_ONE_THREAD=out/samples-one-thread 18 19 ## Dependencies 20 # Has no dependencies 21 MATH=-lm 22 23 ## Flags 24 DEBUG= #'-g' 25 STANDARD=-std=c99 26 WARNINGS=-Wall 27 OPTIMIZED=-O3 #-O3 actually gives better performance than -Ofast, at least for this version 28 OPENMP=-fopenmp 29 30 ## Formatter 31 STYLE_BLUEPRINT=webkit 32 FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) 33 34 ## make build 35 build: $(SRC) 36 $(CC) $(OPTIMIZED) $(DEBUG) $(SRC) $(OPENMP) $(MATH) -o $(OUTPUT) 37 38 static: 39 $(CC) $(OPTIMIZED) $(DEBUG) $(SRC) $(OPENMP) $(MATH) -o $(OUTPUT) 40 41 format: $(SRC) 42 $(FORMATTER) $(SRC) 43 44 run: $(SRC) $(OUTPUT) 45 OMP_NUM_THREADS=1 ./$(OUTPUT) && echo 46 47 multi: 48 OMP_NUM_THREADS=1 ./$(OUTPUT) && echo 49 OMP_NUM_THREADS=2 ./$(OUTPUT) && echo 50 OMP_NUM_THREADS=4 ./$(OUTPUT) && echo 51 OMP_NUM_THREADS=8 ./$(OUTPUT) && echo 52 OMP_NUM_THREADS=16 ./$(OUTPUT) && echo 53 54 ## Timing 55 56 time-linux: 57 @echo "Requires /bin/time, found on GNU/Linux systems" && echo 58 59 @echo "Running 100x and taking avg time: OMP_NUM_THREADS=1 $(OUTPUT)" 60 @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 61 62 @echo "Running 100x and taking avg time: OMP_NUM_THREADS=2 $(OUTPUT)" 63 @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 64 65 @echo "Running 100x and taking avg time: OMP_NUM_THREADS=4 $(OUTPUT)" 66 @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 67 68 @echo "Running 100x and taking avg time: OMP_NUM_THREADS=8 $(OUTPUT)" 69 @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 70 71 @echo "Running 100x and taking avg time: OMP_NUM_THREADS=16 $(OUTPUT)" 72 @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 73 74 time-linux-fastest: 75 @echo "Running 100x and taking avg time: OMP_NUM_THREADS=16 $(OUTPUT)" 76 @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 77 78 time-linux-simple: 79 @echo "Requires /bin/time, found on GNU/Linux systems" && echo 80 OMP_NUM_THREADS=1 /bin/time -f "Time: %es" ./$(OUTPUT) && echo 81 OMP_NUM_THREADS=2 /bin/time -f "Time: %es" ./$(OUTPUT) && echo 82 OMP_NUM_THREADS=4 /bin/time -f "Time: %es" ./$(OUTPUT) && echo 83 OMP_NUM_THREADS=8 /bin/time -f "Time: %es" ./$(OUTPUT) && echo 84 OMP_NUM_THREADS=16 /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