Skip to content

Commit 885f8e7

Browse files
gbaraldiIanButterworth
authored andcommitted
If the user explicitly asked for 1 thread don't add an interactive one. (#57454)
Co-authored-by: Ian Butterworth <[email protected]> (cherry picked from commit b04b104)
1 parent e931e83 commit 885f8e7

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

NEWS.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ Language changes
3636
* Julia now defaults to 1 "interactive" thread, in addition to the 1 default "worker" thread. i.e. `-t1,1`.
3737
This means in default configuration the main task and repl (when in interactive mode), which both run on
3838
thread 1, now run within the `interactive` threadpool. The libuv IO loop also runs on thread 1,
39-
helping efficient utilization of the worker threadpool used by `Threads.@spawn`. Pass `0` to disable the
40-
interactive thread i.e. `-t1,0` or `JULIA_NUM_THREADS=1,0`, or `-tauto,0` etc. The zero is explicitly
41-
required to disable it, `-t2` will set the equivalent of `-t2,1` ([#57087]).
39+
helping efficient utilization of the worker threadpool used by `Threads.@spawn`. Asking for specifically 1 thread
40+
(`-t1`/`JULIA_NUM_THREADS=1`) or passing `0` will disable the interactive thread i.e. `-t1,0` or `JULIA_NUM_THREADS=1,0`
41+
, or `-tauto,0` etc. Asking for more than 1 thread will enable the interactive thread so
42+
`-t2` will set the equivalent of `-t2,1` ([#57087]).
4243
* When a method is replaced with an exactly equivalent one, the old method is not deleted. Instead, the
4344
new method takes priority and becomes more specific than the old method. Thus if the new method is deleted
4445
later, the old method will resume operating. This can be useful in mocking frameworks (as in SparseArrays,

doc/src/manual/multi-threading.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ of Julia multi-threading features.
88
By default, Julia starts up with 2 threads of execution; 1 worker thread and 1 interactive thread.
99
This can be verified by using the command [`Threads.nthreads()`](@ref):
1010

11-
```jldoctest
11+
```julia
1212
julia> Threads.nthreads(:default)
1313
1
1414
julia> Threads.nthreads(:interactive)
@@ -37,6 +37,7 @@ each threadpool.
3737

3838
!!! compat "Julia 1.12"
3939
Starting by default with 1 interactive thread, as well as the 1 worker thread, was made as such in Julia 1.12
40+
If the number of threads is set to 1 by either doing `-t1` or `JULIA_NUM_THREADS=1` an interactive thread will not be spawned.
4041

4142
Lets start Julia with 4 threads:
4243

@@ -144,6 +145,8 @@ julia> nthreads(:interactive)
144145
julia> nthreads()
145146
3
146147
```
148+
!!! note
149+
Explicitly asking for 1 thread by doing `-t1` or `JULIA_NUM_THREADS=1` does not add an interactive thread.
147150

148151
!!! note
149152
The zero-argument version of `nthreads` returns the number of threads

src/jloptions.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,9 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
662662
if (nthreadsi == 0)
663663
jl_options.nthreadpools = 1;
664664
}
665+
} else if (nthreads == 1) { // User asked for 1 thread so don't add an interactive one
666+
jl_options.nthreadpools = 1;
667+
nthreadsi = 0;
665668
}
666669
jl_options.nthreads = nthreads + nthreadsi;
667670
}

src/threading.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,8 @@ void jl_init_threading(void)
735735
if (errno != 0 || endptr == cp || nthreads <= 0)
736736
nthreads = 1;
737737
cp = endptr;
738+
if (nthreads == 1) // User asked for 1 thread so lets assume they dont want an interactive thread
739+
nthreadsi = 0;
738740
}
739741
if (*cp == ',') {
740742
cp++;

test/cmdlineargs.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
351351

352352
# -t, --threads
353353
code = "print(Threads.threadpoolsize())"
354+
code2 = "print(Threads.maxthreadid())"
354355
cpu_threads = ccall(:jl_effective_threads, Int32, ())
355356
@test string(cpu_threads) ==
356357
read(`$exename --threads auto -e $code`, String) ==
@@ -361,6 +362,11 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
361362
withenv("JULIA_NUM_THREADS" => nt) do
362363
@test read(`$exename --threads=2 -e $code`, String) ==
363364
read(`$exename -t 2 -e $code`, String) == "2"
365+
if nt === nothing
366+
@test read(`$exename -e $code2`, String) == "2" #default + interactive
367+
elseif nt == "1"
368+
@test read(`$exename -e $code2`, String) == "1" #if user asks for 1 give 1
369+
end
364370
end
365371
end
366372
# We want to test oversubscription, but on manycore machines, this can

0 commit comments

Comments
 (0)