Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit cf66845

Browse files
[SYCL-MLIR] Add stream-[copy|triad].cpp (#1384)
* [SYCL-MLIR] Add stream-[copy|triad].cpp Signed-off-by: Tsang, Whitney <[email protected]>
1 parent 4683496 commit cf66845

File tree

4 files changed

+94
-4
lines changed

4 files changed

+94
-4
lines changed

SYCL/Basic/cgeist/parallel_for.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using namespace sycl;
1010
static constexpr unsigned N = 8;
1111

12-
void host_parallel_for(std::array<int, N> &A) {
12+
void parallel_for(std::array<int, N> &A) {
1313
auto q = queue{};
1414
device d = q.get_device();
1515
std::cout << "Using " << d.get_info<info::device::name>() << "\n";
@@ -28,7 +28,7 @@ void host_parallel_for(std::array<int, N> &A) {
2828

2929
int main() {
3030
std::array<int, N> A{0};
31-
host_parallel_for(A);
31+
parallel_for(A);
3232
for (unsigned i = 0; i < N; ++i) {
3333
assert(A[i] == i);
3434
}

SYCL/Basic/cgeist/single_task.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <sycl/sycl.hpp>
99
using namespace sycl;
1010

11-
void host_single_task(std::array<int, 1> &A) {
11+
void single_task(std::array<int, 1> &A) {
1212
auto q = queue{};
1313
device d = q.get_device();
1414
std::cout << "Using " << d.get_info<info::device::name>() << "\n";
@@ -26,7 +26,7 @@ void host_single_task(std::array<int, 1> &A) {
2626

2727
int main() {
2828
std::array<int, 1> A = {0};
29-
host_single_task(A);
29+
single_task(A);
3030
assert(A[0] == 1);
3131
std::cout << "Test passed" << std::endl;
3232
}

SYCL/Basic/cgeist/stream-copy.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -Xcgeist -gen-all-sycl-funcs %s -o %t.out
2+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
3+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
5+
// REQUIRES: linux
6+
// UNSUPPORTED: hip || cuda
7+
8+
#include <sycl/sycl.hpp>
9+
using namespace sycl;
10+
static constexpr unsigned N = 16;
11+
12+
template <typename T>
13+
void stream_copy(std::array<T, N> &A, std::array<T, N> &B) {
14+
auto q = queue{};
15+
device d = q.get_device();
16+
std::cout << "Using " << d.get_info<info::device::name>() << "\n";
17+
auto range = sycl::range<1>{N};
18+
19+
{
20+
auto bufA = buffer<T, 1>{A.data(), range};
21+
auto bufB = buffer<T, 1>{B.data(), range};
22+
q.submit([&](handler &cgh) {
23+
auto A = bufA.template get_access<access::mode::write>(cgh);
24+
auto B = bufB.template get_access<access::mode::read>(cgh);
25+
cgh.parallel_for<class kernel_stream_copy>(range, [=](sycl::id<1> id) {
26+
A[id] = B[id];
27+
});
28+
});
29+
}
30+
}
31+
32+
int main() {
33+
std::array<int, N> A{0};
34+
std::array<int, N> B{0};
35+
for (unsigned i = 0; i < N; ++i) {
36+
B[i] = i;
37+
}
38+
stream_copy(A, B);
39+
for (unsigned i = 0; i < N; ++i) {
40+
assert(A[i] == i);
41+
}
42+
std::cout << "Test passed" << std::endl;
43+
}

SYCL/Basic/cgeist/stream-triad.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple -Xcgeist -gen-all-sycl-funcs %s -o %t.out
2+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
3+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
5+
// REQUIRES: linux
6+
// UNSUPPORTED: hip || cuda
7+
8+
#include <sycl/sycl.hpp>
9+
using namespace sycl;
10+
static constexpr unsigned N = 16;
11+
static constexpr int scalar = 8;
12+
13+
template <typename T>
14+
void stream_triad(std::array<T, N> &A, std::array<T, N> &B, std::array<T, N> &C) {
15+
auto q = queue{};
16+
device d = q.get_device();
17+
std::cout << "Using " << d.get_info<info::device::name>() << "\n";
18+
auto range = sycl::range<1>{N};
19+
20+
{
21+
auto bufA = buffer<T, 1>{A.data(), range};
22+
auto bufB = buffer<T, 1>{B.data(), range};
23+
auto bufC = buffer<T, 1>{C.data(), range};
24+
q.submit([&](handler &cgh) {
25+
auto A = bufA.template get_access<access::mode::write>(cgh);
26+
auto B = bufB.template get_access<access::mode::read>(cgh);
27+
auto C = bufC.template get_access<access::mode::read>(cgh);
28+
cgh.parallel_for<class kernel_stream_triad>(range, [=](sycl::id<1> id) {
29+
A[id] = B[id] + C[id] * scalar;
30+
});
31+
});
32+
}
33+
}
34+
35+
int main() {
36+
std::array<int, N> A{0};
37+
std::array<int, N> B{0};
38+
std::array<int, N> C{0};
39+
for (unsigned i = 0; i < N; ++i) {
40+
B[i] = C[i] = i;
41+
}
42+
stream_triad(A, B, C);
43+
for (unsigned i = 0; i < N; ++i) {
44+
assert(A[i] == (i + i * scalar));
45+
}
46+
std::cout << "Test passed" << std::endl;
47+
}

0 commit comments

Comments
 (0)