Skip to content
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

#127 fix: support cones for generic number types. #136

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ version = "0.8.1"
AMD = "14f7f29c-3bd6-536c-9a0b-7339e30b5a3e"
COSMOAccelerators = "bbd8fffe-5ad0-4d78-a55e-85575421b4ac"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
GenericLinearAlgebra = "14197337-ba66-59df-a3e3-ca00e7dcff7a"
IterTools = "c8e1da08-722c-5040-9ed9-7db0dc04731e"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
QDLDL = "bfc457fd-c171-5ab7-bd9e-d5dbfc242d63"
Expand All @@ -29,8 +31,8 @@ DataStructures = "^0.17.0, ^0.18.0"
IterTools = "^1"
MathOptInterface = "~0.9.16"
QDLDL = "~0.1.4"
Requires = "^1"
Reexport = "0.2, ^1"
Requires = "^1"
UnsafeArrays = "0.3, 1"
julia = "^1"

Expand Down
3 changes: 2 additions & 1 deletion src/COSMO.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__precompile__()
module COSMO

using SparseArrays, LinearAlgebra, SuiteSparse, QDLDL, Pkg, DataStructures, Requires, Printf, IterTools
using SparseArrays, LinearAlgebra, SuiteSparse, QDLDL, Pkg, DataStructures, Requires, Printf, IterTools, GenericLinearAlgebra, MutableArithmetics
using Reexport
using COSMOAccelerators

Expand All @@ -12,6 +12,7 @@ export assemble!, warm_start!, empty_model!, update!

const DefaultFloat = Float64
const DefaultInt = LinearAlgebra.BlasInt
const MA = MutableArithmetics


include("./kktsolver.jl")
Expand Down
27 changes: 23 additions & 4 deletions src/convexset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ for (syevr, elty) in
end #@eval
end #for

function _project!(X::AbstractMatrix, ws::PsdBlasWorkspace{T}) where{T}
function _project!(X::AbstractMatrix, ws::PsdBlasWorkspace{T}) where{T <: Union{Float32,Float64}}

#computes the upper triangular part of the projection of X onto the PSD cone

Expand All @@ -203,6 +203,23 @@ function _project!(X::AbstractMatrix, ws::PsdBlasWorkspace{T}) where{T}
rank_k_update!(X, ws)
end

function _project!(X::AbstractMatrix{T}, ws::PsdBlasWorkspace{T}) where {T}
w,Z = GenericLinearAlgebra.eigen(GenericLinearAlgebra.Hermitian(X))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reallocates w and Z at every iteration. Is there a non-allocating version of GenericAlgebra.eigen ? eigen! perhaps?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a function with that name but I do not understand how it works (and where it stores the result). I tried several things and no luck yet :/

# The follwoing lines uses MutableArithmetics to perform the operation : X .= Z*LinearAlgebra.Diagonal(max.(w,0))*Z'
X = zero(X)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of this line, you don't modify the input X of this function

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be MA mutable_operate!(zero, X)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which gives :

ERROR: ArgumentError: Cannot call `mutable_operate!(zero, ::Base.ReshapedArray{BigFloat, 2, SubArray{BigFloat, 1, Vector{BigFloat}, Tuple{UnitRange{Int64}}, true}, Tuple{}})` as objects of type `Base.ReshapedArray{BigFloat, 2, SubArray{BigFloat, 1, Vector{BigFloat}, Tuple{UnitRange{Int64}}, true}, Tuple{}}` cannot be modifed to equal the result of the operation. Use `operate!` instead which returns the value of the result (possibly modifying the first argument) to write generic code that also works when the type cannot be modified.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently, objects of type Base.ReshapedArray{BigFloat, 2, SubArray{BigFloat, 1, Vector{BigFloat}, Tuple{UnitRange{Int64}}, true}, Tuple{}} are not mutable, and this is the type of X.

Copy link
Author

@lrnv lrnv Jul 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which means that the X that lends in the function is not mutable ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @blegat, if this is still a blocker

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should implement MA.operate(zero, ...) for that type of array. IIUC, this function is expected to modify the input array. In that case, it link the local variable X to a new array and then modify it but it is not returned or anything so I don't see how that would work

buffer = zero(X)
for i in eachindex(w)
w[i] <= T(0) && continue
z = view(Z, :, i)
MA.mutable_operate_to!(buffer, *, z, z')
for j in eachindex(X)
#The following line should be : X[j] = MA.add_mul!(X[j], w[i], buffer[j])
# However it does not work for BigFLoats yet, so i keep the allocating working line :
X[j] += w[i]*buffer[j]
end
end
end

function rank_k_update!(X::AbstractMatrix, ws::COSMO.PsdBlasWorkspace{T}) where {T}
n = size(X, 1)
@. X = zero(T)
Expand Down Expand Up @@ -274,9 +291,11 @@ function project!(x::AbstractVector{T}, cone::Union{PsdCone{T}, DensePsdCone{T}}
symmetrize_upper!(X)
_project!(X, cone.work)

#fill in the lower triangular part
for j=1:n, i=1:(j-1)
X[j,i] = X[i,j]
#fill in the lower triangular part, only needed when calling BLAS.
if T <: Union{Float32,Float64}
for j=1:n, i=1:(j-1)
X[j,i] = X[i,j]
end
end
end
return nothing
Expand Down
2 changes: 0 additions & 2 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,6 @@ function type_checks(constraints::Vector{COSMO.Constraint{T}}) where {T <: Abstr
return nothing
end
type_checks(convex_set::AbstractConvexSet) = nothing
type_checks(convex_set::Union{PsdCone{BigFloat}, PsdConeTriangle{BigFloat}}) = throw(ArgumentError("COSMO currently does not support the combination of PSD constraints and BigFloat."))



function check_A_dim(A::Union{AbstractVector{<:Real},AbstractMatrix{<:Real}}, n::Int)
Expand Down