Skip to content

Commit

Permalink
Merge pull request #316 from mabarnes/inbounds-loop-macro
Browse files Browse the repository at this point in the history
@inbounds in loop macros
  • Loading branch information
johnomotani authored Feb 5, 2025
2 parents 801d2d2 + e5b6080 commit 8c90c50
Show file tree
Hide file tree
Showing 27 changed files with 498 additions and 443 deletions.
28 changes: 14 additions & 14 deletions .github/workflows/parallel_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ jobs:
- uses: julia-actions/cache@v2
- run: |
touch Project.toml
julia --project -O3 --check-bounds=no -e 'import Pkg; Pkg.add(["MPI", "MPIPreferences", "PackageCompiler"]); using MPIPreferences; MPIPreferences.use_jll_binary("OpenMPI_jll")'
julia --project -O3 --check-bounds=no -e 'using MPI; MPI.install_mpiexecjl(; destdir=".")'
julia --project -O3 --check-bounds=no -e 'import Pkg; Pkg.add(["NCDatasets", "Random", "SpecialFunctions", "StatsBase", "Test"]); Pkg.develop(path="moment_kinetics/")'
julia --project -O3 --check-bounds=no -e 'import Pkg; Pkg.precompile()'
julia --project -O3 --check-bounds=no precompile.jl
julia --project -O3 -e 'import Pkg; Pkg.add(["MPI", "MPIPreferences", "PackageCompiler"]); using MPIPreferences; MPIPreferences.use_jll_binary("OpenMPI_jll")'
julia --project -O3 -e 'using MPI; MPI.install_mpiexecjl(; destdir=".")'
julia --project -O3 -e 'import Pkg; Pkg.add(["NCDatasets", "Random", "SpecialFunctions", "StatsBase", "Test"]); Pkg.develop(path="moment_kinetics/")'
julia --project -O3 -e 'import Pkg; Pkg.precompile()'
julia --project -O3 precompile.jl
# Need to use openmpi so that we can use `--oversubscribe` to allow using more MPI ranks than physical cores
./mpiexecjl -np 3 --oversubscribe julia -J moment_kinetics.so --project -O3 --check-bounds=no moment_kinetics/test/runtests.jl --ci --debug 1
./mpiexecjl -np 4 --oversubscribe julia -J moment_kinetics.so --project -O3 --check-bounds=no moment_kinetics/test/runtests.jl --ci --debug 1
./mpiexecjl -np 2 --oversubscribe julia -J moment_kinetics.so --project -O3 --check-bounds=no moment_kinetics/test/runtests.jl --ci --debug 1 --long
./mpiexecjl -np 3 --oversubscribe julia -J moment_kinetics.so --project -O3 moment_kinetics/test/runtests.jl --ci --debug 1
./mpiexecjl -np 4 --oversubscribe julia -J moment_kinetics.so --project -O3 moment_kinetics/test/runtests.jl --ci --debug 1
./mpiexecjl -np 2 --oversubscribe julia -J moment_kinetics.so --project -O3 moment_kinetics/test/runtests.jl --ci --debug 1 --long
# Note: MPI.jl's default implementation is mpich, which has a similar option
# `--with-device=ch3:sock`, but that needs to be set when compiling mpich.
shell: bash
Expand All @@ -47,13 +47,13 @@ jobs:
- uses: julia-actions/cache@v2
- run: |
touch Project.toml
julia --project -O3 --check-bounds=no -e 'import Pkg; Pkg.add(["MPI", "MPIPreferences", "PackageCompiler"]); using MPIPreferences; MPIPreferences.use_jll_binary("OpenMPI_jll")'
julia --project -O3 --check-bounds=no -e 'using MPI; MPI.install_mpiexecjl(; destdir=".")'
julia --project -O3 --check-bounds=no -e 'import Pkg; Pkg.add(["NCDatasets", "Random", "SpecialFunctions", "StatsBase", "Test"]); Pkg.develop(path="moment_kinetics/")'
julia --project -O3 --check-bounds=no -e 'import Pkg; Pkg.precompile()'
julia --project -O3 --check-bounds=no precompile.jl
julia --project -O3 -e 'import Pkg; Pkg.add(["MPI", "MPIPreferences", "PackageCompiler"]); using MPIPreferences; MPIPreferences.use_jll_binary("OpenMPI_jll")'
julia --project -O3 -e 'using MPI; MPI.install_mpiexecjl(; destdir=".")'
julia --project -O3 -e 'import Pkg; Pkg.add(["NCDatasets", "Random", "SpecialFunctions", "StatsBase", "Test"]); Pkg.develop(path="moment_kinetics/")'
julia --project -O3 -e 'import Pkg; Pkg.precompile()'
julia --project -O3 precompile.jl
# Need to use openmpi so that we can use `--oversubscribe` to allow using more MPI ranks than physical cores
./mpiexecjl -np 4 --oversubscribe julia -J moment_kinetics.so --project -O3 --check-bounds=no moment_kinetics/test/runtests.jl --ci --debug 1
./mpiexecjl -np 4 --oversubscribe julia -J moment_kinetics.so --project -O3 moment_kinetics/test/runtests.jl --ci --debug 1
# Note: MPI.jl's default implementation is mpich, which has a similar option
# `--with-device=ch3:sock`, but that needs to be set when compiling mpich.
shell: bash
16 changes: 16 additions & 0 deletions docs/src/developing.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,22 @@ communicator is `comm_block[]`).
See also notes on debugging the 'anyv' parallelisation: [Collision operator and
'anyv' region](@ref).

