Created
September 24, 2018 06:56
-
-
Save jlapeyre/da5c103f26edeacc69a7bee376cca4f5 to your computer and use it in GitHub Desktop.
Allow setting number of threads in BLAS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const BLASVENDOR = Compat.LinearAlgebra.BLAS.vendor() | |
@eval Compat.LinearAlgebra.BLAS function get_num_threads end | |
# Following fails in v"0.7.0-DEV.4810". But, this appears to | |
# be a bug. | |
""" | |
BLAS.get_num_threads() | |
Get the number of threads that the BLAS library currently uses. | |
See [`set_num_threads`](@ref) and [`my_set_num_threads`](@ref). | |
This errors out for v"0.7.0-DEV.4810". This appears to be | |
a bug in Julia. | |
""" | |
function Compat.LinearAlgebra.BLAS.get_num_threads() | |
blas = BLASVENDOR | |
if blas == :openblas | |
return ccall((:openblas_get_num_threads, Base.libblas_name), Cint, ()) | |
elseif blas == :openblas64 | |
return ccall((:openblas_get_num_threads64_, Base.libblas_name), Int32, ()) | |
else | |
error("get_num_threads() not supported for BLAS vendor ", vendor) | |
end | |
end | |
""" | |
blas_set_num_threads(n) | |
Set the number of threads the BLAS library should use. In some cases, this is 5000 times faster | |
than `Compat.LinearAlgebra.BLAS.set_num_threads`. | |
""" | |
function blas_set_num_threads(n::Integer) | |
blas = BLASVENDOR | |
if blas == :openblas | |
return ccall((:openblas_set_num_threads, Base.libblas_name), Cvoid, (Int32,), n) | |
elseif blas == :openblas64 | |
return ccall((:openblas_set_num_threads64_, Base.libblas_name), Cvoid, (Int32,), n) | |
elseif blas == :mkl | |
# MKL may let us set the number of threads in several ways | |
return ccall((:MKL_Set_Num_Threads, Base.libblas_name), Cvoid, (Cint,), n) | |
end | |
# OSX BLAS looks at an environment variable | |
@static if Compat.Sys.isapple() | |
ENV["VECLIB_MAXIMUM_THREADS"] = n | |
end | |
return nothing | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment