########################### GATEWAY HEADER ############################# # # _| _| _|_|_| _|_|_| _|_| _|_|_| _|_| # _|_| _|_| _| _| _| _| _| _| _| _| # _| _| _| _| _| _| _|_|_|_| _| _| _| # _| _| _| _| _| _| _| _| _| _| # _| _| _|_|_| _|_|_| _| _| _|_|_| _|_| # # Version 7 # ######################################################################## # # See the MIDACO user manual for detailed information # ######################################################################## # # JULIA GATEWAY to the MIDACO library (midacojl.so / midacojl.dll) # # Call: solution = midaco( problem, option, key ) # # Return: solution["f"], ["g"], ["x"], ["iflag"] and, for o > 1, # solution["pareto"] (number of pareto points) and # solution["pf"] (the pareto front array) # # The problem function is problem["@"] or, without that entry, the # function problem_function(x). # # option["paradigm"] (optional) selects how the P points of a cycle # are evaluated, if option["parallel"] > 1: # # 0 = multithreading Threads.@threads, start Julia with # threads: julia -t 8 example_MINLPc.jl # 1 = multiprocessing Distributed.pmap on worker processes, # start: julia -p 8 example_MINLPc.jl # (define problem_function with # @everywhere, as the example does) # 2 = MPI MPI.jl, start: # mpiexec -n 8 julia example_MINLPc.jl # Every MPI process runs the example, # process 0 runs MIDACO and the others # evaluate points. All of them return the # same solution. MIDACO needs MPI.jl for # this: julia -e 'using Pkg; Pkg.add("MPI")' # ######################################################################## import Distributed function midaco( problem, option, key ) ######################################################################## # ___ ___ _ # # | _ \_ _ ___ __ ___ ______ |_ _|_ _ _ __ _ _| |_ # # | _/ '_/ _ \/ _/ -_|_-<_-< | || ' \| '_ \ || | _| # # |_| |_| \___/\__\___/__/__/ |___|_||_| .__/\_,_|\__| # # |_| # ######################################################################## o = _midaco_count(problem["o"], "o", 1) n = _midaco_count(problem["n"], "n", 1) ni = _midaco_count(problem["ni"], "ni", 0) m = _midaco_count(problem["m"], "m", 0) me = _midaco_count(problem["me"], "me", 0) xl = _midaco_vector(problem["xl"], n, "xl") xu = _midaco_vector(problem["xu"], n, "xu") x = _midaco_vector(problem["x"], n, "x") # Re-scale Infinity values: MIDACO needs finite bounds if any(isinf, xl) || any(isinf, xu) || any(isinf, x) for i=1:n if isinf(xl[i]) xl[i] = -1.0e+6 end if isinf(xu[i]) xu[i] = 1.0e+6 end if isinf(x[i]) x[i] = xl[i] end end println("\n NOTE: Infinity Values have been rescaled to 1.0E+6 \n") end # The problem function: problem["@"] or problem_function func = haskey(problem, "@") ? problem["@"] : problem_function maxeval = option["maxeval"] maxtime = option["maxtime"] printeval = option["printeval"] save2file = option["save2file"] resumable = get(option, "resumable", 0) param = zeros(Float64, 15) param[ 1] = option["param1"] param[ 2] = option["param2"] param[ 3] = option["param3"] param[ 4] = option["param4"] param[ 5] = option["param5"] param[ 6] = option["param6"] param[ 7] = option["param7"] param[ 8] = option["param8"] param[ 9] = option["param9"] param[10] = option["param10"] param[11] = option["param11"] param[12] = option["param12"] param[13] = option["param13"] param[14] = get(option, "param14", 0.0) # SURROGATE param[15] = get(option, "param15", 0.0) # PMISQP PARALLEL = get(option, "parallel", 0) if PARALLEL <= 1 p = 1 else p = Int(PARALLEL) end # Parallelization paradigm: 0 = multithreading, 1 = multiprocessing, 2 = MPI paradigm = get(option, "paradigm", 0) if !(paradigm in (0, 1, 2)) error("MIDACO: option[\"paradigm\"] must be 0 (multithreading), " * "1 (multiprocessing) or 2 (MPI), not $paradigm") end ######################################################################## # paradigm 2 (MPI): midaco() is called on ALL MPI processes. Only # process 0 runs MIDACO below, all others only evaluate points here and # return the solution that process 0 hands out at the end. ######################################################################## MPI = nothing comm = nothing if paradigm == 2 MPI = _midaco_load_mpi() if !_mpi(MPI.Initialized) _mpi(MPI.Init) end comm = MPI.COMM_WORLD if _mpi(MPI.Comm_rank, comm) != 0 _midaco_mpi_worker(MPI, comm, func, o, m) return _mpi(MPI.bcast, nothing, comm; root=0) end end ######################################################################## # Workspace lengths are given by midaco_ws() ######################################################################## lrw = Ref{Cint}(0) liw = Ref{Cint}(0) ccall((:midaco_ws, MIDACO_LIBRARY), Cint, (Ref{Cint},Ref{Cint},Ref{Cint},Ref{Cint},Ref{Cint},Ref{Cint},Ref{Cint}), p, o, n, ni, m, lrw, liw) if lrw[] <= 0 || liw[] <= 0 error("MIDACO cannot split parallel = $p") end lpf = 1 if o > 1 lpf = 1000*(o+m+n)+1+100 if param[10] >= 1.0 lpf = Int(floor( param[10]))*(o+m+n)+1 end if param[10] <= -1.0 lpf = Int(floor(-param[10]))*(o+m+n)+1 end end rw = zeros(Float64, lrw[]) iw = zeros(Cint, liw[]) pf = zeros(Float64, lpf) iflag = Ref{Cint}(0) istop = Ref{Cint}(0) # The P points of a cycle, point c in column c (= one after another) xxx = repeat(x, 1, p) # n x P fff = zeros(Float64, o, p) # o x P ggg = zeros(Float64, max(m,1), p) # m x P (m = 0: one entry dummy) # Arguments of midaco_print that do not change pa = (Cint(printeval), Cint(save2file), Cint(resumable)) dm = (Cint(o), Cint(n), Cint(ni), Cint(m), Cint(me)) lm = (Cint(_midaco_clip(maxeval)), Cint(_midaco_clip(maxtime))) if p > 1 && paradigm == 0 && Threads.nthreads() == 1 println("\n NOTE: Julia runs with 1 thread, the P points are evaluated", " one after another. Start Julia with: julia -t $p ...") end if p > 1 && paradigm == 1 && Distributed.nworkers() == 1 && Distributed.nprocs() == 1 println("\n NOTE: Julia has no worker processes, the P points are evaluated", " one after another. Start Julia with: julia -p $p ...") end ccall((:midaco_screen, MIDACO_LIBRARY), Cvoid, (Ptr{Cvoid},), _midaco_screen_receiver()) try ########################################################################## # ___ _ _ _ _ _ _ # # / _ \ _ __| |_(_)_ __ (_)_____ _| |_(_)___ _ _ | | ___ ___ _ __ # # | (_) | '_ \ _| | ' \| |_ / _` | _| / _ \ ' \ | |__/ _ \/ _ \ '_ \ # # \___/| .__/\__|_|_|_|_|_/__\__,_|\__|_\___/_||_| |____\___/\___/ .__/ # # |_| |_| # ########################################################################## # Print MIDACO head information. The head call gets the P*n array: # a resume (resumable mode) loads all P pending points into it. _midaco_print(1, pa, iflag, istop, fff, ggg, xxx, xl, xu, dm, rw, iw, pf, lm, param, p, key) while istop[] == 0 # Call MIDACO by reverse communication loop # Evaluate the P points of the cycle (Call to OPTIMIZATION PROBLEM) if p == 1 && paradigm != 2 _midaco_store!(fff, ggg, 1, func(xxx[:,1]), o, m) elseif paradigm == 0 Threads.@threads for c = 1:p _midaco_store!(fff, ggg, c, func(xxx[:,c]), o, m) end elseif paradigm == 1 B = _midaco_pmap(func, [xxx[:,c] for c = 1:p]) for c = 1:p _midaco_store!(fff, ggg, c, B[c], o, m) end else _midaco_mpi_evaluate!(MPI, comm, func, xxx, fff, ggg, o, m) end # Check and repair NaN and Inf for i in eachindex(fff) if isnan(fff[i]) fff[i] = 1.0e+33 end if isinf(fff[i]) fff[i] = 1.0e+32 end end for i in eachindex(ggg) if isnan(ggg[i]) ggg[i] = -1.0e+33 end if isinf(ggg[i]) ggg[i] = -1.0e+32 end end ccall((:midaco, MIDACO_LIBRARY), Cint, (Ref{Cint},Ref{Cint},Ref{Cint},Ref{Cint},Ref{Cint},Ref{Cint}, Ptr{Cdouble},Ptr{Cdouble},Ptr{Cdouble},Ptr{Cdouble},Ptr{Cdouble}, Ref{Cint},Ref{Cint},Ptr{Cdouble},Ptr{Cdouble},Ref{Cint}, Ptr{Cint},Ref{Cint},Ptr{Cdouble},Ref{Cint},Ref{Cint},Cstring), p, o, n, ni, m, me, xxx, fff, ggg, xl, xu, iflag, istop, param, rw, lrw, iw, liw, pf, lpf, lm[1], key) _midaco_print(2, pa, iflag, istop, fff, ggg, xxx, xl, xu, dm, rw, iw, pf, lm, param, p, key) end catch if comm !== nothing _mpi(MPI.Abort, comm, 1) end rethrow() finally ccall((:midaco_screen, MIDACO_LIBRARY), Cvoid, (Ptr{Cvoid},), C_NULL) end # paradigm 2: tell the other MPI processes to stop if comm !== nothing for r = 1:_mpi(MPI.Comm_size, comm)-1 _mpi(MPI.send, nothing, comm; dest=r, tag=0) end end ######################################################################## ######################################################################## ######################################################################## ### ___ _ ___ _ _ _ ### ### | _ \___| |_ _ _ _ _ _ _ / __| ___| |_ _| |_(_)___ _ _ ### ### | / -_) _| || | '_| ' \ \__ \/ _ \ | || | _| / _ \ ' \ ### ### |_|_\___|\__|\_,_|_| |_||_| |___/\___/_|\_,_|\__|_\___/_||_| ### ### ### ######################################################################## ######################################################################## ######################################################################## solution = Dict() solution["f"] = fff[1:o, 1] # The solution is the first point solution["g"] = ggg[1:m, 1] solution["x"] = xxx[1:n, 1] solution["iflag"] = Int(iflag[]) solution["pareto"] = pf[1] # Number of pareto points (stored in pf[1]) solution["pf"] = pf # Full array containing the entire PARETO FRONT data # paradigm 2: all MPI processes return the same solution if comm !== nothing _mpi(MPI.bcast, solution, comm; root=0) end return solution ######################################################################## ######################################################################## ######################################################################## end # END OF GATEWAY ######################################################################### # __ __ ___ ___ _ ___ ___ _ _ _ # # | \/ |_ _| \ /_\ / __/ _ \ ___ | | (_) |__ _ _ __ _ _ _ _ _ # # | |\/| || || |) / _ \ (_| (_) | |___| | |__| | '_ \ '_/ _` | '_| || | # # |_| |_|___|___/_/ \_\___\___/ |____|_|_.__/_| \__,_|_| \_, | # # |__/ # ######################################################################### # The MIDACO library is loaded from the folder of this file: # # # # midacojl.dll (Windows) # # midacojl.so (Mac and Linux) # # # # Alternatively, users may enter the full path manually, like # # this: # # # # const MIDACO_LIBRARY = "C:\\Users\\username\\midacojl.dll" # # const MIDACO_LIBRARY = "/home/username/Desktop/midacojl.so" # # # ######################################################################### const MIDACO_LIBRARY = joinpath(@__DIR__, Sys.iswindows() ? "midacojl.dll" : "midacojl.so") function _midaco_print(c, pa, iflag, istop, f, g, x, xl, xu, dm, rw, iw, pf, lm, param, p, key) ccall((:midaco_print, MIDACO_LIBRARY), Cint, (Cint,Cint,Cint,Cint,Ref{Cint},Ref{Cint}, Ptr{Cdouble},Ptr{Cdouble},Ptr{Cdouble},Ptr{Cdouble},Ptr{Cdouble}, Cint,Cint,Cint,Cint,Cint, Ptr{Cdouble},Cint,Ptr{Cint},Cint,Ptr{Cdouble}, Cint,Cint,Ptr{Cdouble},Cint,Cstring), c, pa[1], pa[2], pa[3], iflag, istop, f, g, x, xl, xu, dm[1], dm[2], dm[3], dm[4], dm[5], rw, length(rw), iw, length(iw), pf, lm[1], lm[2], param, p, key) _midaco_screen_flush() end ######################################################################### # MIDACO's screen output. The library hands every piece of screen # # text to _midaco_screen, which only collects it; it is printed by # # Julia right after the library call. # ######################################################################### const _MIDACO_SCREEN = IOBuffer() function _midaco_screen(text::Cstring)::Cvoid write(_MIDACO_SCREEN, unsafe_string(text)) return nothing end _midaco_screen_receiver() = @cfunction(_midaco_screen, Cvoid, (Cstring,)) function _midaco_screen_flush() if _MIDACO_SCREEN.size > 0 print(stdout, String(take!(_MIDACO_SCREEN))) flush(stdout) end end ######################################################################### # Helpers # ######################################################################### # MIDACO's integers are 4-byte 'int': clipped, so that e.g. maxeval = # 10^10 cannot wrap around _midaco_clip(v) = Int(clamp(v, typemin(Cint), typemax(Cint))) function _midaco_count(v, name, lo) if !(v isa Real) || v != round(v) || v < lo error("MIDACO: problem[\"$name\"] must be an integer >= $lo, not $v") end return Int(v) end function _midaco_vector(v, n, name) a = try Float64.(collect(v)) catch error("MIDACO: problem[\"$name\"] must be a list of $n numbers") end if length(a) != n error("MIDACO: problem[\"$name\"] has $(length(a)) entries, but n = $n") end return vec(a) end # f, g = problem_function(x) of point c -> column c of fff and ggg function _midaco_store!(fff, ggg, c, fg, o, m) f, g = fg if length(f) != o error("MIDACO: problem function returned $(length(f)) value(s) for f, but o = $o") end for i = 1:o fff[i,c] = f[i] end if m > 0 if length(g) != m error("MIDACO: problem function returned $(length(g)) value(s) for g, but m = $m") end for i = 1:m ggg[i,c] = g[i] end end end # paradigm 1: the P points on the worker processes function _midaco_pmap(func, A) try return Distributed.pmap(func, A) catch println("\n NOTE: paradigm 1 (multiprocessing) needs the problem function", " on every worker process: define it with @everywhere.") rethrow() end end ######################################################################### # paradigm 2 (MPI) # # # # MPI.jl is loaded only when it is used, so midaco.jl runs without # # it. Everything of MPI.jl is called through _mpi() (invokelatest), # # because the package is loaded after this file was. # ######################################################################### _mpi(f, args...; kwargs...) = Base.invokelatest(f, args...; kwargs...) function _midaco_load_mpi() id = Base.identify_package("MPI") if id === nothing error("MIDACO: option[\"paradigm\"] = 2 (MPI) needs the Julia package ", "MPI.jl: julia -e 'using Pkg; Pkg.add(\"MPI\")'") end return Base.require(id) end # MPI process 0: hand out the points by stride (process r gets the points # r+1, r+1+size, r+1+2*size,...), evaluate the own points, collect all. function _midaco_mpi_evaluate!(MPI, comm, func, xxx, fff, ggg, o, m) s = _mpi(MPI.Comm_size, comm) p = size(xxx, 2) for r = 1:s-1 _mpi(MPI.send, xxx[:, r+1:s:p], comm; dest=r, tag=0) end for c = 1:s:p _midaco_store!(fff, ggg, c, func(xxx[:,c]), o, m) end for r = 1:s-1 F, G = _mpi(MPI.recv, comm; source=r, tag=1) for (k, c) in enumerate(r+1:s:p) fff[:, c] = F[:, k] if m > 0 ggg[:, c] = G[:, k] end end end end # MPI processes 1,2,...: evaluate points until the stop signal (nothing) function _midaco_mpi_worker(MPI, comm, func, o, m) while true X = _mpi(MPI.recv, comm; source=0, tag=0) if X === nothing break end k = size(X, 2) F = zeros(Float64, o, k) G = zeros(Float64, max(m,1), k) try for j = 1:k _midaco_store!(F, G, j, func(X[:,j]), o, m) end catch # An error on one MPI process would leave the others waiting forever Base.display_error(stderr, current_exceptions()) _mpi(MPI.Abort, comm, 1) end _mpi(MPI.send, (F, G), comm; dest=0, tag=1) end end ######################################################################## ############################ END OF FILE ############################### ########################################################################