Skip to content

Commit edd57cc

Browse files
modified sycl_buffer Cython example
1 parent 48dbf1f commit edd57cc

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

examples/cython/sycl_buffer/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ CC=clang CXX=dpcpp python setup.py build_ext --inplace
2121
#2 Running
2222

2323
```
24-
# SYCL_BE=PI_OPENCL sets SYCL backend to OpenCL to avoid a
24+
# SYCL_DEVICE_FILTER=opencl sets SYCL backend to OpenCL to avoid a
2525
# transient issue with MKL's using the default Level-0 backend
26-
(idp) [08:16:12 ansatnuc04 simple]$ SYCL_BE=PI_OPENCL ipython
26+
(idp) [08:16:12 ansatnuc04 simple]$ SYCL_DEVICE_FILTER=opencl ipython
2727
Python 3.7.7 (default, Jul 14 2020, 22:02:37)
2828
Type 'copyright', 'credits' or 'license' for more information
2929
IPython 7.17.0 -- An enhanced Interactive Python. Type '?' for help.
@@ -67,7 +67,7 @@ Times for NumPy
6767
Running run.py:
6868

6969
```
70-
(idp) [09:14:53 ansatnuc04 sycl_buffer]$ SYCL_BE=PI_OPENCL python run.py
70+
(idp) [09:14:53 ansatnuc04 sycl_buffer]$ SYCL_DEVICE_FILTER=opencl python run.py
7171
Result computed by NumPy
7272
[ 0.27170187 -23.36798583 7.31326489 -1.95121928]
7373
Result computed by SYCL extension

examples/cython/sycl_buffer/_buffer_example.pyx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@ cdef extern from "use_sycl_buffer.h":
2424
int c_columnwise_total(c_dpctl.DPCTLSyclQueueRef q, size_t n, size_t m, double *m, double *ct) nogil
2525
int c_columnwise_total_no_mkl(c_dpctl.DPCTLSyclQueueRef q, size_t n, size_t m, double *m, double *ct) nogil
2626

27-
def columnwise_total(double[:, ::1] v, method='mkl'):
27+
def columnwise_total(double[:, ::1] v, method='mkl', queue=None):
2828
cdef cnp.ndarray res_array = np.empty((v.shape[1],), dtype='d')
2929
cdef double[::1] res_memslice = res_array
3030
cdef int ret_status
3131
cdef c_dpctl.SyclQueue q
3232
cdef c_dpctl.DPCTLSyclQueueRef q_ref
3333

34-
q = c_dpctl.get_current_queue()
34+
if (queue is None):
35+
q = c_dpctl.SyclQueue()
36+
elif isinstance(queue, dpctl.SyclQueue):
37+
q = <c_dpctl.SyclQueue> queue
38+
else:
39+
q = c_dpctl.SyclQueue(queue)
3540
q_ref = q.get_queue_ref()
3641

3742
if method == 'mkl':

examples/cython/sycl_buffer/bench.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@
2424
print("=" * 10 + " Executing warm-up " + "=" * 10)
2525
print("NumPy result: ", X.sum(axis=0))
2626

27-
dpctl.set_global_queue("opencl:cpu")
27+
q = dpctl.SyclQueue("opencl:cpu")
2828
print(
2929
"SYCL({}) result: {}".format(
30-
dpctl.get_current_queue().sycl_device.name,
31-
sb.columnwise_total(X),
30+
q.sycl_device.name,
31+
sb.columnwise_total(X, queue=q),
3232
)
3333
)
3434

35-
dpctl.set_default_queue("opencl:gpu")
35+
q = dpctl.SyclQueue("opencl:gpu")
3636
print(
3737
"SYCL({}) result: {}".format(
38-
dpctl.get_current_queue().sycl_device.name,
39-
sb.columnwise_total(X),
38+
q.sycl_device.name,
39+
sb.columnwise_total(X, queue=q),
4040
)
4141
)
4242

@@ -45,9 +45,9 @@
4545
print("Times for 'opencl:cpu'")
4646
print(
4747
timeit.repeat(
48-
stmt="sb.columnwise_total(X)",
49-
setup='dpctl.set_global_queue("opencl:cpu"); '
50-
"sb.columnwise_total(X)", # ensure JIT compilation is not counted
48+
stmt="sb.columnwise_total(X, queue=q)",
49+
setup='q = dpctl.SyclQueue("opencl:cpu"); '
50+
"sb.columnwise_total(X, queue=q)", # ensure JIT compilation is not counted
5151
number=100,
5252
globals=globals(),
5353
)
@@ -56,8 +56,8 @@
5656
print("Times for 'opencl:gpu'")
5757
print(
5858
timeit.repeat(
59-
stmt="sb.columnwise_total(X)",
60-
setup='dpctl.set_default_queue("opencl:gpu"); sb.columnwise_total(X)',
59+
stmt="sb.columnwise_total(X, queue=q)",
60+
setup='q = dpctl.SyclQueue("opencl:gpu"); sb.columnwise_total(X, queue=q)',
6161
number=100,
6262
globals=globals(),
6363
)

examples/cython/sycl_buffer/run.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,24 @@
1616

1717
import syclbuffer as sb
1818
import numpy as np
19+
import dpctl
1920

2021
X = np.random.randn(100, 4)
2122

2223
print("Result computed by NumPy")
2324
print(X.sum(axis=0))
24-
print("Result computed by SYCL extension")
25+
print("Result computed by SYCL extension using default offloading target")
2526
print(sb.columnwise_total(X))
2627

2728

2829
print("")
30+
2931
# controlling where to offload
30-
import dpctl
3132

32-
with dpctl.device_context("opencl:gpu"):
33-
print("Running on: ", dpctl.get_current_queue().sycl_device.name)
34-
print(sb.columnwise_total(X))
33+
q = dpctl.SyclQueue("opencl:gpu")
34+
print("Running on: ", q.sycl_device.name)
35+
print(sb.columnwise_total(X, queue=q))
3536

36-
with dpctl.device_context("opencl:cpu"):
37-
print("Running on: ", dpctl.get_current_queue().sycl_device.name)
38-
print(sb.columnwise_total(X))
37+
q = dpctl.SyclQueue("opencl:cpu")
38+
print("Running on: ", q.sycl_device.name)
39+
print(sb.columnwise_total(X, queue=q))

0 commit comments

Comments
 (0)