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

real_arr_type not type stable #12

Open
roflmaostc opened this issue Feb 9, 2023 · 5 comments
Open

real_arr_type not type stable #12

roflmaostc opened this issue Feb 9, 2023 · 5 comments

Comments

@roflmaostc
Copy link
Member

roflmaostc commented Feb 9, 2023

Where did you need those functions?

julia> real_arr_type(Array{ComplexF64}, dims=2)
Matrix{Float64} (alias for Array{Float64, 2})

julia> real_arr_type(Array{ComplexF64}, dims=3)
Array{Float64, 3}

julia> @code_warntype real_arr_type(Array{ComplexF64}, dims=3)
MethodInstance for (::NDTools.var"#real_arr_type##kw")(::NamedTuple{(:dims,), Tuple{Int64}}, ::typeof(real_arr_type), ::Type{Array{ComplexF64}})
  from (::NDTools.var"#real_arr_type##kw")(::Any, ::typeof(real_arr_type), ::Type{TA}) where TA<:AbstractArray in NDTools at /home/fxw/.julia/dev/NDTools.jl/src/type_tools.jl:27
Static Parameters
  TA = Array{ComplexF64}
Arguments
  _::Core.Const(NDTools.var"#real_arr_type##kw"())
  @_2::NamedTuple{(:dims,), Tuple{Int64}}
  @_3::Core.Const(NDTools.real_arr_type)
  @_4::Type{Array{ComplexF64}}
Locals
  dims::Int64
  @_6::Int64
Body::DataType
1%1  = Base.haskey(@_2, :dims)::Core.Const(true)
│         Core.typeassert(%1, Core.Bool)
│         (@_6 = Base.getindex(@_2, :dims))
└──       goto #3
2 ─       Core.Const(:(@_6 = 1))
3%6  = @_6::Int64
│         (dims = %6)
│   %8  = (:dims,)::Core.Const((:dims,))
│   %9  = Core.apply_type(Core.NamedTuple, %8)::Core.Const(NamedTuple{(:dims,)})
│   %10 = Base.structdiff(@_2, %9)::Core.Const(NamedTuple())
│   %11 = Base.pairs(%10)::Core.Const(Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}())
│   %12 = Base.isempty(%11)::Core.Const(true)
│         Core.typeassert(%12, Core.Bool)
└──       goto #5
4 ─       Core.Const(:(Base.kwerr(@_2, @_3, @_4)))
5%16 = NDTools.:(var"#real_arr_type#2")(dims, @_3, @_4)::DataType
└──       return %16
@RainerHeintzmann
Copy link
Member

@roflmaostc
Copy link
Member Author

In this specific case you could also pass arr and call inside rr2_sep:

similar(arr, real(eltype(arr)), ...)

@roflmaostc
Copy link
Member Author

similar also works for CUDA arrays, that's the point

@RainerHeintzmann
Copy link
Member

Yup. But the point is, that there are functions (like rr2_sep) which request an Array type as input and its good to have a way to create such a type.

@roflmaostc
Copy link
Member Author

But as I said, it needs to perform the runtime dispatch and hence can cause performance issues:

Something like this would be better:

ulia> function similar_arr_type_4(::Type{TA}, ::Type{ET}, dims=Val(2)) where {TA, ET}
           typeof(similar(TA{ET}, ntuple(x->0, dims)))
       end
similar_arr_type_4 (generic function with 2 methods)

julia> similar_arr_type_4(Array, Float64)
Matrix{Float64} (alias for Array{Float64, 2})

julia> similar_arr_type_4(Array, Float64)^C

julia> @code_warntype similar_arr_type_4(Array, Float64)
MethodInstance for similar_arr_type_4(::Type{Array}, ::Type{Float64})
  from similar_arr_type_4(::Type{TA}, ::Type{ET}) where {TA, ET} in Main at REPL[47]:1
Static Parameters
  TA = Array
  ET = Float64
Arguments
  #self#::Core.Const(similar_arr_type_4)
  @_2::Type{Array}
  @_3::Core.Const(Float64)
Body::Type{Matrix{Float64}}
1%1 = Main.Val(2)::Core.Const(Val{2}())
│   %2 = (#self#)(@_2, @_3, %1)::Core.Const(Matrix{Float64})
└──      return %2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants