diff --git a/Project.toml b/Project.toml index 05e9745..bc60e48 100644 --- a/Project.toml +++ b/Project.toml @@ -8,6 +8,8 @@ Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" MKL_jll = "856f044c-d86e-5d09-b602-aeab76dc8ba7" PackageCompiler = "9b87118b-4619-50d2-8e1e-99f35a4d4d9d" +Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" +Preferences = "21216c6a-2e73-6563-6e65-726566657250" [compat] MKL_jll = "2021" diff --git a/deps/build.jl b/deps/build.jl index 8571a83..43e6bfa 100644 --- a/deps/build.jl +++ b/deps/build.jl @@ -1,15 +1,63 @@ -using MKL +# using MKL +using Preferences +using Pkg +using Libdl -if VERSION > MKL.JULIA_VER_NEEDED - exit() # Don't want to build the system image, since we will use LBT +const JULIA_VER_NEEDED = v"1.7.0-DEV.641" +is_lbt_available() = VERSION > JULIA_VER_NEEDED + +function find_uuid_in_project(name) + get(Pkg.Types.EnvCache().project.deps, name, nothing) end -using PackageCompiler -using MKL_jll +if is_lbt_available() + # Julia version >= 1.7: Use LBT + const MKL_uuid = find_uuid_in_project("MKL") + + # 1. update preferences based on env variables if necessary + if haskey(ENV, "JULIA_MKL_USE_JLL") + use_jll = lowercase(get(ENV, "JULIA_MKL_USE_JLL", "true")) + set_preferences!(MKL_uuid, "use_jll" => use_jll) + end + + if haskey(ENV, "JULIA_MKL_PATH") + mkl_path = lowercase(get(ENV, "JULIA_MKL_PATH", "true")) + set_preferences!(MKL_uuid, "mkl_path" => mkl_path) + end + + @show has_preference(MKL_uuid, "use_jll") + + # 2. set up libraries + use_jll = load_preference(MKL_uuid, "use_jll", "true") + mkl_path = load_preference(MKL_uuid, "mkl_path", "") -# if no environment variable ENV["USE_BLAS64"] is set install.jl -# tries to change USE_BLAS64 = false -const USEBLAS64 = parse(Bool,get(ENV, "USE_BLAS64","false")) + if parse(Bool, use_jll) + @info "MKL provider: MKL_jll (default)." + using MKL_jll # force download of artifacts + else + @info "MKL provider: System" + mkl_path != "" && (@info "Explicit MKL path set: ") + + @info "Checking availability of libmkl_core and libmkl_rt" + if find_library(["libmkl_core"], mkl_path) == "" + error("libmkl_core could not be found") + end + if find_library(["libmkl_rt"], mkl_path) == "" + error("libmkl_rt could not be found") + end + end +else + # Julia versions < 1.7: Build a custom system image + using PackageCompiler + using MKL_jll + + # if no environment variable ENV["USE_BLAS64"] is set install.jl + # tries to change USE_BLAS64 = false + const USEBLAS64 = parse(Bool,get(ENV, "USE_BLAS64","false")) + + include("../src/install.jl") + enable_mkl_startup() +end -include("../src/install.jl") -enable_mkl_startup() +# Why? +using MKL \ No newline at end of file diff --git a/src/MKL.jl b/src/MKL.jl index 21cb844..3384e7a 100644 --- a/src/MKL.jl +++ b/src/MKL.jl @@ -1,9 +1,38 @@ module MKL -using MKL_jll +using Preferences -JULIA_VER_NEEDED = v"1.7.0-DEV.641" -VERSION > JULIA_VER_NEEDED && using LinearAlgebra +const JULIA_VER_NEEDED = v"1.7.0-DEV.641" +is_lbt_available() = VERSION > JULIA_VER_NEEDED + +const use_jll = parse(Bool, @load_preference("use_jll", "true")) + +const MKL_uuid = Base.PkgId(MKL).uuid +@show use_jll +@show has_preference(MKL_uuid, "use_jll") +@show load_preference(MKL_uuid, "use_jll") + +if use_jll + # MKL_jll + using MKL_jll + MKL_jll.is_available() || error("MKL.jl not properly configured, please run `Pkg.build(\"MKL\")`.") +else + # System MKL + using Libdl + mkl_path = @load_preference("mkl_path", "") + @show mkl_path + const libmkl_core = find_library(["libmkl_core"], mkl_path == "" ? [""] : [mkl_path]) + @show libmkl_core + const libmkl_rt = find_library(["libmkl_rt"], mkl_path == "" ? [""] : [mkl_path]) + @show libmkl_rt + @show x = dlopen(libmkl_rt) + @show dlclose(x) + if libmkl_core == "" || libmkl_rt == "" + error("MKL.jl not properly configured, please run `Pkg.build(\"MKL\")`.") + end +end + +is_lbt_available() && using LinearAlgebra if Base.USE_BLAS64 const MKLBlasInt = Int64 @@ -38,11 +67,11 @@ function set_interface_layer(interface = Base.USE_BLAS64 ? INTERFACE_ILP64 : INT end function __init__() - if MKL_jll.is_available() - set_threading_layer() - set_interface_layer() - VERSION > JULIA_VER_NEEDED && BLAS.lbt_forward(libmkl_rt, clear=true) - end + # if MKL_jll.is_available() + # set_threading_layer() + # set_interface_layer() + # is_lbt_available() && BLAS.lbt_forward(libmkl_rt, clear=true) + # end end function mklnorm(x::Vector{Float64}) @@ -51,6 +80,7 @@ function mklnorm(x::Vector{Float64}) length(x), x, 1) end -VERSION > JULIA_VER_NEEDED && include("install.jl") +# Carsten: Unnecessary?! +is_lbt_available() && include("install.jl") end # module