-
Notifications
You must be signed in to change notification settings - Fork 0
[WIP] QuadraticModels extension #22
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
base: main
Are you sure you want to change the base?
Changes from all commits
4a6d363
c11788d
1be7ebc
5b2e5b3
1a29872
7765e8e
1647568
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| module CoolPDLPQuadraticModelsExt | ||
|
|
||
| import QuadraticModels: QuadraticModel | ||
| import CoolPDLP | ||
|
|
||
| function CoolPDLP.MILP(qm::QuadraticModel; ignore_islp = false, kwargs...) | ||
| ignore_islp || @assert qm.meta.islp | ||
|
|
||
| return CoolPDLP.MILP(; | ||
| c = qm.data.c, | ||
| lv = qm.meta.lvar, | ||
| uv = qm.meta.uvar, | ||
| A = qm.data.A, | ||
| lc = qm.meta.lcon, | ||
| uc = qm.meta.ucon, | ||
| name = qm.meta.name, | ||
| kwargs... | ||
| ) | ||
| end | ||
|
|
||
| end # module | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,12 +52,18 @@ struct MILP{ | |
| "file path the MILP was read from" | ||
| path::String | ||
|
|
||
| _convert_or_construct(A) = try | ||
| convert(typeof(A), transpose(A)) | ||
| catch | ||
| typeof(A).name.wrapper(transpose(A)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accessing private fields is generally considered an antipattern in Julia. Besides, the |
||
| end | ||
|
|
||
| function MILP(; | ||
| c, | ||
| lv, | ||
| uv, | ||
| A, | ||
| At = convert(typeof(A), transpose(A)), | ||
| At = _convert_or_construct(A), | ||
| lc, | ||
| uc, | ||
| D1 = Diagonal(one!(similar(lc))), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| using Test, SparseArrays, Adapt | ||
| using CoolPDLP, QuadraticModels | ||
| using CUDA, CUDA.CUSPARSE | ||
| using JLArrays | ||
| using KernelAbstractions | ||
| using GPUArraysCore: @allowscalar | ||
|
|
||
| c = [2.0, 3.0] | ||
| H = spzeros(2, 2) | ||
| A = sparse([1, 1], [1, 2], [1.0, 2.0], 1, 2) | ||
| lvar = [0.0, 0.0] | ||
| uvar = [5.0, 5.0] | ||
| lcon = [1.0] | ||
| ucon = [4.0] | ||
|
|
||
| @testset "QuadraticModel → CPU MILP" begin | ||
| qm = QuadraticModel(c, H; A, lvar, uvar, lcon, ucon, name = "tiny_lp") | ||
| milp = CoolPDLP.MILP(qm) | ||
|
|
||
| @test milp.c ≈ c | ||
| @test milp.lv ≈ lvar | ||
| @test milp.uv ≈ uvar | ||
| @test milp.lc ≈ lcon | ||
| @test milp.uc ≈ ucon | ||
| @test Matrix(milp.A) ≈ Matrix(A) | ||
| @test milp.name == "tiny_lp" | ||
| end | ||
|
|
||
| @testset "QuadraticModel → device MILP" begin | ||
| A_dev = adapt(JLBackend(), GPUSparseMatrixCOO(A)) | ||
| H_dev = adapt(JLBackend(), GPUSparseMatrixCOO(H)) | ||
| c_dev = jl(c) | ||
| lv_dev = jl(lvar) | ||
| uv_dev = jl(uvar) | ||
| lc_dev = jl(lcon) | ||
| uc_dev = jl(ucon) | ||
|
|
||
| # we need @allowscalar since initializing NLPModelMeta uses findall | ||
| qm = @allowscalar QuadraticModel( | ||
| c_dev, H_dev; | ||
| A = A_dev, lvar = lv_dev, uvar = uv_dev, lcon = lc_dev, ucon = uc_dev, | ||
| name = "tiny_lp", | ||
| ) | ||
| milp = CoolPDLP.MILP(qm) | ||
|
|
||
| @test milp.c isa JLVector{Float64} | ||
| @test milp.lv isa JLVector{Float64} | ||
| @test milp.lc isa JLVector{Float64} | ||
| @test milp.A isa GPUSparseMatrixCOO{Float64, Int, JLVector{Float64}, JLVector{Int}} | ||
| @test milp.At isa GPUSparseMatrixCOO{Float64, Int, JLVector{Float64}, JLVector{Int}} | ||
| @test get_backend(milp.A) isa JLBackend | ||
|
|
||
| @test Array(milp.c) ≈ c | ||
| @test Array(milp.lv) ≈ lvar | ||
| @test Array(milp.uv) ≈ uvar | ||
| @test Array(milp.lc) ≈ lcon | ||
| @test Array(milp.uc) ≈ ucon | ||
| @test milp.name == "tiny_lp" | ||
| end | ||
|
|
||
| if CUDA.functional() | ||
| @testset "QuadraticModel → CUDA MILP" begin | ||
| A_cu = CuSparseMatrixCSR(A) | ||
| H_cu = CuSparseMatrixCSR(H) | ||
| c_cu = CuVector(c) | ||
| lv_cu = CuVector(lvar) | ||
| uv_cu = CuVector(uvar) | ||
| lc_cu = CuVector(lcon) | ||
| uc_cu = CuVector(ucon) | ||
|
|
||
| # we need @allowscalar since initializing NLPModelMeta uses findall | ||
| qm = @allowscalar QuadraticModel( | ||
| c_cu, H_cu; | ||
| A = A_cu, lvar = lv_cu, uvar = uv_cu, lcon = lc_cu, ucon = uc_cu, | ||
| name = "tiny_lp", | ||
| ) | ||
| milp = CoolPDLP.MILP(qm) | ||
|
|
||
| @test milp.c isa CuVector{Float64} | ||
| @test milp.lv isa CuVector{Float64} | ||
| @test milp.lc isa CuVector{Float64} | ||
| @test milp.A isa CuSparseMatrixCSR | ||
| @test milp.At isa CuSparseMatrixCSR | ||
|
|
||
| @test Array(milp.c) ≈ c | ||
| @test Array(milp.lv) ≈ lvar | ||
| @test Array(milp.uv) ≈ uvar | ||
| @test Array(milp.lc) ≈ lcon | ||
| @test Array(milp.uc) ≈ ucon | ||
| @test milp.name == "tiny_lp" | ||
| end | ||
| else | ||
| @info "Skipping CUDA QuadraticModels test" CUDA.functional() | ||
| end |
Uh oh!
There was an error while loading. Please reload this page.