我在调试下面的代码,想给以前的代码做做性能优化:
using BenchmarkTools
function discrete_integrate(xvec::Vector, y1vec::Vector, y2vec::Vector, y3vec::Vector)
s = zero(promote_type(eltype(xvec), eltype(y1vec), eltype(y2vec), eltype(y3vec)))
for i in 1:length(xvec)-1
s += (y1vec[i+1] * y2vec[i+1] * y3vec[i+1] + y1vec[i] * y2vec[i] * y3vec[i]) * (xvec[i+1] - xvec[i])
end
0.5 * s
end
function funF1(matSlωJ::Array{ComplexF64,3}, Zvec::Vector, ωvec::Vector, nJ::Int64, lm::Int64)
resmat = zeros(ComplexF64, nJ, nJ, 2lm + 1, 2lm + 1)
@sync for j1 = 1:nJ, j2 = 1:nJ
Threads.@spawn begin
for l1 = -lm:lm, l2 = -lm:lm
idx1 = lm + l1 + 1
idx2 = lm + l2 + 1
resmat[j1, j2, idx1, idx2] = discrete_integrate(ωvec, Zvec, conj.(matSlωJ[idx1, :, j1]), matSlωJ[idx2, :, j2])
end
end
end
resmat
end
function funF2(matSlωJ::Array{ComplexF64,3}, Zvec::Vector, ωvec::Vector, nJ::Int64, lm::Int64)
resmat = zeros(ComplexF64, nJ, nJ, 2lm + 1, 2lm + 1)
@sync for l1 = -lm:lm, l2 = -lm:lm
idx1 = lm + l1 + 1
idx2 = lm + l2 + 1
Threads.@spawn for j1 = 1:nJ, j2 = 1:nJ
resmat[j1, j2, idx1, idx2] = discrete_integrate(ωvec, Zvec, conj.(matSlωJ[idx1, :, j1]), matSlωJ[idx2, :, j2])
end
end
resmat
end
function funF3(matSlωJ::Array{ComplexF64,3}, Zvec::Vector, ωvec::Vector, nJ::Int64, lm::Int64)
resmat = zeros(ComplexF64, nJ, nJ, 2lm + 1, 2lm + 1)
nω = length(ωvec)
@sync for l1 = -lm:lm, l2 = -lm:lm
idx1 = lm + l1 + 1
idx2 = lm + l2 + 1
Threads.@spawn for j1 = 1:nJ, j2 = 1:nJ
s = zero(ComplexF64)
for i in 1:(nω-1)
tmpy1 = Zvec[i] * conj.(matSlωJ[idx1, i, j1]) * matSlωJ[idx2, i, j2]
tmpy2 = Zvec[i+1] * conj.(matSlωJ[idx1, i+1, j1]) * matSlωJ[idx2, i+1, j2]
s += (tmpy1 + tmpy2) * (ωvec[i+1] - ωvec[i])
end
resmat[j1, j2, idx1, idx2] = 0.5 * s
end
end
resmat
end
matS = rand(ComplexF64, 5, 1000, 60);
impedance = rand(1000);
ωvec = collect(range(0.0, step=0.01, length=1000));
# @benchmark funF2($matS, $impedance, $ωvec, 60, 2)
# @benchmark funF3($matS, $impedance, $ωvec, 60, 2)
我运行@benchmark funF2($matS, $impedance, $ωvec, 60, 2)
没事,
一运行@benchmark funF3($matS, $impedance, $ωvec, 60, 2)
,就肯定黑屏,同时插在机箱后面的外设都断电了。插在机箱前面面板的硬盘指示灯、内存条RGB灯、内部风扇都是在工作的。
而且,此时按关机键是不能关机的。
但是电源开关关掉后立马重新打开,它自动开机。
在不同机子测试过了,别的机子不关机,所以不是代码的问题。
在同一机子用不同julia版本测试过了,照样黑屏,所以不是julia版本有BUG的问题。
那是哪个硬件的问题呢?我写的funF3
给谁带来了压力了吗?
这是我电脑配置
内存条金手指我擦过了,没用。
funF3
是造成黑屏死机的罪魁祸首。但有趣的是,我取消funF3
多线程就没这个问题了。
不理解。funF2
还要在内存中复制数组,内存吞吐量更大;funF3
都不用复制。难道不是内存问题,是CPU坏了?