DoubleFloats.jl icon indicating copy to clipboard operation
DoubleFloats.jl copied to clipboard

Length of Double32 range is BigInt

Open pepijndevos opened this issue 1 year ago • 2 comments

In Julia 1.8

julia> length(1:Double32(0.1):10) |> typeof
Int64

In Julia 1.10

julia> length(1:Double32(0.1):10) |> typeof
BigInt

This is because of https://github.com/JuliaLang/julia/commit/ff6a3cf3aa91938d6e228ce44b253f1cd671817a which triggers

https://github.com/JuliaMath/DoubleFloats.jl/blob/c96cd63fa4ca550b26feb4729d2c7f6e98062b4e/src/type/convert.jl#L14

And note that this does not match Int64 behavior.

julia> convert(Integer, 1.0) |> typeof
Int64

julia> Base.convert(Integer, 1e200)
ERROR: InexactError: Int64(1.0e200)

pepijndevos avatar Mar 28 '23 14:03 pepijndevos

Do not use Double32! Float64 is better.

On Tue, Mar 28, 2023 at 10:16 AM Pepijn de Vos @.***> wrote:

In Julia 1.8

julia> length(1:Double32(0.1):10) |> typeof Int64

In Julia 1.10

julia> length(1:Double32(0.1):10) |> typeof BigInt

This is because of @.*** https://github.com/JuliaLang/julia/commit/ff6a3cf3aa91938d6e228ce44b253f1cd671817a which triggers

https://github.com/JuliaMath/DoubleFloats.jl/blob/c96cd63fa4ca550b26feb4729d2c7f6e98062b4e/src/type/convert.jl#L14

And note that this does not match Int64 behavior.

julia> convert(Integer, 1.0) |> typeof Int64

julia> Base.convert(Integer, 1e200) ERROR: InexactError: Int64(1.0e200)

— Reply to this email directly, view it on GitHub https://github.com/JuliaMath/DoubleFloats.jl/issues/171, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAM2VRRLMW5Y5CJFGZNPWPLW6LXELANCNFSM6AAAAAAWKUPRLM . You are receiving this because you are subscribed to this thread.Message ID: @.***>

JeffreySarnoff avatar Mar 30 '23 11:03 JeffreySarnoff

Double32 is actually really great for GPU where doubles are 64x slower than floats on consumer hardware. But that's a bit besides the point of this bug which happens regardless of number type.

pepijndevos avatar Mar 30 '23 20:03 pepijndevos

Double32 is actually really great for GPU where doubles are 64x slower than floats on consumer hardware. But that's a bit besides the point of this bug which happens regardless of number type.

Does Double32 have GPU support so far?

photor avatar Mar 16 '24 09:03 photor

As far as I remember, yes.

But the point of this bug is that length of a range is a BigInt, on both Double32 and Double64

pepijndevos avatar Mar 16 '24 11:03 pepijndevos

As far as I remember, yes.

But the point of this bug is that length of a range is a BigInt, on both Double32 and Double64

You are right, Double32 and Double64 can be put in GPU by Adapt.jl, which I experimented a little. Impressively, even Double64 on my GPU (NVIDIA 4060 Ti) is very fast. I tested multiplication of two 1000x1000 Double64 matrices on GPU, which is only 3x slower than that for Float64. In contrast, on CPU it is 300x slower. I have to say that matrix operations of DoubleFloats on CPU are quite suboptimal.

photor avatar Mar 17 '24 05:03 photor

I will investigate how to provide a length function over a range that does not go to BigInt. Re matrices and cpu, Blas does not work with DoubleFloats -- GenericLinearAlgebra is used. If you have a speed-up, let me know.

On Sun, Mar 17, 2024 at 1:06 AM photor @.***> wrote:

As far as I remember, yes.

But the point of this bug is that length of a range is a BigInt, on both Double32 and Double64

You are right, Double32 and Double64 can be put in GPU by Adapt.jl, which I experimented a little. Impressively, even Double64 on my GPU (NVIDIA 4060 Ti) is very fast. I tested multiplication of two 1000x1000 Double64 matrices on GPU, which is only 3x slower than that for Float64. In contrast, on CPU it is 300x slower. I have to say that matrix operations of DoubleFloats on CPU are quite suboptimal.

— Reply to this email directly, view it on GitHub https://github.com/JuliaMath/DoubleFloats.jl/issues/171#issuecomment-2002309517, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAM2VRX6FG6U2Y4U7OOT35TYYUQF3AVCNFSM6AAAAAAWKUPRLOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMBSGMYDSNJRG4 . You are receiving this because you commented.Message ID: @.***>

JeffreySarnoff avatar Mar 18 '24 15:03 JeffreySarnoff

How do you like this -- merge pending.

function convert(::Type{I}, x::DoubleFloat{T}) where {T<:IEEEFloat, I<:Integer}
    bi = trunc(BigInt, BigFloat(HI(x)) + BigFloat(LO(x)))
    abi = abs(bi)
    if abi <= typemax(Int)
        convert(Int, bi)
    elseif abi <= typemax(Int64)
        convert(Int64, bi)
    elseif abi <= typemax(Int128)
        convert(Int128, bi)
    else
        bi
    end
end

JeffreySarnoff avatar Mar 28 '24 05:03 JeffreySarnoff

It looks very type unstable

pepijndevos avatar Mar 28 '24 06:03 pepijndevos

really?

julia> @code_warntype(length(1:Double64(0.5):10))
MethodInstance for length(::StepRangeLen{Double64, Double64, Double64, Int64})
  from length(r::StepRangeLen) @ Base range.jl:769
Arguments
  #self#::Core.Const(length)
  r::StepRangeLen{Double64, Double64, Double64, Int64}
Body::Int64
1 ─ %1 = Base.getproperty(r, :len)::Int64
└──      return %1


julia> @code_warntype(length(1:Double64(0.5):10^20))
MethodInstance for length(::StepRangeLen{Double64, Double64, Double64, Int128})
  from length(r::StepRangeLen) @ Base range.jl:769
Arguments
  #self#::Core.Const(length)
  r::StepRangeLen{Double64, Double64, Double64, Int128}
Body::Int128
1 ─ %1 = Base.getproperty(r, :len)::Int128
└──      return %1

JeffreySarnoff avatar Mar 28 '24 08:03 JeffreySarnoff