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

Use threads local sums of C_h and I_h to reduce memory consumption #33

Merged
merged 26 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Copyright 2024- Banca d'Italia and the authors.

- [Aldo Glielmo](https://github.com/AldoGl) <[[email protected]](mailto:[email protected])>
- [Mitja Devetak](https://github.com/Devetak) <[[email protected]](mailto:[email protected])>
- [Adriano Meligrana](https://github.com/Tortar) <[[email protected]](mailto:[email protected])>
Tortar marked this conversation as resolved.
Show resolved Hide resolved

## Other collaborators and contributors

Expand All @@ -67,7 +68,6 @@ Copyright 2024- Banca d'Italia and the authors.
- [Andrea Gentili](https://www.bankit.art/people/andrea-gentili) for suggesting the name of the pakege
- [Arnau Quera-Bofarull](https://github.com/arnauqb) for help in the deployment of the documentation
- [Steven Hoekstra](https://github.com/SGHoekstra) for fixing a bug in a script
- [Adriano Meligrana](https://github.com/Tortar) for fixing a bug in a script

## Disclaimer

Expand Down
50 changes: 26 additions & 24 deletions src/markets/search_and_matching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ function search_and_matching!(model, multi_threading = false)
c_G_g,
Q_d_i_g,
Q_d_m_g,
C_h_g,
I_h_g,
C_h_t,
I_h_t,
C_j_g,
C_l_g,
P_bar_h_g,
P_bar_CF_h_g,
P_j_g,
P_l_g = initialize_variables_retail_market(firms, rotw, prop, agg, w_act, w_inact, gov, bank)
P_l_g = initialize_variables_retail_market(firms, rotw, prop, agg, w_act, w_inact, gov, bank, multi_threading)

G = size(prop.products.b_HH_g, 1) # number of goods

# Loop over all goods (internal and foreign)

function loopBody(g)
function loopBody(i, g)
F_g = findall(G_f .== g)
S_fg = copy(S_f)
S_fg_ = copy(S_f_)
Expand All @@ -72,6 +72,7 @@ function search_and_matching!(model, multi_threading = false)
)

perform_retail_market!(
i,
g,
agg,
gov,
Expand All @@ -88,8 +89,8 @@ function search_and_matching!(model, multi_threading = false)
c_G_g,
Q_d_i_g,
Q_d_m_g,
C_h_g,
I_h_g,
C_h_t,
I_h_t,
C_j_g,
C_l_g,
P_bar_h_g,
Expand All @@ -107,12 +108,12 @@ function search_and_matching!(model, multi_threading = false)


if multi_threading
Threads.@threads for g in 1:G
loopBody(g)
Threads.@threads :static for g in 1:G
loopBody(Threads.threadid(), g)
end
else
for g in 1:G
loopBody(g)
loopBody(1, g)
end
end

Expand All @@ -128,8 +129,8 @@ function search_and_matching!(model, multi_threading = false)
I_i_g,
P_bar_i_g,
DM_i_g,
C_h_g,
I_h_g,
C_h_t,
I_h_t,
Q_d_i_g,
Q_d_m_g,
C_j_g,
Expand All @@ -154,8 +155,8 @@ function update_aggregate_variables!(
I_i_g,
P_bar_i_g,
DM_i_g,
C_h_g,
I_h_g,
C_h_t,
I_h_t,
Q_d_i_g,
Q_d_m_g,
C_j_g,
Expand All @@ -179,8 +180,8 @@ function update_aggregate_variables!(
Q_d_i = vec(sum(Q_d_i_g, dims = 2))
Q_d_m = vec(sum(Q_d_m_g, dims = 2))

C_h = sum(C_h_g, dims = 2)
I_h = sum(I_h_g, dims = 2)
C_h = sum(C_h_t, dims = 2)
I_h = sum(I_h_t, dims = 2)

gov.C_j = sum(C_j_g)
rotw.C_l = sum(C_l_g)
Expand Down Expand Up @@ -229,7 +230,7 @@ function update_aggregate_variables!(

end

function initialize_variables_retail_market(firms, rotw, prop, agg, w_act, w_inact, gov, bank)
function initialize_variables_retail_market(firms, rotw, prop, agg, w_act, w_inact, gov, bank, multi_threading)
# ... Initialize all the variables ...

# change some variables according to arguments of matlab function
Expand All @@ -256,8 +257,8 @@ function initialize_variables_retail_market(firms, rotw, prop, agg, w_act, w_ina
Q_d_i_g = zeros(size(firms.Y_i)..., G)
Q_d_m_g = zeros(size(rotw.Y_m)..., G)

C_h_g = zeros(H, G)
I_h_g = zeros(H, G)
C_h_t = zeros(H, multi_threading ? Threads.nthreads() : 1)
I_h_t = zeros(H, multi_threading ? Threads.nthreads() : 1)
Tortar marked this conversation as resolved.
Show resolved Hide resolved

C_j_g = zeros(1, G)
C_l_g = zeros(1, G)
Expand All @@ -280,8 +281,8 @@ function initialize_variables_retail_market(firms, rotw, prop, agg, w_act, w_ina
c_G_g,
Q_d_i_g,
Q_d_m_g,
C_h_g,
I_h_g,
C_h_t,
I_h_t,
C_j_g,
C_l_g,
P_bar_h_g,
Expand Down Expand Up @@ -427,6 +428,7 @@ end
Perform the retail market exchange process
"""
function perform_retail_market!(
i,
g,
agg,
gov,
Expand All @@ -443,8 +445,8 @@ function perform_retail_market!(
c_G_g,
Q_d_i_g,
Q_d_m_g,
C_h_g,
I_h_g,
C_h_t,
I_h_t,
C_j_g,
C_l_g,
P_bar_h_g,
Expand Down Expand Up @@ -538,8 +540,8 @@ function perform_retail_market!(
@~ Q_d_i_g[:, g] .= @view(S_f[1:I]) .- @view(S_fg[1:I])
@~ Q_d_m_g[:, g] .= @view(S_f[(I + 1):end]) .- @view(S_fg[(I + 1):end])

@~ C_h_g[:, g] .= b
@~ I_h_g[:, g] .= d
@~ C_h_t[:, i] .+= b
Tortar marked this conversation as resolved.
Show resolved Hide resolved
@~ I_h_t[:, i] .+= d

C_j_g[g] = sum(@~ c_G_g[g] .* gov.C_d_j) - sum(@view(C_d_hg[(H + L + 1):(H + L + J)]))
C_l_g[g] = sum(@~ c_E_g[g] .* rotw.C_d_l) - sum(@view(C_d_hg[(H + 1):(H + L)]))
Expand Down
Loading