【问题标题】:Integration of interaction tensor交互张量的积分
【发布时间】:2021-01-05 06:10:18
【问题描述】:

每个时间步我需要计算几千次以下积分:

和:

到目前为止,我已经在 J​​ulia 中实现了:

using StaticArrays

function interactiontensor(C, a1, a2, a3, ϕ, θ)

n1,n2   = 100,50
T       = fill(0.0,3,3,3,3)
Av      = zeros(4,4)
invAv   = similar(Av)
xi      = Vector{Float64}(undef, 3)


@inbounds  for p ∈ 1:n1        

    sinθp = sind(θ[p])
    cosθp = cosd(θ[p])
    for q ∈ 1:n2

        sinϕq  = sind(ϕ[q])
        cosϕq  = cosd(ϕ[q])            
        
        # -- Director cosines
        xi[1]  = sinθp*cosϕq/a1
        xi[2]  = sinθp*sinϕq/a2
        xi[3]  = cosθp/a3

        Christoffel!(Av,C,xi)
        fillAv!(Av, xi)

        invAv = inv(SMatrix{4,4}(Av))

        tensorT!(T,invAv,xi,sinθp)

        surface += sinθp
    end
end

return T ./= surface
end

@inline function Christoffel!(Av,C,xi)
@inbounds for t ∈ 1:3, r ∈ 1:3
    aux = zero(eltype(C))
    for u ∈ 1:3, s ∈ 1:3
        aux += C[r, s, t, u] * xi[s] * xi[u]        
    end
    Av[r, t] = aux
end
end

@inline function tensorT!(T,invAv,xi,sinθp)
@inbounds for k ∈ 1:3, i ∈ 1:3
    aux = invAv[i, k]
    for l ∈ 1:3, j ∈ 1:3
        T[i, j, k, l] += aux * xi[j] * xi[l] * sinθp
    end
end
end

@inline function fillAv!(Av, xi)
@inbounds for i ∈ 1:3
    xi0      = xi[i]
    Av[i, 4] = xi0
    Av[4, i] = xi0
end
end

n1,n2 = 100,100
step    = π/n1
dθ,dϕ   = π/n1, 2π/n2
θ       = rad2deg.(range(dθ, stop = pi,  length = n1))
ϕ       = rad2deg.(range(dϕ, stop = 2pi, length = n2))
C = @SArray rand(3,3,3,3)
@btime interactiontensor($C, $10.0, $5.0, $1.0, $ϕ, $θ);
# 544.795 μs  (4 allocations: 1.08 KiB)

考虑到理想情况下我需要计算这个积分的次数,我的实现是否有任何优化或替代方法,以显着降低计算成本?

【问题讨论】:

标签: julia integration


【解决方案1】:

以下是一些建议:

  • sinθp, cosθp = sincosd(θ[p]),即一步计算正弦和余弦。
  • xi = @SVector zeros(3)初始化为静态向量,然后使用Setfield.jl在每次迭代中分配值,即@set x[1] = sinθp*cosϕq/a1
  • 加载包LoopVectorization.jl 并使用@avx 宏(大致类似于@simd)来加速Christoffel!tensorT!fillAv! 中的循环。

在我的机器上,我发现这些更改将计算时间减少了 5 倍以上(相对于 OP 中的原始函数)。最大的一块是由于@avx,上面的第二点大约是~30%。

julia> @btime interactiontensor_original($C, $10.0, $5.0, $1.0, $ϕ, $θ);
  661.655 μs (5 allocations: 1.28 KiB)

julia> @btime interactiontensor_optimized($C, $10.0, $5.0, $1.0, $ϕ, $θ);
  125.352 μs (4 allocations: 1.17 KiB)

这里是完整的修改代码(请注意,我注释掉了涉及surface 的行,这在 OP 中没有指定):

using StaticArrays, Setfield, LoopVectorization

function interactiontensor_optimized(C, a1, a2, a3, ϕ, θ)

    n1,n2   = 100,50
    T       = fill(0.0,3,3,3,3)
    Av      = zeros(4,4)
    invAv   = similar(Av)
    xi      = @SVector zeros(3)


    @inbounds for p ∈ 1:n1

        sinθp, cosθp = sincosd(θ[p])
        for q ∈ 1:n2

            sinϕq, cosϕq  = sincosd(ϕ[q])

            # -- Director cosines
            @set xi[1]  = sinθp*cosϕq/a1
            @set xi[2]  = sinθp*sinϕq/a2
            @set xi[3]  = cosθp/a3

            Christoffel!(Av,C,xi)
            fillAv!(Av, xi)

            invAv = inv(SMatrix{4,4}(Av))

            tensorT!(T,invAv,xi,sinθp)

            # surface += sinθp
        end
    end

    return T #./= surface
end

@inline function Christoffel!(Av,C,xi)
    @avx for t ∈ 1:3, r ∈ 1:3
        aux = zero(eltype(C))
        for u ∈ 1:3, s ∈ 1:3
            aux += C[r, s, t, u] * xi[s] * xi[u]
        end
        Av[r, t] = aux
    end
end

@inline function tensorT!(T,invAv,xi,sinθp)
    @avx for k ∈ 1:3, i ∈ 1:3
        aux = invAv[i, k]
        for l ∈ 1:3, j ∈ 1:3
            T[i, j, k, l] += aux * xi[j] * xi[l] * sinθp
        end
    end
end

@inline function fillAv!(Av, xi)
    @avx for i ∈ 1:3
        xi0      = xi[i]
        Av[i, 4] = xi0
        Av[4, i] = xi0
    end
end

【讨论】:

  • 仅供参考,@inbounds @avx 是多余的。 @avx 中的所有数组访问都已隐式入站。它会禁用所有安全功能
  • 谢谢,很高兴知道!我会更新我的答案。
猜你喜欢
  • 2021-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-14
  • 2019-01-07
  • 2016-02-24
  • 2019-11-26
相关资源
最近更新 更多