Skip to content

Commit 7a02e24

Browse files
committed
MPI: move hints/reqs + DockerRequirement checking into Process._init_job()
1 parent 7bce73a commit 7a02e24

File tree

3 files changed

+63
-43
lines changed

3 files changed

+63
-43
lines changed

cwltool/command_line_tool.py

-22
Original file line numberDiff line numberDiff line change
@@ -431,28 +431,6 @@ def make_job_runner(self, runtimeContext: RuntimeContext) -> type[JobBase]:
431431
return SingularityCommandLineJob
432432
elif runtimeContext.user_space_docker_cmd:
433433
return UDockerCommandLineJob
434-
if mpiReq is not None:
435-
if mpiRequired:
436-
if dockerRequired:
437-
raise UnsupportedRequirement(
438-
"No support for Docker and MPIRequirement both being required"
439-
)
440-
else:
441-
_logger.warning(
442-
"MPI has been required while Docker is hinted, discarding Docker hint(s)"
443-
)
444-
self.hints = [h for h in self.hints if h["class"] != "DockerRequirement"]
445-
return CommandLineJob
446-
else:
447-
if dockerRequired:
448-
_logger.warning(
449-
"Docker has been required while MPI is hinted, discarding MPI hint(s)"
450-
)
451-
self.hints = [h for h in self.hints if h["class"] != MPIRequirementName]
452-
else:
453-
raise UnsupportedRequirement(
454-
"Both Docker and MPI have been hinted - don't know what to do"
455-
)
456434
if runtimeContext.podman:
457435
return PodmanCommandLineJob
458436
return DockerCommandLineJob

cwltool/process.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -810,12 +810,43 @@ def inc(d: list[int]) -> None:
810810
tmpdir = ""
811811
stagedir = ""
812812

813-
docker_req, _ = self.get_requirement("DockerRequirement")
813+
docker_req, docker_required = self.get_requirement("DockerRequirement")
814814
default_docker = None
815+
mpi_req, mpi_required = self.get_requirement(MPIRequirementName)
815816

816817
if docker_req is None and runtime_context.default_container:
817818
default_docker = runtime_context.default_container
818819

820+
if (
821+
docker_req is not None
822+
and runtime_context.use_container
823+
and not runtime_context.singularity
824+
and not runtime_context.user_space_docker_cmd
825+
and mpi_req is not None
826+
):
827+
if mpi_required:
828+
if docker_required:
829+
raise UnsupportedRequirement(
830+
"No support for Docker and MPIRequirement both being required"
831+
)
832+
else:
833+
_logger.warning(
834+
"MPI has been required while Docker is hinted, discarding Docker hint(s)"
835+
)
836+
self.hints = [h for h in self.hints if h["class"] != "DockerRequirement"]
837+
docker_req = None
838+
docker_required = False
839+
else:
840+
if docker_required:
841+
_logger.warning(
842+
"Docker has been required while MPI is hinted, discarding MPI hint(s)"
843+
)
844+
self.hints = [h for h in self.hints if h["class"] != MPIRequirementName]
845+
else:
846+
raise UnsupportedRequirement(
847+
"Both Docker and MPI have been hinted - don't know what to do"
848+
)
849+
819850
if (docker_req or default_docker) and runtime_context.use_container:
820851
if docker_req is not None:
821852
# Check if docker output directory is absolute

tests/test_mpi.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@
77
from importlib.resources import files
88
from io import StringIO
99
from pathlib import Path
10-
from typing import Any, Optional
10+
from typing import Any, Optional, cast
1111

1212
import pytest
1313
from ruamel.yaml.comments import CommentedMap, CommentedSeq
1414
from schema_salad.avro.schema import Names
15+
from schema_salad.ref_resolver import file_uri
1516
from schema_salad.utils import yaml_no_ts
1617

1718
import cwltool.load_tool
1819
import cwltool.singularity
1920
import cwltool.udocker
20-
from cwltool.command_line_tool import CommandLineTool
21-
from cwltool.context import LoadingContext, RuntimeContext
21+
from cwltool.context import RuntimeContext
2222
from cwltool.main import main
2323
from cwltool.mpi import MpiConfig, MPIRequirementName
24+
from cwltool.command_line_tool import CommandLineTool
2425

2526
from .util import get_data, working_directory
2627

@@ -292,15 +293,22 @@ def schema_ext11() -> Generator[Names, None, None]:
292293

293294
mpiReq = CommentedMap({"class": MPIRequirementName, "processes": 1})
294295
containerReq = CommentedMap({"class": "DockerRequirement"})
295-
basetool = CommentedMap({"cwlVersion": "v1.1", "inputs": CommentedSeq(), "outputs": CommentedSeq()})
296+
basetool = CommentedMap(
297+
{
298+
"cwlVersion": "v1.1",
299+
"class": "CommandLineTool",
300+
"inputs": CommentedSeq(),
301+
"outputs": CommentedSeq(),
302+
}
303+
)
296304

