Skip to content

Commit 474c934

Browse files
Fznamznonvladimirlaz
authored andcommitted
[SYCL] Implement buffer::set_write_back
Signed-off-by: Mariya Podchishchaeva <[email protected]>
1 parent 0b4e9e9 commit 474c934

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

sycl/include/CL/sycl/buffer.hpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ class buffer {
175175
impl->set_final_data(finalData);
176176
}
177177

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

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

sycl/include/CL/sycl/detail/buffer_impl.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ template <typename AllocatorT> class buffer_impl {
145145
.copyBack<access::mode::read_write, access::target::host_buffer>(
146146
*this);
147147

148-
if (uploadData != nullptr) {
148+
if (uploadData != nullptr && NeedWriteBack) {
149149
uploadData();
150150
}
151151

@@ -187,6 +187,8 @@ template <typename AllocatorT> class buffer_impl {
187187
};
188188
}
189189

190+
void set_write_back(bool flag) { NeedWriteBack = flag; }
191+
190192
AllocatorT get_allocator() const { return MAllocator; }
191193

192194
template <typename T, int dimensions, access::mode mode,
@@ -281,6 +283,7 @@ template <typename AllocatorT> class buffer_impl {
281283
AllocatorT MAllocator;
282284
OpenCLMemState OCLState;
283285
bool OpenCLInterop = false;
286+
bool NeedWriteBack = true;
284287
event AvailableEvent;
285288
cl_context OpenCLContext = nullptr;
286289
void *BufPtr = nullptr;

sycl/test/basic_tests/buffer/buffer.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ using namespace cl::sycl;
1919

2020
int main() {
2121
int data = 5;
22+
bool failed = false;
2223
buffer<int, 1> buf(&data, range<1>(1));
2324
{
2425
int data1[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
@@ -453,5 +454,55 @@ int main() {
453454
for (int i = 5; i < 10; i++)
454455
assert(data1[i] == -1);
455456
}
457+
458+
// Check that data is copied back after forcing write-back using
459+
// set_write_back
460+
{
461+
std::vector<int> data1(10, -1);
462+
{
463+
buffer<int, 1> b(range<1>(10));
464+
b.set_final_data(data1.data());
465+
b.set_write_back(true);
466+
queue myQueue;
467+
myQueue.submit([&](handler &cgh) {
468+
auto B = b.get_access<access::mode::read_write>(cgh);
469+
cgh.parallel_for<class wb>(range<1>{10},
470+
[=](id<1> index) { B[index] = 0; });
471+
});
472+
473+
}
474+
// Data is copied back because there is a user side ptr and write-back is
475+
// enabled
476+
for (int i = 0; i < 10; i++)
477+
if (data1[i] != 0) {
478+
assert(false);
479+
failed = true;
480+
}
481+
}
482+
483+
// Check that data is not copied back after canceling write-back using
484+
// set_write_back
485+
{
486+
std::vector<int> data1(10, -1);
487+
{
488+
buffer<int, 1> b(range<1>(10));
489+
b.set_final_data(data1.data());
490+
b.set_write_back(false);
491+
queue myQueue;
492+
myQueue.submit([&](handler &cgh) {
493+
auto B = b.get_access<access::mode::read_write>(cgh);
494+
cgh.parallel_for<class notwb>(range<1>{10},
495+
[=](id<1> index) { B[index] = 0; });
496+
});
497+
498+
}
499+
// Data is not copied back because write-back is canceled
500+
for (int i = 0; i < 10; i++)
501+
if (data1[i] != -1) {
502+
assert(false);
503+
failed = true;
504+
}
505+
}
456506
// TODO tests with mutex property
507+
return failed;
457508
}

0 commit comments

Comments
 (0)