forked from SgmAstro/DESI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
submit.py
158 lines (115 loc) · 5.08 KB
/
submit.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import os
import argparse
import numpy as np
def customise_script(args, debug=False):
script = args.script
queue = args.queue
memory = args.memory
time = args.time
script_log = args.script_log
account = args.account
nodes = args.nodes
root = os.environ['CODE_ROOT'] + '/bin/'
supported = ['gold_pipeline',\
'gold_d8_pipeline',\
'rand_pipeline',\
'rand_d8_pipeline',\
'rand_ddp1_d8_pipeline',\
'rand_ddp1_pipeline']
if script == None:
for script in supported:
args.script = root + script
customise_script(args)
return 0
else:
assert root in script, f'Error on {script}'
if script_log == None:
# TODO: findfile
ss = os.path.basename(script).split('.')[0]
if 'rand' in script:
log = os.environ['HOME'] + f'/data/GAMA4/randoms/logs/{ss}.log'
else:
log = os.environ['HOME'] + f'/data/GAMA4/logs/{ss}.log'
else:
log = script_log
##
ff = open(f'/{script}')
ff = ff.read()
ff = ff.split('\n')
ff = [x.rstrip() for x in ff]
custom = [x for x in ff if '#SBATCH' in x]
rest = [x for x in ff if '#SBATCH' not in x]
#
# -- Example --
#
#SBATCH -p cordelia
#SBATCH --mem=20G
#SBATCH -t 02:00:00
#SBATCH -o /cosma/home/durham/dc-wils7/data/GAMA4/logs/gold_pipeline.log
#SBATCH -A durham
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=1
#SBATCH --open-mode=append
def swap(xx):
args = {'queue': {'sbatch': ['-p', '--partition'], 'arg': queue, 'equals': False},
'memory': {'sbatch': ['--mem'], 'arg': memory, 'equals': True},
'time': {'sbatch': ['-t'], 'arg': time, 'equals': False},
'account': {'sbatch': ['-A'], 'arg': account, 'equals': False},
'log': {'sbatch': ['-o'], 'arg': log, 'equals': False},
'nodes': {'sbatch': ['--nodes'], 'arg': nodes, 'equals': True}}
if debug:
print('\n\n... Swapping ...')
for i, _ in enumerate(xx):
for arg in sorted(args.keys()):
var = args[arg]['arg']
batch = np.atleast_1d(args[arg]['sbatch'])
equals = args[arg]['equals']
if (var != None):
if arg == 'memory':
var += 'G'
split = []
for yy in _.split():
split += yy.split('=')
found = np.any([xx in batch for xx in split])
# print(var, batch, found, split)
if found:
if equals:
xx[i] = '#SBATCH {}={}'.format(batch[0], var)
else:
xx[i] = '#SBATCH {} {}'.format(batch[0], var)
return xx
if debug:
print('\n\n---- INPUT ----\n')
for xx in custom:
print(xx)
## Swap
custom = swap(custom)
if debug:
print('\n\n---- OUTPUT ----\n')
for xx in custom:
print(xx)
opath = root + '/custom/' + os.path.basename(script).split('.')[0]
with open(opath, 'w') as f:
rest.remove('#!/bin/bash')
to_write = custom + rest
f.write('#!/bin/bash')
f.write('\n')
for line in custom + rest:
f.write(line)
f.write('\n')
print('\nWriting {}'.format(opath))
os.system(f'chmod 700 {opath}')
if __name__ == '__main__':
# /cosma/home/durham/dc-wils7/DESI/bin/gold_pipeline
parser = argparse.ArgumentParser(description='Customise pipeline submission scripts.')
parser.add_argument('-s', '--script', help='Script to customise.', type=str, default=None)
parser.add_argument('-q', '--queue', help='Queue for submission.', type=str, default=None)
parser.add_argument('-m', '--memory', help='Node memory usage [GB].', type=str, default=None)
parser.add_argument('-t', '--time', help='Job time to request.', type=str, default=None)
parser.add_argument('--script_log', help='Job log path.', type=str, default=None)
parser.add_argument('-a', '--account', help='Account for submission.', type=str, default=None)
parser.add_argument('-n', '--nodes', help='Nodes to request.', type=int, default=None)
args = parser.parse_args()
script = args.script
customise_script(args)
print('\n\nDone.\n\n')