-
Notifications
You must be signed in to change notification settings - Fork 768
[SYCL] Reduce urKernelRetain, Release calls when not using kernel bundle or RTC #18324
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 all commits
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 |
---|---|---|
|
@@ -2703,6 +2703,8 @@ void enqueueImpKernel( | |
|
||
std::shared_ptr<kernel_impl> SyclKernelImpl; | ||
std::shared_ptr<device_image_impl> DeviceImageImpl; | ||
// Transfer ownership only of cache is enabled. | ||
const bool TransferownerShipToCache = SYCLConfig<SYCL_CACHE_IN_MEM>::get(); | ||
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. Should it be 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. Also I think it breaks encapsulation: the cache config should be read inside the cache implementation, not by the caller of the cache |
||
|
||
if (nullptr != MSyclKernel) { | ||
assert(MSyclKernel->get_info<info::kernel::context>() == | ||
|
@@ -2730,10 +2732,17 @@ void enqueueImpKernel( | |
EliminatedArgMask = SyclKernelImpl->getKernelArgMask(); | ||
KernelMutex = SyclKernelImpl->getCacheMutex(); | ||
} else { | ||
|
||
// Acquire the reader lock if cache holds the ownership. This ensures | ||
// that the fetched kernel and program are not evicted while we are | ||
// using them. | ||
if (TransferownerShipToCache) | ||
ContextImpl->getKernelProgramCache().acquireReaderLock(); | ||
|
||
std::tie(Kernel, KernelMutex, EliminatedArgMask, Program) = | ||
detail::ProgramManager::getInstance().getOrCreateKernel( | ||
ContextImpl, DeviceImpl, KernelName, KernelNameBasedCachePtr, | ||
NDRDesc); | ||
NDRDesc, TransferownerShipToCache); | ||
} | ||
|
||
// We may need more events for the launch, so we make another reference. | ||
|
@@ -2779,10 +2788,19 @@ void enqueueImpKernel( | |
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 cache is owning the kernel and programs, we don't have to release | ||
// them here, as they will be released when the cache is destroyed or | ||
// when the kernel is evicted from the cache. | ||
if (!SyclKernelImpl && !MSyclKernel && !TransferownerShipToCache) { | ||
|
||
if (TransferownerShipToCache) | ||
ContextImpl->getKernelProgramCache().releaseReaderLock(); | ||
else { | ||
// If cache is disabled, we need to release the kernel and program. | ||
const AdapterPtr &Adapter = Queue->getAdapter(); | ||
Adapter->call<UrApiKind::urKernelRelease>(Kernel); | ||
Adapter->call<UrApiKind::urProgramRelease>(Program); | ||
} | ||
} | ||
} | ||
if (UR_RESULT_SUCCESS != Error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need to invent your own RW lock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, the closest to reader/writer lock in C++ is
shared_lock
(https://en.cppreference.com/w/cpp/thread/shared_lock.html) andunique_lock
. Both of these operate over a mutex. In my implementation, I've used an atomic variable instead, which I suppose will be faster than mutex here as contention between threads is low (w'll evict from cache rarely). In my understanding, for simple atomic counter-like applications,std::atomic
performs better than mutex as the former can leverage HW support for atomic ops while mutex would also require OS support (likefutex
syscall on Linux?).