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

Remove performance-/precompilation-time harmful @eval #3556

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

simone-silvestri
Copy link
Collaborator

this PR fixes the issue of having @eval inside callable functions not resolved at compile time

closes #3555

cc @vchuravy

@eval $parentM = zeros($FT, $metric_size...)
@eval $metric = OffsetArray(on_architecture($arch, $parentM), $offsets...)
end
Δxᶠᶜ = OffsetArray(zeros(FT, arch, metric_size...), offsets...)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I am questioning the use of eval from the beginning :)

You were defining new global variables.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what was the purpose of the @eval?

@vchuravy
Copy link
Collaborator

The next one I ran into is

So one alternative is to use named tuples instead of variables.

@inline function compute_reconstruction_coefficients(grid, FT, scheme; order)

    method = scheme == :Centered ? 1 : scheme == :Upwind ? 2 : 3

    rect_metrics = (:xᶠᵃᵃ, :xᶜᵃᵃ, :yᵃᶠᵃ, :yᵃᶜᵃ, :zᵃᵃᶠ, :zᵃᵃᶜ)

    if grid isa Nothing
        coeffs = (; (m => nothing for m in rect_metrics)...)
    else
        metrics = coordinates(grid)
        dirsize = (:Nx, :Nx, :Ny, :Ny, :Nz, :Nz)

        arch       = architecture(grid)
        Hx, Hy, Hz = halo_size(grid)
        new_grid   = with_halo((Hx+1, Hy+1, Hz+1), grid)
        coeffs = (; (rect_metric => calc_reconstruction_coefficients(
                FT, getfield(new_grid, metric), arch, getfield(new_grid, dir), Val(method); order = order)
            for (dir, metric, rect_metric) in zip(dirsize, metrics, rect_metrics))...)
    end

    return tuple(coeffs...) # actually return named tuple to be order invariant
end

for (dir, metric, rect_metric) in zip(dirsize, metrics, rect_metrics)
@eval $(Symbol(:coeff_ , rect_metric)) = calc_reconstruction_coefficients($FT, $new_grid.$metric, $arch, $new_grid.$dir, Val($method); order = $order)
end
coeff_xᶠᵃᵃ = calc_reconstruction_coefficients(FT, getproperty(metrics[1], new_grid), arch, new_grid.Nx, Val(method); order)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
coeff_xᶠᵃᵃ = calc_reconstruction_coefficients(FT, getproperty(metrics[1], new_grid), arch, new_grid.Nx, Val(method); order)
coeff_xᶠᵃᵃ = reconstruction_coefficients(FT, getproperty(metrics[1], new_grid), arch, new_grid.Nx, Val(method); order)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice to clean up the names while we are at it

Co-authored-by: Gregory L. Wagner <wagner.greg@gmail.com>
@vchuravy
Copy link
Collaborator

vchuravy commented Apr 19, 2024

I think we need to look at all eval usage

I hit:

function minimum_spacing(dir, grid, ℓx, ℓy, ℓz)
    spacing = eval(Symbol(dir, :spacing))
    LX, LY, LZ = map(destantiate, (ℓx, ℓy, ℓz))
    Δ = KernelFunctionOperation{LX, LY, LZ}(spacing, grid, ℓx, ℓy, ℓz)

    return minimum(Δ)
end

next. Which probably should be getglobal?

Co-authored-by: Gregory L. Wagner <wagner.greg@gmail.com>
@navidcy navidcy changed the title Remove harmful eval Remove performance-harmful @eval Apr 21, 2024
@navidcy navidcy changed the title Remove performance-harmful @eval Remove performance-/precompilation-time harmful @eval Apr 21, 2024
@navidcy navidcy added the performance 🏍️ So we can get the wrong answer even faster label Apr 21, 2024
@glwagner
Copy link
Member

@simone-silvestri are you going to shepherd this through to completion?

simone-silvestri and others added 2 commits May 14, 2024 16:35
Co-authored-by: Gregory L. Wagner <wagner.greg@gmail.com>
@simone-silvestri
Copy link
Collaborator Author

I couldn't find any other @eval inside functions so I think we are clear.
@vchuravy can you confirm?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance 🏍️ So we can get the wrong answer even faster
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@eval considered harmful
4 participants