Skip to content

Commit

Permalink
[SYCL] Implement buffer::set_write_back
Browse files Browse the repository at this point in the history
Signed-off-by: Mariya Podchishchaeva <mariya.podchishchaeva@intel.com>
  • Loading branch information
Fznamznon authored and vladimirlaz committed Apr 1, 2019
1 parent 0b4e9e9 commit 474c934
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
3 changes: 1 addition & 2 deletions sycl/include/CL/sycl/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ class buffer {
impl->set_final_data(finalData);
}

// void set_write_back(bool flag = true) { return impl->set_write_back(flag);
// }
void set_write_back(bool flag = true) { return impl->set_write_back(flag); }

// bool is_sub_buffer() const { return impl->is_sub_buffer(); }

Expand Down
5 changes: 4 additions & 1 deletion sycl/include/CL/sycl/detail/buffer_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ template <typename AllocatorT> class buffer_impl {
.copyBack<access::mode::read_write, access::target::host_buffer>(
*this);

if (uploadData != nullptr) {
if (uploadData != nullptr && NeedWriteBack) {
uploadData();
}

Expand Down Expand Up @@ -187,6 +187,8 @@ template <typename AllocatorT> class buffer_impl {
};
}

void set_write_back(bool flag) { NeedWriteBack = flag; }

AllocatorT get_allocator() const { return MAllocator; }

template <typename T, int dimensions, access::mode mode,
Expand Down Expand Up @@ -281,6 +283,7 @@ template <typename AllocatorT> class buffer_impl {
AllocatorT MAllocator;
OpenCLMemState OCLState;
bool OpenCLInterop = false;
bool NeedWriteBack = true;
event AvailableEvent;
cl_context OpenCLContext = nullptr;
void *BufPtr = nullptr;
Expand Down
51 changes: 51 additions & 0 deletions sycl/test/basic_tests/buffer/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using namespace cl::sycl;

int main() {
int data = 5;
bool failed = false;
buffer<int, 1> buf(&data, range<1>(1));
{
int data1[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
Expand Down Expand Up @@ -453,5 +454,55 @@ int main() {
for (int i = 5; i < 10; i++)
assert(data1[i] == -1);
}

// Check that data is copied back after forcing write-back using
// set_write_back
{
std::vector<int> data1(10, -1);
{
buffer<int, 1> b(range<1>(10));
b.set_final_data(data1.data());
b.set_write_back(true);
queue myQueue;
myQueue.submit([&](handler &cgh) {
auto B = b.get_access<access::mode::read_write>(cgh);
cgh.parallel_for<class wb>(range<1>{10},
[=](id<1> index) { B[index] = 0; });
});

}
// Data is copied back because there is a user side ptr and write-back is
// enabled
for (int i = 0; i < 10; i++)
if (data1[i] != 0) {
assert(false);
failed = true;
}
}

// Check that data is not copied back after canceling write-back using
// set_write_back
{
std::vector<int> data1(10, -1);
{
buffer<int, 1> b(range<1>(10));
b.set_final_data(data1.data());
b.set_write_back(false);
queue myQueue;
myQueue.submit([&](handler &cgh) {
auto B = b.get_access<access::mode::read_write>(cgh);
cgh.parallel_for<class notwb>(range<1>{10},
[=](id<1> index) { B[index] = 0; });
});

}
// Data is not copied back because write-back is canceled
for (int i = 0; i < 10; i++)
if (data1[i] != -1) {
assert(false);
failed = true;
}
}
// TODO tests with mutex property
return failed;
}

0 comments on commit 474c934

Please sign in to comment.