fix: use spawn+fetch and refactor module loading

main
Francesco Minnocci 1 year ago
parent 52a08f73f5
commit 65744ea41f
Signed by untrusted user: BachoSeven
GPG Key ID: 2BE4AB7FDAD828A4

@ -6,7 +6,7 @@ module AdaptStep
# Adaptive step size # Adaptive step size
function adapt_step(H, x, t, step, m) function adapt_step(H, x, t, step, m)
Δ = LinearAlgebra.norm([h(variables(H(t))=>x) for h in H(t-step)]) Δ = norm([h(variables(H(t))=>x) for h in H(t-step)])
if Δ > 1e-8 if Δ > 1e-8
step = 0.5 * step step = 0.5 * step
m = 0 m = 0

@ -1,26 +1,29 @@
# External dependencies # External deps
using TypedPolynomials
using LinearAlgebra using LinearAlgebra
using TypedPolynomials
using Distributed, SlurmClusterManager using Distributed, SlurmClusterManager
using SharedArrays addprocs(SlurmClusterManager)
# Local dependencies # Local deps
include("random_poly.jl") include("random_poly.jl")
include("start-system.jl")
include("homotopy.jl")
include("euler-newton.jl")
include("adapt-step.jl")
include("plot.jl") include("plot.jl")
using .RandomPoly using .RandomPoly
using .StartSystem
using .Homotopy
using .EulerNewton
using .AdaptStep
using .Plot using .Plot
@everywhere begin
include("start-system.jl")
include("homotopy.jl")
include("euler-newton.jl")
include("adapt-step.jl")
end
# Macros defined in an @everywhere block aren't available inside it
@everywhere begin
using .StartSystem
using .Homotopy
using .EulerNewton
using .AdaptStep
end
addprocs(SlurmManager()) @everywhere function compute_root(H, r, maxsteps=1000)
function compute_root(H, r, maxsteps=1000)
t = 1.0 t = 1.0
step_size = 0.01 step_size = 0.01
x0 = r x0 = r
@ -40,18 +43,23 @@ end
function solve(F, (G, roots)=start_system(F)) function solve(F, (G, roots)=start_system(F))
H = homotopy(F, G) H = homotopy(F, G)
sols = SharedArray{ComplexF64,2}(length(roots), length(F)) result = Array{Future}(undef, length(roots))
steps = SharedArray{Int64}(length(roots)) for i in eachindex(roots)
@sync @distributed for i in eachindex(roots) result[i] = @spawn compute_root(H, roots[i])
(solutions, step_array) = compute_root(H, roots[i]) end
sols[i, :] = solutions
sols = Array{ComplexF64,2}(undef, length(roots), length(F))
steps = Array{Int64}(undef, length(roots))
for i in eachindex(roots)
(solution, step_array) = fetch(result[i])
sols[i, :] = solution
steps[i] = step_array steps[i] = step_array
end end
return (sols, steps) return (sols, steps)
end end
# Input polynomial systems # Input polynomial system
# @polyvar x y # @polyvar x y
# C = [x^3 - y + 5x^2 - 10, 2x^2 - y - 10] # C = [x^3 - y + 5x^2 - 10, 2x^2 - y - 10]
# Q = [x^2 + 2y, y - 3x^3] # Q = [x^2 + 2y, y - 3x^3]
@ -60,30 +68,15 @@ end
dimension = 2 dimension = 2
R = random_system(2, 2) R = random_system(2, 2)
println("System: ", R) println("System: ", R)
(sol, steps) = solve(R)
# (sC, stepsC) = solve(C) println("Number of steps: ", steps)
# (sQ, stepsQ) = solve(Q)
# (sF, stepsF) = solve(F)
# (sT, stepsT) = solve(T)
(sR, stepsR) = solve(R)
# converting sR to array of arrays instead of a matrix # converting sR to array of arrays instead of a matrix
sR = [sR[i, :] for i in 1:length(sR[:, 1])] sol = [sol[i, :] for i in 1:length(sol[:, 1])]
sol = filter(u -> imag(u[1]) < 0.1 && imag(u[2]) < 0.1, sol)
# println("C: ", stepsC)
# println("Q: ", stepsQ)
# println("F: ", stepsF)
# println("T: ", stepsT)
println("R: ", stepsR)
# sC = filter(u -> imag(u[1]) < 0.1 && imag(u[2]) < 0.1, sC)
# sQ = filter(u -> imag(u[1]) < 0.1 && imag(u[2]) < 0.1, sQ)
# sF = filter(u -> imag(u[1]) < 0.1 && imag(u[2]) < 0.1, sF)
# sT = filter(u -> imag(u[1]) < 0.1 && imag(u[2]) < 0.1, sT)
sR = filter(u -> imag(u[1]) < 0.1 && imag(u[2]) < 0.1, sR)
vars = variables(R) vars = variables(R)
println("Solutions: ", sR) println("Solutions: ", sol)
println("Norms (lower = better): ", [LinearAlgebra.norm([f(vars => s) for f in R]) for s in sR]) println("Norms (lower = better): ", [norm([f(vars => s) for f in R]) for s in sol])
# Plotting the system and the real solutions # Plotting the system and the real solutions
ENV["GKSwstype"] = "nul" ENV["GKSwstype"] = "nul"
@ -91,4 +84,4 @@ ENV["GKSwstype"] = "nul"
# plot_real(sQ, Q, 2, 2, "2") # plot_real(sQ, Q, 2, 2, "2")
# plot_real(sF, F, 4, 4, "3") # plot_real(sF, F, 4, 4, "3")
# plot_real(sT, T, 4, 4, "4") # plot_real(sT, T, 4, 4, "4")
plot_real(sR, R, 5, 5, "random") plot_real(sol, R, 5, 5, "random")
Loading…
Cancel
Save