trixi-framework / TrixiParticles.jl

TrixiParticles.jl: Particle-based multiphysics simulations in Julia
https://trixi-framework.github.io/TrixiParticles.jl/
MIT License
33 stars 10 forks source link

Precompute git hash to avoid overhead #542

Closed efaulhaber closed 5 months ago

efaulhaber commented 5 months ago

For simulations with few particles and frequent saving, get_git_hash had a significant overhead.

julia> @btime TrixiParticles.get_git_hash()
  8.527 ms (130 allocations: 209.62 KiB)
"421743dc-dirty"

Say we want a 10-second video with 60 frames per second. That's 5 seconds only due to get_git_hash, which always returns the same thing. For small simulations, this is a big overhead.

I now precompute the hash at the beginning of the simulation and store it in the callbacks.

Here is the timer output of the default hydrostatic_water_column_2d.jl example. main:

────────────────────────────────────────────────────────────────────────────────────────────────────
Trixi simulation finished.  Final time: 1.0  Time steps: 222 (accepted), 222 (total)
────────────────────────────────────────────────────────────────────────────────────────────────────

 ──────────────────────────────────────────────────────────────────────────────────────────
            TrixiParticles.jl                     Time                    Allocations      
                                         ───────────────────────   ────────────────────────
            Tot / % measured:                 1.37s /  98.8%            117MiB /  99.9%    

 Section                         ncalls     time    %tot     avg     alloc    %tot      avg
 ──────────────────────────────────────────────────────────────────────────────────────────
 save solution                       50    1.25s   92.0%  24.9ms    110MiB   94.1%  2.20MiB
   ~save solution~                   50    1.25s   91.9%  24.9ms    110MiB   94.0%  2.20MiB
   compute boundary pressure         50    338μs    0.0%  6.76μs   26.6KiB    0.0%     544B
   update nhs                        50    171μs    0.0%  3.42μs    134KiB    0.1%  2.69KiB
   inverse state equation            50    125μs    0.0%  2.51μs   12.5KiB    0.0%     256B
   update density diffusion          50    711ns    0.0%  14.2ns     0.00B    0.0%    0.00B
 kick!                            1.11k    107ms    7.9%  96.2μs   6.69MiB    5.7%  6.16KiB
   system interaction             1.11k   80.9ms    6.0%  72.7μs   2.51MiB    2.1%  2.31KiB
     fluid1-fluid1                1.11k   68.8ms    5.1%  61.8μs    539KiB    0.4%     496B
     fluid1-boundary2             1.11k   9.77ms    0.7%  8.78μs    539KiB    0.4%     496B
     ~system interaction~         1.11k   2.26ms    0.2%  2.03μs   1.45MiB    1.2%  1.34KiB
     boundary2-boundary2          1.11k   15.2μs    0.0%  13.7ns     0.00B    0.0%    0.00B
     boundary2-fluid1             1.11k   13.1μs    0.0%  11.8ns     0.00B    0.0%    0.00B
   update systems and nhs         1.11k   21.6ms    1.6%  19.4μs   3.96MiB    3.4%  3.64KiB
     compute boundary pressure    1.11k   8.48ms    0.6%  7.62μs    591KiB    0.5%     544B
     update nhs                   1.11k   6.81ms    0.5%  6.12μs   2.92MiB    2.5%  2.69KiB
     ~update systems and nhs~     1.11k   3.32ms    0.2%  2.99μs    194KiB    0.2%     179B
     inverse state equation       1.11k   2.99ms    0.2%  2.69μs    278KiB    0.2%     256B
     update density diffusion     1.11k   13.1μs    0.0%  11.8ns     0.00B    0.0%    0.00B
   reset ∂v/∂t                    1.11k   3.07ms    0.2%  2.76μs     0.00B    0.0%    0.00B
   source terms                   1.11k   1.16ms    0.1%  1.04μs    226KiB    0.2%     208B
   ~kick!~                        1.11k    356μs    0.0%   320ns   2.94KiB    0.0%    2.70B
 drift!                           1.11k   1.51ms    0.1%  1.36μs    210KiB    0.2%     193B
   velocity                       1.11k   1.01ms    0.1%   905ns    209KiB    0.2%     192B
   reset ∂u/∂t                    1.11k    331μs    0.0%   297ns     0.00B    0.0%    0.00B
   ~drift!~                       1.11k    176μs    0.0%   158ns   1.47KiB    0.0%    1.35B
 update nhs                           1    124μs    0.0%   124μs   2.69KiB    0.0%  2.69KiB
 compute boundary pressure            1   25.7μs    0.0%  25.7μs      544B    0.0%     544B
 inverse state equation               1   6.62μs    0.0%  6.62μs      256B    0.0%     256B
 update density diffusion             1   0.00ns    0.0%  0.00ns     0.00B    0.0%    0.00B
 ──────────────────────────────────────────────────────────────────────────────────────────

This PR:

