|
| 1 | +############################################################### |
| 2 | +# Copyright 2025 Lawrence Livermore National Security, LLC |
| 3 | +# (c.f. AUTHORS, NOTICE.LLNS, COPYING) |
| 4 | +# |
| 5 | +# This file is part of the Flux resource manager framework. |
| 6 | +# For details, see https://github.com/flux-framework. |
| 7 | +# |
| 8 | +# SPDX-License-Identifier: LGPL-3.0 |
| 9 | +############################################################### |
| 10 | + |
| 11 | +import argparse |
| 12 | +import logging |
| 13 | +import os |
| 14 | +import re |
| 15 | +import shlex |
| 16 | +import sys |
| 17 | + |
| 18 | +import flux |
| 19 | +from flux.idset import IDset |
| 20 | +from flux.util import CLIMain |
| 21 | + |
| 22 | + |
| 23 | +class MultiProgLine: |
| 24 | + """Class representing a single "multi-prog" config line""" |
| 25 | + |
| 26 | + def __init__(self, value, lineno=-1): |
| 27 | + self.ranks = IDset() |
| 28 | + self.all = False |
| 29 | + self.args = [] |
| 30 | + self.lineno = lineno |
| 31 | + lexer = shlex.shlex(value, posix=True, punctuation_chars=True) |
| 32 | + lexer.whitespace_split = True |
| 33 | + lexer.escapedquotes = "\"'" |
| 34 | + try: |
| 35 | + args = list(lexer) |
| 36 | + except ValueError as exc: |
| 37 | + raise ValueError(f"line {lineno}: '{value.rstrip()}': {exc}") from None |
| 38 | + if not args: |
| 39 | + return |
| 40 | + |
| 41 | + targets = args.pop(0) |
| 42 | + if targets == "*": |
| 43 | + self.all = True |
| 44 | + else: |
| 45 | + try: |
| 46 | + self.ranks = IDset(targets) |
| 47 | + except ValueError: |
| 48 | + raise ValueError(f"line {lineno}: invalid idset: {targets}") from None |
| 49 | + |
| 50 | + self.args = args |
| 51 | + |
| 52 | + def get_args(self, rank): |
| 53 | + """Return the arguments list with %t and %o substituted for `rank`""" |
| 54 | + |
| 55 | + result = [] |
| 56 | + index = 0 |
| 57 | + if not self.all: |
| 58 | + index = self.ranks.expand().index(rank) |
| 59 | + sub = {"%t": str(rank), "%o": str(index)} |
| 60 | + for arg in self.args: |
| 61 | + result.append(re.sub(r"(%t)|(%o)", lambda x: sub[x.group(0)], arg)) |
| 62 | + return result |
| 63 | + |
| 64 | + def __bool__(self): |
| 65 | + return bool(self.args) |
| 66 | + |
| 67 | + |
| 68 | +class MultiProg: |
| 69 | + """Class representing an entire "multi-prog" config file""" |
| 70 | + |
| 71 | + def __init__(self, inputfile): |
| 72 | + self.fp = inputfile |
| 73 | + self.lines = [] |
| 74 | + self.fallthru = None |
| 75 | + lineno = 0 |
| 76 | + for line in self.fp: |
| 77 | + lineno += 1 |
| 78 | + try: |
| 79 | + mpline = MultiProgLine(line, lineno) |
| 80 | + except ValueError as exc: |
| 81 | + raise ValueError(f"{self.fp.name}: {exc}") from None |
| 82 | + if mpline: |
| 83 | + if mpline.all: |
| 84 | + self.fallthru = mpline |
| 85 | + else: |
| 86 | + self.lines.append(mpline) |
| 87 | + |
| 88 | + def find(self, rank): |
| 89 | + """Return line matching 'rank' in the current config""" |
| 90 | + for line in self.lines: |
| 91 | + if rank in line.ranks: |
| 92 | + return line |
| 93 | + if self.fallthru is not None: |
| 94 | + return self.fallthru |
| 95 | + raise ValueError(f"{self.fp.name}: No matching line for rank {rank}") |
| 96 | + |
| 97 | + def exec(self, rank, dry_run=False): |
| 98 | + """Exec configured command line arguments for a task rank""" |
| 99 | + args = self.find(rank).get_args(rank) |
| 100 | + if dry_run: |
| 101 | + args = " ".join(shlex.quote(arg) for arg in args) |
| 102 | + print(f"{rank}: {args}") |
| 103 | + else: |
| 104 | + os.execvp(args[0], args) |
| 105 | + |
| 106 | + |
| 107 | +def parse_args(): |
| 108 | + description = """ |
| 109 | + Run a parallel program with a different executable and arguments for each task |
| 110 | + """ |
| 111 | + parser = argparse.ArgumentParser( |
| 112 | + prog="flux-multi-prog", |
| 113 | + usage="flux multi-prog [OPTIONS] CONFIG", |
| 114 | + description=description, |
| 115 | + formatter_class=flux.util.help_formatter(), |
| 116 | + ) |
| 117 | + parser.add_argument( |
| 118 | + "-n", |
| 119 | + "--dry-run", |
| 120 | + type=IDset, |
| 121 | + metavar="IDS", |
| 122 | + help="Do not run anything. Instead, print what would be run for" |
| 123 | + + " each rank in IDS", |
| 124 | + ) |
| 125 | + parser.add_argument( |
| 126 | + "conf", metavar="CONFIG", type=str, help="multi-prog configuration file" |
| 127 | + ) |
| 128 | + return parser.parse_args() |
| 129 | + |
| 130 | + |
| 131 | +LOGGER = logging.getLogger("flux-multi-prog") |
| 132 | + |
| 133 | + |
| 134 | +@CLIMain(LOGGER) |
| 135 | +def main(): |
| 136 | + |
| 137 | + sys.stdout = open(sys.stdout.fileno(), "w", encoding="utf8") |
| 138 | + |
| 139 | + args = parse_args() |
| 140 | + |
| 141 | + with open(args.conf) as infile: |
| 142 | + mp = MultiProg(infile) |
| 143 | + |
| 144 | + if args.dry_run: |
| 145 | + for rank in args.dry_run: |
| 146 | + mp.exec(rank, dry_run=True) |
| 147 | + sys.exit(0) |
| 148 | + |
| 149 | + try: |
| 150 | + rank = int(os.getenv("FLUX_TASK_RANK")) |
| 151 | + except TypeError: |
| 152 | + raise ValueError("FLUX_TASK_RANK not found or invalid") |
| 153 | + |
| 154 | + mp.exec(rank) |
0 commit comments