From 9050651b24cbb47ada704b177a8a587c7186bcbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Hoffimann?= Date: Thu, 12 Jul 2018 15:29:55 -0400 Subject: [PATCH 1/3] Rename Sys.CPU_CORES => Sys.CPU_THREADS --- .circleci/config.yml | 4 ++-- .freebsdci.sh | 2 +- .travis.yml | 4 ++-- Makefile | 2 +- base/sysimg.jl | 6 +++--- base/sysinfo.jl | 16 ++++++++-------- base/util.jl | 6 +++--- doc/src/base/constants.md | 2 +- doc/src/manual/environment-variables.md | 4 ++-- stdlib/Distributed/src/managers.jl | 4 ++-- stdlib/Distributed/src/process_messages.jl | 2 +- stdlib/Distributed/test/distributed_exec.jl | 2 +- stdlib/InteractiveUtils/test/runtests.jl | 2 +- test/cmdlineargs.jl | 6 +++--- test/runtests.jl | 2 +- 15 files changed, 32 insertions(+), 32 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index b58da85f23d3c..b91b57d9487be 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -12,7 +12,7 @@ jobs: - image: circleci/python:2.7 environment: ARCH: i686 - JULIA_CPU_CORES: 4 + JULIA_CPU_THREADS: 4 JULIA_TEST_MAXRSS_MB: 800 steps: &steps - run: | # install build dependencies @@ -81,6 +81,6 @@ jobs: - image: circleci/python:2.7 environment: ARCH: x86_64 - JULIA_CPU_CORES: 4 + JULIA_CPU_THREADS: 4 JULIA_TEST_MAXRSS_MB: 800 steps: *steps diff --git a/.freebsdci.sh b/.freebsdci.sh index d55ca329e4802..436d883f80d3a 100755 --- a/.freebsdci.sh +++ b/.freebsdci.sh @@ -41,7 +41,7 @@ runtests(){ export FORCE_ASSERTIONS=1 export LLVM_ASSERTIONS=1 export JULIA_TEST_MAXRSS_MB=600 - export JULIA_CPU_CORES=$MAKE_JOBS_NUMBER + export JULIA_CPU_THREADS=$MAKE_JOBS_NUMBER ./usr/bin/julia --check-bounds=yes test/runtests.jl all ./usr/bin/julia --check-bounds=yes test/runtests.jl \ diff --git a/.travis.yml b/.travis.yml index b07b39eaa785a..bad194ae2321b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -64,7 +64,7 @@ before_install: BUILDOPTS="-j5 VERBOSE=1 FORCE_ASSERTIONS=1 LLVM_ASSERTIONS=1 USECCACHE=1"; echo "override ARCH=$ARCH" >> Make.user; sudo sh -c "echo 0 > /proc/sys/net/ipv6/conf/lo/disable_ipv6"; - export JULIA_CPU_CORES=4; + export JULIA_CPU_THREADS=4; export JULIA_TEST_MAXRSS_MB=1200; TESTSTORUN="all"; elif [ `uname` = "Darwin" ]; then @@ -90,7 +90,7 @@ before_install: export JULIA_MACOS_SPAWN="DYLD_FALLBACK_LIBRARY_PATH=\"$spawn_DYLD_FALLBACK_LIBRARY_PATH\" \$1"; export BUILDOPTS="$BUILDOPTS spawn=\$(JULIA_MACOS_SPAWN)"; make $BUILDOPTS -C contrib -f repackage_system_suitesparse4.make; - export JULIA_CPU_CORES=2; + export JULIA_CPU_THREADS=2; export JULIA_TEST_MAXRSS_MB=600; TESTSTORUN="all --skip linalg/triangular subarray"; fi # TODO: re enable these if possible without timing out - echo "override JULIA_CPU_TARGET=generic;native" >> Make.user diff --git a/Makefile b/Makefile index fcd82bee22ab3..c7ac7258e6120 100644 --- a/Makefile +++ b/Makefile @@ -581,7 +581,7 @@ testall: check-whitespace $(JULIA_BUILD_MODE) @$(MAKE) $(QUIET_MAKE) -C $(BUILDROOT)/test all JULIA_BUILD_MODE=$(JULIA_BUILD_MODE) testall1: check-whitespace $(JULIA_BUILD_MODE) - @env JULIA_CPU_CORES=1 $(MAKE) $(QUIET_MAKE) -C $(BUILDROOT)/test all JULIA_BUILD_MODE=$(JULIA_BUILD_MODE) + @env JULIA_CPU_THREADS=1 $(MAKE) $(QUIET_MAKE) -C $(BUILDROOT)/test all JULIA_BUILD_MODE=$(JULIA_BUILD_MODE) test-%: check-whitespace $(JULIA_BUILD_MODE) @$(MAKE) $(QUIET_MAKE) -C $(BUILDROOT)/test $* JULIA_BUILD_MODE=$(JULIA_BUILD_MODE) diff --git a/base/sysimg.jl b/base/sysimg.jl index 349c78dd0dffb..63d2fd4f3b2fc 100644 --- a/base/sysimg.jl +++ b/base/sysimg.jl @@ -457,12 +457,12 @@ function __init__() end # And try to prevent openblas from starting too many threads, unless/until specifically requested if !haskey(ENV, "OPENBLAS_NUM_THREADS") && !haskey(ENV, "OMP_NUM_THREADS") - cpu_cores = Sys.CPU_CORES::Int + cpu_cores = Sys.CPU_THREADS::Int if cpu_cores > 8 # always at most 8 ENV["OPENBLAS_NUM_THREADS"] = "8" - elseif haskey(ENV, "JULIA_CPU_CORES") # or exactly as specified + elseif haskey(ENV, "JULIA_CPU_THREADS") # or exactly as specified ENV["OPENBLAS_NUM_THREADS"] = cpu_cores - end # otherwise, trust that openblas will pick CPU_CORES anyways, without any intervention + end # otherwise, trust that openblas will pick CPU_THREADS anyways, without any intervention end # for the few uses of Libc.rand in Base: Libc.srand() diff --git a/base/sysinfo.jl b/base/sysinfo.jl index ab19d99998bc7..d96f13a7bf31e 100644 --- a/base/sysinfo.jl +++ b/base/sysinfo.jl @@ -7,7 +7,7 @@ Provide methods for retrieving information about hardware and the operating syst export BINDIR, STDLIB, - CPU_CORES, + CPU_THREADS, CPU_NAME, WORD_SIZE, ARCH, @@ -47,15 +47,15 @@ STDLIB = "$BINDIR/../share/julia/stdlib/v$(VERSION.major).$(VERSION.minor)" # fo # helper to avoid triggering precompile warnings -global CPU_CORES +global CPU_THREADS """ - Sys.CPU_CORES + Sys.CPU_THREADS The number of logical CPU cores available in the system. -See the Hwloc.jl package for extended information, including number of physical cores. +See Hwloc.jl or CpuId.jl for extended information, including number of physical cores. """ -:CPU_CORES +:CPU_THREADS """ Sys.ARCH @@ -87,11 +87,11 @@ Standard word size on the current machine, in bits. const WORD_SIZE = Core.sizeof(Int) * 8 function __init__() - env_cores = get(ENV, "JULIA_CPU_CORES", "") - global CPU_CORES = if !isempty(env_cores) + env_cores = get(ENV, "JULIA_CPU_THREADS", "") + global CPU_THREADS = if !isempty(env_cores) env_cores = tryparse(Int, env_cores) if !(env_cores isa Int && env_cores > 0) - Core.print(Core.stderr, "WARNING: couldn't parse `JULIA_CPU_CORES` environment variable. Defaulting Sys.CPU_CORES to 1.\n") + Core.print(Core.stderr, "WARNING: couldn't parse `JULIA_CPU_THREADS` environment variable. Defaulting Sys.CPU_THREADS to 1.\n") env_cores = 1 end env_cores diff --git a/base/util.jl b/base/util.jl index 2c31b32341d78..c457c004d93c2 100644 --- a/base/util.jl +++ b/base/util.jl @@ -721,7 +721,7 @@ kwdef_val(::Type{T}) where {T} = T() # testing """ - Base.runtests(tests=["all"]; ncores=ceil(Int, Sys.CPU_CORES / 2), + Base.runtests(tests=["all"]; ncores=ceil(Int, Sys.CPU_THREADS / 2), exit_on_error=false, [seed]) Run the Julia unit tests listed in `tests`, which can be either a string or an array of @@ -731,7 +731,7 @@ when `exit_on_error == true`. If a seed is provided via the keyword argument, it is used to seed the global RNG in the context where the tests are run; otherwise the seed is chosen randomly. """ -function runtests(tests = ["all"]; ncores = ceil(Int, Sys.CPU_CORES / 2), +function runtests(tests = ["all"]; ncores = ceil(Int, Sys.CPU_THREADS / 2), exit_on_error=false, seed::Union{BitInteger,Nothing}=nothing) if isa(tests,AbstractString) @@ -740,7 +740,7 @@ function runtests(tests = ["all"]; ncores = ceil(Int, Sys.CPU_CORES / 2), exit_on_error && push!(tests, "--exit-on-error") seed != nothing && push!(tests, "--seed=0x$(string(seed % UInt128, base=16))") # cast to UInt128 to avoid a minus sign ENV2 = copy(ENV) - ENV2["JULIA_CPU_CORES"] = "$ncores" + ENV2["JULIA_CPU_THREADS"] = "$ncores" try run(setenv(`$(julia_cmd()) $(joinpath(Sys.BINDIR::String, Base.DATAROOTDIR, "julia", "test", "runtests.jl")) $tests`, ENV2)) diff --git a/doc/src/base/constants.md b/doc/src/base/constants.md index b56659e77f302..0893c3aa8f022 100644 --- a/doc/src/base/constants.md +++ b/doc/src/base/constants.md @@ -8,7 +8,7 @@ Base.C_NULL Base.VERSION Base.LOAD_PATH Base.Sys.BINDIR -Base.Sys.CPU_CORES +Base.Sys.CPU_THREADS Base.Sys.WORD_SIZE Base.Sys.KERNEL Base.Sys.ARCH diff --git a/doc/src/manual/environment-variables.md b/doc/src/manual/environment-variables.md index 6d6286d932754..853dd77179531 100644 --- a/doc/src/manual/environment-variables.md +++ b/doc/src/manual/environment-variables.md @@ -128,9 +128,9 @@ exists, or `emacs` otherwise. ## Parallelization -### `JULIA_CPU_CORES` +### `JULIA_CPU_THREADS` -Overrides the global variable [`Base.Sys.CPU_CORES`](@ref), the number of +Overrides the global variable [`Base.Sys.CPU_THREADS`](@ref), the number of logical CPU cores available. ### `JULIA_WORKER_TIMEOUT` diff --git a/stdlib/Distributed/src/managers.jl b/stdlib/Distributed/src/managers.jl index 6f7cff928c277..cba0214458cb6 100644 --- a/stdlib/Distributed/src/managers.jl +++ b/stdlib/Distributed/src/managers.jl @@ -294,13 +294,13 @@ end """ addprocs(; kwargs...) -> List of process identifiers -Equivalent to `addprocs(Sys.CPU_CORES; kwargs...)` +Equivalent to `addprocs(Sys.CPU_THREADS; kwargs...)` Note that workers do not run a `.julia/config/startup.jl` startup script, nor do they synchronize their global state (such as global variables, new method definitions, and loaded modules) with any of the other running processes. """ -addprocs(; kwargs...) = addprocs(Sys.CPU_CORES; kwargs...) +addprocs(; kwargs...) = addprocs(Sys.CPU_THREADS; kwargs...) """ addprocs(np::Integer; restrict=true, kwargs...) -> List of process identifiers diff --git a/stdlib/Distributed/src/process_messages.jl b/stdlib/Distributed/src/process_messages.jl index 6c09aaa049417..38a09a4ff5150 100644 --- a/stdlib/Distributed/src/process_messages.jl +++ b/stdlib/Distributed/src/process_messages.jl @@ -330,7 +330,7 @@ function handle_msg(msg::JoinPGRPMsg, header, r_stream, w_stream, version) for wt in wait_tasks; Base._wait(wt); end send_connection_hdr(controller, false) - send_msg_now(controller, MsgHeader(RRID(0,0), header.notify_oid), JoinCompleteMsg(Sys.CPU_CORES, getpid())) + send_msg_now(controller, MsgHeader(RRID(0,0), header.notify_oid), JoinCompleteMsg(Sys.CPU_THREADS, getpid())) end function connect_to_peer(manager::ClusterManager, rpid::Int, wconfig::WorkerConfig) diff --git a/stdlib/Distributed/test/distributed_exec.jl b/stdlib/Distributed/test/distributed_exec.jl index ad3d5e51920d9..5e14b9b40ba8a 100644 --- a/stdlib/Distributed/test/distributed_exec.jl +++ b/stdlib/Distributed/test/distributed_exec.jl @@ -675,7 +675,7 @@ if Sys.isunix() # aka have ssh print("\nMixed ssh addprocs with :auto\n") new_pids = addprocs_with_testenv(["localhost", ("127.0.0.1", :auto), "localhost"]; sshflags=sshflags) - @test length(new_pids) == (2 + Sys.CPU_CORES) + @test length(new_pids) == (2 + Sys.CPU_THREADS) test_n_remove_pids(new_pids) print("\nMixed ssh addprocs with numeric counts\n") diff --git a/stdlib/InteractiveUtils/test/runtests.jl b/stdlib/InteractiveUtils/test/runtests.jl index 25657f8ea90e5..8c565516fe787 100644 --- a/stdlib/InteractiveUtils/test/runtests.jl +++ b/stdlib/InteractiveUtils/test/runtests.jl @@ -192,7 +192,7 @@ end @test !occursin("Environment:", read(setenv(`$exename -e 'using InteractiveUtils; versioninfo()'`, String[]), String)) @test occursin("Environment:", read(setenv(`$exename -e 'using InteractiveUtils; versioninfo()'`, - String["JULIA_CPU_CORES=1"]), String)) + String["JULIA_CPU_THREADS=1"]), String)) end end diff --git a/test/cmdlineargs.jl b/test/cmdlineargs.jl index b6779316eb382..230535cbf0dc9 100644 --- a/test/cmdlineargs.jl +++ b/test/cmdlineargs.jl @@ -51,14 +51,14 @@ let exename = `$(Base.julia_cmd()) --sysimage-native-code=yes --startup-file=no` @test startswith(v[3], "┌ Warning: Failed to insert InteractiveUtils into module Main\n") end for nc in ("0", "-2", "x", "2x", " ") - v = readchomperrors(setenv(`$exename -i -E 'Sys.CPU_CORES'`, "JULIA_CPU_CORES" => nc)) + v = readchomperrors(setenv(`$exename -i -E 'Sys.CPU_THREADS'`, "JULIA_CPU_THREADS" => nc)) @test v[1] @test v[2] == "1" - @test v[3] == "WARNING: couldn't parse `JULIA_CPU_CORES` environment variable. Defaulting Sys.CPU_CORES to 1." + @test v[3] == "WARNING: couldn't parse `JULIA_CPU_THREADS` environment variable. Defaulting Sys.CPU_THREADS to 1." end real_cores = string(ccall(:jl_cpu_cores, Int32, ())) for nc in ("1", " 1 ", " +1 ", " 0x1 ", "") - v = readchomperrors(setenv(`$exename -i -E 'Sys.CPU_CORES'`, "JULIA_CPU_CORES" => nc)) + v = readchomperrors(setenv(`$exename -i -E 'Sys.CPU_THREADS'`, "JULIA_CPU_THREADS" => nc)) @test v[1] @test v[2] == (isempty(nc) ? real_cores : "1") @test isempty(v[3]) diff --git a/test/runtests.jl b/test/runtests.jl index ae169e84ddcd9..85d2747671798 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -59,7 +59,7 @@ import LinearAlgebra cd(dirname(@__FILE__)) do n = 1 if net_on - n = min(Sys.CPU_CORES, length(tests)) + n = min(Sys.CPU_THREADS, length(tests)) n > 1 && addprocs_with_testenv(n) LinearAlgebra.BLAS.set_num_threads(1) end From c8bf20b9a88cee006e988809bdec67e8425d0205 Mon Sep 17 00:00:00 2001 From: Stefan Karpinski Date: Thu, 12 Jul 2018 19:13:23 -0400 Subject: [PATCH 2/3] cores => threads: more thorough replacement where appropriate --- README.windows.md | 2 +- base/sysimg.jl | 6 +++--- base/sysinfo.jl | 14 +++++++------- doc/src/manual/getting-started.md | 2 +- doc/src/manual/parallel-computing.md | 4 ++-- src/gc.c | 2 +- src/init.c | 2 +- src/jloptions.c | 4 ++-- src/julia.h | 2 +- src/sys.c | 4 ++-- src/threading.c | 2 +- stdlib/Distributed/src/cluster.jl | 2 +- stdlib/Distributed/src/managers.jl | 2 +- stdlib/Distributed/src/messages.jl | 2 +- stdlib/Distributed/src/process_messages.jl | 2 +- test/cmdlineargs.jl | 4 ++-- 16 files changed, 28 insertions(+), 28 deletions(-) diff --git a/README.windows.md b/README.windows.md index 6b5e9e476ccef..76056b69959ed 100644 --- a/README.windows.md +++ b/README.windows.md @@ -131,7 +131,7 @@ MinGW-w64 compilers available through Cygwin's package manager. 3. Start the build ```sh - make -j 4 # Adjust the number of cores (4) to match your build environment. + make -j 4 # Adjust the number of threads (4) to match your build environment. ``` diff --git a/base/sysimg.jl b/base/sysimg.jl index 63d2fd4f3b2fc..d380737fc6e82 100644 --- a/base/sysimg.jl +++ b/base/sysimg.jl @@ -457,11 +457,11 @@ function __init__() end # And try to prevent openblas from starting too many threads, unless/until specifically requested if !haskey(ENV, "OPENBLAS_NUM_THREADS") && !haskey(ENV, "OMP_NUM_THREADS") - cpu_cores = Sys.CPU_THREADS::Int - if cpu_cores > 8 # always at most 8 + cpu_threads = Sys.CPU_THREADS::Int + if cpu_threads > 8 # always at most 8 ENV["OPENBLAS_NUM_THREADS"] = "8" elseif haskey(ENV, "JULIA_CPU_THREADS") # or exactly as specified - ENV["OPENBLAS_NUM_THREADS"] = cpu_cores + ENV["OPENBLAS_NUM_THREADS"] = cpu_threads end # otherwise, trust that openblas will pick CPU_THREADS anyways, without any intervention end # for the few uses of Libc.rand in Base: diff --git a/base/sysinfo.jl b/base/sysinfo.jl index d96f13a7bf31e..3ad5e7dba8956 100644 --- a/base/sysinfo.jl +++ b/base/sysinfo.jl @@ -87,16 +87,16 @@ Standard word size on the current machine, in bits. const WORD_SIZE = Core.sizeof(Int) * 8 function __init__() - env_cores = get(ENV, "JULIA_CPU_THREADS", "") - global CPU_THREADS = if !isempty(env_cores) - env_cores = tryparse(Int, env_cores) - if !(env_cores isa Int && env_cores > 0) + env_threads = get(ENV, "JULIA_CPU_THREADS", "") + global CPU_THREADS = if !isempty(env_threads) + env_threads = tryparse(Int, env_threads) + if !(env_threads isa Int && env_threads > 0) Core.print(Core.stderr, "WARNING: couldn't parse `JULIA_CPU_THREADS` environment variable. Defaulting Sys.CPU_THREADS to 1.\n") - env_cores = 1 + env_threads = 1 end - env_cores + env_threads else - Int(ccall(:jl_cpu_cores, Int32, ())) + Int(ccall(:jl_cpu_threads, Int32, ())) end global SC_CLK_TCK = ccall(:jl_SC_CLK_TCK, Clong, ()) global CPU_NAME = ccall(:jl_get_cpu_name, Ref{String}, ()) diff --git a/doc/src/manual/getting-started.md b/doc/src/manual/getting-started.md index f3c48493edb9d..b41eab692e6e1 100644 --- a/doc/src/manual/getting-started.md +++ b/doc/src/manual/getting-started.md @@ -102,7 +102,7 @@ julia [switches] -- [programfile] [args...] |`-e`, `--eval ` |Evaluate ``| |`-E`, `--print ` |Evaluate `` and display the result| |`-L`, `--load ` |Load `` immediately on all processors| -|`-p`, `--procs {N\|auto`} |Integer value N launches N additional local worker processes; `auto` launches as many workers as the number of local cores| +|`-p`, `--procs {N\|auto`} |Integer value N launches N additional local worker processes; `auto` launches as many workers as the number of local CPU threads (logical cores)| |`--machine-file ` |Run processes on hosts listed in ``| |`-i` |Interactive mode; REPL runs and `isinteractive()` is true| |`-q`, `--quiet` |Quiet startup: no banner, suppress REPL warnings| diff --git a/doc/src/manual/parallel-computing.md b/doc/src/manual/parallel-computing.md index 7efd4a413d7d2..bc5b3fca81d39 100644 --- a/doc/src/manual/parallel-computing.md +++ b/doc/src/manual/parallel-computing.md @@ -49,7 +49,7 @@ to as "workers". When there is only one process, process 1 is considered a worke workers are considered to be all processes other than process 1. Let's try this out. Starting with `julia -p n` provides `n` worker processes on the local machine. -Generally it makes sense for `n` to equal the number of CPU cores on the machine. Note that the `-p` +Generally it makes sense for `n` to equal the number of CPU threads (logical cores) on the machine. Note that the `-p` argument implicitly loads module `Distributed`. @@ -1200,7 +1200,7 @@ would typically specify only `io` or `host` / `port`: workers. * `count` with an integer value `n` will launch a total of `n` workers. - * `count` with a value of `:auto` will launch as many workers as the number of cores on that machine. + * `count` with a value of `:auto` will launch as many workers as the number of CPU threads (logical cores) on that machine. * `exename` is the name of the `julia` executable including the full path. * `exeflags` should be set to the required command line arguments for new workers. * `tunnel`, `bind_addr`, `sshflags` and `max_parallel` are used when a ssh tunnel is required to diff --git a/src/gc.c b/src/gc.c index 6cfbc9027cfcd..e86d7433d112a 100644 --- a/src/gc.c +++ b/src/gc.c @@ -2718,7 +2718,7 @@ void jl_gc_init(void) #ifdef _P64 // on a big memory machine, set max_collect_interval to totalmem * nthreads / ncores / 2 - size_t maxmem = (uv_get_total_memory() * jl_n_threads) / jl_cpu_cores() / 2; + size_t maxmem = (uv_get_total_memory() * jl_n_threads) / jl_cpu_threads() / 2; if (maxmem > max_collect_interval) max_collect_interval = maxmem; #endif diff --git a/src/init.c b/src/init.c index 6a167b74b64bd..0764cc90af3e6 100644 --- a/src/init.c +++ b/src/init.c @@ -689,7 +689,7 @@ void _julia_init(JL_IMAGE_SEARCH rel) #if defined(__linux__) - int ncores = jl_cpu_cores(); + int ncores = jl_cpu_threads(); if (ncores > 1) { cpu_set_t cpumask; CPU_ZERO(&cpumask); diff --git a/src/jloptions.c b/src/jloptions.c index 7013973e57c08..147e0b8a28c7f 100644 --- a/src/jloptions.c +++ b/src/jloptions.c @@ -93,7 +93,7 @@ static const char opts[] = // parallel options " -p, --procs {N|auto} Integer value N launches N additional local worker processes\n" - " \"auto\" launches as many workers as the number of local cores\n" + " \"auto\" launches as many workers as the number of local CPU threads (logical cores)\n" " --machine-file Run processes on hosts listed in \n\n" // interactive options @@ -385,7 +385,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp) case 'p': // procs errno = 0; if (!strcmp(optarg,"auto")) { - jl_options.nprocs = jl_cpu_cores(); + jl_options.nprocs = jl_cpu_threads(); } else { long nprocs = strtol(optarg, &endptr, 10); diff --git a/src/julia.h b/src/julia.h index c3d13c3bf31de..6f3c88779b6e5 100644 --- a/src/julia.h +++ b/src/julia.h @@ -1358,7 +1358,7 @@ JL_DLLEXPORT jl_value_t *jl_eqtable_get(jl_array_t *h, void *key, JL_DLLEXPORT int jl_errno(void); JL_DLLEXPORT void jl_set_errno(int e); JL_DLLEXPORT int32_t jl_stat(const char *path, char *statbuf); -JL_DLLEXPORT int jl_cpu_cores(void); +JL_DLLEXPORT int jl_cpu_threads(void); JL_DLLEXPORT long jl_getpagesize(void); JL_DLLEXPORT long jl_getallocationgranularity(void); JL_DLLEXPORT int jl_is_debugbuild(void); diff --git a/src/sys.c b/src/sys.c index 42d9456598621..9333ac2a74d00 100644 --- a/src/sys.c +++ b/src/sys.c @@ -353,7 +353,7 @@ JL_DLLEXPORT uint64_t jl_ios_get_nbyte_int(ios_t *s, const size_t n) JL_DLLEXPORT int jl_errno(void) { return errno; } JL_DLLEXPORT void jl_set_errno(int e) { errno = e; } -// -- get the number of CPU cores -- +// -- get the number of CPU threads (logical cores) -- #ifdef _OS_WINDOWS_ typedef DWORD (WINAPI *GAPC)(WORD); @@ -362,7 +362,7 @@ typedef DWORD (WINAPI *GAPC)(WORD); #endif #endif -JL_DLLEXPORT int jl_cpu_cores(void) +JL_DLLEXPORT int jl_cpu_threads(void) { #if defined(HW_AVAILCPU) && defined(HW_NCPU) size_t len = 4; diff --git a/src/threading.c b/src/threading.c index 49ef363fcd078..1f142204b2f60 100644 --- a/src/threading.c +++ b/src/threading.c @@ -555,7 +555,7 @@ void jl_init_threading(void) #endif // how many threads available, usable - int max_threads = jl_cpu_cores(); + int max_threads = jl_cpu_threads(); jl_n_threads = JULIA_NUM_THREADS; cp = getenv(NUM_THREADS_NAME); if (cp) { diff --git a/stdlib/Distributed/src/cluster.jl b/stdlib/Distributed/src/cluster.jl index f76ab289cacad..082ec65496bcd 100644 --- a/stdlib/Distributed/src/cluster.jl +++ b/stdlib/Distributed/src/cluster.jl @@ -452,7 +452,7 @@ function setup_launched_worker(manager, wconfig, launched_q) # same type. This is done by setting an appropriate value to `WorkerConfig.cnt`. cnt = something(wconfig.count, 1) if cnt === :auto - cnt = wconfig.environ[:cpu_cores] + cnt = wconfig.environ[:cpu_threads] end cnt = cnt - 1 # Removing self from the requested number diff --git a/stdlib/Distributed/src/managers.jl b/stdlib/Distributed/src/managers.jl index cba0214458cb6..3bc838750ab71 100644 --- a/stdlib/Distributed/src/managers.jl +++ b/stdlib/Distributed/src/managers.jl @@ -64,7 +64,7 @@ specified, other workers will connect to this worker at the specified `bind_addr `port`. `count` is the number of workers to be launched on the specified host. If specified as -`:auto` it will launch as many workers as the number of cores on the specific host. +`:auto` it will launch as many workers as the number of CPU threads on the specific host. Keyword arguments: diff --git a/stdlib/Distributed/src/messages.jl b/stdlib/Distributed/src/messages.jl index 852624f91e21c..35b090f703868 100644 --- a/stdlib/Distributed/src/messages.jl +++ b/stdlib/Distributed/src/messages.jl @@ -71,7 +71,7 @@ struct JoinPGRPMsg <: AbstractMsg lazy::Bool end struct JoinCompleteMsg <: AbstractMsg - cpu_cores::Int + cpu_threads::Int ospid::Int end diff --git a/stdlib/Distributed/src/process_messages.jl b/stdlib/Distributed/src/process_messages.jl index 38a09a4ff5150..d95fed74a1b37 100644 --- a/stdlib/Distributed/src/process_messages.jl +++ b/stdlib/Distributed/src/process_messages.jl @@ -349,7 +349,7 @@ end function handle_msg(msg::JoinCompleteMsg, header, r_stream, w_stream, version) w = map_sock_wrkr[r_stream] environ = something(w.config.environ, Dict()) - environ[:cpu_cores] = msg.cpu_cores + environ[:cpu_threads] = msg.cpu_threads w.config.environ = environ w.config.ospid = msg.ospid w.version = version diff --git a/test/cmdlineargs.jl b/test/cmdlineargs.jl index 230535cbf0dc9..e31e176162e76 100644 --- a/test/cmdlineargs.jl +++ b/test/cmdlineargs.jl @@ -56,11 +56,11 @@ let exename = `$(Base.julia_cmd()) --sysimage-native-code=yes --startup-file=no` @test v[2] == "1" @test v[3] == "WARNING: couldn't parse `JULIA_CPU_THREADS` environment variable. Defaulting Sys.CPU_THREADS to 1." end - real_cores = string(ccall(:jl_cpu_cores, Int32, ())) + real_threads = string(ccall(:jl_cpu_threads, Int32, ())) for nc in ("1", " 1 ", " +1 ", " 0x1 ", "") v = readchomperrors(setenv(`$exename -i -E 'Sys.CPU_THREADS'`, "JULIA_CPU_THREADS" => nc)) @test v[1] - @test v[2] == (isempty(nc) ? real_cores : "1") + @test v[2] == (isempty(nc) ? real_threads : "1") @test isempty(v[3]) end end From dff903e328df96f50a3dd8ec33840c24bfd910ad Mon Sep 17 00:00:00 2001 From: Stefan Karpinski Date: Thu, 12 Jul 2018 19:18:03 -0400 Subject: [PATCH 3/3] Add deprecation for Sys.CPU_CORES => Sys.CPU_THREADS This also introduces minor functional changes to the behavior of JULIA_CPU_THREADS: when the environment variable is set but empty that is now considered to be a parse error and the default value will be used for `Sys.CPU_THREADS`; the default value when there is a parse error for JULIA_CPU_THREADS is now `jl_cpu_threads`. --- NEWS.md | 5 +++++ base/deprecated.jl | 4 ++++ base/sysimg.jl | 3 +++ base/sysinfo.jl | 23 ++++++++++++++++------- test/cmdlineargs.jl | 12 ++++++------ 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/NEWS.md b/NEWS.md index 7d0dde31f01fd..c6fe261d7d238 100644 --- a/NEWS.md +++ b/NEWS.md @@ -524,6 +524,11 @@ This section lists changes that do not have deprecation warnings. * `dot(u, v)` now acts recursively. Instead of `sum(u[i]' * v[i] for i in ...)`, it computes `sum(dot(u[i], v[i]) for i in ...)`, similarly to `vecdot` before ([#27401]). + * `Sys.CPU_CORES` has been renamed to `Sys.CPU_THREADS`; it still gives the number + of "logical cores" (including hyperthreading) rather than the number of physical + cores present on the CPU. Similarly, the environment variable `JULIA_CPU_CORES` is + deprecated in favor of `JULIA_CPU_THREADS` ([#27856]). + Library improvements -------------------- diff --git a/base/deprecated.jl b/base/deprecated.jl index 6aa591b069587..6b7011af22bd8 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1575,6 +1575,10 @@ end Base.@deprecate log Base.log end +# PR 27856 +@eval Base.Sys Base.@deprecate_binding CPU_CORES CPU_THREADS true nothing false +# TODO: delete deprecation code in sysimg.jl and sysinfo.jl + # PR 26071 @deprecate(matchall(r::Regex, s::AbstractString; overlap::Bool = false), collect(m.match for m in eachmatch(r, s, overlap = overlap))) diff --git a/base/sysimg.jl b/base/sysimg.jl index d380737fc6e82..e7035adb8794a 100644 --- a/base/sysimg.jl +++ b/base/sysimg.jl @@ -462,6 +462,9 @@ function __init__() ENV["OPENBLAS_NUM_THREADS"] = "8" elseif haskey(ENV, "JULIA_CPU_THREADS") # or exactly as specified ENV["OPENBLAS_NUM_THREADS"] = cpu_threads + elseif haskey(ENV, "JULIA_CPU_CORES") # TODO: delete in 1.0 (deprecation) + Core.print("JULIA_CPU_CORES is deprecated, use JULIA_CPU_THREADS instead.\n") + ENV["OPENBLAS_NUM_THREADS"] = cpu_threads end # otherwise, trust that openblas will pick CPU_THREADS anyways, without any intervention end # for the few uses of Libc.rand in Base: diff --git a/base/sysinfo.jl b/base/sysinfo.jl index 3ad5e7dba8956..df74d3cc78971 100644 --- a/base/sysinfo.jl +++ b/base/sysinfo.jl @@ -47,15 +47,17 @@ STDLIB = "$BINDIR/../share/julia/stdlib/v$(VERSION.major).$(VERSION.minor)" # fo # helper to avoid triggering precompile warnings -global CPU_THREADS """ Sys.CPU_THREADS -The number of logical CPU cores available in the system. +The number of logical CPU cores available in the system, i.e. the number of threads +that the CPU can run concurrently. Note that this is not necessarily the number of +CPU cores, for example, in the presence of +[hyper-threading](https://en.wikipedia.org/wiki/Hyper-threading). See Hwloc.jl or CpuId.jl for extended information, including number of physical cores. """ -:CPU_THREADS +CPU_THREADS = 1 # for bootstrap, changed on startup """ Sys.ARCH @@ -87,17 +89,24 @@ Standard word size on the current machine, in bits. const WORD_SIZE = Core.sizeof(Int) * 8 function __init__() - env_threads = get(ENV, "JULIA_CPU_THREADS", "") - global CPU_THREADS = if !isempty(env_threads) + env_threads = nothing + if haskey(ENV, "JULIA_CPU_THREADS") + env_threads = ENV["JULIA_CPU_THREADS"] + elseif haskey(ENV, "JULIA_CPU_CORES") # TODO: delete in 1.0 (deprecation) + Core.print("JULIA_CPU_CORES is deprecated, use JULIA_CPU_THREADS instead.\n") + env_threads = ENV["JULIA_CPU_CORES"] + end + global CPU_THREADS = if env_threads !== nothing env_threads = tryparse(Int, env_threads) if !(env_threads isa Int && env_threads > 0) - Core.print(Core.stderr, "WARNING: couldn't parse `JULIA_CPU_THREADS` environment variable. Defaulting Sys.CPU_THREADS to 1.\n") - env_threads = 1 + env_threads = Int(ccall(:jl_cpu_threads, Int32, ())) + Core.print(Core.stderr, "WARNING: couldn't parse `JULIA_CPU_THREADS` environment variable. Defaulting Sys.CPU_THREADS to $env_threads.\n") end env_threads else Int(ccall(:jl_cpu_threads, Int32, ())) end + global CPU_CORES = CPU_THREADS # TODO: delete in 1.0 (deprecation) global SC_CLK_TCK = ccall(:jl_SC_CLK_TCK, Clong, ()) global CPU_NAME = ccall(:jl_get_cpu_name, Ref{String}, ()) global JIT = ccall(:jl_get_JIT, Ref{String}, ()) diff --git a/test/cmdlineargs.jl b/test/cmdlineargs.jl index e31e176162e76..2c300ea7e6188 100644 --- a/test/cmdlineargs.jl +++ b/test/cmdlineargs.jl @@ -50,17 +50,17 @@ let exename = `$(Base.julia_cmd()) --sysimage-native-code=yes --startup-file=no` @test isempty(v[2]) @test startswith(v[3], "┌ Warning: Failed to insert InteractiveUtils into module Main\n") end - for nc in ("0", "-2", "x", "2x", " ") + real_threads = string(ccall(:jl_cpu_threads, Int32, ())) + for nc in ("0", "-2", "x", "2x", " ", "") v = readchomperrors(setenv(`$exename -i -E 'Sys.CPU_THREADS'`, "JULIA_CPU_THREADS" => nc)) @test v[1] - @test v[2] == "1" - @test v[3] == "WARNING: couldn't parse `JULIA_CPU_THREADS` environment variable. Defaulting Sys.CPU_THREADS to 1." + @test v[2] == real_threads + @test v[3] == "WARNING: couldn't parse `JULIA_CPU_THREADS` environment variable. Defaulting Sys.CPU_THREADS to $real_threads." end - real_threads = string(ccall(:jl_cpu_threads, Int32, ())) - for nc in ("1", " 1 ", " +1 ", " 0x1 ", "") + for nc in ("1", " 1 ", " +1 ", " 0x1 ") v = readchomperrors(setenv(`$exename -i -E 'Sys.CPU_THREADS'`, "JULIA_CPU_THREADS" => nc)) @test v[1] - @test v[2] == (isempty(nc) ? real_threads : "1") + @test v[2] == "1" @test isempty(v[3]) end end