Skip to content

[SYCL] Do not always create new OpenCL buffers to copy from device to host #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions sycl/include/CL/sycl/detail/buffer_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,31 +390,46 @@ void buffer_impl<AllocatorT>::moveMemoryTo(
Event->setIsHostEvent(false);

OCLState.Queue = std::move(Queue);
OCLState.Mem = nullptr;
return;
}
// Copy from host to OCL device.
if (OCLState.Queue->is_host() && !Queue->is_host()) {
const size_t ByteSize = get_size();
if (nullptr == BufPtr) {
return;
}

cl_int Error;
cl_mem Mem =
clCreateBuffer(Context->getHandleRef(), CL_MEM_READ_WRITE, ByteSize,
/*host_ptr=*/nullptr, &Error);
CHECK_OCL_CODE(Error);
const size_t ByteSize = get_size();

OCLState.Queue = std::move(Queue);
OCLState.Mem = Mem;
// We don't create new OpenCL buffer object to copy from OCL device to host
// when we already have them in OCLState. But if contexts of buffer object
// from OCLState and input Queue are not same - we should create new OpenCL
// buffer object.
bool NeedToCreateCLBuffer = true;

if (OCLState.Mem != nullptr) {
cl_context MemCtx;
Error = clGetMemObjectInfo(OCLState.Mem, CL_MEM_CONTEXT,
sizeof(cl_context), &MemCtx, nullptr);
CHECK_OCL_CODE(Error);
NeedToCreateCLBuffer = MemCtx != Context->getHandleRef();
}

// Just exit if nothing to read from host.
if (nullptr == BufPtr) {
return;
if (NeedToCreateCLBuffer) {
OCLState.Mem =
clCreateBuffer(Context->getHandleRef(), CL_MEM_READ_WRITE, ByteSize,
/*host_ptr=*/nullptr, &Error);
CHECK_OCL_CODE(Error);
}

OCLState.Queue = std::move(Queue);

std::vector<cl_event> CLEvents =
detail::getOrWaitEvents(std::move(DepEvents), Context);
cl_event &WriteBufEvent = Event->getHandleRef();
// Enqueue copying from host to new OCL buffer.
Error =
clEnqueueWriteBuffer(OCLState.Queue->getHandleRef(), Mem,
clEnqueueWriteBuffer(OCLState.Queue->getHandleRef(), OCLState.Mem,
/*blocking_write=*/CL_FALSE, /*offset=*/0,
ByteSize, BufPtr, CLEvents.size(), CLEvents.data(),
&WriteBufEvent); // replace &WriteBufEvent to NULL
Expand Down