-
Notifications
You must be signed in to change notification settings - Fork 63
/
tasks.py
533 lines (476 loc) · 18.1 KB
/
tasks.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
import json
import logging
import math
import multiprocessing
import os
import shlex
import shutil
import subprocess
import sys
from collections import defaultdict, deque
from fnmatch import fnmatch
from pathlib import Path
from datetime import datetime
import click
from hpsspy import HpssOSError
from hpsspy.os.path import isfile
from emmet.cli.decorators import sbatch
from emmet.cli.utils import (
EmmetCliError,
ReturnCodes,
VaspDirsGenerator,
chunks,
ensure_indexes,
iterator_slice,
parse_vasp_dirs,
)
logger = logging.getLogger("emmet")
GARDEN = "/home/m/matcomp/garden"
PREFIXES = ["res_", "aflow_", "block_"]
FILE_FILTERS = [
"INCAR*",
"CONTCAR*",
"KPOINTS*",
"POSCAR*",
"POTCAR*",
"OUTCAR*",
"vasprun.xml*",
"transformation*",
]
STORE_VOLUMETRIC_DATA = []
#STORE_VOLUMETRIC_DATA = ["CHGCAR", "LOCPOT", "AECCAR0", "AECCAR1", "AECCAR2", "ELFCAR"]
for v in STORE_VOLUMETRIC_DATA:
FILE_FILTERS.append(f"{v}*")
FILE_FILTERS_DEFAULT = [
f"{d}{os.sep}{f}" if d else f
for f in FILE_FILTERS
for d in ["", "relax1", "relax2"]
]
@click.group()
@click.option(
"-d",
"--directory",
required=True,
help="Directory to use for HPSS or parsing.",
)
@click.option(
"-m", "--nmax", show_default=True, default=10, help="Maximum number of directories."
)
@click.option(
"-p",
"--pattern",
show_default=True,
default="block_*",
help="Pattern for sub-paths to include.",
)
@click.option("--reorg", is_flag=True, help="Reorganize directory in block/launchers.")
def tasks(directory, nmax, pattern, reorg):
"""Backup, restore, and parse VASP calculations."""
pass
@tasks.command()
@sbatch
def prep():
"""Prepare directory for HPSS backup"""
ctx = click.get_current_context()
directory = ctx.parent.params["directory"]
gen = VaspDirsGenerator()
list(x for x in gen)
logger.info(f"Prepared {gen.value} VASP calculation(s) in {directory}.")
return ReturnCodes.SUCCESS if gen.value else ReturnCodes.ERROR
def run_command(args, filelist):
nargs, nfiles, nshow = len(args), len(filelist), 1
full_args = args + filelist
args_short = (
full_args[: nargs + nshow] + [f"({nfiles-1} more ...)"]
if nfiles > nshow
else full_args
)
logger.info(" ".join(args_short))
popen = subprocess.Popen(
full_args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
)
for stdout_line in iter(popen.stdout.readline, ""):
yield stdout_line
popen.stdout.close()
return_code = popen.wait()
if return_code:
raise subprocess.CalledProcessError(return_code, full_args)
def recursive_chown(path, group):
for dirpath, dirnames, filenames in os.walk(path):
if Path(dirpath).group() != group:
shutil.chown(dirpath, group=group)
for filename in filenames:
fullpath = os.path.join(dirpath, filename)
if Path(fullpath).group() != group:
shutil.chown(fullpath, group=group)
def check_pattern(nested_allowed=False):
ctx = click.get_current_context()
pattern = ctx.parent.params["pattern"]
if not nested_allowed and os.sep in pattern:
raise EmmetCliError(f"Nested pattern ({pattern}) not allowed!")
elif not any(pattern.startswith(p) for p in PREFIXES):
raise EmmetCliError(
f"Pattern ({pattern}) only allowed to start with one of {PREFIXES}!"
)
def load_block_launchers():
prefix = (
"block_" # TODO old prefixes (e.g. res/aflow) might not be needed for backup
)
block_launchers = defaultdict(list)
gen = VaspDirsGenerator()
for idx, vasp_dir in enumerate(gen):
if idx and not idx % 500:
logger.info(f"{idx} launchers found ...")
launch_dir = prefix + vasp_dir.split(prefix, 1)[-1]
block, launcher = launch_dir.split(os.sep, 1)
block_launchers[block].append(launcher)
logger.info(f"Loaded {len(block_launchers)} block(s) with {gen.value} launchers.")
return block_launchers
def extract_filename(line):
ls = line.strip().split()
return ls[-1] if len(ls) == 7 else None
@tasks.command()
@sbatch
@click.option("--clean", is_flag=True, help="Remove original launchers.")
@click.option("--check", is_flag=True, help="Check backup consistency.")
@click.option("--force-new", is_flag=True, help="Generate new backup.")
def backup(clean, check, force_new): # noqa: C901
"""Backup directory to HPSS"""
ctx = click.get_current_context()
run = ctx.parent.parent.params["run"]
ctx.parent.params["nmax"] = sys.maxsize # disable maximum launchers for backup
logger.warning("--nmax ignored for HPSS backup!")
directory = ctx.parent.params["directory"]
if not check and clean:
logger.error("Not running --clean without --check enabled.")
return ReturnCodes.ERROR
check_pattern()
logger.info("Discover launch directories ...")
block_launchers = load_block_launchers()
counter, nremove_total = 0, 0
os.chdir(directory)
for block, launchers in block_launchers.items():
logger.info(f"{block} with {len(launchers)} launcher(s)")
try:
isfile(f"{GARDEN}/{block}.tar")
if force_new and run:
ts = datetime.now().strftime("%Y%m%d-%H%M%S")
tarfile = f"{GARDEN}/{block}.tar"
for suf in ["", ".idx"]:
args = shlex.split(f"hsi -q mv -v {tarfile}{suf} {tarfile}{suf}.bkp_{ts}")
for line in run_command(args, []):
logger.info(line.strip())
raise HpssOSError
except HpssOSError: # block not in HPSS
if run:
filelist = [os.path.join(block, l) for l in launchers]
args = shlex.split(f"htar -M 5000000 -Phcvf {GARDEN}/{block}.tar")
try:
for line in run_command(args, filelist):
logger.info(line.strip())
except subprocess.CalledProcessError as e:
logger.error(str(e))
return ReturnCodes.ERROR
counter += 1
else:
logger.warning(f"Skip {block} - already in HPSS")
# Check backup here to allow running it separately
if check:
logger.info(f"Verify {block}.tar ...")
args = shlex.split(
f"htar -Kv -Hrelpaths -Hverify=all -f {GARDEN}/{block}.tar"
)
files_remove = []
try:
for line in run_command(args, []):
line = line.strip()
if line.startswith("HTAR: V "):
ls = line.split(", ")
if len(ls) == 3:
nfiles = len(files_remove)
if nfiles and not nfiles % 1000:
logger.info(f"{nfiles} files ...")
files_remove.append(ls[0].split()[-1])
else:
logger.info(line)
except subprocess.CalledProcessError as e:
logger.error(str(e))
return ReturnCodes.ERROR
if clean:
nremove = len(files_remove)
nremove_total += nremove
if run:
with click.progressbar(files_remove, label="Removing files") as bar:
for fn in bar:
os.remove(fn)
logger.info(f"Removed {nremove} files from disk for {block}.")
else:
logger.info(f"Would remove {nremove} files from disk for {block}.")
logger.info(f"{counter}/{len(block_launchers)} blocks newly backed up to HPSS.")
if clean:
if run:
logger.info(f"Verified and removed a total of {nremove_total} files.")
else:
logger.info(f"Would verify and remove a total of {nremove_total} files.")
return ReturnCodes.SUCCESS
@tasks.command()
@sbatch
@click.option(
"-l",
"--inputfile",
required=True,
type=click.Path(exists=True),
help="Text file with list of launchers to restore (relative to `directory`).",
)
@click.option(
"-f",
"--file-filter",
multiple=True,
show_default=True,
default=FILE_FILTERS_DEFAULT,
help="Set the file filter(s) to match files against in each launcher.",
)
def restore(inputfile, file_filter): # noqa: C901
"""Restore launchers from HPSS"""
ctx = click.get_current_context()
run = ctx.parent.parent.params["run"]
nmax = ctx.parent.params["nmax"]
pattern = ctx.parent.params["pattern"]
directory = ctx.parent.params["directory"]
if not os.path.exists(directory):
os.makedirs(directory)
check_pattern(nested_allowed=True)
if Path(directory).group() != "matgen":
shutil.chown(directory, group="matgen")
block_launchers = defaultdict(list)
nlaunchers = 0
with open(inputfile, "r") as infile:
os.chdir(directory)
with click.progressbar(infile, label="Load blocks") as bar:
for line in bar:
if fnmatch(line, pattern):
if nlaunchers == nmax:
break
if os.sep in line:
block, launcher = line.split(os.sep, 1)
else:
block, launcher = line.strip(), ""
for ff in file_filter:
block_launchers[block].append(
os.path.join(launcher.strip(), ff)
)
# also try restoring unnested block_/launcher_ (result of --reorg flag)
if os.sep in launcher:
launcher = launcher.strip().split(os.sep)[-1]
for ff in file_filter:
block_launchers[block].append(
os.path.join(launcher.strip(), ff)
)
nlaunchers += 1
nblocks = len(block_launchers)
nfiles = sum(len(v) for v in block_launchers.values())
logger.info(
f"Restore {nblocks} block(s) with {nlaunchers} launchers"
f" and {nfiles} file filters to {directory} ..."
)
nfiles_restore_total, max_args = 0, 14000
for block, files in block_launchers.items():
# check if block exists in HPSS
try:
isfile(f"{GARDEN}/{block}.tar")
except HpssOSError:
logger.error(f"{block} does not exist in HPSS!")
continue
# get full list of matching files in archive and check against existing files
args = shlex.split(f"htar -tf {GARDEN}/{block}.tar")
filelist = [os.path.join(block, f) for f in files]
filelist_chunks = [
filelist[i : i + max_args] for i in range(0, len(filelist), max_args)
]
filelist_restore, cnt = [], 0
try:
for chunk in filelist_chunks:
for line in run_command(args, chunk):
fn = extract_filename(line)
if fn:
cnt += 1
if os.path.exists(fn):
logger.debug(f"Skip {fn} - already exists on disk.")
else:
filelist_restore.append(fn)
except subprocess.CalledProcessError as e:
logger.error(str(e))
return ReturnCodes.ERROR
# restore what's missing
if filelist_restore:
nfiles_restore = len(filelist_restore)
nfiles_restore_total += nfiles_restore
if run:
logger.info(
f"Restore {nfiles_restore}/{cnt} files for {block} to {directory} ..."
)
args = shlex.split(f"htar -xvf {GARDEN}/{block}.tar")
filelist_restore_chunks = [
filelist_restore[i : i + max_args]
for i in range(0, len(filelist_restore), max_args)
]
try:
for chunk in filelist_restore_chunks:
for line in run_command(args, chunk):
logger.info(line.strip())
except subprocess.CalledProcessError as e:
logger.error(str(e))
return ReturnCodes.ERROR
else:
logger.info(
f"Would restore {nfiles_restore}/{cnt} files for {block} to {directory}."
)
else:
logger.warning(f"Nothing to restore for {block}!")
if run:
logger.info(f"Set group of {block} to matgen recursively ...")
recursive_chown(block, "matgen")
if run:
logger.info(f"Restored {nfiles_restore_total} files to {directory}.")
else:
logger.info(f"Would restore {nfiles_restore_total} files to {directory}.")
return ReturnCodes.SUCCESS
@tasks.command()
@sbatch
@click.option(
"--task-ids",
type=click.Path(exists=True),
help="JSON file mapping launcher name to task ID.",
)
@click.option(
"--snl-metas",
type=click.Path(exists=True),
help="JSON file mapping launcher name to SNL metadata.",
)
@click.option(
"--nproc",
type=int,
default=1,
show_default=True,
help="Number of processes for parallel parsing.",
)
@click.option(
"-s",
"--store-volumetric-data",
multiple=True,
default=STORE_VOLUMETRIC_DATA,
help="Store any of CHGCAR, LOCPOT, AECCAR0, AECCAR1, AECCAR2, ELFCAR.",
)
def parse(task_ids, snl_metas, nproc, store_volumetric_data): # noqa: C901
"""Parse VASP launchers into tasks"""
ctx = click.get_current_context()
if "CLIENT" not in ctx.obj:
raise EmmetCliError("Use --spec to set target DB for tasks!")
run = ctx.parent.parent.params["run"]
nmax = ctx.parent.params["nmax"]
directory = ctx.parent.params["directory"].rstrip(os.sep)
tag = os.path.basename(directory)
target = ctx.obj["CLIENT"]
snl_collection = target.db.snls_user
collection_count = target.collection.count_documents({})
logger.info(
f"Connected to {target.collection.full_name} with {collection_count} tasks."
)
ensure_indexes(
["task_id", "tags", "dir_name", "retired_task_id"], [target.collection]
)
chunk_size = math.ceil(nmax / nproc)
if nproc > 1 and nmax <= chunk_size:
nproc = 1
logger.warning(
f"nmax = {nmax} but chunk size = {chunk_size} -> sequential parsing."
)
from multiprocessing_logging import install_mp_handler
install_mp_handler(logger=logger)
pool = multiprocessing.Pool(processes=nproc)
gen = VaspDirsGenerator()
iterator = iterator_slice(gen, chunk_size) # process in chunks
queue = deque()
count = 0
sep_tid = None
if task_ids:
with open(task_ids, "r") as f:
task_ids = json.load(f)
else:
# reserve list of task_ids to avoid collisions during multiprocessing
# insert empty doc with max ID + 1 into target collection for parallel SLURM jobs
# NOTE use regex first to reduce size of distinct below 16MB
q = {"task_id": {"$regex": r"^mp-\d{7,}$"}}
all_task_ids = [
t["task_id"] for t in target.collection.find(q, {"_id": 0, "task_id": 1})
]
if not all_task_ids:
all_task_ids = target.collection.distinct("task_id")
next_tid = max(int(tid.split("-")[-1]) for tid in all_task_ids) + 1
lst = [f"mp-{next_tid + n}" for n in range(nmax)]
task_ids = chunks(lst, chunk_size)
if run:
sep_tid = f"mp-{next_tid + nmax}"
target.collection.insert_one({"task_id": sep_tid})
logger.info(f"Inserted separator task with task_id {sep_tid}.")
logger.info(f"Reserved {len(lst)} task ID(s).")
else:
logger.info(f"Would reserve {len(lst)} task ID(s).")
sep_snlid = None
if snl_metas:
with open(snl_metas, "r") as f:
snl_metas = json.load(f)
# reserve list of snl_ids to avoid collisions during multiprocessing
# insert empty doc with max ID + 1 into target collection for parallel SLURM jobs
all_snl_ids = snl_collection.distinct("snl_id")
prefixes = set()
next_snlid = -1
for snlid in all_snl_ids:
prefix, index = snlid.split("-", 1)
index = int(index)
prefixes.add(prefix)
if index > next_snlid:
next_snlid = index
next_snlid += 1
prefix = prefixes.pop() # NOTE use the first prefix found
nsnls = len(snl_metas)
for n, launcher in enumerate(snl_metas):
snl_id = f"{prefix}-{next_snlid + n}"
snl_metas[launcher]["snl_id"] = snl_id
if run:
sep_snlid = f"{prefix}-{next_snlid + nsnls}"
snl_collection.insert({"snl_id": sep_snlid})
logger.info(f"Inserted separator SNL with snl_id {sep_snlid}.")
logger.info(f"Reserved {nsnls} SNL ID(s).")
else:
logger.info(f"Would reserve {nsnls} SNL ID(s).")
while iterator or queue:
try:
args = [next(iterator), tag, task_ids, snl_metas]
queue.append(pool.apply_async(parse_vasp_dirs, args))
except (StopIteration, TypeError):
iterator = None
while queue and (len(queue) >= pool._processes or not iterator):
process = queue.pop()
process.wait(1)
if not process.ready():
queue.append(process)
else:
count += process.get()
pool.close()
if run:
logger.info(
f"Successfully parsed and inserted {count}/{gen.value} tasks in {directory}."
)
if sep_tid:
target.collection.delete_one({"task_id": sep_tid})
logger.info(f"Removed separator task {sep_tid}.")
if sep_snlid:
snl_collection.remove({"snl_id": sep_snlid})
logger.info(f"Removed separator SNL {sep_snlid}.")
else:
logger.info(f"Would parse and insert {count}/{gen.value} tasks in {directory}.")
return ReturnCodes.SUCCESS if count and gen.value else ReturnCodes.WARNING