Skip to content
This repository was archived by the owner on Sep 26, 2025. It is now read-only.

Commit 2cd4743

Browse files
Update build system/tests for extension to run on Iris Xe
1 parent ff05ded commit 2cd4743

File tree

10 files changed

+82
-11
lines changed

10 files changed

+82
-11
lines changed

common_ext/cy_kde.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def kde_eval(
5757
raise ValueError("Evaluation and observation data must be non-empty")
5858

5959
if cython.floating is float:
60-
f = np.empty(x.shape[0], dtype=np.float)
60+
f = np.empty(x.shape[0], dtype=np.single)
6161
else:
6262
f = np.empty(x.shape[0], dtype=np.double)
6363

common_src/kde.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <CL/sycl.hpp>
1818
#include <cstdint>
19+
#include <iostream>
1920

2021
namespace example {
2122

@@ -66,14 +67,20 @@ kernel_density_estimate(
6667
sycl::buffer<T, 1> buf_F(f, sycl::range<1>(n), buf_props);
6768

6869
// initialize buffer of function values with zeros
70+
try {
6971
q.submit(
7072
[&](sycl::handler &cgh) {
7173
sycl::accessor acc_F(buf_F, cgh, sycl::write_only, sycl::no_init);
7274
cgh.fill(acc_F, T(0));
7375
});
76+
} catch (const std::exception &e){
77+
std::cout << e.what() << std::endl;
78+
std::rethrow_exception(std::current_exception());
79+
}
7480

7581
// populate function values buffer
7682
// perform 2D loop in parallel
83+
try{
7784
q.submit(
7885
[&](sycl::handler &cgh) {
7986
sycl::accessor acc_X(buf_X, cgh, sycl::read_only);
@@ -135,6 +142,10 @@ kernel_density_estimate(
135142
}
136143
});
137144
});
145+
} catch (const std::exception &e) {
146+
std::cout << e.what() << std::endl;
147+
std::rethrow_exception(std::current_exception());
148+
}
138149

139150
return;
140151
}

common_src/kmeans_draft.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include <CL/sycl.hpp>
1919
#include <limits>
20-
#include "kmeans.hpp"
20+
#include <cstdint>
2121

2222
namespace example {
2323

@@ -49,17 +49,17 @@ kmeans_host_data(
4949
unsigned int *labels // optional pointer to array where to give centroid assignments
5050
)
5151
{
52-
const sycl::property_list buf_props = {sycl::property::buffer::use_host_ptr()};
52+
const sycl::property_list buf_props {sycl::property::buffer::use_host_ptr()};
5353
sycl::buffer<T, 2> buf_X(data, sycl::range<2>(n, dim), buf_props);
5454
sycl::buffer<T, 2> buf_C(centroids, sycl::range<2>(k, dim), buf_props);
5555

56-
// temporary space for new centroids
56+
// temporary space for new centroid coordinaties
5757
sycl::buffer<T, 2> buf_Cn{ sycl::range<2>(k, dim) };
58+
// temporary space for point labels (index of centroid it is closest to)
59+
sycl::buffer<unsigned int, 1> buf_L {sycl::range<1>(n)};
5860
// temporary space for new centroid sizes
5961
sycl::buffer<size_t, 1> buf_Cs{ sycl::range<1>(k) };
6062

61-
sycl::buffer<unsigned int, 1> buf_L {sycl::range<1>(n)};
62-
6363
if (labels != nullptr) {
6464
buf_L = sycl::buffer<unsigned int, 1>(labels, sycl::range<1>(n), buf_props);
6565
}
@@ -71,7 +71,7 @@ kmeans_host_data(
7171
[&](sycl::handler &cgh) {
7272

7373
// this kernel only reads data-points and centroids
74-
// hence no reason to copy data from device to host
74+
// hence no reason to copy back data from device to host
7575
sycl::accessor acc_X(buf_X, cgh, sycl::read_only);
7676
sycl::accessor acc_C(buf_C, cgh, sycl::read_only);
7777
// it only assigns labels and overwrites the buffer completely

kde_setuptools/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Assuming `Cython` and `pybind11` are installed and DCP++ has been activated:
44

55
```bash
6-
CC=dpcpp LDSHARED="dpcpp --shared" python setup.py develop
6+
python setup.py develop
77
pytest -m tests
88
```
99

kde_setuptools/setup.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616
from pybind11.setup_helpers import Pybind11Extension
1717
import dpctl
1818

19+
from setuptools.command.build_ext import build_ext
20+
21+
class custom_build_ext(build_ext):
22+
def build_extensions(self):
23+
self.compiler.set_executable("compiler_so", "dpcpp -fPIC")
24+
self.compiler.set_executable("compiler_cxx", "dpcpp -fPIC")
25+
self.compiler.set_executable("linker_so", "dpcpp -shared -fpic -fsycl-device-code-split=per_kernel")
26+
build_ext.build_extensions(self)
27+
1928
ext_modules = [
2029
Pybind11Extension(
2130
"kde_setuptools._pybind11_kde",
@@ -43,5 +52,6 @@
4352
""",
4453
license="Apache 2.0",
4554
url="https://intelpython.github.io/oneAPI-for-SciPy",
46-
ext_modules=ext_modules
55+
ext_modules=ext_modules,
56+
cmdclass={"build_ext": custom_build_ext}
4757
)

kde_setuptools/tests/test_cy_kde.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def test_1d():
4141
x = np.linspace(0.1, 0.9, num=15).reshape((-1, 1))
4242
data = np.random.rand(128,1)
4343

44+
if not q.sycl_device.has_aspect_fp64:
45+
x = np.asarray(x, dtype=np.float32)
46+
data = np.asarray(data, dtype=np.float32)
47+
4448
f_cy = kde_eval(q, x, data, 0.1)
4549
f_ref = ref_kde(x, data, 0.1)
4650

@@ -62,6 +66,10 @@ def test_2d():
6266

6367
data = np.random.rand(128*128, 2)
6468

69+
if not q.sycl_device.has_aspect_fp64:
70+
x = np.asarray(x, dtype=np.float32)
71+
data = np.asarray(data, dtype=np.float32)
72+
6573
f_cy = kde_eval(q, x, data, 0.05)
6674
f_ref = ref_kde(x, data, 0.05)
6775

@@ -84,6 +92,10 @@ def test_3d():
8492

8593
data = np.random.rand(16000,3)
8694

95+
if not q.sycl_device.has_aspect_fp64:
96+
x = np.asarray(x, dtype=np.float32)
97+
data = np.asarray(data, dtype=np.float32)
98+
8799
f_cy = kde_eval(q, x, data, 0.01)
88100
f_ref = ref_kde(x, data, 0.01)
89101

kde_setuptools/tests/test_py_kde.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def test_1d():
4141
x = np.linspace(0.1, 0.9, num=15).reshape((-1, 1))
4242
data = np.random.rand(128,1)
4343

44+
if not q.sycl_device.has_aspect_fp64:
45+
x = np.asarray(x, dtype=np.float32)
46+
data = np.asarray(data, dtype=np.float32)
47+
4448
f_cy = kde_eval(q, x, data, 0.1)
4549
f_ref = ref_kde(x, data, 0.1)
4650

@@ -62,6 +66,10 @@ def test_2d():
6266

6367
data = np.random.rand(128*128, 2)
6468

69+
if not q.sycl_device.has_aspect_fp64:
70+
x = np.asarray(x, dtype=np.float32)
71+
data = np.asarray(data, dtype=np.float32)
72+
6573
f_cy = kde_eval(q, x, data, 0.05)
6674
f_ref = ref_kde(x, data, 0.05)
6775

@@ -84,6 +92,10 @@ def test_3d():
8492

8593
data = np.random.rand(16000,3)
8694

95+
if not q.sycl_device.has_aspect_fp64:
96+
x = np.asarray(x, dtype=np.float32)
97+
data = np.asarray(data, dtype=np.float32)
98+
8799
f_cy = kde_eval(q, x, data, 0.01)
88100
f_ref = ref_kde(x, data, 0.01)
89101

kde_skbuild/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pybind11_add_module(${py_module_name}
4141
MODULE
4242
${CMAKE_SOURCE_DIR}/src/pybind11_kde.cpp
4343
)
44+
target_link_options(${py_module_name} PRIVATE -fsycl-device-code-split=per_kernel)
4445
target_include_directories(${py_module_name}
4546
PUBLIC ${CMAKE_SOURCE_DIR}/src ${Dpctl_INCLUDE_DIR}
4647
)
@@ -57,6 +58,7 @@ add_cython_target(${cy_module_name} ${CMAKE_SOURCE_DIR}/src/_cython_kde.pyx CXX
5758
add_library(${cy_module_name} MODULE ${_generated_src})
5859

5960
target_include_directories(${cy_module_name} PRIVATE ${NumPy_INCLUDE_DIR} ${Dpctl_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/src)
61+
target_link_options(${cy_module_name} PRIVATE -fsycl-device-code-split=per_kernel)
6062
python_extension_module(${cy_module_name})
6163
install(TARGETS ${cy_module_name} LIBRARY DESTINATION kde_skbuild)
6264

kde_skbuild/tests/test_cy_kde.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2020-2021 Intel Corporation
1+
# Copyright 2022 Intel Corporation
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -41,6 +41,10 @@ def test_1d():
4141
x = np.linspace(0.1, 0.9, num=15).reshape((-1, 1))
4242
data = np.random.rand(128,1)
4343

44+
if not q.sycl_device.has_aspect_fp64:
45+
x = np.asarray(x, dtype=np.float32)
46+
data = np.asarray(data, dtype=np.float32)
47+
4448
f_cy = kde_eval(q, x, data, 0.1)
4549
f_ref = ref_kde(x, data, 0.1)
4650

@@ -62,6 +66,10 @@ def test_2d():
6266

6367
data = np.random.rand(128*128, 2)
6468

69+
if not q.sycl_device.has_aspect_fp64:
70+
x = np.asarray(x, dtype=np.float32)
71+
data = np.asarray(data, dtype=np.float32)
72+
6573
f_cy = kde_eval(q, x, data, 0.05)
6674
f_ref = ref_kde(x, data, 0.05)
6775

@@ -84,6 +92,10 @@ def test_3d():
8492

8593
data = np.random.rand(16000,3)
8694

95+
if not q.sycl_device.has_aspect_fp64:
96+
x = np.asarray(x, dtype=np.float32)
97+
data = np.asarray(data, dtype=np.float32)
98+
8799
f_cy = kde_eval(q, x, data, 0.01)
88100
f_ref = ref_kde(x, data, 0.01)
89101

kde_skbuild/tests/test_py_kde.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def test_1d():
4141
x = np.linspace(0.1, 0.9, num=15).reshape((-1, 1))
4242
data = np.random.rand(128,1)
4343

44+
if not q.sycl_device.has_aspect_fp64:
45+
x = np.asarray(x, dtype=np.float32)
46+
data = np.asarray(data, dtype=np.float32)
47+
4448
f_cy = kde_eval(q, x, data, 0.1)
4549
f_ref = ref_kde(x, data, 0.1)
4650

@@ -61,6 +65,10 @@ def test_2d():
6165
).reshape(-1, 2)
6266

6367
data = np.random.rand(128*128, 2)
68+
69+
if not q.sycl_device.has_aspect_fp64:
70+
x = np.asarray(x, dtype=np.float32)
71+
data = np.asarray(data, dtype=np.float32)
6472

6573
f_cy = kde_eval(q, x, data, 0.05)
6674
f_ref = ref_kde(x, data, 0.05)
@@ -83,7 +91,11 @@ def test_3d():
8391
).reshape(-1, 3)
8492

8593
data = np.random.rand(16000,3)
86-
94+
95+
if not q.sycl_device.has_aspect_fp64:
96+
x = np.asarray(x, dtype=np.float32)
97+
data = np.asarray(data, dtype=np.float32)
98+
8799
f_cy = kde_eval(q, x, data, 0.01)
88100
f_ref = ref_kde(x, data, 0.01)
89101

0 commit comments

Comments
 (0)