Multithreading speedup for large models
In this tutorial we illustrate how to make use of multi-threading in BeforeIT.jl to allow for faster executions of single simulation runs.
import BeforeIT as Bit
using Plots, StatsPlotsFirst, we initialise the model, this time we use the Italy 2010Q1 scenario
parameters = Bit.ITALY2010Q1.parameters
initial_conditions = Bit.ITALY2010Q1.initial_conditions
model = Bit.Model(parameters, initial_conditions);The model is in scale 1:2000, so it has around 30,000 households
model.prop.H29916Note that the households number is actually the sum of active and inactive households, the owners of firms and of the bank
length(model.w_act) + length(model.w_inact) + length(model.firms) + 129916Let's fist check how many threads we have available in this Julia session
Threads.nthreads()2Then we need to first compile the code not to count compilation time, we can do that just by executing the function one time
T = 50
Bit.run!(model, T; parallel = false);Let's now compare the performance of single-threading and multi-threading
model = Bit.Model(parameters, initial_conditions);
@time Bit.run!(model, T; parallel = false);
model = Bit.Model(parameters, initial_conditions);
@time Bit.run!(model, T; parallel = true); 4.937495 seconds (265.75 k allocations: 5.342 GiB, 11.76% gc time)
2.294933 seconds (279.88 k allocations: 5.267 GiB, 1.78% gc time)Is the speedup in line to what we would expect? Yes!