Skip to content
Closed
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
9 changes: 7 additions & 2 deletions src/mkl.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module MKL

using LinearAlgebra: LinearAlgebra, BLAS
using Libdl

const MKL_PATH = Ref{Union{Nothing, String}}(nothing)

Expand Down Expand Up @@ -41,15 +42,19 @@ end
mkl_get_dynamic()
Wrapper around the MKL function [`mkl_get_dynamic`](https://www.intel.com/content/www/us/en/develop/documentation/onemkl-developer-reference-fortran/top/support-functions/threading-control/mkl-get-dynamic.html).
"""
mkl_get_dynamic() = @ccall mkl_fullpath().mkl_get_dynamic()::Cint
function mkl_get_dynamic()
sym = Libdl.dlsym(Libdl.dlopen(mkl_fullpath()), :mkl_get_dynamic)
ccall(sym, Cint, ())
Comment on lines +45 to +47

Copilot AI Mar 26, 2026

Copy link

Choose a reason for hiding this comment

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

Libdl.dlopen(mkl_fullpath()) is executed on every call, and the resulting handle is never explicitly closed or cached. This can add overhead and may temporarily increase the dlopen refcount until GC runs. Consider caching the handle (e.g., a const Ref initialized once) or using the Libdl.dlopen(path) do lib ... end form to ensure timely dlclose.

Copilot uses AI. Check for mistakes.
end

"""
mkl_set_dynamic(flag::Integer)

Wrapper around the MKL function [`mkl_set_dynamic`](https://www.intel.com/content/www/us/en/develop/documentation/onemkl-developer-reference-c/top/support-functions/threading-control/mkl-set-dynamic.html).
"""
function mkl_set_dynamic(flag::Integer)
@ccall mkl_fullpath().MKL_Set_Dynamic(flag::Cint)::Cvoid
sym = Libdl.dlsym(Libdl.dlopen(mkl_fullpath()), :MKL_Set_Dynamic)
ccall(sym, Cvoid, (Cint,), flag)
Comment on lines 55 to +57

Copilot AI Mar 26, 2026

Copy link

Choose a reason for hiding this comment

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

This opens the MKL shared library and resolves the symbol on every call. To avoid repeated dlopen/dlsym work (and accumulating handles until GC), cache the dlopen handle/symbol in module-level const refs, or wrap the call in a Libdl.dlopen(...) do lib ... end block so the handle is closed immediately.

Copilot uses AI. Check for mistakes.
end

end # module
Loading