Skip to content

Commit 4c69907

Browse files
authored
Merge pull request #566 from itajaja/parallel-exec
Add parallel execution
2 parents 3ebbb75 + 0ee8aeb commit 4c69907

File tree

9 files changed

+297
-78
lines changed

9 files changed

+297
-78
lines changed

cwltool/argparser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ def arg_parser(): # type: () -> argparse.ArgumentParser
2828
parser.add_argument("--no-container", action="store_false", default=True,
2929
help="Do not execute jobs in a Docker container, even when specified by the CommandLineTool",
3030
dest="use_container")
31-
31+
parser.add_argument("--parallel", action="store_true", default=False,
32+
help="[experimental] Run jobs in parallel. "
33+
"Does not currently keep track of ResourceRequirements like the number of cores"
34+
"or memory and can overload this system")
3235
parser.add_argument("--preserve-environment", type=Text, action="append",
3336
help="Preserve specific environment variable when running CommandLineTools. May be provided multiple times.",
3437
metavar="ENVVAR",

cwltool/executors.py

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
import logging
2+
import tempfile
3+
import threading
4+
5+
import os
6+
from abc import ABCMeta, abstractmethod
7+
8+
from typing import Dict, Text, Any, Tuple, Set, List
9+
10+
from cwltool.builder import Builder
11+
from cwltool.errors import WorkflowException
12+
from cwltool.mutation import MutationManager
13+
from cwltool.job import JobBase
14+
from cwltool.process import relocateOutputs, cleanIntermediate, Process
15+
16+
17+
_logger = logging.getLogger("cwltool")
18+
19+
defaultStreamHandler = logging.StreamHandler()
20+
_logger.addHandler(defaultStreamHandler)
21+
_logger.setLevel(logging.INFO)
22+
23+
24+
class JobExecutor(object):
25+
__metaclass__ = ABCMeta
26+
27+
def __init__(self):
28+
# type: (...) -> None
29+
self.final_output = [] # type: List
30+
self.final_status = [] # type: List
31+
self.output_dirs = set() # type: Set
32+
33+
def __call__(self, *args, **kwargs):
34+
return self.execute(*args, **kwargs)
35+
36+
def output_callback(self, out, processStatus):
37+
self.final_status.append(processStatus)
38+
self.final_output.append(out)
39+
40+
@abstractmethod
41+
def run_jobs(self,
42+
t, # type: Process
43+
job_order_object, # type: Dict[Text, Any]
44+
logger,
45+
**kwargs # type: Any
46+
):
47+
pass
48+
49+
def execute(self, t, # type: Process
50+
job_order_object, # type: Dict[Text, Any]
51+
logger=_logger,
52+
**kwargs # type: Any
53+
):
54+
# type: (...) -> Tuple[Dict[Text, Any], Text]
55+
56+
if "basedir" not in kwargs:
57+
raise WorkflowException("Must provide 'basedir' in kwargs")
58+
59+
finaloutdir = os.path.abspath(kwargs.get("outdir")) if kwargs.get("outdir") else None
60+
kwargs["outdir"] = tempfile.mkdtemp(prefix=kwargs["tmp_outdir_prefix"]) if kwargs.get(
61+
"tmp_outdir_prefix") else tempfile.mkdtemp()
62+
self.output_dirs.add(kwargs["outdir"])
63+
kwargs["mutation_manager"] = MutationManager()
64+
65+
jobReqs = None
66+
if "cwl:requirements" in job_order_object:
67+
jobReqs = job_order_object["cwl:requirements"]
68+
elif ("cwl:defaults" in t.metadata and "cwl:requirements" in t.metadata["cwl:defaults"]):
69+
jobReqs = t.metadata["cwl:defaults"]["cwl:requirements"]
70+
if jobReqs:
71+
for req in jobReqs:
72+
t.requirements.append(req)
73+
74+
self.run_jobs(t, job_order_object, logger, **kwargs)
75+
76+
if self.final_output and self.final_output[0] and finaloutdir:
77+
self.final_output[0] = relocateOutputs(self.final_output[0], finaloutdir,
78+
self.output_dirs, kwargs.get("move_outputs"),
79+
kwargs["make_fs_access"](""))
80+
81+
if kwargs.get("rm_tmpdir"):
82+
cleanIntermediate(self.output_dirs)
83+
84+
if self.final_output and self.final_status:
85+
return (self.final_output[0], self.final_status[0])
86+
else:
87+
return (None, "permanentFail")
88+
89+
90+
class SingleJobExecutor(JobExecutor):
91+
def run_jobs(self,
92+
t, # type: Process
93+
job_order_object, # type: Dict[Text, Any]
94+
logger,
95+
**kwargs # type: Any
96+
):
97+
jobiter = t.job(job_order_object,
98+
self.output_callback,
99+
**kwargs)
100+
101+
try:
102+
for r in jobiter:
103+
if r:
104+
builder = kwargs.get("builder", None) # type: Builder
105+
if builder is not None:
106+
r.builder = builder
107+
if r.outdir:
108+
self.output_dirs.add(r.outdir)
109+
r.run(**kwargs)
110+
else:
111+
logger.error("Workflow cannot make any more progress.")
112+
break
113+
except WorkflowException:
114+
raise
115+
except Exception as e:
116+
logger.exception("Got workflow error")
117+
raise WorkflowException(Text(e))
118+
119+
120+
class MultithreadedJobExecutor(JobExecutor):
121+
def __init__(self):
122+
super(MultithreadedJobExecutor, self).__init__()
123+
self.threads = set()
124+
self.exceptions = []
125+
126+
def run_job(self,
127+
job, # type: JobBase
128+
**kwargs # type: Any
129+
):
130+
# type: (...) -> None
131+
def runner():
132+
try:
133+
job.run(**kwargs)
134+
except WorkflowException as e:
135+
self.exceptions.append(e)
136+
except Exception as e:
137+
self.exceptions.append(WorkflowException(Text(e)))
138+
139+
self.threads.remove(thread)
140+
141+
thread = threading.Thread(target=runner)
142+
thread.daemon = True
143+
self.threads.add(thread)
144+
thread.start()
145+
146+
def wait_for_next_completion(self): # type: () -> None
147+
if self.exceptions:
148+
raise self.exceptions[0]
149+
150+
def run_jobs(self,
151+
t, # type: Process
152+
job_order_object, # type: Dict[Text, Any]
153+
logger,
154+
**kwargs # type: Any
155+
):
156+
157+
jobiter = t.job(job_order_object, self.output_callback, **kwargs)
158+
159+
for r in jobiter:
160+
if r:
161+
builder = kwargs.get("builder", None) # type: Builder
162+
if builder is not None:
163+
r.builder = builder
164+
if r.outdir:
165+
self.output_dirs.add(r.outdir)
166+
self.run_job(r, **kwargs)
167+
else:
168+
if len(self.threads):
169+
self.wait_for_next_completion()
170+
else:
171+
logger.error("Workflow cannot make any more progress.")
172+
break
173+
174+
while len(self.threads) > 0:
175+
self.wait_for_next_completion()

cwltool/factory.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
from typing import Callable as tCallable
44
from typing import Any, Dict, Text, Tuple, Union
55

6-
from . import load_tool, main, workflow
6+
from . import load_tool, workflow
77
from .argparser import get_default_args
8+
from .executors import SingleJobExecutor
89
from .process import Process
910

1011

@@ -36,11 +37,13 @@ class Factory(object):
3637
def __init__(self,
3738
makeTool=workflow.defaultMakeTool, # type: tCallable[[Any], Process]
3839
# should be tCallable[[Dict[Text, Any], Any], Process] ?
39-
executor=main.single_job_executor, # type: tCallable[...,Tuple[Dict[Text,Any], Text]]
40+
executor=None, # type: tCallable[...,Tuple[Dict[Text,Any], Text]]
4041
**execkwargs # type: Any
4142
):
4243
# type: (...) -> None
4344
self.makeTool = makeTool
45+
if executor is None:
46+
executor = SingleJobExecutor()
4447
self.executor = executor
4548

4649
kwargs = get_default_args()

cwltool/job.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import absolute_import
2+
23
import codecs
4+
import datetime
35
import functools
46
import io
57
import json
@@ -11,27 +13,29 @@
1113
import subprocess
1214
import sys
1315
import tempfile
14-
import datetime
1516
from io import open
17+
from threading import Lock
1618
from typing import (IO, Any, Callable, Dict, Iterable, List, MutableMapping, Text,
17-
Tuple, Union, cast)
19+
Union, cast)
1820

