Skip to content

UndefVarError when variable is defined #647

Closed

Description

@efaulhaber
using KernelAbstractions

@kernel function mykernel(y)
    z = length(y)

    for i in 1:z
        @synchronize
    end
end

x = zeros(3)
backend = get_backend(x)

mykernel(backend)(x; ndrange=length(x))

KernelAbstractions.synchronize(backend)

This MWE yields:

ERROR: UndefVarError: `z` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
 [1] cpu_mykernel
   @ ~/.julia/packages/KernelAbstractions/lGrz7/src/macros.jl:307 [inlined]
 [2] cpu_mykernel(__ctx__::KernelAbstractions.CompilerMetadata{…}, x::Vector{…})
   @ Main ./none:0
 [3] __thread_run(tid::Int64, len::Int64, rem::Int64, obj::KernelAbstractions.Kernel{…}, ndrange::Tuple{…}, iterspace::KernelAbstractions.NDIteration.NDRange{…}, args::Tuple{…}, dynamic::KernelAbstractions.NDIteration.NoDynamicCheck)
   @ KernelAbstractions ~/.julia/packages/KernelAbstractions/lGrz7/src/cpu.jl:145
 [4] __run(obj::KernelAbstractions.Kernel{…}, ndrange::Tuple{…}, iterspace::KernelAbstractions.NDIteration.NDRange{…}, args::Tuple{…}, dynamic::KernelAbstractions.NDIteration.NoDynamicCheck, static_threads::Bool)
   @ KernelAbstractions ~/.julia/packages/KernelAbstractions/lGrz7/src/cpu.jl:112
 [5] (::KernelAbstractions.Kernel{…})(args::Vector{…}; ndrange::Int64, workgroupsize::Nothing)
   @ KernelAbstractions ~/.julia/packages/KernelAbstractions/lGrz7/src/cpu.jl:46
 [6] top-level scope
   @ Untitled-2:14
Some type information was truncated. Use `show(err)` to see complete types.

Activity

  1. efaulhaber commented on Oct 24, 2025

    @efaulhaber
    Author

    Update: It seems to work on the GPU, but I would love to run it on the CPU for debugging because I'm getting illegal memory access on the GPU with my larger kernel.

  2. yolhan83 commented on Oct 24, 2025

    @yolhan83

    Are you on main ? otherwise use @uniform,

    @kernel function mykernel(y)
        @uniform z = length(y)
    
        for i in 1:z
            @synchronize
        end
    end

    edit : it does not work, synchronize in loops doesn't work on the old CPU backend.

    use add KernelAbstraction#main and your code it works thanks to the Pocl backend. So its fixed on main.

  3. added theissue type on Oct 19, 2026
  4. added
    kernels
    on Oct 19, 2026
  5. maleadt commented on Oct 19, 2026

    @maleadt
    Member

    Fixed by #556, released in 0.10: with the PoCL-based CPU backend, @synchronize inside a loop works and the MWE runs unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kernels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions