Skip to content

Commit 2c6fbc9

Browse files
By default, propagate Base.active_project(), Base.LOAD_PATH, and Base.DEPOT_PATH to the workers (but allow the user to override that behavior by specifying JULIA_{PROJECT,LOAD_PATH,DEPOT_PATH} in params[:env]
Co-authored-by: jbphyswx <jbenjami@caltech.edu>
1 parent 81e6d4c commit 2c6fbc9

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

src/slurmmanager.jl

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ end
4747
# In Julia 1.9 and later, the no-argument method `Distributed.default_addprocs_params()`
4848
# includes :env, so we don't need to do anything.
4949
# See also: https://github.com/JuliaLang/julia/blob/v1.9.0/stdlib/Distributed/src/cluster.jl#L526-L541
50-
50+
5151
Distributed.default_addprocs_params(::SlurmManager) = Distributed.default_addprocs_params()
5252
elseif v"1.6.0" <= Base.VERSION < v"1.9.0"
5353
# In Julia 1.6 through 1.8, the no-argument method `Distributed.default_addprocs_params()`
@@ -72,6 +72,73 @@ elseif Base.VERSION < v"1.6.0"
7272
# so we will just choose to not support `params[:env]` on Julia 1.5 and earlier.
7373
end
7474

75+
function _new_environment_additions(params_env::Dict{String, String})
76+
env2 = Dict{String, String}()
77+
user_did_specify = Dict{String, Bool}
78+
user_did_specify_JULIA_PROJECT = false
79+
user_did_specify_JULIA_LOAD_PATH = false
80+
user_did_specify_JULIA_DEPOT_PATH = false
81+
82+
for (name, value) in pairs(params_env)
83+
# For each key-value mapping in `params[:env]`, we respect that mapping and we pass it
84+
# to the workers.
85+
env2[name] = value
86+
87+
# If the user did specify `JULIA_{PROJECT,LOAD_PATH,DEPOT_PATH}` in `params[:env]`, then
88+
# we respect that value, and we pass it to the workers.
89+
if name == "JULIA_PROJECT"
90+
user_did_specify_JULIA_PROJECT = true
91+
@debug "The user did specify a value for JULIA_PROJECT in the `env` kwarg to `addprocs()`; that value will be passed to the workers" env2[JULIA_PROJECT]
92+
end
93+
if name == "JULIA_LOAD_PATH"
94+
user_did_specify_JULIA_LOAD_PATH = true
95+
@debug "The user did specify a value for JULIA_LOAD_PATH in the `env` kwarg to `addprocs()`; that value will be passed to the workers" env2[JULIA_LOAD_PATH]
96+
end
97+
if name == "JULIA_DEPOT_PATH"
98+
user_did_specify_JULIA_DEPOT_PATH = true
99+
@debug "The user did specify a value for JULIA_DEPOT_PATH in the `env` kwarg to `addprocs()`; that value will be passed to the workers" env2[JULIA_DEPOT_PATH]
100+
end
101+
end
102+
103+
directory_separator = Sys.iswindows ? ';' : ':'
104+
105+
# If the user did not specify `JULIA_PROJECT` in `params[:env]`, then we pass
106+
# JULIA_PROJECT=Base.active_project() to the workers.
107+
#
108+
# This use case is commonly hit when the user does NOT set the `JULIA_PROJECT` environment
109+
# variable but DOES start Julia with either `julia --project` or `julia --project=something`.
110+
#
111+
# https://github.com/kleinhenz/SlurmClusterManager.jl/issues/16
112+
if !user_did_specify_JULIA_PROJECT
113+
# Important note: We use Base.active_project() here.
114+
# We do NOT use Base.ACTIVE_PROJECT[], because it is not part of Julia's public API.
115+
env2["JULIA_PROJECT"] = Base.active_project()
116+
@debug "Passing JULIA_PROJECT=Base.active_project() to the workers" env2["JULIA_PROJECT"]
117+
end
118+
119+
# If the user did not specify `JULIA_LOAD_PATH` in `params[:env]`, then we pass
120+
# JULIA_LOAD_PATH=Base.LOAD_PATH to the workers.
121+
#
122+
# This is a bit of an edge case, and I doubt that most users will need it.
123+
# But upstream Distributed.jl does it, so we might as well do it too.
124+
if !user_did_specify_JULIA_LOAD_PATH
125+
env2["JULIA_LOAD_PATH"] = join(Base.LOAD_PATH, directory_separator)
126+
@debug "Passing JULIA_LOAD_PATH=Base.LOAD_PATH to the workers" env2["JULIA_LOAD_PATH"]
127+
end
128+
129+
# If the user did not specify `JULIA_DEPOT_PATH` in `params[:env]`, then we pass
130+
# JULIA_DEPOT_PATH=Base.DEPOT_PATH to the workers.
131+
#
132+
# This is a bit of an edge case, and I doubt that most users will need it.
133+
# But upstream Distributed.jl does it, so we might as well do it too.
134+
if !user_did_specify_JULIA_DEPOT_PATH
135+
env2["JULIA_DEPOT_PATH"] = join(Base.DEPOT_PATH, directory_separator)
136+
@debug "Passing JULIA_DEPOT_PATH=Base.DEPOT_PATH to the workers" env2["JULIA_DEPOT_PATH"]
137+
end
138+
139+
return env2
140+
end
141+
75142
function launch(manager::SlurmManager, params::Dict, instances_arr::Array, c::Condition)
76143
try
77144
exehome = params[:dir]

0 commit comments

Comments
 (0)