Skip to content

Qualified KA.@index macro not detected. #542

Closed

Description

@JamesWrigley

When running this slightly minified code from the quickstart guide:

import KernelAbstractions as KA

KA.@kernel function mul2_kernel(A)
    I = KA.@index(Global, Linear)
end
    
dev = KA.CPU()
A = ones(1024, 1024)
ev = mul2_kernel(dev, 64)(A, ndrange=size(A))
KA.synchronize(dev)
all(A .== 2.0)

I get this exception (only on the CPU backend, the CUDA backend works):

TaskFailedException

    nested task error: 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.StaticSize{(64, 1)}, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, Nothing}})
    The function `__index_Global_Linear` exists, but no method is defined for this combination of argument types.
    
    Closest candidates are:
      __index_Global_Linear(::Any, ::CartesianIndex)
       @ KernelAbstractions ~/.julia/packages/KernelAbstractions/491pi/src/cpu.jl:163
    
    Stacktrace:
     [1] macro expansion
       @ ./In[85]:3 [inlined]
     [2] cpu_mul2_kernel
       @ ~/.julia/packages/KernelAbstractions/491pi/src/macros.jl:291 [inlined]
     [3] (::var"#cpu_mul2_kernel#9")(__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{(64, 1)}, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, Nothing}}, A::Matrix{Float64})
       @ Main ./none:0
     [4] __thread_run(tid::Int64, len::Int64, rem::Int64, obj::KernelAbstractions.Kernel{CPU, KernelAbstractions.NDIteration.StaticSize{(64,)}, KernelAbstractions.NDIteration.DynamicSize, var"#cpu_mul2_kernel#9"}, ndrange::Tuple{Int64, Int64}, iterspace::KernelAbstractions.NDIteration.NDRange{2, KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.StaticSize{(64, 1)}, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, Nothing}, args::Tuple{Matrix{Float64}}, dynamic::KernelAbstractions.NDIteration.NoDynamicCheck)
       @ KernelAbstractions ~/.julia/packages/KernelAbstractions/491pi/src/cpu.jl:144
     [5] (::KernelAbstractions.var"#20#23"{KernelAbstractions.Kernel{CPU, KernelAbstractions.NDIteration.StaticSize{(64,)}, KernelAbstractions.NDIteration.DynamicSize, var"#cpu_mul2_kernel#9"}, Tuple{Int64, Int64}, KernelAbstractions.NDIteration.NDRange{2, KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.StaticSize{(64, 1)}, CartesianIndices{2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}, Nothing}, Tuple{Matrix{Float64}}, KernelAbstractions.NDIteration.NoDynamicCheck, Int64})()
       @ KernelAbstractions ~/.julia/packages/KernelAbstractions/491pi/src/cpu.jl:119

...and 39 more exceptions.


Stacktrace:
 [1] sync_end(c::Channel{Any})
   @ Base ./task.jl:466
 [2] macro expansion
   @ ./task.jl:499 [inlined]
 [3] __run(obj::KernelAbstractions.Kernel{CPU, KernelAbstractions.NDIteration.StaticSize{(64,)}, KernelAbstractions.NDIteration.DynamicSize, var"#cpu_mul2_kernel#9"}, ndrange::Tuple{Int64, Int64}, iterspace::KernelAbstractions.NDIteration.NDRange{2, KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.StaticSize{(64, 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/491pi/src/cpu.jl:118
 [4] (::KernelAbstractions.Kernel{CPU, KernelAbstractions.NDIteration.StaticSize{(64,)}, KernelAbstractions.NDIteration.DynamicSize, var"#cpu_mul2_kernel#9"})(args::Matrix{Float64}; ndrange::Tuple{Int64, Int64}, workgroupsize::Nothing)
   @ KernelAbstractions ~/.julia/packages/KernelAbstractions/491pi/src/cpu.jl:46
 [5] top-level scope
   @ In[85]:9

These are the versions I'm running it with:

% julia --project
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.11.1 (2024-10-16)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

(james) pkg> st
Status `Project.toml`
  [824d6782] Bonito v3.2.2
  [052768ef] CUDA v5.5.2
  [f68482b8] Cthulhu v2.15.2
  [8bb1440f] DelimitedFiles v1.9.1
  [7a1cc6ca] FFTW v1.8.0
  [63c18a36] KernelAbstractions v0.9.28
  [2fda8390] LsqFit v0.15.0
  [67456a42] OhMyThreads v0.7.0
  [6099a3de] PythonCall v0.9.23
  [276b4fcb] WGLMakie v0.10.14
Pinned by christiangnrd

Activity

  1. vchuravy commented on Nov 30, 2024

    @vchuravy
    Member

    Huh that's a fun one...

  2. self-assigned this
    on Nov 30, 2024
  3. AntoineBut commented on Mar 24, 2025

    @AntoineBut

    I've encountered the same issue, has anyone found a quick fix to get around the problem?

    It doesn't matter if it is inneficient since it only show up on theCPU backend, but it currently prevents me from testing my package in the CI.

  4. JamesWrigley commented on Mar 24, 2025

    @JamesWrigley
    Author

    Alas, no. My quick fix is not use CPUs 😎

  5. AntoineBut commented on Mar 24, 2025

    @AntoineBut

    Great news, will email GitHub to kindly ask they add a few GPU to their runners 🚀🚀

  6. JamesWrigley commented on Mar 29, 2025

    @JamesWrigley
    Author

    Actually this is fixed on master, so I'll close it. @vchuravy, any chance of getting a new release with the fix?

  7. vchuravy commented on Mar 29, 2025

    @vchuravy
    Member

    Master will need some more work to stabilize, it's a breaking release

  8. JamesWrigley commented on Mar 29, 2025

    @JamesWrigley
    Author

    Roger that 👍 Out of curiosity, what more work would be necessary? I might be able to find some time for it in the next few weeks.

    (not that I know much about KA 😅 )

  9. JamesWrigley commented on May 21, 2025

    @JamesWrigley
    Author

    Gentle bump, is there a timeline for when the next release will come out?

  10. vchuravy commented on May 22, 2025

    @vchuravy
    Member

    #600 is currently tracking some of the work needed, but otherwise when it's done and tested enough

  11. JamesWrigley commented on May 22, 2025

    @JamesWrigley
    Author

    Roger that, is there anything a relative noob could help out with? (disclaimer: I will likely not have a lot of time to work on it for a few weeks)

  12. vchuravy commented on Nov 7, 2025

    @vchuravy
    Member

    Note that this is due to the use of KA.@index instead of @index

    julia> KA.@kernel function mul2_kernel(A)
               I = @index(Global, Linear)
           end
    

    works.

  13. changed the title [-]Exception when getting the index on the CPU backend[/-] [+]Qualified `KA.@index` macro not detected.[/+] on Nov 7, 2025
  14. vchuravy commented on Nov 7, 2025

    @vchuravy
    Member

    This is due to

    if @capture(rhs, @index(args__))

  15. JamesWrigley commented on Nov 7, 2025

    @JamesWrigley
    Author

    Oh my god, I can't believe I avoided using KA on CPUs for like a year because of this 🤦

  16. vchuravy commented on Nov 7, 2025

    @vchuravy
    Member

    😢

  17. mtanneau commented on Mar 2, 2026

    @mtanneau

    bump: still experiencing this issue on v0.9.40 when trying to run on CPU backend

    module MyModule1
       using KernelAbstractions
       
       @kernel function inc_kernel(x)
           I = @index(Global)
           x[I] = x[I] + 1
       end
       
       function increment(x)
           backend = get_backend(x)
           kernel! = inc_kernel(backend)
           kernel!(x, ndrange=size(x))
           synchronize(backend)
           return x
       end
    end
    
    module MyModule2
       import KernelAbstractions as KA
       
       KA.@kernel function inc_kernel(x)
           I = KA.@index(Global)
           x[I] = x[I] + 1
       end
       
       function increment(x)
           backend = KA.get_backend(x)
           kernel! = inc_kernel(backend)
           kernel!(x, ndrange=size(x))
           synchronize(backend)
           return x
       end
    end
    
    
    MyModule1.increment(ones(4))  # no issue
    MyModule2.increment(ones(4))  # Error

    the second call errors with the following stack trace:

    ERROR: MethodError: no method matching __index_Global_Linear(::KernelAbstractions.CompilerMetadata{…})
    The function `__index_Global_Linear` exists, but no method is defined for this combination of argument types.
    
    Closest candidates are:
      __index_Global_Linear(::Any, ::CartesianIndex)
       @ KernelAbstractions ~/.julia/packages/KernelAbstractions/aLmvZ/src/cpu.jl:164
    
    Stacktrace:
     [1] macro expansion
       @ ./REPL[2]:5 [inlined]
     [2] macro expansion
       @ ~/.julia/packages/KernelAbstractions/aLmvZ/src/macros.jl:314 [inlined]
     [3] cpu_inc_kernel(__ctx__::KernelAbstractions.CompilerMetadata{…}, x::Vector{…})
       @ Main.MyModule2 ./none:0
     [4] __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/aLmvZ/src/cpu.jl:145
     [5] __run(obj::KernelAbstractions.Kernel{…}, ndrange::Tuple{…}, iterspace::KernelAbstractions.NDIteration.NDRange{…}, args::Tuple{…}, dynamic::KernelAbstractions.NDIteration.NoDynamicCheck, static_threads::Bool)
       @ KernelAbstractions ~/.julia/packages/KernelAbstractions/aLmvZ/src/cpu.jl:112
     [6] (::KernelAbstractions.Kernel{…})(args::Vector{…}; ndrange::Tuple{…}, workgroupsize::Nothing)
       @ KernelAbstractions ~/.julia/packages/KernelAbstractions/aLmvZ/src/cpu.jl:39
     [7] Kernel
       @ ~/.julia/packages/KernelAbstractions/aLmvZ/src/cpu.jl:39 [inlined]
     [8] increment(x::Vector{Float64})
       @ Main.MyModule2 ./REPL[2]:12
     [9] top-level scope
       @ REPL[4]:1
    Some type information was truncated. Use `show(err)` to see complete types.
  18. christiangnrd commented on Mar 2, 2026

    @christiangnrd
    Member

    bump: still experiencing this issue on v0.9.40 when trying to run on CPU backend

    @mtanneau This will be fixed in KA 0.10 once released. In the meantime, an easy workaround is to import @index directly as implied in #542 (comment)

    module MyModule2Working
        import KernelAbstractions as KA
        import KernelAbstractions: @index
       
        KA.@kernel function inc_kernel(x)
            I = @index(Global)
            x[I] = x[I] + 1
        end
       
        function increment(x)
            backend = KA.get_backend(x)
            kernel! = inc_kernel(backend)
            kernel!(x, ndrange=size(x))
            KA.synchronize(backend)
            return x
        end
    end
    
    
    MyModule2Working.increment(ones(4))  # no issue
  19. added
    kernels
    pocl
    on Oct 19, 2026
  20. added theissue type on Oct 19, 2026
  21. maleadt commented on Oct 19, 2026

    @maleadt
    Member

    Fixed by #556: the CPU back end no longer rewrites @index calls at the macro level, so a qualified KA.@index works the same as @index on every back end in 0.10.

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

Metadata

Metadata

Assignees

Labels

kernelspocl

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions