Skip to content

[SYCL] Use per-kernel mutex for interop kernel enqueue #8165

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 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions sycl/source/detail/kernel_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ class kernel_impl {

ProgramImplPtr getProgramImpl() const { return MProgramImpl; }

std::mutex &getNoncacheableEnqueueMutex() {
return MNoncacheableEnqueueMutex;
}

private:
RT::PiKernel MKernel;
const ContextImplPtr MContext;
Expand All @@ -181,6 +185,7 @@ class kernel_impl {
const DeviceImageImplPtr MDeviceImageImpl;
const KernelBundleImplPtr MKernelBundleImpl;
bool MIsInterop = false;
std::mutex MNoncacheableEnqueueMutex;
};

template <typename Param>
Expand Down
15 changes: 9 additions & 6 deletions sycl/source/detail/scheduler/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2106,12 +2106,7 @@ pi_int32 enqueueImpKernel(
auto ContextImpl = Queue->getContextImplPtr();
auto DeviceImpl = Queue->getDeviceImplPtr();
RT::PiKernel Kernel = nullptr;
// Cacheable kernels use per-kernel mutexes that will be fetched from the
// cache, others (e.g. interoperability kernels) share a single mutex.
// TODO consider adding a PiKernel -> mutex map for allowing to enqueue
// different PiKernel's in parallel.
static std::mutex NoncacheableEnqueueMutex;
std::mutex *KernelMutex = &NoncacheableEnqueueMutex;
std::mutex *KernelMutex = nullptr;
RT::PiProgram Program = nullptr;

std::shared_ptr<kernel_impl> SyclKernelImpl;
Expand Down Expand Up @@ -2152,6 +2147,14 @@ pi_int32 enqueueImpKernel(
OSModuleHandle, ContextImpl, DeviceImpl, KernelName,
SyclProg.get());
assert(FoundKernel == Kernel);
} else {
// Non-cacheable kernels use mutexes from kernel_impls.
// TODO this can still result in a race condition if multiple SYCL
// kernels are created with the same native handle. To address this,
// we need to either store and use a pi_native_handle -> mutex map or
// reuse and return existing SYCL kernels from make_native to avoid
// their duplication in such cases.
KernelMutex = &MSyclKernel->getNoncacheableEnqueueMutex();
}
} else {
std::tie(Kernel, KernelMutex, Program) =
Expand Down