Skip to content

Commit 5c8e81f

Browse files
alexeyvoronov-intelromanovvlad
authored andcommitted
[SYCL] Fix handler::copy [from|to] [raw|shared] pointer.
Case 1: There is a call handler copy method from a pointer (used the accessor range and offset of 0) to an accessor(with a range of 4 and an offset of 2). src_ptr = [0,1,2,3,4,5,6,7] // the data that the pointer points to dest_acc = [x,x,x,x,x,x,x,x] // the data that the accessor points to handler.copy(src_ptr, dest_acc<1>{range{4}, offset{2}} Before this patch dest_acc had: "x x 2 3 4 5 x x" After this patch dest_acc has: "x x 0 1 2 3 x x" The root cause: the destination offset was used for the source. The fix: use the source offset for the source. Case 2: There is a call handler copy method from an accessor(with a range of 4 and an offset of 2) to a pointer (used the accessor range and offset of 0). src_acc = [0,1,2,3,4,5,6,7] // // the data that the accessor points to dest_ptr = [x,x,x,x,x,x,x,x] // the data that the pointer points to handler.copy(src_acc<1>{range{4}, offset{2}, dest_ptr} Before this patch had dest_ptr: "0 1 2 3 x x x x" After this patch has dest_ptr: "2 3 4 5 x x x x" The root cause: the destination offset was used for the source. The fix: use the source offset for the source. Signed-off-by: Alexey Voronov <[email protected]>
1 parent ec738f2 commit 5c8e81f

File tree

3 files changed

+85
-8
lines changed

3 files changed

+85
-8
lines changed

