From 10b26024ca69f4d9bb4079a830c5995e1667186b Mon Sep 17 00:00:00 2001 From: albert-de-montserrat Date: Tue, 5 Dec 2023 00:09:49 +0100 Subject: [PATCH 1/4] test allocations --- test/test_Density.jl | 20 +++++++++----------- test/test_Energy.jl | 10 +++++----- test/test_Plasticity.jl | 2 +- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/test/test_Density.jl b/test/test_Density.jl index 377766caa..855e2b130 100644 --- a/test/test_Density.jl +++ b/test/test_Density.jl @@ -52,30 +52,26 @@ using Test, GeoParams, StaticArrays x = ConstantDensity() num_alloc = @allocated compute_density!(rho, x, args) - num_alloc = @allocated compute_density!(rho, x, args) - # @show num_alloc - # @test num_alloc == 0 + @test num_alloc == 0 #Test allocations using ρ alias ρ!(rho, x, args) num_alloc = @allocated ρ!(rho, x, args) - # @test num_alloc == 0 + @test num_alloc == 0 # This does NOT allocate if I test this with @btime; # yet it does while running the test here x = PT_Density() compute_density!(rho, x, args) num_alloc = @allocated compute_density!(rho, x, args) - # @show num_alloc - # @test num_alloc ≤ 32 + @test num_alloc == 0 # This does NOT allocate if I test this with @btime; # yet it does while running the test here x = Compressible_Density() compute_density!(rho, x, args) num_alloc = @allocated compute_density!(rho, x, args) - # @show num_alloc - # @test num_alloc ≤ 32 + @test num_alloc == 0 # Read Phase diagram interpolation object fname = "test_data/Peridotite_dry.in" @@ -185,15 +181,17 @@ using Test, GeoParams, StaticArrays # Test computing density when Mat_tup1 is provided as a tuple compute_density!(rho, Mat_tup1, Phases, args) num_alloc = @allocated compute_density!(rho, Mat_tup1, Phases, args) # 287.416 μs (0 allocations: 0 bytes) + @test num_alloc == 0 + @test sum(rho) / 400^2 ≈ 2945.000013499999 - # @test num_alloc ≤ 32 + @test num_alloc == 0 #Same test using function alias rho = zeros(size(Phases)) ρ!(rho, Mat_tup1, Phases, args) num_alloc = @allocated compute_density!(rho, Mat_tup1, Phases, args) @test sum(rho) / 400^2 ≈ 2945.000013499999 - # @test num_alloc ≤ 32 + @test num_alloc == 0 # Test for single phase compute_density(MatParam, 1, (P=P[1], T=T[1])) @@ -214,7 +212,7 @@ using Test, GeoParams, StaticArrays num_alloc = @allocated compute_density!(rho, Mat_tup1, PhaseRatio, args) # 136.776 μs (0 allocations: 0 bytes) @test sum(rho) / 400^2 ≈ 2945.000013499999 - # @test num_alloc ≤ 32 # for some reason this does indicate allocations but @btime does not + @test num_alloc == 0 # for some reason this does indicate allocations but @btime does not # Test calling the routine with only pressure as input. # This is ok for Mat_tup1, as it only has constant & P-dependent densities. diff --git a/test/test_Energy.jl b/test/test_Energy.jl index 192c3547a..80d9314c3 100644 --- a/test/test_Energy.jl +++ b/test/test_Energy.jl @@ -97,7 +97,7 @@ using GeoParams num_alloc = @allocated compute_heatcapacity!(Cp, Mat_tup, Phases, args) @test sum(Cp[1, 1, :]) ≈ 121399.0486067196 - # @test num_alloc <= 32 + @test num_alloc == 0 # test if we provide phase ratios PhaseRatio = zeros(n, n, n, 3) @@ -109,7 +109,7 @@ using GeoParams compute_heatcapacity!(Cp, Mat_tup, PhaseRatio, args) num_alloc = @allocated compute_heatcapacity!(Cp, Mat_tup, PhaseRatio, args) @test sum(Cp[1, 1, :]) ≈ 121399.0486067196 - # @test num_alloc <= 32 + @test num_alloc == 0 # ----------------------- @@ -222,12 +222,12 @@ using GeoParams compute_conductivity!(k, Mat_tup, Phases, args) @test sum(k) ≈ 1.9216938849389635e6 # num_alloc = @allocated compute_conductivity!(k, Mat_tup, Phases, args) - # @test num_alloc <= 32 + @test num_alloc == 0 compute_conductivity!(k, Mat_tup, PhaseRatio, args) @test sum(k) ≈ 1.9216938849389635e6 # num_alloc = @allocated compute_conductivity!(k, Mat_tup, PhaseRatio, args) - # @test num_alloc <= 32 + @test num_alloc == 0 ###### @@ -371,7 +371,7 @@ using GeoParams @test Hr[50, 50, 50] ≈ 1e-6 # num_alloc = @allocated compute_radioactive_heat!(Hr, Mat_tup, Phases, args) - # @test num_alloc <= 32 # in the commandline this gives 0; while running the script not always + @test num_alloc == 0 # in the commandline this gives 0; while running the script not always compute_radioactive_heat!(Hr, Mat_tup, PhaseRatio, args) @test sum(Hr) ≈ 0.33715177646857664 diff --git a/test/test_Plasticity.jl b/test/test_Plasticity.jl index 4faf93ec0..2ea614d42 100644 --- a/test/test_Plasticity.jl +++ b/test/test_Plasticity.jl @@ -80,7 +80,7 @@ using GeoParams compute_yieldfunction!(F, MatParam, PhaseRatio, args) num_alloc = @allocated compute_yieldfunction!(F, MatParam, PhaseRatio, args) @test maximum(F[1, 1, :]) ≈ 839745.962155614 - # @test num_alloc <= 32 + @test num_alloc == 0 # Test plastic potential derivatives ## 2D From b9b5ba6f6ef13f3a974a3970f436665a43d2272e Mon Sep 17 00:00:00 2001 From: albert-de-montserrat Date: Tue, 5 Dec 2023 08:36:02 +0100 Subject: [PATCH 2/4] use AllocCheck.jl --- test/Project.toml | 1 + test/test_Density.jl | 34 +++++++++++++++++----------------- test/test_Energy.jl | 24 ++++++++++++------------ test/test_Plasticity.jl | 8 ++++---- 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/test/Project.toml b/test/Project.toml index 9c73695e2..d6e40f685 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,4 +1,5 @@ [deps] +AllocCheck = "9b6a8646-10ed-4001-bbdc-1d2f46dfbb1a" DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a" diff --git a/test/test_Density.jl b/test/test_Density.jl index 855e2b130..ac33d8b8d 100644 --- a/test/test_Density.jl +++ b/test/test_Density.jl @@ -1,4 +1,4 @@ -using Test, GeoParams, StaticArrays +using Test, GeoParams, StaticArrays, AllocCheck @testset "Density.jl" begin @@ -51,27 +51,27 @@ using Test, GeoParams, StaticArrays args = (P=P, T=T) x = ConstantDensity() - num_alloc = @allocated compute_density!(rho, x, args) - @test num_alloc == 0 - + num_alloc = check_allocs(compute_density!, typeof.((rho, x, args))) + @test isempty(num_alloc) + #Test allocations using ρ alias ρ!(rho, x, args) - num_alloc = @allocated ρ!(rho, x, args) - @test num_alloc == 0 + num_alloc = check_allocs(ρ!, typeof.((rho, x, args))) + @test isempty(num_alloc) # This does NOT allocate if I test this with @btime; # yet it does while running the test here x = PT_Density() compute_density!(rho, x, args) - num_alloc = @allocated compute_density!(rho, x, args) - @test num_alloc == 0 + num_alloc = check_allocs(compute_density!, typeof.((rho, x, args))) + @test isempty(num_alloc) # This does NOT allocate if I test this with @btime; # yet it does while running the test here x = Compressible_Density() compute_density!(rho, x, args) - num_alloc = @allocated compute_density!(rho, x, args) - @test num_alloc == 0 + num_alloc = check_allocs(compute_density!, typeof.((rho, x, args))) + @test isempty(num_alloc) # Read Phase diagram interpolation object fname = "test_data/Peridotite_dry.in" @@ -180,18 +180,18 @@ using Test, GeoParams, StaticArrays # Test computing density when Mat_tup1 is provided as a tuple compute_density!(rho, Mat_tup1, Phases, args) - num_alloc = @allocated compute_density!(rho, Mat_tup1, Phases, args) # 287.416 μs (0 allocations: 0 bytes) - @test num_alloc == 0 + num_alloc = check_allocs(compute_density!, typeof.((rho, Mat_tup1, Phases, args))) # 287.416 μs (0 allocations: 0 bytes) + @test isempty(num_alloc) @test sum(rho) / 400^2 ≈ 2945.000013499999 - @test num_alloc == 0 + @test isempty(num_alloc) #Same test using function alias rho = zeros(size(Phases)) ρ!(rho, Mat_tup1, Phases, args) - num_alloc = @allocated compute_density!(rho, Mat_tup1, Phases, args) + num_alloc = check_allocs(compute_density!, typeof.((rho, Mat_tup1, Phases, args))) @test sum(rho) / 400^2 ≈ 2945.000013499999 - @test num_alloc == 0 + @test isempty(num_alloc) # Test for single phase compute_density(MatParam, 1, (P=P[1], T=T[1])) @@ -210,9 +210,9 @@ using Test, GeoParams, StaticArrays compute_density!(rho, Mat_tup1, PhaseRatio, args) - num_alloc = @allocated compute_density!(rho, Mat_tup1, PhaseRatio, args) # 136.776 μs (0 allocations: 0 bytes) + num_alloc = check_allocs(compute_density!, typeof.((rho, Mat_tup1, PhaseRatio, args))) # 136.776 μs (0 allocations: 0 bytes) @test sum(rho) / 400^2 ≈ 2945.000013499999 - @test num_alloc == 0 # for some reason this does indicate allocations but @btime does not + @test isempty(num_alloc) # for some reason this does indicate allocations but @btime does not # Test calling the routine with only pressure as input. # This is ok for Mat_tup1, as it only has constant & P-dependent densities. diff --git a/test/test_Energy.jl b/test/test_Energy.jl index 80d9314c3..23aa596d4 100644 --- a/test/test_Energy.jl +++ b/test/test_Energy.jl @@ -1,5 +1,4 @@ -using Test -using GeoParams +using Test, GeoParams, AllocCheck @testset "EnergyParameters.jl" begin @@ -95,9 +94,10 @@ using GeoParams compute_heatcapacity!(Cp, Mat_tup1, Phases, args1) # computation routine w/out P (not used in most heat capacity formulations) @test sum(Cp[1, 1, :]) ≈ 109050.0 - num_alloc = @allocated compute_heatcapacity!(Cp, Mat_tup, Phases, args) + compute_heatcapacity!(Cp, Mat_tup, Phases, args) @test sum(Cp[1, 1, :]) ≈ 121399.0486067196 - @test num_alloc == 0 + num_alloc = check_allocs(compute_heatcapacity!, typeof.((Cp, Mat_tup, Phases, args))) + @test isempty(num_alloc) # test if we provide phase ratios PhaseRatio = zeros(n, n, n, 3) @@ -107,9 +107,9 @@ using GeoParams PhaseRatio[I] = 1.0 end compute_heatcapacity!(Cp, Mat_tup, PhaseRatio, args) - num_alloc = @allocated compute_heatcapacity!(Cp, Mat_tup, PhaseRatio, args) + num_alloc = check_allocs(compute_heatcapacity!, typeof.((Cp, Mat_tup, PhaseRatio, args))) @test sum(Cp[1, 1, :]) ≈ 121399.0486067196 - @test num_alloc == 0 + @test isempty(num_alloc) # ----------------------- @@ -221,13 +221,13 @@ using GeoParams compute_conductivity!(k, Mat_tup, Phases, args) @test sum(k) ≈ 1.9216938849389635e6 - # num_alloc = @allocated compute_conductivity!(k, Mat_tup, Phases, args) - @test num_alloc == 0 + num_alloc = check_allocs(compute_conductivity!, typeof.((k, Mat_tup, Phases, args))) + @test isempty(num_alloc) compute_conductivity!(k, Mat_tup, PhaseRatio, args) @test sum(k) ≈ 1.9216938849389635e6 - # num_alloc = @allocated compute_conductivity!(k, Mat_tup, PhaseRatio, args) - @test num_alloc == 0 + num_alloc = check_allocs(compute_conductivity!, typeof.((k, Mat_tup, PhaseRatio, args))) + @test isempty(num_alloc) ###### @@ -370,8 +370,8 @@ using GeoParams compute_radioactive_heat!(Hr, Mat_tup1, Phases, args1) @test Hr[50, 50, 50] ≈ 1e-6 - # num_alloc = @allocated compute_radioactive_heat!(Hr, Mat_tup, Phases, args) - @test num_alloc == 0 # in the commandline this gives 0; while running the script not always + num_alloc = check_allocs(compute_radioactive_heat!, typeof.((Hr, Mat_tup, Phases, args))) + @test isempty(num_alloc) # in the commandline this gives 0; while running the script not always compute_radioactive_heat!(Hr, Mat_tup, PhaseRatio, args) @test sum(Hr) ≈ 0.33715177646857664 diff --git a/test/test_Plasticity.jl b/test/test_Plasticity.jl index 2ea614d42..e6c2957c9 100644 --- a/test/test_Plasticity.jl +++ b/test/test_Plasticity.jl @@ -1,5 +1,4 @@ -using Test -using GeoParams +using Test, GeoParams, AllocCheck @testset "Plasticity.jl" begin @@ -78,9 +77,10 @@ using GeoParams PhaseRatio[I] = 1.0 end compute_yieldfunction!(F, MatParam, PhaseRatio, args) - num_alloc = @allocated compute_yieldfunction!(F, MatParam, PhaseRatio, args) + compute_yieldfunction!(F, MatParam, PhaseRatio, args) @test maximum(F[1, 1, :]) ≈ 839745.962155614 - @test num_alloc == 0 + num_alloc = check_allocs(compute_yieldfunction!, typeof.((F, MatParam, PhaseRatio, args))) + @test isempty(num_alloc) # Test plastic potential derivatives ## 2D From 693e63ee871151cbefef3c0ced6c087aa4384016 Mon Sep 17 00:00:00 2001 From: albert-de-montserrat Date: Tue, 5 Dec 2023 10:48:48 +0100 Subject: [PATCH 3/4] fix tests not working on Windows --- test/test_Density.jl | 11 +++++++---- test/test_Energy.jl | 15 +++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/test/test_Density.jl b/test/test_Density.jl index ac33d8b8d..9237852fa 100644 --- a/test/test_Density.jl +++ b/test/test_Density.jl @@ -190,8 +190,9 @@ using Test, GeoParams, StaticArrays, AllocCheck rho = zeros(size(Phases)) ρ!(rho, Mat_tup1, Phases, args) num_alloc = check_allocs(compute_density!, typeof.((rho, Mat_tup1, Phases, args))) - @test sum(rho) / 400^2 ≈ 2945.000013499999 @test isempty(num_alloc) + compute_density!(rho, Mat_tup1, Phases, args) + @test sum(rho) / 400^2 ≈ 2945.000013499999 # Test for single phase compute_density(MatParam, 1, (P=P[1], T=T[1])) @@ -209,10 +210,12 @@ using Test, GeoParams, StaticArrays, AllocCheck end compute_density!(rho, Mat_tup1, PhaseRatio, args) - - num_alloc = check_allocs(compute_density!, typeof.((rho, Mat_tup1, PhaseRatio, args))) # 136.776 μs (0 allocations: 0 bytes) @test sum(rho) / 400^2 ≈ 2945.000013499999 - @test isempty(num_alloc) # for some reason this does indicate allocations but @btime does not + + # num_alloc = check_allocs(compute_density!, typeof.((rho, Mat_tup1, PhaseRatio, args))) # 136.776 μs (0 allocations: 0 bytes) + # @test isempty(num_alloc) # for some reason this does indicate allocations but @btime does not + num_alloc = @allocated compute_density!(rho, Mat_tup1, PhaseRatio, args) + @test iszero(num_alloc) # for some reason this does indicate allocations but @btime does not # Test calling the routine with only pressure as input. # This is ok for Mat_tup1, as it only has constant & P-dependent densities. diff --git a/test/test_Energy.jl b/test/test_Energy.jl index 23aa596d4..5a7838ee5 100644 --- a/test/test_Energy.jl +++ b/test/test_Energy.jl @@ -107,9 +107,10 @@ using Test, GeoParams, AllocCheck PhaseRatio[I] = 1.0 end compute_heatcapacity!(Cp, Mat_tup, PhaseRatio, args) - num_alloc = check_allocs(compute_heatcapacity!, typeof.((Cp, Mat_tup, PhaseRatio, args))) @test sum(Cp[1, 1, :]) ≈ 121399.0486067196 - @test isempty(num_alloc) + # num_alloc = check_allocs(compute_heatcapacity!, typeof.((Cp, Mat_tup, PhaseRatio, args))) + num_alloc = @allocated compute_heatcapacity!(Cp, Mat_tup, PhaseRatio, args) + @test iszero(num_alloc) # ----------------------- @@ -221,13 +222,15 @@ using Test, GeoParams, AllocCheck compute_conductivity!(k, Mat_tup, Phases, args) @test sum(k) ≈ 1.9216938849389635e6 - num_alloc = check_allocs(compute_conductivity!, typeof.((k, Mat_tup, Phases, args))) - @test isempty(num_alloc) + # num_alloc = check_allocs(compute_conductivity!, typeof.((k, Mat_tup, Phases, args))) + num_alloc = @allocated compute_conductivity!(k, Mat_tup, Phases, args) + @test iszero(num_alloc) compute_conductivity!(k, Mat_tup, PhaseRatio, args) @test sum(k) ≈ 1.9216938849389635e6 - num_alloc = check_allocs(compute_conductivity!, typeof.((k, Mat_tup, PhaseRatio, args))) - @test isempty(num_alloc) + # num_alloc = check_allocs(compute_conductivity!, typeof.((k, Mat_tup, PhaseRatio, args))) + num_alloc = @allocated compute_conductivity!(k, Mat_tup, PhaseRatio, args) + @test iszero(num_alloc) ###### From 05a2537546a9ce72b92ac4a00fe080b1dba57cf2 Mon Sep 17 00:00:00 2001 From: albert-de-montserrat Date: Tue, 5 Dec 2023 11:02:16 +0100 Subject: [PATCH 4/4] fix broken test in Windows --- test/test_Plasticity.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_Plasticity.jl b/test/test_Plasticity.jl index e6c2957c9..2d739a3c9 100644 --- a/test/test_Plasticity.jl +++ b/test/test_Plasticity.jl @@ -77,10 +77,10 @@ using Test, GeoParams, AllocCheck PhaseRatio[I] = 1.0 end compute_yieldfunction!(F, MatParam, PhaseRatio, args) - compute_yieldfunction!(F, MatParam, PhaseRatio, args) @test maximum(F[1, 1, :]) ≈ 839745.962155614 - num_alloc = check_allocs(compute_yieldfunction!, typeof.((F, MatParam, PhaseRatio, args))) - @test isempty(num_alloc) + # num_alloc = check_allocs(compute_yieldfunction!, typeof.((F, MatParam, PhaseRatio, args))) + num_alloc = @allocated compute_yieldfunction!(F, MatParam, PhaseRatio, args) + @test iszero(num_alloc) # Test plastic potential derivatives ## 2D