Repository navigation
Description
I have encountered what I think is a variable scoping issue that causes one of my KernelAbstractions kernels to fail when executing on the CPU. (GPU execution is fine.) I'm using KernelAbstractions v0.9.6 in Julia 1.9.2. Here's a minimal example that triggers the problem:
using KernelAbstractions
@kernel function mykernel(x)
i = @index(Global, Linear)
_, Nblocks = @ndrange()
@inbounds begin
id = Nblocks
@synchronize
x[i] = 1.0
end
end
x = ones(256, 1)
backend = get_backend(x)
kernel! = mykernel(backend, (256,))
kernel!(x, ndrange = (256, 1))When I run this code, it fails with:
ERROR: LoadError: UndefVarError: `Nblocks` not defined
Stacktrace:
[1] cpu_mykernel
@ ~/.julia/packages/KernelAbstractions/lhhMo/src/macros.jl:276 [inlined]
[2] cpu_mykernel(__ctx__::KernelAbstractions.CompilerMetadata{KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.NoDynamicCheck, CartesianIndex{2}, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, KernelAbstractions.NDIteration.NDRange{2, KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.StaticSize{(256, 1)}, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, Nothing}}, x::Matrix{Float64})
@ Main ./none:0
[3] __thread_run(tid::Int64, len::Int64, rem::Int64, obj::KernelAbstractions.Kernel{CPU, KernelAbstractions.NDIteration.StaticSize{(256,)}, KernelAbstractions.NDIteration.DynamicSize, typeof(cpu_mykernel)}, ndrange::Tuple{Int64, Int64}, iterspace::KernelAbstractions.NDIteration.NDRange{2, KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.StaticSize{(256, 1)}, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, Nothing}, args::Tuple{Matrix{Float64}}, dynamic::KernelAbstractions.NDIteration.NoDynamicCheck)
@ KernelAbstractions ~/.julia/packages/KernelAbstractions/lhhMo/src/cpu.jl:115
[4] __run(obj::KernelAbstractions.Kernel{CPU, KernelAbstractions.NDIteration.StaticSize{(256,)}, KernelAbstractions.NDIteration.DynamicSize, typeof(cpu_mykernel)}, ndrange::Tuple{Int64, Int64}, iterspace::KernelAbstractions.NDIteration.NDRange{2, KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.StaticSize{(256, 1)}, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, Nothing}, args::Tuple{Matrix{Float64}}, dynamic::KernelAbstractions.NDIteration.NoDynamicCheck, static_threads::Bool)
@ KernelAbstractions ~/.julia/packages/KernelAbstractions/lhhMo/src/cpu.jl:82
[5] (::KernelAbstractions.Kernel{CPU, KernelAbstractions.NDIteration.StaticSize{(256,)}, KernelAbstractions.NDIteration.DynamicSize, typeof(cpu_mykernel)})(args::Matrix{Float64}; ndrange::Tuple{Int64, Int64}, workgroupsize::Nothing)
@ KernelAbstractions ~/.julia/packages/KernelAbstractions/lhhMo/src/cpu.jl:44
[6] top-level scope
@ ~/debug.jl:19
[7] include(fname::String)
@ Base.MainInclude ./client.jl:478
[8] top-level scope
@ REPL[1]:1
The compiler thinks that the variable Nblocks in the id = Nblocks line is not defined, even though it clearly is defined via the call to @ndrange. When I inspect the generated kernel code with code_lowered(), I see:
julia> code_lowered(kernel!.f)
1-element Vector{Core.CodeInfo}:
CodeInfo(
[...]
5 ── i@_14 = KernelAbstractions.__index_Global_Linear(__ctx__, I#301)
│ %25 = (KernelAbstractions.ndrange)(__ctx__)
│ %26 = (size)(%25)
│ %27 = Base.indexed_iterate(%26, 1)
│ Core.getfield(%27, 1)
│ @_11 = Core.getfield(%27, 2)
│ %30 = Base.indexed_iterate(%26, 2, @_11)
└─── Nblocks = Core.getfield(%30, 1)
[...]
12 ─ i@_17 = KernelAbstractions.__index_Global_Linear(__ctx__, I#303)
└─── id = Main.Nblocks
[...]
)
The code in block 5 shows that Nblocks is getting set OK, but the code in block 12 shows that when the id = Nblocks line gets translated, the compiler looks for a definition of Nblocks in the Main module, where it does not exist. (I redacted this listing for readability. I'm happy to provide the full listing if that would be helpful.)
The issue disappears if I remove the call to @synchronize.
Any thoughts here?
EDIT: This is probably related to (maybe even a duplicate of) #274. Also, another way I can get the issue to disappear is to move the call to @ndrange that defines Nblocks inside the @inbounds begin ... end block.
Activity
vchuravy commentedon Aug 2, 2023
Yeah this is expected and the reason why the
@uniformmacro is needed.https://juliagpu.github.io/KernelAbstractions.jl/api/#KernelAbstractions.@uniform
aaustin141 commentedon Aug 2, 2023
OK, yeah---using
@uniformfixes it. I guess what confuses me here is that I didn't need to do that for the variables declared with@index. (Indeed, putting indices inside a@uniformblock triggers a different error.) But I can work with that---thanks!vchuravy commentedon Aug 2, 2023
Yeah the CPU lowering is a bit tricky, and doesn't have the best errors
ManuelCostanzo commentedon Feb 29, 2024
Hey, I need help here please!
No matter if I add or remove @uniform or @Private in the index, I can't run the code:
LoadError: UndefVarError:
indexnot defined:LoadError: MethodError: no method matching __index_Global_Linear(::KernelAbstractions.CompilerMetadata{KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.NoDynamicCheck, CartesianIndex{2}, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, KernelAbstractions.NDIteration.NDRange{2, KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.DynamicSize, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}}}):
ERROR: LoadError: MethodError: no method matching __index_Global_Linear(::KernelAbstractions.CompilerMetadata{KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.NoDynamicCheck, CartesianIndex{2}, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, KernelAbstractions.NDIteration.NDRange{2, KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.DynamicSize, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}}}):
Any ideas? Thanks!
vchuravy commentedon Feb 29, 2024
@ManuelCostanzo please make it easier to help you by formatting your post.
I am unsure what you want to achieve? By definition
@uniformand@indexare incompatible.ManuelCostanzo commentedon Mar 2, 2024
Hi @vchuravy, I just need to access to the thread_id and block_id values after the
@synchronize.In CPU, the only way (as far as I know) is to create the variables with@uniformor@private. So, It's impossible to me to do that because I'm getting the error "ID IS NOT DEFINED". If I don't add@uniform, I get "VARR IS NOT DEFINED" Here is an example code:maleadt commentedon Oct 19, 2026
Fixed by #556, released in 0.10: the
CPUbackend is PoCL-based now, and variables defined before a@synchronizeremain visible after it without@uniform. The MWE runs unchanged.