-
Notifications
You must be signed in to change notification settings - Fork 0
/
apptainer.py
66 lines (47 loc) · 2.24 KB
/
apptainer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import argparse
import json
import os
parser = argparse.ArgumentParser()
parser.add_argument('-c', type=str, help='Path to config file')
parser.add_argument('-t', type=str, help='Thread count')
parser.add_argument('-o', type=str, help='Output directory')
parser.add_argument('-m', type=int, default=10, help='Memory limit in GB, default: 10')
parser.add_argument('-i', type=str,
help='[Optional] Docker image, only for development purposes')
parser.add_argument('-s', type=str, default=None, help='Slurm partition')
args = parser.parse_args()
def process_configs(configs: dict) -> dict:
mounts = {}
for key, value in configs.items():
if isinstance(value, dict):
mounts.update(process_configs(value))
else:
if isinstance(value, str) and os.path.exists(value):
extension = os.path.splitext(value)[1]
inside_path = f"/srv/input/{key}{extension}"
mounts[inside_path] = os.path.abspath(value)
configs[key] = inside_path
return mounts
image = args.i if args.i else "nicotru/tf-prioritizer:master"
config_path = os.path.abspath(args.c)
threads = args.t
output = os.path.abspath(args.o)
memory = args.m
slurm_partition = args.s
os.makedirs(output, exist_ok=True)
with open(config_path, 'r') as f:
configs = json.load(f)
mounts = process_configs(configs)
docker_config_file = os.path.join(os.path.dirname(output), 'docker_config.json')
with open(docker_config_file, 'w') as f:
json.dump(configs, f, indent=4)
volume_string = ' '.join([f'-B {v}:{k}:ro' for k, v in mounts.items()])
internal_command = f"java -Xmx{memory}g -jar /srv/TF-Prioritizer.jar -t {threads} -o /srv/wd -c /srv/input/configs.json"
external_command = f"apptainer run --no-mount tmp --writable-tmpfs" \
f" -B {docker_config_file}:/srv/input/configs.json:ro {volume_string} -B {output}:/srv/wd:rw " \
f"docker://{image} {internal_command}"
slurm_prefix = f"srun -p {slurm_partition} --cpus-per-task={threads} --mem={memory}G --output={output}/slurm-%j.out " \
f"--error={output}/slurm-%j.err" if slurm_partition else ""
if not args.i:
os.system("docker image pull " + image)
os.system(slurm_prefix + external_command)