Skip to content

Commit

Permalink
Merge pull request #162 from chrisbrahms/threadedFFT
Browse files Browse the repository at this point in the history
FFTW wisdom for threads
  • Loading branch information
chrisbrahms authored May 1, 2020
2 parents 0a87315 + 6f275e2 commit ea4f8d1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
18 changes: 17 additions & 1 deletion src/Luna.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down
19 changes: 12 additions & 7 deletions src/Utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Logging
import LibGit2
import Pidfile: mkpidlock
import HDF5
import Luna: @hlock
import Luna: @hlock, settings

function git_commit()
try
Expand Down Expand Up @@ -60,26 +60,31 @@ function sourcecode()
return out
end

function FFTWthreads()
if Threads.nthreads() == 1
1
else
settings["fftw_threads"] == 0 ? 4*Threads.nthreads() : settings["fftw_threads"]
end
end

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)
Logging.@info("Found FFTW wisdom at $fpath")
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)
Expand Down

0 comments on commit ea4f8d1

Please sign in to comment.