Skip to content

Commit

Permalink
Preallocate GPU interpolant
Browse files Browse the repository at this point in the history
  • Loading branch information
Sbozzolo committed Jul 11, 2024
1 parent bcb6ce0 commit d95e8b6
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions ext/InterpolationsRegridderExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import Interpolations as Intp
import ClimaCore
import ClimaCore.Fields: Adapt
import ClimaCore.Fields: ClimaComms
import ClimaCore.Fields: zeros

import ClimaUtilities.Regridders

struct InterpolationsRegridder{
SPACE <: ClimaCore.Spaces.AbstractSpace,
FIELD <: ClimaCore.Fields.Field,
BC,
GITP,
} <: Regridders.AbstractRegridder

"""ClimaCore.Space where the output Field will be defined"""
Expand All @@ -22,6 +24,10 @@ struct InterpolationsRegridder{

"""Tuple of extrapolation conditions as accepted by Interpolations.jl"""
extrapolation_bc::BC

# This is needed because Adapt moves from CPU to GPU and allocates new memory
"""Preallocated area of memory where to store the GPU interpolant (if needed)"""
_gpuitp::GITP
end

# Note, we swap Lat and Long! This is because according to the CF conventions longitude
Expand Down Expand Up @@ -69,7 +75,12 @@ function Regridders.InterpolationsRegridder(
end
end

return InterpolationsRegridder(target_space, coordinates, extrapolation_bc)
return InterpolationsRegridder(
target_space,
coordinates,
extrapolation_bc,
zeros(target_space),
)
end

"""
Expand All @@ -90,7 +101,8 @@ function Regridders.regrid(regridder::InterpolationsRegridder, data, dimensions)
)

# Move it to GPU (if needed)
gpuitp = Adapt.adapt(ClimaComms.array_type(regridder.target_space), itp)
itp._gpuitp .=
Adapt.adapt(ClimaComms.array_type(regridder.target_space), itp)

return map(regridder.coordinates) do coord
gpuitp(totuple(coord)...)
Expand Down

0 comments on commit d95e8b6

Please sign in to comment.