-
Notifications
You must be signed in to change notification settings - Fork 769
[SYCL] Use shared_ptr instead of manual changing UR counters #18565
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
Changes from 7 commits
0edc034
3a3f017
f0be3fe
dad0228
a11ef81
93d9cc4
e9852a6
d5e20f6
db56adf
7715cc6
54d94f1
4a20d5f
76980f2
8c16b6e
441344d
cbb03d2
37bde6f
8c0c60a
728f698
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,11 +19,37 @@ namespace sycl { | |
inline namespace _V1 { | ||
namespace detail { | ||
using FastKernelCacheKeyT = std::pair<ur_device_handle_t, ur_context_handle_t>; | ||
using FastKernelCacheValT = | ||
std::tuple<ur_kernel_handle_t, std::mutex *, const KernelArgMask *, | ||
ur_program_handle_t>; | ||
|
||
struct FastKernelCacheVal { | ||
ur_kernel_handle_t MKernelHandle; /* UR kernel handle pointer. */ | ||
std::mutex *MMutex; /* Mutex guarding this kernel. */ | ||
const KernelArgMask *MKernelArgMask; /* Eliminated kernel argument mask. */ | ||
ur_program_handle_t MProgramHandle; /* UR program handle corresponding to | ||
this kernel. */ | ||
std::weak_ptr<Adapter> MAdapterWeakPtr; /* Weak pointer to the adapter. */ | ||
vinser52 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
FastKernelCacheVal(ur_kernel_handle_t KernelHandle, std::mutex *Mutex, | ||
const KernelArgMask *KernelArgMask, | ||
ur_program_handle_t ProgramHandle, | ||
const AdapterPtr &Adapter) | ||
: MKernelHandle(KernelHandle), MMutex(Mutex), | ||
MKernelArgMask(KernelArgMask), MProgramHandle(ProgramHandle), | ||
MAdapterWeakPtr(Adapter) {} | ||
|
||
~FastKernelCacheVal() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we set to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
if (AdapterPtr Adapter = MAdapterWeakPtr.lock()) { | ||
if (MKernelHandle) | ||
Adapter->call<sycl::detail::UrApiKind::urKernelRelease>(MKernelHandle); | ||
if (MProgramHandle) | ||
Adapter->call<sycl::detail::UrApiKind::urProgramRelease>( | ||
MProgramHandle); | ||
} | ||
} | ||
}; | ||
using FastKernelCacheValPtr = std::shared_ptr<FastKernelCacheVal>; | ||
vinser52 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
using FastKernelSubcacheMapT = | ||
::boost::unordered_flat_map<FastKernelCacheKeyT, FastKernelCacheValT>; | ||
::boost::unordered_flat_map<FastKernelCacheKeyT, FastKernelCacheValPtr>; | ||
|
||
using FastKernelSubcacheMutexT = SpinLock; | ||
using FastKernelSubcacheReadLockT = std::lock_guard<FastKernelSubcacheMutexT>; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1991,8 +1991,6 @@ void instrumentationAddExtraKernelMetadata( | |
auto FilterArgs = [&Args](detail::ArgDesc &Arg, int NextTrueIndex) { | ||
Args.push_back({Arg.MType, Arg.MPtr, Arg.MSize, NextTrueIndex}); | ||
}; | ||
ur_kernel_handle_t Kernel = nullptr; | ||
std::mutex *KernelMutex = nullptr; | ||
const KernelArgMask *EliminatedArgMask = nullptr; | ||
|
||
if (nullptr != SyclKernel) { | ||
|
@@ -2007,11 +2005,11 @@ void instrumentationAddExtraKernelMetadata( | |
// NOTE: Queue can be null when kernel is directly enqueued to a command | ||
// buffer | ||
// by graph API, when a modifiable graph is finalized. | ||
ur_program_handle_t Program = nullptr; | ||
std::tie(Kernel, KernelMutex, EliminatedArgMask, Program) = | ||
FastKernelCacheValPtr FastKernelCacheVal = | ||
detail::ProgramManager::getInstance().getOrCreateKernel( | ||
Queue->getContextImplPtr(), Queue->getDeviceImpl(), KernelName, | ||
KernelNameBasedCachePtr); | ||
EliminatedArgMask = FastKernelCacheVal->MKernelArgMask; | ||
} | ||
|
||
applyFuncOnFilteredArgs(EliminatedArgMask, CGArgs, FilterArgs); | ||
|
@@ -2558,13 +2556,20 @@ getCGKernelInfo(const CGExecKernel &CommandGroup, ContextImplPtr ContextImpl, | |
DeviceImageImpl = SyclKernelImpl->getDeviceImage(); | ||
EliminatedArgMask = SyclKernelImpl->getKernelArgMask(); | ||
} else { | ||
ur_program_handle_t UrProgram = nullptr; | ||
std::tie(UrKernel, std::ignore, EliminatedArgMask, UrProgram) = | ||
FastKernelCacheValPtr FastKernelCacheVal = | ||
sycl::detail::ProgramManager::getInstance().getOrCreateKernel( | ||
ContextImpl, DeviceImpl, CommandGroup.MKernelName, | ||
CommandGroup.MKernelNameBasedCachePtr); | ||
UrKernel = FastKernelCacheVal->MKernelHandle; | ||
EliminatedArgMask = FastKernelCacheVal->MKernelArgMask; | ||
// UrProgram/UrKernel are used after KernelCacheVal is destroyed, so caller | ||
// must call ur*Release. | ||
ContextImpl->getAdapter()->call<UrApiKind::urProgramRetain>( | ||
FastKernelCacheVal->MProgramHandle); | ||
ContextImpl->getAdapter()->call<UrApiKind::urKernelRetain>(UrKernel); | ||
vinser52 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
UrKernelsToRelease.push_back(UrKernel); | ||
UrProgramsToRelease.push_back(UrProgram); | ||
UrProgramsToRelease.push_back(FastKernelCacheVal->MProgramHandle); | ||
} | ||
return std::make_tuple(UrKernel, DeviceImageImpl, EliminatedArgMask); | ||
} | ||
|
@@ -2703,6 +2708,7 @@ void enqueueImpKernel( | |
|
||
std::shared_ptr<kernel_impl> SyclKernelImpl; | ||
std::shared_ptr<device_image_impl> DeviceImageImpl; | ||
FastKernelCacheValPtr KernelCacheVal; | ||
|
||
if (nullptr != MSyclKernel) { | ||
assert(MSyclKernel->get_info<info::kernel::context>() == | ||
|
@@ -2730,10 +2736,12 @@ void enqueueImpKernel( | |
EliminatedArgMask = SyclKernelImpl->getKernelArgMask(); | ||
KernelMutex = SyclKernelImpl->getCacheMutex(); | ||
} else { | ||
std::tie(Kernel, KernelMutex, EliminatedArgMask, Program) = | ||
detail::ProgramManager::getInstance().getOrCreateKernel( | ||
ContextImpl, DeviceImpl, KernelName, KernelNameBasedCachePtr, | ||
NDRDesc); | ||
KernelCacheVal = detail::ProgramManager::getInstance().getOrCreateKernel( | ||
ContextImpl, DeviceImpl, KernelName, KernelNameBasedCachePtr, NDRDesc); | ||
Kernel = KernelCacheVal->MKernelHandle; | ||
KernelMutex = KernelCacheVal->MMutex; | ||
Program = KernelCacheVal->MProgramHandle; | ||
Comment on lines
+2729
to
+2733
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How often do we need to extract data like that? Would enabling structured bindings for this class make sense? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even in that particular code snippet, we cannot use structured bindings because the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And additionally, reference counting is now supported by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure, but I do not think so. As I understand the |
||
EliminatedArgMask = KernelCacheVal->MKernelArgMask; | ||
} | ||
|
||
// We may need more events for the launch, so we make another reference. | ||
|
@@ -2778,12 +2786,6 @@ void enqueueImpKernel( | |
KernelIsCooperative, KernelUsesClusterLaunch, WorkGroupMemorySize, | ||
BinImage, KernelName, KernelFuncPtr, KernelNumArgs, | ||
KernelParamDescGetter, KernelHasSpecialCaptures); | ||
|
||
const AdapterPtr &Adapter = Queue->getAdapter(); | ||
if (!SyclKernelImpl && !MSyclKernel) { | ||
Adapter->call<UrApiKind::urKernelRelease>(Kernel); | ||
Adapter->call<UrApiKind::urProgramRelease>(Program); | ||
} | ||
} | ||
if (UR_RESULT_SUCCESS != Error) { | ||
// If we have got non-success error code, let's analyze it to emit nice | ||
|
Uh oh!
There was an error while loading. Please reload this page.