gpt4 book ai didi

Type-stability in Julia for creating SVector with ::Function(使用::函数创建SVector时Julia中的类型稳定性)

转载 作者:bug小助手 更新时间:2023-10-24 21:36:36 33 4
gpt4 key购买 nike



I'm trying to figure out the proper way to write code in a type-stable manner.
I know that some limitations, such as ::Function and ::Val{} arguments are an issue, and so I'm trying to figure out the best way of going around those.
I'm still deep into the learning phase, so please feel free to provide as much information and comments as you feel like.

我正在试图找出以类型稳定的方式编写代码的正确方法。我知道一些限制,如::Function和::val{}参数是一个问题,所以我正在尝试找出绕过这些限制的最佳方法。我仍在深入学习阶段,所以请随时提供您想要的信息和评论。


As part of that, I was trying to create an SVector that is type-stable, however I keep getting it wrong. here is a MWE (and pardon the stupid names for functions and variables, I tried to maitain the actual structure of the code while removing meaningful content), where the output is SVector{_A, Float64} where _A. How do I get a proper SVector here? Feel free to modify the code as you please, as long as the function order and structure remains.

作为其中的一部分,我试图创建一个类型稳定的SVector,但我总是弄错。这里有一个MWE(请原谅函数和变量的愚蠢名称,我试图在删除有意义的内容的同时保持代码的实际结构),其中输出是SVector{_A,Float64}where_A。我如何在这里获得一个合适的SVector?只要函数顺序和结构保持不变,您可以随意修改代码。


Supporting functions:

支持功能:


using StaticArrays
@inline function HyperTanDistribSymm(α::T,Lx::T=one(T)) where T<:AbstractFloat
return x -> convert(T,0.5)*Lx*( one(T) - tanh( α*(one(T)-convert(T,2.0)*x) ) / tanh(α) )
end

function GenerateVec(L::TF, indxrng::AbstractUnitRange{TI}, ntotal::TI=length(indxrng), Stretchfun::Function=x::TF->x::TF, nghost::TI=1) where {TI<:Integer, TF<:AbstractFloat}
nterms::TI=2*nghost+length(indxrng)+one(TI)
return SVector{nterms,TF}( map( Stretchfun,L*convert.(TF, indxrng[1]-one(TI)-nghost:indxrng[end]+nghost)/convert(TF, ntotal) ) )
end


function madness(varD::TF, indx::AbstractUnitRange{TI}, varN::TI, stretch::Function, nghost::TI) where {TF<:AbstractFloat, TI<:Integer}
testvar=GenerateVec(varD, indx, varN, stretch, nghost)
nterms::TI=length(testvar)-one(TI)
diffvar=SVector{nterms,TF}( diff(testvar) )
end

And running examples:

和运行示例:


using Cthulhu
@descend_code_warntype madness(1.0, 1:6, 6, x->x, 1)
@descend_code_warntype madness(2.0, 2:8, 10, HyperTanDistribSymm(2.20,6.0), 2)

as GenerateVec returns SVector{nterms::Int64,TF::Type{Float64}}::Type{SVector{_A, Float64}} where _A.
I will comment that I tried other versions, including the use of Val approach on something on the lines of:

当GenerateVec返回SVector{nTerms::Int64,tf::type{Float64}}::type{SVector{_A,Float64}}where_A时,我会评论说我尝试过其他版本,包括对以下代码行的内容使用Val方法:


@inline CreateSVector(input::Base.AbstractVecOrTuple{T}) where {T<:Number} = CreateSVector(Val(length(input)), input)
@inline CreateSVector(::Val{N}, input::Base.AbstractVecOrTuple{T}) where {N,T<:Number} = SVector{N,T}( input )

this is just one example, I also tried using N just as a regular N<:Integer input and such. All result in the same issue.

这只是一个例子,我还尝试使用N作为常规的N<:整数输入等等。所有这些都会导致相同的问题。


