FFLAGS = -ffree-form -std=gnu -Ofast -march=native -fopenmp
LDLIBS = -lgfortran -lm -lgomp

all: out1.txt times1.svg times2.svg
	cat $<

out1.txt: jacobi1.f main1.f Makefile
	cp -f jacobi1.f jacobi.f
	cp -f main1.f main.f
	make out.txt
	mv -f out.txt $@

out.txt:main
	./$< 4 > $@

main: main.o jacobi.o
main.o: main.f jacobi.mod
jacobi.o jacobi.mod: jacobi.f

times1.svg:jacobi1.f main.f Makefile
	cp -f jacobi1.f jacobi.f
	cp -f main1.f main.f
	make times.svg
	cp -f times.svg $@

times2.svg:jacobi2.f main.f Makefile
	cp -f jacobi2.f jacobi.f
	cp -f main2.f main.f
	make times.svg
	cp -f times.svg $@

times.svg: out.times.txt Makefile
	echo '\
	set term svg size 800,600 background rgb "white" font "Times,16";\
	set out "$@"; \
	set xlabel "matrix size"; \
	set ylabel "diagonalization time, sec"; \
	set title "diagonalization time as function of matrix size (`uname -m`)"; \
	set key left; \
	f(x)=b+(x/a)**p; \
	b=0; a=10; p=3;\
	fit f(x) "$<" via a,p; \
	T = sprintf("fit: (n/%3.0f)^{%3.1f}",a,p); \
	plot "$<" title "measurement", f(x) title T; \
	'|tee log.gpi|gnuplot

out.times.txt: main
	cat /dev/null > $@
	for n in `seq 103 2 133`; do time --format "$$n %U" --append --output $@ ./main $$n 2>/dev/null; done

clean:
	find . -type f -executable -delete
	rm -f *.o *.mod out* *.svg log* *.log

%.o %.mod: %.f
	$(FC) $(FFLAGS) -c $<
