Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rationals behave badly #118

Open
dpsanders opened this issue Sep 25, 2019 · 4 comments
Open

Rationals behave badly #118

dpsanders opened this issue Sep 25, 2019 · 4 comments
Labels
bug Something isn't working cuda array Stuff about CuArray.

Comments

@dpsanders
Copy link

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)
@maleadt
Copy link
Member

maleadt commented Sep 25, 2019

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 invokelatest in throw_overflow_binaryop?
  • the call to __throw_gcd_overflow is performed dynamically, not sure why (it's not marked @nospecialize or 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.

@dpsanders
Copy link
Author

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).

@maleadt maleadt transferred this issue from JuliaGPU/CuArrays.jl May 27, 2020
@maleadt maleadt added bug Something isn't working cuda array Stuff about CuArray. labels May 27, 2020
@wsshin
Copy link

wsshin commented Jun 21, 2024

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:
...

@maleadt
Copy link
Member

maleadt commented Jun 21, 2024

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 #2403

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cuda array Stuff about CuArray.
Projects
None yet
Development

No branches or pull requests

3 participants