Skip to content

Commit

Permalink
Change clang-format style and add GitHub workflow (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafbiels authored Dec 17, 2024
1 parent bb0c8c8 commit b0cd34d
Show file tree
Hide file tree
Showing 41 changed files with 495 additions and 479 deletions.
26 changes: 18 additions & 8 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
Language: Cpp
BasedOnStyle: LLVM
AccessModifierOffset: -1
BreakInheritanceList: BeforeComma
BreakConstructorInitializers: BeforeComma
Cpp11BracedListStyle: false
PointerAlignment: Left
SpaceBeforeCpp11BracedList: true
BasedOnStyle: Google
IncludeCategories:
- Regex: '^<sycl/.*>$'
Priority: 3
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*\.h>'
Priority: 1
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 4
SortPriority: 0
CaseSensitive: false
17 changes: 17 additions & 0 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: clang-format check
on: [push, pull_request]
permissions:
contents: read
jobs:
clang-format:
name: clang-format check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: clang-format
uses: jidicula/[email protected]
with:
clang-format-version: '19'
check-path: 'Code_Exercises'
fallback-style: 'Google'
include-regex: '^.*\.(((c|C)(c|pp|xx|\+\+)?$)|((h|H)h?(pp|xx|\+\+)?$))$'
40 changes: 20 additions & 20 deletions Code_Exercises/Advanced_Data_Flow/solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
work. If not, see <http://creativecommons.org/licenses/by-sa/4.0/>.
*/

#include "../helpers.hpp"
#include <sycl/sycl.hpp>

#include "../helpers.hpp"

class kernel_a_1;
class kernel_b_1;
class kernel_a_2;
class kernel_b_2;

int usm_selector(const sycl::device& dev) {
if (dev.has(sycl::aspect::usm_device_allocations)) {
if (dev.has(sycl::aspect::gpu))
return 2;
if (dev.has(sycl::aspect::gpu)) return 2;
return 1;
}
return -1;
Expand All @@ -35,31 +35,31 @@ void test_buffer() {
}

try {
auto gpuQueue = sycl::queue { sycl::gpu_selector_v };
auto gpuQueue = sycl::queue{sycl::gpu_selector_v};

auto bufIn = sycl::buffer { in, sycl::range { dataSize } };
auto bufInt = sycl::buffer<float> { sycl::range { dataSize } };
auto bufOut = sycl::buffer<float> { sycl::range { dataSize } };
auto bufIn = sycl::buffer{in, sycl::range{dataSize}};
auto bufInt = sycl::buffer<float>{sycl::range{dataSize}};
auto bufOut = sycl::buffer<float>{sycl::range{dataSize}};

bufIn.set_final_data(nullptr);
bufOut.set_final_data(out);

gpuQueue.submit([&](sycl::handler& cgh) {
sycl::accessor accIn { bufIn, cgh, sycl::read_only };
sycl::accessor accOut { bufInt, cgh, sycl::write_only };
sycl::accessor accIn{bufIn, cgh, sycl::read_only};
sycl::accessor accOut{bufInt, cgh, sycl::write_only};

cgh.parallel_for<kernel_a_1>(
sycl::range { dataSize },
[=](sycl::id<1> idx) { accOut[idx] = accIn[idx] * 8.0f; });
cgh.parallel_for<kernel_a_1>(sycl::range{dataSize}, [=](sycl::id<1> idx) {
accOut[idx] = accIn[idx] * 8.0f;
});
});

gpuQueue.submit([&](sycl::handler& cgh) {
sycl::accessor accIn { bufInt, cgh, sycl::read_only };
sycl::accessor accOut { bufOut, cgh, sycl::write_only };
sycl::accessor accIn{bufInt, cgh, sycl::read_only};
sycl::accessor accOut{bufOut, cgh, sycl::write_only};

cgh.parallel_for<kernel_b_1>(
sycl::range { dataSize },
[=](sycl::id<1> idx) { accOut[idx] = accIn[idx] / 2.0f; });
cgh.parallel_for<kernel_b_1>(sycl::range{dataSize}, [=](sycl::id<1> idx) {
accOut[idx] = accIn[idx] / 2.0f;
});
});

gpuQueue.wait_and_throw();
Expand All @@ -82,7 +82,7 @@ void test_usm() {
}

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

auto devicePtrIn = sycl::malloc_device<float>(dataSize, usmQueue);
auto devicePtrInt = sycl::malloc_device<float>(dataSize, usmQueue);
Expand All @@ -91,13 +91,13 @@ void test_usm() {
auto e1 = usmQueue.memcpy(devicePtrIn, in, sizeof(float) * dataSize);

auto e2 = usmQueue.parallel_for<kernel_a_2>(
sycl::range { dataSize }, e1, [=](sycl::id<1> idx) {
sycl::range{dataSize}, e1, [=](sycl::id<1> idx) {
auto globalId = idx[0];
devicePtrInt[globalId] = devicePtrIn[globalId] * 8.0f;
});

auto e3 = usmQueue.parallel_for<kernel_b_2>(
sycl::range { dataSize }, e2, [=](sycl::id<1> idx) {
sycl::range{dataSize}, e2, [=](sycl::id<1> idx) {
auto globalId = idx[0];
devicePtrOut[globalId] = devicePtrInt[globalId] / 2.0f;
});
Expand Down
110 changes: 55 additions & 55 deletions Code_Exercises/Asynchronous_Execution/solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
work. If not, see <http://creativecommons.org/licenses/by-sa/4.0/>.
*/

#include "../helpers.hpp"
#include <sycl/sycl.hpp>

#include "../helpers.hpp"

class vector_add_1;
class vector_add_2;
class vector_add_3;
Expand All @@ -20,8 +21,7 @@ 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;
if (dev.has(sycl::aspect::gpu)) return 2;
return 1;
}
return -1;
Expand All @@ -38,26 +38,26 @@ void test_buffer_event_wait() {
}

try {
auto defaultQueue = sycl::queue {};
auto defaultQueue = sycl::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 } };
auto bufA = sycl::buffer{a, sycl::range{dataSize}};
auto bufB = sycl::buffer{b, sycl::range{dataSize}};
auto bufR = sycl::buffer{r, sycl::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 };
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::range{dataSize},
[=](sycl::id<1> idx) { accR[idx] = accA[idx] + accB[idx]; });
})
.wait(); // Synchronize
.wait(); // Synchronize

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

Expand All @@ -77,24 +77,24 @@ void test_buffer_queue_wait() {
}

try {
auto defaultQueue = sycl::queue {};
auto defaultQueue = sycl::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 } };
auto bufA = sycl::buffer{a, sycl::range{dataSize}};
auto bufB = sycl::buffer{b, sycl::range{dataSize}};
auto bufR = sycl::buffer{r, sycl::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 };
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_2>(
sycl::range { dataSize },
sycl::range{dataSize},
[=](sycl::id<1> idx) { accR[idx] = accA[idx] + accB[idx]; });
});

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

