Skip to content

Merge usm buffers #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions Code_Exercises/Exercise_03_Scalar_Add/solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,36 @@
#include <CL/sycl.hpp>
#endif

class scalar_add;
class scalar_add_usm;
class scalar_add_buff_acc;

TEST_CASE("scalar_add", "scalar_add_solution") {
TEST_CASE("scalar_add_usm", "scalar_add_solution") {
int a = 18, b = 24, r = 0;

auto defaultQueue = sycl::queue{};

auto dev_A = sycl::malloc_device<int>(1, defaultQueue);
auto dev_B = sycl::malloc_device<int>(1, defaultQueue);
auto dev_R = sycl::malloc_device<int>(1, defaultQueue);

defaultQueue.memcpy(dev_A, &a, 1 * sizeof(int));
defaultQueue.memcpy(dev_B, &b, 1 * sizeof(int));

defaultQueue
.submit([&](sycl::handler &cgh) {
cgh.single_task<scalar_add_usm>([=] { dev_R[0] = dev_A[0] + dev_B[0]; });
});

defaultQueue.memcpy(&r, dev_R, 1 * sizeof(int)).wait();

sycl::free(dev_A, defaultQueue);
sycl::free(dev_B, defaultQueue);
sycl::free(dev_R, defaultQueue);

REQUIRE(r == 42);
}

TEST_CASE("scalar_add_buff_acc", "scalar_add_solution") {
int a = 18, b = 24, r = 0;

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

cgh.single_task<scalar_add>([=] { accR[0] = accA[0] + accB[0]; });
cgh.single_task<scalar_add_buff_acc>([=] { accR[0] = accA[0] + accB[0]; });
})
.wait();
}
Expand Down
55 changes: 53 additions & 2 deletions Code_Exercises/Exercise_03_Scalar_Add/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,69 @@

You should have received a copy of the license along with this
work. If not, see <http://creativecommons.org/licenses/by-sa/4.0/>.

* SYCL Quick Reference
* ~~~~~~~~~~~~~~~~~~~~
*
* // Include SYCL header
* #if __has_include(<SYCL/sycl.hpp>)
* #include <SYCL/sycl.hpp>
* #else
* #include <CL/sycl.hpp>
* #endif
*
* // Default construct a queue
* auto q = sycl::queue{};
*
* // Allocate device memory
* auto * devPtr = sycl::malloc_device<int>(mycount, q);
*
* // Memcpy
* q.memcpy(dst, src, sizeof(T)*n).wait();
* // (dst and src are pointers)
*
* // Free memory
* sycl::free(ptr, q);
*
* // Construct a buffer of size n associated with ptr
* auto buf = sycl::buffer{ptr, sycl::range{n}};
*
* // Submit a kernel
* q.submit([&](sycl::handler &cgh) {
* cgh.single_task([=](){
* // Some kernel code
* });
* }).wait();
*
* // Construct an accessor for buf
* // (must be done within command group)
* auto acc = sycl::accessor{buf, cgh};
* auto acc = sycl::accessor{buf, cgh, sycl::read_only};
* auto acc = sycl::accessor{buf, cgh, sycl::write_only};
* auto acc = sycl::accessor{buf, cgh, sycl::no_init};
*
*/

#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

TEST_CASE("scalar_add", "scalar_add_source") {
TEST_CASE("scalar_add_usm", "scalar_add_source") {

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

// Task: Compute a+b on the SYCL device
// Task: Compute a+b on the SYCL device using USM
r = a + b;

REQUIRE(r == 42);
}

TEST_CASE("scalar_add_buff_acc", "scalar_add_source") {

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

// Task: Compute a+b on the SYCL device using the buffer
// accessor memory model
r = a + b;

REQUIRE(r == 42);
}
Loading