Skip to content

Commit

Permalink
Merge ef69a15 into 8c39f66
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Aug 10, 2018
2 parents 8c39f66 + ef69a15 commit 5e794f1
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 138 deletions.
1 change: 1 addition & 0 deletions deps/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ src
usr
build.64/
src.64/
build.log
3 changes: 2 additions & 1 deletion examples/example.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CSDP
using Compat

#=
# Example copied from `example/example.c`
Expand Down Expand Up @@ -79,6 +80,6 @@ X, y, Z = initsoln(C, b, constraints)

pobj, dobj = easy_sdp(C, b, constraints, X, y, Z)

@static if !is_windows()
@static if !Compat.Sys.iswindows()
CSDP.write_sol("prob.sol", X, y, Z)
end
4 changes: 4 additions & 0 deletions src/CSDP.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module CSDP

using Compat
using Compat.LinearAlgebra # For Diagonal
using Compat.SparseArrays # For SparseMatrixCSC

# Try to load the binary dependency
if isfile(joinpath(dirname(@__FILE__),"..","deps","deps.jl"))
include("../deps/deps.jl")
Expand Down
44 changes: 23 additions & 21 deletions src/MPBWrapper.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
importall MathProgBase.SolverInterface
importall SemidefiniteModels
import MathProgBase
const MPB = MathProgBase.SolverInterface
import SemidefiniteModels
const SDM = SemidefiniteModels

export CSDPMathProgModel, CSDPSolver

struct CSDPSolver <: AbstractMathProgSolver
struct CSDPSolver <: MPB.AbstractMathProgSolver
options::Dict{Symbol,Any}
end
CSDPSolver(;kwargs...) = CSDPSolver(checkoptions(Dict{Symbol,Any}(kwargs)))
CSDPSolver(; kwargs...) = CSDPSolver(checkoptions(Dict{Symbol,Any}(kwargs)))

mutable struct CSDPMathProgModel <: AbstractSDModel
mutable struct CSDPMathProgModel <: SDM.AbstractSDModel
C
b
As
Expand All @@ -24,18 +26,18 @@ mutable struct CSDPMathProgModel <: AbstractSDModel
-1, 0.0, 0.0, checkoptions(Dict{Symbol, Any}(kwargs)))
end
end
SDModel(s::CSDPSolver) = CSDPMathProgModel(; s.options...)
ConicModel(s::CSDPSolver) = SDtoConicBridge(SDModel(s))
LinearQuadraticModel(s::CSDPSolver) = ConicToLPQPBridge(ConicModel(s))
SDM.SDModel(s::CSDPSolver) = CSDPMathProgModel(; s.options...)
MPB.ConicModel(s::CSDPSolver) = SDM.SDtoConicBridge(SDM.SDModel(s))
MPB.LinearQuadraticModel(s::CSDPSolver) = MPB.ConicToLPQPBridge(MPB.ConicModel(s))

supportedcones(s::CSDPSolver) = [:Free,:Zero,:NonNeg,:NonPos,:SOC,:RSOC,:SDP]
function setvartype!(m::CSDPMathProgModel, vtype, blk, i, j)
MPB.supportedcones(s::CSDPSolver) = [:Free,:Zero,:NonNeg,:NonPos,:SOC,:RSOC,:SDP]
function MPB.setvartype!(m::CSDPMathProgModel, vtype, blk, i, j)
if vtype != :Cont
error("Unsupported variable type $vtype by CSDP")
end
end

function loadproblem!(m::CSDPMathProgModel, filename::AbstractString)
function MPB.loadproblem!(m::CSDPMathProgModel, filename::AbstractString)
if endswith(filename,".dat-s")
m.C, m.b, As = read_prob(filename)
m.As = [ConstraintMatrix(As[i], i) for i in 1:length(As)]
Expand All @@ -44,23 +46,23 @@ function loadproblem!(m::CSDPMathProgModel, filename::AbstractString)
end
end
#writeproblem(m, filename::String)
function loadproblem!(m::CSDPMathProgModel, blkdims::Vector{Int}, constr::Int)
function MPB.loadproblem!(m::CSDPMathProgModel, blkdims::Vector{Int}, constr::Int)
m.C = blockmatzeros(blkdims)
m.b = zeros(Cdouble, constr)
m.As = [constrmatzeros(i, blkdims) for i in 1:constr]
end