sycl/source/detail/memory_manager.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,13 @@ void copyH2D(SYCLMemObjI *SYCLMemObj, char *SrcMem, QueueImplPtr SrcQueue,
217217
PI_CALL(RT::piEnqueueMemBufferWrite(
218218
Queue, DstMem,
219219
/*blocking_write=*/CL_FALSE, DstOffset[0], DstAccessRange[0],
220-
SrcMem + DstOffset[0], DepEvents.size(), &DepEvents[0], &OutEvent));
220+
SrcMem + SrcOffset[0], DepEvents.size(), &DepEvents[0], &OutEvent));
221221
} else {
222-
size_t BufferRowPitch = (1 == DimSrc) ? 0 : SrcSize[0];
223-
size_t BufferSlicePitch = (3 == DimSrc) ? SrcSize[0] * SrcSize[1] : 0;
222+
size_t BufferRowPitch = (1 == DimDst) ? 0 : DstSize[0];
223+
size_t BufferSlicePitch = (3 == DimDst) ? DstSize[0] * DstSize[1] : 0;
224224

225-
size_t HostRowPitch = (1 == DimDst) ? 0 : DstSize[0];
226-
size_t HostSlicePitch = (3 == DimDst) ? DstSize[0] * DstSize[1] : 0;
225+
size_t HostRowPitch = (1 == DimSrc) ? 0 : SrcSize[0];
226+
size_t HostSlicePitch = (3 == DimSrc) ? SrcSize[0] * SrcSize[1] : 0;
227227
PI_CALL(RT::piEnqueueMemBufferWriteRect(
228228
Queue, DstMem,
229229
/*blocking_write=*/CL_FALSE, &DstOffset[0], &SrcOffset[0],
@@ -266,7 +266,7 @@ void copyD2H(SYCLMemObjI *SYCLMemObj, RT::PiMem SrcMem, QueueImplPtr SrcQueue,
266266
if (1 == DimDst && 1 == DimSrc) {
267267
PI_CALL(RT::piEnqueueMemBufferRead(
268268
Queue, SrcMem,
269-
/*blocking_read=*/CL_FALSE, DstOffset[0], DstAccessRange[0],
269+
/*blocking_read=*/CL_FALSE, SrcOffset[0], SrcAccessRange[0],
270270
DstMem + DstOffset[0], DepEvents.size(), &DepEvents[0], &OutEvent));
271271
} else {
272272
size_t BufferRowPitch = (1 == DimSrc) ? 0 : SrcSize[0];
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// RUN: %clangxx -fsycl %s -o %t.out
2+
// RUN: env SYCL_DEVICE_TYPE=HOST %t.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
6+
//==--- handler_copy_with_offset.cpp - SYCL handler copy with offset test --==//
7+
//
8+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9+
// See https://llvm.org/LICENSE.txt for license information.
10+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include <CL/sycl.hpp>
15+
16+
#include <cassert>
17+
#include <exception>
18+
#include <memory>
19+
#include <numeric>
20+
#include <vector>
21+
22+
using namespace cl::sycl;
23+
constexpr access::mode read = access::mode::read;
24+
constexpr access::mode write = access::mode::write;
25+
constexpr access::target global_buffer = access::target::global_buffer;
26+
27+
int main() {
28+
{
29+
constexpr size_t Size = 8;
30+
31+
vector_class<char> DataRaw(Size, 'x');
32+
{
33+
buffer<char> Buffer{DataRaw.data(), range<1>{Size}};
34+
35+
vector_class<char> DataGold(Size);
36+
std::iota(DataGold.begin(), DataGold.end(), '0');
37+
38+
queue Queue;
39+
Queue.submit([&](handler &CGH) {
40+
range<1> AccessRange{4};
41+
id<1> AccessOffset{2};
42+
auto Accessor = Buffer.get_access<write, global_buffer>(
43+
CGH, AccessRange, AccessOffset);
44+
CGH.copy(DataGold.data(), Accessor);
45+
});
46+
Queue.wait();
47+
}
48+
49+
vector_class<char> Expected{'x', 'x', '0', '1', '2', '3', 'x', 'x'};
50+
if (DataRaw != Expected)
51+
throw std::runtime_error("Check of hadler.copy(ptr, acc) was failed");
52+
}
53+
54+
{
55+
constexpr size_t Size = 8;
56+
vector_class<char> DataRaw(Size, 'x');
57+
{
58+
vector_class<char> DataGold(Size);
59+
std::iota(DataGold.begin(), DataGold.end(), '0');
60+
buffer<char> Buffer{DataGold.data(), range<1>{Size}};
61+
62+
queue Queue;
63+
Queue.submit([&](handler &CGH) {
64+
range<1> AccessRange{4};
65+
id<1> AccessOffset{2};
66+
auto Accessor = Buffer.get_access<read, global_buffer>(CGH, AccessRange,
67+
AccessOffset);
68+
CGH.copy(Accessor, DataRaw.data());
69+
});
70+
Queue.wait();
71+
}
72+
vector_class<char> Expected{'2', '3', '4', '5', 'x', 'x', 'x', 'x'};
73+
if (DataRaw != Expected)
74+
throw std::runtime_error("Check of hadler.copy(acc, ptr) was failed");
75+
}
76+
return 0;
77+
}

sycl/test/regression/memcpy-update-leafs.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int main() {
3838
return 0;
3939
}
4040

41-
// CHECK: PI ---> RT::piEnqueueMemBufferWrite( Queue, DstMem, CL_FALSE, DstOffset[0], DstAccessRange[0], SrcMem + DstOffset[0], DepEvents.size(), &DepEvents[0], &OutEvent)
42-
// CHECK-NOT: PI ---> RT::piEnqueueMemBufferWrite( Queue, DstMem, CL_FALSE, DstOffset[0], DstAccessRange[0], SrcMem + DstOffset[0], DepEvents.size(), &DepEvents[0], &OutEvent)
41+
// CHECK: PI ---> RT::piEnqueueMemBufferWrite( Queue, DstMem, CL_FALSE, DstOffset[0], DstAccessRange[0], SrcMem + SrcOffset[0], DepEvents.size(), &DepEvents[0], &OutEvent)
42+
// CHECK-NOT: PI ---> RT::piEnqueueMemBufferWrite( Queue, DstMem, CL_FALSE, DstOffset[0], DstAccessRange[0], SrcMem + SrcOffset[0], DepEvents.size(), &DepEvents[0], &OutEvent)
4343
// CHECK-NOT: PI ---> (MappedPtr = RT::piEnqueueMemBufferMap( Queue->getHandleRef(), pi::cast<RT::PiMem>(Mem), CL_FALSE, Flags, AccessOffset[0], AccessRange[0], DepEvents.size(), DepEvents.empty() ? nullptr : &DepEvents[0], &OutEvent, &Error), Error)
4444
// CHECK-NOT: PI ---> RT::piEnqueueMemUnmap( UseExclusiveQueue ? Queue->getExclusiveQueueHandleRef() : Queue->getHandleRef(), pi::cast<RT::PiMem>(Mem), MappedPtr, DepEvents.size(), DepEvents.empty() ? nullptr : &DepEvents[0], &OutEvent)

0 commit comments

Comments
 (0)