Skip to content

Commit ddc0c9d

Browse files
authored
[SYCL] Fix handler::copy(acc, ptr) and handler::copy(ptr, acc) (#1684)
This patch fixes the cases when the memory referenced by a pointer is either 'const void *' or 'void *'. It also fixes handler::copy(ptr, acc) where 'acc' accesses elements having type with const qualifier. Signed-off-by: Vyacheslav N Klochkov <[email protected]>
1 parent 77d9dfb commit ddc0c9d

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

sycl/include/CL/sycl/handler.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,8 @@ class __SYCL_EXPORT handler {
539539
size_t LinearIndex = Index[0];
540540
for (int I = 1; I < Dim; ++I)
541541
LinearIndex += Range[I] * Index[I];
542-
(reinterpret_cast<TSrc *>(Dst))[LinearIndex] = Src[Index];
542+
using TSrcNonConst = typename std::remove_const<TSrc>::type;
543+
(reinterpret_cast<TSrcNonConst *>(Dst))[LinearIndex] = Src[Index];
543544
});
544545
}
545546

@@ -555,7 +556,8 @@ class __SYCL_EXPORT handler {
555556
TDst *Dst) {
556557
single_task<class __copyAcc2Ptr<TSrc, TDst, Dim, AccMode, AccTarget, IsPH>>
557558
([=]() {
558-
*Dst = readFromFirstAccElement(Src);
559+
using TSrcNonConst = typename std::remove_const<TSrc>::type;
560+
*(reinterpret_cast<TSrcNonConst *>(Dst)) = readFromFirstAccElement(Src);
559561
});
560562
}
561563

@@ -566,15 +568,15 @@ class __SYCL_EXPORT handler {
566568
template <typename TSrc, typename TDst, int Dim, access::mode AccMode,
567569
access::target AccTarget, access::placeholder IsPH>
568570
detail::enable_if_t<(Dim > 0)>
569-
copyPtrToAccHost(TDst *Src,
570-
accessor<TSrc, Dim, AccMode, AccTarget, IsPH> Dst) {
571+
copyPtrToAccHost(TSrc *Src,
572+
accessor<TDst, Dim, AccMode, AccTarget, IsPH> Dst) {
571573
range<Dim> Range = Dst.get_range();
572574
parallel_for<class __copyPtr2Acc<TSrc, TDst, Dim, AccMode, AccTarget, IsPH>>
573575
(Range, [=](id<Dim> Index) {
574576
size_t LinearIndex = Index[0];
575577
for (int I = 1; I < Dim; ++I)
576578
LinearIndex += Range[I] * Index[I];
577-
Dst[Index] = (reinterpret_cast<TDst *>(Src))[LinearIndex];
579+
Dst[Index] = (reinterpret_cast<const TDst *>(Src))[LinearIndex];
578580
});
579581
}
580582

@@ -586,11 +588,11 @@ class __SYCL_EXPORT handler {
586588
template <typename TSrc, typename TDst, int Dim, access::mode AccMode,
587589
access::target AccTarget, access::placeholder IsPH>
588590
detail::enable_if_t<Dim == 0>
589-
copyPtrToAccHost(TDst *Src,
590-
accessor<TSrc, Dim, AccMode, AccTarget, IsPH> Dst) {
591+
copyPtrToAccHost(TSrc *Src,
592+
accessor<TDst, Dim, AccMode, AccTarget, IsPH> Dst) {
591593
single_task<class __copyPtr2Acc<TSrc, TDst, Dim, AccMode, AccTarget, IsPH>>
592594
([=]() {
593-
writeToFirstAccElement(Dst, *Src);
595+
writeToFirstAccElement(Dst, *(reinterpret_cast<const TDst *>(Src)));
594596
});
595597
}
596598
#endif // __SYCL_DEVICE_ONLY__

sycl/test/basic_tests/handler/handler_mem_op.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,20 @@ template <typename T> void test_copy_ptr_acc() {
252252
assert(Data[I] == Values[I]);
253253
}
254254

255+
// Check copy from 'const void *' memory to accessor.
256+
{
257+
buffer<T, 1> Buffer(Size);
258+
queue Queue;
259+
Queue.submit([&](handler &Cgh) {
260+
auto Acc = Buffer.template get_access<access::mode::discard_write>(Cgh);
261+
Cgh.copy(reinterpret_cast<const void *>(Values), Acc);
262+
});
263+
264+
auto Acc = Buffer.template get_access<access::mode::read>();
265+
for (int I = 0; I < Size; ++I)
266+
assert(Acc[I] == Values[I]);
267+
}
268+
255269
// Check copy from memory to 0-dimensional accessor.
256270
T SrcValue = 99;
257271
T DstValue = 0;
@@ -287,6 +301,21 @@ template <typename T> void test_copy_acc_ptr() {
287301
assert(Data[I] == Values[I]);
288302
}
289303

304+
// Check copy from 'const T' accessor to 'void *' memory.
305+
{
306+
buffer<const T, 1> Buffer(reinterpret_cast<const T *>(Data), range<1>(Size));
307+
T Dst[Size] = {0};
308+
queue Queue;
309+
Queue.submit([&](handler &Cgh) {
310+
auto Acc = Buffer.template get_access<access::mode::read>(Cgh);
311+
Cgh.copy(Acc, reinterpret_cast<void *>(Dst));
312+
});
313+
Queue.wait();
314+
315+
for (int I = 0; I < Size; ++I)
316+
assert(Dst[I] == Data[I]);
317+
}
318+
290319
// Check copy from 0-dimensional accessor to memory
291320
T SrcValue = 99;
292321
T DstValue = 0;

0 commit comments

Comments
 (0)