Skip to content

Commit ad6bd26

Browse files
Added Cython example exercising dpctl.sycl
1 parent 78a858c commit ad6bd26

File tree

7 files changed

+215
-0
lines changed

7 files changed

+215
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
use_dpctl_sycl/_cython_api.cpp
2+
*~
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Example illustrating use of dpctl.sycl in Cython
2+
3+
Dpctl include `dpctl/sycl.pxd` file with incomplete definitions
4+
of SYCL runtime classes and conversion routines from SYCLInterface
5+
library opaque pointers to pointers to these SYCL classes.
6+
7+
This files simplifies usage of SYCL routines from Python extensions
8+
written in Cython.
9+
10+
## Building
11+
12+
```bash
13+
python setup.py develop
14+
```
15+
16+
## Testing
17+
18+
```bash
19+
python -m pytest tests
20+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2020-2022 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+
import Cython.Build
18+
import setuptools
19+
import setuptools.command.build_ext
20+
21+
import dpctl
22+
23+
24+
class custom_build_ext(
25+
setuptools.command.build_ext.build_ext, Cython.Build.build_ext
26+
):
27+
def build_extensions(self):
28+
self.compiler.set_executable("compiler_so", "icx -fsycl -fPIC")
29+
self.compiler.set_executable("compiler_cxx", "icpx -fsycl -fPIC")
30+
self.compiler.set_executable(
31+
"linker_so",
32+
"icpx -fsycl -shared -fpic -fsycl-device-code-split=per_kernel",
33+
)
34+
super().build_extensions()
35+
36+
37+
ext = setuptools.Extension(
38+
"use_dpctl_sycl._cython_api",
39+
["./use_dpctl_sycl/_cython_api.pyx"],
40+
include_dirs=[dpctl.get_include(), "./use_dpctl_sycl"],
41+
language="c++",
42+
)
43+
44+
setuptools.setup(
45+
name="use_dpctl_sycl",
46+
version="0.0.0",
47+
ext_modules=[ext],
48+
cmdclass={"build_ext": custom_build_ext},
49+
)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2020-2022 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+
import pytest
18+
import use_dpctl_sycl
19+
20+
import dpctl
21+
22+
23+
def test_device_name():
24+
try:
25+
d = dpctl.SyclDevice()
26+
except dpctl.SyclDeviceCreationError:
27+
pytest.skip("Could not create default device. Nothing to do")
28+
d_n = use_dpctl_sycl.device_name(d)
29+
assert d_n.decode("utf-8") == d.name
30+
31+
32+
def test_device_driver_version():
33+
try:
34+
d = dpctl.SyclDevice()
35+
except dpctl.SyclDeviceCreationError:
36+
pytest.skip("Could not create default device. Nothing to do")
37+
d_dv = use_dpctl_sycl.device_driver_version(d)
38+
assert d_dv.decode("utf-8") == d.driver_version
39+
40+
41+
def test_device_copy():
42+
try:
43+
d = dpctl.SyclDevice()
44+
except dpctl.SyclDeviceCreationError:
45+
pytest.skip("Could not create default device. Nothing to do")
46+
d_copy = use_dpctl_sycl.device_copy(d)
47+
assert d_copy == d
48+
assert d_copy.addressof_ref() != d.addressof_ref()
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 2020-2022 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+
from ._cython_api import device_copy, device_driver_version, device_name
18+
19+
__all__ = [
20+
"device_name",
21+
"device_driver_version",
22+
"device_copy",
23+
]
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-2022 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+
# distutils: language = c++
18+
# cython: language_level=3
19+
# cython: linetrace=True
20+
21+
cimport libcpp.string
22+
23+
cimport dpctl
24+
cimport dpctl.sycl
25+
26+
27+
cdef extern from "utils.hpp":
28+
cdef libcpp.string.string get_device_name(dpctl.sycl.device)
29+
cdef libcpp.string.string get_device_driver_version(dpctl.sycl.device)
30+
cdef dpctl.sycl.device *copy_device(dpctl.sycl.device)
31+
32+
33+
def device_name(dpctl.SyclDevice dev):
34+
cdef dpctl.DPCTLSyclDeviceRef d_ref = dev.get_device_ref()
35+
cdef const dpctl.sycl.device *dpcpp_device = dpctl.sycl.unwrap_device(d_ref)
36+
37+
return get_device_name(dpcpp_device[0])
38+
39+
40+
def device_driver_version(dpctl.SyclDevice dev):
41+
cdef dpctl.DPCTLSyclDeviceRef d_ref = dev.get_device_ref()
42+
cdef const dpctl.sycl.device *dpcpp_device = dpctl.sycl.unwrap_device(d_ref)
43+
44+
return get_device_driver_version(dpcpp_device[0])
45+
46+
47+
cpdef dpctl.SyclDevice device_copy(dpctl.SyclDevice dev):
48+
cdef dpctl.DPCTLSyclDeviceRef d_ref = dev.get_device_ref()
49+
cdef const dpctl.sycl.device *dpcpp_device = dpctl.sycl.unwrap_device(d_ref)
50+
cdef dpctl.sycl.device *copied_device = copy_device(dpcpp_device[0])
51+
cdef dpctl.DPCTLSyclDeviceRef copied_d_ref = dpctl.sycl.wrap_device(copied_device)
52+
53+
return dpctl.SyclDevice._create(copied_d_ref)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include <CL/sycl.hpp>
4+
#include <string>
5+
6+
std::string get_device_name(sycl::device d)
7+
{
8+
return d.get_info<sycl::info::device::name>();
9+
}
10+
11+
std::string get_device_driver_version(sycl::device d)
12+
{
13+
return d.get_info<sycl::info::device::driver_version>();
14+
}
15+
16+
sycl::device *copy_device(const sycl::device &d)
17+
{
18+
auto copy_ptr = new sycl::device(d);
19+
return copy_ptr;
20+
}

0 commit comments

Comments
 (0)