From 9a5756212a9dd94e95192326aa53329d8a5df96d Mon Sep 17 00:00:00 2001 From: Charles Kawczynski Date: Fri, 14 Jun 2024 13:34:45 -0400 Subject: [PATCH 1/2] Add test and improve inference of monotonic_check --- .buildkite/pipeline.yml | 4 ++++ src/Meshes/interval.jl | 32 +++++++++++++++++++++++--------- test/Meshes/interval.jl | 24 ++++++++++++++++++++++++ test/Meshes/opt_meshes.jl | 17 +++++++++++++++++ 4 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 test/Meshes/opt_meshes.jl diff --git a/.buildkite/pipeline.yml b/.buildkite/pipeline.yml index 9bb0d0280f..2301c9b3ae 100755 --- a/.buildkite/pipeline.yml +++ b/.buildkite/pipeline.yml @@ -143,6 +143,10 @@ steps: key: unit_meshes_rectangle command: "julia --color=yes --check-bounds=yes --project=.buildkite test/Meshes/rectangle.jl" + - label: "Unit: meshes opt" + key: unit_meshes_opt + command: "julia --color=yes --check-bounds=yes --project=.buildkite test/Meshes/opt_meshes.jl" + - label: "Unit: meshes cubedsphere" key: unit_meshes_cubed_sphere command: "julia --color=yes --check-bounds=yes --project=.buildkite test/Meshes/cubedsphere.jl" diff --git a/src/Meshes/interval.jl b/src/Meshes/interval.jl index 0486294289..d0d799b33c 100644 --- a/src/Meshes/interval.jl +++ b/src/Meshes/interval.jl @@ -89,15 +89,29 @@ function boundary_face_name(mesh::IntervalMesh, elem::Integer, face) return nothing end -function monotonic_check(faces) - if !(hasproperty(first(faces), :z) || eltype(faces) <: Real) - return nothing - end - z(face::AbstractFloat) = face - z(face::Geometry.AbstractPoint) = face.z +monotonic_check( + faces::Union{ + LinRange{<:Geometry.AbstractPoint}, + Vector{<:Geometry.AbstractPoint}, + }, +) = :no_check + +function monotonic_check( + faces::Union{ + LinRange{<:Geometry.ZPoint}, + LinRange{<:Real}, + Vector{<:Geometry.ZPoint}, + Vector{<:Real}, + }, +) n = length(faces) - 1 - monotonic_incr = all(map(i -> z(faces[i]) < z(faces[i + 1]), 1:n)) - monotonic_decr = all(map(i -> z(faces[i]) > z(faces[i + 1]), 1:n)) + if eltype(faces) <: Geometry.AbstractPoint + monotonic_incr = all(i -> faces[i].z < faces[i + 1].z, 1:n) + monotonic_decr = all(i -> faces[i].z > faces[i + 1].z, 1:n) + else + monotonic_incr = all(i -> faces[i] < faces[i + 1], 1:n) + monotonic_decr = all(i -> faces[i] > faces[i + 1], 1:n) + end if !(monotonic_incr || monotonic_decr) error( string( @@ -106,7 +120,7 @@ function monotonic_check(faces) ), ) end - return nothing + return :pass end abstract type StretchingRule end diff --git a/test/Meshes/interval.jl b/test/Meshes/interval.jl index f5afef8af5..1714727c94 100644 --- a/test/Meshes/interval.jl +++ b/test/Meshes/interval.jl @@ -314,3 +314,27 @@ end @test Meshes.coordinates(trunc_mesh, 1, length(trunc_mesh.faces)) == Geometry.ZPoint(z_top) end + +@testset "monotonic_check - dispatch" begin + faces = range(Geometry.XPoint(0), Geometry.XPoint(10); length = 11) + @test Meshes.monotonic_check(faces) == :no_check + @test Meshes.monotonic_check(collect(faces)) == :no_check +end + +@testset "monotonic_check" begin + faces = range(Geometry.ZPoint(0), Geometry.ZPoint(10); length = 11) + @test Meshes.monotonic_check(faces) == :pass # monotonic increasing + @test Meshes.monotonic_check(collect(faces)) == :pass # monotonic increasing + @test Meshes.monotonic_check(map(x -> x.z, faces)) == :pass # monotonic increasing + + faces = range(Geometry.ZPoint(0), Geometry.ZPoint(-10); length = 11) + @test Meshes.monotonic_check(faces) == :pass # monotonic decreasing + @test Meshes.monotonic_check(collect(faces)) == :pass # monotonic decreasing + @test Meshes.monotonic_check(map(x -> x.z, faces)) == :pass # monotonic decreasing + + faces = map(z -> Geometry.ZPoint(1), 1:10) + @test_throws ErrorException Meshes.monotonic_check(faces) # non-monotonic + + faces = range(Geometry.ZPoint(0), Geometry.ZPoint(10); length = 11) + cfaces = collect(faces) +end diff --git a/test/Meshes/opt_meshes.jl b/test/Meshes/opt_meshes.jl new file mode 100644 index 0000000000..711af1c7a3 --- /dev/null +++ b/test/Meshes/opt_meshes.jl @@ -0,0 +1,17 @@ +#= +julia --project +using Revise; include(joinpath("test", "Meshes", "opt_meshes.jl")) +=# +using Test +using SparseArrays +using JET + +import ClimaCore: ClimaCore, Domains, Meshes, Geometry + +@testset "monotonic_check" begin + faces = range(Geometry.ZPoint(0), Geometry.ZPoint(10); length = 11) + cfaces = collect(faces) + @test_opt Meshes.monotonic_check(faces) + @test_opt Meshes.monotonic_check(cfaces) + @test 0 == @allocated Meshes.monotonic_check(cfaces) +end From 617746ebe55ce08cf072d68dae250b5d805fc198 Mon Sep 17 00:00:00 2001 From: Charles Kawczynski Date: Fri, 14 Jun 2024 15:47:48 -0400 Subject: [PATCH 2/2] Update inference limits --- test/Spaces/opt_spaces.jl | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/test/Spaces/opt_spaces.jl b/test/Spaces/opt_spaces.jl index 2daf05d17b..b8b2dcbb8b 100644 --- a/test/Spaces/opt_spaces.jl +++ b/test/Spaces/opt_spaces.jl @@ -33,22 +33,22 @@ end #! format: off if ClimaComms.device(context) isa ClimaComms.CUDADevice test_n_failures(86, TU.PointSpace, context) - test_n_failures(142, TU.SpectralElementSpace1D, context) - test_n_failures(1082, TU.SpectralElementSpace2D, context) - test_n_failures(26, TU.ColumnCenterFiniteDifferenceSpace, context) - test_n_failures(27, TU.ColumnFaceFiniteDifferenceSpace, context) - test_n_failures(1082, TU.SphereSpectralElementSpace, context) - test_n_failures(1097, TU.CenterExtrudedFiniteDifferenceSpace, context) - test_n_failures(1097, TU.FaceExtrudedFiniteDifferenceSpace, context) + test_n_failures(144, TU.SpectralElementSpace1D, context) + test_n_failures(1103, TU.SpectralElementSpace2D, context) + test_n_failures(4, TU.ColumnCenterFiniteDifferenceSpace, context) + test_n_failures(5, TU.ColumnFaceFiniteDifferenceSpace, context) + test_n_failures(1104, TU.SphereSpectralElementSpace, context) + test_n_failures(1109, TU.CenterExtrudedFiniteDifferenceSpace, context) + test_n_failures(1109, TU.FaceExtrudedFiniteDifferenceSpace, context) else test_n_failures(0, TU.PointSpace, context) - test_n_failures(138, TU.SpectralElementSpace1D, context) - test_n_failures(280, TU.SpectralElementSpace2D, context) - test_n_failures(26, TU.ColumnCenterFiniteDifferenceSpace, context) - test_n_failures(27, TU.ColumnFaceFiniteDifferenceSpace, context) + test_n_failures(137, TU.SpectralElementSpace1D, context) + test_n_failures(279, TU.SpectralElementSpace2D, context) + test_n_failures(4, TU.ColumnCenterFiniteDifferenceSpace, context) + test_n_failures(5, TU.ColumnFaceFiniteDifferenceSpace, context) test_n_failures(280, TU.SphereSpectralElementSpace, context) - test_n_failures(305, TU.CenterExtrudedFiniteDifferenceSpace, context) - test_n_failures(305, TU.FaceExtrudedFiniteDifferenceSpace, context) + test_n_failures(285, TU.CenterExtrudedFiniteDifferenceSpace, context) + test_n_failures(285, TU.FaceExtrudedFiniteDifferenceSpace, context) end #! format: on end