-
Notifications
You must be signed in to change notification settings - Fork 119
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
Use Vectors instead of a Dict for nearby offsets #895
Conversation
Codecov Report
@@ Coverage Diff @@
## main #895 +/- ##
==========================================
+ Coverage 84.45% 92.45% +7.99%
==========================================
Files 43 32 -11
Lines 2850 2333 -517
==========================================
- Hits 2407 2157 -250
+ Misses 443 176 -267
... and 11 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
Hi, this is a complex change... You will need to summarize your PR in english words for me to review it. Deducing what the PR does by looking at the code would take too long for me! Sorry I am just in a really stressful life situation right now, really short on time!!! |
No problem, I will not bother you with this, there are more important changes going on, will try to resolve myself, and if I'm totally lost I will ask you for help |
…re *partially* resolve the problem, still it allocated more than before
but you should still start labelling the PRs with an adequate description for future searches and backtracking! |
Done in the main comment 👍 |
ok, after the simplification allocations seem to have disappeared (don't understand why but that's it :D), will do a brief macro-benchmark to see how much of an improvement it is |
Indeed, the improvement is pretty substantial, running the Game of Life simulation (which is already very fast, that's why the improvement is substantial) Benchmarkusing Agents, Random, BenchmarkTools
@agent Automaton GridAgent{2} begin end
function build_model(alive_probability, dims, metric = :chebyshev)
space = GridSpaceSingle(dims; metric, periodic=false)
status = zeros(Bool, dims)
new_status = zeros(Bool, dims)
rules = (2, 3, 3, 3)
properties = (; rules, status, new_status)
model = UnremovableABM(Automaton, space; properties, rng = Xoshiro())
for pos in Agents.positions(model)
if rand(model.rng) < alive_probability
status[pos...] = true
end
end
return model
end
function model_step(model)
new_status = model.new_status
status = model.status
@inbounds for pos in Agents.positions(model)
n = alive_neighbors(pos, model)
if status[pos...] == true && (n ≤ model.rules[4] && n ≥ model.rules[1])
new_status[pos...] = true
elseif status[pos...] == false && (n ≥ model.rules[3] && n ≤ model.rules[4])
new_status[pos...] = true
else
new_status[pos...] = false
end
end
status .= new_status
return
end
function alive_neighbors(pos, model)
c = 0
@inbounds for near_pos in nearby_positions(pos, model)
if model.status[near_pos...] == true
c += 1
end
end
return c
end
function run_model_agents()
model = build_model(0.5, (100, 100))
Agents.step!(model, dummystep, model_step, 1000)
end
@benchmark run_model_agents()
before: julia> @benchmark run_model_agents()
BenchmarkTools.Trial: 22 samples with 1 evaluation.
Range (min … max): 223.142 ms … 280.279 ms ┊ GC (min … max): 0.00% … 0.00%
Time (median): 227.121 ms ┊ GC (median): 0.00%
Time (mean ± σ): 230.362 ms ± 12.257 ms ┊ GC (mean ± σ): 0.00% ± 0.00%
▁ ▃█
▇█▄██▁▄▄▁▄▁▄▁▁▁▁▁▁▁▁▁▁▁▁▁▄▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄ ▁
223 ms Histogram: frequency by time 280 ms <
Memory estimate: 1.32 MiB, allocs estimate: 34950.
after: julia> @benchmark run_model_agents()
BenchmarkTools.Trial: 31 samples with 1 evaluation.
Range (min … max): 160.820 ms … 172.073 ms ┊ GC (min … max): 0.00% … 0.00%
Time (median): 162.463 ms ┊ GC (median): 0.00%
Time (mean ± σ): 163.448 ms ± 2.549 ms ┊ GC (mean ± σ): 0.00% ± 0.00%
▁ ▄ ▁ ▁█ ▁ ▁
█▁▆█▆█▆██▁▁▁▆▆▆▆█▆▆█▆▁▆▁▁▁▁▁▁▁▁▁▁▁▁▁▁▆▁▁▁▁▁▁▁▁▁▁▁▁▁▆▁▁▁▁▁▁▁▁▆ ▁
161 ms Histogram: frequency by time 172 ms <
Memory estimate: 1.32 MiB, allocs estimate: 34944.
it is 30% faster!! So now we are "only" 2x slower than DynamicalGrids.jl (in the single cpu case :D) |
This should make the retrieval of the offsets faster.....
The PR removes the dictionaries used to hold the offsets for calculating nearby stuff and substitute them with vectors. The access to a vector should be faster, and so this should help performance
Still needs benchmarks