JoshuaLampert / DispersiveShallowWater.jl

Structure-preserving numerical methods for dispersive shallow water models
https://joshualampert.github.io/DispersiveShallowWater.jl/
MIT License
15 stars 3 forks source link

Improve efficiency #107

Closed JoshuaLampert closed 5 months ago

JoshuaLampert commented 5 months ago

We store the whole unfactorized dense inverses of the relevant matrices. It would be more efficient to store them in a factorized way and/or use sparse matrices, see also https://github.com/JoshuaLampert/DispersiveShallowWater.jl/pull/106#discussion_r1606998619.

TODO:

JoshuaLampert commented 5 months ago

For the Svärd-Kalisch equations, it's a bit more delicate because the elliptic operators changes over time, which currently leads to massive allocations: https://github.com/JoshuaLampert/DispersiveShallowWater.jl/blob/3adee6615091d6739f35bcae9970e7d8505a7b7c/src/equations/svaerd_kalisch_1d.jl#L259-L262

The SummaryCallback for examples/svaerd_kalisch_1d/svaerd_kalisch_1d_dingemans.jl currently gives

 ──────────────────────────────────────────────────────────────────────────────
        DispersiveSWE                 Time                    Allocations      
                             ───────────────────────   ────────────────────────
      Tot / % measured:           1.91s /  99.3%           3.61GiB /  99.7%    

 Section             ncalls     time    %tot     avg     alloc    %tot      avg
 ──────────────────────────────────────────────────────────────────────────────
 rhs!                 5.16k    1.88s   99.2%   364μs   3.59GiB   99.7%   730KiB
   dv elliptic        5.16k    1.10s   58.2%   213μs   2.77GiB   76.9%   563KiB
   dv hyperbolic      5.16k    719ms   38.0%   139μs    590MiB   16.0%   117KiB
   deta hyperbolic    5.16k   53.3ms    2.8%  10.3μs    250MiB    6.8%  49.5KiB
   ~rhs!~             5.16k   4.87ms    0.3%   944ns    810KiB    0.0%     161B
   source terms       5.16k    265μs    0.0%  51.3ns     0.00B    0.0%    0.00B
 analyze solution        86   14.3ms    0.8%   167μs   10.0MiB    0.3%   119KiB
 ──────────────────────────────────────────────────────────────────────────────

Do you see a possibility to reduce the allocations @ranocha?

ranocha commented 5 months ago

I'm using the following approach in another project:

system_matrix = Symmetric(...)
if issparse(system_matrix)
    (; factorization) = parameters
    cholesky!(factorization, system_matrix)
    dv .= factorization \ tmp
else
    factorization = cholesky!(system_matrix)
    ldiv!(dv, factorization, tmp)
end

You could do something similar here: