From 75ca054d03d3f6ebde9948153a3108bc4b4b4118 Mon Sep 17 00:00:00 2001 From: fpacaud Date: Thu, 25 Jan 2024 10:41:59 +0100 Subject: [PATCH] minor fixes --- src/KKT/reduced_newton.jl | 12 +++-- test/Algorithms/MadNLP_wrapper.jl | 77 ++++++++++++++++++++----------- 2 files changed, 58 insertions(+), 31 deletions(-) diff --git a/src/KKT/reduced_newton.jl b/src/KKT/reduced_newton.jl index 17cf252..a182dc3 100644 --- a/src/KKT/reduced_newton.jl +++ b/src/KKT/reduced_newton.jl @@ -55,7 +55,7 @@ struct BieglerKKTSystem{ A::SMT Gx::SMT Gu::SMT - mapA::VI + mapA::VI # FIXME mapGx::VI mapGu::VI # Hessian nzval @@ -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} @@ -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 @@ -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) @@ -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, diff --git a/test/Algorithms/MadNLP_wrapper.jl b/test/Algorithms/MadNLP_wrapper.jl index be7e8fc..532dddb 100644 --- a/test/Algorithms/MadNLP_wrapper.jl +++ b/test/Algorithms/MadNLP_wrapper.jl @@ -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). @@ -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, @@ -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", ] @@ -123,24 +121,24 @@ 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(), @@ -148,8 +146,33 @@ end 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