Skip to content

Commit 8076bb5

Browse files
committed
[DEV] Add function to parallelize SDDP
1 parent a4d46ec commit 8076bb5

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

examples/parallel_sddp.jl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
import StochDynamicProgramming
3+
4+
require("damsvalley.jl")
5+
6+
# Perform n parallel passes:
7+
N_PARALLEL_COMPUTATIONS = 3
8+
# Synchronize cuts every n iterations:
9+
SYNCHRONIZE = 5
10+
11+
# Import model and params in main worker
12+
# /!\ This line must be before redefinition of seed!
13+
model, params = init_problem()
14+
15+
# Redefine seeds in every processes to maximize randomness:
16+
@everywhere srand()
17+
18+
params.maxItNumber = SYNCHRONIZE
19+
# First pass of algorithm to define value functions in memory:
20+
V = StochDynamicProgramming.solve_SDDP(model, params)[1]
21+
22+
# Count number of available CPU:
23+
ncpu = nprocs() - 1
24+
println("\nLaunch simulation on ", ncpu, " processes")
25+
workers = procs()[2:end]
26+
27+
# As we distribute computation in n process, we perform forward pass in parallel:
28+
params.forwardPassNumber = max(1, round(Int, params.forwardPassNumber/ncpu))
29+
30+
# Start parallel computation:
31+
for i in 1:N_PARALLEL_COMPUTATIONS
32+
# Distribute computation of SDDP in each process:
33+
refs = [@spawnat w StochDynamicProgramming.solve_SDDP(model, params, V, 1)[1] for w in workers]
34+
# Catch the result in the main process:
35+
V = StochDynamicProgramming.catcutsarray([fetch(r) for r in refs]...)
36+
# We clean the resultant cuts:
37+
StochDynamicProgramming.remove_redundant_cuts!(V)
38+
StochDynamicProgramming.prune_cuts!(model, params, V)
39+
println("Lower bound at pass ", i, ": ", StochDynamicProgramming.get_lower_bound(model, params, V))
40+
end
41+

0 commit comments

Comments
 (0)