Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.
Merged
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
30 changes: 29 additions & 1 deletion SYCL/Functor/kernel_functor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ constexpr auto sycl_read_write = cl::sycl::access::mode::read_write;
constexpr auto sycl_global_buffer = cl::sycl::access::target::global_buffer;

// Case 1:
// - functor class is defined in an anonymous namespace
// - the '()' operator:
// * does not have parameters (to be used in 'single_task').
// * has the 'const' qualifier
namespace {
class Functor1 {
public:
Functor1(
int X_,
cl::sycl::accessor<int, 1, sycl_read_write, sycl_global_buffer> &Acc_)
: X(X_), Acc(Acc_) {}

void operator()() const { Acc[0] += X; }

private:
int X;
cl::sycl::accessor<int, 1, sycl_read_write, sycl_global_buffer> Acc;
};
} // namespace

// Case 2:
// - functor class is defined in a namespace
// - the '()' operator:
// * does not have parameters (to be used in 'single_task').
Expand Down Expand Up @@ -85,6 +106,13 @@ int foo(int X) {
cl::sycl::queue Q;
cl::sycl::buffer<int, 1> Buf(A, 1);

Q.submit([&](cl::sycl::handler &cgh) {
auto Acc = Buf.get_access<sycl_read_write, sycl_global_buffer>(cgh);
Functor1 F(X, Acc);

cgh.single_task(F);
});

Q.submit([&](cl::sycl::handler &cgh) {
auto Acc = Buf.get_access<sycl_read_write, sycl_global_buffer>(cgh);
ns::Functor2 F(X, Acc);
Expand Down Expand Up @@ -143,7 +171,7 @@ template <typename T> T bar(T X) {
int main() {
const int Res1 = foo(10);
const int Res2 = bar(10);
const int Gold1 = 30;
const int Gold1 = 40;
const int Gold2 = 80;

assert(Res1 == Gold1);
Expand Down