Skip to content

Commit 4148c19

Browse files
committed
add asv benchmarks
1 parent f039637 commit 4148c19

13 files changed

Lines changed: 933 additions & 0 deletions

‎benchmarks/README.md‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# dpctl ASV Benchmarks
2+
3+
Runtime-overhead benchmarks for [dpctl](https://github.com/IntelPython/dpctl)
4+
using [ASV](https://asv.readthedocs.io/en/stable/): object construction,
5+
queue caching, USM allocation, device enumeration, kernel bundle
6+
compilation, data movement, kernel submission. No compute throughput.
7+
8+
## Coverage
9+
10+
| File | API |
11+
|------|-----|
12+
| `bench_construct.py` | `SyclDevice`/`SyclContext`/`SyclQueue`/`SyclPlatform` construction, device/queue attribute reads |
13+
| `bench_queue_cache.py` | `get_device_cached_queue` for each key kind, vs. an uncached baseline |
14+
| `bench_usm.py` | `MemoryUSM{Device,Host,Shared}` alloc/free, aligned alloc, first touch, USM pointer queries |
15+
| `bench_enumerate.py` | `get_devices`, `get_num_devices`, `get_platforms`, `select_*_device`, `has_*_devices`, `select_device_with_aspects` |
16+
| `bench_compile.py` | Kernel bundles from SPIR-V / OpenCL C source / SYCL source, kernel lookup, availability probes |
17+
| `bench_copy.py` | `SyclQueue.memcpy`/`memcpy_async`/`fill`/`memset`, `_Memory.copy_to_host`/`copy_from_host`/`copy_from_device` |
18+
| `bench_submit.py` | `submit`, `submit_async`, batched submission, `submit_barrier`, idle `wait` |
19+
20+
## Device axis
21+
22+
Benchmarks parameterize over the `cpu`/`gpu` filter selectors and skip when a
23+
selector has no matching device, so the same benchmark names run on any
24+
node. Sizes over 25% of a device's `global_mem_size` skip the same way.
25+
26+
## Compilation caching
27+
28+
`benchmarks/__init__.py` disables the persistent JIT cache
29+
(`SYCL_CACHE_PERSISTENT=0`). `time_bundle_from_source_cold` mints a unique
30+
kernel name per call to defeat the in-memory cache too; `_warm` reuses one
31+
name to measure the cache-hit path.
32+
33+
`create_kernel_bundle_from_source` runs on the OpenCL backend only.
34+
`create_kernel_bundle_from_sycl_source` needs a device where
35+
`can_compile("sycl")` is true.
36+
37+
## Running
38+
39+
```bash
40+
pip install ".[benchmark]"
41+
cd benchmarks && asv machine --yes && asv run --python=same --quick HEAD^!
42+
```
43+
44+
One module: `asv run --python=same --quick --bench bench_compile HEAD^!`
45+
46+
Compare commits: `asv continuous --python=same HEAD~1 HEAD`
47+
48+
View results: `asv publish && asv preview`

‎benchmarks/asv.conf.json‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"version": 1,
3+
"project": "dpctl",
4+
"project_url": "https://github.com/IntelPython/dpctl",
5+
"show_commit_url": "https://github.com/IntelPython/dpctl/commit/",
6+
"repo": "..",
7+
"branches": [
8+
"master",
9+
"dev-milestone"
10+
],
11+
"environment_type": "conda",
12+
"conda_channels": [
13+
"https://software.repos.intel.com/python/conda/",
14+
"conda-forge"
15+
],
16+
"benchmark_dir": "benchmarks",
17+
"env_dir": ".asv/env",
18+
"results_dir": ".asv/results",
19+
"html_dir": ".asv/html",
20+
"build_cache_size": 2,
21+
"default_benchmark_timeout": 900,
22+
"regressions_thresholds": {
23+
".*": 0.2
24+
}
25+
}

‎benchmarks/benchmarks/__init__.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2026 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""ASV benchmarks for dpctl."""
18+
19+
import os
20+
21+
# Disable the persistent JIT cache before dpctl is imported, so cold-compile
22+
# benchmarks in bench_compile.py measure a real compile, not a cache hit.
23+
os.environ.setdefault("SYCL_CACHE_PERSISTENT", "0")

‎benchmarks/benchmarks/_utils.py‎

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2026 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Shared utilities for dpctl benchmarks.
18+
19+
Every benchmark that needs a device goes through :func:`queue_for` or
20+
:func:`device_for` so that benchmark names stay identical on every node in
21+
the pool: a node without a given device reports the parameter as skipped
22+
rather than failing the whole suite.
23+
"""
24+
25+
import os
26+
27+
from asv_runner.benchmarks.mark import SkipNotImplemented
28+
29+
import dpctl
30+
import dpctl.compiler as dpc
31+
32+
# Device axis. Filter-selector strings accepted by SyclQueue/SyclDevice.
33+
_SELECTORS = ["cpu", "gpu"]
34+
35+
# Allocation and transfer size sweep, in bytes: 4 KiB, 1 MiB, 16 MiB, 256 MiB.
36+
_SIZES = [4 * 1024, 1024**2, 16 * 1024**2, 256 * 1024**2]
37+
38+
# USM kinds, mapped to their constructors.
39+
_USM_TYPES = ["device", "host", "shared"]
40+
41+
# Fraction of device global memory a single benchmark allocation may claim.
42+
_MEM_BUDGET = 0.25
43+
44+
_queues = {}
45+
_devices = {}
46+
_spirv = None
47+
48+
49+
def queue_for(selector):
50+
"""Return a memoized queue for *selector*, or skip when unavailable."""
51+
if selector not in _queues:
52+
try:
53+
_queues[selector] = dpctl.SyclQueue(selector)
54+
except dpctl.SyclQueueCreationError:
55+
_queues[selector] = None
56+
q = _queues[selector]
57+
if q is None:
58+
raise SkipNotImplemented(f"no {selector} device available")
59+
return q
60+
61+
62+
def device_for(selector):
63+
"""Return a memoized device for *selector*, or skip when unavailable."""
64+
if selector not in _devices:
65+
try:
66+
_devices[selector] = dpctl.SyclDevice(selector)
67+
except dpctl.SyclDeviceCreationError:
68+
_devices[selector] = None
69+
d = _devices[selector]
70+
if d is None:
71+
raise SkipNotImplemented(f"no {selector} device available")
72+
return d
73+
74+
75+
def usm_class(usm_type):
76+
"""Return the dpctl.memory class allocating *usm_type* memory."""
77+
import dpctl.memory as dpm
78+
79+
return {
80+
"device": dpm.MemoryUSMDevice,
81+
"host": dpm.MemoryUSMHost,
82+
"shared": dpm.MemoryUSMShared,
83+
}[usm_type]
84+
85+
86+
def skip_unless_fits(queue, nbytes):
87+
"""Skip when *nbytes* exceeds this device's allocation budget."""
88+
budget = _MEM_BUDGET * queue.sycl_device.global_mem_size
89+
if nbytes > budget:
90+
raise SkipNotImplemented(
91+
f"{nbytes} bytes exceeds the device memory budget"
92+
)
93+
94+
95+
def opencl_queue_or_skip():
96+
"""Return a memoized OpenCL queue, or skip.
97+
98+
``create_kernel_bundle_from_source`` only supports the OpenCL backend.
99+
"""
100+
return queue_for("opencl")
101+
102+
103+
def sycl_source_queue_or_skip(selector):
104+
"""Return a queue whose device can compile SYCL source, or skip."""
105+
q = queue_for(selector)
106+
if not dpc.is_sycl_source_compilation_available():
107+
raise SkipNotImplemented("SYCL source compilation extension absent")
108+
if not q.sycl_device.can_compile("sycl"):
109+
raise SkipNotImplemented("device cannot compile SYCL source")
110+
return q
111+
112+
113+
def spirv_bytes():
114+
"""Return the SPIR-V module shipped with the installed dpctl, or skip.
115+
116+
Defines ``add(int*, int*, int*)`` and ``axpy(int*, int*, int*, int)``.
117+
"""
118+
global _spirv
119+
if _spirv is None:
120+
path = os.path.join(
121+
os.path.dirname(os.path.abspath(dpctl.__file__)),
122+
"tests",
123+
"input_files",
124+
"multi_kernel.spv",
125+
)
126+
if not os.path.exists(path):
127+
raise SkipNotImplemented(f"SPIR-V module not found at {path}")
128+
with open(path, "rb") as fh:
129+
_spirv = fh.read()
130+
return _spirv
131+
132+
133+
def ocl_axpy_source(kernel_name="axpy"):
134+
"""Return OpenCL C source for an axpy kernel called *kernel_name*."""
135+
return (
136+
f"kernel void {kernel_name}("
137+
" global int *a, global int *b, global int *c, int d) {"
138+
" size_t index = get_global_id(0);"
139+
" c[index] = d * a[index] + b[index];"
140+
"}"
141+
)
142+
143+
144+
def sycl_axpy_source(kernel_name="axpy"):
145+
"""Return SYCL source for an axpy kernel called *kernel_name*."""
146+
return f"""
147+
#include <sycl/sycl.hpp>
148+
149+
namespace syclext = sycl::ext::oneapi::experimental;
150+
151+
extern "C" SYCL_EXTERNAL
152+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclext::nd_range_kernel<1>))
153+
void {kernel_name}(int* a, int* b, int* c, int d) {{
154+
sycl::nd_item<1> item =
155+
sycl::ext::oneapi::this_work_item::get_nd_item<1>();
156+
size_t i = item.get_global_linear_id();
157+
c[i] = d * a[i] + b[i];
158+
}}
159+
"""
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2026 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Benchmarks for kernel bundle creation.
18+
19+
Cold and warm compilation are separate benchmarks. Cold gives every call a
20+
kernel name no compiler has seen, so neither the in-memory nor the
21+
persistent cache (disabled in ``benchmarks/__init__.py``) can serve it; warm
22+
re-submits identical source to measure the cache-hit path instead.
23+
"""
24+
25+
import itertools
26+
27+
import dpctl.compiler as dpc
28+
29+
from ._utils import (
30+
_SELECTORS,
31+
ocl_axpy_source,
32+
opencl_queue_or_skip,
33+
queue_for,
34+
spirv_bytes,
35+
sycl_axpy_source,
36+
sycl_source_queue_or_skip,
37+
)
38+
39+
40+
class BundleFromSPIRV:
41+
"""Kernel bundle from a pre-compiled SPIR-V module."""
42+
43+
params = [_SELECTORS]
44+
param_names = ["selector"]
45+
46+
def setup(self, selector):
47+
self.queue = queue_for(selector)
48+
self.spirv = spirv_bytes()
49+
self.bundle = dpc.create_kernel_bundle_from_spirv(
50+
self.queue, self.spirv
51+
)
52+
53+
def time_bundle_from_spirv(self, selector):
54+
dpc.create_kernel_bundle_from_spirv(self.queue, self.spirv)
55+
56+
def time_get_sycl_kernel(self, selector):
57+
self.bundle.get_sycl_kernel("axpy")
58+
59+
def time_has_sycl_kernel(self, selector):
60+
self.bundle.has_sycl_kernel("axpy")
61+
62+
63+
class BundleFromOpenCLSource:
64+
"""Kernel bundle built from OpenCL C source (OpenCL backend only)."""
65+
66+
timeout = 300
67+
number = 1
68+
repeat = 3
69+
warmup_time = 0
70+
71+
def setup(self):
72+
self.queue = opencl_queue_or_skip()
73+
self.counter = itertools.count()
74+
self.warm_source = ocl_axpy_source()
75+
dpc.create_kernel_bundle_from_source(self.queue, self.warm_source)
76+
77+
def time_bundle_from_source_cold(self):
78+
name = f"axpy_{next(self.counter)}"
79+
dpc.create_kernel_bundle_from_source(self.queue, ocl_axpy_source(name))
80+
81+
def time_bundle_from_source_warm(self):
82+
dpc.create_kernel_bundle_from_source(self.queue, self.warm_source)
83+
84+
85+
class BundleFromSYCLSource:
86+
"""Kernel bundle built from SYCL source via the kernel_compiler
87+
extension.
88+
89+
Skipped unless the extension is present and the device reports it can
90+
compile SYCL source.
91+
"""
92+
93+
params = [_SELECTORS]
94+
param_names = ["selector"]
95+
timeout = 600
96+
number = 1
97+
repeat = 2
98+
warmup_time = 0
99+
100+
def setup(self, selector):
101+
self.queue = sycl_source_queue_or_skip(selector)
102+
self.counter = itertools.count()
103+
self.warm_source = sycl_axpy_source()
104+
dpc.create_kernel_bundle_from_sycl_source(self.queue, self.warm_source)
105+
106+
def time_bundle_from_sycl_source_cold(self, selector):
107+
name = f"axpy_{next(self.counter)}"
108+
dpc.create_kernel_bundle_from_sycl_source(
109+
self.queue, sycl_axpy_source(name)
110+
)
111+
112+
def time_bundle_from_sycl_source_warm(self, selector):
113+
dpc.create_kernel_bundle_from_sycl_source(self.queue, self.warm_source)
114+
115+
116+
class SourceCompilationProbe:
117+
"""Cost of the availability probes themselves.
118+
119+
Called by consumers before every compilation attempt.
120+
"""
121+
122+
params = [_SELECTORS]
123+
param_names = ["selector"]
124+
125+
def setup(self, selector):
126+
self.device = queue_for(selector).sycl_device
127+
dpc.is_sycl_source_compilation_available()
128+
129+
def time_is_sycl_source_compilation_available(self, selector):
130+
dpc.is_sycl_source_compilation_available()
131+
132+
def time_can_compile(self, selector):
133+
self.device.can_compile("sycl")

0 commit comments

Comments
 (0)