Skip to content

Commit

Permalink
Add support for Julia BLAS and LAPACK
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Oct 23, 2019
1 parent 72ce37e commit 84de136
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 143 deletions.
15 changes: 10 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ matrix:
allow_failures:
- julia: nightly

addons:
apt: # apt-get for linux
packages:
- liblapack-dev
- libblas-dev
jobs:
include:
- addons:
apt: # apt-get for linux
packages:
- liblapack-dev
- libblas-dev
- addons:
apt: # apt-get for linux
packages:

notifications:
email: false
Expand Down
12 changes: 10 additions & 2 deletions deps/build.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ using LinearAlgebra, Libdl

@BinDeps.setup

blas = library_dependency("libblas", alias=["libblas.dll"])
lapack = library_dependency("liblapack", alias=["liblapack.dll"])

const JULIA_LAPACK = (BinDeps.issatisfied(blas) && BinDeps.issatisfied(lapack))
if JULIA_LAPACK
@info "Using blas and lapack libraries shipped with Julia."
else
@info "Using system blas and lapack libraries."
end

include("constants.jl")
include("compile.jl")

# @info "libname = $libname"
blas = library_dependency("libblas", alias=["libblas.dll"])
lapack = library_dependency("liblapack", alias=["liblapack.dll"])
depends = JULIA_LAPACK ? [] : [blas, lapack]

# LaPack/BLAS dependencies
Expand Down
4 changes: 2 additions & 2 deletions deps/clang.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ readchangewrite(header_naming("blockmat.h")) do b
#,(r"::Ptr", s"::Ref")
,(r"\ntype "m, "\nimmutable ")
]
b = replace(b, pat, subs)
b = replace(b, pat => subs)
end
b
end

readchangewrite(header_naming("declarations.h")) do d
replace(d, r"\nfunction \w+_\(\)\n(.+?)\nend\n"sm, "")
replace(d, r"\nfunction \w+_\(\)\n(.+?)\nend\n"sm => "")
end

7 changes: 4 additions & 3 deletions deps/compile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ function patch_int(; verbose::Bool = false)
(r"\(int\)", s"(integer)"),
(r"%d", s"%ld"),
(r"%2d", s"%2ld"),
(r" int ", s" integer ")]
content = replace(content, re, subst)
(r" int ", s" integer "),
(r"dsymv_", s"dsymv_64_")]
content = replace(content, re => subst)
end
open(cfile, "w") do io
print(io, content)
Expand All @@ -63,7 +64,7 @@ end
function compile_objs(JULIA_LAPACK=JULIA_LAPACK)
if JULIA_LAPACK
lapack = Libdl.dlpath(LinearAlgebra.LAPACK.liblapack)
lflag = replace(splitext(basename(lapack))[1], r"^lib", "")
lflag = replace(splitext(basename(lapack))[1], r"^lib" => "")
libs = ["-L$(dirname(lapack))", "-l$lflag"]
@info libs
if endswith(LinearAlgebra.LAPACK.liblapack, "64_")
Expand Down
10 changes: 4 additions & 6 deletions deps/constants.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# use Julia's libopenblas instead of system's liblapack and libblas?
const JULIA_LAPACK = false
const suffix = JULIA_LAPACK ? ".64" : ""
const version = "6.2.0"
const libname = "libcsdp$suffix.$(Libdl.dlext)"
const libname = "libcsdp.$(Libdl.dlext)"
const csdpversion = "Csdp-readprob"
const download_url =
"https://github.com/blegat/Csdp/archive/readprob.zip"

patchf = joinpath(dirname(@__FILE__), "src$suffix", "debug-mat.c")
srcdir = joinpath(dirname(@__FILE__), "src$suffix", csdpversion, "lib")
patchf = joinpath(dirname(@__FILE__), "src", "debug-mat.c")
srcdir = joinpath(dirname(@__FILE__), "src", csdpversion, "lib")
prefix = joinpath(dirname(@__FILE__), "usr")
builddir = joinpath(dirname(@__FILE__), "build$suffix")
builddir = joinpath(dirname(@__FILE__), "build")
cflags = ["-I$srcdir/../include", "-DNOSHORTS", "-g"]
libdir = joinpath(prefix, @static Sys.iswindows() ? "bin" : "lib/")
dlpath = joinpath(libdir, libname)
Expand Down
2 changes: 2 additions & 0 deletions src/CSDP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ end

export Blockmatrix

const CSDP_INT = Clong

include("blockdiag.jl")
include("blockmat.h.jl")
include("blockmat.jl")
Expand Down
10 changes: 5 additions & 5 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const AFFEQ = MOI.ConstraintIndex{MOI.ScalarAffineFunction{Cdouble}, MOI.EqualTo
mutable struct Optimizer <: MOI.AbstractOptimizer
objconstant::Cdouble
objsign::Int
blockdims::Vector{Cint}
blockdims::Vector{CSDP_INT}
varmap::Vector{Tuple{Int, Int, Int}} # Variable Index vi -> blk, i, j
num_entries::Dict{Tuple{Int, Int}, Int}
b::Vector{Cdouble}
Expand All @@ -15,15 +15,15 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
X::blockmatrix
y::Union{Nothing, Vector{Cdouble}}
Z::blockmatrix
status::Cint
status::CSDP_INT
pobj::Cdouble
dobj::Cdouble
solve_time::Float64
silent::Bool
options::Dict{Symbol, Any}
function Optimizer(; kwargs...)
optimizer = new(
zero(Cdouble), 1, Cint[], Tuple{Int, Int, Int}[],
zero(Cdouble), 1, CSDP_INT[], Tuple{Int, Int, Int}[],
Dict{Tuple{Int, Int}, Int}(), Cdouble[],
blockmatrix(), nothing, blockmatrix(), nothing, blockmatrix(),
-1, NaN, NaN, NaN, false, Dict{Symbol, Any}())
Expand Down Expand Up @@ -211,11 +211,11 @@ function MOIU.load_variables(optimizer::Optimizer, nvars)
if dummy
# See https://github.com/coin-or/Csdp/issues/2
optimizer.b = [one(Cdouble)]
optimizer.blockdims = [optimizer.blockdims; Cint(-1)]
optimizer.blockdims = [optimizer.blockdims; CSDP_INT(-1)]
count_entry(optimizer, 1, length(optimizer.blockdims))
end
optimizer.C.nblocks = length(optimizer.blockdims)
num_entries = zeros(Cint, length(optimizer.b), length(optimizer.blockdims))
num_entries = zeros(CSDP_INT, length(optimizer.b), length(optimizer.blockdims))
for (key, value) in optimizer.num_entries
num_entries[key...] = value
end
Expand Down
10 changes: 5 additions & 5 deletions src/MPB_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ end
CSDPSolver(; kwargs...) = CSDPSolver(checkoptions(Dict{Symbol,Any}(kwargs)))

mutable struct CSDPMathProgModel <: SDM.AbstractSDModel
blockdims::Vector{Cint}
blockdims::Vector{CSDP_INT}
b::Vector{Cdouble}
entries::Vector{Tuple{Cint,Cint,Cint,Cint,Cdouble}}
entries::Vector{Tuple{CSDP_INT,CSDP_INT,CSDP_INT,CSDP_INT,Cdouble}}
C::blockmatrix
problem::Union{Nothing, LoadingProblem}
X::blockmatrix
y::Union{Nothing, Vector{Cdouble}}
Z::blockmatrix
status::Cint
status::CSDP_INT
pobj::Cdouble
dobj::Cdouble
options::Dict{Symbol,Any}
function CSDPMathProgModel(; kwargs...)
new(Cint[], Cdouble[], Tuple{Cint,Cint,Cint,Cint,Cdouble}[],
new(CSDP_INT[], Cdouble[], Tuple{CSDP_INT,CSDP_INT,CSDP_INT,CSDP_INT,Cdouble}[],
blockmatrix(), nothing, blockmatrix(), nothing, blockmatrix(),
-1, 0.0, 0.0, checkoptions(Dict{Symbol, Any}(kwargs)))
end
Expand Down Expand Up @@ -93,7 +93,7 @@ function MPB.optimize!(m::CSDPMathProgModel)
if m.problem === nothing
# `m.problem` is not `nothing` if it was loaded from a file.
m.C.nblocks = length(m.blockdims)
num_entries = zeros(Cint, length(m.b), length(m.blockdims))
num_entries = zeros(CSDP_INT, length(m.b), length(m.blockdims))
for entry in m.entries
if entry[1] > 0
num_entries[entry[1], entry[2]] += 1
Expand Down
18 changes: 9 additions & 9 deletions src/blockmat.h.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@static if Sys.iswindows()
const csdpshort = Cushort
else
const csdpshort = Cint
const csdpshort = CSDP_INT
end

# begin enum blockcat
Expand All @@ -27,18 +27,18 @@ struct blockrec <: AbstractMatrix{Cdouble}
end

mutable struct blockmatrix <: AbstractBlockMatrix{Cdouble}
nblocks::Cint
nblocks::CSDP_INT
blocks::Ptr{blockrec}
end
blockmatrix() = blockmatrix(Cint(0), C_NULL)
blockmatrix() = blockmatrix(CSDP_INT(0), C_NULL)

mutable struct sparseblock
next::Ptr{sparseblock}
nextbyblock::Ptr{sparseblock}
entries::Ptr{Cdouble}
iindices::Ptr{csdpshort}
jindices::Ptr{csdpshort}
numentries::Cint
numentries::CSDP_INT
blocknum::csdpshort
blocksize::csdpshort
constraintnum::csdpshort
Expand Down Expand Up @@ -67,16 +67,16 @@ mutable struct paramstruc
objtol::Cdouble
pinftol::Cdouble
dinftol::Cdouble
maxiter::Cint
maxiter::CSDP_INT
minstepfrac::Cdouble
maxstepfrac::Cdouble
minstepp::Cdouble
minstepd::Cdouble
usexzgap::Cint
tweakgap::Cint
affine::Cint
usexzgap::CSDP_INT
tweakgap::CSDP_INT
affine::CSDP_INT
perturbobj::Cdouble
fastmode::Cint
fastmode::CSDP_INT
end

function paramstruc(options::Dict)
Expand Down
8 changes: 4 additions & 4 deletions src/blockmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ mutable struct SparseBlock <: AbstractMatrix{Cdouble}
i::Vector{csdpshort}
j::Vector{csdpshort}
v::Vector{Cdouble}
n::Cint
n::CSDP_INT
csdp::sparseblock
function SparseBlock(i::Vector{csdpshort}, j::Vector{csdpshort}, v::Vector{Cdouble}, n::Integer, csdp)
new(i, j, v, n, csdp)
Expand Down Expand Up @@ -217,7 +217,7 @@ function SparseBlock(A::SparseMatrixCSC{Cdouble})
end
SparseBlock(I, J, V, n)
end
SparseBlock(A::AbstractMatrix{Cdouble}) = SparseBlock(SparseMatrixCSC{Cdouble, Cint}(A))
SparseBlock(A::AbstractMatrix{Cdouble}) = SparseBlock(SparseMatrixCSC{Cdouble, CSDP_INT}(A))
SparseBlock(A::AbstractMatrix) = SparseBlock(map(Cdouble, A))
Base.convert(::Type{SparseBlock}, A::AbstractMatrix) = SparseBlock(A)

Expand Down Expand Up @@ -320,8 +320,8 @@ export BlockMatrix, ConstraintMatrix

"""Solver status"""
mutable struct Csdp
n::Cint
k::Cint
n::CSDP_INT
k::CSDP_INT
X::BlockMatrix
y::Vector{Cdouble}
constant_offset::Cdouble
Expand Down
2 changes: 1 addition & 1 deletion src/debug-mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function print_sizeof()
end

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


Expand Down

0 comments on commit 84de136

Please sign in to comment.