JuliaGPU / JuliaGPU/KernelAbstractions.jl

Types as arguments fail on GPU

Open
#285 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
523
Forks
88
Avg merge
1d 11h
Merged PRs (30d)
25

Description

I feel like this must be known somewhere, but I couldn't find the issue in my quick search. Here's a MWE:

using Test
using CUDA
using CUDAKernels
using KernelAbstractions

@kernel function f_test_kernel!(input, T)
    tid = @index(Global, Linear)

    input[tid] = T(tid)
end

function f_test!(input, T; numcores = 4, numthreads = 256)

    if isa(input, Array)
        kernel! = f_test_kernel!(CPU(), numcores)
    else
        kernel! = f_test_kernel!(CUDADevice(), numthreads)
    end

    kernel!(input, T, ndrange=length(input))
end

This works:

julia> input = zeros(Int32, 1024);

julia> wait(f_test!(input, Int32));

This doesn't:

julia> d_input = CuArray(zeros(Int32, 1024));

julia> wait(f_test!(d_input, Int32));

Error:

ERROR: InvalidIRError: compiling kernel gpu_f_test_kernel!(Cassette.Context{nametype(CUDACtx), Nothing, Nothing, KernelAbstractions.var"##PassType#274", Nothing, Cassette.DisableHooks}, typeof(gpu_f_test_kernel!), KernelAbstractions.CompilerMetadata{KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.DynamicCheck, Nothing, CartesianIndices{1, Tuple{Base.OneTo{Int64}}}, KernelAbstractions.NDIteration.NDRange{1, KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.StaticSize{(256,)}, CartesianIndices{1, Tuple{Base.OneTo{Int64}}}, Nothing}}, CuDeviceVector{Int32, 1}, Type{Int32}) resulted in invalid LLVM IR
Reason: unsupported call to an unknown function (call to jl_f_tuple)
Stacktrace:
 [1] overdub
   @ ~/.julia/packages/Cassette/1lyEM/src/overdub.jl:636
Reason: unsupported call to an unknown function (call to jl_f_getfield)
Stacktrace:
 [1] gpu_f_test_kernel!(::KernelAbstractions.CompilerMetadata{KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.DynamicCheck, Nothing, CartesianIndices{1, Tuple{Base.OneTo{Int64}}}, KernelAbstractions.NDIteration.NDRange{1, KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.StaticSize{(256,)}, CartesianIndices{1, Tuple{Base.OneTo{Int64}}}, Nothing}}, ::CuDeviceVector{Int32, 1}, ::Type{Int32})
   @ ./none:0
 [2] overdub
   @ ./none:0
 [3] overdub
   @ ~/.julia/packages/Cassette/1lyEM/src/overdub.jl:0
Stacktrace:
  [1] check_ir(job::GPUCompiler.CompilerJob{GPUCompiler.PTXCompilerTarget, CUDA.CUDACompilerParams, GPUCompiler.FunctionSpec{typeof(Cassette.overdub), Tuple{Cassette.Context{nametype(CUDACtx), Nothing, Nothing, KernelAbstractions.var"##PassType#274", Nothing, Cassette.DisableHooks}, typeof(gpu_f_test_kernel!), KernelAbstractions.CompilerMetadata{KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.DynamicCheck, Nothing, CartesianIndices{1, Tuple{Base.OneTo{Int64}}}, KernelAbstractions.NDIteration.NDRange{1, KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.StaticSize{(256,)}, CartesianIndices{1, Tuple{Base.OneTo{Int64}}}, Nothing}}, CuDeviceVector{Int32, 1}, Type{Int32}}}}, args::LLVM.Module)
    @ GPUCompiler ~/.julia/packages/GPUCompiler/HeCT6/src/validation.jl:111
  [2] macro expansion
    @ ~/.julia/packages/GPUCompiler/HeCT6/src/driver.jl:326 [inlined]
  [3] macro expansion
    @ ~/.julia/packages/TimerOutputs/SSeq1/src/TimerOutput.jl:252 [inlined]
  [4] macro expansion
    @ ~/.julia/packages/GPUCompiler/HeCT6/src/driver.jl:324 [inlined]
  [5] emit_asm(job::GPUCompiler.CompilerJob, ir::LLVM.Module; strip::Bool, validate::Bool, format::LLVM.API.LLVMCodeGenFileType)
    @ GPUCompiler ~/.julia/packages/GPUCompiler/HeCT6/src/utils.jl:64
  [6] cufunction_compile(job::GPUCompiler.CompilerJob)
    @ CUDA ~/projects/CUDA.jl/src/compiler/execution.jl:326
  [7] cached_compilation(cache::Dict{UInt64, Any}, job::GPUCompiler.CompilerJob, compiler::typeof(CUDA.cufunction_compile), linker::typeof(CUDA.cufunction_link))
    @ GPUCompiler ~/.julia/packages/GPUCompiler/HeCT6/src/cache.jl:90
  [8] cufunction(f::typeof(Cassette.overdub), tt::Type{Tuple{Cassette.Context{nametype(CUDACtx), Nothing, Nothing, KernelAbstractions.var"##PassType#274", Nothing, Cassette.DisableHooks}, typeof(gpu_f_test_kernel!), KernelAbstractions.CompilerMetadata{KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.DynamicCheck, Nothing, CartesianIndices{1, Tuple{Base.OneTo{Int64}}}, KernelAbstractions.NDIteration.NDRange{1, KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.StaticSize{(256,)}, CartesianIndices{1, Tuple{Base.OneTo{Int64}}}, Nothing}}, CuDeviceVector{Int32, 1}, Type{Int32}}}; name::String, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ CUDA ~/projects/CUDA.jl/src/compiler/execution.jl:297
  [9] macro expansion
    @ ~/projects/CUDA.jl/src/compiler/execution.jl:102 [inlined]
 [10] (::KernelAbstractions.Kernel{CUDADevice, KernelAbstractions.NDIteration.StaticSize{(256,)}, KernelAbstractions.NDIteration.DynamicSize, typeof(gpu_f_test_kernel!)})(::CuArray{Int32, 1, CUDA.Mem.DeviceBuffer}, ::Vararg{Any}; ndrange::Int64, dependencies::CUDAKernels.CudaEvent, workgroupsize::Nothing, progress::Function)
    @ CUDAKernels ~/projects/KernelAbstractions.jl/lib/CUDAKernels/src/CUDAKernels.jl:194
 [11] f_test!(input::CuArray{Int32, 1, CUDA.Mem.DeviceBuffer}, T::Type; numcores::Int64, numthreads::Int64)
    @ Main ~/projects/simuleios/GPU/test15.jl:20
 [12] f_test!(input::CuArray{Int32, 1, CUDA.Mem.DeviceBuffer}, T::Type)
    @ Main ~/projects/simuleios/GPU/test15.jl:14
 [13] top-level scope
    @ REPL[16]:1
 [14] top-level scope
    @ ~/projects/CUDA.jl/src/initialization.jl:52

Note that it is possible to use types as arguments for CUDA.jl, which is why I made the issue. I might have missed something, though!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the MWE with the CPU and GPU paths, then start at the CUDAKernels invocation shown at CUDAKernels.jl:194 and inspect the GPU compilation path for Type{Int32} arguments. Compare this behavior with the CUDA.jl case mentioned in the report. Done means the GPU kernel accepts the type argument, compiles without InvalidIRError, and produces the expected output.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
hpc
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.