Skip to content

Commit 2e2e35d

Browse files
Merge pull request #981 from IntelPython/dpctl-sycl-pxd
Introduce dpctl/sycl.pxd
2 parents 3d04af8 + 73a39c1 commit 2e2e35d

File tree

10 files changed

+293
-1
lines changed

10 files changed

+293
-1
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ per-file-ignores =
3232
examples/cython/sycl_buffer/_buffer_example.pyx: E999, E225, E402
3333
examples/cython/sycl_direct_linkage/_buffer_example.pyx: E999, E225, E402
3434
examples/cython/usm_memory/blackscholes.pyx: E999, E225, E226, E402
35+
examples/cython/use_dpctl_sycl/use_dpctl_sycl/_cython_api.pyx: E999, E225, E226, E402

.github/workflows/conda-package.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,12 @@ jobs:
473473
CC=dpcpp CXX=dpcpp LDSHARED="dpcpp -shared" \
474474
python setup.py build_ext --inplace || exit 1
475475
conda deactivate
476-
LD_LIBRARY_PATH=${CONDA_PREFIX}/lib python run.py || exit 1
476+
if [ -e tests ]
477+
then
478+
LD_LIBRARY_PATH=${CONDA_PREFIX}/lib python -m pytest tests || exit 1
479+
else
480+
LD_LIBRARY_PATH=${CONDA_PREFIX}/lib python run.py || exit 1
481+
fi
477482
popd
478483
done
479484
cd ../c

dpctl/sycl.pxd

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright 2022 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# distutils: language = c++
16+
# cython: language_level=3
17+
18+
# SYCL static imports for Cython
19+
20+
from . cimport _backend as dpctl_backend
21+
22+
23+
cdef extern from "CL/sycl.hpp" namespace "sycl":
24+
cdef cppclass queue "sycl::queue":
25+
pass
26+
27+
cdef cppclass device "sycl::device":
28+
pass
29+
30+
cdef cppclass context "sycl::context":
31+
pass
32+
33+
cdef cppclass event "sycl::event":
34+
pass
35+
36+
cdef cppclass kernel "sycl::kernel":
37+
pass
38+
39+
cdef cppclass executable_kernel_bundle \
40+
"sycl::kernel_bundle<sycl::bundle_state::executable>":
41+
pass
42+
43+
cdef extern from "syclinterface/dpctl_sycl_type_casters.hpp" \
44+
namespace "dpctl::syclinterface":
45+
# queue
46+
cdef dpctl_backend.DPCTLSyclQueueRef wrap_queue \
47+
"dpctl::syclinterface::wrap<sycl::queue>" (const queue *)
48+
cdef queue * unwrap_queue "dpctl::syclinterface::unwrap<sycl::queue>" (
49+
dpctl_backend.DPCTLSyclQueueRef)
50+
51+
# device
52+
cdef dpctl_backend.DPCTLSyclDeviceRef wrap_device \
53+
"dpctl::syclinterface::wrap<sycl::device>" (const device *)
54+
cdef device * unwrap_device "dpctl::syclinterface::unwrap<sycl::device>" (
55+
dpctl_backend.DPCTLSyclDeviceRef)
56+
57+
# context
58+
cdef dpctl_backend.DPCTLSyclContextRef wrap_context \
59+
"dpctl::syclinterface::wrap<sycl::context>" (const context *)
60+
cdef context * unwrap_context "dpctl::syclinterface::unwrap<sycl::context>" (
61+
dpctl_backend.DPCTLSyclContextRef)
62+
63+
# event
64+
cdef dpctl_backend.DPCTLSyclEventRef wrap_event \
65+
"dpctl::syclinterface::wrap<sycl::event>" (const event *)
66+
cdef event * unwrap_event "dpctl::syclinterface::unwrap<sycl::event>" (
67+
dpctl_backend.DPCTLSyclEventRef)
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: 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+
import Cython.Build
18+
import setuptools
19+
from setuptools.command.build_ext import build_ext as build_ext_base
20+
21+
import dpctl
22+
23+
24+
class custom_build_ext(build_ext_base):
25+
def build_extensions(self):
26+
self.compiler.set_executable("compiler_so", "icx -fsycl -fPIC")
27+
self.compiler.set_executable("compiler_cxx", "icpx -fsycl -fPIC")
28+
self.compiler.set_executable(
29+
"linker_so",
30+
"icpx -fsycl -shared -fpic -fsycl-device-code-split=per_kernel",
31+
)
32+
super().build_extensions()
33+
34+
35+
ext = setuptools.Extension(
36+
"use_dpctl_sycl._cython_api",
37+
["./use_dpctl_sycl/_cython_api.pyx"],
38+
include_dirs=[dpctl.get_include(), "./use_dpctl_sycl"],
39+
language="c++",
40+
)
41+
42+
(cythonized_ext,) = Cython.Build.cythonize(
43+
[
44+
ext,
45+
]
46+
)
47+
48+
setuptools.setup(
49+
name="use_dpctl_sycl",
50+
version="0.0.0",
51+
ext_modules=[cythonized_ext],
52+
cmdclass={"build_ext": custom_build_ext},
53+
)
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)