Expand All @@ -114,23 +114,23 @@ void test_buffer_buffer_destruction() {
}

try {
auto defaultQueue = sycl::queue {};
auto defaultQueue = sycl::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 } };
auto bufA = sycl::buffer{a, sycl::range{dataSize}};
auto bufB = sycl::buffer{b, sycl::range{dataSize}};
auto bufR = sycl::buffer{r, sycl::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 };
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_3>(
sycl::range { dataSize },
sycl::range{dataSize},
[=](sycl::id<1> idx) { accR[idx] = accA[idx] + accB[idx]; });
});
} // Synchronize and copy-back
} // Synchronize and copy-back

defaultQueue.throw_asynchronous();
} catch (const sycl::exception& e) {
Expand All @@ -153,32 +153,32 @@ void test_usm_event_wait() {
}

try {
auto usmQueue = sycl::queue { usm_selector };
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
.wait(); // Synchronize
usmQueue.memcpy(devicePtrB, b,
sizeof(float) * dataSize)
.wait(); // Synchronize
.wait(); // Synchronize

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

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

sycl::free(devicePtrA, usmQueue);
sycl::free(devicePtrB, usmQueue);
Expand All @@ -205,7 +205,7 @@ void test_usm_queue_wait() {
}

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

auto devicePtrA = sycl::malloc_device<float>(dataSize, usmQueue);
auto devicePtrB = sycl::malloc_device<float>(dataSize, usmQueue);
Expand All @@ -214,21 +214,21 @@ void test_usm_queue_wait() {
usmQueue.memcpy(devicePtrA, a, sizeof(float) * dataSize);
usmQueue.memcpy(devicePtrB, b, sizeof(float) * dataSize);

usmQueue.wait(); // Synchronize
usmQueue.wait(); // Synchronize

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

usmQueue.wait(); // Synchronize
usmQueue.wait(); // Synchronize

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

usmQueue.wait(); // Synchronize
usmQueue.wait(); // Synchronize

sycl::free(devicePtrA, usmQueue);
sycl::free(devicePtrB, usmQueue);
Expand All @@ -255,34 +255,34 @@ void test_buffer_host_accessor() {
}

try {
auto defaultQueue = sycl::queue {};
auto defaultQueue = sycl::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 } };
auto bufA = sycl::buffer{a, sycl::range{dataSize}};
auto bufB = sycl::buffer{b, sycl::range{dataSize}};
auto bufR = sycl::buffer{r, sycl::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 };
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_6>(
sycl::range { dataSize },
sycl::range{dataSize},
[=](sycl::id<1> idx) { accR[idx] = accA[idx] + accB[idx]; });
});

defaultQueue.wait(); // Synchronize
defaultQueue.wait(); // Synchronize

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

for (int i = 0; i < dataSize; ++i) {
SYCLACADEMY_ASSERT(hostAccR[i] == i * 2);
}
}

} // Copy-back
} // Copy-back

defaultQueue.throw_asynchronous();
} catch (const sycl::exception& e) {
Expand Down
Loading

0 comments on commit b0cd34d

Please sign in to comment.