-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from psrenergy/jg/optim
Add optim test
- Loading branch information
Showing
3 changed files
with
58 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import JobQueueMPI as JQM | ||
using Test | ||
using MPI | ||
using Optim | ||
|
||
JQM.mpi_init() | ||
|
||
N = 5 | ||
data = collect(1:N) | ||
function g(x, i, data) | ||
return i * (x[i] - 2 * i) ^ 2 + data[i] | ||
end | ||
x0 = zeros(N) | ||
list_i = collect(1:N) | ||
fake_input = Int[] # ignored | ||
|
||
|
||
let | ||
is_done = false | ||
if JQM.is_controller_process() | ||
ret = optimize(x0, NelderMead()) do x | ||
MPI.bcast(is_done, MPI.COMM_WORLD) | ||
g_x = JQM.pmap((v)->g(v[1], v[2], data), [(x, i) for i in list_i]) | ||
return sum(g_x) | ||
end | ||
# tell workers to stop calling pmap | ||
is_done = true | ||
MPI.bcast(is_done, MPI.COMM_WORLD) | ||
@testset "pmap MPI Optim" begin | ||
@test maximum(Optim.minimizer(ret) .- collect(2:2:2N)) ≈ 0.0 atol = 1e-3 | ||
@test Optim.minimum(ret) ≈ sum(1:N) atol = 1e-3 | ||
end | ||
else | ||
# optimize will call pmap multiple times | ||
# hence, the other processes need to be continuously calling pmap | ||
# to work with the controller. | ||
# once the controller leaves the optimize function it will | ||
# broadcast a is_done = true, so that the workers stop the | ||
# infinite loop of pmaps. | ||
while true | ||
is_done = MPI.bcast(is_done, MPI.COMM_WORLD) | ||
if is_done | ||
break | ||
end | ||
JQM.pmap((v)->g(v[1], v[2], data), fake_input) | ||
end | ||
end | ||
end | ||
|
||
JQM.mpi_barrier() | ||
|
||
JQM.mpi_finalize() |