1921
import shellescape
2022

21-
from .utils import copytree_with_merge, docker_windows_path_adjust, onWindows
2223
from . import docker
2324
from .builder import Builder
2425
from .docker_id import docker_vm_id
2526
from .errors import WorkflowException
2627
from .pathmapper import PathMapper, ensure_writable
27-
from .process import (UnsupportedRequirement, empty_subtree, get_feature,
28+
from .process import (UnsupportedRequirement, get_feature,
2829
stageFiles)
2930
from .utils import bytes2str_in_dicts
31+
from .utils import copytree_with_merge, docker_windows_path_adjust, onWindows
3032

3133
_logger = logging.getLogger("cwltool")
3234

3335
needs_shell_quoting_re = re.compile(r"""(^$|[\s|&;()<>\'"$@])""")
3436

37+
job_output_lock = Lock()
38+
3539
FORCE_SHELLED_POPEN = os.getenv("CWLTOOL_FORCE_SHELL_POPEN", "0") == "1"
3640

3741
SHELL_COMMAND_TEMPLATE = """#!/bin/bash
@@ -267,7 +271,8 @@ def _execute(self, runtime, env, rm_tmpdir=True, move_outputs="move"):
267271
if _logger.isEnabledFor(logging.DEBUG):
268272
_logger.debug(u"[job %s] %s", self.name, json.dumps(outputs, indent=4))
269273

270-
self.output_callback(outputs, processStatus)
274+
with job_output_lock:
275+
self.output_callback(outputs, processStatus)
271276

272277
if self.stagedir and os.path.exists(self.stagedir):
273278
_logger.debug(u"[job %s] Removing input staging directory %s", self.name, self.stagedir)

0 commit comments

Comments
 (0)