访问读缓冲区拥有的内存

Access memory owned by read buffer

根据 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.

这对于输出缓冲区来说是可以理解的。但是只读缓冲区呢?在下面的代码片段中,在内核完成之前访问只读缓冲区是未定义的行为吗?

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] << ' ';
}

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

由于 in_buf 访问器的访问模式是 read,input_buffer 的内容永远不会在内核中更新。因此在内核完成之前访问 input_buffer 不会产生任何问题。

但是,如果您尝试在内核完成之前访问 output_buffer(其访问模式为写入),您可能会得到 incomplete/incorrect 数据。