Skip to content

Commit 2bf3774

Browse files
authored
Merge pull request #20 from DilumAluthge-forks/dpa/params-env
Add support for `params[:env]` (that is, the `env` kwarg to the `addprocs()` function)
2 parents 2c65df2 + 5ba8364 commit 2bf3774

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

src/slurmmanager.jl

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,72 @@ mutable struct SlurmManager <: ClusterManager
4343
end
4444
end
4545

46+
@static if Base.VERSION >= v"1.9.0"
47+
# In Julia 1.9 and later, the no-argument method `Distributed.default_addprocs_params()`
48+
# includes :env, so we don't need to do anything.
49+
# See also: https://github.com/JuliaLang/julia/blob/v1.9.0/stdlib/Distributed/src/cluster.jl#L526-L541
50+
51+
Distributed.default_addprocs_params(::SlurmManager) = Distributed.default_addprocs_params()
52+
elseif v"1.6.0" <= Base.VERSION < v"1.9.0"
53+
# In Julia 1.6 through 1.8, the no-argument method `Distributed.default_addprocs_params()`
54+
# does not include :env. However, Julia does allow us to add a specialized method
55+
# `Distributed.default_addprocs_params(::SlurmManager)`, so we do so here.
56+
#
57+
# The ability to add the specialized `Distributed.default_addprocs_params(::SlurmManager)`
58+
# method was added to Julia in https://github.com/JuliaLang/julia/pull/38570
59+
#
60+
# See also: https://github.com/JuliaLang/julia/blob/v1.8.0/stdlib/Distributed/src/cluster.jl#L526-L540
61+
function Distributed.default_addprocs_params(::SlurmManager)
62+
our_stuff = Dict{Symbol,Any}(
63+
:env => [],
64+
)
65+
upstreams_stuff = Distributed.default_addprocs_params()
66+
total_stuff = merge(our_stuff, upstreams_stuff)
67+
return total_stuff
68+
end
69+
elseif Base.VERSION < v"1.6.0"
70+
# In Julia 1.5 and earlier, Julia does not have the `addenv()` function.
71+
# I don't want to add a dependency on Compat.jl just for this one feature,
72+
# so we will just choose to not support `params[:env]` on Julia 1.5 and earlier.
73+
end
74+
75+
function _new_environment_additions(params_env::Dict{String, String})
76+
env2 = Dict{String, String}()
77+
for (name, value) in pairs(params_env)
78+
# For each key-value mapping in `params[:env]`, we respect that mapping and we pass it
79+
# to the workers.
80+
env2[name] = value
81+
end
82+
return env2
83+
end
84+
4685
function launch(manager::SlurmManager, params::Dict, instances_arr::Array, c::Condition)
4786
try
4887
exehome = params[:dir]
4988
exename = params[:exename]
5089
exeflags = params[:exeflags]
5190

91+
_srun_cmd_without_env = `srun -D $exehome $exename $exeflags --worker`
92+
93+
@static if Base.VERSION >= v"1.6.0"
94+
env_arr = params[:env]
95+
# Pass the key-value pairs from `params[:env]` to the `srun` command:
96+
env2 = Dict{String,String}()
97+
for (name, value) in pairs(Dict{String,String}(env_arr))
98+
env2[name] = value
99+
end
100+
srun_cmd_with_env = addenv(_srun_cmd_without_env, env2)
101+
else
102+
# See discussion above for why we don't support this functionality on Julia 1.5 and earlier.
103+
if haskey(params, :env)
104+
@warn "SlurmClusterManager.jl does not support params[:env] on Julia 1.5 and earlier" Base.VERSION
105+
end
106+
srun_cmd_with_env = _srun_cmd_without_env
107+
end
108+
52109
# Pass cookie as stdin to srun; srun forwards stdin to process
53110
# This way the cookie won't be visible in ps, top, etc on the compute node
54-
srun_cmd = `srun -D $exehome $exename $exeflags --worker`
55-
manager.srun_proc = open(srun_cmd, write=true, read=true)
111+
manager.srun_proc = open(srun_cmd_with_env, write=true, read=true)
56112
write(manager.srun_proc, cluster_cookie())
57113
write(manager.srun_proc, "\n")
58114

0 commit comments

Comments
 (0)