|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import time |
| 6 | +from collections.abc import Iterable |
| 7 | + |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +import setigen as stg |
| 11 | + |
| 12 | + |
| 13 | +def _parse_counts(raw_counts: str) -> list[int]: |
| 14 | + """Parse a comma-separated list of selected coarse-channel counts.""" |
| 15 | + return [int(item.strip()) for item in raw_counts.split(",") if item.strip()] |
| 16 | + |
| 17 | + |
| 18 | +def _time_call(repeats: int, func: object) -> float: |
| 19 | + """Return the best runtime across repeated calls.""" |
| 20 | + timings = [] |
| 21 | + for _ in range(repeats): |
| 22 | + start = time.perf_counter() |
| 23 | + func() |
| 24 | + timings.append(time.perf_counter() - start) |
| 25 | + return min(timings) |
| 26 | + |
| 27 | + |
| 28 | +def run_benchmark( |
| 29 | + *, |
| 30 | + num_taps: int, |
| 31 | + num_branches: int, |
| 32 | + windows: int, |
| 33 | + counts: Iterable[int], |
| 34 | + repeats: int, |
| 35 | + seed: int, |
| 36 | +) -> None: |
| 37 | + """Benchmark full-FFT slicing against selected-bin PFB channelization.""" |
| 38 | + rng = np.random.default_rng(seed) |
| 39 | + samples = rng.standard_normal(num_taps * num_branches * windows) |
| 40 | + |
| 41 | + print( |
| 42 | + "backend={backend} taps={taps} branches={branches} windows={windows} repeats={repeats}".format( |
| 43 | + backend="cupy" if os.getenv("SETIGEN_ENABLE_GPU") == "1" else "numpy", |
| 44 | + taps=num_taps, |
| 45 | + branches=num_branches, |
| 46 | + windows=windows, |
| 47 | + repeats=repeats, |
| 48 | + ) |
| 49 | + ) |
| 50 | + print("channels,full_seconds,selected_seconds,speedup") |
| 51 | + |
| 52 | + for count in counts: |
| 53 | + full_filterbank = stg.voltage.PolyphaseFilterbank( |
| 54 | + num_taps=num_taps, |
| 55 | + num_branches=num_branches, |
| 56 | + ) |
| 57 | + selected_filterbank = stg.voltage.PolyphaseFilterbank( |
| 58 | + num_taps=num_taps, |
| 59 | + num_branches=num_branches, |
| 60 | + ) |
| 61 | + |
| 62 | + full_seconds = _time_call( |
| 63 | + repeats, |
| 64 | + lambda: full_filterbank.channelize( |
| 65 | + samples, |
| 66 | + cache=False, |
| 67 | + start_chan=0, |
| 68 | + num_chans=count, |
| 69 | + method="full", |
| 70 | + ), |
| 71 | + ) |
| 72 | + selected_seconds = _time_call( |
| 73 | + repeats, |
| 74 | + lambda: selected_filterbank.channelize( |
| 75 | + samples, |
| 76 | + cache=False, |
| 77 | + start_chan=0, |
| 78 | + num_chans=count, |
| 79 | + method="selected", |
| 80 | + ), |
| 81 | + ) |
| 82 | + speedup = full_seconds / selected_seconds if selected_seconds else float("inf") |
| 83 | + print(f"{count},{full_seconds:.8f},{selected_seconds:.8f},{speedup:.3f}") |
| 84 | + |
| 85 | + |
| 86 | +def main() -> None: |
| 87 | + """Run the selected-bin voltage benchmark.""" |
| 88 | + parser = argparse.ArgumentParser(description=__doc__) |
| 89 | + parser.add_argument("--num-taps", type=int, default=8) |
| 90 | + parser.add_argument("--num-branches", type=int, default=1024) |
| 91 | + parser.add_argument("--windows", type=int, default=16) |
| 92 | + parser.add_argument("--counts", default="1,2,4,8,16,64") |
| 93 | + parser.add_argument("--repeats", type=int, default=5) |
| 94 | + parser.add_argument("--seed", type=int, default=0) |
| 95 | + args = parser.parse_args() |
| 96 | + |
| 97 | + run_benchmark( |
| 98 | + num_taps=args.num_taps, |
| 99 | + num_branches=args.num_branches, |
| 100 | + windows=args.windows, |
| 101 | + counts=_parse_counts(args.counts), |
| 102 | + repeats=args.repeats, |
| 103 | + seed=args.seed, |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + main() |
0 commit comments