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

Make library relocatable #362

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
RelocatableFolders = "05181044-ff0b-4ac5-8273-598c1e38db00"
SolverCore = "ff4d7338-4cf1-434d-91df-b86cb86fb843"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9"
Expand All @@ -20,12 +21,12 @@ MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
MadNLPMOI = "MathOptInterface"

[compat]
LDLFactorizations = "0.10"
MINLPTests = "~0.5"
MadNLPTests = "0.5"
MathOptInterface = "1"
NLPModels = "~0.17.2, 0.18, 0.19, 0.20, 0.21"
SolverCore = "~0.3"
LDLFactorizations = "0.10"
julia = "1.9"

[extras]
Expand Down
7 changes: 4 additions & 3 deletions lib/MadNLPMumps/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ uuid = "3b83494e-c0a4-4895-918b-9157a7a085a1"
version = "0.4.1"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MUMPS_seq_jll = "d7ed1dd3-d0ae-5e8e-bfb4-87a502085b8d"
OpenBLAS32_jll = "656ef2d0-ae68-5445-9ca0-591084a874a2"
MadNLP = "2621e9c9-9eb4-46b1-8089-e8c72242dfb6"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
OpenBLAS32_jll = "656ef2d0-ae68-5445-9ca0-591084a874a2"
RelocatableFolders = "05181044-ff0b-4ac5-8273-598c1e38db00"

[compat]
MUMPS_seq_jll = "~5.3, ~500.600"
OpenBLAS32_jll = "0.3"
MadNLP = "0.5, 0.6, 0.7, 0.8"
MadNLPTests = "0.5"
OpenBLAS32_jll = "0.3"
julia = "1.6"

[extras]
Expand Down
40 changes: 21 additions & 19 deletions lib/MadNLPMumps/src/MadNLPMumps.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module MadNLPMumps

import MUMPS_seq_jll
using MUMPS_seq_jll
import RelocatableFolders: @path
import MadNLP:
MadNLP, parsefile, dlopen,
MadNLP, parsefile,
@kwdef, MadNLPLogger, @debug, @warn, @error,
SparseMatrixCSC, SubVector,
SymbolicException,FactorizationException,SolveException,InertiaException,
Expand All @@ -19,7 +20,8 @@ function __init__()
end
end

const version = parsefile(joinpath(dirname(pathof(MUMPS_seq_jll)),"..","Project.toml"))["version"]
const PROJECT_TOML = @path joinpath(dirname(pathof(MUMPS_seq_jll)),"..","Project.toml")
const version = parsefile(PROJECT_TOML)["version"]

setindex(tup,a,n) = (tup[1:n-1]...,a,tup[n+1:end]...)
tzeros(n) = tuple((0 for i=1:n)...)
Expand Down Expand Up @@ -151,22 +153,22 @@ mutable struct MumpsSolver{T} <: AbstractLinearSolver{T}
logger::MadNLPLogger
end

for (lib,fname,typ) in [(MUMPS_seq_jll.libdmumps,:dmumps_c,Float64), (MUMPS_seq_jll.libsmumps, :smumps_c,Float32)]
@eval begin
dmumps_c(mumps_struc::Struc{$typ})=ccall(
($(string(fname)),$lib),
Cvoid,
(Ref{Struc},),
mumps_struc)
# this is necessary, when multi-threaded calls are made with Mumps, not to clash with MPI
mumps_lock = Threads.SpinLock()

function locked_mumps_c(mumps_struc::Struc{Float32})
lock(mumps_lock)
try
@ccall libsmumps.smumps_c(mumps_struc::Ref{Struc{Float32}})::Cvoid
finally
unlock(mumps_lock)
end
end

# this is necessary, when multi-threaded calls are made with Mumps, not to clash with MPI
mumps_lock = Threads.SpinLock()
function locked_dmumps_c(mumps_struc::Struc)
function locked_mumps_c(mumps_struc::Struc{Float64})
lock(mumps_lock)
try
dmumps_c(mumps_struc)
@ccall libdmumps.dmumps_c(mumps_struc::Ref{Struc{Float64}})::Cvoid
finally
unlock(mumps_lock)
end
Expand All @@ -188,7 +190,7 @@ function MumpsSolver(csc::SparseMatrixCSC{T,Int32};
mumps_struc.job = -1
mumps_struc.comm_fortran = -987654 # MPI.COMM_WORLD.val

locked_dmumps_c(mumps_struc)
locked_mumps_c(mumps_struc)
mumps_struc.n = csc.n;
mumps_struc.nz= nnz(csc);
mumps_struc.a = pointer(csc.nzval)
Expand All @@ -215,7 +217,7 @@ function MumpsSolver(csc::SparseMatrixCSC{T,Int32};
a = copy(csc.nzval) # would there be a better way?
csc.nzval.=1

locked_dmumps_c(mumps_struc);
locked_mumps_c(mumps_struc);
mumps_struc.info[1] < 0 && throw(SymbolicException())

csc.nzval.=a
Expand All @@ -231,7 +233,7 @@ function factorize!(M::MumpsSolver)
M.mumps_struc.job = 2;
cnt = 0
while true
locked_dmumps_c(M.mumps_struc)
locked_mumps_c(M.mumps_struc)
if M.mumps_struc.info[1] in [-8,-9]
cnt >= 10 && throw(FactorizationException())
M.mumps_struc.icntl = setindex(M.mumps_struc.icntl,M.mumps_struc.icntl[14]*2.,14)
Expand All @@ -252,7 +254,7 @@ function solve!(M::MumpsSolver{T},rhs::Vector{T}) where T
M.is_singular && return rhs
M.mumps_struc.rhs = pointer(rhs)
M.mumps_struc.job = 3
locked_dmumps_c(M.mumps_struc)
locked_mumps_c(M.mumps_struc)
M.mumps_struc.info[1] < 0 && throw(SolveException())
return rhs
end
Expand All @@ -277,7 +279,7 @@ end

function finalize(M::MumpsSolver)
M.mumps_struc.job = -2
locked_dmumps_c(M.mumps_struc);
locked_mumps_c(M.mumps_struc);
end

introduce(::MumpsSolver)="mumps"
Expand Down
5 changes: 4 additions & 1 deletion src/MadNLP.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module MadNLP

import Pkg.TOML: parsefile
import RelocatableFolders: @path
import Printf: @sprintf
import LinearAlgebra: BLAS, Adjoint, Symmetric, mul!, ldiv!, norm, dot, diagind, normInf, transpose!, issuccess
import LinearAlgebra: cholesky, qr, lu, cholesky!, axpy!
Expand All @@ -15,7 +16,9 @@ export MadNLPSolver, MadNLPOptions, UmfpackSolver, LDLSolver, CHOLMODSolver, Lap
import LDLFactorizations

# Version info
version() = parsefile(joinpath(@__DIR__,"..","Project.toml"))["version"]
# version() = parsefile(joinpath(@__DIR__,"..","Project.toml"))["version"]
const PROJECT_TOML = @path joinpath(@__DIR__,"..","Project.toml")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does the following work with PackageCompiler.jl?

version() = packageversion(@__MODULE__)

Using that we don't have to parse the toml file and won't have to depend on RelocatableFolders.jl.

This requires Julia >= 1.9

Copy link
Author

Choose a reason for hiding this comment

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

I can try but I don't find this packageversion method is custom or part of some Package?

Copy link
Collaborator

Choose a reason for hiding this comment

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

My apologies, I meant pkgversion instead of packageversion. The function is part of Base.

version() = parsefile(PROJECT_TOML)["version"]
introduce() = "MadNLP version v$(version())"

include("enums.jl")
Expand Down
Loading