Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove restriction on number of threads #36778

Merged
merged 1 commit into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,16 +407,13 @@ void jl_init_threading(void)
#endif

// how many threads available, usable
int max_threads = jl_cpu_threads();
jl_n_threads = JULIA_NUM_THREADS;
if (jl_options.nthreads < 0) // --threads=auto
jl_n_threads = max_threads;
jl_n_threads = jl_cpu_threads();
else if (jl_options.nthreads > 0) // --threads=N
jl_n_threads = jl_options.nthreads;
else if ((cp = getenv(NUM_THREADS_NAME)))
jl_n_threads = (uint64_t)strtol(cp, NULL, 10);
if (jl_n_threads > max_threads)
jl_n_threads = max_threads;
if (jl_n_threads <= 0)
jl_n_threads = 1;
#ifndef __clang_analyzer__
Expand Down Expand Up @@ -451,6 +448,10 @@ void jl_start_threads(void)
// according to a 'compact' policy
// non-exclusive: no affinity settings; let the kernel move threads about
if (exclusive) {
if (jl_n_threads > jl_cpu_threads()) {
jl_n_threads = 1;
jl_error("Too many threads running for " MACHINE_EXCLUSIVE_NAME " option.");
}
memset(mask, 0, cpumasksize);
mask[0] = 1;
uvtid = (uv_thread_t)uv_thread_self();
Expand Down
26 changes: 14 additions & 12 deletions test/cmdlineargs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,29 +190,31 @@ let exename = `$(Base.julia_cmd()) --startup-file=no`

# -t, --threads
code = "print(Threads.nthreads())"
cpu_threads = ccall(:jl_cpu_threads, Int32, ())
@test string(cpu_threads) ==
cpu_threads = string(ccall(:jl_cpu_threads, Int32, ()))
@test cpu_threads ==
read(`$exename --threads auto -e $code`, String) ==
read(`$exename --threads=auto -e $code`, String) ==
read(`$exename -tauto -e $code`, String) ==
read(`$exename -t auto -e $code`, String) ==
read(`$exename -t $(cpu_threads+1) -e $code`, String)
if cpu_threads > 1
for nt in (nothing, "1"); withenv("JULIA_NUM_THREADS"=>nt) do
@test read(`$exename --threads 2 -e $code`, String) ==
read(`$exename --threads=2 -e $code`, String) ==
read(`$exename -t2 -e $code`, String) ==
read(`$exename -t auto -e $code`, String)
for nt in (nothing, "1")
withenv("JULIA_NUM_THREADS" => nt) do
@test read(`$exename --threads=2 -e $code`, String) ==
read(`$exename -t 2 -e $code`, String) == "2"
end end
end
end
cpu_threads *= "0"
@test read(`$exename -t $cpu_threads -e $code`, String) == cpu_threads
withenv("JULIA_NUM_THREADS" => cpu_threads) do
@test read(`$exename -e $code`, String) == cpu_threads
end
@test !success(`$exename -t 0`)
@test !success(`$exename -t -1`)

# Combining --threads and --procs: --threads does propagate
if cpu_threads > 1; withenv("JULIA_NUM_THREADS"=>nothing) do
withenv("JULIA_NUM_THREADS" => nothing) do
code = "print(sum(remotecall_fetch(Threads.nthreads, x) for x in procs()))"
@test read(`$exename -p2 -t2 -e $code`, String) == "6"
end end
end

# --procs
@test readchomp(`$exename -q -p 2 -e "println(nworkers())"`) == "2"
Expand Down