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

refactor: Green function that can be fast with SVector or Zygote-differentiable with Vectors #3

Merged
merged 4 commits into from
Jan 20, 2025

Conversation

mancellin
Copy link
Collaborator

@mancellin mancellin commented Jan 17, 2025

Replaces #1.

If the elements are built with SVectors, the evaluation is state-of-the-art fast.
If they are Vectors, it is a bit slower, but the code is Zygote-differentiable.

Performance of some of the gradients with Zygote seems slightly worse than before. I'm not sure how to really interpret that.

WITH STATIC VECTORS

Evaluation
 Rankine
  140.250 ns (0 allocations: 0 bytes)
  162.703 ns (0 allocations: 0 bytes)
 GFWu
  68.591 ns (0 allocations: 0 bytes)
  106.808 ns (0 allocations: 0 bytes)

WITH VECTORS

Evaluation
 Rankine
  1.313 μs (33 allocations: 2.58 KiB)
  2.415 μs (70 allocations: 5.47 KiB)
 GFWu
  70.861 ns (0 allocations: 0 bytes)
  222.322 ns (4 allocations: 416 bytes)

Gradients
 Rankine
  661.467 μs (2696 allocations: 81.56 KiB)
  5.365 ms (14671 allocations: 436.78 KiB)
 GFWu
  48.893 μs (424 allocations: 19.31 KiB)
  525.291 μs (2598 allocations: 84.78 KiB)
Benchmark code

using BEM
using BenchmarkTools
using StaticArrays
using Zygote

wavenumber = 1.0

println("WITH STATIC VECTORS")
element_1 = (center=SVector(0.0, 0.0, -1.0),)
element_2 = (
    center=SVector(1.0, 1.0, -2.0),
    vertices= @SMatrix([-0.5 -0.5 0.0; 0.5 -0.5 0.0; 0.5 0.5 0.0; -0.5 0.5 0.0]) .+ SVector(1.0, 1.0, -2.0)',
    normal=SVector(0.0, 0.0, 1.0),
    radius=sqrt(2)/2,
    area=1.0,
)

println("Evaluation")
println(" Rankine")
@btime BEM.integral($(Rankine()), $element_1, $element_2)
@btime BEM.integral_gradient($(Rankine()), $element_1, $element_2)

println(" GFWu")
@btime BEM.integral($(GFWu()), $element_1, $element_2, $wavenumber)
@btime BEM.integral_gradient($(GFWu()), $element_1, $element_2, $wavenumber)

println("WITH VECTORS")
element_1 = (center=[0.0, 0.0, -1.0],)
element_2 = (
    center=[1.0, 1.0, -2.0],
    vertices= [-0.5 -0.5 0.0; 0.5 -0.5 0.0; 0.5 0.5 0.0; -0.5 0.5 0.0] .+ [1.0, 1.0, -2.0]',
    normal=[0.0, 0.0, 1.0],
    radius=sqrt(2)/2,
    area=1.0,
)

println("Evaluation")
println(" Rankine")
@btime BEM.integral($(Rankine()), $element_1, $element_2)
@btime BEM.integral_gradient($(Rankine()), $element_1, $element_2)

println(" GFWu")
@btime BEM.integral($(GFWu()), $element_1, $element_2, $wavenumber)
@btime BEM.integral_gradient($(GFWu()), $element_1, $element_2, $wavenumber)

println("Gradients")
println(" Rankine")
S(z) = BEM.integral(Rankine(), (center=[0.0, 0.0, z],), element_2)
@btime Zygote.gradient($S, $(-1.0))
D(z) = BEM.integral_gradient(Rankine(), (center=[0.0, 0.0, z],), element_2)
@btime Zygote.jacobian($D, $(-1.0))

println(" GFWu")
S(z) = real(BEM.integral(GFWu(), (center=[0.0, 0.0, z],), element_2, wavenumber))
@btime Zygote.gradient($S, $(-1.0))
D(z) = real.(BEM.integral_gradient(GFWu(), (center=[0.0, 0.0, z],), element_2, wavenumber))
@btime Zygote.jacobian($D, $(-1.0))

nothing

@KapilKhanal
Copy link
Collaborator

oopsie I merged in wrong sequence. This will have to updated to use MarineHydro.jl instead

@KapilKhanal
Copy link
Collaborator

what will be the best way to merge now? I did not realize the problem it will have not merging in order especially with the file name changed

@KapilKhanal KapilKhanal merged commit 9ea4c15 into symbiotic-engineering:main Jan 20, 2025
1 check passed
@mancellin mancellin deleted the versatile_gf branch January 27, 2025 11:22
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