With the following toy code using Intel OneAPI beta6.
#include <CL/sycl.hpp>
#include <iostream>
namespace sycl = cl::sycl;
const int SIZE=1;
class Increment_accessor {
public:
Increment_accessor(sycl::accessor<int, 1, sycl::access::mode::read_write, sycl::access::target::global_buffer> ptr_) : ptr {ptr_} {}
void operator()(sycl::item<1> item) {
ptr[item.get_linear_id()]++;
}
private:
sycl::accessor<int, 1, sycl::access::mode::read_write, sycl::access::target::global_buffer> ptr;
};
class Increment_pointer {
public:
Increment_pointer(sycl::global_ptr<int> ptr_) : ptr {ptr_} {}
void operator()(sycl::item<1> item) {
ptr[item.get_linear_id()]++;
}
private:
sycl::global_ptr<int> ptr;
};
int
main(int argc, char *argv[])
{
sycl::device dev = sycl::default_selector().select_device();
sycl::queue q(dev);
int hbuffer[SIZE] = {};
{
sycl::buffer<int, 1> hbuf(hbuffer, sycl::range<1> {SIZE});
q.submit([&](sycl::handler& cgh) {
auto harray = hbuf.get_access<sycl::access::mode::read_write, sycl::access::target::global_buffer>(cgh);
// !!! Uncomment _one_ of the following lines to compile !!!
//Increment_accessor increment {harray};
//Increment_pointer increment {harray};
//Increment_pointer increment {harray.get_pointer()};
cgh.parallel_for<class kernel1>(
sycl::range<1> {SIZE},
increment
);
}
);
}
for (int i=0; i<SIZE; i++) std::cout << "hbuffer[" << i << "]= " << hbuffer[i] << std::endl;
}
Question: why are the Increment_pointer versions "wrong"? There's no compile/runtime error. You just don't get the incremented hbuffer at the end. (I've played with some similar versions where the ptr in operator() ends up being 0x0).
I'm still learning to think in "SYCL" so elaborating explanation is welcome.