-
Notifications
You must be signed in to change notification settings - Fork 94
Add tests for Utilities/sparse_matrix.jl #1369
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
module TestSparseMatrix | ||
|
||
import SparseArrays | ||
using Test | ||
|
||
import MathOptInterface | ||
const MOI = MathOptInterface | ||
|
||
function runtests() | ||
for name in names(@__MODULE__; all = true) | ||
if startswith("$(name)", "test_") | ||
@testset "$(name)" begin | ||
getfield(@__MODULE__, name)() | ||
end | ||
end | ||
end | ||
return | ||
end | ||
|
||
""" | ||
test_empty_size() | ||
|
||
Julia 1.7 is going to outlaw constructing empty sparse matrices. Test that we | ||
can still do so. | ||
""" | ||
function test_empty_size() | ||
A = MOI.Utilities.MutableSparseMatrixCSC{ | ||
Float64, | ||
Int, | ||
MOI.Utilities.ZeroBasedIndexing, | ||
}() | ||
MOI.empty!(A) | ||
@test A.rowval == Int[] | ||
@test A.nzval == Float64[] | ||
@test A.colptr == Int[0] | ||
end | ||
|
||
function test_empty_large() | ||
A = MOI.Utilities.MutableSparseMatrixCSC{ | ||
Float64, | ||
Int, | ||
MOI.Utilities.ZeroBasedIndexing, | ||
}() | ||
MOI.empty!(A) | ||
MOI.Utilities.add_column(A) | ||
MOI.Utilities.add_column(A) | ||
MOI.Utilities.set_number_of_rows(A, 5) | ||
B = convert(SparseArrays.SparseMatrixCSC{Float64,Int}, A) | ||
@test size(B) == (5, 2) | ||
end | ||
|
||
function test_VectorAffine_ZeroBased() | ||
A = MOI.Utilities.MutableSparseMatrixCSC{ | ||
Float64, | ||
Int, | ||
MOI.Utilities.ZeroBasedIndexing, | ||
}() | ||
MOI.empty!(A) | ||
x = MOI.VariableIndex.(1:3) | ||
f = MOI.VectorAffineFunction( | ||
vcat( | ||
MOI.VectorAffineTerm.(1, MOI.ScalarAffineTerm.(1.0, x)), | ||
MOI.VectorAffineTerm.(2, MOI.ScalarAffineTerm.([2.0, 3.0], x[2:3])), | ||
), | ||
[0.5, 1.2], | ||
) | ||
|
||
index_map = MOI.Utilities.IndexMap() | ||
for i in 1:3 | ||
MOI.Utilities.add_column(A) | ||
index_map[x[i]] = x[i] | ||
end | ||
MOI.Utilities.allocate_terms(A, index_map, f) | ||
MOI.Utilities.set_number_of_rows(A, 2) | ||
MOI.Utilities.load_terms(A, index_map, f, 0) | ||
MOI.Utilities.final_touch(A) | ||
B = convert(SparseArrays.SparseMatrixCSC{Float64,Int}, A) | ||
@test B == [1.0 1.0 1.0; 0.0 2.0 3.0] | ||
@test A.rowval == [0, 0, 1, 0, 1] | ||
@test A.nzval == [1.0, 1.0, 2.0, 1.0, 3.0] | ||
@test A.colptr == [0, 1, 3, 5] | ||
end | ||
|
||
function test_VectorAffine_OneBased() | ||
A = MOI.Utilities.MutableSparseMatrixCSC{ | ||
Float64, | ||
Int, | ||
MOI.Utilities.OneBasedIndexing, | ||
}() | ||
MOI.empty!(A) | ||
x = MOI.VariableIndex.(1:3) | ||
f = MOI.VectorAffineFunction( | ||
vcat( | ||
MOI.VectorAffineTerm.(1, MOI.ScalarAffineTerm.(1.0, x)), | ||
MOI.VectorAffineTerm.(2, MOI.ScalarAffineTerm.([2.0, 3.0], x[2:3])), | ||
), | ||
[0.5, 1.2], | ||
) | ||
|
||
index_map = MOI.Utilities.IndexMap() | ||
for i in 1:3 | ||
MOI.Utilities.add_column(A) | ||
index_map[x[i]] = x[i] | ||
end | ||
MOI.Utilities.allocate_terms(A, index_map, f) | ||
MOI.Utilities.set_number_of_rows(A, 2) | ||
MOI.Utilities.load_terms(A, index_map, f, 0) | ||
MOI.Utilities.final_touch(A) | ||
B = convert(SparseArrays.SparseMatrixCSC{Float64,Int}, A) | ||
@test B == [1.0 1.0 1.0; 0.0 2.0 3.0] | ||
@test A.rowval == [1, 1, 2, 1, 2] | ||
@test A.nzval == [1.0, 1.0, 2.0, 1.0, 3.0] | ||
@test A.colptr == [1, 2, 4, 6] | ||
end | ||
|
||
function test_ScalarAffine_ZeroBased() | ||
A = MOI.Utilities.MutableSparseMatrixCSC{ | ||
Float64, | ||
Int, | ||
MOI.Utilities.ZeroBasedIndexing, | ||
}() | ||
MOI.empty!(A) | ||
x = MOI.VariableIndex.(1:3) | ||
f1 = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(1.0, x), 0.5) | ||
f2 = | ||
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([2.0, 3.0], x[2:3]), 1.2) | ||
index_map = MOI.Utilities.IndexMap() | ||
for i in 1:3 | ||
MOI.Utilities.add_column(A) | ||
index_map[x[i]] = x[i] | ||
end | ||
MOI.Utilities.allocate_terms(A, index_map, f1) | ||
MOI.Utilities.allocate_terms(A, index_map, f2) | ||
MOI.Utilities.set_number_of_rows(A, 2) | ||
MOI.Utilities.load_terms(A, index_map, f1, 0) | ||
MOI.Utilities.load_terms(A, index_map, f2, 1) | ||
MOI.Utilities.final_touch(A) | ||
B = convert(SparseArrays.SparseMatrixCSC{Float64,Int}, A) | ||
@test B == [1.0 1.0 1.0; 0.0 2.0 3.0] | ||
@test A.rowval == [0, 0, 1, 0, 1] | ||
@test A.nzval == [1.0, 1.0, 2.0, 1.0, 3.0] | ||
@test A.colptr == [0, 1, 3, 5] | ||
end | ||
|
||
function test_ScalarAffine_OneBased() | ||
A = MOI.Utilities.MutableSparseMatrixCSC{ | ||
Float64, | ||
Int, | ||
MOI.Utilities.OneBasedIndexing, | ||
}() | ||
MOI.empty!(A) | ||
x = MOI.VariableIndex.(1:3) | ||
f1 = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(1.0, x), 0.5) | ||
f2 = | ||
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([2.0, 3.0], x[2:3]), 1.2) | ||
index_map = MOI.Utilities.IndexMap() | ||
for i in 1:3 | ||
MOI.Utilities.add_column(A) | ||
index_map[x[i]] = x[i] | ||
end | ||
MOI.Utilities.allocate_terms(A, index_map, f1) | ||
MOI.Utilities.allocate_terms(A, index_map, f2) | ||
MOI.Utilities.set_number_of_rows(A, 2) | ||
MOI.Utilities.load_terms(A, index_map, f1, 0) | ||
MOI.Utilities.load_terms(A, index_map, f2, 1) | ||
MOI.Utilities.final_touch(A) | ||
B = convert(SparseArrays.SparseMatrixCSC{Float64,Int}, A) | ||
@test B == [1.0 1.0 1.0; 0.0 2.0 3.0] | ||
@test A.rowval == [1, 1, 2, 1, 2] | ||
@test A.nzval == [1.0, 1.0, 2.0, 1.0, 3.0] | ||
@test A.colptr == [1, 2, 4, 6] | ||
end | ||
|
||
end | ||
|
||
TestSparseMatrix.runtests() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@blegat what was the motivation to have these
_allocate_terms
function barriers?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It dates back to jump-dev/SCS.jl#192 where I benchmarked the two approaches and the function barrier seemed to be faster even if I didn't really get why.
It's maybe not the case anymore in Julia v1.6 or maybe I was mistaken when writing this.
We can revisit later. An easier speedup would be obtained by using
@inbounds
I guess.The good thing with MatrixOfConstraints is that we will be able to spend time optimizing these pieces at once in MOI and it will affect all solver's
copy_to
performance so it might start being worth playing with@inbounds
, etc...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the massive amount of new complexity we're introducing, I'd rather that we merged a simplified version first, got it working and debugged, then wrote the various solvers, and only then revisited performance implications with benchmarks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, definitely