forked from cms-rwth/CoffeaRunner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.py
515 lines (477 loc) · 17.3 KB
/
runner.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
import os
import sys
import json
import argparse
import time
import numpy as np
import uproot
from coffea.util import load, save
from coffea import processor
from BTVNanoCommissioning.workflows import workflows
def validate(file):
try:
fin = uproot.open(file)
return fin["Events"].num_entries
except:
print("Corrupted file: {}".format(file))
return file
def check_port(port):
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind(("0.0.0.0", port))
available = True
except:
available = False
sock.close()
return available
def retry_handler(exception, task_record):
from parsl.executors.high_throughput.interchange import ManagerLost
if isinstance(exception, ManagerLost):
return 0.1
else:
return 1
def get_main_parser():
parser = argparse.ArgumentParser(
description="Run analysis on baconbits files using processor coffea files"
)
# Inputs
parser.add_argument(
"--wf",
"--workflow",
dest="workflow",
choices=list(workflows.keys()),
help="Which processor to run",
required=True,
)
parser.add_argument(
"-o",
"--output",
default=r"hists.coffea",
help="Output histogram filename (default: %(default)s)",
)
parser.add_argument(
"--samples",
"--json",
dest="samplejson",
default="dummy_samples.json",
help="JSON file containing dataset and file locations (default: %(default)s)",
)
parser.add_argument("--year", default="2017", help="Year")
parser.add_argument(
"--campaign",
default="Rereco17_94X",
help="Dataset campaign, change the corresponding correction files",
)
# Scale out
parser.add_argument(
"--executor",
choices=[
"iterative",
"futures",
"parsl/slurm",
"parsl/condor",
"parsl/condor/naf_lite",
"dask/condor",
"dask/slurm",
"dask/lpc",
"dask/lxplus",
"dask/casa",
],
default="futures",
help="The type of executor to use (default: %(default)s). Other options can be implemented. "
"For example see https://parsl.readthedocs.io/en/stable/userguide/configuring.html"
"- `parsl/slurm` - tested at DESY/Maxwell"
"- `parsl/condor` - tested at DESY, RWTH"
"- `parsl/condor/naf_lite` - tested at DESY"
"- `dask/slurm` - tested at DESY/Maxwell"
"- `dask/condor` - tested at DESY, RWTH"
"- `dask/lpc` - custom lpc/condor setup (due to write access restrictions)"
"- `dask/lxplus` - custom lxplus/condor setup (due to port restrictions)",
)
parser.add_argument(
"-j",
"--workers",
type=int,
default=6,
help="Number of workers (cores/threads) to use for multi-worker executors "
"(e.g. futures or condor) (default: %(default)s)",
)
parser.add_argument(
"-s",
"--scaleout",
type=int,
default=40,
help="Number of nodes to scale out to if using slurm/condor. Total number of "
"concurrent threads is ``workers x scaleout`` (default: %(default)s)",
)
parser.add_argument(
"--voms",
default=None,
type=str,
help="Path to voms proxy, made accessible to worker nodes. By default a copy will be made to $HOME.",
)
# Debugging
parser.add_argument(
"--validate",
action="store_true",
help="Do not process, just check all files are accessible",
)
parser.add_argument("--skipbadfiles", action="store_true", help="Skip bad files.")
parser.add_argument(
"--only", type=str, default=None, help="Only process specific dataset or file"
)
parser.add_argument(
"--limit",
type=int,
default=None,
metavar="N",
help="Limit to the first N files of each dataset in sample JSON",
)
parser.add_argument(
"--chunk",
type=int,
default=100000,
metavar="N",
help="Number of events per process chunk",
)
parser.add_argument(
"--retries",
type=int,
default=10,
metavar="N",
help="Number of retries for coffea processor",
)
parser.add_argument(
"--max",
type=int,
default=None,
metavar="N",
help="Max number of chunks to run in total",
)
parser.add_argument(
"--export_array",
action="store_true",
default=False,
help="stored selected events to np.arrays",
)
parser.add_argument(
"--systematics",
action="store_true",
default=False,
help="process systematics",
)
return parser
if __name__ == "__main__":
parser = get_main_parser()
args = parser.parse_args()
if args.output == parser.get_default("output"):
index = args.samplejson.rfind("/") + 1
sample_json = args.samplejson[index:]
args.output = f'hists_{args.workflow}_{(sample_json).rstrip(".json")}.coffea'
# load dataset
with open(args.samplejson) as f:
sample_dict = json.load(f)
for key in sample_dict.keys():
sample_dict[key] = sample_dict[key][: args.limit]
if args.executor == "dask/casa":
for key in sample_dict.keys():
sample_dict[key] = [
path.replace("xrootd-cms.infn.it/", "xcache")
for path in sample_dict[key]
]
# For debugging
if args.only is not None:
if args.only in sample_dict.keys(): # is dataset
sample_dict = dict([(args.only, sample_dict[args.only])])
if "*" in args.only: # wildcard for datasets
_new_dict = {}
print("Will only proces the following datasets:")
for k, v in sample_dict.items():
if k.lstrip("/").startswith(args.only.rstrip("*")):
print(" ", k)
_new_dict[k] = v
sample_dict = _new_dict
else: # is file
for key in sample_dict.keys():
if args.only in sample_dict[key]:
sample_dict = dict([(key, [args.only])])
# Scan if files can be opened
if args.validate:
start = time.time()
from p_tqdm import p_map
all_invalid = []
for sample in sample_dict.keys():
_rmap = p_map(
validate,
sample_dict[sample],
num_cpus=args.workers,
desc=f"Validating {sample[:20]}...",
)
_results = list(_rmap)
counts = np.sum([r for r in _results if np.isreal(r)])
all_invalid += [r for r in _results if type(r) == str]
print("Events:", np.sum(counts))
print("Bad files:")
for fi in all_invalid:
print(f" {fi}")
end = time.time()
print("TIME:", time.strftime("%H:%M:%S", time.gmtime(end - start)))
if input("Remove bad files? (y/n)") == "y":
print("Removing:")
for fi in all_invalid:
print(f"Removing: {fi}")
os.system(f"rm {fi}")
if input("Write list of bad files? (y/n)") == "y":
corrupted_name = (args.samplejson).split(".json")[0]
with open(f"{corrupted_name}_corrupted.txt", "w") as bad_txt:
print("Writing:")
for fi in all_invalid:
print(f"Writing: {fi}")
bad_txt.write(fi)
bad_txt.write("\n")
sys.exit(0)
# load workflow
# if args.systematics is not None:
# processor_instance = workflows[args.workflow](
# year=args.year,
# campaign=args.campaign,
# export_array=args.export_array,
# systematics=args.systematics,
# isData=args.isData,
# )
# else:
processor_instance = workflows[args.workflow](args.year, args.campaign)
if args.export_array is not None:
processor_instance = workflows[args.workflow](
year=args.year, campaign=args.campaign, export_array=args.export_array
)
# AS: not all workflows will have these two parameters, so probably
# we want to avoid always calling it like that in the future
if args.executor not in ["futures", "iterative", "dask/lpc", "dask/casa"]:
"""
dask/parsl needs to export x509 to read over xrootd
dask/lpc uses custom jobqueue provider that handles x509
"""
if args.voms is not None:
_x509_path = args.voms
else:
try:
_x509_localpath = (
[
l
for l in os.popen("voms-proxy-info").read().split("\n")
if l.startswith("path")
][0]
.split(":")[-1]
.strip()
)
except:
raise RuntimeError(
"x509 proxy could not be parsed, try creating it with 'voms-proxy-init'"
)
_x509_path = os.environ["HOME"] + f'/.{_x509_localpath.split("/")[-1]}'
os.system(f"cp {_x509_localpath} {_x509_path}")
env_extra = [
"export XRD_RUNFORKHANDLER=1",
f"export X509_USER_PROXY={_x509_path}",
f'export X509_CERT_DIR={os.environ["X509_CERT_DIR"]}',
f"export PYTHONPATH=$PYTHONPATH:{os.getcwd()}",
]
condor_extra = [
f"cd {os.getcwd()}",
f"ls {os.getcwd()}",
f'source {os.environ["HOME"]}/.bashrc',
f"source {os.getcwd()}/CondaSetup.sh",
f'conda activate {os.environ["CONDA_PREFIX"]}',
]
#########
# Execute
if args.executor in ["futures", "iterative"]:
if args.executor == "iterative":
_exec = processor.iterative_executor
else:
_exec = processor.futures_executor
output = processor.run_uproot_job(
sample_dict,
treename="Events",
processor_instance=processor_instance,
executor=_exec,
executor_args={
"skipbadfiles": args.skipbadfiles,
"schema": processor.NanoAODSchema,
"workers": args.workers,
},
chunksize=args.chunk,
maxchunks=args.max,
)
elif "parsl" in args.executor:
import parsl
from parsl.providers import LocalProvider, CondorProvider, SlurmProvider
from parsl.channels import LocalChannel
from parsl.config import Config
from parsl.executors import HighThroughputExecutor
from parsl.launchers import SrunLauncher
from parsl.addresses import address_by_hostname, address_by_query
if "slurm" in args.executor:
htex_config = Config(
executors=[
HighThroughputExecutor(
label="coffea_parsl_slurm",
address=address_by_hostname(),
prefetch_capacity=0,
provider=SlurmProvider(
channel=LocalChannel(script_dir="logs_parsl"),
launcher=SrunLauncher(),
max_blocks=(args.scaleout) + 10,
init_blocks=args.scaleout,
partition="all",
worker_init="\n".join(env_extra),
walltime="00:120:00",
),
)
],
retries=args.retries,
)
elif "condor" in args.executor:
if "naf_lite" in args.executor:
htex_config = Config(
executors=[
HighThroughputExecutor(
label="coffea_parsl_condor",
address=address_by_query(),
max_workers=1,
worker_debug=True,
provider=CondorProvider(
nodes_per_block=1,
cores_per_slot=args.workers,
mem_per_slot=2, # lite job / opportunistic can only use this much
init_blocks=args.scaleout,
max_blocks=(args.scaleout) + 2,
worker_init="\n".join(env_extra + condor_extra),
walltime="03:00:00", # lite / short queue requirement
),
)
],
retries=20,
retry_handler=retry_handler,
)
else:
htex_config = Config(
executors=[
HighThroughputExecutor(
label="coffea_parsl_condor",
address=address_by_query(),
max_workers=1,
provider=CondorProvider(
nodes_per_block=1,
cores_per_slot=args.workers,
init_blocks=args.scaleout,
max_blocks=(args.scaleout) + 2,
worker_init="\n".join(env_extra + condor_extra),
walltime="00:20:00",
),
)
],
retries=args.retries,
)
else:
raise NotImplementedError
dfk = parsl.load(htex_config)
output = processor.run_uproot_job(
sample_dict,
treename="Events",
processor_instance=processor_instance,
executor=processor.parsl_executor,
executor_args={
"skipbadfiles": args.skipbadfiles,
"schema": processor.NanoAODSchema,
"config": None,
},
chunksize=args.chunk,
maxchunks=args.max,
)
elif "dask" in args.executor:
from dask_jobqueue import SLURMCluster, HTCondorCluster
from distributed import Client
from dask.distributed import performance_report
if "lpc" in args.executor:
env_extra = [
f"export PYTHONPATH=$PYTHONPATH:{os.getcwd()}",
]
from lpcjobqueue import LPCCondorCluster
cluster = LPCCondorCluster(
transfer_input_files="/srv/src/",
ship_env=True,
env_extra=env_extra,
)
elif "lxplus" in args.executor:
n_port = 8786
if not check_port(8786):
raise RuntimeError(
"Port '8786' is not occupied on this node. Try another one."
)
import socket
cluster = HTCondorCluster(
cores=1,
memory="2GB", # hardcoded
disk="1GB",
death_timeout="60",
nanny=False,
scheduler_options={"port": n_port, "host": socket.gethostname()},
job_extra={
"log": "dask_job_output.log",
"output": "dask_job_output.out",
"error": "dask_job_output.err",
"should_transfer_files": "Yes",
"when_to_transfer_output": "ON_EXIT",
"+JobFlavour": '"workday"',
},
extra=["--worker-port {}".format(n_port)],
env_extra=env_extra,
)
elif "slurm" in args.executor:
cluster = SLURMCluster(
queue="all",
cores=args.workers,
processes=args.workers,
memory="200 GB",
retries=args.retries,
walltime="00:30:00",
env_extra=env_extra,
)
elif "condor" in args.executor:
cluster = HTCondorCluster(
cores=args.workers,
memory="4GB",
disk="4GB",
env_extra=env_extra,
)
if args.executor == "dask/casa":
client = Client("tls://localhost:8786")
import shutil
shutil.make_archive("workflows", "zip", base_dir="workflows")
client.upload_file("workflows.zip")
else:
cluster.adapt(minimum=args.scaleout)
client = Client(cluster)
print("Waiting for at least one worker...")
client.wait_for_workers(1)
with performance_report(filename="dask-report.html"):
output = processor.run_uproot_job(
sample_dict,
treename="Events",
processor_instance=processor_instance,
executor=processor.dask_executor,
executor_args={
"client": client,
"skipbadfiles": args.skipbadfiles,
"schema": processor.NanoAODSchema,
"retries": args.retries,
},
chunksize=args.chunk,
maxchunks=args.max,
)
save(output, args.output)
print(output)
print(f"Saving output to {args.output}")