function setconstrB!(m::CSDPMathProgModel, val, constr::Integer)
function SDM.setconstrB!(m::CSDPMathProgModel, val, constr::Integer)
m.b[constr] = val
end
function setconstrentry!(m::CSDPMathProgModel, coef, constr::Integer, blk::Integer, i::Integer, j::Integer)
function SDM.setconstrentry!(m::CSDPMathProgModel, coef, constr::Integer, blk::Integer, i::Integer, j::Integer)
SDOI.block(m.As[constr], blk)[i, j] = coef
end
function setobjentry!(m::CSDPMathProgModel, coef, blk::Integer, i::Integer, j::Integer)
function SDM.setobjentry!(m::CSDPMathProgModel, coef, blk::Integer, i::Integer, j::Integer)
SDOI.block(m.C, blk)[i, j] = coef
end

function optimize!(m::CSDPMathProgModel)
function MPB.optimize!(m::CSDPMathProgModel)
As = map(A->A.csdp, m.As)

write_prob(m)
Expand All @@ -71,7 +73,7 @@ function optimize!(m::CSDPMathProgModel)
m.status, m.pobj, m.dobj = sdp(m.C, m.b, m.As, m.X, m.y, m.Z, m.options)
end

function status(m::CSDPMathProgModel)
function MPB.status(m::CSDPMathProgModel)
status = m.status
if status == 0
return :Optimal
Expand All @@ -92,15 +94,15 @@ function status(m::CSDPMathProgModel)
end
end

function getobjval(m::CSDPMathProgModel)
function MPB.getobjval(m::CSDPMathProgModel)
m.pobj
end
function getsolution(m::CSDPMathProgModel)
function MPB.getsolution(m::CSDPMathProgModel)
m.X
end
function getdual(m::CSDPMathProgModel)
function MPB.getdual(m::CSDPMathProgModel)
m.y
end
function getvardual(m::CSDPMathProgModel)
function MPB.getvardual(m::CSDPMathProgModel)
m.Z
end
2 changes: 1 addition & 1 deletion src/blockmat.h.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# TODO detect size and use bitstype because if the DLL changes and gets
# compiled with NOSHORTS we are screwed with the following code...
@static if is_windows()
@static if Compat.Sys.iswindows()
const csdpshort = Cushort
else
const csdpshort = Cint
Expand Down
32 changes: 18 additions & 14 deletions src/blockmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ end
export fptr, ptr

function mywrap(X::blockmatrix)
finalizer(X, free_blockmatrix)
@compat finalizer(free_blockmatrix, X)
BlockMatrix(X)
end

function mywrap(x::Ptr{T}, len) where T
# I give false to unsafe_wrap to specify that Julia do not own the array so it should not free it
# because the pointer it has has an offset
y = unsafe_wrap(Array, x + sizeof(T), len, false)
y = Base.unsafe_wrap(Array, x + sizeof(T), len, own=false)
# fptr takes care of this offset
finalizer(y, s -> Libc.free(fptr(s)))
@compat finalizer(s -> Libc.free(fptr(s)), y)
y
end

Expand Down Expand Up @@ -63,7 +63,9 @@ function BlockRec(a::Vector{Cdouble}, n::Int)
end
end
BlockRec(a::Vector, n::Int) = BlockRec(Vector{Cdouble}(a), n)
BlockRec(A::Matrix) = BlockRec(reshape(A, length(A)), Base.LinAlg.checksquare(A))
function BlockRec(A::Matrix)
return BlockRec(reshape(A, length(A)), Compat.LinearAlgebra.checksquare(A))
end
function BlockRec(A::Diagonal)
a = Vector{Cdouble}(diag(A))
BlockRec(a, -length(a))
Expand Down Expand Up @@ -129,13 +131,13 @@ blockmatzeros(blkdims) = BlockMatrix(map(blockreczeros, blkdims))

