Skip to content

Commit bf85601

Browse files
authored
Merge pull request #178 from hdelan/merge-usm-buffers
Merge usm buffers
2 parents 9ec5961 + 48cb960 commit bf85601

File tree

10 files changed

+256
-101
lines changed

10 files changed

+256
-101
lines changed

Code_Exercises/Exercise_03_Scalar_Add/solution.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,36 @@
1717
#include <CL/sycl.hpp>
1818
#endif
1919

20-
class scalar_add;
20+
class scalar_add_usm;
21+
class scalar_add_buff_acc;
2122

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

2552
auto defaultQueue = sycl::queue{};
@@ -35,7 +62,7 @@ TEST_CASE("scalar_add", "scalar_add_solution") {
3562
auto accB = sycl::accessor{bufB, cgh, sycl::read_only};
3663
auto accR = sycl::accessor{bufR, cgh, sycl::write_only};
3764

38-
cgh.single_task<scalar_add>([=] { accR[0] = accA[0] + accB[0]; });
65+
cgh.single_task<scalar_add_buff_acc>([=] { accR[0] = accA[0] + accB[0]; });
3966
})
4067
.wait();
4168
}

Code_Exercises/Exercise_03_Scalar_Add/source.cpp

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,69 @@
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+
* #if __has_include(<SYCL/sycl.hpp>)
15+
* #include <SYCL/sycl.hpp>
16+
* #else
17+
* #include <CL/sycl.hpp>
18+
* #endif
19+
*
20+
* // Default construct a queue
21+
* auto q = sycl::queue{};
22+
*
23+
* // Allocate device memory
24+
* auto * devPtr = sycl::malloc_device<int>(mycount, q);
25+
*
26+
* // Memcpy
27+
* q.memcpy(dst, src, sizeof(T)*n).wait();
28+
* // (dst and src are pointers)
29+
*
30+
* // Free memory
31+
* sycl::free(ptr, q);
32+
*
33+
* // Construct a buffer of size n associated with ptr
34+
* auto buf = sycl::buffer{ptr, sycl::range{n}};
35+
*
36+
* // Submit a kernel
37+
* q.submit([&](sycl::handler &cgh) {
38+
* cgh.single_task([=](){
39+
* // Some kernel code
40+
* });
41+
* }).wait();
42+
*
43+
* // Construct an accessor for buf
44+
* // (must be done within command group)
45+
* auto acc = sycl::accessor{buf, cgh};
46+
* auto acc = sycl::accessor{buf, cgh, sycl::read_only};
47+
* auto acc = sycl::accessor{buf, cgh, sycl::write_only};
48+
* auto acc = sycl::accessor{buf, cgh, sycl::no_init};
49+
*
950
*/
1051

1152
#define CATCH_CONFIG_MAIN
1253
#include <catch2/catch.hpp>
1354

14-
TEST_CASE("scalar_add", "scalar_add_source") {
55+
TEST_CASE("scalar_add_usm", "scalar_add_source") {
1556

1657
int a = 18, b = 24, r = 0;
1758

18-
// Task: Compute a+b on the SYCL device
59+
// Task: Compute a+b on the SYCL device using USM
1960
r = a + b;
2061

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

2273
REQUIRE(r == 42);
2374
}

0 commit comments

Comments
 (0)