Skip to content

Commit ee90103

Browse files
committed
Enable launchers to forward environment variables
Support for environment variable forwarding is added to the srun launcher, as well as two new launchers (mpirun-openmpi, mpirun-intelmpi). Signed-off-by: Jack Morrison <[email protected]>
1 parent 9debc30 commit ee90103

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

reframe/core/launchers/__init__.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ class JobLauncher(metaclass=_JobLauncherMeta):
3636
#: :default: ``[]``
3737
options = variable(typ.List[str], value=[])
3838

39+
#: Dictionary of environment variables to be passed via the job launcher
40+
#: invocation. The keys are the variable names and the values are their
41+
#: corresponding values.
42+
#:
43+
#: :type: :class:`Dict[str, str]`
44+
#: :default: ``{}``
45+
environment_variables = variable(typ.Dict[str, str], value={})
46+
3947
#: Optional modifier of the launcher command.
4048
#:
4149
#: This will be combined with the :attr:`modifier_options` and prepended to
@@ -59,6 +67,7 @@ class JobLauncher(metaclass=_JobLauncherMeta):
5967

6068
def __init__(self):
6169
self.options = []
70+
self.environment_variables = {}
6271

6372
@abc.abstractmethod
6473
def command(self, job):
@@ -73,8 +82,6 @@ def command(self, job):
7382
def run_command(self, job):
7483
'''The full launcher command to be emitted for a specific job.
7584
76-
This includes any user options.
77-
7885
:param job: a job descriptor.
7986
:returns: the launcher command as a string.
8087
'''

reframe/core/launchers/mpi.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ def command(self, job):
4848
if self.use_cpus_per_task and job.num_cpus_per_task:
4949
ret.append(f'--cpus-per-task={job.num_cpus_per_task}')
5050

51-
return ret
51+
if self.environment_variables:
52+
env_vars = ','.join(f'{k}={v}' for k, v in self.environment_variables.items())
53+
ret.append(f'--export={env_vars}')
5254

55+
return ret
5356

5457
@register_launcher('ibrun')
5558
class IbrunLauncher(JobLauncher):
@@ -108,6 +111,21 @@ class MpirunLauncher(JobLauncher):
108111
def command(self, job):
109112
return ['mpirun', '-np', str(job.num_tasks)]
110113

114+
@register_launcher('mpirun-openmpi')
115+
class MpirunOpenMPILauncher(JobLauncher):
116+
def command(self, job):
117+
cmd = ['mpirun', '-np', str(job.num_tasks)]
118+
for name, value in self.environment_variables.items():
119+
cmd += ['-x', f'{name}={value}']
120+
return cmd
121+
122+
@register_launcher('mpirun-intelmpi')
123+
class MpirunIntelMPILauncher(JobLauncher):
124+
def command(self, job):
125+
cmd = ['mpirun', '-np', str(job.num_tasks)]
126+
for name, value in self.environment_variables.items():
127+
cmd += ['-genv', name, value]
128+
return cmd
111129

112130
@register_launcher('mpiexec')
113131
class MpiexecLauncher(JobLauncher):

unittests/test_launchers.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def job(make_job, launcher):
9292
job.exclusive_access = True
9393
job.options = ['--gres=gpu:4', '#DW jobdw anything']
9494
job.launcher.options = ['--foo']
95+
job.launcher.environment_variables = {'FOO': 'bar', 'BAR': 'baz'}
9596
return job
9697

9798

@@ -127,7 +128,11 @@ def test_run_command(job):
127128
elif launcher_name == 'mpirun':
128129
assert command == 'mpirun -np 4 --foo'
129130
elif launcher_name == 'srun':
130-
assert command == 'srun --cpus-per-task=2 --foo'
131+
assert command == 'srun --cpus-per-task=2 --export=FOO=bar,BAR=baz --foo'
132+
elif launcher_name == 'mpirun-openmpi':
133+
assert command == 'mpirun -np 4 -x FOO=bar -x BAR=baz --foo'
134+
elif launcher_name == 'mpirun-intelmpi':
135+
assert command == 'mpirun -np 4 -genv FOO bar -genv BAR baz --foo'
131136
elif launcher_name == 'srunalloc':
132137
assert command == ('srun '
133138
'--job-name=fake_job '

0 commit comments

Comments
 (0)