Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Taal: Should we use @inbounds + @boundscheck to increase standard performance? #210

Open
sloede opened this issue Oct 9, 2020 · 5 comments
Labels
performance We are greedy taal

Comments

@sloede
Copy link
Member

sloede commented Oct 9, 2020

I think we should have a discussion about our way of boundschecking in general. There are at least two possible ways:

  1. Use more @boundscheck blocks and @inbounds afterwards to make the assumptions on bounds explicit and the code fast even if the users start Julia with default arguments. While debugging some code, we can start Julia with the flag --check-bounds=yes to override @inbounds.
  2. Do not use @boundscheck or @inbounds at all. By default, Julia will check bounds on every access. Hence, users need to start Julia with --check-bounds=no (or --check-bounds=0 in @sloede's custom build 😉) - our status quo.

Personally, I favor the first option.

Originally posted by @ranocha in #203 (comment)

@sloede sloede added performance We are greedy taal labels Oct 9, 2020
@sloede sloede mentioned this issue Oct 9, 2020
@ranocha
Copy link
Member

ranocha commented Oct 9, 2020

Maybe we can use a hybrid way for now: If we want to introduce additional bounds checks, we hide them inside @boundscheck to eliminate them when using --check-bounds=no. What I have in mind is something like

Trixi.jl/src/amr/amr.jl

Lines 22 to 23 in 2c670e2

@assert length(lambda) == length(leaf_cell_ids) ("Indicator and leaf cell arrays have " *
"different length")

if isperiodic(mesh.tree) && n_l2mortars == 0 && n_ecmortars == 0
@assert n_interfaces == 2*n_elements ("For 2D and periodic domains and conforming elements, "
* "n_surf must be the same as 2*n_elem")
end

We don't use @inbounds for now, though.

@boundscheck tells Julia that the following code performs some bounds checks for security, see https://docs.julialang.org/en/v1/devdocs/boundscheck/. It doesn't perform a check like @assert. It's purpose is solely to mark some code as to be removed when bounds checking is disabled, e.g. by using --check-bounds=no. Hence, I don't want to convert @assert stuff to @boundscheck stuff but to @boundscheck @assert stuff.

@ranocha
Copy link
Member

ranocha commented Oct 12, 2020

Result: Use @boundscheck for things like

Trixi.jl/src/amr/amr.jl

Lines 22 to 23 in 2c670e2

@assert length(lambda) == length(leaf_cell_ids) ("Indicator and leaf cell arrays have " *
"different length")

but not for sanity checks such as
if isperiodic(mesh.tree) && n_l2mortars == 0 && n_ecmortars == 0
@assert n_interfaces == 2*n_elements ("For 2D and periodic domains and conforming elements, "
* "n_surf must be the same as 2*n_elem")
end

Keep the status quo of requiring julia --check-bounds=no for optimal performance.

@sloede
Copy link
Member Author

sloede commented Oct 12, 2020

From my perspective, the first example is just on the edge between being a bounds check and a logic check. However, an open-and-shut example for @inbounds to me is the following:

@boundscheck begin
@assert old_element_id >= 1
@assert size(old_u, 1) == nvariables(equations)
@assert size(old_u, 2) == nnodes(dg)
@assert size(old_u, 3) == nnodes(dg)
@assert size(old_u, 4) >= old_element_id
@assert element_id >= 1
@assert size( u, 1) == nvariables(equations)
@assert size( u, 2) == nnodes(dg)
@assert size( u, 3) == nnodes(dg)
@assert size( u, 4) >= element_id + 3
end

ranocha added a commit to ranocha/Trixi.jl that referenced this issue Oct 14, 2020
ranocha added a commit to ranocha/Trixi.jl that referenced this issue Oct 14, 2020
@ranocha ranocha mentioned this issue Nov 5, 2020
@ranocha
Copy link
Member

ranocha commented Jun 30, 2021

Just in case we decide to change the default behavior in the future: Stuff like

@inline function multiply_add_to_node_vars!(...)

should be

Base.@propagate_inbounds function multiply_add_to_node_vars!(...)

@ranocha
Copy link
Member

ranocha commented Jan 24, 2023

We might need to revisit this based on the outcome of JuliaLang/julia#48245

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance We are greedy taal
Projects
None yet
Development

No branches or pull requests

2 participants