Skip to content

Commit 79927dd

Browse files
authored
[docs] add an example to the Parallelism tutorial about channels (#4155)
1 parent 7f3a9f5 commit 79927dd

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

docs/src/tutorials/algorithms/parallelism.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,80 @@ A JuMP Model
399399
for help on the [community forum](https://jump.dev/forum). Make sure to
400400
include a reproducible example of your code.
401401

402+
## Example: using Channels
403+
404+
Here's an example where we split the model building from the model solution.
405+
Instead of using an explicit `ReentrantLock`, we use a `Channel` to store the
406+
solutions.
407+
408+
```julia
409+
julia> using JuMP
410+
411+
julia> import HiGHS
412+
413+
julia> function build_model(s::Int; N::Int = 80)
414+
model = Model(HiGHS.Optimizer)
415+
set_silent(model)
416+
@variable(model, x[1:N], Bin)
417+
@variable(model, y[1:N], Bin)
418+
@variable(model, z[1:N, 1:N] >= 0)
419+
@constraint(model, [i in 1:N, j in 1:N], z[i, j] <= x[i])
420+
@constraint(model, [i in 1:N, j in 1:N], z[i, j] <= y[j])
421+
@constraint(model, [i in 1:N, j in 1:N], z[i, j] >= x[i] + y[j] - 1)
422+
@objective(model, Min, sum(rand(-10:10) * i for i in z))
423+
set_time_limit_sec(model, s)
424+
return model
425+
end
426+
build_model (generic function with 1 method)
427+
428+
julia> struct Solution
429+
scenario::Int
430+
objective_value::Float64
431+
end
432+
433+
julia> function solve_model(ch::Channel{Solution}, s::Int, model::Model)
434+
optimize!(model)
435+
@assert has_values(model) # There is always the trivial solution
436+
put!(ch, Solution(s, objective_value(model)))
437+
return
438+
end
439+
solve_model (generic function with 1 method)
440+
441+
julia> function run_channel_example(S::Int)
442+
models = Vector{Model}(undef, S)
443+
Threads.@threads for s in 1:S
444+
models[s] = build_model(s)
445+
end
446+
ch = Channel{Solution}()
447+
for (s, model) in enumerate(models)
448+
Threads.@spawn solve_model(ch, s, model)
449+
end
450+
for i in 1:S
451+
solution = take!(ch)
452+
println("s=$(solution) [solved $i/$S]")
453+
end
454+
return
455+
end
456+
run_channel_example (generic function with 1 method)
457+
458+
julia> run_channel_example(15)
459+
s=Solution(1, -380.0) [solved 1/15]
460+
s=Solution(3, -73.0) [solved 2/15]
461+
s=Solution(4, -229.0) [solved 3/15]
462+
s=Solution(10, -195.0) [solved 4/15]
463+
s=Solution(8, 0.0) [solved 5/15]
464+
s=Solution(14, -434.0) [solved 6/15]
465+
s=Solution(12, -483.0) [solved 7/15]
466+
s=Solution(7, -315.0) [solved 8/15]
467+
s=Solution(2, -696.0) [solved 9/15]
468+
s=Solution(9, -116.0) [solved 10/15]
469+
s=Solution(15, -471.0) [solved 11/15]
470+
s=Solution(11, -518.0) [solved 12/15]
471+
s=Solution(6, -37.0) [solved 13/15]
472+
s=Solution(5, 0.0) [solved 14/15]
473+
s=Solution(13, -390.0) [solved 15/15]
474+
```
475+
402476
## Distributed computing
403477

404478
To use distributed computing with Julia, use the `Distributed` package:

0 commit comments

Comments
 (0)