Skip to content

FastTrack code and slides update for ISC25 #398

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 9 commits into from
Jun 10, 2025
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
207 changes: 46 additions & 161 deletions Code_Exercises/Asynchronous_Execution/solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,7 @@
#include <sycl/sycl.hpp>

#include "../helpers.hpp"

class vector_add_1;
class vector_add_2;
class vector_add_3;
class vector_add_4;
class vector_add_5;
class vector_add_6;

int usm_selector(const sycl::device& dev) {
if (dev.has(sycl::aspect::usm_device_allocations)) {
if (dev.has(sycl::aspect::gpu)) return 2;
return 1;
}
return -1;
}
using namespace sycl;

void test_buffer_event_wait() {
constexpr size_t dataSize = 1024;
Expand All @@ -38,26 +24,26 @@ void test_buffer_event_wait() {
}

try {
auto defaultQueue = sycl::queue{};
queue defaultQueue = queue{};

auto bufA = sycl::buffer{a, sycl::range{dataSize}};
auto bufB = sycl::buffer{b, sycl::range{dataSize}};
auto bufR = sycl::buffer{r, sycl::range{dataSize}};
buffer bufA = buffer{a, range{dataSize}};
buffer bufB = buffer{b, range{dataSize}};
buffer bufR = buffer{r, range{dataSize}};

defaultQueue
.submit([&](sycl::handler& cgh) {
auto accA = sycl::accessor{bufA, cgh, sycl::read_only};
auto accB = sycl::accessor{bufB, cgh, sycl::read_only};
auto accR = sycl::accessor{bufR, cgh, sycl::write_only};

cgh.parallel_for<vector_add_1>(
sycl::range{dataSize},
[=](sycl::id<1> idx) { accR[idx] = accA[idx] + accB[idx]; });
.submit([&](handler& cgh) {
accessor accA = accessor{bufA, cgh, read_only};
accessor accB = accessor{bufB, cgh, read_only};
accessor accR = accessor{bufR, cgh, write_only};

cgh.parallel_for(range{dataSize}, [=](id<1> idx) {
accR[idx] = accA[idx] + accB[idx];
});
})
.wait(); // Synchronize

defaultQueue.throw_asynchronous();
} catch (const sycl::exception& e) { // Copy back
} catch (const exception& e) { // Copy back
std::cout << "Exception caught: " << e.what() << std::endl;
}

Expand All @@ -75,20 +61,19 @@ void test_buffer_queue_wait() {
}

try {
auto defaultQueue = sycl::queue{};
queue defaultQueue = queue{};

auto bufA = sycl::buffer{a, sycl::range{dataSize}};
auto bufB = sycl::buffer{b, sycl::range{dataSize}};
auto bufR = sycl::buffer{r, sycl::range{dataSize}};
buffer bufA = buffer{a, range{dataSize}};
buffer bufB = buffer{b, range{dataSize}};
buffer bufR = buffer{r, range{dataSize}};

defaultQueue.submit([&](sycl::handler& cgh) {
auto accA = sycl::accessor{bufA, cgh, sycl::read_only};
auto accB = sycl::accessor{bufB, cgh, sycl::read_only};
auto accR = sycl::accessor{bufR, cgh, sycl::write_only};
defaultQueue.submit([&](handler& cgh) {
accessor accA = accessor{bufA, cgh, read_only};
accessor accB = accessor{bufB, cgh, read_only};
accessor accR = accessor{bufR, cgh, write_only};

cgh.parallel_for<vector_add_2>(
sycl::range{dataSize},
[=](sycl::id<1> idx) { accR[idx] = accA[idx] + accB[idx]; });
cgh.parallel_for(range{dataSize},
[=](id<1> idx) { accR[idx] = accA[idx] + accB[idx]; });
});

defaultQueue.wait_and_throw(); // Synchronize
Expand All @@ -110,21 +95,20 @@ void test_buffer_buffer_destruction() {
}

try {
auto defaultQueue = sycl::queue{};
queue defaultQueue = queue{};

{
auto bufA = sycl::buffer{a, sycl::range{dataSize}};
auto bufB = sycl::buffer{b, sycl::range{dataSize}};
auto bufR = sycl::buffer{r, sycl::range{dataSize}};
buffer bufA = buffer{a, range{dataSize}};
buffer bufB = buffer{b, range{dataSize}};
buffer bufR = buffer{r, range{dataSize}};

defaultQueue.submit([&](sycl::handler& cgh) {
auto accA = sycl::accessor{bufA, cgh, sycl::read_only};
auto accB = sycl::accessor{bufB, cgh, sycl::read_only};
auto accR = sycl::accessor{bufR, cgh, sycl::write_only};
accessor accA = accessor{bufA, cgh, read_only};
accessor accB = accessor{bufB, cgh, read_only};
accessor accR = accessor{bufR, cgh, write_only};

cgh.parallel_for<vector_add_3>(
sycl::range{dataSize},
[=](sycl::id<1> idx) { accR[idx] = accA[idx] + accB[idx]; });
cgh.parallel_for(range{dataSize},
[=](id<1> idx) { accR[idx] = accA[idx] + accB[idx]; });
});
} // Synchronize and copy-back

Expand All @@ -136,104 +120,6 @@ void test_buffer_buffer_destruction() {
SYCLACADEMY_ASSERT_EQUAL(r, [](size_t i) { return i * 2; });
}

void test_usm_event_wait() {
constexpr size_t dataSize = 1024;

float a[dataSize], b[dataSize], r[dataSize];
for (int i = 0; i < dataSize; ++i) {
a[i] = static_cast<float>(i);
b[i] = static_cast<float>(i);
r[i] = 0.0f;
}

try {
auto usmQueue = sycl::queue{usm_selector};

auto devicePtrA = sycl::malloc_device<float>(dataSize, usmQueue);
auto devicePtrB = sycl::malloc_device<float>(dataSize, usmQueue);
auto devicePtrR = sycl::malloc_device<float>(dataSize, usmQueue);

usmQueue.memcpy(devicePtrA, a,
sizeof(float) * dataSize)
.wait(); // Synchronize
usmQueue.memcpy(devicePtrB, b,
sizeof(float) * dataSize)
.wait(); // Synchronize

usmQueue
.parallel_for<vector_add_4>(sycl::range{dataSize},
[=](sycl::id<1> idx) {
auto globalId = idx[0];
devicePtrR[globalId] =
devicePtrA[globalId] +
devicePtrB[globalId];
})
.wait(); // Synchronize

usmQueue.memcpy(r, devicePtrR,
sizeof(float) * dataSize)
.wait(); // Synchronize and copy-back

sycl::free(devicePtrA, usmQueue);
sycl::free(devicePtrB, usmQueue);
sycl::free(devicePtrR, usmQueue);

usmQueue.throw_asynchronous();
} catch (const sycl::exception& e) {
std::cout << "Exception caught: " << e.what() << std::endl;
}

SYCLACADEMY_ASSERT_EQUAL(r, [](size_t i) { return i * 2; });
}

void test_usm_queue_wait() {
constexpr size_t dataSize = 1024;

float a[dataSize], b[dataSize], r[dataSize];
for (int i = 0; i < dataSize; ++i) {
a[i] = static_cast<float>(i);
b[i] = static_cast<float>(i);
r[i] = 0.0f;
}

try {
auto usmQueue = sycl::queue{usm_selector};

auto devicePtrA = sycl::malloc_device<float>(dataSize, usmQueue);
auto devicePtrB = sycl::malloc_device<float>(dataSize, usmQueue);
auto devicePtrR = sycl::malloc_device<float>(dataSize, usmQueue);

usmQueue.memcpy(devicePtrA, a, sizeof(float) * dataSize);
usmQueue.memcpy(devicePtrB, b, sizeof(float) * dataSize);

usmQueue.wait(); // Synchronize

usmQueue.parallel_for<vector_add_5>(
sycl::range{dataSize}, [=](sycl::id<1> idx) {
auto globalId = idx[0];
devicePtrR[globalId] = devicePtrA[globalId] + devicePtrB[globalId];
});

usmQueue.wait(); // Synchronize

usmQueue.memcpy(r, devicePtrR,
sizeof(float) * dataSize)
.wait(); // Copy-back

usmQueue.wait(); // Synchronize

sycl::free(devicePtrA, usmQueue);
sycl::free(devicePtrB, usmQueue);
sycl::free(devicePtrR, usmQueue);

usmQueue.throw_asynchronous();
} catch (const sycl::exception& e) {
std::cout << "Exception caught: " << e.what() << std::endl;
}

SYCLACADEMY_ASSERT_EQUAL(r, [](size_t i) { return i * 2; });
}

void test_buffer_host_accessor() {
constexpr size_t dataSize = 1024;

Expand All @@ -245,35 +131,36 @@ void test_buffer_host_accessor() {
}

try {
auto defaultQueue = sycl::queue{};
queue defaultQueue = queue{};

{
auto bufA = sycl::buffer{a, sycl::range{dataSize}};
auto bufB = sycl::buffer{b, sycl::range{dataSize}};
auto bufR = sycl::buffer{r, sycl::range{dataSize}};
buffer bufA = buffer{a, range{dataSize}};
buffer bufB = buffer{b, range{dataSize}};
buffer bufR = buffer{r, range{dataSize}};

defaultQueue.submit([&](sycl::handler& cgh) {
auto accA = sycl::accessor{bufA, cgh, sycl::read_only};
auto accB = sycl::accessor{bufB, cgh, sycl::read_only};
auto accR = sycl::accessor{bufR, cgh, sycl::write_only};
accessor accA = accessor{bufA, cgh, read_only};
accessor accB = accessor{bufB, cgh, read_only};
accessor accR = accessor{bufR, cgh, write_only};

cgh.parallel_for<vector_add_6>(
sycl::range{dataSize},
[=](sycl::id<1> idx) { accR[idx] = accA[idx] + accB[idx]; });
cgh.parallel_for(range{dataSize}, [=](sycl::id<1> idx) {
accR[idx] = accA[idx] + accB[idx];
});
});

defaultQueue.wait(); // Synchronize

{
auto hostAccR = bufR.get_host_access(sycl::read_only); // Copy-to-host
host_accessor hostAccR =
bufR.get_host_access(read_only); // Copy-to-host

SYCLACADEMY_ASSERT_EQUAL(hostAccR, [](size_t i) { return i * 2; });
}

} // Copy-back

defaultQueue.throw_asynchronous();
} catch (const sycl::exception& e) {
} catch (const exception& e) {
std::cout << "Exception caught: " << e.what() << std::endl;
}
}
Expand All @@ -282,7 +169,5 @@ int main() {
test_buffer_event_wait();
test_buffer_queue_wait();
test_buffer_buffer_destruction();
test_usm_event_wait();
test_usm_queue_wait();
test_buffer_host_accessor();
}
28 changes: 7 additions & 21 deletions Code_Exercises/Asynchronous_Execution/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@
* ~~~~~~~~~~~~~~~~~~~~
*
* // Default construct a queue
* auto q = sycl::queue{};
* sycl::queue queue q = sycl::queue{};
*
* // Declare a buffer pointing to ptr
* auto buf = sycl::buffer{ptr, sycl::range{n}};
*
* // Do a USM malloc_device
* auto ptr = sycl::malloc_device<T>(n, q);
*
* // Do a USM memcpy
* q.memcpy(dst_ptr, src_ptr, sizeof(T)*n);
* sycl::buffer buf = sycl::buffer{ptr, sycl::range{n}};
*
* // Wait on a queue
* q.wait();
Expand All @@ -33,10 +27,10 @@
*
* // Within the command group you can
* // 1. Declare an accessor to a buffer
* auto read_write_acc = sycl::accessor{buf, cgh};
* auto read_acc = sycl::accessor{buf, cgh, sycl::read_only};
* auto write_acc = sycl::accessor{buf, cgh, sycl::write_only};
* auto no_init_acc = sycl::accessor{buf, cgh, sycl::no_init};
* accessor read_write_acc = sycl::accessor{buf, cgh};
* accessor read_acc = sycl::accessor{buf, cgh, sycl::read_only};
* accessor write_acc = sycl::accessor{buf, cgh, sycl::write_only};
* accessor no_init_acc = sycl::accessor{buf, cgh, sycl::no_init};
* // 2. Enqueue a parallel for:
* cgh.parallel_for<class mykernel>(sycl::range{n},
* [=](sycl::id<1> i) { // Do something });
Expand All @@ -46,17 +40,9 @@

#include "../helpers.hpp"

void test_usm() {
// Use your code from the "Data Parallelism" exercise to start
SYCLACADEMY_ASSERT_EQUAL(/*output data*/ 0, /*expected data*/ 0);
}

void test_buffer() {
// Use your code from the "Data Parallelism" exercise to start
SYCLACADEMY_ASSERT_EQUAL(/*output data*/ 0, /*expected data*/ 0);
}

int main() {
test_usm();
test_buffer();
}
int main() { test_buffer(); }
Loading