this is a very simple problem, so i fear i’m making a dumb mistake. apologies if so. but i can’t figure out why the function foo
below is type unstable when input with a struct that has a CuVector
in it, whereas if it’s a regular Vector
it is fine. thx.
julia> using CUDA
julia> struct Foo
x::Vector{Float64}
end
julia> foo = Foo(ones(3))
Foo([1.0, 1.0, 1.0])
julia> struct CuFoo
x::CuVector{Float64}
end
julia> cu_foo = CuFoo(ones(3))
CuFoo([1.0, 1.0, 1.0])
julia> function fun(s)
s.x .= 0.0
end
fun (generic function with 1 method)
julia> @code_warntype fun(foo) # everything here is type stable
MethodInstance for fun(::Foo)
from fun(s) in Main at REPL[6]:1
Arguments
#self#::Core.Const(fun)
s::Foo
Body::Vector{Float64}
1 ─ %1 = Base.dotgetproperty(s, :x)::Vector{Float64}
│ %2 = Base.broadcasted(Base.identity, 0.0)::Core.Const(Base.Broadcast.Broadcasted(identity, (0.0,)))
│ %3 = Base.materialize!(%1, %2)::Vector{Float64}
└── return %3
julia> @code_warntype fun(cu_foo) # this is NOT type stable :(
MethodInstance for fun(::CuFoo)
from fun(s) in Main at REPL[6]:1
Arguments
#self#::Core.Const(fun)
s::CuFoo
Body::CuArray{Float64, 1} ### RED RED RED
1 ─ %1 = Base.dotgetproperty(s, :x)::CuArray{Float64, 1} ### RED RED RED
│ %2 = Base.broadcasted(Base.identity, 0.0)::Core.Const(Base.Broadcast.Broadcasted(identity, (0.0,)))
│ %3 = Base.materialize!(%1, %2)::CuArray{Float64, 1} ### RED RED RED
└── return %3
julia> using MethodAnalysis
julia> methodinstances(fun)
2-element Vector{Core.MethodInstance}:
MethodInstance for fun(::Foo)
MethodInstance for fun(::CuFoo)
this is with julia 1.8.5 and CUDA.jl v4.1.4.