Note: I have read around and figured that possibly the use of Tuples pass arguments and set sizes might be a solution - if that is the case, why?

注意:我已经仔细阅读过,并认为使用元组传递参数和集大小可能是一种解决方案--如果是这样,为什么?


更多回答

Just as a side comment: if you see various conversions that seem out-of-place, that is me trying to verify that everything else is type-stable and not causing issues. Originally they were not there (as it was seen as type-stable)

顺便说一句:如果您看到各种转换看起来不合时宜,那就是我在尝试验证其他所有内容都是类型稳定的,并且不会造成问题。最初它们不在那里(因为它被视为类型稳定)

Yeah, it's a bit more than necessary. You could do Lx * (1 - tanh(α*(1 - 2*x))/ 2tanh(α). If you are concerned about unwanted float promotion, just use integers.

是的,这比必要的要多一点。你可以做lx*(1-tanh(α*(1-2*x))/2tanh(α))。如果您担心不需要的浮点数提升,只需使用整数即可。

There's a bit much going on to analyze on a phone, but what will definitely not work is SVector{nterms,TF} where nterms is a runtime value. It must be a compile time constant to achieve type stability.

在电话上要分析的内容有点多,但肯定不会起作用的是SVector{nTerms,Tf},其中nTerms是一个运行时值。它必须是编译时间常量才能实现类型稳定性。

As DNF said, SVectors are for compile-time length known vectors. If the SVector casts are removed, the functions become type-stable (just checked this).

正如DNF所说,SVectors用于编译时长度已知的向量。如果删除了SVector强制转换,函数就会变成类型稳定的(刚刚检查过了)。

@DanGetz, the thing is, I want it to end up as an SVector for other math operations later. These used to be Tuples originally (and I think it was type-stable then), but since the output is continuously used I want the benefit of the LinearAlegbra implemented operations. I guess I can make this an MVector, if that helps, but I see no point in doing so.

@DanGetz,问题是,我希望它在以后作为其他数学运算的SVector结束。它们最初是元组(我认为它当时是类型稳定的),但由于输出是连续使用的,所以我希望从LinearAlegbra实现的操作中受益。如果有帮助的话,我想我可以将其设置为MVector,但我认为这样做没有任何意义。

优秀答案推荐

You might find a text I wrote useful in your quest for understanding type stability:
https://www.juliabloggers.com/writing-type-stable-julia-code/

您可能会发现我写的一篇文章对您了解类型稳定性很有用:https://www.juliabloggers.com/writing-type-stable-julia-code/


But the short version is as follows:
a) the type of a StaticVector depends on its length.
b) type stability means that types (and so StaticVectors') length are defined at compile time
c) so the length must be either hard coded, or be given as a type parameter. Examples, where the parameter is called VecLen:

但简短的版本如下:a)StaticVector的类型取决于它的长度。B)类型稳定性意味着类型(以及StaticVectors)长度是在编译时定义的c),因此长度必须是硬编码的,或者作为类型参数给出。示例,其中该参数称为VecLen:


... ::Val{VecLen}}...) where {VecLen} 

or


... v::StaticArray{VecLen,Float64}...) where {VecLen}

You want type stability 'cause you want speed 'cause you're in a loop. If you can compute VecLen outside the loop (see the concept of barrier function) and pass it down to your function (using Val, or other technique) you win.

你想要类型稳定性,因为你想要速度,因为你在循环中。如果你能在循环外计算VecLen(参见屏障函数的概念)并将其传递给你的函数(使用val或其他技术),你就赢了。


If you cannot precompute VecLen (e.g. because it changes at each iteration) then we are busted, you will not get type stability. You would then StaticArrays are not for you today.

如果您不能预先计算VecLen(例如,因为它在每次迭代时都会改变),那么我们就失败了,您将无法获得类型稳定性。那么你就会发现StaticArray今天不适合你。


I hope this helps!

我希望这能帮到你!


: )

:)


Philippe

菲利普


更多回答

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com