pep-dortmund / toolbox-workshop

Materialien zum PeP et al. Toolbox-Workshop
http://toolbox.pep-dortmund.org
Other
25 stars 6 forks source link

Parallization of Makefiles #408

Open LuckyJosh opened 1 month ago

LuckyJosh commented 1 month ago

Another thing I found working on the lab report:

The context: For the most extensive solution (exporting every value from the python script into a tex file) I added the manual synchronization from the last `make` -slide: ```make build/parameter-g_ball.tex: build/plot_ball_g.pdf build/parameter-t0-g_ball.tex: build/parameter-g_ball.tex build/plot-g_cylinder.pdf: build/parameter-t0-g_ball.tex build/parameter-g_cylinder.tex: build/plot_cylinder_g.pdf build/parameter-t0-g_cylinder.tex: build/parameter-g_cylinder.tex build/plot-I_ball.pdf: build/parameter-g_cylinder.tex build/parameter-I_ball.tex: build/plot_ball_I.pdf build/parameter-t0-I_ball.tex: build/parameter-I_ball.tex build/plot-I_cylinder.pdf: build/parameter-t0-I_ball.tex build/parameter-I_cylinder.tex: build/plot_cylinder_I.pdf build/parameter-t0-I_cylinder.tex: build/parameter-I_cylinder.tex build/table_all-measurements.tex: build/parameter-t0-I_cylinder.tex build/table_averaged-measurements.tex: build/table_all-measurements.tex build/tracklength.tex: build/table_averaged-measurements.tex build/framerate.tex: build/tracklength.tex build/mass_ball.tex: build/framerate.tex build/radius_ball.tex: build/mass_ball.tex build/theoretical-I_ball.tex: build/radius_ball.tex build/mass_cylinder.tex: build/theoretical-I_ball.tex build/radius-inner_cylinder.tex: build/mass_cylinder.tex build/radius-outer_cylinder.tex: build/radius-inner_cylinder.tex build/theoretical-I_cylinder.tex: build/radius-outer_cylinder.tex ``` does not look very nice in my opinion ... and it is also somewhat tedious to write ... though come to think of it, might be a prime example for multiline editing :thinking:

I found a relatively recent addition to Make (4.3), so called 'grouped targets' docs

grouped targets are all created by just on execution of the specific rule. So the manual changing for parallel processing is not necessary.

Example ```make # Makefile all: result.txt a.txt b.txt c.txt d.txt: touch a.txt b.txt c.txt d.txt result.txt: a.txt b.txt c.txt d.txt touch result.txt ``` results in ```shell > make -j4 touch a.txt b.txt c.txt d.txt touch a.txt b.txt c.txt d.txt touch a.txt b.txt c.txt d.txt touch a.txt b.txt c.txt d.txt touch result.txt ``` ```make # Makefile with grouped targets all: result.txt # ↓ This & is all to add a.txt b.txt c.txt d.txt &: touch a.txt b.txt c.txt d.txt result.txt: a.txt b.txt c.txt d.txt touch result.txt ``` ```shell > make -j4 touch a.txt b.txt c.txt d.txt touch result.txt ```
LuckyJosh commented 1 month ago

And a small addition to that: Combined with a variable to list all the targets the Makefile looks very clean:

# Makefile
all: result.txt
targets = a.txt b.txt c.txt d.txt

$(targets) &: 
    touch a.txt b.txt c.txt d.txt 

result.txt: $(targets)
    touch result.txt
