Skip to content

Commit

Permalink
Propagate --threads to workers spawned with --procs
Browse files Browse the repository at this point in the history
or --machine-file.
  • Loading branch information
fredrikekre committed Mar 14, 2020
1 parent 417231c commit c11d9db
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 12 deletions.
5 changes: 2 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ Command-line option changes
---------------------------

* `-t N`, `--threads N` starts Julia with `N` threads. This option takes precedence over
`JULIA_NUM_THREADS`. The specified number of threads only applies to the main Julia
process and it does not propagate to worker processes spawned using the `-p`/`--procs`
or `--machine-file` command line arguments ([#TBD]).
`JULIA_NUM_THREADS`. The specified number of threads also propagates to worker processes
spawned using the `-p`/`--procs` or `--machine-file` command line arguments ([#TBD]).

Multi-threading changes
-----------------------
Expand Down
9 changes: 5 additions & 4 deletions doc/src/manual/parallel-computing.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,11 @@ julia> Threads.threadid()
Note that this must be done *before* starting Julia.

!!! note
The number of threads specified with `-t`/`--threads` only applies to the main process,
and, thus, it does not propagate to processes spawned using the `-p`/`--procs` or
`--machine-file` command line options. To spawn workers with multiple threads enabled,
use [`addprocs`](@ref) and pass `-t`/`--threads` as `exeflags`.
The number of threads specified with `-t`/`--threads` is propagated to worker processes
that are spawned using the `-p`/`--procs` or `--machine-file` command line options.
For example, `julia -p2 -t2` spawns 1 main process with 2 worker processes, and all
three processes has 2 threads enabled. For more fine grained control over worker
threads use [`addprocs`](@ref) and pass `-t`/`--threads` as `exeflags`.

## The `@threads` Macro

Expand Down
2 changes: 1 addition & 1 deletion src/jloptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
case 't': // threads
errno = 0;
if (!strcmp(optarg,"auto")) {
jl_options.nthreads = jl_cpu_threads();
jl_options.nthreads = -1;
}
else {
long nthreads = strtol(optarg, &endptr, 10);
Expand Down
4 changes: 3 additions & 1 deletion src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,9 @@ void jl_init_threading(void)
// how many threads available, usable
int max_threads = jl_cpu_threads();
jl_n_threads = JULIA_NUM_THREADS;
if (jl_options.nthreads != 0)
if (jl_options.nthreads < 0) // --threads=auto
jl_n_threads = max_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);
Expand Down
8 changes: 6 additions & 2 deletions stdlib/Distributed/src/cluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,7 @@ end

write_cookie(io::IO) = print(io.in, string(cluster_cookie(), "\n"))

# Starts workers specified by (-n|--procs) and --machine-file command line options
function process_opts(opts)
# startup worker.
# opts.startupfile, opts.load, etc should should not be processed for workers.
Expand All @@ -1310,14 +1311,17 @@ function process_opts(opts)
end
end

# Propagate --threads to workers
exeflags = opts.nthreads > 0 ? `--threads=$(opts.nthreads)` : ``

# add processors
if opts.nprocs > 0
addprocs(opts.nprocs)
addprocs(opts.nprocs; exeflags=exeflags)
end

# load processes from machine file
if opts.machine_file != C_NULL
addprocs(load_machine_file(unsafe_string(opts.machine_file)))
addprocs(load_machine_file(unsafe_string(opts.machine_file)); exeflags=exeflags)
end
return nothing
end
Expand Down
2 changes: 1 addition & 1 deletion test/cmdlineargs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ let exename = `$(Base.julia_cmd()) --startup-file=no`
# Combining --threads and --procs: --threads does not propagate
if cpu_threads > 1; withenv("JULIA_NUM_THREADS"=>nothing) do
code = "print(Threads.nthreads() + sum(remotecall_fetch(() -> Threads.nthreads(), x) for x in workers()))"
@test read(`$exename -p2 -t2 -e $code`, String) == "4"
@test read(`$exename -p2 -t2 -e $code`, String) == "6"
end end

# --procs
Expand Down

0 comments on commit c11d9db

Please sign in to comment.