Skip to content

Commit f20fd4d

Browse files
[SYCL] Fix explicit copy operation for host device (#2627)
Fix the linear index calculation in handler::copy() for host-to-host-device and host-device-to-host cases. Signed-off-by: Sergey Semenov <[email protected]>
1 parent e8d9cc8 commit f20fd4d

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

sycl/include/CL/sycl/handler.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,7 @@ class __SYCL_EXPORT handler {
641641
range<Dim> Range = Src.get_range();
642642
parallel_for<class __copyAcc2Ptr<TSrc, TDst, Dim, AccMode, AccTarget, IsPH>>
643643
(Range, [=](id<Dim> Index) {
644-
size_t LinearIndex = Index[0];
645-
for (int I = 1; I < Dim; ++I)
646-
LinearIndex += Range[I] * Index[I];
644+
const size_t LinearIndex = detail::getLinearIndex(Index, Range);
647645
using TSrcNonConst = typename std::remove_const<TSrc>::type;
648646
(reinterpret_cast<TSrcNonConst *>(Dst))[LinearIndex] = Src[Index];
649647
});
@@ -678,9 +676,7 @@ class __SYCL_EXPORT handler {
678676
range<Dim> Range = Dst.get_range();
679677
parallel_for<class __copyPtr2Acc<TSrc, TDst, Dim, AccMode, AccTarget, IsPH>>
680678
(Range, [=](id<Dim> Index) {
681-
size_t LinearIndex = Index[0];
682-
for (int I = 1; I < Dim; ++I)
683-
LinearIndex += Range[I] * Index[I];
679+
const size_t LinearIndex = detail::getLinearIndex(Index, Range);
684680
Dst[Index] = (reinterpret_cast<const TDst *>(Src))[LinearIndex];
685681
});
686682
}

sycl/test/basic_tests/handler/handler_mem_op.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ template <typename T> struct point {
3434

3535
template <typename T> void test_fill(T Val);
3636
template <typename T> void test_copy_ptr_acc();
37+
template <typename T> void test_3D_copy_ptr_acc();
3738
template <typename T> void test_copy_acc_ptr();
39+
template <typename T> void test_3D_copy_acc_ptr();
3840
template <typename T> void test_copy_shared_ptr_acc();
3941
template <typename T> void test_copy_shared_ptr_const_acc();
4042
template <typename T> void test_copy_acc_shared_ptr();
@@ -72,6 +74,14 @@ int main() {
7274
test_copy_ptr_acc<point<int>>();
7375
test_copy_ptr_acc<point<float>>();
7476
}
77+
// handler.copy(ptr, acc) 3D
78+
{
79+
test_3D_copy_ptr_acc<int>();
80+
test_3D_copy_ptr_acc<int>();
81+
test_3D_copy_ptr_acc<point<int>>();
82+
test_3D_copy_ptr_acc<point<int>>();
83+
test_3D_copy_ptr_acc<point<float>>();
84+
}
7585
// handler.copy(acc, ptr)
7686
{
7787
test_copy_acc_ptr<int>();
@@ -80,6 +90,14 @@ int main() {
8090
test_copy_acc_ptr<point<int>>();
8191
test_copy_acc_ptr<point<float>>();
8292
}
93+
// handler.copy(acc, ptr) 3D
94+
{
95+
test_3D_copy_acc_ptr<int>();
96+
test_3D_copy_acc_ptr<int>();
97+
test_3D_copy_acc_ptr<point<int>>();
98+
test_3D_copy_acc_ptr<point<int>>();
99+
test_3D_copy_acc_ptr<point<float>>();
100+
}
83101
// handler.copy(shared_ptr, acc)
84102
{
85103
test_copy_shared_ptr_acc<int>();
@@ -277,6 +295,28 @@ template <typename T> void test_copy_ptr_acc() {
277295
assert(DstValue == 99);
278296
}
279297

298+
template <typename T> void test_3D_copy_ptr_acc() {
299+
const range<3> Range{2, 3, 4};
300+
const size_t Size = 2 * 3 * 4;
301+
T Data[Size] = {0};
302+
T Values[Size] = {0};
303+
for (size_t I = 0; I < Size; ++I)
304+
Values[I] = I;
305+
306+
{
307+
buffer<T, 3> Buffer(Data, Range);
308+
queue Queue;
309+
Queue.submit([&](handler &Cgh) {
310+
accessor<T, 3, access::mode::write, access::target::global_buffer>
311+
Accessor(Buffer, Cgh, Range);
312+
Cgh.copy(Values, Accessor);
313+
});
314+
}
315+
316+
for (int I = 0; I < Size; ++I)
317+
assert(Data[I] == Values[I]);
318+
}
319+
280320
template <typename T> void test_copy_acc_ptr() {
281321
const size_t Size = 10;
282322
T Data[Size] = {0};
@@ -345,6 +385,28 @@ template <typename T> void test_copy_acc_ptr() {
345385
assert(DstValue == 77);
346386
}
347387

388+
template <typename T> void test_3D_copy_acc_ptr() {
389+
const range<3> Range{2, 3, 4};
390+
const size_t Size = 2 * 3 * 4;
391+
T Data[Size] = {0};
392+
T Values[Size] = {0};
393+
for (size_t I = 0; I < Size; ++I)
394+
Data[I] = I;
395+
396+
{
397+
buffer<T, 3> Buffer(Data, Range);
398+
queue Queue;
399+
Queue.submit([&](handler &Cgh) {
400+
accessor<T, 3, access::mode::read, access::target::global_buffer>
401+
Accessor(Buffer, Cgh, Range);
402+
Cgh.copy(Accessor, Values);
403+
});
404+
}
405+
406+
for (size_t I = 0; I < Size; ++I)
407+
assert(Data[I] == Values[I]);
408+
}
409+
348410
template <typename T> void test_copy_shared_ptr_acc() {
349411
const size_t Size = 10;
350412
T Data[Size] = {0};

0 commit comments

Comments
 (0)