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

added @threads benchmark, addresses issue #21 #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Florian Oswald <[email protected]>"]
version = "0.1.0"

[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
Expand Down
4 changes: 4 additions & 0 deletions runb.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#! /usr/local/bin/fish
for n in (seq 4)
env JULIA_NUM_THREADS=$n julia --project=. ./src/bmark.jl
end
14 changes: 14 additions & 0 deletions src/bmark.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using DCEGM
using BenchmarkTools
println("using $(Threads.nthreads()) threads")
println("===========================")
na = 5000; ny = 50

println("fedors model on na=$na,ny=$ny")
DCEGM.runf(par = Dict(:na => na, :ny => ny));
@btime DCEGM.runf(par = Dict(:na => na, :ny => ny));

na = 500; ny = 15
println("general model on na=$na,ny=$ny")
DCEGM.rung(par = Dict(:na => na, :ny => ny));
@btime DCEGM.rung(par = Dict(:na => na, :ny => ny));
20 changes: 13 additions & 7 deletions src/dc_algo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function dc_EGM!(m::FModel,p::Param)
end
else
for id in 1:p.nD # current period dchoice
# Threads.@threads for id in 1:p.nD # current period dchoice
working = id==1 # working today is id=1
# what's next period's cash on hand given you work/not TODAY?
mm1 = m.m1[it][id]
Expand All @@ -121,7 +122,7 @@ function dc_EGM!(m::FModel,p::Param)
vmat = fill(-Inf,p.nD,p.na*p.ny)

# get future cons and values
for iid in 1:p.nD
Threads.@threads for iid in 1:p.nD
c1 = interp(m.c[iid,it+1].env, mm1[:])
floory!(c1,p.cfloor) # floor negative consumption
cmat[iid,:] = gety(c1) # get y-values
Expand Down Expand Up @@ -191,7 +192,7 @@ function dc_EGM!(m::GModel,p::Param)
cmat = fill(-Inf,p.nD,p.na)
vmat = fill(-Inf,p.nD,p.na)
ctmp = fill(-Inf,p.nD,p.ny,p.na)
# vtmp = fill(-Inf,p.nD,p.ny,p.na)
vtmp = fill(-Inf,p.ny,p.nD,p.na)

for it in p.nT:-1:1
for iy in 1:p.ny # current state
Expand All @@ -210,27 +211,32 @@ function dc_EGM!(m::GModel,p::Param)
# reset all value matrices
fill!(vmat,0.0)
fill!(ctmp,-Inf)
# fill!(vtmp,0.0)
# fill!(vtmp,-Inf)

# Threads.@threads for jy in 1:p.ny # future state: owner, renter, income, etc
for jy in 1:p.ny # future state: owner, renter, income, etc
pr = m.ywgt[iy,jy] # transprob

for iid in 1:p.nD # future dchoice
Threads.@threads for iid in 1:p.nD # future dchoice
# for iid in 1:p.nD # future dchoice
# only feasible choices at this state
# if renter, cannot sell etc

m1 = m.m1[it+1][iid][jy,:] # state specific mvec
c1 = interp(m.c[iid,jy,it+1].env, m1) # C(d',y',m')
floory!(c1,p.cfloor) # floor negative consumption
ctmp[iid,jy,:] = gety(c1)
# vtmp[iid,jy,:] = vfun(iid,it+1,ctmp[iid,jy,:],m1,m.v[iid,jy,it+1],p)
# vtmp[jy,iid,:] = pr * vfun(iid,it+1,ctmp[iid,jy,:],m1,m.v[iid,jy,it+1],p)
vmat[iid,:] += pr * vfun(iid,it+1,ctmp[iid,jy,:],m1,m.v[iid,jy,it+1],p)
end
end # end future state

# now get expectated value function conditional on iy: E[V(t+1,iid,y')|iy]
# vmat = integrate over second dimension
# vmat = dropdims( reduce(+, vtmp, dims = 2), dims = 2)
# vmat = integrate over first dimension
# vmat2 = dropdims( reduce(+, vtmp, dims = 1), dims = 1)
# @assert all(vmat2 .== vmat)

# vmat = dropdims( reduce(+, vtmp, dims = 1), dims = 1)

# get ccp of choices: P(d'|iy), pwork
pwork = working ? ccp(vmat,p) : zeros(size(vmat)[2])
Expand Down