## Bounds checking

For best performance (i.e. 'production' runs), it is important that bounds
checks not be included on array accesses. It should be possible to do this by
running `julia` with the flag `--check-bounds=no`, but this flag has negative
effects on the core Julia code and compiler, and works less well in Julia
versions 1.10 and 1.11. As a workaround/alternative, the `@loop_*` macros
described in the previous section wrap the contained code with an `@inbounds`
macro (which disables bounds checks within the block, but the effect of
`@inbounds` does not propagate down into functions called within the block). If
performance-critical code that you write is within an `@loop`, then you do not
need to do anything. However if it is not within an `@loop`, then you should
add `@inbounds begin ... end` around any performance critical code. You can see
examples of this being done in
[`moment_kinetics.fokker_planck_calculus`](@ref).

## [Parallel I/O](@id parallel_io_section)

The code provides an option to use parallel I/O, which allows all output to be
Expand Down
2 changes: 1 addition & 1 deletion machines/archer/jobscript-precompile-no-run.template
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ export SRUN_CPUS_PER_TASK=$SLURM_CPUS_PER_TASK

echo "precompiling $(date)"

bin/julia --project -O3 --check-bounds=no precompile-no-run.jl
bin/julia --project -O3 precompile-no-run.jl

echo "finished! $(date)"
2 changes: 1 addition & 1 deletion machines/archer/jobscript-precompile.template
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ export SRUN_CPUS_PER_TASK=$SLURM_CPUS_PER_TASK

echo "precompiling $(date)"

bin/julia --project -O3 --check-bounds=no precompile.jl
bin/julia --project -O3 precompile.jl

echo "finished! $(date)"
2 changes: 1 addition & 1 deletion machines/archer/jobscript-restart.template
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ export SRUN_CPUS_PER_TASK=$SLURM_CPUS_PER_TASK

echo "running INPUTFILE $(date)"

srun --distribution=block:block --hint=nomultithread --ntasks=$SLURM_NTASKS bin/julia -Jmoment_kinetics.so --project -O3 --check-bounds=no run_moment_kinetics.jl --restart INPUTFILE RESTARTFROM
srun --distribution=block:block --hint=nomultithread --ntasks=$SLURM_NTASKS bin/julia -Jmoment_kinetics.so --project -O3 run_moment_kinetics.jl --restart INPUTFILE RESTARTFROM

echo "finished INPUTFILE $(date)"
2 changes: 1 addition & 1 deletion machines/archer/jobscript-run.template
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ export SRUN_CPUS_PER_TASK=$SLURM_CPUS_PER_TASK

echo "running INPUTFILE $(date)"

srun --distribution=block:block --hint=nomultithread --ntasks=$SLURM_NTASKS bin/julia -Jmoment_kinetics.so --project -O3 --check-bounds=no run_moment_kinetics.jl INPUTFILE
srun --distribution=block:block --hint=nomultithread --ntasks=$SLURM_NTASKS bin/julia -Jmoment_kinetics.so --project -O3 run_moment_kinetics.jl INPUTFILE

echo "finished INPUTFILE $(date)"
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ source julia.env

echo "precompiling $(date)"

bin/julia --project -O3 --check-bounds=no precompile-no-run.jl
bin/julia --project -O3 precompile-no-run.jl

echo "finished! $(date)"
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ source julia.env

echo "precompiling $(date)"

bin/julia --project -O3 --check-bounds=no precompile.jl
bin/julia --project -O3 precompile.jl

echo "finished! $(date)"
2 changes: 1 addition & 1 deletion machines/generic-batch-template/jobscript-restart.template
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ source julia.env
echo "running INPUTFILE $(date)"

# May need to change this if mpirun` is not what should be used on your system
mpirun -np $SLURM_NTASKS bin/julia -Jmoment_kinetics.so --project -O3 --check-bounds=no run_moment_kinetics.jl --restart INPUTFILE RESTARTFROM
mpirun -np $SLURM_NTASKS bin/julia -Jmoment_kinetics.so --project -O3 run_moment_kinetics.jl --restart INPUTFILE RESTARTFROM

echo "finished INPUTFILE $(date)"
2 changes: 1 addition & 1 deletion machines/generic-batch-template/jobscript-run.template
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ source julia.env
echo "running INPUTFILE $(date)"

