Thread safety fixes - #611
Merged
Merged
Conversation
ZeKernel argument slots and group size are mutable state shared by cached HostKernel instances. Concurrent launches could interleave their zeKernelSetArgumentValue and zeKernelSetGroupSize calls, causing either command list to capture mixed arguments or the other launch’s group size. Give each kernel its own lock and hold it continuously from the first mutation through append_launch!, while leaving independent kernel handles concurrent.
The global context and compiler-configuration dictionaries used unsynchronized get! and check-then-insert paths. Concurrent cold calls could mutate a Dict simultaneously or construct duplicate handles and configurations. Protect each cache with a dedicated lock and keep lookup, construction, and insertion in one atomic get! transaction.
The process-wide device cache returned the same mutable RNG and state array to tasks that use independent queues. Concurrent seed! and generation calls could therefore update shared device state without queue ordering. Move the per-device RNG dictionary into task-local storage so repeated calls within one task reuse state while separate tasks receive independent RNGs.
Contributor
|
Your PR requires formatting changes to meet the project's style guidelines. Click here to view the suggested changes.diff --git a/src/compiler/compilation.jl b/src/compiler/compilation.jl
index a8c117a..5f46fff 100644
--- a/src/compiler/compilation.jl
+++ b/src/compiler/compilation.jl
@@ -305,7 +305,7 @@ const _compiler_configs = Dict{UInt, oneAPICompilerConfig}()
const compiler_config_lock = ReentrantLock()
function compiler_config(dev; kwargs...)
h = hash(dev.driver, hash(dev, hash(kwargs)))
- Base.@lock compiler_config_lock begin
+ return Base.@lock compiler_config_lock begin
get!(_compiler_configs, h) do
_compiler_config(dev; kwargs...)
end
diff --git a/src/compiler/execution.jl b/src/compiler/execution.jl
index 7cb37cf..18f9114 100644
--- a/src/compiler/execution.jl
+++ b/src/compiler/execution.jl
@@ -323,7 +323,7 @@ const _kernel_instances = Dict{UInt, Any}()
@inline function onecall(kernel::ZeKernel, tt, args...; groups::ZeDim=1, items::ZeDim=1,
queue::ZeCommandQueue=global_queue(context(), device()))
- Base.@lock kernel begin
+ return Base.@lock kernel begin
for (i, arg) in enumerate(args)
oneL0.arguments(kernel)[i] = arg
end
diff --git a/src/gpuarrays.jl b/src/gpuarrays.jl
index a0a3816..2a58f72 100644
--- a/src/gpuarrays.jl
+++ b/src/gpuarrays.jl
@@ -3,9 +3,9 @@
function GPUArrays.default_rng(::Type{<:oneArray})
dev = device()
rngs = get!(task_local_storage(), :oneAPI_GLOBAL_RNGs) do
- Dict{ZeDevice,GPUArrays.RNG}()
+ Dict{ZeDevice, GPUArrays.RNG}()
end
- get!(rngs, dev) do
+ return get!(rngs, dev) do
N = oneL0.compute_properties(dev).maxTotalGroupSize
state = oneArray{NTuple{4, UInt32}}(undef, N)
rng = GPUArrays.RNG(state) |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #611 +/- ##
=======================================
Coverage 78.51% 78.51%
=======================================
Files 50 50
Lines 3490 3496 +6
=======================================
+ Hits 2740 2745 +5
- Misses 750 751 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Counterpart of JuliaGPU/OpenCL.jl#472