CXXFLAGS = -std=c++23 -Wall
LDLIBS = -lstdc++ -lm -lpthread
TIME = time --format "$$nthreads %e %U" --append --output out.times

default : Out.gnuplot.png Out.graph.png

Out.graph.png : out.times Makefile
	cat $< | while read c1 c2 c3; do echo $$c1 $$c2; done | \
	graph \
		--output-format $(subst .,,$(suffix $@)) \
		--bitmap-size 700x500 \
		--height-of-plot 0.7 \
		--font-name "HersheySans" \
		--font-size 0.06 \
		--x-label "number of threads" \
		--y-label "real time in seconds" \
		--title-font-size 0.0525 \
		--title-font-name "HersheySans" \
		--top-label "Program running time as function of number of threads" \
		--line-mode 4 \
		--symbol 4 \
		--x-limits 0.9 4.1 1 \
		--y-limits 0.0 \
	>$@

N = 800000000
out.times : main #Makefile
	>$@
	>log.main
	for nthreads in 1 2 3 4; do \
		$(TIME) ./main -n $$nthreads -N $(N) >>log.main ;\
	done

Out.gnuplot.png : out.times Makefile
	echo '\
	set terminal $(subst .,,$(suffix $@)) background "white" ;\
	set output "$@" ;\
	set tics out ;\
	set title "Program running time as function of number of threads" ;\
	set xlabel "number of threads" ;\
	set ylabel "real time in seconds" ;\
	set xtics 1 ;\
	plot [0.9:4.1][0:] \
		"$<" index 0 with linespoints title "c++"\
		;\
	' | tee log.gpi | gnuplot

clean:
	$(RM) main [Oo]ut* [Ll]og*
