# 2016q3 Homework1(Compute Pi) contributed by <`Weinux`> ###### tags:`Computepi` `Weinux` ## 預期目標 * 學習透過離散微積分求圓周率 * Leibniz formula for π * 積分計算圓周率π * Function * 著手透過 SIMD 指令作效能最佳化 ## 開發環境 * OS:Lubuntu 15.10 * Linux系統版本: 4.2.0-42-generic * CPU: Intel(R) Core(TM) i5-2410M CPU @ 2.30GHz * MEM: 6GB * Cache: * L1d cache: 32K * L1i cache: 32K * L2 cache: 256K * L3 cache: 3072K --- ```c= double computePi_v1(size_t N) { double pi = 0.0; double dt = 1.0 / N; // dt = (b-a)/N, b = 1, a = 0 for (size_t i = 0; i < N; i++) { double x = (double) i / N; // x = ti = a+(b-a)*i/N = i/N pi += dt / (1.0 + x * x); // integrate 1/(1+x^2), i = 0....N } return pi * 4.0; } ``` --- ```text= CC = gcc CFLAGS = -O0 -std=gnu99 -Wall -fopenmp -mavx EXECUTABLE = \ time_test_baseline time_test_openmp_2 time_test_openmp_4 \ time_test_avx time_test_avxunroll \ benchmark_clock_gettime default: computepi.o $(CC) $(CFLAGS) computepi.o time_test.c -DBASELINE -o time_test_baseline $(CC) $(CFLAGS) computepi.o time_test.c -DOPENMP_2 -o time_test_openmp_2 $(CC) $(CFLAGS) computepi.o time_test.c -DOPENMP_4 -o time_test_openmp_4 $(CC) $(CFLAGS) computepi.o time_test.c -DAVX -o time_test_avx $(CC) $(CFLAGS) computepi.o time_test.c -DAVXUNROLL -o time_test_avxunroll $(CC) $(CFLAGS) computepi.o benchmark_clock_gettime.c -o benchmark_clock_gettime .PHONY: clean default %.o: %.c $(CC) -c $(CFLAGS) $< -o $@ check: default time ./time_test_baseline time ./time_test_openmp_2 time ./time_test_openmp_4 time ./time_test_avx time ./time_test_avxunroll gencsv: default for i in `seq 100 5000 25000`; do \ printf "%d," $$i;\ ./benchmark_clock_gettime $$i; \ done > result_clock_gettime.csv clean: rm -f $(EXECUTABLE) *.o *.s result_clock_gettime.csv ```