-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
945: Inline `copyto!` to fix Ref allocations r=charleskawczynski a=charleskawczynski This PR applies ``@inline`` to `copyto!`, which was found to fix allocations with `Ref`s inside broadcast expressions, and adds tests. I'm going to try this out with ClimaAtmos to see if one more minor change is needed, too. This fixes our `bycolumn` allocations with `Ref`s from 24576 bytes to 0. Closes #946. Co-authored-by: Charles Kawczynski <kawczynski.charles@gmail.com>
- Loading branch information
Showing
4 changed files
with
74 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Test | ||
using StaticArrays, IntervalSets | ||
import ClimaCore | ||
import ClimaCore.Utilities: PlusHalf | ||
import ClimaCore.DataLayouts: IJFH | ||
import ClimaCore: | ||
Fields, slab, Domains, Topologies, Meshes, Operators, Spaces, Geometry | ||
|
||
using LinearAlgebra: norm | ||
using Statistics: mean | ||
using ForwardDiff | ||
|
||
function FieldFromNamedTuple(space, nt::NamedTuple) | ||
cmv(z) = nt | ||
return cmv.(Fields.coordinate_field(space)) | ||
end | ||
|
||
include(joinpath(@__DIR__, "util_spaces.jl")) | ||
|
||
# https://github.com/CliMA/ClimaCore.jl/issues/946 | ||
@testset "Allocations with broadcasting Refs" begin | ||
FT = Float64 | ||
function foo!(Yx::Fields.Field) | ||
Yx .= Ref(1) .+ Yx | ||
return nothing | ||
end | ||
function foocolumn!(Yx::Fields.Field) | ||
Fields.bycolumn(axes(Yx)) do colidx | ||
Yx[colidx] .= Ref(1) .+ Yx[colidx] | ||
nothing | ||
end | ||
return nothing | ||
end | ||
for space in all_spaces(FT) | ||
( | ||
space isa Spaces.ExtrudedFiniteDifferenceSpace || | ||
space isa Spaces.SpectralElementSpace1D || | ||
space isa Spaces.SpectralElementSpace2D | ||
) || continue | ||
Y = FieldFromNamedTuple(space, (; x = FT(2))) | ||
|
||
# Plain broadcast | ||
Yx = Y.x | ||
foo!(Yx) # compile first | ||
p = @allocated foo!(Yx) | ||
@test p == 0 | ||
|
||
# bycolumn | ||
foocolumn!(Yx) # compile first | ||
p = @allocated foocolumn!(Yx) | ||
@test p == 0 | ||
end | ||
end |