bbopt / NOMAD.jl

Julia interface to the NOMAD blackbox optimization software
Other
47 stars 5 forks source link

Fix #39 #59

Closed SobhanMP closed 2 years ago

SobhanMP commented 2 years ago

This makes it possible to run threaded function in nomad.jl

ref: https://discourse.julialang.org/t/x/84343

SobhanMP commented 2 years ago

PS. i removed the finalizer because it's non deterministic when it is ran. it's pointless in the code right now.

codecov[bot] commented 2 years ago

Codecov Report

Merging #59 (212e980) into master (d72c5aa) will increase coverage by 0.17%. The diff coverage is 96.51%.

@@            Coverage Diff             @@
##           master      #59      +/-   ##
==========================================
+ Coverage   96.21%   96.38%   +0.17%     
==========================================
  Files           3        3              
  Lines         317      332      +15     
==========================================
+ Hits          305      320      +15     
  Misses         12       12              
Impacted Files Coverage Δ
src/core.jl 97.18% <96.00%> (+0.14%) :arrow_up:
src/c_wrappers.jl 93.54% <100.00%> (+0.95%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update d72c5aa...212e980. Read the comment docs.

SobhanMP commented 2 years ago

ah yes, windows does not have sigaction

SobhanMP commented 2 years ago

yeah, i'll fix this tonight, i think it should no be tied to c_Nomad_problem

SobhanMP commented 2 years ago

@amontoison this is ready to be merged, it's either this or a fork of nomad that doesn't set handlers. After than i'll have another patch ready for the compatibility with nomad 4.2

martinmestre commented 11 months ago

This makes it possible to run threaded function in nomad.jl

ref: https://discourse.julialang.org/t/x/84343

Hi, how should I run multithread with NOMAD.jl. I tried with the following:

function bb(x)
        χ² = χ²Full(x, p)
        c = -1.0
        success = true
        count_eval = true
        bb_outputs = [χ²;c]
        return (success, count_eval, bb_outputs)
    end

"""Worker function."""
function worker(m, ic, r☼, lb, ub)
    local p
    len = length(lb)
    x₀ = 0.5*ones(len)
    p = (m, ic, r☼, lb, ub)
    bb_c(x) = bb(x,p)
    prob = NomadProblem(3, 2, ["OBJ", "EB"], bb_c, lower_bound=zeros(len), upper_bound=ones(len))
    result = solve(prob, x₀)
    return result
end

"""Parallel function."""
function cooperative(m, ic, r☼, lb_g, ub_g, n_grid)
    lb_a, ub_a, x₀_a = build_grid(lb_g, ub_g, n_grid)
    n_full = n_grid^3
    # res = Vector{Float64}(undef, n_full)
    Threads.@threads for i in eachindex(x₀_a)
        println("i=$i $(Threads.threadid())")
        println("lb -- ub = , $(lb_a[i]) -- $(ub_a[i])")
        res = worker(m, ic, r☼, lb_a[i], ub_a[i])
    end
    res
end

but it gives the following error:

Threads=6
m = 300.0
sol_file = "param_optim_pot_m300.txt"
r☼ = 8.122
maxiters = 500
i=3 1
i=1 2
i=8 6
i=6 5
i=7 4
i=5 3
lb -- ub = , [35.0, 28.0, 1.0e-5] -- [40.0, 31.0, 0.002505]
lb -- ub = , [40.0, 25.0, 1.0e-5] -- [45.0, 28.0, 0.002505]
lb -- ub = , [40.0, 28.0, 0.002505] -- [45.0, 31.0, 0.005]
Warning: SubproblemManager::clear() called on non-empty SubproblemManager
lb -- ub = , [40.0, 28.0, 1.0e-5] -- [45.0, 31.0, 0.002505]
lb -- ub = , [40.0, 25.0, 0.002505] -- [45.0, 28.0, 0.005]
lb -- ub = , [35.0, 25.0, 1.0e-5] -- [40.0, 28.0, 0.002505]
free(): double free detected in tcache 2

[85675] signal (6.-6): Aborted
in expression starting at /mnt/md1/mmestre/work/2020/halostream/sources/stream_fit/pipeline_paper/optim_pot.jl:140
Caught seg fault in thread 0Caught seg fault in thread 0

terminate called after throwing an instance of 'NOMAD_4_3::Exception'
terminate called recursively
Aborted

What could be the problem? Thanks a lot.