Rationals behave badly
Rationals do not behave well inside CuArrays.
Not sure there's much to do about this, except implement a simpler Rational type:
julia> v = CuArray([1//3])
1-element CuArray{Rational{Int64},1}:
1//3
julia> v .+ v
ERROR: InvalidIRError: compiling JuliaGPU/CuArrays.jl#23(CuArrays.CuKernelState, CUDAnative.CuDeviceArray{Rational{Int64},1,CUDAnative.AS.Global}, Base.Broadcast.Broadcasted{Nothing,Tuple{Base.OneTo{Int64}},typeof(+),Tuple{Base.Broadcast.Extruded{CUDAnative.CuDeviceArray{Rational{Int64},1,CUDAnative.AS.Global},Tuple{Bool},Tuple{Int64}},Base.Broadcast.Extruded{CUDAnative.CuDeviceArray{Rational{Int64},1,CUDAnative.AS.Global},Tuple{Bool},Tuple{Int64}}}}) resulted in invalid LLVM IR
Reason: unsupported call to the Julia runtime (call to jl_f__apply_latest)
The stacktraces are actually quite revealing:
Reason: unsupported call to the Julia runtime (call to jl_f__apply_latest)
Stacktrace:
[1] #invokelatest#1 at essentials.jl:709
[2] invokelatest at essentials.jl:708
[3] throw_overflowerr_binaryop at checked.jl:154
[4] multiple call sites at unknown:0
Reason: unsupported dynamic function invocation (call to __throw_gcd_overflow(a, b) in Base at intfuncs.jl:50)
Stacktrace:
[1] gcd at intfuncs.jl:47
[2] divgcd at rational.jl:26
[3] multiple call sites at unknown:0
Reason: unsupported dynamic function invocation (call to __throw_rational_argerror(T) in Base at rational.jl:19)
Stacktrace:
[1] Rational at rational.jl:14
[2] Rational at rational.jl:21
[3] + at rational.jl:258
[4] _broadcast_getindex_evalf at broadcast.jl:630
[5] _broadcast_getindex at broadcast.jl:603
[6] getindex at broadcast.jl:563
[7] JuliaGPU/CuArrays.jl#23 at /home/tim/Julia/pkg/GPUArrays/src/broadcast.jl:50
All exception related:
- there's an
invokelatestinthrow_overflow_binaryop? - the call to
__throw_gcd_overflowis performed dynamically, not sure why (it's not marked@nospecializeor anything). maybe because of the string interpolation? - same with
__throw_rational_argerror
So the type itself is fine, it's just that we have a hard time lowering the exception paths. CUDAnative already contains quite some hacks to deal with such functions, but that's for the actual throwing (i.e. it shouldn't be dynamically dispatching). The invokelatest is unsupported of course.
Unfortunately the nastiness happens in the inner constructor of Rational, so you can't even construct them correctly in my experience (even though I managed it above -- not sure what's going on).
A related observation. CuArray{Rational{Int64}} supports some primitive arithmetic operations, but not all:
julia> v = CuVector{Rational{Int64}}([1//2, 2//3, 3//4])
3-element CuArray{Rational{Int64}, 1, CUDA.DeviceMemory}:
1//2
2//3
3//4
julia> v .+ 1
3-element CuArray{Rational{Int64}, 1, CUDA.DeviceMemory}:
3//2
5//3
7//4
julia> v .- 1
3-element CuArray{Rational{Int64}, 1, CUDA.DeviceMemory}:
-1//2
-1//3
-1//4
julia> v .* 2
3-element CuArray{Rational{Int64}, 1, CUDA.DeviceMemory}:
1
4//3
3//2
julia> v .^ 2
3-element CuArray{Rational{Int64}, 1, CUDA.DeviceMemory}:
1//4
4//9
9//16
julia> v ./ 2
ERROR: InvalidIRError: compiling MethodInstance for (::GPUArrays.var"#34#36")(::CUDA.CuKernelContext, ::CuDeviceVector{…}, ::Base.Broadcast.Broadcasted{…}, ::Int64) resulted in invalid LLVM IR
Reason: unsupported dynamic function invocation (call to __throw_rational_argerror_typemin(T) @ Base rational.jl:20)
Stacktrace:
...
julia> v .// 2
ERROR: InvalidIRError: compiling MethodInstance for (::GPUArrays.var"#34#36")(::CUDA.CuKernelContext, ::CuDeviceVector{…}, ::Base.Broadcast.Broadcasted{…}, ::Int64) resulted in invalid LLVM IR
Reason: unsupported dynamic function invocation (call to __throw_rational_argerror_typemin(T) @ Base rational.jl:20)
Stacktrace:
...
julia> v .+ v
ERROR: InvalidIRError: compiling MethodInstance for (::GPUArrays.var"#34#36")(::CUDA.CuKernelContext, ::CuDeviceVector{…}, ::Base.Broadcast.Broadcasted{…}, ::Int64) resulted in invalid LLVM IR
Reason: unsupported dynamic function invocation (call to __throw_rational_argerror_zero(T) @ Base rational.jl:32)
Stacktrace:
...
julia> v ./ 2 ERROR: InvalidIRError: compiling MethodInstance for (::GPUArrays.var"#34#36")(::CUDA.CuKernelContext, ::CuDeviceVector{…}, ::Base.Broadcast.Broadcasted{…}, ::Int64) resulted in invalid LLVM IR Reason: unsupported dynamic function invocation (call to __throw_rational_argerror_typemin(T) @ Base rational.jl:20) Stacktrace: ... julia> v .// 2 ERROR: InvalidIRError: compiling MethodInstance for (::GPUArrays.var"#34#36")(::CUDA.CuKernelContext, ::CuDeviceVector{…}, ::Base.Broadcast.Broadcasted{…}, ::Int64) resulted in invalid LLVM IR Reason: unsupported dynamic function invocation (call to __throw_rational_argerror_typemin(T) @ Base rational.jl:20) Stacktrace: ... julia> v .+ v ERROR: InvalidIRError: compiling MethodInstance for (::GPUArrays.var"#34#36")(::CUDA.CuKernelContext, ::CuDeviceVector{…}, ::Base.Broadcast.Broadcasted{…}, ::Int64) resulted in invalid LLVM IR Reason: unsupported dynamic function invocation (call to __throw_rational_argerror_zero(T) @ Base rational.jl:32) Stacktrace:
These have been fixed by https://github.com/JuliaGPU/CUDA.jl/pull/2403
All the examples in OP and the subsequent comment work for me now, I think this can be closed. Feel free to re-open if you find another example!