From 11cca3a3dc0836aff80bc3e64a52509ec07e14fe Mon Sep 17 00:00:00 2001 From: chrisbrahms <38351086+chrisbrahms@users.noreply.github.com> Date: Tue, 28 Apr 2020 10:42:00 +0100 Subject: [PATCH 1/2] FFTW wisdom for threads --- src/Luna.jl | 18 +++++++++++++++++- src/Utils.jl | 13 ++++++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/Luna.jl b/src/Luna.jl index e1cafe10..18d194f3 100644 --- a/src/Luna.jl +++ b/src/Luna.jl @@ -36,7 +36,8 @@ end Dictionary of global settings for `Luna`. """ -settings = Dict{String, Any}("fftw_flag" => FFTW.PATIENT) +settings = Dict{String, Any}("fftw_flag" => FFTW.PATIENT, + "fftw_threads" => 0) """ set_fftw_mode(mode) @@ -58,6 +59,21 @@ function set_fftw_mode(mode) settings["fftw_flag"] = flag end +""" + set_fftw_threads(nthr) + +Set number of threads to be used by FFTW. If set to `0`, the number of threads used by +FFTW is determined automatically (see [`Utils.FFTWthreads()`](@ref)) +""" +function set_fftw_threads(nthr=0) + settings["fftw_threads"] = nthr + FFTW.set_num_threads(Utils.FFTWthreads()) +end + +function __init__() + set_fftw_threads() +end + include("Utils.jl") include("Scans.jl") include("Output.jl") diff --git a/src/Utils.jl b/src/Utils.jl index 374d3171..521f8325 100644 --- a/src/Utils.jl +++ b/src/Utils.jl @@ -5,7 +5,7 @@ import Logging import LibGit2 import Pidfile: mkpidlock import HDF5 -import Luna: @hlock +import Luna: @hlock, settings function git_commit() try @@ -60,8 +60,11 @@ function sourcecode() return out end +FFTWthreads() = settings["fftw_threads"] == 0 ? 4*Threads.nthreads() : settings["fftw_threads"] + function loadFFTwisdom() - fpath = joinpath(cachedir(), "FFTWcache") + FFTW.set_num_threads(FFTWthreads()) + fpath = joinpath(cachedir(), "FFTWcache_$(FFTWthreads())threads") lockpath = joinpath(cachedir(), "FFTWlock") isdir(cachedir()) || mkpath(cachedir()) if isfile(fpath) @@ -69,17 +72,13 @@ function loadFFTwisdom() pidlock = mkpidlock(lockpath) ret = FFTW.import_wisdom(fpath) close(pidlock) - success = (ret != 0) - Logging.@info(success ? "FFTW wisdom loaded" : "Loading FFTW wisdom failed") - return success else Logging.@info("No FFTW wisdom found") - return false end end function saveFFTwisdom() - fpath = joinpath(cachedir(), "FFTWcache") + fpath = joinpath(cachedir(), "FFTWcache_$(FFTWthreads())threads") lockpath = joinpath(cachedir(), "FFTWlock") pidlock = mkpidlock(lockpath) isfile(fpath) && rm(fpath) From 6f275e2470f18778b37c7232fc32ba83e163a677 Mon Sep 17 00:00:00 2001 From: chrisbrahms <38351086+chrisbrahms@users.noreply.github.com> Date: Fri, 1 May 2020 09:17:32 +0100 Subject: [PATCH 2/2] add nthreads() == 1 case --- src/Utils.jl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Utils.jl b/src/Utils.jl index 521f8325..dad9fc76 100644 --- a/src/Utils.jl +++ b/src/Utils.jl @@ -60,7 +60,13 @@ function sourcecode() return out end -FFTWthreads() = settings["fftw_threads"] == 0 ? 4*Threads.nthreads() : settings["fftw_threads"] +function FFTWthreads() + if Threads.nthreads() == 1 + 1 + else + settings["fftw_threads"] == 0 ? 4*Threads.nthreads() : settings["fftw_threads"] + end +end function loadFFTwisdom() FFTW.set_num_threads(FFTWthreads())