Skip to content

Commit f607520

Browse files
s-kanaevromanovvlad
authored andcommitted
[SYCL] Fix working with subbuffers with non-nil offset (#957)
Signed-off-by: Sergey Kanaev <[email protected]>
1 parent 2da1933 commit f607520

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

sycl/source/detail/scheduler/graph_builder.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -318,22 +318,19 @@ Command *Scheduler::GraphBuilder::addHostAccessor(Requirement *Req) {
318318
printGraphAsDot("before_addHostAccessor");
319319
markModifiedIfWrite(Record, Req);
320320

321-
AllocaCommandBase *SrcAllocaCmd = nullptr;
321+
AllocaCommandBase *HostAllocaCmd =
322+
getOrCreateAllocaForReq(Record, Req, HostQueue);
322323

323-
if (Record->MAllocaCommands.empty())
324-
SrcAllocaCmd = getOrCreateAllocaForReq(Record, Req, HostQueue);
325-
else
326-
SrcAllocaCmd = findAllocaForReq(Record, Req, Record->MCurContext);
327-
328-
if (!SrcAllocaCmd->getQueue()->is_host())
324+
if (!sameCtx(HostAllocaCmd->getQueue()->get_context_impl(),
325+
Record->MCurContext))
329326
insertMemoryMove(Record, Req, HostQueue);
330327

331328
Command *UpdateHostAccCmd = insertUpdateHostReqCmd(Record, Req, HostQueue);
332329

333330
// Need empty command to be blocked until host accessor is destructed
334331
EmptyCommand *EmptyCmd = new EmptyCommand(HostQueue, *Req);
335332
EmptyCmd->addDep(
336-
DepDesc{UpdateHostAccCmd, EmptyCmd->getRequirement(), SrcAllocaCmd});
333+
DepDesc{UpdateHostAccCmd, EmptyCmd->getRequirement(), HostAllocaCmd});
337334
UpdateHostAccCmd->addUser(EmptyCmd);
338335

339336
EmptyCmd->MIsBlockable = true;

sycl/test/basic_tests/buffer/subbuffer.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,64 @@ void checkExceptions() {
194194
}
195195
}
196196

197+
void copyBlock() {
198+
using typename cl::sycl::access::mode;
199+
using buffer = cl::sycl::buffer<int, 1>;
200+
201+
auto CopyF = [](buffer& Buffer, buffer& Block, size_t Idx, size_t BlockSize) {
202+
auto Subbuf = buffer(Buffer, Idx * BlockSize, BlockSize);
203+
auto *Src = Subbuf.get_access<mode::read>().get_pointer();
204+
auto *Dst = Block.get_access<mode::write>().get_pointer();
205+
std::copy(Src, Src + BlockSize, Dst);
206+
};
207+
208+
try {
209+
static const size_t N = 100;
210+
static const size_t NBlock = 4;
211+
static const size_t BlockSize = N / NBlock;
212+
213+
buffer Buffer(N);
214+
215+
// Init with data
216+
{
217+
auto *Acc = Buffer.get_access<mode::write>().get_pointer();
218+
219+
for (size_t Idx = 0; Idx < N; Idx++) {
220+
Acc[Idx] = Idx;
221+
}
222+
}
223+
224+
std::vector<buffer> BlockBuffers;
225+
BlockBuffers.reserve(NBlock);
226+
227+
// Copy block by block
228+
for (size_t Idx = 0; Idx < NBlock; Idx++) {
229+
auto InsertedIt = BlockBuffers.emplace(BlockBuffers.end(), BlockSize);
230+
CopyF(Buffer, *InsertedIt, Idx, BlockSize);
231+
}
232+
233+
// Validate copies
234+
for (size_t Idx = 0; Idx < BlockBuffers.size(); ++Idx) {
235+
buffer &BlockB = BlockBuffers[Idx];
236+
237+
auto *V = BlockB.get_access<mode::read>().get_pointer();
238+
239+
for (size_t Idx2 = 0; Idx2 < BlockSize; ++Idx2) {
240+
assert(V[Idx2] == Idx2 + BlockSize * Idx &&
241+
"Invalid data in block buffer");
242+
}
243+
}
244+
}
245+
catch (cl::sycl::exception& ex) {
246+
assert(false && "Unexpected exception captured!");
247+
}
248+
}
249+
197250
int main() {
198251
cl::sycl::queue q;
199252
check1DSubBuffer(q);
200253
checkHostAccessor(q);
201254
checkExceptions();
255+
copyBlock();
202256
return 0;
203257
}

0 commit comments

Comments
 (0)