diff --git a/docs/user-tutorial/benchmarks/micro-benchmarks.md b/docs/user-tutorial/benchmarks/micro-benchmarks.md index 4b385c10d..21548f17b 100644 --- a/docs/user-tutorial/benchmarks/micro-benchmarks.md +++ b/docs/user-tutorial/benchmarks/micro-benchmarks.md @@ -368,6 +368,23 @@ make NATIVE NATIVE_ARRAY_SIZE=400000000 cp streamNative ${SB_MICRO_PATH}/bin/ ``` +#### Core selection + +The `cores` parameter accepts either the existing space-separated core list (for example, `cores: 88 90 92`) +or a single compact `start,end,step` range: + +```yaml +parameters: + cpu_arch: native + numa_mem_nodes: 1 + cores: 88,151,1 +``` + +The end is inclusive when reached by the step: `88,151,1` selects cores 88 through 151 (64 threads), +while `88,151,2` selects 88, 90, 92, ..., 150 (32 threads). +Ranges require nonnegative core IDs, `start <= end`, and a positive step. Do not mix a range with explicit core IDs. +The same syntax works on the command line: `--cores 88,151,1`. The default core configuration is unchanged. + #### Metrics | Name | Unit | Description | diff --git a/superbench/benchmarks/micro_benchmarks/cpu_stream_performance.py b/superbench/benchmarks/micro_benchmarks/cpu_stream_performance.py index f8afe0eb4..65800f244 100644 --- a/superbench/benchmarks/micro_benchmarks/cpu_stream_performance.py +++ b/superbench/benchmarks/micro_benchmarks/cpu_stream_performance.py @@ -4,6 +4,7 @@ """Module for running the University of Virginia STREAM tool. It measures sustainable main memory \ bandwidth in MB/s and the corresponding computation rate for simple vector kernels.""" +import argparse import os from superbench.common.utils import logger @@ -11,6 +12,26 @@ from superbench.benchmarks.micro_benchmarks import MicroBenchmarkWithInvoke +class _CoreListAction(argparse.Action): + """Parse an explicit core list or a single inclusive start,end,step range.""" + def __call__(self, parser, namespace, values, option_string=None): + """Store the selected cores as a flat list of integers.""" + try: + if len(values) == 1 and ',' in values[0]: + start, end, step = map(int, values[0].split(',')) + if start < 0 or end < start or step <= 0: + raise ValueError('Invalid core range bounds or step.') + cores = list(range(start, end + 1, step)) + else: + cores = [int(value) for value in values] + except ValueError as error: + raise argparse.ArgumentError( + self, 'Expected space-separated core IDs or a single start,end,step range ' + 'with 0 <= start <= end and step > 0 (end inclusive).' + ) from error + setattr(namespace, self.dest, cores) + + class CpuStreamBenchmark(MicroBenchmarkWithInvoke): """The Stream benchmark class.""" def __init__(self, name, parameters=''): @@ -46,12 +67,14 @@ def add_parser_arguments(self): self._parser.add_argument( '--cores', nargs='+', - type=int, + type=str, + action=_CoreListAction, default=[ 0, 8, 16, 24, 32, 38, 44, 52, 60, 68, 76, 82, 88, 96, 104, 112, 120, 126, 132, 140, 148, 156, 164, 170 ], required=False, - help='List of cores to perform test. Default core configuration is for HBv4/Zen4 SKU offering. \ + help='Space-separated core IDs or a single start,end,step range (end inclusive, positive step). \ + Default core configuration is for HBv4/Zen4 SKU offering. \ For HBv3/Zen3 please see: ' + core_link ) diff --git a/tests/benchmarks/micro_benchmarks/test_cpu_stream_performance.py b/tests/benchmarks/micro_benchmarks/test_cpu_stream_performance.py index e46ec8bb0..28d618a31 100644 --- a/tests/benchmarks/micro_benchmarks/test_cpu_stream_performance.py +++ b/tests/benchmarks/micro_benchmarks/test_cpu_stream_performance.py @@ -25,6 +25,71 @@ def setUpClass(cls): cls.createMockFiles(cls, ['bin/streamNative']) return True + def test_stream_core_selection(self): + """Test explicit core lists and inclusive start,end,step ranges.""" + benchmark_name = 'cpu-stream' + (benchmark_class, + predefine_params) = BenchmarkRegistry._BenchmarkRegistry__select_benchmark(benchmark_name, Platform.CPU) + assert (benchmark_class) + + cases = [ + ('88,151,1', list(range(88, 152))), + ('88,151,2', list(range(88, 152, 2))), + ('88,150,2', list(range(88, 151, 2))), + ('88,88,1', [88]), + ('0,3,2', [0, 2]), + ('88,90,10', [88]), + ('88 90 92', [88, 90, 92]), + ('92 88 90', [92, 88, 90]), + ('88', [88]), + ] + for cores, expected in cases: + with self.subTest(cores=cores): + benchmark = benchmark_class( + benchmark_name, parameters='--cpu_arch native --numa_mem_nodes 1 --cores ' + cores + ) + assert (benchmark._preprocess() is True) + assert (benchmark.return_code == ReturnCode.SUCCESS) + assert (benchmark._args.cores == expected) + assert (len(benchmark._commands) == 1) + command = benchmark._commands[0].split() + assert ('OMP_NUM_THREADS={}'.format(len(expected)) in command) + assert ('OMP_PLACES=' + ','.join(f'{{{core}}}' for core in expected) in command) + assert ('numactl -m1' in benchmark._commands[0]) + assert ('streamNative' in benchmark._commands[0]) + + def test_stream_default_cores(self): + """Test that the default core configuration is unchanged.""" + benchmark_name = 'cpu-stream' + (benchmark_class, + predefine_params) = BenchmarkRegistry._BenchmarkRegistry__select_benchmark(benchmark_name, Platform.CPU) + assert (benchmark_class) + + benchmark = benchmark_class(benchmark_name) + assert (benchmark._preprocess() is True) + assert ( + benchmark._args.cores == [ + 0, 8, 16, 24, 32, 38, 44, 52, 60, 68, 76, 82, 88, 96, 104, 112, 120, 126, 132, 140, 148, 156, 164, 170 + ] + ) + + def test_stream_invalid_cores(self): + """Test that malformed core selections fail argument validation.""" + benchmark_name = 'cpu-stream' + (benchmark_class, + predefine_params) = BenchmarkRegistry._BenchmarkRegistry__select_benchmark(benchmark_name, Platform.CPU) + assert (benchmark_class) + + for cores in [ + '', '88,151', '88,151,1,2', '88,,1', 'a,151,1', '88,151,a', '88,151,0', '88,151,-1', '151,88,1', '-1,151,1', + '88,151,1 152', '88 90,100,2', '88 90 foo' + ]: + with self.subTest(cores=cores): + benchmark = benchmark_class(benchmark_name, parameters='--cores ' + cores) + assert (benchmark._preprocess() is False) + assert (benchmark.return_code == ReturnCode.INVALID_ARGUMENT) + assert (not benchmark._commands) + @decorator.load_data('tests/data/streamResultZen.log') def test_stream_zen(self, results): """Test STREAM benchmark command generation."""