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

Improve speed of simulations #18

Merged
merged 6 commits into from
Nov 1, 2024
Merged

Improve speed of simulations #18

merged 6 commits into from
Nov 1, 2024

Conversation

Tortar
Copy link
Collaborator

@Tortar Tortar commented Oct 26, 2024

This gives a 20% speed-up in the single-threaded model at https://github.com/bancaditalia/BeforeIT.jl/blob/main/examples/benchmark_w_matlab.jl#L24, I expected the multi-threaded version to benefit more because we reduced also some allocations, but didn't test it

@Tortar
Copy link
Collaborator Author

Tortar commented Oct 26, 2024

Sorry I got fooled by some previous benchmarks, there is still some improvements but it is not 2x as I previously wrote, I edited my main post

@AldoGl
Copy link
Collaborator

AldoGl commented Oct 28, 2024

Thanks @Tortar, I can see the trick is the "@view" macro.

Can I ask you what speedup do you get? On my machine I could measure around 6-10% for the standard Austrian parameterization (not bad!). Possibly it's even a bit higher for larger models

@Tortar
Copy link
Collaborator Author

Tortar commented Oct 28, 2024

same code as in #20

julia> @benchmark run(parameters, initial_conditions, T) # this PR
BenchmarkTools.Trial: 132 samples with 1 evaluation.
 Range (min  max):  36.550 ms   41.297 ms  ┊ GC (min  max): 8.72%  14.80%
 Time  (median):     37.836 ms               ┊ GC (median):    8.46%
 Time  (mean ± σ):   37.910 ms ± 815.271 μs  ┊ GC (mean ± σ):  8.76% ±  1.34%

      ▁    ▁▄▂ ▅▅▄▅▅▂█▂▁▄                                       
  ▅▁▃▁█▃█▆▆███▆██████████▅▅▆▅▃▅▁▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃▁▁▁▁▁▁▁▃▃▃▁▁▃ ▃
  36.6 ms         Histogram: frequency by time         41.2 ms <

 Memory estimate: 110.18 MiB, allocs estimate: 670279.

julia> @benchmark run(parameters, initial_conditions, T) # master
BenchmarkTools.Trial: 118 samples with 1 evaluation.
 Range (min  max):  40.662 ms  52.037 ms  ┊ GC (min  max): 8.02%  6.88%
 Time  (median):     42.019 ms              ┊ GC (median):    7.65%
 Time  (mean ± σ):   42.583 ms ±  1.657 ms  ┊ GC (mean ± σ):  8.61% ± 2.31%

           █▂█ ▄ ▃ ▄                                           
  ▅▅▁▃▆▃█▆██████▆█▅█▆█▅▁▃▁▁▃▃▁▁▁▁▁▁▁▁▁▁▅▁▃▅▁▁▆▆▃▃▁▁▅▃▃▃▃▁▃▁▁▃ ▃
  40.7 ms         Histogram: frequency by time        46.6 ms <

 Memory estimate: 139.26 MiB, allocs estimate: 679000.

yes, nearly 10% :-)

@AldoGl
Copy link
Collaborator

AldoGl commented Nov 1, 2024

Thanks @Tortar, squashing and merging now

@AldoGl AldoGl merged commit eb8d22e into bancaditalia:main Nov 1, 2024
2 checks passed
@finmod
Copy link

finmod commented Nov 4, 2024

@Tortar Can you see an advantage to bringing agents, agents_actions and markets of BeforeIT and ABCredit within JuliaDynamics/Agents.jl? This would provide better integration with the JuliaDynamics.jl-SciML,jl-ReinforcementLearning.jl ecosystems. Perhaps a ABM example in AgentsModelZoo...

See also this question on Discourse: https://discourse.julialang.org/t/specific-dom/121140

@Tortar
Copy link
Collaborator Author

Tortar commented Nov 4, 2024

Hi @finmod, I think that we should do the other way around if possible, try to base both of them on the Agents.jl framework. But I don't see this as a big priority and there are some challenges associated with this because of the origin and the current structure of BeforeIT.jl. I think if possible we should do that but this requires a lot of refactoring, and the current structure is not really that well supported from Agents.jl which uses an approach of arrays of structs while BeforeIT.jl uses struct of arrays (which could be more performant actually). Maybe we should allow the struct of arrays approach in Agents.jl. But this is a project which requires some efforts.

Personally, being one of the author of Agents.jl I would love to see this great models as part of the ecosystem, but for now it remains actually hard to accomplish. The advantages I see are that we could reuse some of the tools in Agents.jl and maybe more standardization in code style.

First step is understanding if we could support struct of arrays in Agents.jl, this adds complexity though on the framework side

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.

3 participants