From 2dd747a7482d2c16754842c7308bbeb8ce4e101f Mon Sep 17 00:00:00 2001 From: kalmarek Date: Mon, 6 Nov 2017 14:24:50 +0100 Subject: [PATCH] move cpu/multithreading selection to CPUselect.jl --- AutFN_orbit.jl | 24 +++--------------------- CPUselect.jl | 30 ++++++++++++++++++++++++++++++ SL_orbit.jl | 25 +++---------------------- 3 files changed, 36 insertions(+), 43 deletions(-) create mode 100644 CPUselect.jl diff --git a/AutFN_orbit.jl b/AutFN_orbit.jl index bf8575e..50fae9c 100644 --- a/AutFN_orbit.jl +++ b/AutFN_orbit.jl @@ -41,29 +41,11 @@ end parsed_args = parse_commandline() -function cpuinfo_physicalcores() - maxcore = -1 - for line in eachline("/proc/cpuinfo") - if startswith(line, "core id") - maxcore = max(maxcore, parse(Int, split(line, ':')[2])) - end - end - maxcore < 0 && error("failure to read core ids from /proc/cpuinfo") - return maxcore + 1 -end +include("CPUselect.jl") -if parsed_args["cpus"] == nothing - N = cpuinfo_physicalcores() - parsed_args["cpus"] = N - info("Setting --cpus to $N") -elseif parsed_args["cpus"] > cpuinfo_physicalcores() - warn("Number of specified cores exceeds the physical core count. Performance may suffer.") -end - -addprocs(parsed_args["cpus"]) -BLAS.set_num_threads(parsed_args["cpus"]) +set_parallel_mthread(parsed_args) include("SAutFNs.jl") - include("Orbit.jl") + main(SAutFNs, parsed_args) diff --git a/CPUselect.jl b/CPUselect.jl new file mode 100644 index 0000000..0bb350a --- /dev/null +++ b/CPUselect.jl @@ -0,0 +1,30 @@ +function cpuinfo_physicalcores() + maxcore = -1 + for line in eachline("/proc/cpuinfo") + if startswith(line, "core id") + maxcore = max(maxcore, parse(Int, split(line, ':')[2])) + end + end + maxcore < 0 && error("failure to read core ids from /proc/cpuinfo") + return maxcore + 1 +end + +function set_parallel_mthread(parsed_args) + + if parsed_args["cpus"] == nothing + N = cpuinfo_physicalcores() + else + N = parsed_args["cpus"] + if N > cpuinfo_physicalcores() + warn("Number of specified cores exceeds the physical core count. Performance may suffer.") + end + end + + info("Using $N cpus in @parallel code.") + info("Using $(Threads.nthreads()) in @threads code.") + + addprocs(N) + BLAS.set_num_threads(N) + + return N +end diff --git a/SL_orbit.jl b/SL_orbit.jl index 2f80de1..4163526 100644 --- a/SL_orbit.jl +++ b/SL_orbit.jl @@ -48,30 +48,11 @@ end parsed_args = parse_commandline() -function cpuinfo_physicalcores() - maxcore = -1 - for line in eachline("/proc/cpuinfo") - if startswith(line, "core id") - maxcore = max(maxcore, parse(Int, split(line, ':')[2])) - end - end - maxcore < 0 && error("failure to read core ids from /proc/cpuinfo") - return maxcore + 1 -end +include("CPUselect.jl") -if parsed_args["cpus"] == nothing - N = cpuinfo_physicalcores() - parsed_args["cpus"] = N - info("Setting --cpus to $N") -elseif parsed_args["cpus"] > cpuinfo_physicalcores() - warn("Number of specified cores exceeds the physical core count. Performance will suffer.") - end -end - -addprocs(parsed_args["cpus"]) -BLAS.set_num_threads(parsed_args["cpus"]) +set_parallel_mthread(parsed_args) include("SLNs.jl") - include("Orbit.jl") + main(SLNs, parsed_args)