Skip to content

Commit 4b9148f

Browse files
author
Hugh Delaney
committed
Fusing USM and buff acc chapters
1 parent 04ce9cb commit 4b9148f

File tree

10 files changed

+251
-100
lines changed

10 files changed

+251
-100
lines changed

Code_Exercises/Exercise_03_Scalar_Add/solution.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,33 @@
1919

2020
class scalar_add;
2121

22-
TEST_CASE("scalar_add", "scalar_add_solution") {
22+
TEST_CASE("scalar_add_usm", "scalar_add_solution") {
23+
int a = 18, b = 24, r = 0;
24+
25+
auto defaultQueue = sycl::queue{};
26+
27+
auto dev_A = sycl::malloc_device<int>(1, defaultQueue);
28+
auto dev_B = sycl::malloc_device<int>(1, defaultQueue);
29+
auto dev_R = sycl::malloc_device<int>(1, defaultQueue);
30+
31+
defaultQueue.memcpy(dev_A, &a, 1 * sizeof(int));
32+
defaultQueue.memcpy(dev_B, &b, 1 * sizeof(int));
33+
34+
defaultQueue
35+
.submit([&](sycl::handler &cgh) {
36+
cgh.single_task([=] { dev_R[0] = dev_A[0] + dev_B[0]; });
37+
});
38+
39+
defaultQueue.memcpy(&r, dev_R, 1 * sizeof(int)).wait();
40+
41+
sycl::free(dev_A, defaultQueue);
42+
sycl::free(dev_B, defaultQueue);
43+
sycl::free(dev_R, defaultQueue);
44+
45+
REQUIRE(r == 42);
46+
}
47+
48+
TEST_CASE("scalar_add_buff_acc", "scalar_add_solution") {
2349
int a = 18, b = 24, r = 0;
2450

2551
auto defaultQueue = sycl::queue{};
@@ -35,7 +61,7 @@ TEST_CASE("scalar_add", "scalar_add_solution") {
3561
auto accB = sycl::accessor{bufB, cgh, sycl::read_only};
3662
auto accR = sycl::accessor{bufR, cgh, sycl::write_only};
3763

38-
cgh.single_task<scalar_add>([=] { accR[0] = accA[0] + accB[0]; });
64+
cgh.single_task([=] { accR[0] = accA[0] + accB[0]; });
3965
})
4066
.wait();
4167
}

Code_Exercises/Exercise_03_Scalar_Add/source.cpp

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,65 @@
66
77
You should have received a copy of the license along with this
88
work. If not, see <http://creativecommons.org/licenses/by-sa/4.0/>.
9+
10+
* SYCL Quick Reference
11+
* ~~~~~~~~~~~~~~~~~~~~
12+
*
13+
* // Include SYCL header
14+
* #include <CL/sycl.hpp>
15+
*
16+
* // Default construct a queue
17+
* auto q = sycl::queue{};
18+
*
19+
* // Allocate device memory
20+
* auto * devPtr = sycl::malloc_device<int>(mycount, q);
21+
*
22+
* // Memcpy
23+
* q.memcpy(dst, src, sizeof(T)*n).wait();
24+
* // (dst and src are pointers)
25+
*
26+
* // Free memory
27+
* sycl::free(ptr, q);
28+
*
29+
* // Construct a buffer of size n associated with ptr
30+
* auto buf = sycl::buffer{ptr, sycl::range{n}};
31+
*
32+
* // Submit a kernel
33+
* q.submit([&](sycl::handler &cgh) {
34+
* cgh.single_task([=](){
35+
* // Some kernel code
36+
* });
37+
* }).wait();
38+
*
39+
* // Construct an accessor for buf
40+
* // (must be done within command group)
41+
* auto acc = sycl::accessor{buf, cgh};
42+
* auto acc = sycl::accessor{buf, cgh, sycl::read_only};
43+
* auto acc = sycl::accessor{buf, cgh, sycl::write_only};
44+
* auto acc = sycl::accessor{buf, cgh, sycl::no_init};
45+
*
946
*/
1047

1148
#define CATCH_CONFIG_MAIN
1249
#include <catch2/catch.hpp>
1350

14-
TEST_CASE("scalar_add", "scalar_add_source") {
51+
TEST_CASE("scalar_add_usm", "scalar_add_source") {
1552

1653
int a = 18, b = 24, r = 0;
1754

18-
// Task: Compute a+b on the SYCL device
55+
// Task: Compute a+b on the SYCL device using USM
1956
r = a + b;
2057

58+
REQUIRE(r == 42);
59+
}
60+
61+
TEST_CASE("scalar_add_buff_acc", "scalar_add_source") {
62+
63+
int a = 18, b = 24, r = 0;
64+
65+
// Task: Compute a+b on the SYCL device using the buffer
66+
// accessor memory model
67+
r = a + b;
2168

2269
REQUIRE(r == 42);
2370
}

0 commit comments

Comments
 (0)