-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
157 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import spython | ||
|
||
import docker | ||
|
||
|
||
class DockerRunner: | ||
def __init__(self, image, command, volumes_map, num_cores): | ||
self._image = image | ||
self._command = command | ||
self._volumes = {} | ||
self._num_cores = num_cores | ||
for k, v in volumes_map.items(): | ||
self._volumes[k] = {"bind": v, "mode": "rw"} | ||
|
||
def run(self): | ||
client = docker.from_env() | ||
container = client.containers.run( | ||
volumes=self._volumes, | ||
nano_cpus=int(self._num_cores * 1e9), | ||
image=self._image, | ||
command=self._command, | ||
detach=False, | ||
stream=True, | ||
) | ||
return container | ||
|
||
|
||
class DockerRunnerBuilder: | ||
def __init__(self): | ||
self._instance = None | ||
|
||
def __call__(self, image, command, volumes_map, num_cores, **_ignored): | ||
if not self._instance: | ||
self._instance = DockerRunner(image, command, volumes_map, num_cores) | ||
return self._instance | ||
|
||
|
||
class SingularityRunner: | ||
def __init__(self, image, command, volumes_map, num_cores, use_gpu): | ||
self._image = image | ||
self._command = command | ||
self._num_cores = num_cores | ||
self._use_gpu = use_gpu | ||
self._volumes = [] | ||
for k, v in volumes_map.items(): | ||
self._volumes.append(f"{k}:{v}") | ||
|
||
def run(self): | ||
executor = spython.main.Client.execute( | ||
image=self._image, | ||
command=self._command, | ||
bind=self._volumes, | ||
nv=self._use_gpu, | ||
singularity_options=[f"cpus={self._num_cores}"], | ||
stream=True, | ||
) | ||
return executor | ||
|
||
|
||
class SingularityRunnerBuilder: | ||
def __init__(self): | ||
self._instance = None | ||
|
||
def __call__(self, image, command, volumes_map, num_cores, use_gpu, **_ignored): | ||
if not self._instance: | ||
self._instance = SingularityRunner( | ||
image, command, volumes_map, num_cores, use_gpu | ||
) | ||
return self._instance | ||
|
||
|
||
class RunnerFactory: | ||
def __init__(self): | ||
self._builders = {} | ||
|
||
def register_builder(self, key, builder): | ||
self._builders[key] = builder | ||
|
||
def create(self, key, **kwargs): | ||
builder = self._builders.get(key) | ||
if not builder: | ||
raise ValueError(key) | ||
return builder(**kwargs) | ||
|
||
|
||
class RunnerProvider(RunnerFactory): | ||
def get(self, runner_type, **kwargs): | ||
return self.create(runner_type, **kwargs) | ||
|
||
|
||
runner_provider = RunnerProvider() | ||
runner_provider.register_builder("DOCKER", DockerRunnerBuilder()) | ||
runner_provider.register_builder("SINGULARITY", SingularityRunnerBuilder()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters