Skip to content

Commit 0e756c1

Browse files
committed
Add feature overview and an example
1 parent 4aa87da commit 0e756c1

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

sycl/doc/SYCL2020-SpecializationConstants.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,65 @@ by SYCL 2020 specification: [SYCL registry][sycl-registry],
77
[sycl-registry]: https://www.khronos.org/registry/SYCL/
88
[sycl-2020-spec]: https://www.khronos.org/registry/SYCL/specs/sycl-2020/html/sycl-2020.html
99

10-
TODO: feature overview? code example?
10+
> Specialization constants represent constants whose values can be set
11+
> dynamically during execution of the SYCL application. The values of these
12+
> constants are fixed when a SYCL kernel function is invoked, and they do not
13+
> change during the execution of the kernel. However, the application is able to
14+
> set a new value for a specialization constants each time a kernel is invoked,
15+
> so the values can be tuned differently for each invocation.
16+
>
17+
> [Section 4.9.5 Specialization constants][sycl-2020-4-9-5]
18+
19+
[sycl-2020-4-9-5]: https://www.khronos.org/registry/SYCL/specs/sycl-2020/html/sycl-2020.html#_specialization_constants
20+
21+
Example usage:
22+
23+
```
24+
#include <sycl/sycl.hpp>
25+
using namespace sycl;
26+
27+
using coeff_t = std::array<std::array<float, 3>, 3>;
28+
29+
// Read coefficients from somewhere.
30+
coeff_t get_coefficients();
31+
32+
// Identify the specialization constant.
33+
constexpr specialization_id<coeff_t> coeff_id;
34+
35+
void do_conv(buffer<float, 2> in, buffer<float, 2> out) {
36+
queue myQueue;
37+
38+
myQueue.submit([&](handler &cgh) {
39+
accessor in_acc { in, cgh, read_only };
40+
accessor out_acc { out, cgh, write_only };
41+
42+
// Set the coefficient of the convolution as constant.
43+
// This will build a specific kernel the coefficient available as literals.
44+
cgh.set_specialization_constant<coeff_id>(get_coefficients());
45+
46+
cgh.parallel_for<class Convolution>(
47+
in.get_range(), [=](item<2> item_id, kernel_handler h) {
48+
float acc = 0;
49+
coeff_t coeff = h.get_specialization_constant<coeff_id>();
50+
for (int i = -1; i <= 1; i++) {
51+
if (item_id[0] + i < 0 || item_id[0] + i >= in_acc.get_range()[0])
52+
continue;
53+
for (int j = -1; j <= 1; j++) {
54+
if (item_id[1] + j < 0 || item_id[1] + j >= in_acc.get_range()[1])
55+
continue;
56+
// The underlying JIT can see all the values of the array returned
57+
// by coeff.get().
58+
acc += coeff[i + 1][j + 1] *
59+
in_acc[item_id[0] + i][item_id[1] + j];
60+
}
61+
}
62+
out_acc[item_id] = acc;
63+
});
64+
});
65+
66+
myQueue.wait();
67+
}
68+
```
1169

1270
## Design objectives
1371

0 commit comments

Comments
 (0)