Skip to content

Commit 540fe39

Browse files
alexeyvoronov-intelvladimirlaz
authored andcommitted
[SYCL] Add getHandleRef method to the context_impl class.
This method is used in the SYCL Runtime unstead get method to avoid overhead retain and release logic. Signed-off-by: Voronov, Alexey <[email protected]> Signed-off-by: Vladimir Lazarev <[email protected]>
1 parent bbd85f2 commit 540fe39

File tree

10 files changed

+71
-57
lines changed

10 files changed

+71
-57
lines changed

sycl/include/CL/sycl/detail/buffer_impl.hpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace cl {
2929
namespace sycl {
3030
using QueueImplPtr = std::shared_ptr<detail::queue_impl>;
3131
using EventImplPtr = std::shared_ptr<cl::sycl::detail::event_impl>;
32+
using ContextImplPtr = std::shared_ptr<cl::sycl::detail::context_impl>;
3233
// Forward declarations
3334
template <typename dataT, int dimensions, access::mode accessmode,
3435
access::target accessTarget, access::placeholder isPlaceholder>
@@ -120,8 +121,9 @@ class buffer_impl {
120121
"allowed");
121122

122123
CHECK_OCL_CODE(clGetMemObjectInfo(MemObject, CL_MEM_CONTEXT,
123-
sizeof(OpenCLContext), &OpenCLContext, nullptr));
124-
if (SyclContext.get() != OpenCLContext)
124+
sizeof(OpenCLContext), &OpenCLContext,
125+
nullptr));
126+
if (detail::getSyclObjImpl(SyclContext)->getHandleRef() != OpenCLContext)
125127
throw cl::sycl::invalid_parameter_error(
126128
"Input context must be the same as the context of cl_mem");
127129
OCLState.Mem = MemObject;
@@ -268,7 +270,7 @@ void buffer_impl<T, dimensions, AllocatorT>::fill(
268270
size_t Offset = OffsetArr[0];
269271
size_t Size = RangeArr[0] * PatternSize;
270272

271-
cl::sycl::context Context = Queue->get_context();
273+
ContextImplPtr Context = detail::getSyclObjImpl(Queue->get_context());
272274

273275
OCLState.Queue = std::move(Queue);
274276
Event->setIsHostEvent(false);
@@ -278,9 +280,9 @@ void buffer_impl<T, dimensions, AllocatorT>::fill(
278280
detail::getOrWaitEvents(std::move(DepEvents), Context);
279281

280282
cl_command_queue CommandQueue = OCLState.Queue->get();
281-
cl_int Error = clEnqueueFillBuffer(
282-
CommandQueue, OCLState.Mem, Pattern, PatternSize, Offset, Size,
283-
CLEvents.size(), CLEvents.data(), &BufEvent);
283+
cl_int Error = clEnqueueFillBuffer(CommandQueue, OCLState.Mem, Pattern,
284+
PatternSize, Offset, Size, CLEvents.size(),
285+
CLEvents.data(), &BufEvent);
284286

285287
CHECK_OCL_CODE(Error);
286288
CHECK_OCL_CODE(clReleaseCommandQueue(CommandQueue));
@@ -299,7 +301,7 @@ void buffer_impl<T, dimensions, AllocatorT>::copy(
299301
size_t SizeTyDest = sizeof(T);
300302
const int DimDest = dimensions;
301303

302-
cl::sycl::context Context = Queue->get_context();
304+
ContextImplPtr Context = detail::getSyclObjImpl(Queue->get_context());
303305

304306
cl_event &BufEvent = Event->getHandleRef();
305307
std::vector<cl_event> CLEvents =
@@ -314,11 +316,11 @@ void buffer_impl<T, dimensions, AllocatorT>::copy(
314316
CLEvents.data(), &BufEvent);
315317
} else {
316318
size_t SrcOrigin[3] = {SrcOffset[0] * SizeTySrc,
317-
(1 == DimSrc) ? 0 : SrcOffset[1],
318-
(3 == DimSrc) ? SrcOffset[2] : 0};
319+
(1 == DimSrc) ? 0 : SrcOffset[1],
320+
(3 == DimSrc) ? SrcOffset[2] : 0};
319321
size_t DstOrigin[3] = {DestOffset[0] * SizeTyDest,
320-
(1 == DimDest) ? 0 : DestOffset[1],
321-
(3 == DimDest) ? DestOffset[2] : 0};
322+
(1 == DimDest) ? 0 : DestOffset[1],
323+
(3 == DimDest) ? DestOffset[2] : 0};
322324
size_t Region[3] = {SrcRange[0] * SizeTySrc,
323325
(1 == DimSrc) ? 1 : SrcRange[1],
324326
(3 == DimSrc) ? SrcRange[2] : 1};
@@ -345,9 +347,9 @@ void buffer_impl<T, dimensions, AllocatorT>::moveMemoryTo(
345347
QueueImplPtr Queue, std::vector<cl::sycl::event> DepEvents,
346348
EventImplPtr Event) {
347349

348-
cl::sycl::context Context = Queue->get_context();
350+
ContextImplPtr Context = detail::getSyclObjImpl(Queue->get_context());
349351

350-
if (OpenCLInterop && (Context.get() != OpenCLContext))
352+
if (OpenCLInterop && (Context->getHandleRef() != OpenCLContext))
351353
throw cl::sycl::runtime_error(
352354
"Interoperability buffer could not be used in a context other than the "
353355
"context associated with the OpenCL memory object.");
@@ -364,7 +366,7 @@ void buffer_impl<T, dimensions, AllocatorT>::moveMemoryTo(
364366
return;
365367
}
366368

367-
assert(OCLState.Queue->get_context() != Context ||
369+
assert(OCLState.Queue->get_context() != Queue->get_context() ||
368370
OCLState.Queue->get_device() != Queue->get_device() &&
369371
"Attempt to move to the same env");
370372

@@ -394,8 +396,9 @@ void buffer_impl<T, dimensions, AllocatorT>::moveMemoryTo(
394396
if (OCLState.Queue->is_host() && !Queue->is_host()) {
395397
const size_t ByteSize = get_size();
396398
cl_int Error;
397-
cl_mem Mem = clCreateBuffer(Context.get(), CL_MEM_READ_WRITE, ByteSize,
398-
/*host_ptr=*/nullptr, &Error);
399+
cl_mem Mem =
400+
clCreateBuffer(Context->getHandleRef(), CL_MEM_READ_WRITE, ByteSize,
401+
/*host_ptr=*/nullptr, &Error);
399402
CHECK_OCL_CODE(Error);
400403

401404
OCLState.Queue = std::move(Queue);
@@ -461,9 +464,9 @@ void buffer_impl<T, dimensions, AllocatorT>::allocate(
461464

462465
detail::waitEvents(DepEvents);
463466

464-
cl::sycl::context Context = Queue->get_context();
467+
ContextImplPtr Context = detail::getSyclObjImpl(Queue->get_context());
465468

466-
if (OpenCLInterop && (Context.get() != OpenCLContext))
469+
if (OpenCLInterop && (Context->getHandleRef() != OpenCLContext))
467470
throw cl::sycl::runtime_error(
468471
"Interoperability buffer could not be used in a context other than the "
469472
"context associated with the OpenCL memory object.");
@@ -479,8 +482,9 @@ void buffer_impl<T, dimensions, AllocatorT>::allocate(
479482
size_t ByteSize = get_size();
480483
cl_int Error;
481484

482-
cl_mem Mem = clCreateBuffer(Context.get(), convertSycl2OCLMode(mode),
483-
ByteSize, nullptr, &Error);
485+
cl_mem Mem =
486+
clCreateBuffer(Context->getHandleRef(), convertSycl2OCLMode(mode),
487+
ByteSize, nullptr, &Error);
484488
CHECK_OCL_CODE(Error);
485489

486490
cl_event &WriteBufEvent = Event->getHandleRef();

sycl/include/CL/sycl/detail/context_impl.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class context_impl {
4747
typename info::param_traits<info::context, param>::return_type
4848
get_info() const;
4949

50+
// Warning. Returned reference will be invalid if context_impl was destroyed.
51+
cl_context &getHandleRef();
52+
5053
private:
5154
async_handler m_AsyncHandler;
5255
vector_class<device> m_Devices;

sycl/include/CL/sycl/detail/event_impl.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class event_impl {
4646

4747
void waitInternal() const;
4848

49+
// Warning. Returned reference will be invalid if event_impl was destroyed.
4950
cl_event &getHandleRef();
5051

5152
void setIsHostEvent(bool Value);

sycl/include/CL/sycl/detail/helpers.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#pragma once
1111

1212
#include <CL/sycl/detail/common.hpp>
13+
14+
#include <memory>
1315
#include <stdexcept>
1416
#include <type_traits>
1517
#include <vector>
@@ -24,12 +26,12 @@ template <int dimensions> class range;
2426
template <int dimensions> class id;
2527
template <int dimensions> class nd_item;
2628
namespace detail {
27-
29+
class context_impl;
2830
// The function returns list of events that can be passed to OpenCL API as
2931
// dependency list and waits for others.
3032
std::vector<cl_event>
3133
getOrWaitEvents(std::vector<cl::sycl::event> DepEvents,
32-
cl::sycl::context Context);
34+
std::shared_ptr<cl::sycl::detail::context_impl> Context);
3335

3436
void waitEvents(std::vector<cl::sycl::event> DepEvents);
3537

sycl/include/CL/sycl/detail/program_impl.hpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
//==----- program_impl.hpp --- SYCL program implementation -----------------==//
22
//
3-
// The LLVM Compiler Infrastructure
4-
//
5-
// This file is distributed under the University of Illinois Open Source
6-
// License. See LICENSE.TXT for details.
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
76
//
87
//===----------------------------------------------------------------------===//
9-
108
#pragma once
119

12-
#include <CL/sycl/detail/program_manager/program_manager.hpp>
10+
#include <CL/sycl/context.hpp>
1311
#include <CL/sycl/detail/common.hpp>
1412
#include <CL/sycl/detail/common_info.hpp>
1513
#include <CL/sycl/detail/kernel_desc.hpp>
16-
#include <CL/sycl/context.hpp>
14+
#include <CL/sycl/detail/program_manager/program_manager.hpp>
1715
#include <CL/sycl/device.hpp>
1816
#include <CL/sycl/kernel.hpp>
1917
#include <CL/sycl/stl.hpp>
@@ -66,10 +64,10 @@ class program_impl {
6664
ClPrograms.push_back(Prg->ClProgram);
6765
}
6866
cl_int Err;
69-
ClProgram =
70-
clLinkProgram(Context.get(), ClDevices.size(), ClDevices.data(),
71-
LinkOptions.c_str(), ProgramList.size(),
72-
ClPrograms.data(), nullptr, nullptr, &Err);
67+
ClProgram = clLinkProgram(detail::getSyclObjImpl(Context)->getHandleRef(),
68+
ClDevices.size(), ClDevices.data(),
69+
LinkOptions.c_str(), ProgramList.size(),
70+
ClPrograms.data(), nullptr, nullptr, &Err);
7371
CHECK_OCL_CODE_THROW(Err, compile_program_error);
7472
}
7573
}
@@ -179,9 +177,10 @@ class program_impl {
179177
if (!is_host()) {
180178
vector_class<cl_device_id> ClDevices(get_cl_devices());
181179
cl_int Err;
182-
ClProgram = clLinkProgram(Context.get(), ClDevices.size(),
183-
ClDevices.data(), LinkOptions.c_str(), 1,
184-
&ClProgram, nullptr, nullptr, &Err);
180+
ClProgram =
181+
clLinkProgram(detail::getSyclObjImpl(Context)->getHandleRef(),
182+
ClDevices.size(), ClDevices.data(), LinkOptions.c_str(),
183+
1, &ClProgram, nullptr, nullptr, &Err);
185184
CHECK_OCL_CODE_THROW(Err, compile_program_error);
186185
LinkOptions = LinkOptions;
187186
}
@@ -282,7 +281,8 @@ class program_impl {
282281
cl_int Err;
283282
const char *Src = Source.c_str();
284283
size_t Size = Source.size();
285-
ClProgram = clCreateProgramWithSource(Context.get(), 1, &Src, &Size, &Err);
284+
ClProgram = clCreateProgramWithSource(
285+
detail::getSyclObjImpl(Context)->getHandleRef(), 1, &Src, &Size, &Err);
286286
CHECK_OCL_CODE(Err);
287287
}
288288

sycl/include/CL/sycl/detail/queue_impl.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ class queue_impl {
7373
template <info::queue param>
7474
typename info::param_traits<info::queue, param>::return_type get_info() const;
7575

76-
template <typename T> event submit(T cgf, std::shared_ptr<queue_impl> self,
77-
std::shared_ptr<queue_impl> second_queue) {
76+
template <typename T>
77+
event submit(T cgf, std::shared_ptr<queue_impl> self,
78+
std::shared_ptr<queue_impl> second_queue) {
7879
event Event;
7980
try {
8081
Event = submit_impl(cgf, self);
@@ -89,7 +90,7 @@ class queue_impl {
8990
event Event;
9091
try {
9192
Event = submit_impl(cgf, self);
92-
} catch(...) {
93+
} catch (...) {
9394
m_Exceptions.push_back(std::current_exception());
9495
}
9596
return Event;
@@ -130,14 +131,15 @@ class queue_impl {
130131

131132
cl_int Error = CL_SUCCESS;
132133
cl_command_queue Queue;
134+
cl_context ClContext = detail::getSyclObjImpl(m_Context)->getHandleRef();
133135
#ifdef CL_VERSION_2_0
134136
cl_queue_properties CreationFlagProperties[] = {
135137
CL_QUEUE_PROPERTIES, CreationFlags, 0};
136138
Queue = clCreateCommandQueueWithProperties(
137-
m_Context.get(), m_Device.get(), CreationFlagProperties,
139+
ClContext, m_Device.get(), CreationFlagProperties,
138140
&Error);
139141
#else
140-
Queue = clCreateCommandQueue(m_Context.get(), m_Device.get(),
142+
Queue = clCreateCommandQueue(ClContext, m_Device.get(),
141143
CreationFlags, &Error);
142144
#endif
143145
CHECK_OCL_CODE(Error);
@@ -146,6 +148,7 @@ class queue_impl {
146148
return Queue;
147149
}
148150

151+
// Warning. Returned reference will be invalid if queue_impl was destroyed.
149152
cl_command_queue &getHandleRef() {
150153
if (!m_Device.is_accelerator()) {
151154
return m_CommandQueue;

sycl/include/CL/sycl/detail/scheduler/commands.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ void ExecuteKernelCommand<
4040
runOnHost();
4141
return;
4242
}
43-
43+
context Context = m_Queue->get_context();
4444
if (!m_ClKernel) {
4545
m_ClKernel = detail::ProgramManager::getInstance().getOrCreateKernel(
46-
m_Queue->get_context(), m_KernelName.c_str());
46+
Context, m_KernelName.c_str());
4747
}
4848

4949
if (m_KernelArgs != nullptr) {
@@ -99,8 +99,8 @@ void ExecuteKernelCommand<
9999
}
100100
}
101101

102-
std::vector<cl_event> CLEvents =
103-
detail::getOrWaitEvents(std::move(DepEvents), m_Queue->get_context());
102+
std::vector<cl_event> CLEvents = detail::getOrWaitEvents(
103+
std::move(DepEvents), detail::getSyclObjImpl(Context));
104104
cl_event &CLEvent = Event->getHandleRef();
105105
CLEvent = runEnqueueNDRangeKernel(m_Queue->getHandleRef(), m_ClKernel,
106106
std::move(CLEvents));

sycl/source/detail/context_impl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ context_impl::get_info<info::context::devices>() const {
103103
return get_devices();
104104
}
105105

106+
cl_context &context_impl::getHandleRef() { return m_ClContext; }
107+
106108
} // namespace detail
107109
} // namespace sycl
108110
} // namespace cl

sycl/source/detail/helpers.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
//==---------------- helpers.cpp - SYCL helpers ---------------------------==//
22
//
3-
// The LLVM Compiler Infrastructure
4-
//
5-
// This file is distributed under the University of Illinois Open Source
6-
// License. See LICENSE.TXT for details.
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
76
//
87
//===----------------------------------------------------------------------===//
98

10-
#include <CL/sycl/context.hpp>
9+
#include <CL/sycl/detail/context_impl.hpp>
1110
#include <CL/sycl/detail/helpers.hpp>
1211
#include <CL/sycl/event.hpp>
1312

13+
#include <memory>
14+
1415
namespace cl {
1516
namespace sycl {
17+
using ContextImplPtr = std::shared_ptr<cl::sycl::detail::context_impl>;
1618
namespace detail {
17-
18-
std::vector<cl_event>
19-
getOrWaitEvents(std::vector<cl::sycl::event> DepEvents,
20-
cl::sycl::context Context) {
19+
std::vector<cl_event> getOrWaitEvents(std::vector<cl::sycl::event> DepEvents,
20+
ContextImplPtr Context) {
2121
std::vector<cl_event> CLEvents;
2222
for (auto SyclEvent : DepEvents) {
2323
auto SyclEventImplPtr = detail::getSyclObjImpl(SyclEvent);

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,9 @@ static cl_program createProgram(const platform &Platform,
114114

115115
cl_program ProgramManager::createOpenCLProgram(const context &Context) {
116116
vector_class<char> DeviceProg = getSpirvSource();
117-
cl_context ClContext = Context.get();
117+
cl_context ClContext = detail::getSyclObjImpl(Context)->getHandleRef();
118118
const platform &Platform = Context.get_platform();
119119
cl_program ClProgram = createProgram(Platform, ClContext, DeviceProg);
120-
clReleaseContext(ClContext);
121120
return ClProgram;
122121
}
123122

0 commit comments

Comments
 (0)