Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
frapac committed Jan 25, 2024
1 parent 58fd1d4 commit 75ca054
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
12 changes: 8 additions & 4 deletions src/KKT/reduced_newton.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct BieglerKKTSystem{
A::SMT
Gx::SMT
Gu::SMT
mapA::VI
mapA::VI # FIXME
mapGx::VI
mapGu::VI
# Hessian nzval
Expand All @@ -64,7 +64,7 @@ struct BieglerKKTSystem{
j_V::VT
# Regularization terms
reg::Vector{T}
pr_diag::VT
pr_diag::VT # FIXME
du_diag::Vector{T}
l_diag::Vector{T}
u_diag::Vector{T}
Expand Down Expand Up @@ -136,6 +136,10 @@ function MadNLP.create_kkt_system(
A_h = J_h[nx+1:end, :]
# Associated mappings
mapA, mapGx, mapGu = split_jacobian(J, nx, nu)
# Transfer to device
g_mapA = VI(mapA)
g_mapGx = VI(mapGx)
g_mapGu = VI(mapGu)

# Transfer to device
Gx = Gx_h |> SMT
Expand All @@ -146,7 +150,7 @@ function MadNLP.create_kkt_system(
K = HJDJ(W, A)

pr_diag = VT(undef, n + n_slack) ; fill!(pr_diag, zero(T))
du_diag = VT(undef, m) ; fill!(du_diag, zero(T))
du_diag = zeros(m)

reg = zeros(n + n_slack)

Expand Down Expand Up @@ -192,7 +196,7 @@ function MadNLP.create_kkt_system(
etc = Dict{Symbol, Any}(:reduction_time=>0.0, :cond=>Float64[], :scaling_initialized=>false)

return BieglerKKTSystem(
K, Wref, W, J, A, Gx, Gu, mapA, mapGx, mapGu,
K, Wref, W, J, A, Gx, Gu, g_mapA, g_mapGx, g_mapGu,
h_V, j_V,
reg, pr_diag, du_diag,
l_diag, u_diag, l_lower, u_lower,
Expand Down
77 changes: 50 additions & 27 deletions test/Algorithms/MadNLP_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@
using MadNLP
using MadNLPTests

MADNLP_BACKEND = Any[(CPU(), Array, LapackCPUSolver)]
if false # GPU interface currently broken
if CUDA.has_cuda_gpu()
using MadNLPGPU
CUDA_ARCH = (CUDABackend(), CuArray, nothing)
push!(MADNLP_BACKEND, (CUDABackend(), CuArray, LapackGPUSolver))
end

function _test_results_match(ips1, ips2; atol=1e-10)
@test ips1.status == ips2.status
@test ips1.cnt.k == ips2.cnt.k
@test ips1.obj_val ips2.obj_val atol=atol
@test MadNLP.primal(ips1.x) MadNLP.primal(ips2.x) atol=atol
@test ips1.y ips2.y atol=atol
function _test_results_match(solver1, solver2; atol=1e-10)
@test solver1.status == solver2.status
@test solver1.cnt.k == solver2.cnt.k
@test solver1.obj_val solver2.obj_val atol=atol
@test MadNLP.primal(solver1.x) MadNLP.primal(solver2.x) atol=atol
@test solver1.y solver2.y atol=atol
end

# Solve with default options (reference).
Expand Down Expand Up @@ -74,16 +71,17 @@ function _madnlp_biegler_kkt(nlp; kwargs...)
return solver
end

@testset "BieglerKKTSystem ($(backend))" for (backend, Array_, linear_solver) in MADNLP_BACKEND
@testset "[CPU] BieglerKKTSystem" begin
case = "case9.m"
datafile = joinpath(INSTANCES_DIR, case)
opf = Argos.FullSpaceEvaluator(datafile; device=backend)
opf = Argos.FullSpaceEvaluator(datafile)

T = Float64
VI = Array_{Int, 1}
VT = Array_{T, 1}
MT = Array_{T, 2}
VI = Array{Int, 1}
VT = Array{T, 1}
MT = Array{T, 2}

linear_solver = LapackCPUSolver
options = MadNLP.MadNLPOptions(; linear_solver=linear_solver)
options_linear_solver = MadNLP.LapackOptions(
lapack_algorithm=MadNLP.LU,
Expand All @@ -110,7 +108,7 @@ end
MadNLPTests.test_kkt_system(kkt, cb)
end

@testset "MadNLP wrapper: $case" for case in [
@testset "[CPU] MadNLP wrapper: $case" for case in [
"case30.m",
"case57.m",
]
Expand All @@ -123,33 +121,58 @@ end
)
@testset "Reduce-then-linearize" begin
nlp = Argos.ReducedSpaceEvaluator(datafile)
ips = _madnlp_default(nlp; options...)
@test ips.status == MadNLP.SOLVE_SUCCEEDED
solver = _madnlp_default(nlp; options...)
@test solver.status == MadNLP.SOLVE_SUCCEEDED
ipd = _madnlp_dense_kkt(nlp; options...)
_test_results_match(ips, ipd; atol=tol)
_test_results_match(solver, ipd; atol=tol)
ipc = _madnlp_condensed_kkt(nlp; options...)
_test_results_match(ips, ipc; atol=tol)
_test_results_match(solver, ipc; atol=tol)
end
@testset "Linearize-then-reduce" begin
flp = Argos.FullSpaceEvaluator(datafile)
ips = _madnlp_default(flp; options...)
@test ips.status == MadNLP.SOLVE_SUCCEEDED
solver = _madnlp_default(flp; options...)
@test solver.status == MadNLP.SOLVE_SUCCEEDED
ipb = _madnlp_biegler_kkt(flp; options...)
_test_results_match(ips, ipb; atol=tol)
_test_results_match(solver, ipb; atol=tol)
@test ipb.kkt.Wref === flp.hess.H
end
end

@testset "Solve OPF with $form" for form in [
@testset "[CPU] Solve OPF with $form" for form in [
Argos.FullSpace(),
Argos.BieglerReduction(),
Argos.DommelTinney(),
]
case = "case9.m"
datafile = joinpath(INSTANCES_DIR, case)

ips = Argos.run_opf(datafile, form; tol=1e-5, print_level=MadNLP.ERROR)
@test isa(ips, MadNLP.MadNLPSolver)
@test ips.status == MadNLP.SOLVE_SUCCEEDED
solver = Argos.run_opf(datafile, form; tol=1e-5, print_level=MadNLP.ERROR)
@test isa(solver, MadNLP.MadNLPSolver)
@test solver.status == MadNLP.SOLVE_SUCCEEDED
end

if CUDA.has_cuda_gpu()
# Test BieglerKKTSystem on the GPU.
@testset "[CUDA] Solve OPF with BieglerKKTSystem" begin
case = "case9.m"
datafile = joinpath(INSTANCES_DIR, case)
opf = Argos.FullSpaceEvaluator(datafile)
solver_ref = _madnlp_default(opf; print_level=MadNLP.ERROR)

opf_gpu = Argos.FullSpaceEvaluator(datafile; device=CUDABackend())
KKT = Argos.BieglerKKTSystem{Float64, CuVector{Int}, CuVector{Float64}, CuMatrix{Float64}}
nlp = Argos.OPFModel(Argos.bridge(opf_gpu))
solver_gpu = MadNLP.MadNLPSolver(
nlp;
kkt_system=KKT,
linear_solver=LapackGPUSolver,
lapack_algorithm=MadNLP.CHOLESKY,
callback=MadNLP.SparseCallback,
equality_treatment=MadNLP.EnforceEquality,
print_level=MadNLP.ERROR,
)
stats = MadNLP.solve!(solver_gpu)
_test_results_match(solver_ref, solver_gpu; atol=1e-6)
end
end

0 comments on commit 75ca054

Please sign in to comment.