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

Add interpolation routine to make Var.resampled_as faster #183

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

Conversation

ph-kev
Copy link
Member

@ph-kev ph-kev commented Dec 4, 2024

closes #182 - This commit adds an interpolation routine that makes resampled_as and any other functions that use interpolations faster.

Checklist

  • Add benchmarks on postprocessing pipeline for 4D OutputVars (see issue on ClimaCoupler)
  • Add results from current postprocessing pipeline to demonstrate results do not change
  • Add this to NEWS.md

Benchmarks from @time

# Latest commit on main (on first run)
490.620013 seconds (6.27 G allocations: 370.999 GiB, 5.72% gc time, 9.64% compilation time: 13% of which was recompilation)

# Latest commit on main (on second run)
449.763246 seconds (6.19 G allocations: 365.709 GiB, 6.46% gc time, 0.27% compilation time)

# This PR (on first run)
166.437237 seconds (849.41 M allocations: 151.120 GiB, 7.57% gc time, 28.15% compilation time: 14% of which was recompilation)

# This PR (on second run)
125.102550 seconds (769.52 M allocations: 145.852 GiB, 8.16% gc time, 0.96% compilation time)

The image below is from the current postprocessing pipeline using the latest commit on main.
bias_pr_all_seasons_orig_intp

The image below is from the current postprocessing pipeline using this PR.
bias_pr_all_seasons_new_intp

Copy link

codecov bot commented Dec 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.45%. Comparing base (e4494a8) to head (5e45769).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #183      +/-   ##
==========================================
+ Coverage   98.35%   98.45%   +0.10%     
==========================================
  Files          11       11              
  Lines        1215     1297      +82     
==========================================
+ Hits         1195     1277      +82     
  Misses         20       20              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ph-kev ph-kev force-pushed the kp/new-intp branch 5 times, most recently from 5e3c3be to d4e432a Compare December 5, 2024 00:36
@ph-kev ph-kev requested a review from Sbozzolo December 5, 2024 00:37
This commit adds an interpolation routine for use in ClimaAnalysis,
which seeks to replace Interpolations.jl in Var.jl. The interpolation
routine supports N-dimensional linear interpolation on a grid with
throw, flat, or periodic boundary conditions. Compared against
Interpolations.jl, the interpolation routine does not make a struct and
does not allocate anything on the heap when interpolating a point.
This commit removes Interpolations.jl from Var.jl. To do this, the
function `_make_interpolant` was removed. Three new functions are added
which are `_check_interpolant`, `interpolate_point`, and
`interpolate_points`, where the latter two functions replace the
functionality of `_make_interpolant`. Furthermore, the function
`_find_extp_bound_cond` was refactored to `_find_extp_bound_conds` which
find multiple extrapolation condtions using `_find_extp_bound_cond`
which is refactored to find the extrapolation condition for a single
point.

All functions that use an interpolant are updated to use the new
interpolation routine. The test for computing the bias in Atmos changes
to check approximately close to 0.0, due to floating point errors. The
tests that check for errors when interpolating out of bounds now check
for ErrorException instead of BoundsError.
Comment on lines +230 to +258
function linear_interpolate(
point::Number,
axes,
data::AbstractArray{FT2, N},
extp_conds,
) where {N, FT2}
point = Tuple(point...)
return linear_interpolate(point, axes, data, extp_conds)
end

function linear_interpolate(
point::AbstractVector,
axes,
data::AbstractArray{FT2, N},
extp_conds,
) where {N, FT2}
point = Tuple(coord for coord in point)
return linear_interpolate(point, axes, data, extp_conds)
end

function linear_interpolate(
point::Tuple,
axes,
data::AbstractArray{FT2, N},
extp_conds,
) where {N, FT2}
point = promote(point...)
return linear_interpolate(point, axes, data, extp_conds)
end
Copy link
Member

Choose a reason for hiding this comment

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

Can you add comments to explain what these methods are accomplishing?

Given a tuple consisting of 2-tuple, return a tuple of one element from each tuple according to bits.
"""
function get_indices(indices::NTuple{N}, bits) where {N}
return ntuple(dim -> if (bits & (1 << (dim - 1)) != 0)
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a link to where you found this bit manipulation?

Copy link
Member

@Sbozzolo Sbozzolo left a comment

Choose a reason for hiding this comment

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

Could you also move all the linear interpolation into its own module, so that it will be easier in the future to switch it to something else?

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

Successfully merging this pull request may close these issues.

Var.resampled_as too much allocations
2 participants