4D张量旋转的优化

问题描述 投票:0回答:1

我必须在Stokes解算器中按时间步长旋转3x3x3x3 4D张量+ 100k次,其中旋转的4D张量为Crot [i,j,k,l] = Crot [i,j,k,l ] + Q [m,i] * Q [n,j] * Q [o,k] * Q [p,l] * C [m,n,o,p],所有索引从1到3。 >

到目前为止,我已经很天真地用Julia编写了以下代码:

Q    = rand(3,3)
C    = rand(3,3,3,3)
Crot = Array{Float64}(undef,3,3,3,3)
function rotation_4d!(Crot::Array{Float64,4},Q::Array{Float64,2},C::Array{Float64,4})
aux = 0.0
for i = 1:3
    for j = 1:3
        for k = 1:3
            for l = 1:3

                for m = 1:3
                    for n = 1:3
                        for o = 1:3
                            for p = 1:3                                     
                                aux += Q[m,i] * Q[n,j] * Q[o,k] * Q[p,l] * C[m,n,o,p];
                            end
                        end
                    end
                end

                Crot[i,j,k,l] += aux

            end
        end
    end
end

end

使用:

@btime rotation_4d(Crot,Q,C)
14.255 μs (0 allocations: 0 bytes)

有什么方法可以优化代码?

我必须在Stokes解算器中按时间步长旋转3x3x3x3 4D张量+ 100k次,其中旋转的4D张量为Crot [i,j,k,l] = Crot [i,j,k,l ] + Q [m,i] * Q [n,j] * Q [o,k] * Q [p,l] * C [...

arrays math julia tensor
1个回答
0
投票

您在此处进行了大量索引编制,因此进行了很多边界检查。一种节省时间的方法是使用@inbounds宏,该宏会关闭边界检查。将代码重写为:

© www.soinside.com 2019 - 2024. All rights reserved.