-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobtools.py
253 lines (198 loc) · 8.05 KB
/
jobtools.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# -*- coding: utf-8 -*-
import os
import sys
import stat
import subprocess
from pathlib import Path
import json
import time
import random
import string
import joblib
import xarray as xr
job_list = {}
def register_job(job):
global job_list
assert job.job_name not in job_list
job_list[job.job_name] = job
def retrieve_job(job_name):
return job_list[job_name]
def get_path(base_folder, job_name, params):
hash = joblib.hash(params)
save_path = Path(base_folder) / job_name / hash
if not os.path.exists(save_path):
os.makedirs(save_path)
with open(save_path / '__params__.json', mode='w') as f:
json.dump(params, f, indent=4)
return save_path
def _run_one_job_task(base_folder, job_name, params, func, keys, force_recompute):
job = Job(base_folder, job_name, params, func)
job.compute(keys, force_recompute=force_recompute)
_slurm_script = """#! {python}
import sys
sys.path.append("{module_folder}")
from jobtools import _run_one_job_task
from {module_name} import {job_instance_name} as job
job.compute({keys}, force_recompute={force_recompute})
"""
def compute_job_list(job, list_keys, force_recompute=True, engine='loop', **engine_kargs):
if not force_recompute:
cleaned_list_key = []
#clean the list
for keys in list_keys:
output_filename = job.get_filename(*keys)
if not os.path.exists(output_filename):
cleaned_list_key.append(keys)
else:
print(job.job_name , 'already processed',keys)
list_keys = cleaned_list_key
t0 = time.perf_counter()
if engine == 'loop':
for keys in list_keys:
job.compute(keys, force_recompute=force_recompute)
elif engine == 'dask':
#~ raise(NotImplementedError)
#~
client = engine_kargs['client']
tasks = []
for keys in list_keys:
#~ print('submit', keys)
task = client.submit(_run_one_job_task, job.base_folder, job.job_name, job.params, job.func, keys, force_recompute)
tasks.append(task)
for task in tasks:
task.result()
elif engine == 'joblib':
n_jobs = engine_kargs['n_jobs']
#~ joblib.Parallel(n_jobs=n_jobs)(joblib.delayed(job.compute)(keys) for keys in list_keys)
#~ print(job.base_folder, job.job_name, job.params, job.func, list_keys[0])
joblib.Parallel(n_jobs=n_jobs)(joblib.delayed(_run_one_job_task)(job.base_folder, job.job_name, job.params, job.func, keys, force_recompute) for keys in list_keys)
elif engine == 'slurm':
# create python script and launch then with "srun"
rand_name = ''.join(random.choices(string.ascii_uppercase + string.digits, k=8))
slurm_script_folder = Path('.') / 'slurm_scripts'
slurm_script_folder = slurm_script_folder.absolute()
print(slurm_script_folder)
slurm_script_folder.mkdir(exist_ok=True)
for i, keys in enumerate(list_keys):
key_txt = '_'.join([str(e) for e in keys])
script_name = slurm_script_folder / f'{job.job_name}_{key_txt}_{rand_name}_{i}.py'
output_name = slurm_script_folder / f'{job.job_name}_{key_txt}_{rand_name}_{i}_%j.out'
# output_name = slurm_script_folder / f'{job.job_name}_{rand_name}_{i}.log'
print()
print('###', script_name.stem, '###')
# print(output_name)
module_name = engine_kargs['module_name']
with open(script_name, 'w') as f:
slurm_script = _slurm_script.format(
python=sys.executable,
module_folder=Path('.').absolute(),
module_name=module_name,
job_instance_name=job.job_name+'_job',
keys=keys,
force_recompute=force_recompute,
)
f.write(slurm_script)
os.fchmod(f.fileno(), mode = stat.S_IRWXU)
# _slurm_script
# cpus_per_task = engine_kargs.get('cpus_per_task', 1)
# partition = engine_kargs.get('partition', 'shared-cpu') #shared-gpu
# gpus = engine_kargs.get('gpus', 0) #turing:1
# mem = engine_kargs.get('mem', '1G')
# # matlab = engine_kargs.get('matlab', 1)
# # mem = engine_kargs.get('mem', '1G')
# list = []
#
# ## TODO condition on cluster/local and module load things
#
# cmd = ['sbatch',
# f'--cpus-per-task={cpus_per_task}',
# f'--mem={mem}',
# # f'[email protected]',
# f'--job-name="{job.job_name}"',
# f'--partition={partition}',
# f'--gpus={gpus}',
# f'--time=6:00:00',
# f'--output="{output_name}"',
# str(script_name),
# ]
#
slurm_params = engine_kargs['slurm_params']
if not slurm_params:
slurm_params = {'partition':'shared-cpu', 'cpus-per-task':'1', 'mem':'1G'}
# cmd = ['sbatch',
# f'--cpus-per-task={cpus_per_task}',
# f'--mem={mem}',
# f'--exclude={exclude}',
# f'--output="{output_name}"',
# str(script_name),
# ]
cmd = ['sbatch']
cmd += [f'--{key}={value}' for key, value in slurm_params.items()]
cmd += [f'--output="{output_name}"',
str(script_name),]
cmd2 = ' '.join(cmd)
print(cmd2)
os.system(cmd2)
# process = subprocess.Popen(cmd,
# stdout=subprocess.PIPE,
# stderr=subprocess.PIPE)
# stdout, stderr = process.communicate()
# print('stdout', stdout)
# print('stderr', stderr)
t1 = time.perf_counter()
print(job.job_name, 'Total time {:.3f}'.format(t1-t0))
class Job:
def __init__(self, base_folder, job_name, params, func):
self.base_folder = base_folder
self.job_name = job_name
self.params = params
self.save_path = get_path(base_folder, job_name, params)
self.func = func
def _make_keys(self, *args):
if len(args) == 1:
arg = args[0]
if isinstance(arg, str):
keys = (arg, )
elif isinstance(arg, tuple):
keys = arg
elif isinstance(arg, list):
keys = tuple(arg)
else:
raise(ValueError('need keys'))
elif len(args) > 1:
keys = args
else:
raise(ValueError('need keys'))
return keys
def get_filename(self, *args):
keys = self._make_keys(*args)
keys = tuple(str(k) for k in keys)
filename = self.save_path / ('_'.join(keys) + '.nc')
return filename
def get(self, *args, compute=True):
filename = self.get_filename(*args)
if not os.path.exists(filename) and compute:
ds = self.compute(*args)
return ds
ds = xr.open_dataset(filename)
return ds
def compute(self, *args, force_recompute=False):
keys = self._make_keys(*args)
output_filename = self.get_filename(*args)
if not force_recompute and os.path.exists(output_filename):
print(self.job_name , 'already processed',keys)
return
print(self.job_name, 'is processing' , keys)
try:
ds = self.func(*keys, **self.params)
except:
print('Erreur processing', self.job_name, keys)
return None
if ds is not None:
try :
ds.to_netcdf(output_filename)
except PermissionError:
# 2 job are computed in parralel
pass
print('erreur write')
return ds