297305

298306
def mk_tool(
299307
schema: Names,
300308
opts: list[str],
301309
reqs: Optional[list[CommentedMap]] = None,
302310
hints: Optional[list[CommentedMap]] = None,
303-
) -> tuple[LoadingContext, RuntimeContext, CommentedMap]:
311+
) -> tuple[RuntimeContext, CommandLineTool]:
304312
tool = basetool.copy()
305313

306314
if reqs is not None:
@@ -313,50 +321,53 @@ def mk_tool(
313321
rc = RuntimeContext(vars(args))
314322
lc = cwltool.main.setup_loadingContext(None, rc, args)
315323
lc.avsc_names = schema
316-
return lc, rc, tool
324+
tool["id"] = file_uri(os.path.abspath("./mktool.cwl"))
325+
assert lc.loader is not None
326+
lc.loader.idx[tool["id"]] = tool
327+
return rc, cast(CommandLineTool, cwltool.load_tool.load_tool(tool, lc))
317328

318329

319330
def test_singularity(schema_ext11: Names) -> None:
320-
lc, rc, tool = mk_tool(schema_ext11, ["--singularity"], reqs=[mpiReq, containerReq])
321-
clt = CommandLineTool(tool, lc)
331+
rc, clt = mk_tool(schema_ext11, ["--singularity"], reqs=[mpiReq, containerReq])
332+
clt._init_job({}, rc)
322333
jr = clt.make_job_runner(rc)
323334
assert jr is cwltool.singularity.SingularityCommandLineJob
324335

325336

326337
def test_udocker(schema_ext11: Names) -> None:
327-
lc, rc, tool = mk_tool(schema_ext11, ["--udocker"], reqs=[mpiReq, containerReq])
328-
clt = CommandLineTool(tool, lc)
338+
rc, clt = mk_tool(schema_ext11, ["--udocker"], reqs=[mpiReq, containerReq])
339+
clt._init_job({}, rc)
329340
jr = clt.make_job_runner(rc)
330341
assert jr is cwltool.udocker.UDockerCommandLineJob
331342

332343

333344
def test_docker_hint(schema_ext11: Names) -> None:
334345
# Docker hint, MPI required
335-
lc, rc, tool = mk_tool(schema_ext11, [], hints=[containerReq], reqs=[mpiReq])
336-
clt = CommandLineTool(tool, lc)
346+
rc, clt = mk_tool(schema_ext11, [], hints=[containerReq], reqs=[mpiReq])
347+
clt._init_job({}, rc)
337348
jr = clt.make_job_runner(rc)
338349
assert jr is cwltool.job.CommandLineJob
339350

340351

341352
def test_docker_required(schema_ext11: Names) -> None:
342353
# Docker required, MPI hinted
343-
lc, rc, tool = mk_tool(schema_ext11, [], reqs=[containerReq], hints=[mpiReq])
344-
clt = CommandLineTool(tool, lc)
354+
rc, clt = mk_tool(schema_ext11, [], reqs=[containerReq], hints=[mpiReq])
355+
clt._init_job({}, rc)
345356
jr = clt.make_job_runner(rc)
346357
assert jr is cwltool.docker.DockerCommandLineJob
347358

348359

349360
def test_docker_mpi_both_required(schema_ext11: Names) -> None:
350361
# Both required - error
351-
lc, rc, tool = mk_tool(schema_ext11, [], reqs=[mpiReq, containerReq])
352-
clt = CommandLineTool(tool, lc)
362+
rc, clt = mk_tool(schema_ext11, [], reqs=[mpiReq, containerReq])
353363
with pytest.raises(cwltool.errors.UnsupportedRequirement):
354-
clt.make_job_runner(rc)
364+
clt._init_job({}, rc)
365+
clt.make_job_runner(rc)
355366

356367

357368
def test_docker_mpi_both_hinted(schema_ext11: Names) -> None:
358369
# Both hinted - error
359-
lc, rc, tool = mk_tool(schema_ext11, [], hints=[mpiReq, containerReq])
360-
clt = CommandLineTool(tool, lc)
370+
rc, clt = mk_tool(schema_ext11, [], hints=[mpiReq, containerReq])
361371
with pytest.raises(cwltool.errors.UnsupportedRequirement):
362-
clt.make_job_runner(rc)
372+
clt._init_job({}, rc)
373+
clt.make_job_runner(rc)

0 commit comments

Comments
 (0)