# May need to change this if mpirun` is not what should be used on your system
mpirun -np $SLURM_NTASKS bin/julia -Jmoment_kinetics.so --project -O3 --check-bounds=no run_moment_kinetics.jl INPUTFILE
mpirun -np $SLURM_NTASKS bin/julia -Jmoment_kinetics.so --project -O3 run_moment_kinetics.jl INPUTFILE

echo "finished INPUTFILE $(date)"
2 changes: 1 addition & 1 deletion machines/machine_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ SEPARATE_POSTPROC_PROJECTS=$(bin/julia --project machines/shared/get_mk_preferen
if [[ $BATCH_SYSTEM -eq 0 || $SEPARATE_POSTPROC_PROJECTS == "y" ]]; then
# Batch systems can (conveniently) use different optimization flags for
# running simulations and for post-processing.
OPTIMIZATION_FLAGS="-O3 --check-bounds=no"
OPTIMIZATION_FLAGS="-O3"
POSTPROC_OPTIMIZATION_FLAGS="-O3"
else
# On interactive systems which use the same project for running simulations
Expand Down
2 changes: 1 addition & 1 deletion machines/marconi/jobscript-precompile-no-run.template
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ source julia.env

echo "precompiling $(date)"

bin/julia --project -O3 --check-bounds=no precompile-no-run.jl
bin/julia --project -O3 precompile-no-run.jl

echo "finished! $(date)"
2 changes: 1 addition & 1 deletion machines/marconi/jobscript-precompile.template
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ source julia.env

echo "precompiling $(date)"

bin/julia --project -O3 --check-bounds=no precompile.jl
bin/julia --project -O3 precompile.jl

echo "finished! $(date)"
2 changes: 1 addition & 1 deletion machines/marconi/jobscript-restart.template
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ source julia.env

echo "running INPUTFILE $(date)"

mpirun -np $SLURM_NTASKS bin/julia -Jmoment_kinetics.so --project -O3 --check-bounds=no run_moment_kinetics.jl --restart INPUTFILE RESTARTFROM
mpirun -np $SLURM_NTASKS bin/julia -Jmoment_kinetics.so --project -O3 run_moment_kinetics.jl --restart INPUTFILE RESTARTFROM

echo "finished INPUTFILE $(date)"
2 changes: 1 addition & 1 deletion machines/marconi/jobscript-run.template
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ source julia.env

echo "running INPUTFILE $(date)"

mpirun -np $SLURM_NTASKS bin/julia -Jmoment_kinetics.so --project -O3 --check-bounds=no run_moment_kinetics.jl INPUTFILE
mpirun -np $SLURM_NTASKS bin/julia -Jmoment_kinetics.so --project -O3 run_moment_kinetics.jl INPUTFILE

echo "finished INPUTFILE $(date)"
2 changes: 1 addition & 1 deletion machines/shared/submit-restart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ sed -e "s|NODES|$NODES|" -e "s|RUNTIME|$RUNTIME|" -e "s|ACCOUNT|$ACCOUNT|" -e "s

if [[ "$WARN_OLD_SYSIMAGE" -eq 0 ]]; then
# Check that source code has not been changed since moment_kinetics.so was created
bin/julia --project -O3 --check-bounds=no moment_kinetics/src/check_so_newer_than_code.jl moment_kinetics.so
bin/julia --project -O3 moment_kinetics/src/check_so_newer_than_code.jl moment_kinetics.so
fi

if [[ $SUBMIT -eq 0 ]]; then
Expand Down
2 changes: 1 addition & 1 deletion machines/shared/submit-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ sed -e "s|NODES|$NODES|" -e "s|RUNTIME|$RUNTIME|" -e "s|ACCOUNT|$ACCOUNT|" -e "s

if [[ "$WARN_OLD_SYSIMAGE" -eq 0 ]]; then
# Check that source code has not been changed since moment_kinetics.so was created
bin/julia --project -O3 --check-bounds=no moment_kinetics/src/check_so_newer_than_code.jl moment_kinetics.so
bin/julia --project -O3 moment_kinetics/src/check_so_newer_than_code.jl moment_kinetics.so
fi

if [[ $SUBMIT -eq 0 ]]; then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using moment_kinetics.analysis: check_Chodura_condition

"""
Chodura_condition_plots(run_info::Tuple; plot_prefix)
Chodura_condition_plots(run_info; plot_prefix=nothing, axes=nothing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ function plots_for_variable(run_info, variable_name; plot_prefix, has_rdim=true,
try
variable = get_variable(run_info, variable_name)
catch e
return makie_post_processing_error_handler(
e,
"plots_for_variable() failed for $variable_name - could not load data.")
if isa(e, KeyError)
println("Key $(e.key) not found when loading $variable_name - probably not "
* "present in output")
return nothing
else
return makie_post_processing_error_handler(
e,
"plots_for_variable() failed for $variable_name - could not load data.")
end
end

if variable_name em_variables
Expand Down
Loading

0 comments on commit 8c90c50

Please sign in to comment.