Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #39 #59

Merged
merged 1 commit into from
Jul 20, 2022
Merged

Fix #39 #59

merged 1 commit into from
Jul 20, 2022

Conversation

SobhanMP
Copy link
Contributor

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

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

@SobhanMP
Copy link
Contributor Author

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

@codecov
Copy link

codecov bot commented Jul 17, 2022

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%) ⬆️
src/c_wrappers.jl 93.54% <100.00%> (+0.95%) ⬆️

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
Copy link
Contributor Author

ah yes, windows does not have sigaction

Comment on lines 150 to 175
Any),
Ptr{Cvoid}),
prob.ref, nb_starting_points, x0s,
exists_feas_sol, x_feas_sol, outputs_feas_sol,
exists_inf_sol, x_inf_sol, outputs_inf_sol, prob)
exists_inf_sol, x_inf_sol, outputs_inf_sol, pointer_from_objref(prob))
Copy link

@mkitti mkitti Jul 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave this as it was with Any. Otherwise, you will have to use GC.@preserve.

Copy link

@mkitti mkitti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thr ccall still needs to be fixed.

statusflag = ccall((:solveNomadProblem, libnomadCInterface), Cint,
(Ptr{Cvoid}, Cint, Ptr{Float64}, # internal data, number of starting points, starting points,
Ptr{Cint}, Ptr{Float64}, Ptr{Float64}, # feasible solution flag, x_feas, outputs feas
Ptr{Cint}, Ptr{Float64}, Ptr{Float64}, # infeasible solution flag, x_inf, output inf
Any),
prob.ref, nb_starting_points, x0s,
exists_feas_sol, x_feas_sol, outputs_feas_sol,
exists_inf_sol, x_inf_sol, outputs_inf_sol, prob)
exists_inf_sol, x_inf_sol, outputs_inf_sol, pointer_from_objref(prob))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the pointer_from_objref.

Copy link

@mkitti mkitti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to do a try ... finally here with the free occurring in the finally. The try should start just after we obtain the CNomadProblem.

In case of an error, we should always free.

@SobhanMP
Copy link
Contributor Author

SobhanMP commented Jul 17, 2022

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

@SobhanMP
Copy link
Contributor Author

@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
Copy link

martinmestre commented Aug 16, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants