2

According SYCL reference

Buffers can be initialized by a host data pointer. While the buffer exists, it owns the host data and direct access of the host data pointer during that time is undefined.

Which is understandable for output buffers. But what about read only buffer? In the following code fragment is it undefined behavior to access read only buffer before kernel completes?

range<1> bufer_size{ 100 };

buffer<int> in_buf(input_buffer, bufer_size);
buffer<int> out_buf(output_buffer, bufer_size);

auto my_device = default_selector{};
queue q{ my_device };

q.submit([&](handler& h) {
    auto x = in_buf.get_access<access::mode::read>(h);
    auto y = out_buf.get_access<access::mode::write>(h);

    h.parallel_for(bufer_size,
        [=](id<1> idx) {
            y[idx] = 100 * x[idx] + 1;
        });
    });

// Is it OK to read input_buffer without waiting for queue to complete?
for (int i = 0; i < size; ++i) {
    std::cout << input_buffer[i] << ' ';
}
pic11
  • 14,267
  • 21
  • 83
  • 119

1 Answers1

0

buffer in_buf(input_buffer, bufer_size);
auto x = in_buf.get_accessaccess::mode::read(h);

As the access mode of in_buf accessor is read, the contents of the input_buffer will never get updated inside the kernel. So accessing the input_buffer before the completion of the kernel doesn't create any issues.

However, if you try to access the output_buffer (which has the access mode as write) before the completion of the kernel you may get incomplete/incorrect data.