Skip to content

Commit

Permalink
added @threads benchmark, addresses issue #21
Browse files Browse the repository at this point in the history
  • Loading branch information
floswald committed May 30, 2020
1 parent 1220ce9 commit ca7a53c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
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 2)
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));
22 changes: 14 additions & 8 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,11 +211,13 @@ 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)

for jy in 1:p.ny # future state: owner, renter, income, etc
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

# 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
Expand All @@ -223,14 +226,17 @@ function dc_EGM!(m::GModel,p::Param)
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)
vmat[iid,:] += pr * 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

0 comments on commit ca7a53c

Please sign in to comment.