# 2016q3 Homework 1 (raytracing) contributed by <`Weinux`> ###### tags: `raytracing` `Weinux` ## 開發環境 * 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 ## gprof * gprof是個可以拿來看程式執行時,跑過哪些function和執行比率的一個實用工具。 * 從這些資訊中,我們可以觀察 到哪些function特別花時間,和被呼叫特別多次,藉由這些東西,可以針對特定function去做改善,因而得到更好的表現。 * 以raytracing-origin為例,make時得先立個flag ``` make PROFILE=1 ``` * **Makefile** ```text= EXEC = raytracing .PHONY: all all: $(EXEC) CC ?= gcc CFLAGS = \ -std=gnu99 -Wall -O0 -g LDFLAGS = \ -lm ifeq ($(strip $(PROFILE)),1) PROF_FLAGS = -pg CFLAGS += $(PROF_FLAGS) LDFLAGS += $(PROF_FLAGS) endif OBJS := \ objects.o \ raytracing.o \ main.o %.o: %.c $(CC) $(CFLAGS) -c -o $@ $< $(EXEC): $(OBJS) $(CC) -o $@ $^ $(LDFLAGS) main.o: use-models.h use-models.h: models.inc Makefile @echo '#include "models.inc"' > use-models.h @egrep "^(light|sphere|rectangular) " models.inc | \ sed -e 's/^light /append_light/g' \ -e 's/light[0-9]/(\&&, \&lights);/g' \ -e 's/^sphere /append_sphere/g' \ -e 's/sphere[0-9]/(\&&, \&spheres);/g' \ -e 's/^rectangular /append_rectangular/g' \ -e 's/rectangular[0-9]/(\&&, \&rectangulars);/g' \ -e 's/ = {//g' >> use-models.h check: $(EXEC) @./$(EXEC) && diff -u baseline.ppm out.ppm || (echo Fail; exit) @echo "Verified OK" clean: $(RM) $(EXEC) $(OBJS) use-models.h \ out.ppm gmon.out ``` ```text= Flat profile: Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls s/call s/call name 20.85 0.65 0.65 56956357 0.00 0.00 subtract_vector 15.71 1.14 0.49 69646433 0.00 0.00 dot_product 13.79 1.57 0.43 10598450 0.00 0.00 normalize 10.10 1.89 0.32 31410180 0.00 0.00 multiply_vector 8.02 2.14 0.25 13861875 0.00 0.00 rayRectangularIntersection 6.90 2.35 0.22 17836094 0.00 0.00 add_vector 6.09 2.54 0.19 4620625 0.00 0.00 ray_hit_object 3.53 2.65 0.11 17821809 0.00 0.00 cross_product 2.73 2.74 0.09 4221152 0.00 0.00 multiply_vectors 2.57 2.82 0.08 1048576 0.00 0.00 ray_color 1.92 2.88 0.06 13861875 0.00 0.00 raySphereIntersection 1.60 2.93 0.05 2110576 0.00 0.00 compute_specular_diffuse 1.44 2.97 0.05 2520791 0.00 0.00 idx_stack_top 1.28 3.01 0.04 1241598 0.00 0.00 protect_color_overflow 0.96 3.04 0.03 2110576 0.00 0.00 localColor 0.80 3.07 0.03 3838091 0.00 0.00 length 0.64 3.09 0.02 1 0.02 3.12 raytracing 0.32 3.10 0.01 1241598 0.00 0.00 refraction 0.32 3.11 0.01 1204003 0.00 0.00 idx_stack_push 0.32 3.12 0.01 1048576 0.00 0.00 rayConstruction 0.16 3.12 0.01 37595 0.00 0.00 idx_stack_pop 0.00 3.12 0.00 2558386 0.00 0.00 idx_stack_empty 0.00 3.12 0.00 1241598 0.00 0.00 reflection 0.00 3.12 0.00 1048576 0.00 0.00 idx_stack_init 0.00 3.12 0.00 113297 0.00 0.00 fresnel 0.00 3.12 0.00 3 0.00 0.00 append_rectangular 0.00 3.12 0.00 3 0.00 0.00 append_sphere 0.00 3.12 0.00 2 0.00 0.00 append_light 0.00 3.12 0.00 1 0.00 0.00 calculateBasisVectors 0.00 3.12 0.00 1 0.00 0.00 delete_light_list ``` ``` ./raytracing gprof ./raytracing | less ``` Raytracing結果圖 ![](https://i.imgur.com/2xNrnRd.png) ## 總結 ## References