Same Example as in the details above ```make # Makefile 'old version' all: build/v16516.pdf # hier Python-Skripte: build/plot-g_ball.pdf: auswertung.py ../matplotlibrc ../header-matplotlib.tex data/*| build # so that matplotlib can find the tex header when running # LaTeX in the tmp directory # and set the matplotlibrc TEXINPUTS=$$(pwd)/..: MATPLOTLIBRC=../matplotlibrc python auswertung.py build/parameter-g_ball.tex: build/plot_ball_g.pdf build/parameter-t0-g_ball.tex: build/parameter-g_ball.tex build/plot-g_cylinder.pdf: build/parameter-t0-g_ball.tex build/parameter-g_cylinder.tex: build/plot_cylinder_g.pdf build/parameter-t0-g_cylinder.tex: build/parameter-g_cylinder.tex build/plot-I_ball.pdf: build/parameter-g_cylinder.tex build/parameter-I_ball.tex: build/plot_ball_I.pdf build/parameter-t0-I_ball.tex: build/parameter-I_ball.tex build/plot-I_cylinder.pdf: build/parameter-t0-I_ball.tex build/parameter-I_cylinder.tex: build/plot_cylinder_I.pdf build/parameter-t0-I_cylinder.tex: build/parameter-I_cylinder.tex build/table_all-measurements.tex: build/parameter-t0-I_cylinder.tex build/table_averaged-measurements.tex: build/table_all-measurements.tex build/tracklength.tex: build/table_averaged-measurements.tex build/framerate.tex: build/tracklength.tex build/mass_ball.tex: build/framerate.tex build/radius_ball.tex: build/mass_ball.tex build/theoretical-I_ball.tex: build/radius_ball.tex build/mass_cylinder.tex: build/theoretical-I_ball.tex build/radius-inner_cylinder.tex: build/mass_cylinder.tex build/radius-outer_cylinder.tex: build/radius-inner_cylinder.tex build/theoretical-I_cylinder.tex: build/radius-outer_cylinder.tex # hier weitere Abhängigkeiten für build/vXXX.pdf deklarieren: # These dependencies arent even complete build/v16516.pdf: build/plot_ball_g.pdf build/plot_ball_I.pdf build/plot_cylinder_g.pdf build/plot_cylinder_I.pdf build/v16516.pdf: FORCE | build # to find header and bib files in the main directory TEXINPUTS=..: \ BIBINPUTS=..: \ max_print_line=1048576 \ latexmk \ --lualatex \ --output-directory=build \ --interaction=nonstopmode \ --halt-on-error \ v16516.tex build: mkdir -p build clean: rm -rf build FORCE: .PHONY: all clean ``` ```make # Makefile 'new version' all: build/v16516.pdf script_targets = build/plot-g_ball.pdf \ build/parameter-g_ball.tex\ build/parameter-t0-g_ball.tex\ build/plot-g_cylinder.pdf\ build/parameter-g_cylinder.tex\ build/parameter-t0-g_cylinder.tex\ build/plot-I_ball.pdf\ build/parameter-I_ball.tex\ build/parameter-t0-I_ball.tex\ build/plot-I_cylinder.pdf\ build/parameter-I_cylinder.tex\ build/parameter-t0-I_cylinder.tex\ build/framerate.tex\ build/theoretical-I_ball.tex\ build/theoretical-I_cylinder.tex\ build/tracklength.tex\ build/mass_ball.tex\ build/mass_cylinder.tex\ build/radius_ball.tex\ build/radius-inner_cylinder.tex\ build/radius-outer_cylinder.tex\ build/table_all-measurements.tex\ build/table_averaged-measurements.tex # hier Python-Skripte: $(script_targets): auswertung.py ../matplotlibrc ../header-matplotlib.tex data/*| build # so that matplotlib can find the tex header when running # LaTeX in the tmp directory # and set the matplotlibrc TEXINPUTS=$$(pwd)/..: MATPLOTLIBRC=../matplotlibrc python auswertung.py # hier weitere Abhängigkeiten für build/vXXX.pdf deklarieren: build/v16516.pdf: $(script_targets) build/v16516.pdf: FORCE | build # to find header and bib files in the main directory TEXINPUTS=..: \ BIBINPUTS=..: \ max_print_line=1048576 \ latexmk \ --lualatex \ --output-directory=build \ --interaction=nonstopmode \ --halt-on-error \ v16516.tex build: mkdir -p build clean: rm -rf build FORCE: .PHONY: all clean ```