Skip to content

Commit 1e45e4c

Browse files
Add the SyclTimer class to the examples
1 parent dd3d7c0 commit 1e45e4c

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

examples/python/dppy_kernel.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2020-2021 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+
18+
import numba_dppy as dppy
19+
import numpy as np
20+
from sycl_timer import SyclTimer
21+
22+
import dpctl
23+
24+
25+
@dppy.kernel
26+
def dppy_gemm(a, b, c):
27+
i = dppy.get_global_id(0)
28+
j = dppy.get_global_id(1)
29+
if i >= c.shape[0] or j >= c.shape[1]:
30+
return
31+
c[i, j] = 0
32+
for k in range(c.shape[0]):
33+
c[i, j] += a[i, k] * b[k, j]
34+
35+
36+
X = 1024
37+
Y = 16
38+
global_size = X, X
39+
40+
griddim = X, X
41+
blockdim = Y, Y
42+
43+
a = np.arange(X * X, dtype=np.float32).reshape(X, X)
44+
b = np.array(np.random.random(X * X), dtype=np.float32).reshape(X, X)
45+
c = np.ones_like(a).reshape(X, X)
46+
47+
q = dpctl.SyclQueue("opencl:gpu", property="enable_profiling")
48+
with dpctl.device_context(q):
49+
timers = SyclTimer(time_scale=1)
50+
with timers(q):
51+
dppy_gemm[griddim, blockdim](a, b, c)
52+
host_time, device_time = timers.dt()
53+
print("Wall time: ", host_time, "\n", "Device time: ", device_time)

examples/python/sycl_timer.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2020-2021 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+
18+
import timeit
19+
20+
import dpctl
21+
22+
23+
class SyclTimer:
24+
def __init__(self, host_time=timeit.default_timer, time_scale=1):
25+
self.timer = host_time
26+
self.time_scale = time_scale
27+
28+
def __call__(self, queue=None):
29+
if isinstance(queue, dpctl.SyclQueue):
30+
if queue.has_enable_profiling:
31+
self.queue = queue
32+
else:
33+
raise ValueError(
34+
"The queue does not contain the enable_profiling property"
35+
)
36+
else:
37+
raise ValueError(
38+
"The passed queue must be <class 'dpctl._sycl_queue.SyclQueue'>"
39+
)
40+
return self.__enter__()
41+
42+
def __enter__(self):
43+
self.event_start = dpctl.SyclEventRaw(self.queue.submit_barrier())
44+
self.host_start = self.timer()
45+
return self
46+
47+
def __exit__(self, *args):
48+
self.event_finish = dpctl.SyclEventRaw(self.queue.submit_barrier())
49+
self.host_finish = self.timer()
50+
51+
def dt(self):
52+
self.event_start.wait()
53+
self.event_finish.wait()
54+
return (
55+
(self.host_finish - self.host_start) * self.time_scale,
56+
(
57+
self.event_finish.profiling_info_start
58+
- self.event_start.profiling_info_end
59+
)
60+
/ 1e9
61+
* self.time_scale,
62+
)

0 commit comments

Comments
 (0)