Skip to content

Commit 660ca1f

Browse files
Added example of C extension using DPCTLSyclInterface
1 parent b68b1e4 commit 660ca1f

File tree

5 files changed

+217
-0
lines changed

5 files changed

+217
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 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 ._py_sycl_ls import sycl_ls
18+
19+
__all__ = [
20+
"sycl_ls",
21+
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
from py_sycl_ls import sycl_ls
18+
19+
if __name__ == "__main__":
20+
sycl_ls()

examples/c/py_sycl_ls/setup.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 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 os.path
18+
import sysconfig
19+
20+
from setuptools import Extension, setup
21+
22+
import dpctl
23+
24+
setup(
25+
name="py_sycl_ls",
26+
version="0.0.1",
27+
description="An example of C extension calling SYCLInterface routines",
28+
long_description="""
29+
Example of using SYCLInterface.
30+
31+
See README.md for more details.
32+
""",
33+
license="Apache 2.0",
34+
author="Intel Corporation",
35+
url="https://github.com/IntelPython/dpctl",
36+
ext_modules=[
37+
Extension(
38+
name="py_sycl_ls._py_sycl_ls",
39+
sources=[
40+
"src/py_sycl-ls.c",
41+
],
42+
include_dirs=[
43+
dpctl.get_include(),
44+
os.path.join(sysconfig.get_paths()["include"], ".."),
45+
],
46+
library_dirs=[
47+
os.path.join(dpctl.get_include(), ".."),
48+
],
49+
libraries=["DPCTLSyclInterface"],
50+
runtime_library_dirs=[
51+
os.path.join(dpctl.get_include(), ".."),
52+
],
53+
extra_compile_args=[
54+
"-Wall",
55+
"-Wextra",
56+
],
57+
extra_link_args=["-fPIC"],
58+
language="c",
59+
)
60+
],
61+
)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//==- py_sycl-ls.c - Example of C extension working with -===//
2+
// DPCTLSyclInterface C-interface library.
3+
//
4+
// Data Parallel Control (dpctl)
5+
//
6+
// Copyright 2022 Intel Corporation
7+
//
8+
// Licensed under the Apache License, Version 2.0 (the "License");
9+
// you may not use this file except in compliance with the License.
10+
// You may obtain a copy of the License at
11+
//
12+
// http://www.apache.org/licenses/LICENSE-2.0
13+
//
14+
// Unless required by applicable law or agreed to in writing, software
15+
// distributed under the License is distributed on an "AS IS" BASIS,
16+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
// See the License for the specific language governing permissions and
18+
// limitations under the License.
19+
//
20+
//===----------------------------------------------------------------------===//
21+
///
22+
/// \file
23+
/// This file implements C Python extension using DPCTLSyclInterface library.
24+
///
25+
//===----------------------------------------------------------------------===//
26+
27+
// clang-format off
28+
#include "Python.h"
29+
#include "dpctl_capi.h"
30+
#include "syclinterface/dpctl_sycl_platform_interface.h"
31+
#include "syclinterface/dpctl_sycl_platform_manager.h"
32+
#include "syclinterface/dpctl_utils.h"
33+
// clang-format on
34+
35+
PyObject *sycl_ls(PyObject *self_unused, PyObject *args)
36+
{
37+
DPCTLPlatformVectorRef PVRef = NULL;
38+
size_t psz = 0;
39+
40+
(void)(self_unused); // avoid unused arguments warning
41+
(void)(args);
42+
PVRef = DPCTLPlatform_GetPlatforms();
43+
44+
if (PVRef) {
45+
psz = DPCTLPlatformVector_Size(PVRef);
46+
47+
for (size_t i = 0; i < psz; ++i) {
48+
DPCTLSyclPlatformRef PRef = DPCTLPlatformVector_GetAt(PVRef, i);
49+
const char *pl_info = DPCTLPlatformMgr_GetInfo(PRef, 2);
50+
51+
printf("Platform: %ld::\n%s\n", i, pl_info);
52+
53+
DPCTLCString_Delete(pl_info);
54+
DPCTLPlatform_Delete(PRef);
55+
}
56+
57+
DPCTLPlatformVector_Delete(PVRef);
58+
}
59+
60+
Py_RETURN_NONE;
61+
}
62+
63+
static PyMethodDef SyclLSMethods[] = {
64+
{"sycl_ls", sycl_ls, METH_NOARGS, "Output information about SYCL platform"},
65+
{NULL, NULL, 0, NULL} /* Sentinel */
66+
};
67+
68+
static struct PyModuleDef syclls_module = {
69+
PyModuleDef_HEAD_INIT,
70+
"_py_sycl_ls", /* name of module */
71+
"", /* module documentation, may be NULL */
72+
-1, /* size of per-interpreter state of the module,
73+
or -1 if the module keeps state in global variables. */
74+
SyclLSMethods,
75+
NULL,
76+
NULL,
77+
NULL,
78+
NULL};
79+
80+
PyMODINIT_FUNC PyInit__py_sycl_ls(void)
81+
{
82+
PyObject *m;
83+
84+
import_dpctl();
85+
86+
m = PyModule_Create(&syclls_module);
87+
88+
return m;
89+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 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 subprocess
18+
import sys
19+
20+
21+
def test_sycl_ls():
22+
r = subprocess.run(
23+
[sys.executable, "-m", "py_sycl_ls"], capture_output=True, check=True
24+
)
25+
assert r.stdout
26+
assert not r.stderr

0 commit comments

Comments
 (0)