function BlockMatrix(csdp::blockmatrix)
# I give false so that Julia does not try to free it
blocks = unsafe_wrap(Array, csdp.blocks + sizeof(blockrec), csdp.nblocks, false)
blocks = Base.unsafe_wrap(Array, csdp.blocks + sizeof(blockrec), csdp.nblocks, own=false)
jblocks = map(blocks) do csdp
let n = csdp.blocksize, c = csdp.blockcategory, d = csdp.data._blockdatarec
if c == MATRIX
_blockdatarec = unsafe_wrap(Array, d, n^2, false)
_blockdatarec = Base.unsafe_wrap(Array, d, n^2, own=false)
elseif c == DIAG
_blockdatarec = unsafe_wrap(Array, d + sizeof(Cdouble), n, false)
_blockdatarec = Base.unsafe_wrap(Array, d + sizeof(Cdouble), n, own=false)
else
error("Unknown block category $(c)")
end
Expand Down Expand Up @@ -193,9 +195,9 @@ function SparseBlock(i::Vector{csdpshort}, j::Vector{csdpshort}, v::Vector{Cdoub
SparseBlock(i, j, v, n, block)
end

Base.convert(::Type{SparseBlock}, A::SparseBlock) = A
function Base.convert(::Type{SparseBlock}, A::SparseMatrixCSC{Cdouble})
n = Base.LinAlg.checksquare(A)
SparseBlock(A::SparseBlock) = A
function SparseBlock(A::SparseMatrixCSC{Cdouble})
n = Compat.LinearAlgebra.checksquare(A)
nn = nnz(A)
I = csdpshort[]
J = csdpshort[]
Expand All @@ -214,8 +216,10 @@ function Base.convert(::Type{SparseBlock}, A::SparseMatrixCSC{Cdouble})
end
SparseBlock(I, J, V, n)
end
Base.convert(::Type{SparseBlock}, A::AbstractMatrix{Cdouble}) = SparseBlock(SparseMatrixCSC{Cdouble, Cint}(A))
Base.convert(::Type{SparseBlock}, A::AbstractMatrix) = SparseBlock(map(Cdouble, A))
SparseBlock(A::AbstractMatrix{Cdouble}) = SparseBlock(SparseMatrixCSC{Cdouble, Cint}(A))
SparseBlock(A::AbstractMatrix) = SparseBlock(map(Cdouble, A))
Base.convert(::Type{SparseBlock}, A::AbstractMatrix) = SparseBlock(A)

function sparseblockzeros(n)
SparseBlock(csdpshort[], csdpshort[], Cdouble[], abs(n))
end
Expand Down Expand Up @@ -288,7 +292,7 @@ constrmatzeros(i, blkdims) = ConstraintMatrix(i, map(sparseblockzeros, blkdims))
# but this is kind of annoying since I want to modify its entries in setindex!(::SparseBlock, ...)
function ConstraintMatrix(csdp::constraintmatrix, k::Integer)
# I take care to free the blocks array when necessary in mywrap since CSDP won't take care of that (see the code of read_prob)
blocks = unsafe_wrap(Array, csdp.blocks, k, true) # FIXME this is a linked list, not an array...
blocks = Base.unsafe_wrap(Array, csdp.blocks, k, own=true) # FIXME this is a linked list, not an array...
jblocks = map(blocks) do csdp
ne = csdp.numentries
i = mywrap(csdp.iindices, ne)
Expand All @@ -310,7 +314,7 @@ end
SDOI.nblocks(A::Union{BlockMatrix, ConstraintMatrix}) = length(A.jblocks)

function free_blockmatrix(m::blockmatrix)
ccall((:free_mat, CSDP.csdp), Void, (blockmatrix,), m)
ccall((:free_mat, CSDP.csdp), Nothing, (blockmatrix,), m)
end
export free_blockmatrix

Expand Down
12 changes: 5 additions & 7 deletions src/debug-mat.jl
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
function print_block(b::blockrec)
ccall((:printb, CSDP.csdp), Void, (blockrec,), b)
ccall((:printb, CSDP.csdp), Nothing, (blockrec,), b)
end

function printm(A::blockmatrix)
ccall((:printm, CSDP.csdp), Void, (blockmatrix,), A)
ccall((:printm, CSDP.csdp), Nothing, (blockmatrix,), A)
end
printm(A::BlockMatrix) = printm(A.csdp)
export printm

print_sparseblock(A::sparseblock) = print_sparseblock(pointer_from_objref(A))
function print_sparseblock(a::Ptr{sparseblock})
ccall((:print_sparse_block, CSDP.csdp), Void, (Ptr{sparseblock},), a)
ccall((:print_sparse_block, CSDP.csdp), Nothing, (Ptr{sparseblock},), a)
end


function print_sizeof()
ccall((:print_sizeof, CSDP.csdp), Void, ())
ccall((:print_sizeof, CSDP.csdp), Nothing, ())
end

function print_constraints(C::Vector{constraintmatrix})
ccall((:print_constraints, CSDP.csdp), Void, (Cint, Ptr{constraintmatrix}), length(C), fptr(C))
ccall((:print_constraints, CSDP.csdp), Nothing, (Cint, Ptr{constraintmatrix}), length(C), fptr(C))
end


using Base.show

function Base.show(io::IO, b::sparseblock)
println(io, "\nsparseblock(", pointer_from_objref(b))
println(io, " next : ", b.next )
Expand Down

0 comments on commit 5e794f1

Please sign in to comment.