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

Some performance improvements in bivariate interpolation #2

Merged
merged 1 commit into from
Sep 6, 2024

Conversation

DanielVandH
Copy link
Contributor

I changed the small vectors to just be Tuples, removing all allocations. The way I did it also has the benefit that it'll be type stable for non-Float64. I also made some better use of the DelaunayTriangulation.jl API.

I don't think there are any 2D tests or I missed them, but I tested some random values and they are the same between master and this branch.

Master:

julia> Random.seed!(123)
TaskLocalRNG()

julia> x, y, z, dzdx, dzdy = rand(100), rand(100), rand(100), rand(100), rand(100);

julia> spl2d = BivariateCHSInterpolation(x, y, z, dzdx, dzdy);

julia>  spl2d(x[5], y[5])
0.5320809801118653

julia> interp(spl2d, x[6], y[6])
0.042869724439189925

julia>  grad(spl2d, x[10], y[10])
(0.9563604567029627, 0.6881752317560613)

julia> grad(spl2d, 0.49211, 0.9492)
(-10.075789358336532, 17.877296736109372)

julia> spl2d(0.123, 0.99992)
NaN

julia> spl2d(0.123, 0.30491)
0.7794865485348513

This PR:

julia> Random.seed!(123)
TaskLocalRNG()

julia> x, y, z, dzdx, dzdy = rand(100), rand(100), rand(100), rand(100), rand(100);

julia> spl2d = BivariateCHSInterpolation(x, y, z, dzdx, dzdy);

julia> spl2d(x[5], y[5])
0.5320809801118653

julia> interp(spl2d, x[6], y[6])
0.042869724439189925

julia> grad(spl2d, x[10], y[10])
(0.9563604567029627, 0.6881752317560613)

julia> grad(spl2d, 0.49211, 0.9492)
(-10.075789358336532, 17.877296736109372)

julia> spl2d(0.123, 0.99992)
NaN

julia> spl2d(0.123, 0.30491)
0.7794865485348513

Some benchmarks to show that this is an actual improvement:

Master:

julia> using BenchmarkTools

julia> @benchmark $spl2d(x, y) setup=(x=rand(); y = rand())
BenchmarkTools.Trial: 10000 samples with 10 evaluations.
 Range (min … max):  1.320 μs … 29.960 μs  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     2.500 μs              ┊ GC (median):    0.00%
 Time  (mean ± σ):   2.790 μs ±  1.108 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

   ▂▁       ▄▇█▇▆▅▄▅▅▄▄▃▃▃▃▂▂▁▁▁ ▁▁                          ▂
  ▆██▇▅▇▅▅▅▅█████████████████████████▇▇▇▇▇▇▆▆▅▆▄▅▃▄▄▄▄▅▄▄▂▄▂ █
  1.32 μs      Histogram: log(frequency) by time     6.41 μs <

 Memory estimate: 80 bytes, allocs estimate: 1.

julia> @benchmark $spl2d.($x, $y)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):  123.100 μs …  1.487 ms  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     263.650 μs              ┊ GC (median):    0.00%
 Time  (mean ± σ):   274.048 μs ± 86.035 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

                   ██▁
  ▂▃▃▅▃▂▂▂▂▂▁▁▁▃▃▂▃███▆▅▄▃▃▃▂▂▂▂▂▂▂▂▂▂▂▁▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▂
  123 μs          Histogram: frequency by time          574 μs <

 Memory estimate: 32.12 KiB, allocs estimate: 401.

julia> @benchmark $grad.($(spl2d,), $x, $y)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):  130.100 μs … 200.146 ms  ┊ GC (min … max): 0.00% … 99.78%
 Time  (median):     293.800 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   343.414 μs ±   2.015 ms  ┊ GC (mean ± σ):  7.34% ±  2.55%

              █    ▂
  ▁▃▃▂▂▂▂▁▂▂▁▆█▇▆▄▅█▇▆▅▅▄▄▄▄▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▂
  130 μs           Histogram: frequency by time          688 μs <

 Memory estimate: 56.45 KiB, allocs estimate: 701.

This PR:

julia> using BenchmarkTools

julia> @benchmark $spl2d(x, y) setup=(x=rand(); y = rand())
BenchmarkTools.Trial: 10000 samples with 10 evaluations.
 Range (min … max):  1.100 μs … 69.380 μs  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     1.370 μs              ┊ GC (median):    0.00%
 Time  (mean ± σ):   1.485 μs ±  1.015 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

      ▆█▆▁
  ▁▃▅▆████▅▄▃▃▃▃▄▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▂
  1.1 μs         Histogram: frequency by time        3.54 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark $spl2d.($x, $y)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):  110.200 μs …  1.155 ms  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     128.100 μs              ┊ GC (median):    0.00%
 Time  (mean ± σ):   137.327 μs ± 38.222 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▃▆▄▄██▆▄▄▄▃▃▃▂▂▂▁▁                                           ▂
  █████████████████████▇█▇█▅▇▅▇▆▆▆▆▆▆▄▆▆▆▆▄▆▆▄▆▄▄▅▄▅▅▅▅▄▄▆▄▅▂▅ █
  110 μs        Histogram: log(frequency) by time       319 μs <

 Memory estimate: 896 bytes, allocs estimate: 1.

julia> @benchmark $grad.($(spl2d,), $x, $y)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):  112.900 μs …  1.621 ms  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     130.500 μs              ┊ GC (median):    0.00%
 Time  (mean ± σ):   144.823 μs ± 51.121 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

      █
  ▄▇▃██▆▃▃▂▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▂
  113 μs          Histogram: frequency by time          341 μs <

 Memory estimate: 1.77 KiB, allocs estimate: 1.

@liuyxpp
Copy link
Owner

liuyxpp commented Sep 6, 2024

Wow, huge thanks! I like your revisions, especially those DT methods which I am not aware of. I will also try to add some tests for 2D. Thanks again!

@liuyxpp liuyxpp merged commit 351a087 into liuyxpp:master Sep 6, 2024
@DanielVandH DanielVandH deleted the dt branch September 6, 2024 13:37
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.

2 participants