────────────────────────────────────────────────────────────────────────────────────────────────────
Trixi simulation finished.  Final time: 1.0  Time steps: 222 (accepted), 222 (total)
────────────────────────────────────────────────────────────────────────────────────────────────────

 ──────────────────────────────────────────────────────────────────────────────────────────
            TrixiParticles.jl                     Time                    Allocations      
                                         ───────────────────────   ────────────────────────
            Tot / % measured:                 383ms /  86.1%           97.0MiB /  99.7%    

 Section                         ncalls     time    %tot     avg     alloc    %tot      avg
 ──────────────────────────────────────────────────────────────────────────────────────────
 kick!                            1.11k    177ms   53.6%   159μs   6.69MiB    6.9%  6.16KiB
   system interaction             1.11k    113ms   34.4%   102μs   2.51MiB    2.6%  2.31KiB
     fluid1-fluid1                1.11k   96.3ms   29.2%  86.5μs    539KiB    0.5%     496B
     fluid1-boundary2             1.11k   15.0ms    4.6%  13.5μs    539KiB    0.5%     496B
     ~system interaction~         1.11k   1.92ms    0.6%  1.72μs   1.45MiB    1.5%  1.34KiB
     boundary2-fluid1             1.11k   13.3μs    0.0%  12.0ns     0.00B    0.0%    0.00B
     boundary2-boundary2          1.11k   12.8μs    0.0%  11.5ns     0.00B    0.0%    0.00B
   update systems and nhs         1.11k   58.1ms   17.6%  52.2μs   3.96MiB    4.1%  3.64KiB
     update nhs                   1.11k   37.7ms   11.4%  33.9μs   2.92MiB    3.0%  2.69KiB
     compute boundary pressure    1.11k   12.8ms    3.9%  11.5μs    591KiB    0.6%     544B
     inverse state equation       1.11k   4.00ms    1.2%  3.60μs    278KiB    0.3%     256B
     ~update systems and nhs~     1.11k   3.62ms    1.1%  3.25μs    194KiB    0.2%     179B
     update density diffusion     1.11k   13.3μs    0.0%  12.0ns     0.00B    0.0%    0.00B
   reset ∂v/∂t                    1.11k   2.87ms    0.9%  2.58μs     0.00B    0.0%    0.00B
   source terms                   1.11k   1.99ms    0.6%  1.79μs    226KiB    0.2%     208B
   ~kick!~                        1.11k    371μs    0.1%   333ns   2.94KiB    0.0%    2.70B
 save solution                       50    151ms   45.8%  3.02ms   89.8MiB   92.9%  1.80MiB
   write to vtk                     100    132ms   40.2%  1.32ms   86.4MiB   89.4%   885KiB
   ~save solution~                   50   17.6ms    5.3%   351μs   3.18MiB    3.3%  65.1KiB
   update systems                    50   1.02ms    0.3%  20.4μs    185KiB    0.2%  3.70KiB
     compute boundary pressure       50    491μs    0.1%  9.82μs   26.6KiB    0.0%     544B
     update nhs                      50    227μs    0.1%  4.54μs    134KiB    0.1%  2.69KiB
     inverse state equation          50    170μs    0.1%  3.40μs   12.5KiB    0.0%     256B
     ~update systems~                50    130μs    0.0%  2.59μs   11.5KiB    0.0%     236B
     update density diffusion        50   1.42μs    0.0%  28.3ns     0.00B    0.0%    0.00B
 drift!                           1.11k   1.98ms    0.6%  1.77μs    210KiB    0.2%     193B
   velocity                       1.11k   1.33ms    0.4%  1.20μs    209KiB    0.2%     192B
   reset ∂u/∂t                    1.11k    474μs    0.1%   426ns     0.00B    0.0%    0.00B
   ~drift!~                       1.11k    170μs    0.1%   152ns   1.47KiB    0.0%    1.35B
 update nhs                           1   55.9μs    0.0%  55.9μs   2.69KiB    0.0%  2.69KiB
 compute boundary pressure            1   19.2μs    0.0%  19.2μs      544B    0.0%     544B
 inverse state equation               1   4.54μs    0.0%  4.54μs      256B    0.0%     256B
 update density diffusion             1   41.0ns    0.0%  41.0ns     0.00B    0.0%    0.00B
 ──────────────────────────────────────────────────────────────────────────────────────────
efaulhaber commented 5 months ago

Note that I didn't make this a const git_hash = compute_git_hash() like the timer(), so that it also works correctly when you switch branches in between simulations without restarting the REPL.

codecov[bot] commented 5 months ago

Codecov Report

Attention: Patch coverage is 0% with 17 lines in your changes missing coverage. Please review.

Project coverage is 68.73%. Comparing base (ad90288) to head (6bf74c6).

Files Patch % Lines
src/visualization/write2vtk.jl 0.00% 7 Missing :warning:
src/callbacks/post_process.jl 0.00% 4 Missing :warning:
src/callbacks/solution_saving.jl 0.00% 3 Missing :warning:
src/general/general.jl 0.00% 3 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #542 +/- ## ========================================== + Coverage 68.62% 68.73% +0.11% ========================================== Files 70 69 -1 Lines 4060 4053 -7 ========================================== Hits 2786 2786 + Misses 1274 1267 -7 ``` | [Flag](https://app.codecov.io/gh/trixi-framework/TrixiParticles.jl/pull/542/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=trixi-framework) | Coverage Δ | | |---|---|---| | [unit](https://app.codecov.io/gh/trixi-framework/TrixiParticles.jl/pull/542/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=trixi-framework) | `68.73% <0.00%> (+0.11%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=trixi-framework#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.