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

Optimize sample! #943

Merged
merged 19 commits into from
Dec 22, 2023
Merged

Optimize sample! #943

merged 19 commits into from
Dec 22, 2023

Conversation

Tortar
Copy link
Member

@Tortar Tortar commented Dec 11, 2023

This is possible, and even much faster than when using a Dict (wrong)

@Tortar Tortar marked this pull request as draft December 11, 2023 22:45
@Tortar
Copy link
Member Author

Tortar commented Dec 12, 2023

Actually I don't really like that the same id will be given to different agents when using sample! with a vector

@Tortar Tortar changed the title implement sample! when a vector container is used Optimize sample! Dec 14, 2023
@Tortar
Copy link
Member Author

Tortar commented Dec 14, 2023

As you can see, the speed-up is very good:

# this pr:

@btime sample!(model, 10^6) setup=(model = fake_model(Xoshiro(42), 10^7)) evals=1 samples=10 seconds=10
 276.573 ms (1000010 allocations: 114.44 MiB)

# before:

@btime sample!(model, 10^6) setup=(model = fake_model(Xoshiro(42), 10^7)) evals=1 samples=10 seconds=10
  1.947 s (48424 allocations: 168.59 MiB)

(I changed the scope of this pr to be just an optimization of the current function)

@codecov-commenter
Copy link

codecov-commenter commented Dec 14, 2023

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (3526b5c) 92.27% compared to head (30895f9) 92.18%.
Report is 1 commits behind head on main.

Files Patch % Lines
src/core/model_free_extensions.jl 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #943      +/-   ##
==========================================
- Coverage   92.27%   92.18%   -0.09%     
==========================================
  Files          33       33              
  Lines        2277     2330      +53     
==========================================
+ Hits         2101     2148      +47     
- Misses        176      182       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Tortar Tortar marked this pull request as ready for review December 14, 2023 21:51
@Tortar Tortar requested a review from Datseris December 14, 2023 22:40
@Tortar
Copy link
Member Author

Tortar commented Dec 15, 2023

Actually I used a model with no space to benchmark, the new method falls behind when there is a space instead. Need to change the code to acknowledge this

@Tortar
Copy link
Member Author

Tortar commented Dec 21, 2023

should be okay now, this is an example benchmark:

using Agents, BenchmarkTools, Random

# no space model

model_step!(model) = nothing

function fake_model(rng, nagents)
    model = StandardABM(NoSpaceAgent; model_step!, rng)
    for i in 1:nagents
        add_agent!(model)
    end
    model
end

@btime sample!(model, 10^5) setup=(model = fake_model(Xoshiro(42), 10^6)) evals=1

# space model

function fake_model(rng, nagents)
    model = StandardABM(GridAgent{2}, GridSpace((100, 100)); model_step!, rng)
    for i in 1:nagents
        add_agent!(model)
    end
    model
end

@btime sample!(model, 10^5) setup=(model = fake_model(Xoshiro(42), 10^6)) evals=1

where we have that

before pr, no space: 87.246 ms (4769 allocations: 16.87 MiB)
after pr, no space: 17.157 ms (100010 allocations: 11.45 MiB)

before pr, space: 162.883 ms (4789 allocations: 16.95 MiB) 
after pr, space: 22.714 ms (100010 allocations: 12.98 MiB)

In general, the perf improvement can vary, but it should be always an improvement except for edge cases like 10 agent in a 1 million positions space.

Copy link
Member

@Datseris Datseris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooooookay ooookay. i cry a bit from adding all these new methods but the speedup is significant!!!

@Datseris Datseris merged commit 433faa4 into main Dec 22, 2023
4 of 5 checks passed
@Datseris Datseris deleted the Tortar-patch-1 branch December 22, 2023 09:49
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